1
1
import { resolve } from 'path' ;
2
2
3
- import { compile } from 'svelte/compiler' ;
3
+ import { compile , VERSION } from 'svelte/compiler' ;
4
4
5
5
import sveltePreprocess from '../../src' ;
6
6
import { loadTsconfig } from '../../src/transformers/typescript' ;
@@ -16,6 +16,8 @@ import type { Diagnostic } from 'typescript';
16
16
17
17
spyConsole ( { silent : true } ) ;
18
18
19
+ const IS_SVELTE_5_PLUS = Number ( VERSION . split ( '.' ) [ 0 ] ) >= 5 ;
20
+
19
21
const EXPECTED_SCRIPT = getFixtureContent ( 'script.js' ) ;
20
22
21
23
const autoProcessTS = ( content : string , compilerOptions ?: any ) => {
@@ -113,34 +115,6 @@ describe('transformer - typescript', () => {
113
115
expect ( code ) . toContain ( getFixtureContent ( 'script.js' ) ) ;
114
116
} ) ;
115
117
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
-
144
118
it ( 'should keep all value imports with verbatimModuleSyntax' , async ( ) => {
145
119
const tpl = getFixtureContent ( 'PreserveValueImports.svelte' ) ;
146
120
@@ -192,18 +166,6 @@ describe('transformer - typescript', () => {
192
166
expect ( code ) . toBe ( `<script lang="ts" context="module"></script>` ) ;
193
167
} ) ;
194
168
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
-
207
169
it ( 'should produce sourcemap' , async ( ) => {
208
170
const tpl = getFixtureContent ( 'TypeScriptImportsModule.svelte' ) ;
209
171
@@ -217,20 +179,6 @@ describe('transformer - typescript', () => {
217
179
expect ( map ) . toHaveProperty ( 'sources' , [ 'App.svelte' ] ) ;
218
180
} ) ;
219
181
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
-
234
182
it ( 'supports extends field' , ( ) => {
235
183
const { options } = loadTsconfig ( { } , getTestAppFilename ( ) , {
236
184
tsconfigFile : './test/fixtures/tsconfig.extends1.json' ,
@@ -280,4 +228,60 @@ describe('transformer - typescript', () => {
280
228
expect ( code ) . not . toContain ( '||=' ) ;
281
229
} ) ;
282
230
} ) ;
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
+ } ) ;
283
287
} ) ;
0 commit comments