@@ -4,7 +4,12 @@ import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';
4
4
import { handleHotUpdate } from './handle-hot-update.js' ;
5
5
import { log , logCompilerWarnings } from './utils/log.js' ;
6
6
import { createCompileSvelte } from './utils/compile.js' ;
7
- import { buildIdParser , buildModuleIdParser } from './utils/id.js' ;
7
+ import {
8
+ buildIdFilter ,
9
+ buildIdParser ,
10
+ buildModuleIdFilter ,
11
+ buildModuleIdParser
12
+ } from './utils/id.js' ;
8
13
import {
9
14
buildExtraViteConfig ,
10
15
validateInlineOptions ,
@@ -20,6 +25,7 @@ import { saveSvelteMetadata } from './utils/optimizer.js';
20
25
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js' ;
21
26
import { loadRaw } from './utils/load-raw.js' ;
22
27
import * as svelteCompiler from 'svelte/compiler' ;
28
+ import { SVELTE_VIRTUAL_STYLE_ID_REGEX } from './utils/constants.js' ;
23
29
24
30
/**
25
31
* @param {Partial<import('./public.d.ts').Options> } [inlineOptions]
@@ -42,67 +48,74 @@ export function svelte(inlineOptions) {
42
48
let viteConfig ;
43
49
/** @type {import('./types/compile.d.ts').CompileSvelte } */
44
50
let compileSvelte ;
45
- /** @type {import('./types/plugin-api.d.ts').PluginAPI } */
46
- const api = { } ;
47
- /** @type {import('vite').Plugin[] } */
48
- const plugins = [
49
- {
50
- name : 'vite-plugin-svelte' ,
51
- // make sure our resolver runs before vite internal resolver to resolve svelte field correctly
52
- enforce : 'pre' ,
53
- api,
54
- async config ( config , configEnv ) {
55
- // setup logger
56
- if ( process . env . DEBUG ) {
57
- log . setLevel ( 'debug' ) ;
58
- } else if ( config . logLevel ) {
59
- log . setLevel ( config . logLevel ) ;
60
- }
61
- // @ts -expect-error temporarily lend the options variable until fixed in configResolved
62
- options = await preResolveOptions ( inlineOptions , config , configEnv ) ;
63
- // extra vite config
64
- const extraViteConfig = await buildExtraViteConfig ( options , config ) ;
65
- log . debug ( 'additional vite config' , extraViteConfig , 'config' ) ;
66
- return extraViteConfig ;
67
- } ,
68
51
69
- configEnvironment ( name , config , opts ) {
70
- ensureConfigEnvironmentMainFields ( name , config , opts ) ;
71
- // @ts -expect-error the function above should make `resolve.mainFields` non-nullable
72
- config . resolve . mainFields . unshift ( 'svelte' ) ;
52
+ /** @type {import('vite').Plugin } */
53
+ const compilePlugin = {
54
+ name : 'vite-plugin-svelte' ,
55
+ // make sure our resolver runs before vite internal resolver to resolve svelte field correctly
56
+ enforce : 'pre' ,
57
+ /** @type {import('./types/plugin-api.d.ts').PluginAPI } */
58
+ api : { } ,
59
+ async config ( config , configEnv ) {
60
+ // setup logger
61
+ if ( process . env . DEBUG ) {
62
+ log . setLevel ( 'debug' ) ;
63
+ } else if ( config . logLevel ) {
64
+ log . setLevel ( config . logLevel ) ;
65
+ }
66
+ // @ts -expect-error temporarily lend the options variable until fixed in configResolved
67
+ options = await preResolveOptions ( inlineOptions , config , configEnv ) ;
68
+ // extra vite config
69
+ const extraViteConfig = await buildExtraViteConfig ( options , config ) ;
70
+ log . debug ( 'additional vite config' , extraViteConfig , 'config' ) ;
71
+ return extraViteConfig ;
72
+ } ,
73
+
74
+ configEnvironment ( name , config , opts ) {
75
+ ensureConfigEnvironmentMainFields ( name , config , opts ) ;
76
+ // @ts -expect-error the function above should make `resolve.mainFields` non-nullable
77
+ config . resolve . mainFields . unshift ( 'svelte' ) ;
73
78
74
- ensureConfigEnvironmentConditions ( name , config , opts ) ;
75
- // @ts -expect-error the function above should make `resolve.conditions` non-nullable
76
- config . resolve . conditions . push ( 'svelte' ) ;
77
- } ,
79
+ ensureConfigEnvironmentConditions ( name , config , opts ) ;
80
+ // @ts -expect-error the function above should make `resolve.conditions` non-nullable
81
+ config . resolve . conditions . push ( 'svelte' ) ;
82
+ } ,
78
83
79
- async configResolved ( config ) {
80
- options = resolveOptions ( options , config , cache ) ;
81
- patchResolvedViteConfig ( config , options ) ;
82
- requestParser = buildIdParser ( options ) ;
83
- compileSvelte = createCompileSvelte ( ) ;
84
- viteConfig = config ;
85
- // TODO deep clone to avoid mutability from outside?
86
- api . options = options ;
87
- log . debug ( 'resolved options' , options , 'config' ) ;
88
- } ,
84
+ async configResolved ( config ) {
85
+ options = resolveOptions ( options , config , cache ) ;
86
+ patchResolvedViteConfig ( config , options ) ;
87
+ const filter = buildIdFilter ( options ) ;
88
+ //@ts -expect-error transform defined below but filter not in type
89
+ compilePlugin . transform . filter = filter ;
90
+ //@ts -expect-error load defined below but filter not in type
91
+ compilePlugin . load . filter = filter ;
89
92
90
- async buildStart ( ) {
91
- if ( ! options . prebundleSvelteLibraries ) return ;
92
- const isSvelteMetadataChanged = await saveSvelteMetadata ( viteConfig . cacheDir , options ) ;
93
- if ( isSvelteMetadataChanged ) {
94
- // Force Vite to optimize again. Although we mutate the config here, it works because
95
- // Vite's optimizer runs after `buildStart()`.
96
- viteConfig . optimizeDeps . force = true ;
97
- }
98
- } ,
93
+ requestParser = buildIdParser ( options ) ;
94
+ compileSvelte = createCompileSvelte ( ) ;
95
+ viteConfig = config ;
96
+ // TODO deep clone to avoid mutability from outside?
97
+ compilePlugin . api . options = options ;
98
+ log . debug ( 'resolved options' , options , 'config' ) ;
99
+ log . debug ( 'filters' , filter , 'config' ) ;
100
+ } ,
101
+
102
+ async buildStart ( ) {
103
+ if ( ! options . prebundleSvelteLibraries ) return ;
104
+ const isSvelteMetadataChanged = await saveSvelteMetadata ( viteConfig . cacheDir , options ) ;
105
+ if ( isSvelteMetadataChanged ) {
106
+ // Force Vite to optimize again. Although we mutate the config here, it works because
107
+ // Vite's optimizer runs after `buildStart()`.
108
+ viteConfig . optimizeDeps . force = true ;
109
+ }
110
+ } ,
99
111
100
- configureServer ( server ) {
101
- options . server = server ;
102
- setupWatchers ( options , cache , requestParser ) ;
103
- } ,
112
+ configureServer ( server ) {
113
+ options . server = server ;
114
+ setupWatchers ( options , cache , requestParser ) ;
115
+ } ,
104
116
105
- async load ( id , opts ) {
117
+ load : {
118
+ async handler ( id , opts ) {
106
119
const ssr = ! ! opts ?. ssr ;
107
120
const svelteRequest = requestParser ( id , ! ! ssr ) ;
108
121
if ( svelteRequest ) {
@@ -137,30 +150,23 @@ export function svelte(inlineOptions) {
137
150
}
138
151
}
139
152
}
140
- } ,
153
+ }
154
+ } ,
141
155
142
- async resolveId ( importee , importer , opts ) {
143
- const ssr = ! ! opts ?. ssr ;
144
- const svelteRequest = requestParser ( importee , ssr ) ;
145
- if ( svelteRequest ?. query . svelte ) {
146
- if (
147
- svelteRequest . query . type === 'style' &&
148
- ! svelteRequest . raw &&
149
- ! svelteRequest . query . inline
150
- ) {
151
- // return cssId with root prefix so postcss pipeline of vite finds the directory correctly
152
- // see https://github.com/sveltejs/vite-plugin-svelte/issues/14
153
- log . debug (
154
- `resolveId resolved virtual css module ${ svelteRequest . cssId } ` ,
155
- undefined ,
156
- 'resolve'
157
- ) ;
158
- return svelteRequest . cssId ;
159
- }
160
- }
161
- } ,
156
+ resolveId : {
157
+ // we don't use our generic filter here but a reduced one that only matches our virtual css
158
+ filter : { id : SVELTE_VIRTUAL_STYLE_ID_REGEX } ,
159
+ handler ( id ) {
160
+ // return cssId with root prefix so postcss pipeline of vite finds the directory correctly
161
+ // see https://github.com/sveltejs/vite-plugin-svelte/issues/14
162
+ log . debug ( `resolveId resolved virtual css module ${ id } ` , undefined , 'resolve' ) ;
163
+ // TODO: do we have to repeat the dance for constructing the virtual id here? our transform added it that way
164
+ return id ;
165
+ }
166
+ } ,
162
167
163
- async transform ( code , id , opts ) {
168
+ transform : {
169
+ async handler ( code , id , opts ) {
164
170
const ssr = ! ! opts ?. ssr ;
165
171
const svelteRequest = requestParser ( id , ssr ) ;
166
172
if ( ! svelteRequest || svelteRequest . query . type === 'style' || svelteRequest . raw ) {
@@ -194,28 +200,34 @@ export function svelte(inlineOptions) {
194
200
}
195
201
}
196
202
} ;
197
- } ,
203
+ }
204
+ } ,
198
205
199
- handleHotUpdate ( ctx ) {
200
- if ( ! options . compilerOptions . hmr || ! options . emitCss ) {
201
- return ;
202
- }
203
- const svelteRequest = requestParser ( ctx . file , false , ctx . timestamp ) ;
204
- if ( svelteRequest ) {
205
- return handleHotUpdate ( compileSvelte , ctx , svelteRequest , cache , options ) ;
206
- }
207
- } ,
208
- async buildEnd ( ) {
209
- await options . stats ?. finishAll ( ) ;
206
+ handleHotUpdate ( ctx ) {
207
+ if ( ! options . compilerOptions . hmr || ! options . emitCss ) {
208
+ return ;
210
209
}
210
+ const svelteRequest = requestParser ( ctx . file , false , ctx . timestamp ) ;
211
+ if ( svelteRequest ) {
212
+ return handleHotUpdate ( compileSvelte , ctx , svelteRequest , cache , options ) ;
213
+ }
214
+ } ,
215
+ async buildEnd ( ) {
216
+ await options . stats ?. finishAll ( ) ;
217
+ }
218
+ } ;
219
+
220
+ /** @type {import('vite').Plugin } */
221
+ const moduleCompilePlugin = {
222
+ name : 'vite-plugin-svelte-module' ,
223
+ enforce : 'post' ,
224
+ async configResolved ( ) {
225
+ //@ts -expect-error transform defined below but filter not in type
226
+ moduleCompilePlugin . transform . filter = buildModuleIdFilter ( options ) ;
227
+ moduleRequestParser = buildModuleIdParser ( options ) ;
211
228
} ,
212
- {
213
- name : 'vite-plugin-svelte-module' ,
214
- enforce : 'post' ,
215
- async configResolved ( ) {
216
- moduleRequestParser = buildModuleIdParser ( options ) ;
217
- } ,
218
- async transform ( code , id , opts ) {
229
+ transform : {
230
+ async handler ( code , id , opts ) {
219
231
const ssr = ! ! opts ?. ssr ;
220
232
const moduleRequest = moduleRequestParser ( id , ssr ) ;
221
233
if ( ! moduleRequest ) {
@@ -233,9 +245,11 @@ export function svelte(inlineOptions) {
233
245
throw toRollupError ( e , options ) ;
234
246
}
235
247
}
236
- } ,
237
- svelteInspector ( )
238
- ] ;
248
+ }
249
+ } ;
250
+
251
+ /** @type {import('vite').Plugin[] } */
252
+ const plugins = [ compilePlugin , moduleCompilePlugin , svelteInspector ( ) ] ;
239
253
return plugins ;
240
254
}
241
255
0 commit comments