Skip to content

Commit fbec63f

Browse files
committed
Fix for import type statement causes optimizeImports error carbon-design-system#80 [carbon-design-system#80]
1 parent 657e95a commit fbec63f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Diff for: src/preprocessors/optimize-imports.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const optimizeImports: SveltePreprocessor<"script"> = () => {
3232
if (/node_modules/.test(filename)) return;
3333

3434
// Wrap the content in a `<script>` tag to parse it with the Svelte parser.
35-
const content = `<script>${raw}</script>`;
35+
const content = `<script lang="ts">${raw}</script>`;
3636
const s = new MagicString(content);
3737

3838
walk(parse(content), {
@@ -60,7 +60,7 @@ export const optimizeImports: SveltePreprocessor<"script"> = () => {
6060
},
6161
});
6262

63-
s.replace(/^<script>/, "");
63+
s.replace(/^<script lang="ts">/, "");
6464
s.replace(/<\/script>$/, "");
6565

6666
return {

Diff for: tests/optimize-imports.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,18 @@ import Analytics from "carbon-pictograms-svelte/lib/Analytics.svelte";`);
138138
import Accordion from "carbon-components-svelte/src/Accordion/Accordion.svelte";
139139
`);
140140
});
141+
142+
test("import type statements should be preserved", () => {
143+
expect(
144+
preprocess({
145+
content: `
146+
import { Theme } from "carbon-components-svelte";
147+
import type { CarbonTheme } from "carbon-components-svelte/src/Theme/Theme.svelte";
148+
`,
149+
}),
150+
).toEqual(`
151+
import Theme from "carbon-components-svelte/src/Theme/Theme.svelte";
152+
import type { CarbonTheme } from "carbon-components-svelte/src/Theme/Theme.svelte";
153+
`);
154+
});
141155
});

0 commit comments

Comments
 (0)