Skip to content

Commit 052ad63

Browse files
dominikgConduitrybluwy
authored
fix: Remove user-specified values for essential compilerOptions and log a warning (#346)
* fix: Remove user-set values for essential compilerOptions and log a warning * chore: improve language Co-authored-by: Conduitry <git@chor.date> * chore: more language updates Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> * fix: ignore cssHash in dev only * docs: update config Co-authored-by: Conduitry <git@chor.date> Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
1 parent 61b5988 commit 052ad63

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

.changeset/three-icons-behave.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': patch
3+
---
4+
5+
Remove user-specified values for essential compilerOptions generate, format, cssHash and filename and log a warning

docs/config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ A basic Svelte config looks like this:
4242
// svelte.config.js
4343
export default {
4444
// config options
45+
compilerOptions: {},
46+
preprocess: []
4547
};
4648
```
4749

@@ -77,13 +79,11 @@ export default defineConfig({
7779

7880
These options are specific to the Svelte compiler and are generally shared across many bundler integrations.
7981

80-
<!-- TODO: Also note where these options can be placed in svelte.config.js -->
81-
8282
### compilerOptions
8383

8484
- **Type:** `CompileOptions` - See [svelte.compile](https://svelte.dev/docs#svelte_compile)
8585

86-
The options to be passed to the Svelte compiler. A few options are set by default, including `dev`, `format` and `css`. However, some options are non-configurable, like `filename`, `generate`, and `cssHash`.
86+
The options to be passed to the Svelte compiler. A few options are set by default, including `dev` and `css`. However, some options are non-configurable, like `filename`, `format`, `generate`, and `cssHash` (in dev).
8787

8888
### preprocess
8989

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const _createCompileSvelte = (makeHot: Function) =>
2121
const compileOptions: CompileOptions = {
2222
...options.compilerOptions,
2323
filename,
24-
generate: ssr ? 'ssr' : 'dom'
24+
generate: ssr ? 'ssr' : 'dom',
25+
format: 'esm'
2526
};
2627
if (options.hot && options.emitCss) {
2728
const hash = `s-${safeBase64Hash(normalizedFilename)}`;

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ export async function preResolveOptions(
6161
};
6262
const defaultOptions: Partial<Options> = {
6363
extensions: ['.svelte'],
64-
emitCss: true,
65-
compilerOptions: {
66-
format: 'esm'
67-
}
64+
emitCss: true
6865
};
6966
const svelteConfig = await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions);
7067
const extraOptions: Partial<PreResolvedOptions> = {
@@ -118,6 +115,7 @@ export function resolveOptions(
118115
};
119116
const merged: ResolvedOptions = mergeConfigs(defaultOptions, preResolveOptions, extraOptions);
120117

118+
removeIgnoredOptions(merged);
121119
addExtraPreprocessors(merged, viteConfig);
122120
enforceOptionsForHmr(merged);
123121
enforceOptionsForProduction(merged);
@@ -177,6 +175,26 @@ function enforceOptionsForProduction(options: ResolvedOptions) {
177175
}
178176
}
179177

178+
function removeIgnoredOptions(options: ResolvedOptions) {
179+
const ignoredCompilerOptions = ['generate', 'format', 'filename'];
180+
if (options.hot && options.emitCss) {
181+
ignoredCompilerOptions.push('cssHash');
182+
}
183+
const passedCompilerOptions = Object.keys(options.compilerOptions || {});
184+
const passedIgnored = passedCompilerOptions.filter((o) => ignoredCompilerOptions.includes(o));
185+
if (passedIgnored.length) {
186+
log.warn(
187+
`The following Svelte compilerOptions are controlled by vite-plugin-svelte and essential to its functionality. User-specified values are ignored. Please remove them from your configuration: ${passedIgnored.join(
188+
', '
189+
)}`
190+
);
191+
passedIgnored.forEach((ignored) => {
192+
// @ts-expect-error string access
193+
delete options.compilerOptions[ignored];
194+
});
195+
}
196+
}
197+
180198
// vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
181199
// https://github.com/sveltejs/vite-plugin-svelte/issues/113
182200
// https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
@@ -401,11 +419,13 @@ export interface Options {
401419
emitCss?: boolean;
402420

403421
/**
404-
* The options to be passed to the Svelte compiler
422+
* The options to be passed to the Svelte compiler. A few options are set by default,
423+
* including `dev` and `css`. However, some options are non-configurable, like
424+
* `filename`, `format`, `generate`, and `cssHash` (in dev).
405425
*
406426
* @see https://svelte.dev/docs#svelte_compile
407427
*/
408-
compilerOptions?: CompileOptions;
428+
compilerOptions?: Omit<CompileOptions, 'filename' | 'format' | 'generate'>;
409429

410430
/**
411431
* Handles warning emitted from the Svelte compiler

0 commit comments

Comments
 (0)