代码分隔
在runEsbuild
方法中,针对 ESM 的模块,默认设置splitting
为true
。
否则,根据 CLI 指令传给esbuild
的splitting
属性。
const splitting =
format === 'iife'
? false
: typeof options.splitting === 'boolean'
? options.splitting
: format === 'esm'
esbuild({
splitting,
})
针对CJS
,使用自定义插件,基于sucrase
进行构建。
export const cjsSplitting = (): Plugin => {
return {
name: 'cjs-splitting',
async renderChunk(code, info) {
if (
!this.splitting ||
this.options.treeshake || // <-- handled by rollup
this.format !== 'cjs' ||
info.type !== 'chunk' ||
!/\.(js|cjs)$/.test(info.path)
) {
return
}
const { transform } = await import('sucrase')
const result = transform(code, {
filePath: info.path,
transforms: ['imports'],
sourceMapOptions: this.options.sourcemap
? {
compiledFilename: info.path,
}
: undefined,
})
return {
code: result.code,
map: result.sourceMap,
}
},
}
}