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 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
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
---

feat: add version info to `window`. You can opt out by setting `discloseVersion` to `false` in the compiler options
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>
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/disclose-version": {
"import": "./src/runtime/internal/disclose-version/index.js"
},
"./transition": {
"types": "./types/index.d.ts",
"import": "./src/runtime/transition/index.js"
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/scripts/generate-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ fs.writeFileSync(
* @type {string}
*/
export const VERSION = '${pkg.version}';
export const PUBLIC_VERSION = '${pkg.version.split('.')[0]}';
`
);
7 changes: 6 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',
'discloseVersion'
];
const valid_css_values = [true, false, 'injected', 'external', 'none'];
const regex_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
Expand Down Expand Up @@ -112,6 +113,10 @@ function validate_options(options, warnings) {
throw new Error(`Invalid namespace '${namespace}'`);
}
}

if (options.discloseVersion == undefined) {
options.discloseVersion = true;
}
}

/**
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.discloseVersion === true) {
component.imports.unshift({
type: 'ImportDeclaration',
specifiers: [],
source: {
type: 'Literal',
value: `${options.sveltePath ?? 'svelte'}/internal/disclose-version`
}
});
}

return { js: flatten(body), css };
}
6 changes: 6 additions & 0 deletions packages/svelte/src/compiler/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ 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;
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
}

export interface ParserOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PUBLIC_VERSION } from '../../../shared/version.js';

if (typeof window !== 'undefined')
// @ts-ignore
(window.__svelte || (window.__svelte = { v: new Set() })).v.add(PUBLIC_VERSION);
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/svelte/src/shared/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
* @type {string}
*/
export const VERSION = '4.0.0-next.2';
export const PUBLIC_VERSION = '4';
1 change: 1 addition & 0 deletions packages/svelte/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,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(
{
discloseVersion: false
},
config.options || {}
);

actual = svelte
.compile(input, options)
Expand Down
11 changes: 3 additions & 8 deletions packages/svelte/test/runtime-browser/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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');

/** @type {import('@playwright/test').Browser} */
let browser;
Expand Down Expand Up @@ -62,9 +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': path.resolve(__dirname, 'assert.js'),
'svelte/internal': internal,
svelte: index
'assert.js': assert_file
},
plugins: [
{
Expand Down Expand Up @@ -169,9 +166,7 @@ async function run_custom_elements_test(dir) {
entryPoints: [`${cwd}/test.js`],
write: false,
alias: {
'assert.js': path.resolve(__dirname, 'assert.js'),
'svelte/internal': internal,
svelte: index
'assert.js': assert_file
},
plugins: [
{
Expand Down
Loading