Skip to content

Commit 4540d96

Browse files
bluwydominikg
andauthored
fix: use createRequire to require svelte.config.cjs in esm project (#142)
* feat: add config tests * feat: try import cjs config * add changeset * fix: change cjs loading strategy * fix: really load cjs config * refactor: load cjs config * docs: improve changeset description Co-authored-by: dominikg <dominik.goepel@gmx.de>
1 parent b195fc3 commit 4540d96

File tree

7 files changed

+57
-9
lines changed

7 files changed

+57
-9
lines changed

.changeset/fuzzy-mails-brush.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+
use createRequire to load svelte.config.cjs in esm projects (fixes #141)

packages/e2e-tests/configfile-custom/__tests__/configfile-custom.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
it('should load config and work', async () => {
1+
import { editViteConfig } from 'testUtils';
2+
3+
it('should load default config and work', async () => {
4+
expect(await page.textContent('h1')).toMatch('Hello world!');
5+
expect(await page.textContent('#test-child')).toBe('test-child');
6+
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
7+
});
8+
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+
);
213
expect(await page.textContent('h1')).toMatch('Hello world!');
314
expect(await page.textContent('#test-child')).toBe('test-child');
415
expect(await page.textContent('#dependency-import')).toBe('dependency-import');

packages/e2e-tests/configfile-esm/__tests__/configfile-esm.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
it('should load config and work', async () => {
1+
import { editViteConfig } from 'testUtils';
2+
3+
it('should load default config and work', async () => {
4+
expect(await page.textContent('h1')).toMatch('Hello world!');
5+
expect(await page.textContent('#test-child')).toBe('test-child');
6+
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
7+
});
8+
9+
it('should load custom cjs config and work', async () => {
10+
await editViteConfig((c) =>
11+
c.replace('svelte()', `svelte({configFile:'svelte.config.custom.cjs'})`)
12+
);
213
expect(await page.textContent('h1')).toMatch('Hello world!');
314
expect(await page.textContent('#test-child')).toBe('test-child');
415
expect(await page.textContent('#dependency-import')).toBe('dependency-import');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const sveltePreprocess = require('svelte-preprocess');
2+
3+
module.exports = {
4+
preprocess: sveltePreprocess()
5+
};

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
getColor,
1010
editFile,
1111
addFile,
12-
removeFile
12+
removeFile,
13+
editViteConfig
1314
} from '../../testUtils';
1415

1516
test('should render App', async () => {
@@ -123,10 +124,7 @@ if (!isBuild) {
123124
});
124125

125126
test('should work with emitCss: false', async () => {
126-
await editFile('vite.config.js', (c) => c.replace('svelte()', 'svelte({emitCss:false})'));
127-
await sleep(isWin ? 1000 : 500); // editing vite config restarts server, give it some time
128-
await page.goto(viteTestUrl, { waitUntil: 'networkidle' });
129-
await sleep(50);
127+
await editViteConfig((c) => c.replace('svelte()', 'svelte({emitCss:false})'));
130128
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');
131129
expect(await getColor(`#hmr-test-1 .label`)).toBe('green');
132130
await (await getEl(`#hmr-test-1 .increment`)).click();

packages/e2e-tests/testUtils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,10 @@ export async function saveScreenshot(name?: string) {
196196
console.log('failed to take screenshot', e);
197197
}
198198
}
199+
200+
export async function editViteConfig(replacer: (str: string) => string) {
201+
editFile('vite.config.js', replacer);
202+
await sleep(isWin ? 1000 : 500); // editing vite config restarts server, give it some time
203+
await page.goto(viteTestUrl, { waitUntil: 'networkidle' });
204+
await sleep(50);
205+
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { createRequire } from 'module';
12
import path from 'path';
23
import fs from 'fs';
34
import { pathToFileURL } from 'url';
45
import { log } from './log';
56
import { Options } from './options';
67
import { UserConfig } from 'vite';
78

9+
// used to require cjs config in esm.
10+
// NOTE dynamic import() cjs technically works, but timestamp query cache bust
11+
// have no effect, likely because it has another internal cache?
12+
let esmRequire: NodeRequire;
13+
814
export const knownSvelteConfigNames = [
915
'svelte.config.js',
1016
'svelte.config.cjs',
@@ -46,9 +52,14 @@ export async function loadSvelteConfig(
4652
// cjs or error with dynamic import
4753
if (!configFile.endsWith('.mjs')) {
4854
try {
55+
// identify which require function to use (esm and cjs mode)
56+
const _require = import.meta.url
57+
? (esmRequire ??= createRequire(import.meta.url))
58+
: require;
59+
4960
// avoid loading cached version on reload
50-
delete require.cache[require.resolve(configFile)];
51-
const result = require(configFile);
61+
delete _require.cache[_require.resolve(configFile)];
62+
const result = _require(configFile);
5263
if (result != null) {
5364
return {
5465
...result,

0 commit comments

Comments
 (0)