Skip to content

Commit 405298a

Browse files
authored
feat: run vite preprocessors first (#189)
* feat: run vite preprocessors first * chore: add changeset * chore: update ssr comments
1 parent 30f01f6 commit 405298a

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

.changeset/nasty-seahorses-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Run Vite preprocessors first in markup phase

packages/vite-plugin-svelte/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
9595
},
9696

9797
async resolveId(importee, importer, opts, _ssr) {
98-
// @ts-expect-error anticipate vite changing second parameter as options object
98+
// @ts-expect-error anticipate vite deprecating forth parameter and rely on `opts.ssr` instead`
9999
// see https://github.com/vitejs/vite/discussions/5109
100100
const ssr: boolean = _ssr === true || opts.ssr;
101101
const svelteRequest = requestParser(importee, !!ssr);
@@ -149,7 +149,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
149149
},
150150

151151
async transform(code, id, opts) {
152-
// @ts-expect-error anticipate vite changing second parameter as options object
152+
// @ts-expect-error anticipate vite changing third parameter as options object
153153
// see https://github.com/vitejs/vite/discussions/5109
154154
const ssr: boolean = opts === true || opts?.ssr;
155155
const svelteRequest = requestParser(id, !!ssr);

packages/vite-plugin-svelte/src/utils/preprocess.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Plugin
77
} from 'vite';
88
import MagicString from 'magic-string';
9+
import { preprocess } from 'svelte/compiler';
910
import { Preprocessor, PreprocessorGroup, Processed, ResolvedOptions } from './options';
1011
import { TransformPluginContext } from 'rollup';
1112
import { log } from './log';
@@ -70,8 +71,16 @@ function createViteStylePreprocessor(config: ResolvedConfig): Preprocessor {
7071

7172
export 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

97106
function 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

165176
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) {
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

Comments
 (0)