You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
next.js does not honor webpack root-style externals during build phase. For example:
// next.config.js// consider adding a webpack externals entry as follows to next.js webpack configuration{react: "root React"// now require("react") statements should use [global.]React}
on npm run build (next build):
$ yarn build
yarn run v1.22.4
$ next build
info - Creating an optimized production build
info - Compiled successfully
> Build error occurred
ReferenceError: React is not defined
at Object.cDcd (/Users/c0d01a5/node/next-root-externals-bork/next-root-externals-bork/.next/server/pages/_app.js:245:18)
at __webpack_require__ (/Users/c0d01a5/node/next-root-externals-bork/next-root-externals-bork/.next/server/pages/_app.js:23:31)
...
In development, this all works great! I provide my resources in my custom server, and everything is peachy 🍑 . Build time only, does this become a problem.
Expected behavior
Allow me to use webpack root externalized resources on build. One could argue that next DOES respect my externals--it is trying to lookup React--however, the way the build is executed prevents me from exposing my externals to the build worker. Because the build happens in a worker, my global, root-style resources get dropped from the current node process.
One solve would be allow users to participate in the worker process. The solve would be to introduce a super thin file, such as next-worker.js. Everytime we create a worker now, we would instead go through this script first:
all workers enter via next-worker.js
setupArgs = [...next.config.js::onWorkerInit (optional array of userland scripts), "original-worker-target.js"]
next-worker.js executes each script in setupArgs.
In this fashion, next still does all of it's normal jest-worker goodness, just allows userland to participate.
For example, right now, everytime we create a worker, we do:
just a slight schema change, with a proxied entry to allow user scripts in.
Why would anyone want to externalize something like this?
I have a CDN-hosted, isomorphic lib that loads resources onto the window & into node. This library has things like react, react-dom, and some other common libs that our application uses. For reasons beyond this PR, we do not want next.js to bundle these assets--we want to control such assets. We ideally want to use the same library that we use client-side on the server to provide strongly aligned resources--hence the usage of root externals!
Screenshots
n/a
System information
Next data (cat package.json | grep next | pbcopy):
This discussion was converted from issue #16130 on April 12, 2021 12:01.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Bug report
Describe the bug
next.js
does not honor webpack root-styleexternals
during build phase. For example:on
npm run build
(next build
):To Reproduce
Reproduction is very easy.
create-next-app
demonstration here: https://github.com/cdaringe/next-root-externals-bork.As you can see, I:
react
usingroot React
into webpackindex.js
on build, the error is emitted.
In development, this all works great! I provide my resources in my custom server, and everything is peachy 🍑 . Build time only, does this become a problem.
Expected behavior
Allow me to use webpack
root
externalized resources on build. One could argue thatnext
DOES respect my externals--it is trying to lookupReact
--however, the way the build is executed prevents me from exposing my externals to the build worker. Because the build happens in a worker, myglobal
,root
-style resources get dropped from the current node process.One solve would be allow users to participate in the worker process. The solve would be to introduce a super thin file, such as
next-worker.js
. Everytime we create a worker now, we would instead go through this script first:next-worker.js
setupArgs = [...next.config.js::onWorkerInit (optional array of userland scripts), "original-worker-target.js"]
next-worker.js
executes each script in setupArgs.In this fashion, next still does all of it's normal jest-worker goodness, just allows userland to participate.
For example, right now, everytime we create a worker, we do:
this proposal would suggest instead:
just a slight schema change, with a proxied entry to allow user scripts in.
I have a CDN-hosted, isomorphic lib that loads resources onto the window & into node. This library has things like react, react-dom, and some other common libs that our application uses. For reasons beyond this PR, we do not want next.js to bundle these assets--we want to control such assets. We ideally want to use the same library that we use client-side on the server to provide strongly aligned resources--hence the usage of
root
externals!Screenshots
n/a
System information
Next data (
cat package.json | grep next | pbcopy
):From
npx envinfo | pbcopy
:Additional context
Add any other context about the problem here.
Beta Was this translation helpful? Give feedback.
All reactions