Skip to content

fix context export not working if conflicts with instance variable #3991

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

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
48 changes: 21 additions & 27 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ export default class Component {
this.tag = this.name.name;
}

this.walk_module_js_pre_template();
this.walk_module_js();
this.walk_instance_js_pre_template();

this.fragment = new Fragment(this, ast.html);
this.name = this.get_unique_name(name);

this.walk_module_js_post_template();
this.walk_instance_js_post_template();

if (!compile_options.customElement) this.stylesheet.reify();
Expand Down Expand Up @@ -517,7 +516,7 @@ export default class Component {
});
}

walk_module_js_pre_template() {
walk_module_js() {
const component = this;
const script = this.ast.module;
if (!script) return;
Expand Down Expand Up @@ -568,6 +567,25 @@ export default class Component {
});
}
});

const { body } = script.content;
let i = body.length;
while (--i >= 0) {
const node = body[i];
if (node.type === 'ImportDeclaration') {
this.extract_imports(node);
body.splice(i, 1);
}

if (/^Export/.test(node.type)) {
const replacement = this.extract_exports(node);
if (replacement) {
body[i] = replacement;
} else {
body.splice(i, 1);
}
}
}
}

walk_instance_js_pre_template() {
Expand Down Expand Up @@ -665,30 +683,6 @@ export default class Component {
this.track_references_and_mutations();
}

walk_module_js_post_template() {
const script = this.ast.module;
if (!script) return;

const { body } = script.content;
let i = body.length;
while (--i >= 0) {
const node = body[i];
if (node.type === 'ImportDeclaration') {
this.extract_imports(node);
body.splice(i, 1);
}

if (/^Export/.test(node.type)) {
const replacement = this.extract_exports(node);
if (replacement) {
body[i] = replacement;
} else {
body.splice(i, 1);
}
}
}
}

walk_instance_js_post_template() {
const script = this.ast.instance;
if (!script) return;
Expand Down
7 changes: 7 additions & 0 deletions test/runtime/samples/module-context-export/Foo.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script context="module">
export const foo = 42;
</script>
<script>
let foo = 100;
console.log(foo);
</script>
3 changes: 3 additions & 0 deletions test/runtime/samples/module-context-export/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: `<p>(42)(99)</p>`
};
6 changes: 6 additions & 0 deletions test/runtime/samples/module-context-export/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
import { foo } from './Foo.svelte';
export let bar = 99;
</script>

<p>({foo})({bar})</p>