-
Notifications
You must be signed in to change notification settings - Fork 826
/
tree-shaking.test.js
54 lines (48 loc) · 1.29 KB
/
tree-shaking.test.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const path = require('node:path')
const commonjs = require('@rollup/plugin-commonjs')
const {nodeResolve} = require('@rollup/plugin-node-resolve')
const {rollup} = require('rollup')
const virtual = require('@rollup/plugin-virtual')
const packageImport = path.resolve(__dirname, '..')
test('tree shaking', async () => {
const bundle = await rollup({
input: '__entrypoint__',
external: [],
plugins: [
nodeResolve(),
commonjs(),
virtual({
__entrypoint__: `import { AlertIcon } from '${packageImport}'`
})
],
onwarn: ({code, message}) => {
if (code !== 'EMPTY_BUNDLE') {
throw new Error(message)
}
}
})
const {output} = await bundle.generate({
format: 'esm'
})
for (const {code} of output) {
expect(code.trim()).toBe('')
}
})
test('tree shaking single export', async () => {
const bundle = await rollup({
input: '__entrypoint__',
external: ['react'],
plugins: [
nodeResolve(),
commonjs(),
virtual({
__entrypoint__: `export { XIcon } from '${packageImport}'`
})
]
})
const {output} = await bundle.generate({
format: 'esm'
})
const bundleSize = Buffer.byteLength(output[0].code.trim()) / 1000
expect(`${bundleSize}kB`).toMatchInlineSnapshot(`"2.826kB"`)
})