Skip to content

Commit 5e037ae

Browse files
committed
enforce treeshakeability
1 parent 9fc8726 commit 5e037ae

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

packages/svelte/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"templating"
9191
],
9292
"scripts": {
93-
"build": "rollup -c && node scripts/build.js",
93+
"build": "rollup -c && node scripts/build.js && node scripts/check-treeshakeability.js",
9494
"watch": "rollup -cw",
9595
"check": "tsc && cd ./tests/types && tsc",
9696
"check:watch": "tsc --watch",
@@ -103,6 +103,7 @@
103103
"@rollup/plugin-commonjs": "^25.0.7",
104104
"@rollup/plugin-node-resolve": "^15.2.3",
105105
"@rollup/plugin-terser": "^0.4.4",
106+
"@rollup/plugin-virtual": "^3.0.2",
106107
"@types/aria-query": "^5.0.3",
107108
"@types/estree": "^1.0.5",
108109
"dts-buddy": "^0.4.0",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { rollup } from 'rollup';
4+
import virtual from '@rollup/plugin-virtual';
5+
6+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
7+
8+
for (const key in pkg.exports) {
9+
for (const type of ['browser', 'default']) {
10+
if (!pkg.exports[key][type]) continue;
11+
12+
const resolved = path.resolve(pkg.exports[key][type]);
13+
14+
const bundle = await rollup({
15+
input: '__entry__',
16+
plugins: [
17+
virtual({
18+
__entry__: `import ${JSON.stringify(resolved)}`
19+
})
20+
],
21+
onwarn: (warning, handle) => {
22+
// if (warning.code !== 'EMPTY_BUNDLE') handle(warning);
23+
}
24+
});
25+
26+
const { output } = await bundle.generate({});
27+
28+
if (output.length > 1) {
29+
throw new Error('errr what');
30+
}
31+
32+
const { code } = output[0];
33+
if (code.trim()) {
34+
console.error(code);
35+
throw new Error(`${path.join(pkg.name, key)} ${type} export is not tree-shakeable`);
36+
}
37+
}
38+
}

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)