Unverified Commit 30fe5fc1 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Compute Chunk Graph file hashes on emit (#6897)

* Compute hashes of files during emit

* Fix tests

* Remove test from old location

* Correct the target
parent 19c63517
......@@ -273,7 +273,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, debug =
!isServer && new ReactLoadablePlugin({
filename: REACT_LOADABLE_MANIFEST
}),
!isServer && new ChunkGraphPlugin(path.resolve(dir), { filename: CHUNK_GRAPH_MANIFEST }),
!isServer && __selectivePageBuilding && new ChunkGraphPlugin(path.resolve(dir), { filename: CHUNK_GRAPH_MANIFEST }),
...(dev ? (() => {
// Even though require.cache is server only we have to clear assets from both compilations
// This is because the client compilation generates the build manifest that's used on the server side
......
......@@ -3,6 +3,8 @@ import path from 'path'
import { EOL } from 'os'
import { parse } from 'querystring'
import { CLIENT_STATIC_FILES_RUNTIME_MAIN } from 'next-server/constants'
import fs from 'fs'
import { createHash } from 'crypto'
function getFiles(dir: string, modules: any[]): string[] {
if (!(modules && modules.length)) {
......@@ -57,13 +59,19 @@ export class ChunkGraphPlugin implements Plugin {
const { dir } = this
compiler.hooks.emit.tap('ChunkGraphPlugin', compilation => {
type StringDictionary = { [pageName: string]: string[] }
const manifest: { pages: StringDictionary; chunks: StringDictionary } = {
const manifest: {
pages: StringDictionary
chunks: StringDictionary
hashes: StringDictionary
} = {
pages: {},
chunks: {},
hashes: {},
}
let clientRuntime = [] as string[]
const pages: StringDictionary = {}
const allFiles = new Set()
compilation.chunks.forEach(chunk => {
if (!chunk.hasEntryModule()) {
......@@ -95,6 +103,8 @@ export class ChunkGraphPlugin implements Plugin {
.map(f => path.relative(dir, f))
.sort()
files.forEach(f => allFiles.add(f))
let pageName: string | undefined
if (chunk.entryModule && chunk.entryModule.loaders) {
const entryLoader = chunk.entryModule.loaders.find(
......@@ -130,6 +140,21 @@ export class ChunkGraphPlugin implements Plugin {
}
})
manifest.hashes = ([...allFiles] as string[]).sort().reduce(
(acc, cur) =>
Object.assign(
acc,
fs.existsSync(path.join(dir, cur))
? {
[cur]: createHash('sha1')
.update(fs.readFileSync(path.join(dir, cur)))
.digest('hex'),
}
: undefined
),
{}
)
const json = JSON.stringify(manifest, null, 2) + EOL
compilation.assets[this.filename] = {
source() {
......
module.exports = {
target: 'serverless',
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
}
}
export default () => (
<div>Hello World</div>
)
/* eslint-env jest */
/* global jasmine */
import { join, resolve, relative } from 'path'
import { existsSync, readFileSync } from 'fs'
import { CHUNK_GRAPH_MANIFEST } from 'next-server/constants'
import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Chunk Graph on Build', () => {
beforeAll(async () => {
await nextBuild(appDir, ['--experimental-page', '/'])
})
describe('Module collection', () => {
it('should build a chunk graph file', () => {
const cgf = join(__dirname, `/../.next/${CHUNK_GRAPH_MANIFEST}`)
expect(existsSync(cgf)).toBeTruthy()
expect(
JSON.parse(readFileSync(cgf, 'utf8')).pages['/'].includes(
relative(appDir, resolve(__dirname, '..', 'pages', 'index.js'))
)
)
})
})
})
/* eslint-env jest */
/* global jasmine */
import { join, resolve, relative } from 'path'
import { existsSync, readFileSync } from 'fs'
import { BUILD_ID_FILE, CHUNK_GRAPH_MANIFEST } from 'next-server/constants'
import { join } from 'path'
import { existsSync } from 'fs'
import { BUILD_ID_FILE } from 'next-server/constants'
import {
nextServer,
nextBuild,
......@@ -50,16 +50,4 @@ describe('Production Usage', () => {
).toBeFalsy()
})
})
describe('Module collection', () => {
it('should build a chunk graph file', () => {
const cgf = join(__dirname, `/../dist/${CHUNK_GRAPH_MANIFEST}`)
expect(existsSync(cgf)).toBeTruthy()
expect(
JSON.parse(readFileSync(cgf, 'utf8')).pages['/'].includes(
relative(appDir, resolve(__dirname, '..', 'pages', 'index.js'))
)
)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment