From 5e037ae0a2b3095d202a32abaac3fe2e5920bc44 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 13 Nov 2023 14:54:01 -0500 Subject: [PATCH] enforce treeshakeability --- packages/svelte/package.json | 3 +- .../svelte/scripts/check-treeshakeability.js | 38 +++++++++++++++++++ pnpm-lock.yaml | 15 ++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 packages/svelte/scripts/check-treeshakeability.js diff --git a/packages/svelte/package.json b/packages/svelte/package.json index 91f313a2a4..b689111dbb 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -90,7 +90,7 @@ "templating" ], "scripts": { - "build": "rollup -c && node scripts/build.js", + "build": "rollup -c && node scripts/build.js && node scripts/check-treeshakeability.js", "watch": "rollup -cw", "check": "tsc && cd ./tests/types && tsc", "check:watch": "tsc --watch", @@ -103,6 +103,7 @@ "@rollup/plugin-commonjs": "^25.0.7", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-terser": "^0.4.4", + "@rollup/plugin-virtual": "^3.0.2", "@types/aria-query": "^5.0.3", "@types/estree": "^1.0.5", "dts-buddy": "^0.4.0", diff --git a/packages/svelte/scripts/check-treeshakeability.js b/packages/svelte/scripts/check-treeshakeability.js new file mode 100644 index 0000000000..bdcdcc9ef6 --- /dev/null +++ b/packages/svelte/scripts/check-treeshakeability.js @@ -0,0 +1,38 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { rollup } from 'rollup'; +import virtual from '@rollup/plugin-virtual'; + +const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); + +for (const key in pkg.exports) { + for (const type of ['browser', 'default']) { + if (!pkg.exports[key][type]) continue; + + const resolved = path.resolve(pkg.exports[key][type]); + + const bundle = await rollup({ + input: '__entry__', + plugins: [ + virtual({ + __entry__: `import ${JSON.stringify(resolved)}` + }) + ], + onwarn: (warning, handle) => { + // if (warning.code !== 'EMPTY_BUNDLE') handle(warning); + } + }); + + const { output } = await bundle.generate({}); + + if (output.length > 1) { + throw new Error('errr what'); + } + + const { code } = output[0]; + if (code.trim()) { + console.error(code); + throw new Error(`${path.join(pkg.name, key)} ${type} export is not tree-shakeable`); + } + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cb0e47baa7..1dffc51f17 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -108,6 +108,9 @@ importers: '@rollup/plugin-terser': specifier: ^0.4.4 version: 0.4.4(rollup@4.3.0) + '@rollup/plugin-virtual': + specifier: ^3.0.2 + version: 3.0.2(rollup@4.3.0) '@types/aria-query': specifier: ^5.0.3 version: 5.0.4 @@ -1962,6 +1965,18 @@ packages: terser: 5.24.0 dev: true + /@rollup/plugin-virtual@3.0.2(rollup@4.3.0): + resolution: {integrity: sha512-10monEYsBp3scM4/ND4LNH5Rxvh3e/cVeL3jWTgZ2SrQ+BmUoQcopVQvnaMcOnykb1VkxUFuDAN+0FnpTFRy2A==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + dependencies: + rollup: 4.3.0 + dev: true + /@rollup/pluginutils@4.2.1: resolution: {integrity: sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==} engines: {node: '>= 8.0.0'}