Skip to content

Commit 10d02a8

Browse files
authored
feat: skip reading config file with inline option (#317)
1 parent 9867103 commit 10d02a8

File tree

9 files changed

+43
-11
lines changed

9 files changed

+43
-11
lines changed

.changeset/good-plums-rest.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+
skip reading default svelte config file with inline option `configFile: false`

docs/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export default defineConfig({
3636
});
3737
```
3838

39+
> To prevent reading the default config, use `configFile: false`.
40+
3941
A basic Svelte config looks like this:
4042

4143
```js
Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
import { editViteConfig } from 'testUtils';
1+
import { editViteConfig, isBuild } from '../../testUtils';
22

33
it('should load default config and work', async () => {
4+
expect(e2eServer.logs.server.out).toContain('default svelte config loaded');
45
expect(await page.textContent('h1')).toMatch('Hello world!');
56
expect(await page.textContent('#test-child')).toBe('test-child');
67
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
78
});
89

9-
it('should load custom mjs config and work', async () => {
10-
await editViteConfig((c) =>
11-
c.replace('svelte()', `svelte({configFile:'svelte.config.custom.cjs'})`)
12-
);
13-
expect(await page.textContent('h1')).toMatch('Hello world!');
14-
expect(await page.textContent('#test-child')).toBe('test-child');
15-
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
16-
});
10+
if (!isBuild) {
11+
// editing vite config does not work in build tests, build only runs once
12+
// TODO split into different tests
13+
it('should load custom cjs config and work', async () => {
14+
await editViteConfig((c) =>
15+
c.replace(/svelte\([^)]*\)/, `svelte({configFile:'svelte.config.custom.cjs'})`)
16+
);
17+
expect(e2eServer.logs.server.out).toContain('custom svelte config loaded cjs');
18+
expect(await page.textContent('h1')).toMatch('Hello world!');
19+
expect(await page.textContent('#test-child')).toBe('test-child');
20+
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
21+
});
22+
23+
it('should not read default config when explicitly disabled', async () => {
24+
const currentLogPos = e2eServer.logs.server.out.length;
25+
await editViteConfig((c) => c.replace(/svelte\([^)]*\)/, `svelte({configFile: false})`));
26+
const logsAfterChange = e2eServer.logs.server.out.slice(currentLogPos);
27+
expect(logsAfterChange).not.toContain('default svelte config loaded');
28+
expect(await page.textContent('h1')).toMatch('Hello world!');
29+
expect(await page.textContent('#test-child')).toBe('test-child');
30+
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
31+
});
32+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('default svelte config loaded')
2+
module.exports = {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
console.log('custom svelte config loaded cjs')
12
module.exports = {
23
emitCss: false
34
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
console.log('custom svelte config loaded mjs')
12
export default {
23
emitCss: false
34
}

packages/e2e-tests/configfile-custom/vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { defineConfig } = require('vite');
44
module.exports = defineConfig(() => {
55
return {
66
root: './', // ensure custom root works, see https://github.com/sveltejs/vite-plugin-svelte/issues/113
7-
plugins: [svelte({ configFile: 'svelte.config.custom.cjs' })],
7+
plugins: [svelte()],
88
build: {
99
// make build faster by skipping transforms and minification
1010
target: 'esnext',

packages/vite-plugin-svelte/src/utils/load-svelte-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export async function loadSvelteConfig(
2929
viteConfig: UserConfig,
3030
inlineOptions: Partial<Options>
3131
): Promise<Partial<Options> | undefined> {
32+
if (inlineOptions.configFile === false) {
33+
return;
34+
}
3235
const configFile = findConfigToLoad(viteConfig, inlineOptions);
3336
if (configFile) {
3437
let err;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,11 @@ export interface Options {
355355
/**
356356
* Path to a svelte config file, either absolute or relative to Vite root
357357
*
358+
* set to `false` to skip reading config from a file
359+
*
358360
* @see https://vitejs.dev/config/#root
359361
*/
360-
configFile?: string;
362+
configFile?: string | false;
361363

362364
/**
363365
* A `picomatch` pattern, or array of patterns, which specifies the files the plugin should

0 commit comments

Comments
 (0)