Skip to content

Commit

Permalink
fix: Add export maps and .mjs to node ES modules (fixes #10172)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
rhengles committed Mar 8, 2023
1 parent bc6fefa commit f54a03f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/brave-humans-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"react-router": patch
"react-router-dom": patch
---

Added export maps to package.json and .mjs extension for Node ES modules
9 changes: 9 additions & 0 deletions packages/react-router-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
"unpkg": "./dist/umd/react-router-dom.production.min.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"browser": "./dist/react-router-dom.production.min.js",
"umd": "./ddist/umd/react-router.production.min.js",
"import": "./dist/index.mjs",
"require": "./dist/main.js"
}
},
"dependencies": {
"@remix-run/router": "1.3.3",
"react-router": "6.8.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router-dom/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function rollup() {
{
input: `${SOURCE_DIR}/index.tsx`,
output: {
file: `${OUTPUT_DIR}/index.js`,
file: `${OUTPUT_DIR}/index.mjs`,
format: "esm",
sourcemap: !PRETTY,
banner: createBanner("React Router DOM", version),
Expand Down
11 changes: 10 additions & 1 deletion packages/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@
"sideEffects": false,
"main": "./dist/main.js",
"unpkg": "./dist/umd/react-router.production.min.js",
"module": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"browser": "./dist/react-router.production.min.js",
"umd": "./ddist/umd/react-router.production.min.js",
"import": "./dist/index.mjs",
"require": "./dist/main.js"
}
},
"dependencies": {
"@remix-run/router": "1.3.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function rollup() {
{
input: `${SOURCE_DIR}/index.ts`,
output: {
file: `${OUTPUT_DIR}/index.js`,
file: `${OUTPUT_DIR}/index.mjs`,
format: "esm",
sourcemap: !PRETTY,
banner: createBanner("React Router", version),
Expand Down

0 comments on commit f54a03f

Please sign in to comment.