Created
June 18, 2024 01:31
-
-
Save alexzhang1030/03a9f2a8d68cfbfcfd5d519d11d31c22 to your computer and use it in GitHub Desktop.
Custom bundle vue with treeshake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { resolve } from 'node:path' | |
import { defineConfig } from 'tsup' | |
export default defineConfig({ | |
entryPoints: [ | |
'src/index.ts', | |
], | |
noExternal: ['vue'], | |
clean: true, | |
format: ['esm', 'cjs'], | |
dts: true, | |
treeshake: 'smallest', | |
define: { | |
__VUE_OPTIONS_API__: 'false', | |
}, | |
shims: true, | |
esbuildPlugins: [ | |
{ | |
// https://vuejs.org/guide/best-practices/production-deployment.html#with-build-tools | |
// Replace Vue imports path to tree-shakable version | |
name: 'replace-vue-import', | |
setup(build) { | |
const moduleMap = new Map<string, string>() | |
build.onResolve({ | |
filter: /^vue$/, | |
}, () => { | |
return { | |
path: resolve(__dirname, 'node_modules/vue/dist/vue.runtime.esm-bundler.js'), | |
} | |
}) | |
build.onResolve({ | |
filter: /^(@vue\/.*)$/, | |
}, async (args) => { | |
const name = args.path.replace('@vue/', '') | |
let moduleFilePath: string | |
if (moduleMap.has(args.path)) { | |
moduleFilePath = moduleMap.get(args.path)! | |
} | |
else { | |
moduleFilePath = `${import.meta.resolve(args.path).slice(/* file:// */7, /* /index.js */ -9)}/dist/${name}.esm-bundler.js` | |
moduleMap.set(args.path, moduleFilePath) | |
} | |
return { | |
path: moduleFilePath, | |
} | |
}) | |
}, | |
}, | |
], | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment