From 53009cb19236f3931fd8ccc27a1f15afdf90fe3e Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 19 Jun 2023 21:37:25 +0545 Subject: [PATCH 01/18] version info thingy --- packages/playground/src/App.svelte | 4 ++++ packages/playground/start.js | 7 ++++++- packages/svelte/package.json | 3 +++ .../src/compiler/compile/render_dom/index.js | 14 ++++++++++++++ packages/svelte/src/compiler/interfaces.d.ts | 1 + packages/svelte/src/runtime/tag-version.js | 4 ++++ packages/svelte/test/helpers.js | 5 +++++ .../svelte/test/runtime-browser/browser.test.js | 2 ++ packages/svelte/vitest.config.js | 4 ++++ 9 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 packages/svelte/src/runtime/tag-version.js diff --git a/packages/playground/src/App.svelte b/packages/playground/src/App.svelte index ab87de6d9679..b3c16eec67ce 100644 --- a/packages/playground/src/App.svelte +++ b/packages/playground/src/App.svelte @@ -1,3 +1,7 @@ + +
Hello world!
\ No newline at end of file diff --git a/packages/playground/start.js b/packages/playground/start.js index af2ed10a54ca..ef077bd9721d 100644 --- a/packages/playground/start.js +++ b/packages/playground/start.js @@ -20,7 +20,12 @@ function create_plugin(ssr = false) { ssr ? '../svelte/src/runtime/ssr.js' : '../svelte/src/runtime/index.js' ); } else if (id.startsWith('svelte/')) { - return path.resolve(__dirname, `../svelte/src/runtime/${id.slice(7)}/index.js`); + return path.resolve( + __dirname, + id === 'svelte/tag-version' + ? '../svelte/src/runtime/tag-version.js' + : `../svelte/src/runtime/${id.slice(7)}/index.js` + ); } }, transform(code, id) { diff --git a/packages/svelte/package.json b/packages/svelte/package.json index 82d022c0f237..f30cbcf10fab 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -56,6 +56,9 @@ "types": "./types/index.d.ts", "import": "./src/runtime/store/index.js" }, + "./tag-version": { + "import": "./src/runtime/tag-version.js" + }, "./transition": { "types": "./types/index.d.ts", "import": "./src/runtime/transition/index.js" diff --git a/packages/svelte/src/compiler/compile/render_dom/index.js b/packages/svelte/src/compiler/compile/render_dom/index.js index 7e640ac64af5..2f1b5a0ed9e0 100644 --- a/packages/svelte/src/compiler/compile/render_dom/index.js +++ b/packages/svelte/src/compiler/compile/render_dom/index.js @@ -604,5 +604,19 @@ export default function dom(component, options) { ); } } + + if (options.disableVersionTagging !== false) { + /** @type {import('estree').ImportDeclaration} */ + const version_tag_import = { + type: 'ImportDeclaration', + specifiers: [], + source: { + type: 'Literal', + value: `${options.sveltePath ?? 'svelte'}/tag-version` + } + }; + component.imports.unshift(version_tag_import); + } + return { js: flatten(body), css }; } diff --git a/packages/svelte/src/compiler/interfaces.d.ts b/packages/svelte/src/compiler/interfaces.d.ts index 43e5ece4fdfd..4715a4d0e254 100644 --- a/packages/svelte/src/compiler/interfaces.d.ts +++ b/packages/svelte/src/compiler/interfaces.d.ts @@ -195,6 +195,7 @@ export interface CompileOptions { preserveComments?: boolean; preserveWhitespace?: boolean; + disableVersionTagging?: boolean; } export interface ParserOptions { diff --git a/packages/svelte/src/runtime/tag-version.js b/packages/svelte/src/runtime/tag-version.js new file mode 100644 index 000000000000..df9dd09c77ca --- /dev/null +++ b/packages/svelte/src/runtime/tag-version.js @@ -0,0 +1,4 @@ +import { VERSION } from '../shared/version'; + +// @ts-ignore +if (typeof window !== undefined) (window.__svelte || (window.__svelte = {})).version = VERSION; diff --git a/packages/svelte/test/helpers.js b/packages/svelte/test/helpers.js index 8e6741dc2126..ba06118963ab 100644 --- a/packages/svelte/test/helpers.js +++ b/packages/svelte/test/helpers.js @@ -134,6 +134,10 @@ export function create_loader(compileOptions, cwd) { resolved = `${svelte_path}src/runtime/index.js`; } + if (id === 'svelte/tag-version') { + return `${svelte_path}src/runtime/tag-version.js`; + } + if (id.startsWith('svelte/')) { resolved = `${svelte_path}src/runtime/${id.slice(7)}/index.js`; } @@ -148,6 +152,7 @@ export function create_loader(compileOptions, cwd) { // any imported Svelte components as well. A few edge cases aren't handled but also // currently unused in the tests, for example `export * from`and live bindings. let transformed = compiled.js.code + .replace(/^import ['"]([^'"]+)['"]/gm, 'await __import("$1")') .replace( /^import \* as (\w+) from ['"]([^'"]+)['"];?/gm, 'const $1 = await __import("$2");' diff --git a/packages/svelte/test/runtime-browser/browser.test.js b/packages/svelte/test/runtime-browser/browser.test.js index 09eb43cf0b10..f768a7b40237 100644 --- a/packages/svelte/test/runtime-browser/browser.test.js +++ b/packages/svelte/test/runtime-browser/browser.test.js @@ -64,6 +64,7 @@ async function run_browser_test(dir) { __CONFIG__: path.resolve(__dirname, 'samples', dir, '_config.js'), 'assert.js': path.resolve(__dirname, 'assert.js'), 'svelte/internal': internal, + 'svelte/tag-version': path.resolve('src/runtime/tag-version.js'), svelte: index }, plugins: [ @@ -171,6 +172,7 @@ async function run_custom_elements_test(dir) { alias: { 'assert.js': path.resolve(__dirname, 'assert.js'), 'svelte/internal': internal, + 'svelte/tag-version': path.resolve('src/runtime/tag-version.js'), svelte: index }, plugins: [ diff --git a/packages/svelte/vitest.config.js b/packages/svelte/vitest.config.js index 4392caa0e1c3..f16e7115bd06 100644 --- a/packages/svelte/vitest.config.js +++ b/packages/svelte/vitest.config.js @@ -13,6 +13,10 @@ export default defineConfig({ return `${__dirname}/src/runtime/index.js`; } + if (id === 'svelte/tag-version') { + return `${__dirname}/src/runtime/tag-version.js`; + } + if (id.startsWith('svelte/')) { return id.replace(/^svelte(.*)\/?$/, `${__dirname}/src/runtime/$1/index.js`); } From aacec5b52de6f4f6517d4c026e528748f12a11d8 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 19 Jun 2023 21:50:49 +0545 Subject: [PATCH 02/18] fix opt-out and tests --- packages/svelte/src/compiler/compile/index.js | 3 ++- packages/svelte/src/compiler/compile/render_dom/index.js | 2 +- packages/svelte/test/js/js-output.test.js | 7 ++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/svelte/src/compiler/compile/index.js b/packages/svelte/src/compiler/compile/index.js index d2ac8eba63fd..84d53d995524 100644 --- a/packages/svelte/src/compiler/compile/index.js +++ b/packages/svelte/src/compiler/compile/index.js @@ -30,7 +30,8 @@ const valid_options = [ 'loopGuardTimeout', 'preserveComments', 'preserveWhitespace', - 'cssHash' + 'cssHash', + 'disableVersionTagging' ]; const valid_css_values = [true, false, 'injected', 'external', 'none']; const regex_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/; diff --git a/packages/svelte/src/compiler/compile/render_dom/index.js b/packages/svelte/src/compiler/compile/render_dom/index.js index 2f1b5a0ed9e0..03fb377533a6 100644 --- a/packages/svelte/src/compiler/compile/render_dom/index.js +++ b/packages/svelte/src/compiler/compile/render_dom/index.js @@ -605,7 +605,7 @@ export default function dom(component, options) { } } - if (options.disableVersionTagging !== false) { + if (options.disableVersionTagging !== true) { /** @type {import('estree').ImportDeclaration} */ const version_tag_import = { type: 'ImportDeclaration', diff --git a/packages/svelte/test/js/js-output.test.js b/packages/svelte/test/js/js-output.test.js index 31ffe9e5eb60..50e39be36ba1 100644 --- a/packages/svelte/test/js/js-output.test.js +++ b/packages/svelte/test/js/js-output.test.js @@ -33,7 +33,12 @@ describe('js-output', () => { let actual; try { - const options = Object.assign({}, config.options || {}); + const options = Object.assign( + { + disableVersionTagging: true + }, + config.options || {} + ); actual = svelte .compile(input, options) From 2e4ba1546df8872f8c530583bf526cda37489747 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 19 Jun 2023 22:20:20 +0545 Subject: [PATCH 03/18] use a more obscure name with an array --- packages/svelte/src/runtime/tag-version.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/svelte/src/runtime/tag-version.js b/packages/svelte/src/runtime/tag-version.js index df9dd09c77ca..f162c327fd61 100644 --- a/packages/svelte/src/runtime/tag-version.js +++ b/packages/svelte/src/runtime/tag-version.js @@ -1,4 +1,5 @@ import { VERSION } from '../shared/version'; -// @ts-ignore -if (typeof window !== undefined) (window.__svelte || (window.__svelte = {})).version = VERSION; +if (typeof window !== undefined) + // @ts-ignore + (window.__svelte_versions || (window.__svelte_versions = [])).push(VERSION); From 039c06071455ad5b39005f120400f29796b7ca2b Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 19 Jun 2023 22:23:54 +0545 Subject: [PATCH 04/18] whoops --- packages/svelte/src/runtime/tag-version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/runtime/tag-version.js b/packages/svelte/src/runtime/tag-version.js index f162c327fd61..5b94852cb9ef 100644 --- a/packages/svelte/src/runtime/tag-version.js +++ b/packages/svelte/src/runtime/tag-version.js @@ -1,5 +1,5 @@ import { VERSION } from '../shared/version'; -if (typeof window !== undefined) +if (typeof window !== "undefined") // @ts-ignore (window.__svelte_versions || (window.__svelte_versions = [])).push(VERSION); From edf487d531cb26176f6938b26741f45a263c15ec Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 19 Jun 2023 22:26:11 +0545 Subject: [PATCH 05/18] formatting --- packages/svelte/src/runtime/tag-version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/runtime/tag-version.js b/packages/svelte/src/runtime/tag-version.js index 5b94852cb9ef..18d1e58ee800 100644 --- a/packages/svelte/src/runtime/tag-version.js +++ b/packages/svelte/src/runtime/tag-version.js @@ -1,5 +1,5 @@ import { VERSION } from '../shared/version'; -if (typeof window !== "undefined") +if (typeof window !== 'undefined') // @ts-ignore (window.__svelte_versions || (window.__svelte_versions = [])).push(VERSION); From 9e7b2ab7a23fa5f303b627a1fd5b29580d40b699 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Mon, 19 Jun 2023 22:26:38 +0545 Subject: [PATCH 06/18] use extension --- packages/svelte/src/runtime/tag-version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/runtime/tag-version.js b/packages/svelte/src/runtime/tag-version.js index 18d1e58ee800..0d50555c4e12 100644 --- a/packages/svelte/src/runtime/tag-version.js +++ b/packages/svelte/src/runtime/tag-version.js @@ -1,4 +1,4 @@ -import { VERSION } from '../shared/version'; +import { VERSION } from '../shared/version.js'; if (typeof window !== 'undefined') // @ts-ignore From 1fb6dcc7c79cf440520779347bcf088909435208 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Tue, 20 Jun 2023 13:29:11 +0545 Subject: [PATCH 07/18] rename to internal/tag-version --- packages/playground/start.js | 2 +- packages/svelte/package.json | 4 ++-- .../svelte/src/compiler/compile/render_dom/index.js | 8 +++----- .../svelte/src/runtime/{ => internal}/tag-version.js | 2 +- packages/svelte/test/helpers.js | 4 ++-- packages/svelte/test/runtime-browser/browser.test.js | 10 ++++++---- packages/svelte/vitest.config.js | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) rename packages/svelte/src/runtime/{ => internal}/tag-version.js (71%) diff --git a/packages/playground/start.js b/packages/playground/start.js index ef077bd9721d..8ef6000b3fdc 100644 --- a/packages/playground/start.js +++ b/packages/playground/start.js @@ -23,7 +23,7 @@ function create_plugin(ssr = false) { return path.resolve( __dirname, id === 'svelte/tag-version' - ? '../svelte/src/runtime/tag-version.js' + ? '../svelte/src/runtime/internal/tag-version.js' : `../svelte/src/runtime/${id.slice(7)}/index.js` ); } diff --git a/packages/svelte/package.json b/packages/svelte/package.json index f30cbcf10fab..d58f746c3507 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -56,8 +56,8 @@ "types": "./types/index.d.ts", "import": "./src/runtime/store/index.js" }, - "./tag-version": { - "import": "./src/runtime/tag-version.js" + "./internal/tag-version": { + "import": "./src/runtime/internal/tag-version.js" }, "./transition": { "types": "./types/index.d.ts", diff --git a/packages/svelte/src/compiler/compile/render_dom/index.js b/packages/svelte/src/compiler/compile/render_dom/index.js index 03fb377533a6..8d66030571e8 100644 --- a/packages/svelte/src/compiler/compile/render_dom/index.js +++ b/packages/svelte/src/compiler/compile/render_dom/index.js @@ -606,16 +606,14 @@ export default function dom(component, options) { } if (options.disableVersionTagging !== true) { - /** @type {import('estree').ImportDeclaration} */ - const version_tag_import = { + component.imports.unshift({ type: 'ImportDeclaration', specifiers: [], source: { type: 'Literal', - value: `${options.sveltePath ?? 'svelte'}/tag-version` + value: `${options.sveltePath ?? 'svelte'}/internal/tag-version` } - }; - component.imports.unshift(version_tag_import); + }); } return { js: flatten(body), css }; diff --git a/packages/svelte/src/runtime/tag-version.js b/packages/svelte/src/runtime/internal/tag-version.js similarity index 71% rename from packages/svelte/src/runtime/tag-version.js rename to packages/svelte/src/runtime/internal/tag-version.js index 0d50555c4e12..a7178d562522 100644 --- a/packages/svelte/src/runtime/tag-version.js +++ b/packages/svelte/src/runtime/internal/tag-version.js @@ -1,4 +1,4 @@ -import { VERSION } from '../shared/version.js'; +import { VERSION } from '../../shared/version.js'; if (typeof window !== 'undefined') // @ts-ignore diff --git a/packages/svelte/test/helpers.js b/packages/svelte/test/helpers.js index ba06118963ab..4f8769a0b7fe 100644 --- a/packages/svelte/test/helpers.js +++ b/packages/svelte/test/helpers.js @@ -134,8 +134,8 @@ export function create_loader(compileOptions, cwd) { resolved = `${svelte_path}src/runtime/index.js`; } - if (id === 'svelte/tag-version') { - return `${svelte_path}src/runtime/tag-version.js`; + if (id === 'svelte/internal/tag-version') { + return `${svelte_path}src/runtime/internal/tag-version.js`; } if (id.startsWith('svelte/')) { diff --git a/packages/svelte/test/runtime-browser/browser.test.js b/packages/svelte/test/runtime-browser/browser.test.js index f768a7b40237..4cce649d6565 100644 --- a/packages/svelte/test/runtime-browser/browser.test.js +++ b/packages/svelte/test/runtime-browser/browser.test.js @@ -8,6 +8,8 @@ import { pretty_print_browser_assertion, try_load_config } from '../helpers.js'; const internal = path.resolve('src/runtime/internal/index.js'); const index = path.resolve('src/runtime/index.js'); +const assert_file = path.resolve(__dirname, 'assert.js'); +const tag_version = path.resolve(__dirname, '../../src/runtime/internal/tag-version.js'); /** @type {import('@playwright/test').Browser} */ let browser; @@ -62,9 +64,9 @@ async function run_browser_test(dir) { alias: { __MAIN_DOT_SVELTE__: path.resolve(__dirname, 'samples', dir, 'main.svelte'), __CONFIG__: path.resolve(__dirname, 'samples', dir, '_config.js'), - 'assert.js': path.resolve(__dirname, 'assert.js'), + 'assert.js': assert_file, + 'svelte/internal/tag-version': tag_version, 'svelte/internal': internal, - 'svelte/tag-version': path.resolve('src/runtime/tag-version.js'), svelte: index }, plugins: [ @@ -170,9 +172,9 @@ async function run_custom_elements_test(dir) { entryPoints: [`${cwd}/test.js`], write: false, alias: { - 'assert.js': path.resolve(__dirname, 'assert.js'), + 'assert.js': assert_file, + 'svelte/internal/tag-version': tag_version, 'svelte/internal': internal, - 'svelte/tag-version': path.resolve('src/runtime/tag-version.js'), svelte: index }, plugins: [ diff --git a/packages/svelte/vitest.config.js b/packages/svelte/vitest.config.js index f16e7115bd06..a95fc4a45f1e 100644 --- a/packages/svelte/vitest.config.js +++ b/packages/svelte/vitest.config.js @@ -13,8 +13,8 @@ export default defineConfig({ return `${__dirname}/src/runtime/index.js`; } - if (id === 'svelte/tag-version') { - return `${__dirname}/src/runtime/tag-version.js`; + if (id === 'svelte/internal/tag-version') { + return `${__dirname}/src/runtime/internal/tag-version.js`; } if (id.startsWith('svelte/')) { From e2cefd0aa2622c9591447d969dab3f4eb230cc4c Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Tue, 20 Jun 2023 13:34:25 +0545 Subject: [PATCH 08/18] use Set --- packages/svelte/src/runtime/internal/tag-version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/runtime/internal/tag-version.js b/packages/svelte/src/runtime/internal/tag-version.js index a7178d562522..5e26259b9f5f 100644 --- a/packages/svelte/src/runtime/internal/tag-version.js +++ b/packages/svelte/src/runtime/internal/tag-version.js @@ -2,4 +2,4 @@ import { VERSION } from '../../shared/version.js'; if (typeof window !== 'undefined') // @ts-ignore - (window.__svelte_versions || (window.__svelte_versions = [])).push(VERSION); + (window.__svelte ||= { versions: new Set() }).versions.add(VERSION); From 1bc1949d42e0de9d050c9662ba20fa692dbb49c7 Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Tue, 20 Jun 2023 13:36:48 +0545 Subject: [PATCH 09/18] whoops --- packages/playground/start.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/start.js b/packages/playground/start.js index 8ef6000b3fdc..5659f86995a2 100644 --- a/packages/playground/start.js +++ b/packages/playground/start.js @@ -22,7 +22,7 @@ function create_plugin(ssr = false) { } else if (id.startsWith('svelte/')) { return path.resolve( __dirname, - id === 'svelte/tag-version' + id === 'svelte/internal/tag-version' ? '../svelte/src/runtime/internal/tag-version.js' : `../svelte/src/runtime/${id.slice(7)}/index.js` ); From a05c6f2be3c1b35412acd4dd1dcfafd56634b74b Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Tue, 20 Jun 2023 18:10:09 +0545 Subject: [PATCH 10/18] changeset --- .changeset/yellow-squids-explain.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/yellow-squids-explain.md diff --git a/.changeset/yellow-squids-explain.md b/.changeset/yellow-squids-explain.md new file mode 100644 index 000000000000..52d5ed5f7e05 --- /dev/null +++ b/.changeset/yellow-squids-explain.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +chore: add version info to window From 9ad62f311724bb545b935fe7e534bf592395c976 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:16:07 +0200 Subject: [PATCH 11/18] Update .changeset/yellow-squids-explain.md --- .changeset/yellow-squids-explain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/yellow-squids-explain.md b/.changeset/yellow-squids-explain.md index 52d5ed5f7e05..6407a405c30f 100644 --- a/.changeset/yellow-squids-explain.md +++ b/.changeset/yellow-squids-explain.md @@ -2,4 +2,4 @@ 'svelte': patch --- -chore: add version info to window +chore: add version info to window. You can opt out by setting `disableVersionTagging` to `true` in the compiler options From f2590616572b7276df33a5645ad2cd012c275dab Mon Sep 17 00:00:00 2001 From: gtmnayan <50981692+gtm-nayan@users.noreply.github.com> Date: Wed, 21 Jun 2023 21:10:18 +0545 Subject: [PATCH 12/18] Update .changeset/yellow-squids-explain.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- .changeset/yellow-squids-explain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/yellow-squids-explain.md b/.changeset/yellow-squids-explain.md index 6407a405c30f..406c4c4a9ea2 100644 --- a/.changeset/yellow-squids-explain.md +++ b/.changeset/yellow-squids-explain.md @@ -2,4 +2,4 @@ 'svelte': patch --- -chore: add version info to window. You can opt out by setting `disableVersionTagging` to `true` in the compiler options +feat: add version info to `window`. You can opt out by setting `disableVersionTagging` to `true` in the compiler options From aad1ea36cde04f009f15ce17010d2e7e45369fef Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Wed, 21 Jun 2023 21:50:27 +0545 Subject: [PATCH 13/18] rename option --- .changeset/yellow-squids-explain.md | 2 +- packages/svelte/src/compiler/compile/index.js | 2 +- packages/svelte/src/compiler/compile/render_dom/index.js | 2 +- packages/svelte/src/compiler/interfaces.d.ts | 2 +- packages/svelte/test/js/js-output.test.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.changeset/yellow-squids-explain.md b/.changeset/yellow-squids-explain.md index 406c4c4a9ea2..68726741887c 100644 --- a/.changeset/yellow-squids-explain.md +++ b/.changeset/yellow-squids-explain.md @@ -2,4 +2,4 @@ 'svelte': patch --- -feat: add version info to `window`. You can opt out by setting `disableVersionTagging` to `true` in the compiler options +feat: add version info to `window`. You can opt out by setting `discloseVersion` to `false` in the compiler options diff --git a/packages/svelte/src/compiler/compile/index.js b/packages/svelte/src/compiler/compile/index.js index 84d53d995524..83e9d0e5ed44 100644 --- a/packages/svelte/src/compiler/compile/index.js +++ b/packages/svelte/src/compiler/compile/index.js @@ -31,7 +31,7 @@ const valid_options = [ 'preserveComments', 'preserveWhitespace', 'cssHash', - 'disableVersionTagging' + 'discloseVersion' ]; const valid_css_values = [true, false, 'injected', 'external', 'none']; const regex_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/; diff --git a/packages/svelte/src/compiler/compile/render_dom/index.js b/packages/svelte/src/compiler/compile/render_dom/index.js index 8d66030571e8..2a0250d3761e 100644 --- a/packages/svelte/src/compiler/compile/render_dom/index.js +++ b/packages/svelte/src/compiler/compile/render_dom/index.js @@ -605,7 +605,7 @@ export default function dom(component, options) { } } - if (options.disableVersionTagging !== true) { + if (options.discloseVersion === true) { component.imports.unshift({ type: 'ImportDeclaration', specifiers: [], diff --git a/packages/svelte/src/compiler/interfaces.d.ts b/packages/svelte/src/compiler/interfaces.d.ts index 4715a4d0e254..3a4647d831e3 100644 --- a/packages/svelte/src/compiler/interfaces.d.ts +++ b/packages/svelte/src/compiler/interfaces.d.ts @@ -195,7 +195,7 @@ export interface CompileOptions { preserveComments?: boolean; preserveWhitespace?: boolean; - disableVersionTagging?: boolean; + discloseVersion?: boolean; } export interface ParserOptions { diff --git a/packages/svelte/test/js/js-output.test.js b/packages/svelte/test/js/js-output.test.js index 50e39be36ba1..95409390af52 100644 --- a/packages/svelte/test/js/js-output.test.js +++ b/packages/svelte/test/js/js-output.test.js @@ -35,7 +35,7 @@ describe('js-output', () => { try { const options = Object.assign( { - disableVersionTagging: true + discloseVersion: false }, config.options || {} ); From 205fc8c9a60c96ad9826076703fef09e87eaac7c Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Wed, 21 Jun 2023 22:07:52 +0545 Subject: [PATCH 14/18] fix default, golf things a bit, only major version --- packages/svelte/src/compiler/compile/index.js | 4 ++++ packages/svelte/src/runtime/internal/tag-version.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/svelte/src/compiler/compile/index.js b/packages/svelte/src/compiler/compile/index.js index 83e9d0e5ed44..1a3dc176f423 100644 --- a/packages/svelte/src/compiler/compile/index.js +++ b/packages/svelte/src/compiler/compile/index.js @@ -113,6 +113,10 @@ function validate_options(options, warnings) { throw new Error(`Invalid namespace '${namespace}'`); } } + + if (options.discloseVersion == undefined) { + options.discloseVersion = true; + } } /** diff --git a/packages/svelte/src/runtime/internal/tag-version.js b/packages/svelte/src/runtime/internal/tag-version.js index 5e26259b9f5f..e481d2c9e119 100644 --- a/packages/svelte/src/runtime/internal/tag-version.js +++ b/packages/svelte/src/runtime/internal/tag-version.js @@ -2,4 +2,4 @@ import { VERSION } from '../../shared/version.js'; if (typeof window !== 'undefined') // @ts-ignore - (window.__svelte ||= { versions: new Set() }).versions.add(VERSION); + (window.__svelte ||= { v: new Set() }).v.add(VERSION.replace(/(\d+).*/, '$1')); From d5f97bfab679c628a2df54ea8326203f38d104cb Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Thu, 22 Jun 2023 06:47:06 +0545 Subject: [PATCH 15/18] actually hide the rest of the version and rename entrypoint --- packages/playground/start.js | 7 +------ packages/svelte/package.json | 4 ++-- packages/svelte/scripts/generate-version.js | 5 ++++- packages/svelte/src/compiler/compile/render_dom/index.js | 2 +- .../svelte/src/runtime/internal/disclose-version/index.js | 5 +++++ packages/svelte/src/runtime/internal/tag-version.js | 5 ----- packages/svelte/src/shared/version.js | 1 + packages/svelte/test/helpers.js | 4 ---- packages/svelte/vitest.config.js | 4 ---- 9 files changed, 14 insertions(+), 23 deletions(-) create mode 100644 packages/svelte/src/runtime/internal/disclose-version/index.js delete mode 100644 packages/svelte/src/runtime/internal/tag-version.js diff --git a/packages/playground/start.js b/packages/playground/start.js index 5659f86995a2..af2ed10a54ca 100644 --- a/packages/playground/start.js +++ b/packages/playground/start.js @@ -20,12 +20,7 @@ function create_plugin(ssr = false) { ssr ? '../svelte/src/runtime/ssr.js' : '../svelte/src/runtime/index.js' ); } else if (id.startsWith('svelte/')) { - return path.resolve( - __dirname, - id === 'svelte/internal/tag-version' - ? '../svelte/src/runtime/internal/tag-version.js' - : `../svelte/src/runtime/${id.slice(7)}/index.js` - ); + return path.resolve(__dirname, `../svelte/src/runtime/${id.slice(7)}/index.js`); } }, transform(code, id) { diff --git a/packages/svelte/package.json b/packages/svelte/package.json index d58f746c3507..009689d34018 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -56,8 +56,8 @@ "types": "./types/index.d.ts", "import": "./src/runtime/store/index.js" }, - "./internal/tag-version": { - "import": "./src/runtime/internal/tag-version.js" + "./internal/disclose-version": { + "import": "./src/runtime/internal/disclose-version/index.js" }, "./transition": { "types": "./types/index.d.ts", diff --git a/packages/svelte/scripts/generate-version.js b/packages/svelte/scripts/generate-version.js index 351ead1eca6c..e20fe8343a71 100644 --- a/packages/svelte/scripts/generate-version.js +++ b/packages/svelte/scripts/generate-version.js @@ -4,5 +4,8 @@ const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8')); fs.writeFileSync( './src/shared/version.js', - `// generated during release, do not modify\n\n/** @type {string} */\nexport const VERSION = '${pkg.version}';\n` + `// generated during release, do not modify\n\n/** @type {string} */ +export const VERSION = '${pkg.version}'; +export const PUBLIC_VERSION = '${pkg.version.split('.')[0]}'; +` ); diff --git a/packages/svelte/src/compiler/compile/render_dom/index.js b/packages/svelte/src/compiler/compile/render_dom/index.js index 2a0250d3761e..9a11013fc90a 100644 --- a/packages/svelte/src/compiler/compile/render_dom/index.js +++ b/packages/svelte/src/compiler/compile/render_dom/index.js @@ -611,7 +611,7 @@ export default function dom(component, options) { specifiers: [], source: { type: 'Literal', - value: `${options.sveltePath ?? 'svelte'}/internal/tag-version` + value: `${options.sveltePath ?? 'svelte'}/internal/disclose-version` } }); } diff --git a/packages/svelte/src/runtime/internal/disclose-version/index.js b/packages/svelte/src/runtime/internal/disclose-version/index.js new file mode 100644 index 000000000000..60667ff86ddd --- /dev/null +++ b/packages/svelte/src/runtime/internal/disclose-version/index.js @@ -0,0 +1,5 @@ +import { PUBLIC_VERSION } from '../../../shared/version.js'; + +if (typeof window !== 'undefined') + // @ts-ignore + (window.__svelte ||= { v: new Set() }).v.add(PUBLIC_VERSION); diff --git a/packages/svelte/src/runtime/internal/tag-version.js b/packages/svelte/src/runtime/internal/tag-version.js deleted file mode 100644 index e481d2c9e119..000000000000 --- a/packages/svelte/src/runtime/internal/tag-version.js +++ /dev/null @@ -1,5 +0,0 @@ -import { VERSION } from '../../shared/version.js'; - -if (typeof window !== 'undefined') - // @ts-ignore - (window.__svelte ||= { v: new Set() }).v.add(VERSION.replace(/(\d+).*/, '$1')); diff --git a/packages/svelte/src/shared/version.js b/packages/svelte/src/shared/version.js index c18a0d71f609..139bffa0947f 100644 --- a/packages/svelte/src/shared/version.js +++ b/packages/svelte/src/shared/version.js @@ -2,3 +2,4 @@ /** @type {string} */ export const VERSION = '4.0.0-next.2'; +export const PUBLIC_VERSION = '4'; diff --git a/packages/svelte/test/helpers.js b/packages/svelte/test/helpers.js index 4f8769a0b7fe..9dfb9af0826a 100644 --- a/packages/svelte/test/helpers.js +++ b/packages/svelte/test/helpers.js @@ -134,10 +134,6 @@ export function create_loader(compileOptions, cwd) { resolved = `${svelte_path}src/runtime/index.js`; } - if (id === 'svelte/internal/tag-version') { - return `${svelte_path}src/runtime/internal/tag-version.js`; - } - if (id.startsWith('svelte/')) { resolved = `${svelte_path}src/runtime/${id.slice(7)}/index.js`; } diff --git a/packages/svelte/vitest.config.js b/packages/svelte/vitest.config.js index a95fc4a45f1e..4392caa0e1c3 100644 --- a/packages/svelte/vitest.config.js +++ b/packages/svelte/vitest.config.js @@ -13,10 +13,6 @@ export default defineConfig({ return `${__dirname}/src/runtime/index.js`; } - if (id === 'svelte/internal/tag-version') { - return `${__dirname}/src/runtime/internal/tag-version.js`; - } - if (id.startsWith('svelte/')) { return id.replace(/^svelte(.*)\/?$/, `${__dirname}/src/runtime/$1/index.js`); } From b2a0f8a71365cf1ffd08ccb2cb1355965868756e Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Thu, 22 Jun 2023 06:48:27 +0545 Subject: [PATCH 16/18] syntax compatibility --- packages/svelte/src/runtime/internal/disclose-version/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/runtime/internal/disclose-version/index.js b/packages/svelte/src/runtime/internal/disclose-version/index.js index 60667ff86ddd..7cda592dca15 100644 --- a/packages/svelte/src/runtime/internal/disclose-version/index.js +++ b/packages/svelte/src/runtime/internal/disclose-version/index.js @@ -2,4 +2,4 @@ import { PUBLIC_VERSION } from '../../../shared/version.js'; if (typeof window !== 'undefined') // @ts-ignore - (window.__svelte ||= { v: new Set() }).v.add(PUBLIC_VERSION); + (window.__svelte || (window.__svelte = { v: new Set() })).v.add(PUBLIC_VERSION); From d121960d7755fe41933a981dd894efd0ee89708c Mon Sep 17 00:00:00 2001 From: gtmnayan Date: Thu, 22 Jun 2023 09:39:41 +0545 Subject: [PATCH 17/18] let esbuild handle resolution --- .../svelte/test/runtime-browser/browser.test.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/packages/svelte/test/runtime-browser/browser.test.js b/packages/svelte/test/runtime-browser/browser.test.js index 4cce649d6565..00ecaa652263 100644 --- a/packages/svelte/test/runtime-browser/browser.test.js +++ b/packages/svelte/test/runtime-browser/browser.test.js @@ -6,10 +6,7 @@ import * as svelte from 'svelte/compiler'; import { afterAll, assert, beforeAll, describe, it } from 'vitest'; import { pretty_print_browser_assertion, try_load_config } from '../helpers.js'; -const internal = path.resolve('src/runtime/internal/index.js'); -const index = path.resolve('src/runtime/index.js'); const assert_file = path.resolve(__dirname, 'assert.js'); -const tag_version = path.resolve(__dirname, '../../src/runtime/internal/tag-version.js'); /** @type {import('@playwright/test').Browser} */ let browser; @@ -64,10 +61,7 @@ async function run_browser_test(dir) { alias: { __MAIN_DOT_SVELTE__: path.resolve(__dirname, 'samples', dir, 'main.svelte'), __CONFIG__: path.resolve(__dirname, 'samples', dir, '_config.js'), - 'assert.js': assert_file, - 'svelte/internal/tag-version': tag_version, - 'svelte/internal': internal, - svelte: index + 'assert.js': assert_file }, plugins: [ { @@ -172,10 +166,7 @@ async function run_custom_elements_test(dir) { entryPoints: [`${cwd}/test.js`], write: false, alias: { - 'assert.js': assert_file, - 'svelte/internal/tag-version': tag_version, - 'svelte/internal': internal, - svelte: index + 'assert.js': assert_file }, plugins: [ { From ffd543ae225ac0e9b109fa678a553df9d0baa901 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 22 Jun 2023 10:44:42 +0200 Subject: [PATCH 18/18] Update packages/svelte/src/compiler/interfaces.d.ts --- packages/svelte/src/compiler/interfaces.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/svelte/src/compiler/interfaces.d.ts b/packages/svelte/src/compiler/interfaces.d.ts index a95fb33dae21..64be9698f5b3 100644 --- a/packages/svelte/src/compiler/interfaces.d.ts +++ b/packages/svelte/src/compiler/interfaces.d.ts @@ -344,6 +344,11 @@ export interface CompileOptions { * @default false */ preserveWhitespace?: boolean; + /** + * If `true`, exposes the Svelte major version on the global `window` object in the browser. + * + * @default true + */ discloseVersion?: boolean; }