Skip to content

Commit

Permalink
fix(react): Fix React jsx runtime import for esm (#12740)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Jul 3, 2024
1 parent a8c4db8 commit 071adae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dev-packages/rollup-utils/npmHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
makeSetSDKSourcePlugin,
makeSucrasePlugin,
} from './plugins/index.mjs';
import { makePackageNodeEsm } from './plugins/make-esm-plugin.mjs';
import { makePackageNodeEsm, makeReactEsmJsxRuntimePlugin } from './plugins/make-esm-plugin.mjs';
import { mergePlugins } from './utils.mjs';

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

if (emitEsm) {
variantSpecificConfigs.push({
output: { format: 'esm', dir: path.join(baseConfig.output.dir, 'esm'), plugins: [makePackageNodeEsm()] },
output: {
format: 'esm',
dir: path.join(baseConfig.output.dir, 'esm'),
plugins: [makePackageNodeEsm(), makeReactEsmJsxRuntimePlugin()],
},
});
}

Expand Down
15 changes: 15 additions & 0 deletions dev-packages/rollup-utils/plugins/make-esm-plugin.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs';
import replacePlugin from '@rollup/plugin-replace';

/**
* Outputs a package.json file with {type: module} in the root of the output directory so that Node
Expand Down Expand Up @@ -29,3 +30,17 @@ export function makePackageNodeEsm() {
},
};
}

/**
* Makes sure that whenever we add an `react/jsx-runtime` import, we add a `.js` to make the import esm compatible.
*/
export function makeReactEsmJsxRuntimePlugin() {
return replacePlugin({
preventAssignment: false,
sourceMap: true,
values: {
"'react/jsx-runtime'": "'react/jsx-runtime.js'",
'"react/jsx-runtime"': '"react/jsx-runtime.js"',
},
});
}

0 comments on commit 071adae

Please sign in to comment.