Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions exports/index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// ES modules wrapper
import { createRequire } from 'node:module';
import pluginModule from '../dist/index.js';

const require = createRequire(import.meta.url);
const { ReactRefreshRspackPlugin } = require('../dist/index.js');
const ReactRefreshRspackPlugin =
pluginModule?.ReactRefreshRspackPlugin ??
pluginModule?.default ??
pluginModule;

// default export will be deprecated in next major version
export default ReactRefreshRspackPlugin;
Expand Down
31 changes: 31 additions & 0 deletions test/exports-default-fallback.spec.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';

test('esm wrapper supports default-only dist export shape', async () => {
const tempDir = mkdtempSync(join(tmpdir(), 'react-refresh-export-wrapper-'));
try {
const wrapperSource = readFileSync('exports/index.mjs', 'utf8');
const wrapperPath = join(tempDir, 'index.mjs');
const fixturePath = join(tempDir, 'dist-fixture.mjs');
const fixtureClassName = 'MockReactRefreshRspackPlugin';

writeFileSync(
fixturePath,
`export default class ${fixtureClassName} {\n constructor() {\n this.options = { reactRefreshLoader: true };\n }\n}\n`,
);
Comment thread
chenjiahan marked this conversation as resolved.
Outdated
writeFileSync(
wrapperPath,
wrapperSource.replace('../dist/index.js', './dist-fixture.mjs'),
);

const mod = await import(pathToFileURL(wrapperPath).href);
const instance = new mod.ReactRefreshRspackPlugin();

expect(instance.options.reactRefreshLoader).toBeTruthy();
expect(mod.default).toBe(mod.ReactRefreshRspackPlugin);
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});