Repository navigation
Fix React Native with webpack 5 / Rspack - #4754
Merged
Merged
Conversation
The `react-native` and `module` entries point at the ESM `dist/*.esm.js` builds. With `"type": "commonjs"`, bundlers that honour the field, such as webpack, Rspack and Re.Pack, parse those files as CommonJS and fail on their `import`/`export` statements. Without it they detect the module format from the syntax, as with mobx 6, and Node still loads `dist/index.js` as CommonJS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: f989b82 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Collaborator
|
Thank you |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MobX 7 added
"type": "commonjs"tomobx,mobx-react-liteandmobx-react. In each package, thereact-nativeexport condition and thereact-nativeandmodulefields still point atdist/<pkg>.esm.js, which is an ES module. The publish-onlydist/<pkg>.esm.development.jsanddist/<pkg>.esm.production.min.jsbuilds are ES modules too.Bundlers that honour
typefor.jsfiles parse those files as CommonJS and fail. This affects webpack 5, Rspack, and anything built on them, such as Re.Pack for React Native. For example, with webpack 5.99 andresolve.conditionNames: ['react-native', 'import', 'require', 'default']:Metro doesn't read
type, so it isn't affected. MobX 6 didn't declaretype, so these bundlers detected the format from the file's syntax.Fix
Remove
"type": "commonjs"from the three manifests, which is how MobX 6 shipped. A package withouttypeis CommonJS by default in Node, sodist/index.jsand itscjsbuilds load exactly as before. TypeScript'snode16/nodenextresolution also treats the declarations as CommonJS, as before. Theimportcondition still resolves to the.mjsbuild.I checked the published
mobx@7.1.1with the field removed:react-nativecondition now bundlesmobx.esm.jswithout errors.require('mobx'),import 'mobx'and a deepimport('mobx/dist/mobx.esm.js')all work. With the field, the deep import fails.The changeset bumps all three packages as a patch.
Alternative
This PR restores MobX 6's layout, where bundlers detect the format from the syntax. The fully explicit layout would keep
"type": "commonjs"and move the ESM builds intodist/esm/, next to apackage.jsoncontaining{ "type": "module" }. Every file's declared format would then match its syntax for every tool, including the legacymoduleandreact-nativefields, and the builds keep their.jsextension. The trade-off is that it moves files that"./dist/*"exposes, so deep imports such asmobx/dist/mobx.esm.jswould break. That probably makes it a change for a minor or major release rather than a patch. Happy to rework this PR that way if you prefer it.Code change checklist
/docs. For new functionality, at leastAPI.mdshould be updated (no API change)npm -w mobx run test:performance) (packaging only)🤖 Generated with Claude Code