Skip to content
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

feat: skip reading config file with inline option #317

Merged
merged 1 commit into from
Apr 27, 2022
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/good-plums-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': minor
---

skip reading default svelte config file with inline option `configFile: false`
2 changes: 2 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default defineConfig({
});
```

> To prevent reading the default config, use `configFile: false`.

A basic Svelte config looks like this:

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { editViteConfig } from 'testUtils';
import { editViteConfig, isBuild } from '../../testUtils';

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

it('should load custom mjs config and work', async () => {
await editViteConfig((c) =>
c.replace('svelte()', `svelte({configFile:'svelte.config.custom.cjs'})`)
);
expect(await page.textContent('h1')).toMatch('Hello world!');
expect(await page.textContent('#test-child')).toBe('test-child');
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
});
if (!isBuild) {
// editing vite config does not work in build tests, build only runs once
// TODO split into different tests
it('should load custom cjs config and work', async () => {
await editViteConfig((c) =>
c.replace(/svelte\([^)]*\)/, `svelte({configFile:'svelte.config.custom.cjs'})`)
);
expect(e2eServer.logs.server.out).toContain('custom svelte config loaded cjs');
expect(await page.textContent('h1')).toMatch('Hello world!');
expect(await page.textContent('#test-child')).toBe('test-child');
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
});

it('should not read default config when explicitly disabled', async () => {
const currentLogPos = e2eServer.logs.server.out.length;
await editViteConfig((c) => c.replace(/svelte\([^)]*\)/, `svelte({configFile: false})`));
const logsAfterChange = e2eServer.logs.server.out.slice(currentLogPos);
expect(logsAfterChange).not.toContain('default svelte config loaded');
expect(await page.textContent('h1')).toMatch('Hello world!');
expect(await page.textContent('#test-child')).toBe('test-child');
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
});
}
2 changes: 2 additions & 0 deletions packages/e2e-tests/configfile-custom/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('default svelte config loaded')
module.exports = {};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
console.log('custom svelte config loaded cjs')
module.exports = {
emitCss: false
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
console.log('custom svelte config loaded mjs')
export default {
emitCss: false
}
2 changes: 1 addition & 1 deletion packages/e2e-tests/configfile-custom/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { defineConfig } = require('vite');
module.exports = defineConfig(() => {
return {
root: './', // ensure custom root works, see https://github.com/sveltejs/vite-plugin-svelte/issues/113
plugins: [svelte({ configFile: 'svelte.config.custom.cjs' })],
plugins: [svelte()],
build: {
// make build faster by skipping transforms and minification
target: 'esnext',
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-plugin-svelte/src/utils/load-svelte-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export async function loadSvelteConfig(
viteConfig: UserConfig,
inlineOptions: Partial<Options>
): Promise<Partial<Options> | undefined> {
if (inlineOptions.configFile === false) {
return;
}
const configFile = findConfigToLoad(viteConfig, inlineOptions);
if (configFile) {
let err;
Expand Down
4 changes: 3 additions & 1 deletion packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,11 @@ export interface Options {
/**
* Path to a svelte config file, either absolute or relative to Vite root
*
* set to `false` to skip reading config from a file
*
* @see https://vitejs.dev/config/#root
*/
configFile?: string;
configFile?: string | false;

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