Skip to content

Fix React Native with webpack 5 / Rspack - #4754

Merged
kubk merged 1 commit into
mobxjs:mainfrom
jzhan-canva:jzhan-esm-js-module-type
Oct 27, 2026
Merged

kubk merged 1 commit into
mobxjs:mainfrom
jzhan-canva:jzhan-esm-js-module-type

Conversation

@jzhan-canva

Copy link
Copy Markdown
Contributor

Problem

MobX 7 added "type": "commonjs" to mobx, mobx-react-lite and mobx-react. In each package, the react-native export condition and the react-native and module fields still point at dist/<pkg>.esm.js, which is an ES module. The publish-only dist/<pkg>.esm.development.js and dist/<pkg>.esm.production.min.js builds are ES modules too.

Bundlers that honour type for .js files 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 and resolve.conditionNames: ['react-native', 'import', 'require', 'default']:

Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (5420:0)

Metro doesn't read type, so it isn't affected. MobX 6 didn't declare type, 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 without type is CommonJS by default in Node, so dist/index.js and its cjs builds load exactly as before. TypeScript's node16/nodenext resolution also treats the declarations as CommonJS, as before. The import condition still resolves to the .mjs build.

I checked the published mobx@7.1.1 with the field removed:

  • webpack 5.99 with the react-native condition now bundles mobx.esm.js without errors.
  • On Node 24, require('mobx'), import 'mobx' and a deep import('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 into dist/esm/, next to a package.json containing { "type": "module" }. Every file's declared format would then match its syntax for every tool, including the legacy module and react-native fields, and the builds keep their .js extension. The trade-off is that it moves files that "./dist/*" exposes, so deep imports such as mobx/dist/mobx.esm.js would 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

  • Added/updated unit tests (packaging only; verified by bundling and loading the published package as above)
  • Updated /docs. For new functionality, at least API.md should be updated (no API change)
  • Verified that there is no significant performance drop (npm -w mobx run test:performance) (packaging only)

🤖 Generated with Claude Code

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-bot

changeset-bot Bot commented Oct 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f989b82

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
mobx Patch
mobx-react-lite Patch
mobx-react Patch

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

@kubk kubk changed the title don't mark the packages as CommonJS Fix React Native with webpack 5 / Rspack Oct 27, 2026
@kubk

kubk commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Thank you

@kubk
kubk merged commit 1442c8b into mobxjs:main Oct 27, 2026
1 check passed
@github-actions github-actions Bot mentioned this pull request Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants