Skip to content

Commit 5efdad1

Browse files
authored
feat: clean up options interface (fixes #12) (#99)
1 parent 4683ad6 commit 5efdad1

File tree

6 files changed

+50
-16
lines changed

6 files changed

+50
-16
lines changed

.changeset/angry-penguins-jog.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Add `experimental` section to options and move `useVitePreprocess` there
6+
7+
Experimental options are not ready for production use and breaking changes to them can occur in any release
8+
9+
If you already had `useVitePreprocess` enabled, update you config:
10+
11+
```diff
12+
- svelte({useVitePreprocess: true})
13+
+ svelte({experimental: {useVitePreprocess: true}})
14+
```

.changeset/dirty-boxes-sip.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
removed redundant `disableCssHmr` option
6+
7+
You can use `emitCss: false` or `emitCss: !!isProduction` instead

packages/playground/preprocess-with-vite/vite.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ module.exports = defineConfig(({ command, mode }) => {
66
return {
77
plugins: [
88
svelte({
9-
useVitePreprocess: true
9+
experimental: {
10+
useVitePreprocess: true
11+
}
1012
})
1113
],
1214
build: {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
171171
},
172172

173173
handleHotUpdate(ctx: HmrContext): void | Promise<Array<ModuleNode> | void> {
174-
if (!options.emitCss || options.disableCssHmr) {
174+
if (!options.hot || !options.emitCss) {
175175
return;
176176
}
177177
const svelteRequest = requestParser(ctx.file, false, ctx.timestamp);

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ const knownOptions = new Set([
1515
'preprocess',
1616
'hot',
1717
'disableCssHmr',
18-
'useVitePreprocess',
19-
'ignorePluginPreprocessors'
18+
'ignorePluginPreprocessors',
19+
'experimental'
2020
]);
2121

2222
function buildDefaultOptions(isProduction: boolean, options: Partial<Options>): Partial<Options> {
23-
const disableCssHmr = !!options?.disableCssHmr;
2423
// emit for prod, emit in dev unless css hmr is disabled
25-
const emitCss = options?.emitCss != null ? options.emitCss : isProduction || !disableCssHmr;
24+
const emitCss = options?.emitCss != null ? options.emitCss : true;
2625
// no hmr in prod, only inject css in dev if emitCss is false
2726
const hot = isProduction
2827
? false
@@ -40,12 +39,7 @@ function buildDefaultOptions(isProduction: boolean, options: Partial<Options>):
4039
hydratable: true
4140
}
4241
};
43-
log.debug(
44-
`default options for ${isProduction ? 'production' : 'development'} ${
45-
!isProduction && disableCssHmr ? ' with css hmr disabled' : ''
46-
}`,
47-
defaultOptions
48-
);
42+
log.debug(`default options for ${isProduction ? 'production' : 'development'}`, defaultOptions);
4943
return defaultOptions;
5044
}
5145

@@ -125,6 +119,10 @@ function mergeOptions(
125119
...(svelteConfig?.compilerOptions || {}),
126120
...(inlineOptions?.compilerOptions || {})
127121
},
122+
experimental: {
123+
...(svelteConfig?.experimental || {}),
124+
...(inlineOptions?.experimental || {})
125+
},
128126
root: viteConfig.root || process.cwd(),
129127
isProduction: viteEnv.mode === 'production',
130128
isBuild: viteEnv.command === 'build',
@@ -195,7 +193,7 @@ export function buildExtraViteConfig(
195193
}
196194
}
197195

198-
if (options.useVitePreprocess) {
196+
if (options.experimental?.useVitePreprocess) {
199197
// needed to transform svelte files with component imports
200198
// can cause issues with other typescript files, see https://github.com/sveltejs/vite-plugin-svelte/pull/20
201199
extraViteConfig.esbuild = {
@@ -390,7 +388,20 @@ export interface Options {
390388
ignorePluginPreprocessors?: boolean | string[];
391389

392390
/**
393-
* use vite as extra css preprocessor EXPERIMENTAL!
391+
* These options are considered experimental and breaking changes to them can occur in any release
392+
*/
393+
experimental?: ExperimentalOptions;
394+
}
395+
396+
/**
397+
* These options are considered experimental and breaking changes to them can occur in any release
398+
*/
399+
export interface ExperimentalOptions {
400+
/**
401+
* use extra preprocessors that delegate style and typescript preproessing to native vite plugins
402+
*
403+
* do not use together with svelte-preprocess!
404+
*
394405
* @default false
395406
*/
396407
useVitePreprocess?: boolean;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function createInjectScopeEverythingRulePreprocessorGroup(): PreprocessorGroup {
8181

8282
function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfig) {
8383
const extraPreprocessors = [];
84-
if (options.useVitePreprocess) {
84+
if (options.experimental?.useVitePreprocess) {
8585
log.debug('adding vite preprocessor');
8686
extraPreprocessors.push(createVitePreprocessorGroup(config, options));
8787
}
@@ -140,7 +140,7 @@ function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfi
140140
extraPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
141141
}
142142

143-
if (options.hot && !options.disableCssHmr) {
143+
if (options.hot && options.emitCss) {
144144
extraPreprocessors.push(createInjectScopeEverythingRulePreprocessorGroup());
145145
}
146146

0 commit comments

Comments
 (0)