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
b4b68c0b
Unverified
Commit
b4b68c0b
authored
Jun 27, 2020
by
Tim Neutkens
Committed by
GitHub
Jun 27, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use assetEmitted hook instead of afterEmit (#14626)
Makes sure this works correctly with webpack 5.
parent
bd24b70d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
17 deletions
+45
-17
nextjs-require-cache-hot-reloader.ts
...uild/webpack/plugins/nextjs-require-cache-hot-reloader.ts
+45
-17
No files found.
packages/next/build/webpack/plugins/nextjs-require-cache-hot-reloader.ts
View file @
b4b68c0b
import
{
Compiler
,
Plugin
}
from
'webpack'
import
{
Compiler
,
Plugin
,
version
}
from
'webpack'
import
{
realpathSync
}
from
'fs'
const
isWebpack5
=
parseInt
(
version
!
)
===
5
function
deleteCache
(
path
:
string
)
{
try
{
delete
require
.
cache
[
realpathSync
(
path
)]
...
...
@@ -11,30 +13,56 @@ function deleteCache(path: string) {
}
}
const
PLUGIN_NAME
=
'NextJsRequireCacheHotReloader'
// This plugin flushes require.cache after emitting the files. Providing 'hot reloading' of server files.
export
class
NextJsRequireCacheHotReloader
implements
Plugin
{
prevAssets
:
any
=
null
previousOutputPathsWebpack5
:
Set
<
string
>
=
new
Set
()
currentOutputPathsWebpack5
:
Set
<
string
>
=
new
Set
()
apply
(
compiler
:
Compiler
)
{
compiler
.
hooks
.
afterEmit
.
tapAsync
(
'NextJsRequireCacheHotReloader'
,
(
compilation
,
callback
)
=>
{
const
{
assets
}
=
compilation
if
(
this
.
prevAssets
)
{
for
(
const
f
of
Object
.
keys
(
assets
))
{
deleteCache
(
assets
[
f
].
existsAt
)
}
for
(
const
f
of
Object
.
keys
(
this
.
prevAssets
))
{
if
(
!
assets
[
f
])
{
deleteCache
(
this
.
prevAssets
[
f
].
existsAt
)
}
if
(
isWebpack5
)
{
// @ts-ignored Webpack has this hooks
compiler
.
hooks
.
assetEmitted
.
tap
(
PLUGIN_NAME
,
(
_file
:
any
,
{
targetPath
}:
any
)
=>
{
this
.
currentOutputPathsWebpack5
.
add
(
targetPath
)
deleteCache
(
targetPath
)
}
)
compiler
.
hooks
.
afterEmit
.
tap
(
PLUGIN_NAME
,
()
=>
{
for
(
const
path
of
this
.
previousOutputPathsWebpack5
)
{
if
(
!
this
.
currentOutputPathsWebpack5
.
has
(
path
))
{
deleteCache
(
path
)
}
}
this
.
prevAssets
=
assets
callback
()
this
.
previousOutputPathsWebpack5
=
new
Set
(
this
.
currentOutputPathsWebpack5
)
this
.
currentOutputPathsWebpack5
.
clear
()
})
return
}
compiler
.
hooks
.
afterEmit
.
tapAsync
(
PLUGIN_NAME
,
(
compilation
,
callback
)
=>
{
const
{
assets
}
=
compilation
if
(
this
.
prevAssets
)
{
for
(
const
f
of
Object
.
keys
(
assets
))
{
deleteCache
(
assets
[
f
].
existsAt
)
}
for
(
const
f
of
Object
.
keys
(
this
.
prevAssets
))
{
if
(
!
assets
[
f
])
{
deleteCache
(
this
.
prevAssets
[
f
].
existsAt
)
}
}
}
)
this
.
prevAssets
=
assets
callback
()
})
}
}
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