-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathcjs-splitting.ts
39 lines (34 loc) · 954 Bytes
/
cjs-splitting.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Workaround to enable code splitting for cjs format
// Manually transform esm to cjs
// TODO: remove this once esbuild supports code splitting for cjs natively
import { Plugin } from '../plugin'
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,
}
},
}
}