Skip to content

Commit 385148a

Browse files
authored
fix(exports): support default-only module shape in ESM wrapper (#72)
1 parent 654851d commit 385148a

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

exports/index.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// ES modules wrapper
2-
import { createRequire } from 'node:module';
2+
import pluginModule from '../dist/index.js';
33

4-
const require = createRequire(import.meta.url);
5-
const { ReactRefreshRspackPlugin } = require('../dist/index.js');
4+
const ReactRefreshRspackPlugin =
5+
pluginModule?.ReactRefreshRspackPlugin ??
6+
pluginModule?.default ??
7+
pluginModule;
68

79
// default export will be deprecated in next major version
810
export default ReactRefreshRspackPlugin;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2+
import { tmpdir } from 'node:os';
3+
import { join } from 'node:path';
4+
import { pathToFileURL } from 'node:url';
5+
6+
test('esm wrapper supports default-only dist export shape', async () => {
7+
const tempDir = mkdtempSync(join(tmpdir(), 'react-refresh-export-wrapper-'));
8+
try {
9+
const wrapperSource = readFileSync('exports/index.mjs', 'utf8');
10+
const wrapperPath = join(tempDir, 'index.mjs');
11+
const fixturePath = join(tempDir, 'dist-fixture.mjs');
12+
const fixtureClassName = 'MockReactRefreshRspackPlugin';
13+
const fixtureSource = `export default class ${fixtureClassName} {
14+
constructor() {
15+
this.options = { reactRefreshLoader: true };
16+
}
17+
}
18+
`;
19+
20+
writeFileSync(fixturePath, fixtureSource);
21+
writeFileSync(
22+
wrapperPath,
23+
wrapperSource.replace('../dist/index.js', './dist-fixture.mjs'),
24+
);
25+
26+
const mod = await import(pathToFileURL(wrapperPath).href);
27+
const instance = new mod.ReactRefreshRspackPlugin();
28+
29+
expect(instance.options.reactRefreshLoader).toBeTruthy();
30+
expect(mod.default).toBe(mod.ReactRefreshRspackPlugin);
31+
} finally {
32+
rmSync(tempDir, { recursive: true, force: true });
33+
}
34+
});

0 commit comments

Comments
 (0)