Skip to content

enforce treeshakeability #9430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
62 changes: 62 additions & 0 deletions packages/svelte/scripts/check-treeshakeability.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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'));

let failed = false;

// eslint-disable-next-line no-console
console.group('checking treeshakeability');

for (const key in pkg.exports) {
// special cases
if (key === './compiler') continue;
if (key === './internal/disclose-version') continue;

for (const type of ['browser', 'default']) {
if (!pkg.exports[key][type]) continue;

const subpackage = path.join(pkg.name, key);

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].code.replace(/import\s+([^'"]+from\s+)?(['"])[^'"]+\2\s*;?/, '');
if (code.trim()) {
// eslint-disable-next-line no-console
console.error(code);
// eslint-disable-next-line no-console
console.error(`❌ ${subpackage} (${type})`);
failed = true;
} else {
// eslint-disable-next-line no-console
console.error(`✅ ${subpackage} (${type})`);
}
}
}

// eslint-disable-next-line no-console
console.groupEnd();

if (failed) {
process.exit(1);
}
7 changes: 2 additions & 5 deletions packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const INVALID_ATTR_NAME_CHAR_REGEX =
/[\s'">/=\u{FDD0}-\u{FDEF}\u{FFFE}\u{FFFF}\u{1FFFE}\u{1FFFF}\u{2FFFE}\u{2FFFF}\u{3FFFE}\u{3FFFF}\u{4FFFE}\u{4FFFF}\u{5FFFE}\u{5FFFF}\u{6FFFE}\u{6FFFF}\u{7FFFE}\u{7FFFF}\u{8FFFE}\u{8FFFF}\u{9FFFE}\u{9FFFF}\u{AFFFE}\u{AFFFF}\u{BFFFE}\u{BFFFF}\u{CFFFE}\u{CFFFF}\u{DFFFE}\u{DFFFF}\u{EFFFE}\u{EFFFF}\u{FFFFE}\u{FFFFF}\u{10FFFE}\u{10FFFF}]/u;

// This is duplicated from the compiler, but we need it at runtime too.
const DOMBooleans = [
export const DOMBooleanAttributes = [
'allowfullscreen',
'async',
'autofocus',
Expand All @@ -59,9 +59,6 @@ const DOMBooleans = [
'selected'
];

/** @type {Set<string>} */
export const DOMBooleanAttributes = new Set(DOMBooleans);

export const VoidElements = new Set([
'area',
'base',
Expand Down Expand Up @@ -280,7 +277,7 @@ export function spread_attributes(attrs, class_hash, additional) {

for (name in merged_attrs) {
if (INVALID_ATTR_NAME_CHAR_REGEX.test(name)) continue;
const is_boolean = DOMBooleanAttributes.has(name);
const is_boolean = DOMBooleanAttributes.includes(name);
attr_str += attr(name, merged_attrs[name], is_boolean);
}

Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.