@@ -62,67 +62,56 @@ function applyForcedOptions(config, forcedOptions) {
62
62
return appliedChanges ;
63
63
}
64
64
65
- function setupTypeScriptPreprocessor ( svelteConfig , isBuild ) {
66
- if ( ! svelteConfig . preprocess ) {
67
- svelteConfig . preprocess = [ ] ;
68
- } else if ( ! Array . isArray ( svelteConfig . preprocess ) ) {
69
- svelteConfig . preprocess = [ svelteConfig . preprocess ] ;
65
+ async function hasTypescriptPreprocessor ( svelteConfig ) {
66
+ if ( ! Array . isArray ( svelteConfig . preprocess ) ) {
67
+ return false ;
70
68
}
71
- if ( svelteConfig . preprocess . some ( ( preprocessor ) => preprocessor . defaultLanguages ) ) {
72
- log . error (
73
- 'found svelte-preprocess automatic preprocessor in your svelte config. This is not compatible with svite cli --typescript option.' ,
74
- ) ;
75
- throw new Error ( 'no dual typescript setup allowed' ) ;
76
- } else if (
77
- svelteConfig . preprocess . some ( ( preprocessor ) => preprocessor . script && preprocessor . script . toString ( ) . indexOf ( 'typescript' ) > - 1 )
78
- ) {
79
- log . error (
80
- 'found a script preprocessor for typescript in your svelte config. This is not compatible with svite cli --typescript option' ,
81
- ) ;
82
- throw new Error ( 'no dual typescript setup allowed' ) ;
83
- } else {
84
- log . debug ( 'no typescript preprocessor found, adding svelte-preprocess typescript' ) ;
69
+ if ( ! svelteConfig . preprocess [ 0 ] . script ) {
70
+ return false ;
71
+ }
72
+ try {
73
+ const result = await svelteConfig . preprocess [ 0 ] . script ( { content :'const x:string="y";export{}' , attributes :{ lang :'ts' } , filename :'Test.svelte' } , { lang :"ts" } , "Test.svelte" )
74
+ log . info ( 'result' , result ) ;
75
+ return true ;
76
+ } catch ( e ) {
77
+ log . debug ( 'script preprocessor at index 0 failed to transform typescript' , e ) ;
78
+ return false ;
79
+ }
80
+ }
81
+ async function setupTypeScriptPreprocessor ( svelteConfig , isBuild ) {
82
+ const hasTypescript = await hasTypescriptPreprocessor ( svelteConfig ) ;
83
+ if ( ! hasTypescript ) {
84
+ throw new Error ( 'svite -ts requires a typescript preprocessor at index 0 in svelte.config.js preprocess:[]' )
85
+ }
86
+ if ( ! isBuild ) {
87
+
85
88
// unfortunately esbuild preprocess does not work properly with imports of svelte components as of now
86
89
// svelteConfig.preprocess.unshift(require('./tools/svelte-preprocess-ts-vite'));
87
- const sveltePreprocess = require ( 'svelte-preprocess' ) ;
88
- const preprocessOptions = {
89
- // disable all but typescript
90
- babel : false ,
91
- scss : false ,
92
- sass : false ,
93
- less : false ,
94
- stylus : false ,
95
- postcss : false ,
96
- coffeescript : false ,
97
- pug : false ,
98
- globalStyle : false ,
99
- replace : false ,
100
- } ;
101
- if ( ! isBuild ) {
90
+
91
+
92
+ const { typescript} = require ( 'svelte-preprocess' ) ;
102
93
const devTS = {
103
- // in dev: force ultra modern and add vite/dist/importMeta to enable custom hmr
104
- compilerOptions : {
105
- module : 'esnext' ,
106
- target : 'esnext' ,
107
- types : [ 'svelte' , 'vite/dist/importMeta' ] ,
108
- } ,
94
+ module : 'esnext' ,
95
+ target : 'es2017' ,
96
+ moduleResolution : "node" ,
97
+ importsNotUsedAsValues : "error" ,
98
+ types : [ 'svelte' , 'vite/dist/importMeta' ] ,
99
+ sourceMap : true
109
100
} ;
110
- log . info ( 'overriding typescript config to support hmr in vite' , devTS ) ;
111
- preprocessOptions . typescript = devTS ;
101
+ log . warn ( 'overriding typescript preprocessor to support hmr in vite' , devTS ) ;
102
+ svelteConfig . preprocess [ 0 ] = typescript ( devTS ) ;
112
103
}
113
- svelteConfig . preprocess . unshift ( sveltePreprocess ( preprocessOptions ) ) ;
114
- }
115
104
}
116
105
117
- function finalizeConfig ( config , type ) {
106
+ async function finalizeConfig ( config , type ) {
118
107
const isProduction = process . env . NODE_ENV === 'production' ;
119
108
const isDevServer = ! ! config . vite ;
120
109
const isBuild = type === 'build' ;
121
110
const svelteConfig = config [ type ] ;
122
111
const forcedOptions = forcedSvelteOptions [ type ] ;
123
112
const isTypescript = config . svite . typescript ;
124
113
if ( isTypescript ) {
125
- setupTypeScriptPreprocessor ( svelteConfig , isBuild ) ;
114
+ await setupTypeScriptPreprocessor ( svelteConfig , isBuild ) ;
126
115
}
127
116
if ( isBuild ) {
128
117
forcedOptions . dev = ( isDevServer && ! ! config . dev . hot ) || ! isProduction ;
@@ -245,8 +234,8 @@ function updateViteConfig(config) {
245
234
log . debug . enabled && log . debug ( 'vite config' , viteConfig ) ;
246
235
}
247
236
248
- function createRollupPluginSvelteHot ( config , type ) {
249
- const finalSvelteConfig = finalizeConfig ( config , type ) ;
237
+ async function createRollupPluginSvelteHot ( config , type ) {
238
+ const finalSvelteConfig = await finalizeConfig ( config , type ) ;
250
239
return rollupPluginSvelteHot ( finalSvelteConfig ) ;
251
240
}
252
241
@@ -261,7 +250,7 @@ function createDev(config) {
261
250
async ( { config : viteConfig } ) => {
262
251
config . vite = viteConfig ;
263
252
updateViteConfig ( config ) ;
264
- devPlugin = createRollupPluginSvelteHot ( config , 'dev' ) ;
253
+ devPlugin = await createRollupPluginSvelteHot ( config , 'dev' ) ;
265
254
} ,
266
255
] ;
267
256
@@ -309,22 +298,22 @@ function createDev(config) {
309
298
function rollupPluginDeferred ( name , createPlugin ) {
310
299
const wrapper = {
311
300
name,
312
- options : function ( options ) {
313
- const plugin = createPlugin ( ) ;
301
+ options : async function ( options ) {
302
+ const plugin = await createPlugin ( ) ;
314
303
Object . keys ( plugin ) . forEach ( ( key ) => {
315
304
if ( key !== 'options' ) {
316
305
wrapper [ key ] = plugin [ key ] ;
317
306
}
318
307
} ) ;
319
- return plugin . options ? plugin . options . apply ( this , options ) : null ;
308
+ return plugin . options ? await plugin . options . apply ( this , options ) : options ;
320
309
} ,
321
310
} ;
322
311
return wrapper ;
323
312
}
324
313
325
314
function createBuildPlugins ( config ) {
326
- const buildPlugin = rollupPluginDeferred ( 'svelte' , ( ) => {
327
- return createRollupPluginSvelteHot ( config , 'build' ) ;
315
+ const buildPlugin = rollupPluginDeferred ( 'svelte' , async ( ) => {
316
+ return await createRollupPluginSvelteHot ( config , 'build' ) ;
328
317
} ) ;
329
318
330
319
const rollupPluginDedupeSvelte = rollupPluginDeferred ( 'node-resolve' , ( ) =>
0 commit comments