Skip to content

Commit dfa4cbd

Browse files
Fix: TS config file using DefineConfig in a non-type="module" project (#917)
* fix: Enforce that wmr.config.ts be transpiled to ESM * docs: Adding changeset * Apply suggestions from code review Co-authored-by: Ryan Christian <[email protected]> Co-authored-by: Jason Miller <[email protected]>
1 parent d5d8fff commit dfa4cbd

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

.changeset/eighty-rivers-compare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'wmr': patch
3+
---
4+
5+
Ensures TS config file is transpiled to ESM so that it can use `defineConfig`

packages/wmr/src/lib/compile-single-module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let cache;
3030
* @param {import('rollup').ModuleFormat} [options.format]
3131
*/
3232
export const compileSingleModule = withCache(
33-
async (input, { out, hmr = true, rewriteNodeImports = true, format = 'es' }) => {
33+
async (input, { out, filename, hmr = true, rewriteNodeImports = true, format = 'es' }) => {
3434
// The TS config file should be the only one that passes through here
3535
const isConfigFile = input.endsWith('wmr.config.ts');
3636
input = input.replace(/\.css\.js$/, '.css');
@@ -73,7 +73,7 @@ export const compileSingleModule = withCache(
7373
const result = await bundle.write({
7474
...ROLLUP_FAST_OUTPUT,
7575
dir: out,
76-
assetFileNames: '[name].[ext]',
76+
entryFileNames: filename || '[name].[ext]',
7777
paths: str => str,
7878
format,
7979
exports: 'auto'

packages/wmr/src/lib/normalize-options.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ export async function normalizeOptions(options, mode, configWatchFiles = []) {
9999
if (ext === '.ts') {
100100
// Create a temporary file to write compiled output into
101101
// TODO: Do this in memory
102-
configFile = resolve(options.cwd, 'wmr.config.js');
102+
const tmpConfig = `wmr.config.${pkg && pkg.type === 'module' ? 'js' : 'mjs'}`;
103+
configFile = resolve(options.cwd, tmpConfig);
103104
await compileSingleModule(file, {
104105
out: resolve('.'),
106+
filename: tmpConfig,
105107
hmr: false,
106108
rewriteNodeImports: false,
107-
format: pkg && pkg.type === 'module' ? 'es' : 'commonjs'
109+
format: 'es'
108110
});
109111
}
110112

packages/wmr/test/config.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ describe('config', () => {
2626
await waitForMessage(instance.output, /name: ["']foo["']/);
2727
});
2828

29-
it('should support setting options via object arg (ts)', async () => {
29+
it('should support setting options via object arg (ts + esm)', async () => {
3030
await loadFixture('define-config-typescript', env);
3131
instance = await runWmrFast(env.tmp.path);
3232
await instance.address;
3333
await waitForMessage(instance.output, /name: ["']foo["']/);
3434
});
3535

36+
it('should support setting options via object arg (ts + cjs)', async () => {
37+
await loadFixture('define-config-typescript-cjs', env);
38+
instance = await runWmrFast(env.tmp.path);
39+
await instance.address;
40+
await waitForMessage(instance.output, /name: ["']foo["']/);
41+
});
42+
3643
it('should support setting options via function', async () => {
3744
await loadFixture('define-config-fn', env);
3845
instance = await runWmrFast(env.tmp.path);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>it works</h1>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from 'wmr';
2+
3+
export default defineConfig({
4+
plugins: [{ name: 'foo' }]
5+
});

0 commit comments

Comments
 (0)