Skip to content

Commit 44eef13

Browse files
authored
fix: handle emitCss in svelte config (#194)
1 parent 950ae49 commit 44eef13

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

.changeset/red-experts-work.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+
Fix emitCss behaviour in a svelte config

packages/e2e-tests/hmr/__tests__/hmr.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ test('should render dynamic import', async () => {
3232
await dynamicImportButton.click();
3333
await untilUpdated(() => getText('#dynamic-import .label'), 'dynamic-import');
3434
});
35+
3536
test('should not have failed requests', async () => {
3637
browserLogs.forEach((msg) => {
3738
expect(msg).not.toMatch('404');
@@ -123,7 +124,7 @@ if (!isBuild) {
123124
expect(await getText(`#hmr-test-3 .counter`)).toBe('0');
124125
});
125126

126-
test('should work with emitCss: false', async () => {
127+
test('should work with emitCss: false in vite config', async () => {
127128
await editViteConfig((c) => c.replace('svelte()', 'svelte({emitCss:false})'));
128129
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');
129130
expect(await getColor(`#hmr-test-1 .label`)).toBe('green');
@@ -135,6 +136,15 @@ if (!isBuild) {
135136
expect(await getText(`#hmr-test-1 .counter`)).toBe('1');
136137
});
137138

139+
test('should work with emitCss: false in svelte config', async () => {
140+
addFile('svelte.config.cjs', `module.exports={emitCss:false}`);
141+
await sleep(isWin ? 1000 : 500); // adding config restarts server, give it some time
142+
await page.goto(viteTestUrl, { waitUntil: 'networkidle' });
143+
await sleep(50);
144+
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
145+
removeFile('svelte.config.cjs');
146+
});
147+
138148
test('should detect changes in svelte config and restart', async () => {
139149
const injectPreprocessor = ({ content, filename }) => {
140150
if (filename && filename.includes('App.svelte')) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ const knownOptions = new Set([
3131
'experimental'
3232
]);
3333

34-
function buildDefaultOptions(isProduction: boolean, options: Partial<Options>): Partial<Options> {
35-
// emit for prod, emit in dev unless css hmr is disabled
36-
const emitCss = options?.emitCss != null ? options.emitCss : true;
34+
function buildDefaultOptions(isProduction: boolean, emitCss = true): Partial<Options> {
3735
// no hmr in prod, only inject css in dev if emitCss is false
3836
const hot = isProduction
3937
? false
4038
: {
39+
// emit for prod, emit in dev unless css hmr is disabled
4140
injectCss: !emitCss
4241
};
4342
const defaultOptions: Partial<Options> = {
@@ -156,8 +155,11 @@ export async function resolveOptions(
156155
...viteConfig,
157156
root: resolveViteRoot(viteConfig)
158157
};
159-
const defaultOptions = buildDefaultOptions(viteEnv.mode === 'production', inlineOptions);
160158
const svelteConfig = (await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions)) || {};
159+
const defaultOptions = buildDefaultOptions(
160+
viteEnv.mode === 'production',
161+
inlineOptions.emitCss ?? svelteConfig.emitCss
162+
);
161163
const resolvedOptions = mergeOptions(
162164
defaultOptions,
163165
svelteConfig,

0 commit comments

Comments
 (0)