diff --git a/exports/index.mjs b/exports/index.mjs index d602f0f..c650585 100644 --- a/exports/index.mjs +++ b/exports/index.mjs @@ -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; diff --git a/test/exports-default-fallback.spec.mts b/test/exports-default-fallback.spec.mts new file mode 100644 index 0000000..bb7fd45 --- /dev/null +++ b/test/exports-default-fallback.spec.mts @@ -0,0 +1,34 @@ +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'; + const fixtureSource = `export default class ${fixtureClassName} { + constructor() { + this.options = { reactRefreshLoader: true }; + } +} +`; + + writeFileSync(fixturePath, fixtureSource); + 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 }); + } +});