Skip to content

Commit 071adae

Browse files
authored
fix(react): Fix React jsx runtime import for esm (#12740)
1 parent a8c4db8 commit 071adae

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

dev-packages/rollup-utils/npmHelpers.mjs

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
makeSetSDKSourcePlugin,
2323
makeSucrasePlugin,
2424
} from './plugins/index.mjs';
25-
import { makePackageNodeEsm } from './plugins/make-esm-plugin.mjs';
25+
import { makePackageNodeEsm, makeReactEsmJsxRuntimePlugin } from './plugins/make-esm-plugin.mjs';
2626
import { mergePlugins } from './utils.mjs';
2727

2828
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -140,7 +140,11 @@ export function makeNPMConfigVariants(baseConfig, options = {}) {
140140

141141
if (emitEsm) {
142142
variantSpecificConfigs.push({
143-
output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm'), plugins: [makePackageNodeEsm()] },
143+
output: {
144+
format: 'esm',
145+
dir: path.join(baseConfig.output.dir, 'esm'),
146+
plugins: [makePackageNodeEsm(), makeReactEsmJsxRuntimePlugin()],
147+
},
144148
});
145149
}
146150

dev-packages/rollup-utils/plugins/make-esm-plugin.mjs

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import replacePlugin from '@rollup/plugin-replace';
23

34
/**
45
* Outputs a package.json file with {type: module} in the root of the output directory so that Node
@@ -29,3 +30,17 @@ export function makePackageNodeEsm() {
2930
},
3031
};
3132
}
33+
34+
/**
35+
* Makes sure that whenever we add an `react/jsx-runtime` import, we add a `.js` to make the import esm compatible.
36+
*/
37+
export function makeReactEsmJsxRuntimePlugin() {
38+
return replacePlugin({
39+
preventAssignment: false,
40+
sourceMap: true,
41+
values: {
42+
"'react/jsx-runtime'": "'react/jsx-runtime.js'",
43+
'"react/jsx-runtime"': '"react/jsx-runtime.js"',
44+
},
45+
});
46+
}

0 commit comments

Comments
 (0)