Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
8eb97306
Unverified
Commit
8eb97306
authored
Jul 03, 2023
by
Shu Ding
Committed by
GitHub
Jul 03, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid unnecessary `resolveExternal` calls (#52053)
parent
967b876f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
58 deletions
+58
-58
webpack-config.ts
packages/next/src/build/webpack-config.ts
+58
-33
app-external.test.ts
test/e2e/app-dir/app-external/app-external.test.ts
+0
-25
No files found.
packages/next/src/build/webpack-config.ts
View file @
8eb97306
...
@@ -1425,6 +1425,64 @@ export default async function getBaseWebpackConfig(
...
@@ -1425,6 +1425,64 @@ export default async function getBaseWebpackConfig(
}
}
}
}
// Don't bundle @vercel/og nodejs bundle for nodejs runtime.
// TODO-APP: bundle route.js with different layer that externals common node_module deps.
if
(
layer
===
WEBPACK_LAYERS
.
server
&&
request
===
'next/dist/compiled/@vercel/og/index.node.js'
)
{
return
`module
${
request
}
`
}
// Specific Next.js imports that should remain external
// TODO-APP: Investigate if we can remove this.
if
(
request
.
startsWith
(
'next/dist/'
))
{
// Image loader needs to be transpiled
if
(
/^next
\/
dist
\/
shared
\/
lib
\/
image-loader/
.
test
(
request
))
{
return
}
if
(
/^next
\/
dist
\/
shared
\/(?!
lib
\/
router
\/
router
)
/
.
test
(
request
)
||
/^next
\/
dist
\/
compiled
\/
.*
\.
c
?
js$/
.
test
(
request
)
)
{
return
`commonjs
${
request
}
`
}
if
(
/^next
\/
dist
\/
esm
\/
shared
\/(?!
lib
\/
router
\/
router
)
/
.
test
(
request
)
||
/^next
\/
dist
\/
compiled
\/
.*
\.
mjs$/
.
test
(
request
)
)
{
return
`module
${
request
}
`
}
// Other Next.js internals need to be transpiled.
return
}
// Early return if the request needs to be bundled, such as in the client layer.
// Treat react packages and next internals as external for SSR layer,
// also map react to builtin ones with require-hook.
if
(
layer
===
WEBPACK_LAYERS
.
client
)
{
if
(
reactPackagesRegex
.
test
(
request
))
{
return
`commonjs next/dist/compiled/
${
request
.
replace
(
/^
(
react-server-dom-webpack|react-dom|react
)
/
,
(
name
)
=>
{
return
name
+
bundledReactChannel
}
)}
`
}
const
isRelative
=
request
.
startsWith
(
'.'
)
const
fullRequest
=
isRelative
?
path
.
join
(
context
,
request
).
replace
(
/
\\
/g
,
'/'
)
:
request
const
resolveNextExternal
=
isLocalCallback
(
fullRequest
)
return
resolveNextExternal
}
// TODO-APP: Let's avoid this resolve call as much as possible, and eventually get rid of it.
const
resolveResult
=
await
resolveExternal
(
const
resolveResult
=
await
resolveExternal
(
dir
,
dir
,
config
.
experimental
.
esmExternals
,
config
.
experimental
.
esmExternals
,
...
@@ -1446,15 +1504,6 @@ export default async function getBaseWebpackConfig(
...
@@ -1446,15 +1504,6 @@ export default async function getBaseWebpackConfig(
resolveResult
.
res
=
require
.
resolve
(
request
)
resolveResult
.
res
=
require
.
resolve
(
request
)
}
}
// Don't bundle @vercel/og nodejs bundle for nodejs runtime.
// TODO-APP: bundle route.js with different layer that externals common node_module deps.
if
(
layer
===
WEBPACK_LAYERS
.
server
&&
request
===
'next/dist/compiled/@vercel/og/index.node.js'
)
{
return
`module
${
request
}
`
}
const
{
res
,
isEsm
}
=
resolveResult
const
{
res
,
isEsm
}
=
resolveResult
// If the request cannot be resolved we need to have
// If the request cannot be resolved we need to have
...
@@ -1473,18 +1522,8 @@ export default async function getBaseWebpackConfig(
...
@@ -1473,18 +1522,8 @@ export default async function getBaseWebpackConfig(
const
externalType
=
isEsm
?
'module'
:
'commonjs'
const
externalType
=
isEsm
?
'module'
:
'commonjs'
if
(
/next
[/\\]
dist
[/\\](
esm
[\\/])?
shared
[/\\](?!
lib
[/\\]
router
[/\\]
router
)
/
.
test
(
res
)
||
/next
[/\\]
dist
[/\\]
compiled
[/\\]
.*
\.[
mc
]?
js$/
.
test
(
res
)
)
{
return
`
${
externalType
}
${
request
}
`
}
// Default pages have to be transpiled
// Default pages have to be transpiled
if
(
if
(
/
[/\\]
next
[/\\]
dist
[/\\]
/
.
test
(
res
)
||
// This is the @babel/plugin-transform-runtime "helpers: true" option
// This is the @babel/plugin-transform-runtime "helpers: true" option
/node_modules
[/\\]
@babel
[/\\]
runtime
[/\\]
/
.
test
(
res
)
/node_modules
[/\\]
@babel
[/\\]
runtime
[/\\]
/
.
test
(
res
)
)
{
)
{
...
@@ -1544,20 +1583,6 @@ export default async function getBaseWebpackConfig(
...
@@ -1544,20 +1583,6 @@ export default async function getBaseWebpackConfig(
return
return
}
}
// Treat react packages and next internals as external for SSR layer,
// also map react to builtin ones with require-hook.
if
(
layer
===
WEBPACK_LAYERS
.
client
)
{
if
(
reactPackagesRegex
.
test
(
request
))
{
return
`commonjs next/dist/compiled/
${
request
.
replace
(
/^
(
react-server-dom-webpack|react-dom|react
)
/
,
(
name
)
=>
{
return
name
+
bundledReactChannel
}
)}
`
}
return
}
if
(
shouldBeBundled
)
return
if
(
shouldBeBundled
)
return
// Anything else that is standard JavaScript within `node_modules`
// Anything else that is standard JavaScript within `node_modules`
...
...
test/e2e/app-dir/app-external/app-external.test.ts
View file @
8eb97306
...
@@ -214,31 +214,6 @@ createNextDescribe(
...
@@ -214,31 +214,6 @@ createNextDescribe(
expect
(
html
).
toContain
(
'Foo'
)
expect
(
html
).
toContain
(
'Foo'
)
})
})
if
(
isNextDev
)
{
it
(
'should error for require ESM package in CJS package'
,
async
()
=>
{
const
page
=
'app/cjs-import-esm/page.js'
// reuse esm-client-ref/page.js
const
pageSource
=
await
next
.
readFile
(
'app/esm-client-ref/page.js'
)
try
{
await
next
.
patchFile
(
page
,
pageSource
.
replace
(
"'client-esm-module'"
,
"'client-cjs-import-esm-wildcard'"
)
)
await
next
.
render
(
'/cjs-import-esm'
)
}
finally
{
await
next
.
patchFile
(
page
,
pageSource
)
}
expect
(
next
.
cliOutput
).
toInclude
(
`ESM packages (client-esm-module-wildcard) need to be imported`
)
})
}
it
(
'should have proper tree-shaking for known modules in CJS'
,
async
()
=>
{
it
(
'should have proper tree-shaking for known modules in CJS'
,
async
()
=>
{
const
html
=
await
next
.
render
(
'/test-middleware'
)
const
html
=
await
next
.
render
(
'/test-middleware'
)
expect
(
html
).
toContain
(
'it works'
)
expect
(
html
).
toContain
(
'it works'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment