Skip to content

Commit 692cb61

Browse files
authored
(fix) handle $store imported in module script (#1173)
When module script is the only script #1172
1 parent f7c311b commit 692cb61

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

packages/svelte2tsx/src/svelte2tsx/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,11 @@ export function svelte2tsx(
372372
processModuleScriptTag(
373373
str,
374374
moduleScriptTag,
375-
new ImplicitStoreValues(implicitStoreValues.getAccessedStores(), renderFunctionStart)
375+
new ImplicitStoreValues(
376+
implicitStoreValues.getAccessedStores(),
377+
renderFunctionStart,
378+
scriptTag ? undefined : (input) => `</>;${input}<>`
379+
)
376380
);
377381
}
378382

packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitStoreValues.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export class ImplicitStoreValues {
2121
public addReactiveDeclaration = this.reactiveDeclarations.push.bind(this.reactiveDeclarations);
2222
public addImportStatement = this.importStatements.push.bind(this.importStatements);
2323

24-
constructor(storesResolvedInTemplate: string[] = [], private renderFunctionStart: number) {
24+
constructor(
25+
storesResolvedInTemplate: string[] = [],
26+
private renderFunctionStart: number,
27+
private storeFromImportsWrapper = (input: string) => input
28+
) {
2529
storesResolvedInTemplate.forEach(this.addStoreAcess);
2630
}
2731

@@ -103,8 +107,8 @@ export class ImplicitStoreValues {
103107
return;
104108
}
105109

106-
const storeDeclarations = surroundWithIgnoreComments(
107-
this.createStoreDeclarations(storeNames)
110+
const storeDeclarations = this.storeFromImportsWrapper(
111+
surroundWithIgnoreComments(this.createStoreDeclarations(storeNames))
108112
);
109113

110114
str.appendRight(this.renderFunctionStart, storeDeclarations);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
///<reference types="svelte" />
2+
<></>;
3+
import { store1 } from './somewhere';
4+
const store2 = ''/*Ωignore_startΩ*/;let $store2 = __sveltets_1_store_get(store2);/*Ωignore_endΩ*/;
5+
;<></>;function render() {
6+
<></>;/*Ωignore_startΩ*/;let $store1 = __sveltets_1_store_get(store1);/*Ωignore_endΩ*/<>
7+
8+
{(__sveltets_1_store_get(store1), $store1)}
9+
{(__sveltets_1_store_get(store2), $store2)}</>
10+
return { props: {}, slots: {}, getters: {}, events: {} }}
11+
12+
export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script context="module">
2+
import { store1 } from './somewhere';
3+
const store2 = '';
4+
</script>
5+
6+
{$store1}
7+
{$store2}

0 commit comments

Comments
 (0)