6
6
Plugin
7
7
} from 'vite' ;
8
8
import MagicString from 'magic-string' ;
9
+ import { preprocess } from 'svelte/compiler' ;
9
10
import { Preprocessor , PreprocessorGroup , Processed , ResolvedOptions } from './options' ;
10
11
import { TransformPluginContext } from 'rollup' ;
11
12
import { log } from './log' ;
@@ -70,8 +71,16 @@ function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor {
70
71
71
72
export function createVitePreprocessorGroup ( config : ResolvedConfig ) : PreprocessorGroup {
72
73
return {
73
- script : createViteScriptPreprocessor ( ) ,
74
- style : createViteStylePreprocessor ( config )
74
+ markup ( { content, filename } ) {
75
+ return preprocess (
76
+ content ,
77
+ {
78
+ script : createViteScriptPreprocessor ( ) ,
79
+ style : createViteStylePreprocessor ( config )
80
+ } ,
81
+ { filename }
82
+ ) ;
83
+ }
75
84
} as PreprocessorGroup ;
76
85
}
77
86
@@ -95,10 +104,12 @@ function createInjectScopeEverythingRulePreprocessorGroup(): PreprocessorGroup {
95
104
}
96
105
97
106
function buildExtraPreprocessors ( options : ResolvedOptions , config : ResolvedConfig ) {
98
- const extraPreprocessors = [ ] ;
107
+ const prependPreprocessors : PreprocessorGroup [ ] = [ ] ;
108
+ const appendPreprocessors : PreprocessorGroup [ ] = [ ] ;
109
+
99
110
if ( options . experimental ?. useVitePreprocess ) {
100
111
log . debug ( 'adding vite preprocessor' ) ;
101
- extraPreprocessors . push ( createVitePreprocessorGroup ( config ) ) ;
112
+ prependPreprocessors . push ( createVitePreprocessorGroup ( config ) ) ;
102
113
}
103
114
104
115
// @ts -ignore
@@ -152,25 +163,26 @@ function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfi
152
163
. map ( ( p ) => p . name )
153
164
. join ( ', ' ) } `
154
165
) ;
155
- extraPreprocessors . push ( ...pluginsWithPreprocessors . map ( ( p ) => p . api . sveltePreprocess ) ) ;
166
+ appendPreprocessors . push ( ...pluginsWithPreprocessors . map ( ( p ) => p . api . sveltePreprocess ) ) ;
156
167
}
157
168
158
169
if ( options . hot && options . emitCss ) {
159
- extraPreprocessors . push ( createInjectScopeEverythingRulePreprocessorGroup ( ) ) ;
170
+ appendPreprocessors . push ( createInjectScopeEverythingRulePreprocessorGroup ( ) ) ;
160
171
}
161
172
162
- return extraPreprocessors ;
173
+ return { prependPreprocessors , appendPreprocessors } ;
163
174
}
164
175
165
176
export function addExtraPreprocessors ( options : ResolvedOptions , config : ResolvedConfig ) {
166
- const extra = buildExtraPreprocessors ( options , config ) ;
167
- if ( extra ? .length > 0 ) {
177
+ const { prependPreprocessors , appendPreprocessors } = buildExtraPreprocessors ( options , config ) ;
178
+ if ( prependPreprocessors . length > 0 || appendPreprocessors . length > 0 ) {
168
179
if ( ! options . preprocess ) {
169
- options . preprocess = extra ;
180
+ options . preprocess = [ ... prependPreprocessors , ... appendPreprocessors ] ;
170
181
} else if ( Array . isArray ( options . preprocess ) ) {
171
- options . preprocess . push ( ...extra ) ;
182
+ options . preprocess . unshift ( ...prependPreprocessors ) ;
183
+ options . preprocess . push ( ...appendPreprocessors ) ;
172
184
} else {
173
- options . preprocess = [ options . preprocess , ...extra ] ;
185
+ options . preprocess = [ ... prependPreprocessors , options . preprocess , ...appendPreprocessors ] ;
174
186
}
175
187
}
176
188
const generateMissingSourceMaps = ! ! options . experimental ?. generateMissingPreprocessorSourcemaps ;
0 commit comments