Skip to content

Commit 4cb37f2

Browse files
authored
Preserve all typescript imports, not only .svelte
Hi! Thanks for the project. Library already preserves all `.svelte` imports. This PR change behaviour to preserve _all_ imports, because imported variables can be not only svelte components but also values which svelte template actually uses. Example: ``` <script lang="ts"> import { writable } from "svelte/store"; import { count } from "./stores"; import Counter from "./Counter.svelte"; export let name: number; export let age: number; </script> <h1>Hello {name} ({age})!</h1> <p> <Counter /> <Counter value={1}>Counter 1</Counter> <Counter value={$count} step={3}>Counter 2</Counter> </p> ``` Without the fix compiler throws: ``` (!) svelte plugin: 'count' is not defined src/App.svelte <Counter /> <Counter value={1}>Counter 1</Counter> <Counter value={$count} step={3}>Counter 2</Counter> ^ </p> ``` Relates to: - PaulMaly#4 - microsoft/TypeScript#9191
1 parent 552322c commit 4cb37f2

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

packages/lib/src/svelte-ts-preprocess.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ function importTransformer<T extends ts.Node>(): ts.TransformerFactory<T> {
66
return context => {
77
const visit: ts.Visitor = node => {
88
if (ts.isImportDeclaration(node)) {
9-
const text = node.moduleSpecifier.getText().slice(0, -1)
10-
if (text.endsWith('.svelte')) {
11-
// console.log('------- svelte import -----')
12-
// console.log(node.getFullText().trim())
13-
return ts.createImportDeclaration(
14-
node.decorators,
15-
node.modifiers,
16-
node.importClause,
17-
node.moduleSpecifier
18-
)
19-
}
9+
return ts.createImportDeclaration(
10+
node.decorators,
11+
node.modifiers,
12+
node.importClause,
13+
node.moduleSpecifier
14+
)
2015
}
2116
return ts.visitEachChild(node, child => visit(child), context)
2217
}

0 commit comments

Comments
 (0)