This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-package.js
More file actions
57 lines (50 loc) · 1.39 KB
/
validate-package.js
File metadata and controls
57 lines (50 loc) · 1.39 KB
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
55
56
57
const { rollup } = require('rollup');
const acorn = require('acorn');
const virtual = require('rollup-plugin-virtual');
const pkg = require('./package.json');
const external = Object.keys(pkg.dependencies || {});
// Other external dependencies that are not imported directly
external.push(/@material-ui\/.*/);
external.push('react');
external.push('react-dom');
external.push('react/jsx-runtime');
// Check that the bundle is empty when tree shaken.
// Inspired by https://github.com/Rich-Harris/agadoo/blob/master/index.js
function check() {
return rollup({
input: '__virual_input__',
external: external,
plugins: [
virtual({
__virual_input__: `import * as Bundle from './dist/es/index'`,
}),
],
onwarn: (warning, handle) => {
if (warning.code !== 'EMPTY_BUNDLE') handle(warning);
},
})
.then((bundle) =>
bundle.generate({
format: 'esm',
})
)
.then((result) => {
const { code } = result.output[0];
const ast = acorn.parse(code, {
ecmaVersion: 11,
sourceType: 'module',
});
const nodes = ast.body.filter((node) => {
return node.type !== 'ImportDeclaration';
});
if (nodes.length > 0) {
throw new Error(
`The bundle is not tree shakable. Content: \r\n${code}`
);
}
return {
shaken: nodes.length === 0,
};
});
}
check();