Skip to content

Commit 9c3516d

Browse files
feat: runes globals error (#9773)
* feat: runes globals error throw descriptive error for using runes globals outside of Svelte-compiled files * less hacky/more future-proof treeshaking check * tweak --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 24777c3 commit 9c3516d

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

.changeset/swift-seahorses-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: throw descriptive error for using runes globals outside of Svelte-compiled files

packages/svelte/scripts/check-treeshakeability.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { rollup } from 'rollup';
44
import virtual from '@rollup/plugin-virtual';
5+
import { nodeResolve } from '@rollup/plugin-node-resolve';
56

67
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
78

@@ -27,6 +28,9 @@ for (const key in pkg.exports) {
2728
plugins: [
2829
virtual({
2930
__entry__: `import ${JSON.stringify(resolved)}`
31+
}),
32+
nodeResolve({
33+
exportConditions: ['production', 'import', 'browser', 'default']
3034
})
3135
],
3236
onwarn: (warning, handle) => {
@@ -40,16 +44,17 @@ for (const key in pkg.exports) {
4044
throw new Error('errr what');
4145
}
4246

43-
const code = output[0].code.replace(/import\s+([^'"]+from\s+)?(['"])[^'"]+\2\s*;?/, '');
44-
if (code.trim()) {
47+
const code = output[0].code.trim();
48+
49+
if (code === '') {
50+
// eslint-disable-next-line no-console
51+
console.error(`✅ ${subpackage} (${type})`);
52+
} else {
4553
// eslint-disable-next-line no-console
4654
console.error(code);
4755
// eslint-disable-next-line no-console
4856
console.error(`❌ ${subpackage} (${type})`);
4957
failed = true;
50-
} else {
51-
// eslint-disable-next-line no-console
52-
console.error(`✅ ${subpackage} (${type})`);
5358
}
5459
}
5560
}

packages/svelte/src/internal/client/runtime.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,3 +1875,22 @@ export function unwrap(value) {
18751875
// @ts-ignore
18761876
return value;
18771877
}
1878+
1879+
if (DEV) {
1880+
/** @param {string} rune */
1881+
function throw_rune_error(rune) {
1882+
if (!(rune in globalThis)) {
1883+
// @ts-ignore
1884+
globalThis[rune] = () => {
1885+
// TODO if people start adjusting the "this can contain runes" config through v-p-s more, adjust this message
1886+
throw new Error(`${rune} is only available inside .svelte and .svelte.js/ts files`);
1887+
};
1888+
}
1889+
}
1890+
1891+
throw_rune_error('$state');
1892+
throw_rune_error('$effect');
1893+
throw_rune_error('$derived');
1894+
throw_rune_error('$inspect');
1895+
throw_rune_error('$props');
1896+
}

0 commit comments

Comments
 (0)