Skip to content

Commit 5702142

Browse files
gtm-nayandummdidummbenmccann
authored
feat: attach Svelte major version info to window global (#8761)
Can be opt out by setting discloseVersion to false --------- Co-authored-by: Simon H <[email protected]> Co-authored-by: Ben McCann <[email protected]>
1 parent 0394216 commit 5702142

File tree

12 files changed

+53
-10
lines changed

12 files changed

+53
-10
lines changed

.changeset/yellow-squids-explain.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: add version info to `window`. You can opt out by setting `discloseVersion` to `false` in the compiler options

packages/playground/src/App.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<script>
2+
import Counter from "./lib/Counter.svelte";
3+
</script>
4+
15
<div>
26
Hello world!
37
</div>

packages/svelte/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
"types": "./types/index.d.ts",
5757
"import": "./src/runtime/store/index.js"
5858
},
59+
"./internal/disclose-version": {
60+
"import": "./src/runtime/internal/disclose-version/index.js"
61+
},
5962
"./transition": {
6063
"types": "./types/index.d.ts",
6164
"import": "./src/runtime/transition/index.js"

packages/svelte/scripts/generate-version.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ fs.writeFileSync(
1313
* @type {string}
1414
*/
1515
export const VERSION = '${pkg.version}';
16+
export const PUBLIC_VERSION = '${pkg.version.split('.')[0]}';
1617
`
1718
);

packages/svelte/src/compiler/compile/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const valid_options = [
3030
'loopGuardTimeout',
3131
'preserveComments',
3232
'preserveWhitespace',
33-
'cssHash'
33+
'cssHash',
34+
'discloseVersion'
3435
];
3536
const valid_css_values = [true, false, 'injected', 'external', 'none'];
3637
const regex_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
@@ -112,6 +113,10 @@ function validate_options(options, warnings) {
112113
throw new Error(`Invalid namespace '${namespace}'`);
113114
}
114115
}
116+
117+
if (options.discloseVersion == undefined) {
118+
options.discloseVersion = true;
119+
}
115120
}
116121

117122
/**

packages/svelte/src/compiler/compile/render_dom/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,5 +604,17 @@ export default function dom(component, options) {
604604
);
605605
}
606606
}
607+
608+
if (options.discloseVersion === true) {
609+
component.imports.unshift({
610+
type: 'ImportDeclaration',
611+
specifiers: [],
612+
source: {
613+
type: 'Literal',
614+
value: `${options.sveltePath ?? 'svelte'}/internal/disclose-version`
615+
}
616+
});
617+
}
618+
607619
return { js: flatten(body), css };
608620
}

packages/svelte/src/compiler/interfaces.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ export interface CompileOptions {
344344
* @default false
345345
*/
346346
preserveWhitespace?: boolean;
347+
/**
348+
* If `true`, exposes the Svelte major version on the global `window` object in the browser.
349+
*
350+
* @default true
351+
*/
352+
discloseVersion?: boolean;
347353
}
348354

349355
export interface ParserOptions {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { PUBLIC_VERSION } from '../../../shared/version.js';
2+
3+
if (typeof window !== 'undefined')
4+
// @ts-ignore
5+
(window.__svelte || (window.__svelte = { v: new Set() })).v.add(PUBLIC_VERSION);

packages/svelte/src/shared/version.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
* @type {string}
88
*/
99
export const VERSION = '4.0.0-next.2';
10+
export const PUBLIC_VERSION = '4';

packages/svelte/test/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export function create_loader(compileOptions, cwd) {
147147
// any imported Svelte components as well. A few edge cases aren't handled but also
148148
// currently unused in the tests, for example `export * from`and live bindings.
149149
let transformed = compiled.js.code
150+
.replace(/^import ['"]([^'"]+)['"]/gm, 'await __import("$1")')
150151
.replace(
151152
/^import \* as (\w+) from ['"]([^'"]+)['"];?/gm,
152153
'const $1 = await __import("$2");'

packages/svelte/test/js/js-output.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ describe('js-output', () => {
3333
let actual;
3434

3535
try {
36-
const options = Object.assign({}, config.options || {});
36+
const options = Object.assign(
37+
{
38+
discloseVersion: false
39+
},
40+
config.options || {}
41+
);
3742

3843
actual = svelte
3944
.compile(input, options)

packages/svelte/test/runtime-browser/browser.test.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import * as svelte from 'svelte/compiler';
66
import { afterAll, assert, beforeAll, describe, it } from 'vitest';
77
import { pretty_print_browser_assertion, try_load_config } from '../helpers.js';
88

9-
const internal = path.resolve('src/runtime/internal/index.js');
10-
const index = path.resolve('src/runtime/index.js');
9+
const assert_file = path.resolve(__dirname, 'assert.js');
1110

1211
/** @type {import('@playwright/test').Browser} */
1312
let browser;
@@ -62,9 +61,7 @@ async function run_browser_test(dir) {
6261
alias: {
6362
__MAIN_DOT_SVELTE__: path.resolve(__dirname, 'samples', dir, 'main.svelte'),
6463
__CONFIG__: path.resolve(__dirname, 'samples', dir, '_config.js'),
65-
'assert.js': path.resolve(__dirname, 'assert.js'),
66-
'svelte/internal': internal,
67-
svelte: index
64+
'assert.js': assert_file
6865
},
6966
plugins: [
7067
{
@@ -169,9 +166,7 @@ async function run_custom_elements_test(dir) {
169166
entryPoints: [`${cwd}/test.js`],
170167
write: false,
171168
alias: {
172-
'assert.js': path.resolve(__dirname, 'assert.js'),
173-
'svelte/internal': internal,
174-
svelte: index
169+
'assert.js': assert_file
175170
},
176171
plugins: [
177172
{

0 commit comments

Comments
 (0)