@@ -4,7 +4,7 @@ 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 { buildIdFilter , buildIdParser , buildModuleIdParser } from './utils/id.js' ;
8
8
import {
9
9
buildExtraViteConfig ,
10
10
validateInlineOptions ,
@@ -20,6 +20,7 @@ import { saveSvelteMetadata } from './utils/optimizer.js';
20
20
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache.js' ;
21
21
import { loadRaw } from './utils/load-raw.js' ;
22
22
import * as svelteCompiler from 'svelte/compiler' ;
23
+ import { SVELTE_VIRTUAL_STYLE_ID_REGEX } from './utils/constants.js' ;
23
24
24
25
/**
25
26
* @param {Partial<import('./public.d.ts').Options> } [inlineOptions]
@@ -42,8 +43,13 @@ export function svelte(inlineOptions) {
42
43
let viteConfig ;
43
44
/** @type {import('./types/compile.d.ts').CompileSvelte } */
44
45
let compileSvelte ;
46
+
47
+ /** @type {import('./types/id.d.ts').IdFilter } */
48
+ let filter = { id : { include : [ ] , exclude : [ ] } } ; // set with correct values in configResolved
49
+
45
50
/** @type {import('./types/plugin-api.d.ts').PluginAPI } */
46
51
const api = { } ;
52
+
47
53
/** @type {import('vite').Plugin[] } */
48
54
const plugins = [
49
55
{
@@ -79,6 +85,7 @@ export function svelte(inlineOptions) {
79
85
async configResolved ( config ) {
80
86
options = resolveOptions ( options , config , cache ) ;
81
87
patchResolvedViteConfig ( config , options ) ;
88
+ filter = buildIdFilter ( options ) ;
82
89
requestParser = buildIdParser ( options ) ;
83
90
compileSvelte = createCompileSvelte ( ) ;
84
91
viteConfig = config ;
@@ -102,98 +109,95 @@ export function svelte(inlineOptions) {
102
109
setupWatchers ( options , cache , requestParser ) ;
103
110
} ,
104
111
105
- async load ( id , opts ) {
106
- const ssr = ! ! opts ?. ssr ;
107
- const svelteRequest = requestParser ( id , ! ! ssr ) ;
108
- if ( svelteRequest ) {
109
- const { filename, query, raw } = svelteRequest ;
110
- if ( raw ) {
111
- const code = await loadRaw ( svelteRequest , compileSvelte , options ) ;
112
- // prevent vite from injecting sourcemaps in the results.
113
- return {
114
- code,
115
- map : {
116
- mappings : ''
117
- }
118
- } ;
119
- } else {
120
- if ( query . svelte && query . type === 'style' ) {
121
- const cachedCss = cache . getCSS ( svelteRequest ) ;
122
- if ( cachedCss ) {
123
- const { hasGlobal, ...css } = cachedCss ;
124
- if ( hasGlobal === false ) {
125
- // hasGlobal was added in svelte 5.26.0, so make sure it is boolean false
126
- css . meta ??= { } ;
127
- css . meta . vite ??= { } ;
128
- css . meta . vite . cssScopeTo = [ svelteRequest . filename , 'default' ] ;
112
+ load : {
113
+ filter,
114
+ async handler ( id , opts ) {
115
+ const ssr = ! ! opts ?. ssr ;
116
+ const svelteRequest = requestParser ( id , ! ! ssr ) ;
117
+ if ( svelteRequest ) {
118
+ const { filename, query, raw } = svelteRequest ;
119
+ if ( raw ) {
120
+ const code = await loadRaw ( svelteRequest , compileSvelte , options ) ;
121
+ // prevent vite from injecting sourcemaps in the results.
122
+ return {
123
+ code,
124
+ map : {
125
+ mappings : ''
126
+ }
127
+ } ;
128
+ } else {
129
+ if ( query . svelte && query . type === 'style' ) {
130
+ const cachedCss = cache . getCSS ( svelteRequest ) ;
131
+ if ( cachedCss ) {
132
+ const { hasGlobal, ...css } = cachedCss ;
133
+ if ( hasGlobal === false ) {
134
+ // hasGlobal was added in svelte 5.26.0, so make sure it is boolean false
135
+ css . meta ??= { } ;
136
+ css . meta . vite ??= { } ;
137
+ css . meta . vite . cssScopeTo = [ svelteRequest . filename , 'default' ] ;
138
+ }
139
+ return css ;
129
140
}
130
- return css ;
131
141
}
132
- }
133
- // prevent vite asset plugin from loading files as url that should be compiled in transform
134
- if ( viteConfig . assetsInclude ( filename ) ) {
135
- log . debug ( `load returns raw content for ${ filename } ` , undefined , 'load ') ;
136
- return fs . readFileSync ( filename , 'utf-8' ) ;
142
+ // prevent vite asset plugin from loading files as url that should be compiled in transform
143
+ if ( viteConfig . assetsInclude ( filename ) ) {
144
+ log . debug ( `load returns raw content for ${ filename } ` , undefined , 'load' ) ;
145
+ return fs . readFileSync ( filename , 'utf-8 ') ;
146
+ }
137
147
}
138
148
}
139
149
}
140
150
} ,
141
151
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
- }
152
+ resolveId : {
153
+ // we don't use our generic filter here but a reduced one that only matches our virtual css
154
+ filter : { id : SVELTE_VIRTUAL_STYLE_ID_REGEX } ,
155
+ handler ( id ) {
156
+ // return cssId with root prefix so postcss pipeline of vite finds the directory correctly
157
+ // see https://github.com/sveltejs/vite-plugin-svelte/issues/14
158
+ log . debug ( `resolveId resolved virtual css module ${ id } ` , undefined , 'resolve' ) ;
159
+ // TODO: do we have to repeat the dance for constructing the virtual id here? our transform added it that way
160
+ return id ;
160
161
}
161
162
} ,
162
163
163
- async transform ( code , id , opts ) {
164
- const ssr = ! ! opts ?. ssr ;
165
- const svelteRequest = requestParser ( id , ssr ) ;
166
- if ( ! svelteRequest || svelteRequest . query . type === 'style' || svelteRequest . raw ) {
167
- return ;
168
- }
169
- let compileData ;
170
- try {
171
- compileData = await compileSvelte ( svelteRequest , code , options ) ;
172
- } catch ( e ) {
173
- cache . setError ( svelteRequest , e ) ;
174
- throw toRollupError ( e , options ) ;
175
- }
176
- logCompilerWarnings ( svelteRequest , compileData . compiled . warnings , options ) ;
177
- cache . update ( compileData ) ;
178
- if ( compileData . dependencies ?. length ) {
179
- if ( options . server ) {
180
- for ( const dep of compileData . dependencies ) {
181
- ensureWatchedFile ( options . server . watcher , dep , options . root ) ;
182
- }
183
- } else if ( options . isBuild && viteConfig . build . watch ) {
184
- for ( const dep of compileData . dependencies ) {
185
- this . addWatchFile ( dep ) ;
186
- }
164
+ transform : {
165
+ filter,
166
+ async handler ( code , id , opts ) {
167
+ const ssr = ! ! opts ?. ssr ;
168
+ const svelteRequest = requestParser ( id , ssr ) ;
169
+ if ( ! svelteRequest || svelteRequest . query . type === 'style' || svelteRequest . raw ) {
170
+ return ;
187
171
}
188
- }
189
- return {
190
- ...compileData . compiled . js ,
191
- meta : {
192
- vite : {
193
- lang : compileData . lang
172
+ let compileData ;
173
+ try {
174
+ compileData = await compileSvelte ( svelteRequest , code , options ) ;
175
+ } catch ( e ) {
176
+ cache . setError ( svelteRequest , e ) ;
177
+ throw toRollupError ( e , options ) ;
178
+ }
179
+ logCompilerWarnings ( svelteRequest , compileData . compiled . warnings , options ) ;
180
+ cache . update ( compileData ) ;
181
+ if ( compileData . dependencies ?. length ) {
182
+ if ( options . server ) {
183
+ for ( const dep of compileData . dependencies ) {
184
+ ensureWatchedFile ( options . server . watcher , dep , options . root ) ;
185
+ }
186
+ } else if ( options . isBuild && viteConfig . build . watch ) {
187
+ for ( const dep of compileData . dependencies ) {
188
+ this . addWatchFile ( dep ) ;
189
+ }
194
190
}
195
191
}
196
- } ;
192
+ return {
193
+ ...compileData . compiled . js ,
194
+ meta : {
195
+ vite : {
196
+ lang : compileData . lang
197
+ }
198
+ }
199
+ } ;
200
+ }
197
201
} ,
198
202
199
203
handleHotUpdate ( ctx ) {
0 commit comments