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

pull/11627/head
dominikg 2 years ago
parent 573b9f1d74
commit 313d8c9f47

@ -0,0 +1,5 @@
---
"svelte": patch
---
feat: introduce rootDir option and strip rootDir from filename in dev

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: rename **svelte_meta.filename to **svelte_meta.file to align with svelte4

@ -473,10 +473,12 @@ export function client_component(source, analysis, options) {
if (options.dev) { if (options.dev) {
if (options.filename) { if (options.filename) {
let filename = options.filename; let filename = options.filename;
if (/(\/|\w:)/.test(options.filename)) { if(options.rootDir && filename.startsWith(options.rootDir)) {
// filename is absolute — truncate it filename = filename.replace(options.rootDir,'');
const parts = filename.split(/[/\\]/); if(filename !== options.filename) {
filename = parts.length > 3 ? ['...', ...parts.slice(-3)].join('/') : 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 // add `App.filename = 'App.svelte'` so that we can print useful messages later

@ -216,12 +216,19 @@ export interface ModuleCompileOptions {
* Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically. * Used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
*/ */
filename?: string; 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 // 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']; filename: ModuleCompileOptions['filename'];
rootDir: ModuleCompileOptions['rootDir'];
}; };
export type ValidatedCompileOptions = ValidatedModuleCompileOptions & export type ValidatedCompileOptions = ValidatedModuleCompileOptions &

@ -10,6 +10,10 @@ import * as w from './warnings.js';
const common = { const common = {
filename: string(undefined), 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?.()),
dev: boolean(false), dev: boolean(false),
generate: validator('client', (input, keypath) => { generate: validator('client', (input, keypath) => {

@ -34,7 +34,7 @@ export function add_locations(fn, filename, locations) {
function assign_location(element, filename, location) { function assign_location(element, filename, location) {
// @ts-expect-error // @ts-expect-error
element.__svelte_meta = { element.__svelte_meta = {
loc: { filename, line: location[0], column: location[1] } loc: { file: filename, line: location[0], column: location[1] }
}; };
if (location[2]) { if (location[2]) {

@ -116,7 +116,7 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace, locat
// @ts-expect-error // @ts-expect-error
element.__svelte_meta = { element.__svelte_meta = {
loc: { loc: {
filename, file: filename,
line: location[0], line: location[0],
column: location[1] column: location[1]
} }

@ -26,7 +26,7 @@ export default test({
assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`); assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`);
assert.deepEqual(warnings, [ 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'
]); ]);
} }
}); });

@ -8,6 +8,6 @@ export default test({
}, },
warnings: [ 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'
] ]
}); });

@ -33,7 +33,7 @@ export default test({
assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button><button>clicks: 1</button>`); assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button><button>clicks: 1</button>`);
assert.deepEqual(warnings, [ 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'
]); ]);
} }
}); });

@ -14,14 +14,14 @@ export default test({
// @ts-expect-error // @ts-expect-error
assert.deepEqual(ps[0].__svelte_meta.loc, { 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, line: 7,
column: 0 column: 0
}); });
// @ts-expect-error // @ts-expect-error
assert.deepEqual(ps[1].__svelte_meta.loc, { 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, line: 13,
column: 0 column: 0
}); });
@ -32,7 +32,7 @@ export default test({
// @ts-expect-error // @ts-expect-error
assert.deepEqual(strong.__svelte_meta.loc, { 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, line: 10,
column: 1 column: 1
}); });

@ -14,14 +14,14 @@ export default test({
// @ts-expect-error // @ts-expect-error
assert.deepEqual(ps[0].__svelte_meta.loc, { 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, line: 7,
column: 0 column: 0
}); });
// @ts-expect-error // @ts-expect-error
assert.deepEqual(ps[1].__svelte_meta.loc, { 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, line: 13,
column: 0 column: 0
}); });
@ -32,7 +32,7 @@ export default test({
// @ts-expect-error // @ts-expect-error
assert.deepEqual(strong.__svelte_meta.loc, { 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, line: 10,
column: 1 column: 1
}); });

Loading…
Cancel
Save