Skip to content
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

Version info thingy #8761

Merged
merged 20 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
5 changes: 5 additions & 0 deletions .changeset/yellow-squids-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: add version info to window
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions packages/playground/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script>
import Counter from "./lib/Counter.svelte";
</script>

<div>
Hello world!
</div>
7 changes: 6 additions & 1 deletion packages/playground/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/internal/tag-version'
? '../svelte/src/runtime/internal/tag-version.js'
: `../svelte/src/runtime/${id.slice(7)}/index.js`
);
}
},
transform(code, id) {
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"types": "./types/index.d.ts",
"import": "./src/runtime/store/index.js"
},
"./internal/tag-version": {
"import": "./src/runtime/internal/tag-version.js"
},
"./transition": {
"types": "./types/index.d.ts",
"import": "./src/runtime/transition/index.js"
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const valid_options = [
'loopGuardTimeout',
'preserveComments',
'preserveWhitespace',
'cssHash'
'cssHash',
'disableVersionTagging'
gtm-nayan marked this conversation as resolved.
Show resolved Hide resolved
];
const valid_css_values = [true, false, 'injected', 'external', 'none'];
const regex_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
Expand Down
12 changes: 12 additions & 0 deletions packages/svelte/src/compiler/compile/render_dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,5 +604,17 @@ export default function dom(component, options) {
);
}
}

if (options.disableVersionTagging !== true) {
component.imports.unshift({
type: 'ImportDeclaration',
specifiers: [],
source: {
type: 'Literal',
value: `${options.sveltePath ?? 'svelte'}/internal/tag-version`
}
});
}

return { js: flatten(body), css };
}
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export interface CompileOptions {

preserveComments?: boolean;
preserveWhitespace?: boolean;
disableVersionTagging?: boolean;
}

export interface ParserOptions {
Expand Down
5 changes: 5 additions & 0 deletions packages/svelte/src/runtime/internal/tag-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { VERSION } from '../../shared/version.js';

if (typeof window !== 'undefined')
// @ts-ignore
(window.__svelte ||= { versions: new Set() }).versions.add(VERSION);
5 changes: 5 additions & 0 deletions packages/svelte/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export function create_loader(compileOptions, cwd) {
resolved = `${svelte_path}src/runtime/index.js`;
}

if (id === 'svelte/internal/tag-version') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps this should be called disclose-version to match the option name

return `${svelte_path}src/runtime/internal/tag-version.js`;
}

if (id.startsWith('svelte/')) {
resolved = `${svelte_path}src/runtime/${id.slice(7)}/index.js`;
}
Expand All @@ -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");'
Expand Down
7 changes: 6 additions & 1 deletion packages/svelte/test/js/js-output.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions packages/svelte/test/runtime-browser/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +64,8 @@ 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: index
},
Expand Down Expand Up @@ -169,7 +172,8 @@ 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: index
},
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ 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`);
}
Expand Down