66 Plugin
77} from 'vite' ;
88import MagicString from 'magic-string' ;
9+ import { preprocess } from 'svelte/compiler' ;
910import { Preprocessor , PreprocessorGroup , Processed , ResolvedOptions } from './options' ;
1011import { TransformPluginContext } from 'rollup' ;
1112import { log } from './log' ;
@@ -70,8 +71,16 @@ function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor {
7071
7172export function createVitePreprocessorGroup ( config : ResolvedConfig ) : PreprocessorGroup {
7273 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+ }
7584 } as PreprocessorGroup ;
7685}
7786
@@ -95,10 +104,12 @@ function createInjectScopeEverythingRulePreprocessorGroup(): PreprocessorGroup {
95104}
96105
97106function buildExtraPreprocessors ( options : ResolvedOptions , config : ResolvedConfig ) {
98- const extraPreprocessors = [ ] ;
107+ const prependPreprocessors : PreprocessorGroup [ ] = [ ] ;
108+ const appendPreprocessors : PreprocessorGroup [ ] = [ ] ;
109+
99110 if ( options . experimental ?. useVitePreprocess ) {
100111 log . debug ( 'adding vite preprocessor' ) ;
101- extraPreprocessors . push ( createVitePreprocessorGroup ( config ) ) ;
112+ prependPreprocessors . push ( createVitePreprocessorGroup ( config ) ) ;
102113 }
103114
104115 // @ts -ignore
@@ -152,25 +163,26 @@ function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfi
152163 . map ( ( p ) => p . name )
153164 . join ( ', ' ) } `
154165 ) ;
155- extraPreprocessors . push ( ...pluginsWithPreprocessors . map ( ( p ) => p . api . sveltePreprocess ) ) ;
166+ appendPreprocessors . push ( ...pluginsWithPreprocessors . map ( ( p ) => p . api . sveltePreprocess ) ) ;
156167 }
157168
158169 if ( options . hot && options . emitCss ) {
159- extraPreprocessors . push ( createInjectScopeEverythingRulePreprocessorGroup ( ) ) ;
170+ appendPreprocessors . push ( createInjectScopeEverythingRulePreprocessorGroup ( ) ) ;
160171 }
161172
162- return extraPreprocessors ;
173+ return { prependPreprocessors , appendPreprocessors } ;
163174}
164175
165176export 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 ) {
168179 if ( ! options . preprocess ) {
169- options . preprocess = extra ;
180+ options . preprocess = [ ... prependPreprocessors , ... appendPreprocessors ] ;
170181 } else if ( Array . isArray ( options . preprocess ) ) {
171- options . preprocess . push ( ...extra ) ;
182+ options . preprocess . unshift ( ...prependPreprocessors ) ;
183+ options . preprocess . push ( ...appendPreprocessors ) ;
172184 } else {
173- options . preprocess = [ options . preprocess , ...extra ] ;
185+ options . preprocess = [ ... prependPreprocessors , options . preprocess , ...appendPreprocessors ] ;
174186 }
175187 }
176188 const generateMissingSourceMaps = ! ! options . experimental ?. generateMissingPreprocessorSourcemaps ;
0 commit comments