Skip to content

Commit 9a0cfed

Browse files
committed
Add tree shaking
1 parent 76b6362 commit 9a0cfed

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"files": [
88
"dist"
99
],
10-
"main": "./dist/index.umd.cjs",
10+
"main": "./dist/index.cjs",
1111
"module": "./dist/index.js",
1212
"types": "./dist/index.d.ts",
1313
"sideEffects": false,
1414
"exports": {
1515
".": {
1616
"import": "./dist/index.js",
17-
"require": "./dist/index.umd.cjs"
17+
"require": "./dist/index.cjs"
1818
}
1919
},
2020
"keywords": [

vite.config.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,27 @@ export default defineConfig({
1919
// Could also be a dictionary or array of multiple entry points
2020
entry: resolve(__dirname, 'src/lib/index.ts'),
2121
name: 'Library name',
22-
// the proper extensions will be added
23-
fileName: 'index',
22+
fileName: (format, entryName) => {
23+
if (entryName === 'src/lib/index') {
24+
// Create entry file(s) inside the bundle
25+
return `index.${format === 'es' ? 'js' : 'cjs'}`;
26+
} else if (entryName.includes('node_modules')) {
27+
// Organize external dependencies which included in the bundle
28+
return `external/module.${format === 'es' ? 'js' : 'cjs'}`;
29+
}
30+
// Keep other modules in places
31+
return `${entryName}.${format === 'es' ? 'js' : 'cjs'}`;
32+
},
33+
// Change bundle formats to ES Modules and commonJS.
34+
// UMD bundle will not work with preserveModules:true
35+
formats: ['es', 'cjs'],
2436
},
2537
rollupOptions: {
2638
// make sure to externalize deps that shouldn't be bundled
2739
// into your library
2840
external: external(),
2941
output: {
30-
// Provide global variables to use in the UMD build
31-
// for externalized deps
32-
globals: {
33-
react: 'React',
34-
},
42+
preserveModules: true,
3543
},
3644
},
3745
},

0 commit comments

Comments
 (0)