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

feat: add rootDir option and set __svelte_meta.file like in svelte4 #11627

Merged
merged 11 commits into from
May 16, 2024
5 changes: 5 additions & 0 deletions .changeset/khaki-mails-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: introduce rootDir option and strip rootDir from filename in dev
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions .changeset/kind-doors-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: rename **svelte_meta.filename to **svelte_meta.file to align with svelte4
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,12 @@ export function client_component(source, analysis, options) {
if (options.dev) {
if (options.filename) {
let filename = options.filename;
if (/(\/|\w:)/.test(options.filename)) {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
// filename is absolute — truncate it
const parts = filename.split(/[/\\]/);
filename = parts.length > 3 ? ['...', ...parts.slice(-3)].join('/') : filename;
if(options.rootDir && filename.startsWith(options.rootDir)) {
filename = filename.replace(options.rootDir,'');
if(filename !== options.filename) {
// if after remove of rootDir first char is a path separator, remove that too
filename = filename.replace(/^[/\\]/,'')
}
}

// add `App.filename = 'App.svelte'` so that we can print useful messages later
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/src/compiler/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,19 @@ export interface ModuleCompileOptions {
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
*/
filename?: string;

/**
* Used for ensuring filenames don't leak filesystem information. Your bundler plugin will set it automatically.
* @default process.cwd() on node-like environments, undefined elsewhere
*/
rootDir?: string;
}

// The following two somewhat scary looking types ensure that certain types are required but can be undefined still

export type ValidatedModuleCompileOptions = Omit<Required<ModuleCompileOptions>, 'filename'> & {
export type ValidatedModuleCompileOptions = Omit<Required<ModuleCompileOptions>, 'filename' | 'rootDir'> & {
filename: ModuleCompileOptions['filename'];
rootDir: ModuleCompileOptions['rootDir'];
};

export type ValidatedCompileOptions = ValidatedModuleCompileOptions &
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/src/compiler/validate-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import * as w from './warnings.js';
const common = {
filename: string(undefined),

// default to process.cwd() where it exists to replicate svelte4 behavior
// see https://github.com/sveltejs/svelte/blob/b62fc8c8fd2640c9b99168f01b9d958cb2f7574f/packages/svelte/src/compiler/compile/Component.js#L211
rootDir: string(process?.cwd?.()),
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved

dev: boolean(false),

generate: validator('client', (input, keypath) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dev/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function add_locations(fn, filename, locations) {
function assign_location(element, filename, location) {
// @ts-expect-error
element.__svelte_meta = {
loc: { filename, line: location[0], column: location[1] }
loc: { file: filename, line: location[0], column: location[1] }
};

if (location[2]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace, locat
// @ts-expect-error
element.__svelte_meta = {
loc: {
filename,
file: filename,
line: location[0],
column: location[1]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default test({
assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`);

assert.deepEqual(warnings, [
'.../samples/non-local-mutation-discouraged/Counter.svelte mutated a value owned by .../samples/non-local-mutation-discouraged/main.svelte. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead'
'packages/svelte/tests/runtime-runes/samples/non-local-mutation-discouraged/Counter.svelte mutated a value owned by packages/svelte/tests/runtime-runes/samples/non-local-mutation-discouraged/main.svelte. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead'
]);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default test({
},

warnings: [
'.../samples/non-local-mutation-with-binding-2/Intermediate.svelte passed a value to .../samples/non-local-mutation-with-binding-2/Counter.svelte with `bind:`, but the value is owned by .../samples/non-local-mutation-with-binding-2/main.svelte. Consider creating a binding between .../samples/non-local-mutation-with-binding-2/main.svelte and .../samples/non-local-mutation-with-binding-2/Intermediate.svelte'
'packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-2/Intermediate.svelte passed a value to packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-2/Counter.svelte with `bind:`, but the value is owned by packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-2/main.svelte. Consider creating a binding between packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-2/main.svelte and packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-2/Intermediate.svelte'
]
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default test({
assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button><button>clicks: 1</button>`);

assert.deepEqual(warnings, [
'.../samples/non-local-mutation-with-binding-3/Counter.svelte mutated a value owned by .../samples/non-local-mutation-with-binding-3/main.svelte. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead'
'packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-3/Counter.svelte mutated a value owned by packages/svelte/tests/runtime-runes/samples/non-local-mutation-with-binding-3/main.svelte. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead'
]);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default test({

// @ts-expect-error
assert.deepEqual(ps[0].__svelte_meta.loc, {
filename: '.../samples/svelte-meta-dynamic/main.svelte',
file: 'packages/svelte/tests/runtime-runes/samples/svelte-meta-dynamic/main.svelte',
line: 7,
column: 0
});

// @ts-expect-error
assert.deepEqual(ps[1].__svelte_meta.loc, {
filename: '.../samples/svelte-meta-dynamic/main.svelte',
file: 'packages/svelte/tests/runtime-runes/samples/svelte-meta-dynamic/main.svelte',
line: 13,
column: 0
});
Expand All @@ -32,7 +32,7 @@ export default test({

// @ts-expect-error
assert.deepEqual(strong.__svelte_meta.loc, {
filename: '.../samples/svelte-meta-dynamic/main.svelte',
file: 'packages/svelte/tests/runtime-runes/samples/svelte-meta-dynamic/main.svelte',
line: 10,
column: 1
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default test({

// @ts-expect-error
assert.deepEqual(ps[0].__svelte_meta.loc, {
filename: '.../samples/svelte-meta/main.svelte',
file: 'packages/svelte/tests/runtime-runes/samples/svelte-meta/main.svelte',
line: 7,
column: 0
});

// @ts-expect-error
assert.deepEqual(ps[1].__svelte_meta.loc, {
filename: '.../samples/svelte-meta/main.svelte',
file: 'packages/svelte/tests/runtime-runes/samples/svelte-meta/main.svelte',
line: 13,
column: 0
});
Expand All @@ -32,7 +32,7 @@ export default test({

// @ts-expect-error
assert.deepEqual(strong.__svelte_meta.loc, {
filename: '.../samples/svelte-meta/main.svelte',
file: 'packages/svelte/tests/runtime-runes/samples/svelte-meta/main.svelte',
line: 10,
column: 1
});
Expand Down
Loading