react: Fix build with next/webpack + react@<18.0.0 #810
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.
build only cjs to be compatible with
react@<18.0.0
, and use a wrapper to support esm.About this PR
This is going to fix #782 .
What's the problem
In
@giscus/[email protected]
, next/webpack will resolve@giscus/react
to only./dist/index.cjs.js
whatever you writerequire
orimport
in your project, which escapes the issue.(
console.log
was added manually for debugging)But in
@giscus/[email protected]
, with.mjs
extension for esmimport
configured,@giscus/react
will be resolved to./dist/index.cjs
when usingrequire
, and./dist/index.mjs
when usingimport
../dist/index.mjs
importsreact/jsx-runtime
which triggers this issue from react facebook/react#20235 .The workaround
To be compatible with
react@<18.0.0
, we should onlyrequire
thereact/jsx-runtime
(no esmimport
) in our build products.To be esm compatible, we can use a wrapper mjs that imports the cjs product. Official approach here https://nodejs.org/api/packages.html#approach-1-use-an-es-module-wrapper
So I wrote a vite plugin to emit the wrapper file.
(
console.log
was added manually for debugging)Thus, everything works fine!
My testing environment
node: 16.18.1
npm: 8.19.2
next: 12.3.3
react: 17.0.2