Unverified Commit 2e32aa86 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Ensure correct key order (#6616)

* Ensure correct key order

* Fix bug

* Format this document while we're at it
parent 179d09e8
import { RawSource } from 'webpack-sources' import { RawSource } from 'webpack-sources'
import { BUILD_MANIFEST, ROUTE_NAME_REGEX, IS_BUNDLED_PAGE_REGEX, CLIENT_STATIC_FILES_RUNTIME_MAIN } from 'next-server/constants' import {
BUILD_MANIFEST,
ROUTE_NAME_REGEX,
IS_BUNDLED_PAGE_REGEX,
CLIENT_STATIC_FILES_RUNTIME_MAIN
} from 'next-server/constants'
// This plugin creates a build-manifest.json for all assets that are being output // This plugin creates a build-manifest.json for all assets that are being output
// It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production // It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
export default class BuildManifestPlugin { export default class BuildManifestPlugin {
apply (compiler) { apply (compiler) {
compiler.hooks.emit.tapAsync('NextJsBuildManifest', (compilation, callback) => { compiler.hooks.emit.tapAsync(
'NextJsBuildManifest',
(compilation, callback) => {
const { chunks } = compilation const { chunks } = compilation
const assetMap = { devFiles: [], pages: {} } const assetMap = { devFiles: [], pages: {} }
const mainJsChunk = chunks.find((c) => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN) const mainJsChunk = chunks.find(
const mainJsFiles = mainJsChunk && mainJsChunk.files.length > 0 ? mainJsChunk.files.filter((file) => /\.js$/.test(file)) : [] c => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN
)
const mainJsFiles =
mainJsChunk && mainJsChunk.files.length > 0
? mainJsChunk.files.filter(file => /\.js$/.test(file))
: []
for (const filePath of Object.keys(compilation.assets)) { for (const filePath of Object.keys(compilation.assets)) {
const path = filePath.replace(/\\/g, '/') const path = filePath.replace(/\\/g, '/')
...@@ -58,7 +70,10 @@ export default class BuildManifestPlugin { ...@@ -58,7 +70,10 @@ export default class BuildManifestPlugin {
} }
} }
assetMap.pages[`/${pagePath.replace(/\\/g, '/')}`] = [...filesForEntry, ...mainJsFiles] assetMap.pages[`/${pagePath.replace(/\\/g, '/')}`] = [
...filesForEntry,
...mainJsFiles
]
} }
if (typeof assetMap.pages['/index'] !== 'undefined') { if (typeof assetMap.pages['/index'] !== 'undefined') {
...@@ -67,10 +82,14 @@ export default class BuildManifestPlugin { ...@@ -67,10 +82,14 @@ export default class BuildManifestPlugin {
assetMap.pages = Object.keys(assetMap.pages) assetMap.pages = Object.keys(assetMap.pages)
.sort() .sort()
.reduce((a, c) => Object.assign(a, { [c]: assetMap.pages[c] }), {}) // eslint-disable-next-line
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {})
compilation.assets[BUILD_MANIFEST] = new RawSource(JSON.stringify(assetMap, null, 2)) compilation.assets[BUILD_MANIFEST] = new RawSource(
JSON.stringify(assetMap, null, 2)
)
callback() callback()
}) }
)
} }
} }
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