Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

fix(webpack): support for jsx-runtime for react<18 with esm #615

Merged
merged 2 commits into from
Mar 7, 2024

Conversation

zacowan
Copy link
Member

@zacowan zacowan commented Feb 29, 2024

Description

Adds aliases to the webpack 5 config to allow webpack to resolve references to react/jsx-runtime and react/jsx-dev-runtime when being referenced by packages that declare themselves as modules.

Motivation

When bundling modules with webpack 5 that contain packages that declare themselves as modules and reference the newer JSX transforms supported by React, Webpack 5 cannot resolve the imports because React<18 does not have an exports field in its package.json. When trying to bundle modules that fit this criteria, Webpack 5 outputs the following error:

ModuleNotFoundError: Module not found: Error: Can't resolve 'react/jsx-runtime' in 'file' Did you mean 'jsx-runtime.js'?

BREAKING CHANGE: The request 'react/jsx-runtime' failed to resolve only because it was resolved as fully specified (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '.mjs' file, or a '.js' file where the package.json contains '"type": "module"'). The extension in the request is mandatory for it to be fully specified. Add the extension to the request.

Starting with React 18, the React team have provided an exports field in the package.json for React that solves this issue. However, they have declined to bring it into older versions of React: facebook/react#20235.

To solve this, we can add aliases to our Webpack config to resolve to the files that these imports refer to: facebook/react#20235 (comment).

Test Conditions

In a test module, I bundled code that imported things from a package that declared itself as a module and referred to react/jsx-runtime. Before this change, Webpack output an error. After this change, no errors are output and the module bundles successfully.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (adding or updating documentation)
  • Dependency update
  • Security update

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation and I have updated the documentation accordingly.
  • These changes should be applied to a maintenance branch.
  • I have added the Apache 2.0 license header to any new files created.

@zacowan zacowan force-pushed the fix/react-jsx-runtime-resolution branch from 55ecb82 to aad0828 Compare February 29, 2024 15:40
@code-forger code-forger requested a review from a team March 7, 2024 07:20
@Matthew-Mallimo Matthew-Mallimo enabled auto-merge (squash) March 7, 2024 12:21
@Matthew-Mallimo Matthew-Mallimo merged commit 42c2858 into main Mar 7, 2024
4 checks passed
@Matthew-Mallimo Matthew-Mallimo deleted the fix/react-jsx-runtime-resolution branch March 7, 2024 12:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants