Skip to content

Commit 74398d0

Browse files
authored
Separate shared shuttle modules (#7287)
* Separate shared shuttle modules * Correct types
1 parent 92cbe13 commit 74398d0

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

packages/next/build/flying-shuttle.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const fsWriteFile = promisify(fs.writeFile)
2020
const fsCopyFile = promisify(fs.copyFile)
2121

2222
type ChunkGraphManifest = {
23+
sharedFiles: string[] | undefined
2324
pages: { [page: string]: string[] }
2425
pageChunks: { [page: string]: string[] }
2526
chunks: { [page: string]: string[] }
@@ -32,11 +33,12 @@ export class FlyingShuttle {
3233
private buildId: string
3334
private pagesDirectory: string
3435
private distDirectory: string
35-
private cacheIdentifier: string
36+
private parentCacheIdentifier: string
3637

3738
private _shuttleBuildId: string | undefined
3839
private _restoreSema = new Sema(1)
3940
private _recalledManifest: ChunkGraphManifest = {
41+
sharedFiles: [],
4042
pages: {},
4143
pageChunks: {},
4244
chunks: {},
@@ -65,7 +67,7 @@ export class FlyingShuttle {
6567
this.buildId = buildId
6668
this.pagesDirectory = pagesDirectory
6769
this.distDirectory = distDirectory
68-
this.cacheIdentifier = cacheIdentifier
70+
this.parentCacheIdentifier = cacheIdentifier
6971
}
7072

7173
hasShuttle = async () => {
@@ -100,9 +102,9 @@ export class FlyingShuttle {
100102
const manifestPath = path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST)
101103
const manifest = require(manifestPath) as ChunkGraphManifest
102104

103-
const { pages: pageFileDictionary, hashes } = manifest
105+
const { sharedFiles, pages: pageFileDictionary, hashes } = manifest
104106
const pageNames = Object.keys(pageFileDictionary)
105-
const allFiles = new Set()
107+
const allFiles = new Set(sharedFiles)
106108
pageNames.forEach(pageName =>
107109
pageFileDictionary[pageName].forEach(file => allFiles.add(file))
108110
)
@@ -118,23 +120,28 @@ export class FlyingShuttle {
118120

119121
const hash = crypto
120122
.createHash('sha1')
121-
.update(this.cacheIdentifier)
123+
.update(this.parentCacheIdentifier)
122124
.update(await fsReadFile(filePath))
123125
.digest('hex')
124126
fileChanged.set(file, hash !== hashes[file])
125127
})
126128
)
127129

128-
const unchangedPages = pageNames
129-
.filter(
130-
p => !pageFileDictionary[p].map(f => fileChanged.get(f)).some(Boolean)
131-
)
132-
.filter(
133-
pageName =>
134-
pageName !== '/_app' &&
135-
pageName !== '/_error' &&
136-
pageName !== '/_document'
137-
)
130+
const unchangedPages = (sharedFiles || [])
131+
.map(f => fileChanged.get(f))
132+
.some(Boolean)
133+
? []
134+
: pageNames
135+
.filter(
136+
p =>
137+
!pageFileDictionary[p].map(f => fileChanged.get(f)).some(Boolean)
138+
)
139+
.filter(
140+
pageName =>
141+
pageName !== '/_app' &&
142+
pageName !== '/_error' &&
143+
pageName !== '/_document'
144+
)
138145

139146
if (unchangedPages.length) {
140147
const u = unchangedPages.length
@@ -254,6 +261,8 @@ export class FlyingShuttle {
254261
) as ChunkGraphManifest
255262

256263
const storeManifest: ChunkGraphManifest = {
264+
// Intentionally does not merge with the recalled manifest
265+
sharedFiles: nextManifest.sharedFiles,
257266
pages: Object.assign(
258267
{},
259268
this._recalledManifest.pages,

packages/next/build/webpack/plugins/chunk-graph-plugin.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { Compiler, Plugin } from 'webpack'
88

99
type StringDictionary = { [pageName: string]: string[] }
1010
const manifest: {
11+
sharedFiles: string[]
1112
pages: StringDictionary
1213
pageChunks: StringDictionary
1314
chunks: StringDictionary
1415
} = {
16+
sharedFiles: [],
1517
pages: {},
1618
pageChunks: {},
1719
chunks: {},
@@ -90,7 +92,7 @@ export function exportManifest({
9092
hashes: {} as { [pageName: string]: string },
9193
}
9294

93-
const allFiles = new Set<string>()
95+
const allFiles = new Set<string>(manifest.sharedFiles)
9496
for (const page of Object.keys(finalManifest.pages)) {
9597
finalManifest.pages[page].forEach(f => allFiles.add(f))
9698
}
@@ -283,13 +285,13 @@ export class ChunkGraphPlugin implements Plugin {
283285
.replace(/[.]js$/, `.${this.buildId}.js`)
284286
: name
285287

288+
manifest.sharedFiles = [
289+
...new Set([...(manifest.sharedFiles || []), ...sharedFiles]),
290+
].sort()
291+
286292
for (const page in pages) {
287293
manifest.pages[page] = [
288-
...new Set([
289-
...(manifest.pages[page] || []),
290-
...pages[page],
291-
...sharedFiles,
292-
]),
294+
...new Set([...(manifest.pages[page] || []), ...pages[page]]),
293295
].sort()
294296

295297
// There's no chunks to save from serverless bundles

0 commit comments

Comments
 (0)