From b6b7f5c8a46c18712c502a41341a81e586e9e97c Mon Sep 17 00:00:00 2001 From: Vaibhav Rai Date: Wed, 13 Oct 2021 01:43:44 +0530 Subject: [PATCH] [fix] properly respect sveltePath when rewriting user imports (#6842) --- src/compiler/compile/create_module.ts | 21 +++--- test/js/samples/custom-svelte-path/_config.js | 5 ++ .../js/samples/custom-svelte-path/expected.js | 64 +++++++++++++++++++ .../samples/custom-svelte-path/input.svelte | 9 +++ 4 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 test/js/samples/custom-svelte-path/_config.js create mode 100644 test/js/samples/custom-svelte-path/expected.js create mode 100644 test/js/samples/custom-svelte-path/input.svelte diff --git a/src/compiler/compile/create_module.ts b/src/compiler/compile/create_module.ts index 037b2b396e..c9f925c4b4 100644 --- a/src/compiler/compile/create_module.ts +++ b/src/compiler/compile/create_module.ts @@ -26,7 +26,7 @@ export default function create_module( helpers.sort((a, b) => (a.name < b.name) ? -1 : 1); globals.sort((a, b) => (a.name < b.name) ? -1 : 1); - + const formatter = wrappers[format]; if (!formatter) { @@ -43,7 +43,7 @@ function edit_source(source, sveltePath) { } function get_internal_globals( - globals: Array<{ name: string; alias: Identifier }>, + globals: Array<{ name: string; alias: Identifier }>, helpers: Array<{ name: string; alias: Identifier }> ) { return globals.length > 0 && { @@ -66,7 +66,7 @@ function get_internal_globals( init: helpers.find(({ name }) => name === 'globals').alias }] }; -} +} function esm( program: any, @@ -93,12 +93,15 @@ function esm( const internal_globals = get_internal_globals(globals, helpers); // edit user imports - imports.forEach(node => { - node.source.value = edit_source(node.source.value, sveltePath); - }); - exports_from.forEach(node => { - node.source!.value = edit_source(node.source!.value, sveltePath); - }); + function rewrite_import(node) { + const value = edit_source(node.source.value, sveltePath); + if (node.source.value !== value) { + node.source.value = value; + node.source.raw = null; + } + } + imports.forEach(rewrite_import); + exports_from.forEach(rewrite_import); const exports = module_exports.length > 0 && { type: 'ExportNamedDeclaration', diff --git a/test/js/samples/custom-svelte-path/_config.js b/test/js/samples/custom-svelte-path/_config.js new file mode 100644 index 0000000000..19a7366845 --- /dev/null +++ b/test/js/samples/custom-svelte-path/_config.js @@ -0,0 +1,5 @@ +export default { + options: { + sveltePath: 'a/b/svelte' + } +}; diff --git a/test/js/samples/custom-svelte-path/expected.js b/test/js/samples/custom-svelte-path/expected.js new file mode 100644 index 0000000000..3210f216d3 --- /dev/null +++ b/test/js/samples/custom-svelte-path/expected.js @@ -0,0 +1,64 @@ +/* generated by Svelte vX.Y.Z */ +import { + SvelteComponent, + append, + detach, + element, + init, + insert, + noop, + safe_not_equal, + set_data, + text +} from "a/b/svelte/internal"; + +import { onMount } from "a/b/svelte"; + +function create_fragment(ctx) { + let h1; + let t0; + let t1; + let t2; + + return { + c() { + h1 = element("h1"); + t0 = text("Hello "); + t1 = text(/*name*/ ctx[0]); + t2 = text("!"); + }, + m(target, anchor) { + insert(target, h1, anchor); + append(h1, t0); + append(h1, t1); + append(h1, t2); + }, + p(ctx, [dirty]) { + if (dirty & /*name*/ 1) set_data(t1, /*name*/ ctx[0]); + }, + i: noop, + o: noop, + d(detaching) { + if (detaching) detach(h1); + } + }; +} + +function instance($$self, $$props, $$invalidate) { + let name = ''; + + onMount(() => { + $$invalidate(0, name = 'world'); + }); + + return [name]; +} + +class Component extends SvelteComponent { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal, {}); + } +} + +export default Component; \ No newline at end of file diff --git a/test/js/samples/custom-svelte-path/input.svelte b/test/js/samples/custom-svelte-path/input.svelte new file mode 100644 index 0000000000..03d31e7c7b --- /dev/null +++ b/test/js/samples/custom-svelte-path/input.svelte @@ -0,0 +1,9 @@ + + +

Hello {name}!

\ No newline at end of file