This is a monorepo template for building a reusable library that supports both Vue and React components. It is set up using Vite for fast builds, TypeScript for type safety, and includes configuration for generating declaration files (.d.ts). The template is optimized for creating UI components that can be shared across different frontend frameworks.
- π Fast Builds with Vite: Vite is used as the build tool, providing fast build times for both Vue and React libraries.
- π¬ TypeScript Support: Type-safe development with automatic declaration file generation.
- π¦ ESM and UMD Bundles: Outputs both ES modules (
.es.js) and UMD bundles (.umd.js). - π Framework-Specific Bundles: Separate builds and types for Vue and React (
dist/vueanddist/react). - ποΈ Modular Design: Supports tree-shaking to minimize bundle sizes.
- Vue and React Library Template
Clone this repository to start developing your library:
git clone https://github.com/your-username/vue-and-react-library-template.git
cd vue-and-react-library-templateInstall the latest version of dependencies by running
npx npm-check-updates -u && npm iThe template includes two main modes:
- Vue Mode: Builds the library for Vue components.
- React Mode: Builds the library for React components.
Before using this template, make sure to replace all instances of "Library-Name" with your actual library name. This includes:
- Package.json: Update
name,description, and other relevant fields. - Vite Config: Replace
"LibraryName"invite.config.ts. - README.md: Write your description and instructions for using your library.
-
Build:
npm run build # Builds both Vue and React packages npm run build:vue # Builds Vue package only npm run build:react # Builds React package only
-
Clean:
npm run clean # Removes the dist folder -
Release Commands: Automated scripts are provided for versioning and GitHub releases using Conventional Changelog: Prerequisites: GitHub CLI installed
gh- Patch Release:
Updates patch version (e.g.,
1.0.0β1.0.1).npm run release:patch
- Minor Release:
Updates minor version (e.g.,
1.0.0β1.1.0).npm run release:minor
- Major Release:
Updates major version (e.g.,
1.0.0β2.0.0).npm run release:major
After building your library, the output will be in the dist folder, and after publishing, it will be available on npm with the following package names:
- Vue package:
libraryName/vue - React package:
libraryName/react
You can install and use your library as follows:
import { MyVueComponent } from 'libraryName/vue'; export default { components: { MyVueComponent } };
import { MyReactComponent } from 'libraryName/react'; function App() { return <MyReactComponent />; }
vue-and-react-library-template/
βββ dist/ # Build output
β βββ vue/ # Vue library bundle
β βββ react/ # React library bundle
βββ src/ # Source code
βββ βββ core/ # Shared core code
β βββ vue/ # Vue source code
β βββ react/ # React source code
βββ tsconfig.json # Main TypeScript config
βββ tsconfig.vue.json # Vue-specific TypeScript config
βββ tsconfig.react.json # React-specific TypeScript config
βββ vite.config.ts # Vite config for both Vue and React
βββ package.json
βββ README.md
The vite.config.ts is set up to detect the build mode (vue or react) and apply the respective configuration:
// ... vite.config.ts code ... export default defineConfig(({ mode }) => { const isVue = mode === 'vue'; const framework = isVue ? 'vue' : 'react'; const global = ( isVue ? { vue: 'Vue' } : { react: 'React', 'react/jsx-runtime': 'JSX' } ) as { [key: string]: string }; return { // ... vite config code ... }; });
If you only want to build a Vue or React version of your library and skip dual-mode support, you can simplify your Vite configuration as follows:
-
Remove React files and configurations:
- Delete the
src/react/directory. - Delete
tsconfig.react.json. - Delete any React-related dependencies from
package.jsonlike@vitejs/plugin-react,react, andreact-dom.
- Delete the
-
Update
vite.config.tsto a Vue-only configuration:
import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; import dts from 'vite-plugin-dts'; import { resolve } from 'path'; export default defineConfig({ plugins: [ vue(), dts({ tsconfigPath: './tsconfig.vue.json', outDir: 'dist/vue', include: ['src/vue/**/*', 'src/vue-entry.ts'], insertTypesEntry: true, cleanVueFileName: true, rollupTypes: true, entryRoot: 'src/' }) ], build: { target: 'es2015', copyPublicDir: false, outDir: 'dist/vue', lib: { entry: resolve(__dirname, 'src/vue-entry.ts'), name: 'LibraryName', formats: ['es', 'cjs', 'umd'], fileName: (format) => format === 'es' ? 'index.js' : `index.${format}.js` }, rollupOptions: { external: ['vue'], output: { exports: 'named', globals: { vue: 'Vue' } } } } });
-
Remove Vue files and configurations:
- Delete the
src/vue/directory. - Delete
tsconfig.vue.json. - Delete any Vue-related dependencies from
package.jsonlike@vitejs/plugin-vueandvue.
- Delete the
-
Update
vite.config.tsto a React-only configuration:
import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import dts from 'vite-plugin-dts'; import { resolve } from 'path'; export default defineConfig({ plugins: [ react(), dts({ tsconfigPath: './tsconfig.react.json', outDir: 'dist/react', include: ['src/react/**/*', 'src/react-entry.ts'], insertTypesEntry: true, cleanVueFileName: true, rollupTypes: true, entryRoot: 'src/' }) ], build: { target: 'es2015', copyPublicDir: false, outDir: 'dist/react', lib: { entry: resolve(__dirname, 'src/react-entry.ts'), name: 'LibraryName', formats: ['es', 'cjs', 'umd'], fileName: (format) => format === 'es' ? 'index.js' : `index.${format}.js` }, rollupOptions: { external: ['react', 'react/jsx-runtime'], output: { exports: 'named', globals: { react: 'React', 'react/jsx-runtime': 'JSX' } } } } });
tsconfig.json: Common settingstsconfig.vue.json: Vue-specific TypeScript settingstsconfig.react.json: React-specific TypeScript settings
for individual libraries, you can add the respective TypeScript configuration file (tsconfig.react.json for React or tsconfig.vue.json for Vue) to the main tsconfig.json file.
Install necessary dependencies based on your preferred setup:
If you want both Vue and React support:
npm install vue react react-dom \ @vitejs/plugin-vue @vitejs/plugin-react \ @types/vue @types/react @types/node \ typescript vite vite-plugin-dts generate-changelog
npm install vue @vitejs/plugin-vue @types/node typescript vite vite-plugin-dts generate-changelog
npm install react react-dom @vitejs/plugin-react @types/react @types/node typescript vite vite-plugin-dts generate-changelog
To build the project for both Vue and React, use:
npm run build
Or to build individually:
npm run build:vue npm run build:react
The compiled files will be available in:
dist/vue: For Vue componentsdist/react: For React components
Contributions are welcome! Please follow these steps:
- Fork this repository
- Create a feature branch (
feat/your-feature) - Commit your changes with descriptive messages (
feat: add new component) - Open a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Happy coding! π