11import { resolve } from 'path' ;
22
3- import { compile } from 'svelte/compiler' ;
3+ import { compile , VERSION } from 'svelte/compiler' ;
44
55import sveltePreprocess from '../../src' ;
66import { loadTsconfig } from '../../src/transformers/typescript' ;
@@ -16,6 +16,8 @@ import type { Diagnostic } from 'typescript';
1616
1717spyConsole ( { silent : true } ) ;
1818
19+ const IS_SVELTE_5_PLUS = Number ( VERSION . split ( '.' ) [ 0 ] ) >= 5 ;
20+
1921const EXPECTED_SCRIPT = getFixtureContent ( 'script.js' ) ;
2022
2123const autoProcessTS = ( content : string , compilerOptions ?: any ) => {
@@ -113,34 +115,6 @@ describe('transformer - typescript', () => {
113115 expect ( code ) . toContain ( getFixtureContent ( 'script.js' ) ) ;
114116 } ) ;
115117
116- it ( 'should strip unused and type imports' , async ( ) => {
117- const tpl = getFixtureContent ( 'TypeScriptImports.svelte' ) ;
118-
119- const opts = sveltePreprocess ( {
120- typescript : { tsconfigFile : false } ,
121- } ) ;
122-
123- const { code } = await preprocess ( tpl , opts ) ;
124-
125- // Test that imports are properly preserved
126- expect ( code ) . toContain ( `import { fly } from "svelte/transition"` ) ;
127- expect ( code ) . toContain ( `import { flip } from "svelte/animate"` ) ;
128- expect ( code ) . toContain ( `import Nested from "./Nested.svelte"` ) ;
129- expect ( code ) . toContain ( `import { hello } from "./script"` ) ;
130- expect ( code ) . toContain ( `import { AValue } from "./types"` ) ;
131- expect ( code ) . toContain (
132- `import { storeTemplateOnly, storeScriptOnly, store0 } from "./store"` ,
133- ) ;
134- expect ( code ) . toContain (
135- `import { onlyUsedInModuleScript } from "./modulescript";` ,
136- ) ;
137- expect ( code ) . toContain (
138- `import { storeModuleTemplateOnly, storeModuleScriptOnly } from "./store";` ,
139- ) ;
140- // Test that comments are properly preserved
141- expect ( code ) . toContain ( '<!-- Some comment -->' ) ;
142- } ) ;
143-
144118 it ( 'should keep all value imports with verbatimModuleSyntax' , async ( ) => {
145119 const tpl = getFixtureContent ( 'PreserveValueImports.svelte' ) ;
146120
@@ -192,18 +166,6 @@ describe('transformer - typescript', () => {
192166 expect ( code ) . toBe ( `<script lang="ts" context="module"></script>` ) ;
193167 } ) ;
194168
195- it ( 'should strip unused and type imports in context="module" tags' , async ( ) => {
196- const tpl = getFixtureContent ( 'TypeScriptImportsModule.svelte' ) ;
197-
198- const opts = sveltePreprocess ( {
199- typescript : { tsconfigFile : false } ,
200- } ) ;
201-
202- const { code } = await preprocess ( tpl , opts ) ;
203-
204- expect ( code ) . toContain ( `import { AValue } from "./types";` ) ;
205- } ) ;
206-
207169 it ( 'should produce sourcemap' , async ( ) => {
208170 const tpl = getFixtureContent ( 'TypeScriptImportsModule.svelte' ) ;
209171
@@ -217,20 +179,6 @@ describe('transformer - typescript', () => {
217179 expect ( map ) . toHaveProperty ( 'sources' , [ 'App.svelte' ] ) ;
218180 } ) ;
219181
220- it ( 'should work when tsconfig contains outDir' , async ( ) => {
221- const opts = sveltePreprocess ( {
222- typescript : {
223- tsconfigFile : './test/fixtures/tsconfig.outdir.json' ,
224- handleMixedImports : true ,
225- } ,
226- sourceMap : true ,
227- } ) ;
228-
229- const preprocessed = await preprocess ( template , opts ) ;
230-
231- expect ( preprocessed . toString ?.( ) ) . toContain ( EXPECTED_SCRIPT ) ;
232- } ) ;
233-
234182 it ( 'supports extends field' , ( ) => {
235183 const { options } = loadTsconfig ( { } , getTestAppFilename ( ) , {
236184 tsconfigFile : './test/fixtures/tsconfig.extends1.json' ,
@@ -280,4 +228,60 @@ describe('transformer - typescript', () => {
280228 expect ( code ) . not . toContain ( '||=' ) ;
281229 } ) ;
282230 } ) ;
231+
232+ ( IS_SVELTE_5_PLUS ? describe . skip : describe ) ( 'mixed imports' , ( ) => {
233+ it ( 'should strip unused and type imports' , async ( ) => {
234+ const tpl = getFixtureContent ( 'TypeScriptImports.svelte' ) ;
235+
236+ const opts = sveltePreprocess ( {
237+ typescript : { tsconfigFile : false } ,
238+ } ) ;
239+
240+ const { code } = await preprocess ( tpl , opts ) ;
241+
242+ // Test that imports are properly preserved
243+ expect ( code ) . toContain ( `import { fly } from "svelte/transition"` ) ;
244+ expect ( code ) . toContain ( `import { flip } from "svelte/animate"` ) ;
245+ expect ( code ) . toContain ( `import Nested from "./Nested.svelte"` ) ;
246+ expect ( code ) . toContain ( `import { hello } from "./script"` ) ;
247+ expect ( code ) . toContain ( `import { AValue } from "./types"` ) ;
248+ expect ( code ) . toContain (
249+ `import { storeTemplateOnly, storeScriptOnly, store0 } from "./store"` ,
250+ ) ;
251+ expect ( code ) . toContain (
252+ `import { onlyUsedInModuleScript } from "./modulescript";` ,
253+ ) ;
254+ expect ( code ) . toContain (
255+ `import { storeModuleTemplateOnly, storeModuleScriptOnly } from "./store";` ,
256+ ) ;
257+ // Test that comments are properly preserved
258+ expect ( code ) . toContain ( '<!-- Some comment -->' ) ;
259+ } ) ;
260+
261+ it ( 'should strip unused and type imports in context="module" tags' , async ( ) => {
262+ const tpl = getFixtureContent ( 'TypeScriptImportsModule.svelte' ) ;
263+
264+ const opts = sveltePreprocess ( {
265+ typescript : { tsconfigFile : false } ,
266+ } ) ;
267+
268+ const { code } = await preprocess ( tpl , opts ) ;
269+
270+ expect ( code ) . toContain ( `import { AValue } from "./types";` ) ;
271+ } ) ;
272+
273+ it ( 'should work when tsconfig contains outDir' , async ( ) => {
274+ const opts = sveltePreprocess ( {
275+ typescript : {
276+ tsconfigFile : './test/fixtures/tsconfig.outdir.json' ,
277+ handleMixedImports : true ,
278+ } ,
279+ sourceMap : true ,
280+ } ) ;
281+
282+ const preprocessed = await preprocess ( template , opts ) ;
283+
284+ expect ( preprocessed . toString ?.( ) ) . toContain ( EXPECTED_SCRIPT ) ;
285+ } ) ;
286+ } ) ;
283287} ) ;
0 commit comments