Skip to content

fix: handle emitCss in svelte config #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-experts-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

Fix emitCss behaviour in a svelte config
12 changes: 11 additions & 1 deletion packages/e2e-tests/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test('should render dynamic import', async () => {
await dynamicImportButton.click();
await untilUpdated(() => getText('#dynamic-import .label'), 'dynamic-import');
});

test('should not have failed requests', async () => {
browserLogs.forEach((msg) => {
expect(msg).not.toMatch('404');
Expand Down Expand Up @@ -123,7 +124,7 @@ if (!isBuild) {
expect(await getText(`#hmr-test-3 .counter`)).toBe('0');
});

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

test('should work with emitCss: false in svelte config', async () => {
addFile('svelte.config.cjs', `module.exports={emitCss:false}`);
await sleep(isWin ? 1000 : 500); // adding config restarts server, give it some time
await page.goto(viteTestUrl, { waitUntil: 'networkidle' });
await sleep(50);
expect(await getColor(`#hmr-test-1 .label`)).toBe('red');
removeFile('svelte.config.cjs');
});

test('should detect changes in svelte config and restart', async () => {
const injectPreprocessor = ({ content, filename }) => {
if (filename && filename.includes('App.svelte')) {
Expand Down
10 changes: 6 additions & 4 deletions packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ const knownOptions = new Set([
'experimental'
]);

function buildDefaultOptions(isProduction: boolean, options: Partial<Options>): Partial<Options> {
// emit for prod, emit in dev unless css hmr is disabled
const emitCss = options?.emitCss != null ? options.emitCss : true;
function buildDefaultOptions(isProduction: boolean, emitCss = true): Partial<Options> {
// no hmr in prod, only inject css in dev if emitCss is false
const hot = isProduction
? false
: {
// emit for prod, emit in dev unless css hmr is disabled
injectCss: !emitCss
};
const defaultOptions: Partial<Options> = {
Expand Down Expand Up @@ -156,8 +155,11 @@ export async function resolveOptions(
...viteConfig,
root: resolveViteRoot(viteConfig)
};
const defaultOptions = buildDefaultOptions(viteEnv.mode === 'production', inlineOptions);
const svelteConfig = (await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions)) || {};
const defaultOptions = buildDefaultOptions(
viteEnv.mode === 'production',
inlineOptions.emitCss ?? svelteConfig.emitCss
);
const resolvedOptions = mergeOptions(
defaultOptions,
svelteConfig,
Expand Down