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
4518c1ec
Commit
4518c1ec
authored
Mar 02, 2024
by
JJ Kasper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reduce memory/cache overhead from over loader processing #62005
parent
1ad65483
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
11 deletions
+47
-11
webpack-config.ts
packages/next/src/build/webpack-config.ts
+2
-1
next-swc-loader.ts
packages/next/src/build/webpack/loaders/next-swc-loader.ts
+45
-0
index.ts
.../build/webpack/plugins/terser-webpack-plugin/src/index.ts
+0
-10
No files found.
packages/next/src/build/webpack-config.ts
View file @
4518c1ec
...
@@ -103,7 +103,7 @@ if (parseInt(React.version) < 18) {
...
@@ -103,7 +103,7 @@ if (parseInt(React.version) < 18) {
throw
new
Error
(
'Next.js requires react >= 18.2.0 to be installed.'
)
throw
new
Error
(
'Next.js requires react >= 18.2.0 to be installed.'
)
}
}
const
babelIncludeRegexes
:
RegExp
[]
=
[
export
const
babelIncludeRegexes
:
RegExp
[]
=
[
/next
[\\/]
dist
[\\/](
esm
[\\/])?
shared
[\\/]
lib/
,
/next
[\\/]
dist
[\\/](
esm
[\\/])?
shared
[\\/]
lib/
,
/next
[\\/]
dist
[\\/](
esm
[\\/])?
client/
,
/next
[\\/]
dist
[\\/](
esm
[\\/])?
client/
,
/next
[\\/]
dist
[\\/](
esm
[\\/])?
pages/
,
/next
[\\/]
dist
[\\/](
esm
[\\/])?
pages/
,
...
@@ -455,6 +455,7 @@ export default async function getBaseWebpackConfig(
...
@@ -455,6 +455,7 @@ export default async function getBaseWebpackConfig(
jsConfig
,
jsConfig
,
supportedBrowsers
,
supportedBrowsers
,
swcCacheDir
:
path
.
join
(
dir
,
config
?.
distDir
??
'.next'
,
'cache'
,
'swc'
),
swcCacheDir
:
path
.
join
(
dir
,
config
?.
distDir
??
'.next'
,
'cache'
,
'swc'
),
transpilePackages
:
config
.
transpilePackages
,
...
extraOptions
,
...
extraOptions
,
}
satisfies
SWCLoaderOptions
,
}
satisfies
SWCLoaderOptions
,
}
}
...
...
packages/next/src/build/webpack/loaders/next-swc-loader.ts
View file @
4518c1ec
...
@@ -31,6 +31,22 @@ import type { WebpackLayerName } from '../../../lib/constants'
...
@@ -31,6 +31,22 @@ import type { WebpackLayerName } from '../../../lib/constants'
import
{
isWasm
,
transform
}
from
'../../swc'
import
{
isWasm
,
transform
}
from
'../../swc'
import
{
getLoaderSWCOptions
}
from
'../../swc/options'
import
{
getLoaderSWCOptions
}
from
'../../swc/options'
import
path
,
{
isAbsolute
}
from
'path'
import
path
,
{
isAbsolute
}
from
'path'
import
{
babelIncludeRegexes
}
from
'../../webpack-config'
import
{
isResourceInPackages
}
from
'../../handle-externals'
const
maybeExclude
=
(
excludePath
:
string
,
transpilePackages
:
string
[]
):
boolean
=>
{
if
(
babelIncludeRegexes
.
some
((
r
)
=>
r
.
test
(
excludePath
)))
{
return
false
}
const
shouldBeBundled
=
isResourceInPackages
(
excludePath
,
transpilePackages
)
if
(
shouldBeBundled
)
return
false
return
excludePath
.
includes
(
'node_modules'
)
}
export
interface
SWCLoaderOptions
{
export
interface
SWCLoaderOptions
{
rootDir
:
string
rootDir
:
string
...
@@ -46,8 +62,14 @@ export interface SWCLoaderOptions {
...
@@ -46,8 +62,14 @@ export interface SWCLoaderOptions {
serverComponents
?:
boolean
serverComponents
?:
boolean
bundleLayer
?:
WebpackLayerName
bundleLayer
?:
WebpackLayerName
esm
?:
boolean
esm
?:
boolean
transpilePackages
?:
string
[]
}
}
// these are exact code conditions checked
// for to force transpiling a `node_module`
const
FORCE_TRANSPILE_CONDITIONS
=
/
(
next
\/
font|next
\/
dynamic|use server|use client
)
/
async
function
loaderTransform
(
async
function
loaderTransform
(
this
:
any
,
this
:
any
,
parentTrace
:
any
,
parentTrace
:
any
,
...
@@ -58,6 +80,20 @@ async function loaderTransform(
...
@@ -58,6 +80,20 @@ async function loaderTransform(
const
filename
=
this
.
resourcePath
const
filename
=
this
.
resourcePath
let
loaderOptions
:
SWCLoaderOptions
=
this
.
getOptions
()
||
{}
let
loaderOptions
:
SWCLoaderOptions
=
this
.
getOptions
()
||
{}
const
shouldMaybeExclude
=
maybeExclude
(
filename
,
loaderOptions
.
transpilePackages
||
[]
)
if
(
shouldMaybeExclude
)
{
if
(
!
source
)
{
throw
new
Error
(
`Invariant might be excluded but missing source`
)
}
if
(
!
FORCE_TRANSPILE_CONDITIONS
.
test
(
source
))
{
return
[
source
,
inputSourceMap
]
}
}
const
{
const
{
isServer
,
isServer
,
...
@@ -150,8 +186,17 @@ const EXCLUDED_PATHS =
...
@@ -150,8 +186,17 @@ const EXCLUDED_PATHS =
export
function
pitch
(
this
:
any
)
{
export
function
pitch
(
this
:
any
)
{
const
callback
=
this
.
async
()
const
callback
=
this
.
async
()
let
loaderOptions
:
SWCLoaderOptions
=
this
.
getOptions
()
||
{}
const
shouldMaybeExclude
=
maybeExclude
(
this
.
resourcePath
,
loaderOptions
.
transpilePackages
||
[]
)
;(
async
()
=>
{
;(
async
()
=>
{
if
(
if
(
// if it might be excluded/no-op we can't use pitch loader
!
shouldMaybeExclude
&&
// TODO: investigate swc file reading in PnP mode?
// TODO: investigate swc file reading in PnP mode?
!
process
.
versions
.
pnp
&&
!
process
.
versions
.
pnp
&&
!
EXCLUDED_PATHS
.
test
(
this
.
resourcePath
)
&&
!
EXCLUDED_PATHS
.
test
(
this
.
resourcePath
)
&&
...
...
packages/next/src/build/webpack/plugins/terser-webpack-plugin/src/index.ts
View file @
4518c1ec
...
@@ -98,16 +98,6 @@ export class TerserPlugin {
...
@@ -98,16 +98,6 @@ export class TerserPlugin {
return
false
return
false
}
}
// don't minify _middleware as it can break in some cases
// and doesn't provide too much of a benefit as it's server-side
if
(
name
.
match
(
/
(
edge-runtime-webpack
\.
js|edge-chunks|middleware
\.
js$
)
/
)
)
{
return
false
}
const
{
info
}
=
res
const
{
info
}
=
res
// Skip double minimize assets from child compilation
// Skip double minimize assets from child compilation
...
...
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