未验证 提交 74398d09 编写于 作者: J Joe Haddad 提交者: GitHub

Separate shared shuttle modules (#7287)

* Separate shared shuttle modules

* Correct types
上级 92cbe13b
...@@ -20,6 +20,7 @@ const fsWriteFile = promisify(fs.writeFile) ...@@ -20,6 +20,7 @@ const fsWriteFile = promisify(fs.writeFile)
const fsCopyFile = promisify(fs.copyFile) const fsCopyFile = promisify(fs.copyFile)
type ChunkGraphManifest = { type ChunkGraphManifest = {
sharedFiles: string[] | undefined
pages: { [page: string]: string[] } pages: { [page: string]: string[] }
pageChunks: { [page: string]: string[] } pageChunks: { [page: string]: string[] }
chunks: { [page: string]: string[] } chunks: { [page: string]: string[] }
...@@ -32,11 +33,12 @@ export class FlyingShuttle { ...@@ -32,11 +33,12 @@ export class FlyingShuttle {
private buildId: string private buildId: string
private pagesDirectory: string private pagesDirectory: string
private distDirectory: string private distDirectory: string
private cacheIdentifier: string private parentCacheIdentifier: string
private _shuttleBuildId: string | undefined private _shuttleBuildId: string | undefined
private _restoreSema = new Sema(1) private _restoreSema = new Sema(1)
private _recalledManifest: ChunkGraphManifest = { private _recalledManifest: ChunkGraphManifest = {
sharedFiles: [],
pages: {}, pages: {},
pageChunks: {}, pageChunks: {},
chunks: {}, chunks: {},
...@@ -65,7 +67,7 @@ export class FlyingShuttle { ...@@ -65,7 +67,7 @@ export class FlyingShuttle {
this.buildId = buildId this.buildId = buildId
this.pagesDirectory = pagesDirectory this.pagesDirectory = pagesDirectory
this.distDirectory = distDirectory this.distDirectory = distDirectory
this.cacheIdentifier = cacheIdentifier this.parentCacheIdentifier = cacheIdentifier
} }
hasShuttle = async () => { hasShuttle = async () => {
...@@ -100,9 +102,9 @@ export class FlyingShuttle { ...@@ -100,9 +102,9 @@ export class FlyingShuttle {
const manifestPath = path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST) const manifestPath = path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST)
const manifest = require(manifestPath) as ChunkGraphManifest const manifest = require(manifestPath) as ChunkGraphManifest
const { pages: pageFileDictionary, hashes } = manifest const { sharedFiles, pages: pageFileDictionary, hashes } = manifest
const pageNames = Object.keys(pageFileDictionary) const pageNames = Object.keys(pageFileDictionary)
const allFiles = new Set() const allFiles = new Set(sharedFiles)
pageNames.forEach(pageName => pageNames.forEach(pageName =>
pageFileDictionary[pageName].forEach(file => allFiles.add(file)) pageFileDictionary[pageName].forEach(file => allFiles.add(file))
) )
...@@ -118,23 +120,28 @@ export class FlyingShuttle { ...@@ -118,23 +120,28 @@ export class FlyingShuttle {
const hash = crypto const hash = crypto
.createHash('sha1') .createHash('sha1')
.update(this.cacheIdentifier) .update(this.parentCacheIdentifier)
.update(await fsReadFile(filePath)) .update(await fsReadFile(filePath))
.digest('hex') .digest('hex')
fileChanged.set(file, hash !== hashes[file]) fileChanged.set(file, hash !== hashes[file])
}) })
) )
const unchangedPages = pageNames const unchangedPages = (sharedFiles || [])
.filter( .map(f => fileChanged.get(f))
p => !pageFileDictionary[p].map(f => fileChanged.get(f)).some(Boolean) .some(Boolean)
) ? []
.filter( : pageNames
pageName => .filter(
pageName !== '/_app' && p =>
pageName !== '/_error' && !pageFileDictionary[p].map(f => fileChanged.get(f)).some(Boolean)
pageName !== '/_document' )
) .filter(
pageName =>
pageName !== '/_app' &&
pageName !== '/_error' &&
pageName !== '/_document'
)
if (unchangedPages.length) { if (unchangedPages.length) {
const u = unchangedPages.length const u = unchangedPages.length
...@@ -254,6 +261,8 @@ export class FlyingShuttle { ...@@ -254,6 +261,8 @@ export class FlyingShuttle {
) as ChunkGraphManifest ) as ChunkGraphManifest
const storeManifest: ChunkGraphManifest = { const storeManifest: ChunkGraphManifest = {
// Intentionally does not merge with the recalled manifest
sharedFiles: nextManifest.sharedFiles,
pages: Object.assign( pages: Object.assign(
{}, {},
this._recalledManifest.pages, this._recalledManifest.pages,
......
...@@ -8,10 +8,12 @@ import { Compiler, Plugin } from 'webpack' ...@@ -8,10 +8,12 @@ import { Compiler, Plugin } from 'webpack'
type StringDictionary = { [pageName: string]: string[] } type StringDictionary = { [pageName: string]: string[] }
const manifest: { const manifest: {
sharedFiles: string[]
pages: StringDictionary pages: StringDictionary
pageChunks: StringDictionary pageChunks: StringDictionary
chunks: StringDictionary chunks: StringDictionary
} = { } = {
sharedFiles: [],
pages: {}, pages: {},
pageChunks: {}, pageChunks: {},
chunks: {}, chunks: {},
...@@ -90,7 +92,7 @@ export function exportManifest({ ...@@ -90,7 +92,7 @@ export function exportManifest({
hashes: {} as { [pageName: string]: string }, hashes: {} as { [pageName: string]: string },
} }
const allFiles = new Set<string>() const allFiles = new Set<string>(manifest.sharedFiles)
for (const page of Object.keys(finalManifest.pages)) { for (const page of Object.keys(finalManifest.pages)) {
finalManifest.pages[page].forEach(f => allFiles.add(f)) finalManifest.pages[page].forEach(f => allFiles.add(f))
} }
...@@ -283,13 +285,13 @@ export class ChunkGraphPlugin implements Plugin { ...@@ -283,13 +285,13 @@ export class ChunkGraphPlugin implements Plugin {
.replace(/[.]js$/, `.${this.buildId}.js`) .replace(/[.]js$/, `.${this.buildId}.js`)
: name : name
manifest.sharedFiles = [
...new Set([...(manifest.sharedFiles || []), ...sharedFiles]),
].sort()
for (const page in pages) { for (const page in pages) {
manifest.pages[page] = [ manifest.pages[page] = [
...new Set([ ...new Set([...(manifest.pages[page] || []), ...pages[page]]),
...(manifest.pages[page] || []),
...pages[page],
...sharedFiles,
]),
].sort() ].sort()
// There's no chunks to save from serverless bundles // There's no chunks to save from serverless bundles
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册