Skip to content

Commit 9ea8ea0

Browse files
committed
fix(exports): support default-only esm wrapper shape
1 parent 654851d commit 9ea8ea0

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
14+
writeFileSync(
15+
fixturePath,
16+
`export default class ${fixtureClassName} {\n constructor() {\n this.options = { reactRefreshLoader: true };\n }\n}\n`,
17+
);
18+
writeFileSync(
19+
wrapperPath,
20+
wrapperSource.replace('../dist/index.js', './dist-fixture.mjs'),
21+
);
22+
23+
const mod = await import(pathToFileURL(wrapperPath).href);
24+
const instance = new mod.ReactRefreshRspackPlugin();
25+
26+
expect(instance.options.reactRefreshLoader).toBeTruthy();
27+
expect(mod.default).toBe(mod.ReactRefreshRspackPlugin);
28+
} finally {
29+
rmSync(tempDir, { recursive: true, force: true });
30+
}
31+
});

0 commit comments

Comments
 (0)