Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add tree shaking
  • Loading branch information
morewings committed Sep 28, 2025
commit 10d9437e92554c39f9d01df667cd46b0842f4006
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"files": [
"dist"
],
"main": "./dist/index.umd.cjs",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.umd.cjs"
"require": "./dist/index.cjs"
}
},
"keywords": [
Expand Down
22 changes: 15 additions & 7 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,27 @@ export default defineConfig({
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, 'src/lib/index.ts'),
name: 'Library name',
// the proper extensions will be added
fileName: 'index',
fileName: (format, entryName) => {
if (entryName === 'src/lib/index') {
// Create entry file(s) inside the bundle
return `index.${format === 'es' ? 'js' : 'cjs'}`;
} else if (entryName.includes('node_modules')) {
// Organize external dependencies which included in the bundle
return `external/module.${format === 'es' ? 'js' : 'cjs'}`;
}
// Keep other modules in places
return `${entryName}.${format === 'es' ? 'js' : 'cjs'}`;
},
// Change bundle formats to ES Modules and commonJS.
// UMD bundle will not work with preserveModules:true
formats: ['es', 'cjs'],
},
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: external(),
output: {
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
react: 'React',
},
preserveModules: true,
},
},
},
Expand Down