Skip to content

Commit f54a03f

Browse files
committedMar 8, 2023
fix: Add export maps and .mjs to node ES modules (fixes remix-run#10172)
This fixes an issue with Preact + Vite + SSR. Without the export maps, Vite SSR and Node.js gets confused and loads both the ESM and CJS versions of Preact. This breaks the hooks system of Preact as discussed here[1] and here[2]. During SSR, Vite starts as ESM and loads my modules (and Preact). But when I load React-Router, as it does not have the exports map, Node loads the CJS version, which loads the CJS version of Preact. Then Preact breaks because it can't find the hooks registered in the ESM code. Also, in the version of Node that I tested, v16.14.0, Node printed the warning below, so I also changed the extension of the ES module for node from ".js" to ".mjs". (node:5753) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. [1] preactjs/preact#3220 (comment) [2] vitest-dev/vitest#747 (comment)
1 parent bc6fefa commit f54a03f

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed
 

‎.changeset/brave-humans-invite.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"react-router": patch
3+
"react-router-dom": patch
4+
---
5+
6+
Added export maps to package.json and .mjs extension for Node ES modules

‎packages/react-router-dom/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
"unpkg": "./dist/umd/react-router-dom.production.min.js",
2323
"module": "./dist/index.js",
2424
"types": "./dist/index.d.ts",
25+
"exports": {
26+
".": {
27+
"types": "./dist/index.d.ts",
28+
"browser": "./dist/react-router-dom.production.min.js",
29+
"umd": "./ddist/umd/react-router.production.min.js",
30+
"import": "./dist/index.mjs",
31+
"require": "./dist/main.js"
32+
}
33+
},
2534
"dependencies": {
2635
"@remix-run/router": "1.3.3",
2736
"react-router": "6.8.2"

‎packages/react-router-dom/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = function rollup() {
2121
{
2222
input: `${SOURCE_DIR}/index.tsx`,
2323
output: {
24-
file: `${OUTPUT_DIR}/index.js`,
24+
file: `${OUTPUT_DIR}/index.mjs`,
2525
format: "esm",
2626
sourcemap: !PRETTY,
2727
banner: createBanner("React Router DOM", version),

‎packages/react-router/package.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@
2020
"sideEffects": false,
2121
"main": "./dist/main.js",
2222
"unpkg": "./dist/umd/react-router.production.min.js",
23-
"module": "./dist/index.js",
23+
"module": "./dist/index.mjs",
2424
"types": "./dist/index.d.ts",
25+
"exports": {
26+
".": {
27+
"types": "./dist/index.d.ts",
28+
"browser": "./dist/react-router.production.min.js",
29+
"umd": "./ddist/umd/react-router.production.min.js",
30+
"import": "./dist/index.mjs",
31+
"require": "./dist/main.js"
32+
}
33+
},
2534
"dependencies": {
2635
"@remix-run/router": "1.3.3"
2736
},

‎packages/react-router/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = function rollup() {
2121
{
2222
input: `${SOURCE_DIR}/index.ts`,
2323
output: {
24-
file: `${OUTPUT_DIR}/index.js`,
24+
file: `${OUTPUT_DIR}/index.mjs`,
2525
format: "esm",
2626
sourcemap: !PRETTY,
2727
banner: createBanner("React Router", version),

0 commit comments

Comments
 (0)
Please sign in to comment.