Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate shared shuttle modules #7287

Merged
merged 2 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 24 additions & 15 deletions packages/next/build/flying-shuttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const fsWriteFile = promisify(fs.writeFile)
const fsCopyFile = promisify(fs.copyFile)

type ChunkGraphManifest = {
sharedFiles: string[] | undefined
pages: { [page: string]: string[] }
pageChunks: { [page: string]: string[] }
chunks: { [page: string]: string[] }
Expand All @@ -32,11 +33,12 @@ export class FlyingShuttle {
private buildId: string
private pagesDirectory: string
private distDirectory: string
private cacheIdentifier: string
private parentCacheIdentifier: string

private _shuttleBuildId: string | undefined
private _restoreSema = new Sema(1)
private _recalledManifest: ChunkGraphManifest = {
sharedFiles: [],
pages: {},
pageChunks: {},
chunks: {},
Expand Down Expand Up @@ -65,7 +67,7 @@ export class FlyingShuttle {
this.buildId = buildId
this.pagesDirectory = pagesDirectory
this.distDirectory = distDirectory
this.cacheIdentifier = cacheIdentifier
this.parentCacheIdentifier = cacheIdentifier
}

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

const { pages: pageFileDictionary, hashes } = manifest
const { sharedFiles, pages: pageFileDictionary, hashes } = manifest
const pageNames = Object.keys(pageFileDictionary)
const allFiles = new Set()
const allFiles = new Set(sharedFiles)
pageNames.forEach(pageName =>
pageFileDictionary[pageName].forEach(file => allFiles.add(file))
)
Expand All @@ -118,23 +120,28 @@ export class FlyingShuttle {

const hash = crypto
.createHash('sha1')
.update(this.cacheIdentifier)
.update(this.parentCacheIdentifier)
.update(await fsReadFile(filePath))
.digest('hex')
fileChanged.set(file, hash !== hashes[file])
})
)

const unchangedPages = pageNames
.filter(
p => !pageFileDictionary[p].map(f => fileChanged.get(f)).some(Boolean)
)
.filter(
pageName =>
pageName !== '/_app' &&
pageName !== '/_error' &&
pageName !== '/_document'
)
const unchangedPages = (sharedFiles || [])
.map(f => fileChanged.get(f))
.some(Boolean)
? []
: pageNames
.filter(
p =>
!pageFileDictionary[p].map(f => fileChanged.get(f)).some(Boolean)
)
.filter(
pageName =>
pageName !== '/_app' &&
pageName !== '/_error' &&
pageName !== '/_document'
)

if (unchangedPages.length) {
const u = unchangedPages.length
Expand Down Expand Up @@ -254,6 +261,8 @@ export class FlyingShuttle {
) as ChunkGraphManifest

const storeManifest: ChunkGraphManifest = {
// Intentionally does not merge with the recalled manifest
sharedFiles: nextManifest.sharedFiles,
pages: Object.assign(
{},
this._recalledManifest.pages,
Expand Down
14 changes: 8 additions & 6 deletions packages/next/build/webpack/plugins/chunk-graph-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { Compiler, Plugin } from 'webpack'

type StringDictionary = { [pageName: string]: string[] }
const manifest: {
sharedFiles: string[]
pages: StringDictionary
pageChunks: StringDictionary
chunks: StringDictionary
} = {
sharedFiles: [],
pages: {},
pageChunks: {},
chunks: {},
Expand Down Expand Up @@ -90,7 +92,7 @@ export function exportManifest({
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)) {
finalManifest.pages[page].forEach(f => allFiles.add(f))
}
Expand Down Expand Up @@ -283,13 +285,13 @@ export class ChunkGraphPlugin implements Plugin {
.replace(/[.]js$/, `.${this.buildId}.js`)
: name

manifest.sharedFiles = [
...new Set([...(manifest.sharedFiles || []), ...sharedFiles]),
].sort()

for (const page in pages) {
manifest.pages[page] = [
...new Set([
...(manifest.pages[page] || []),
...pages[page],
...sharedFiles,
]),
...new Set([...(manifest.pages[page] || []), ...pages[page]]),
].sort()

// There's no chunks to save from serverless bundles
Expand Down