2018-04-12 07:47:42 +00:00
|
|
|
import { RawSource } from 'webpack-sources'
|
2018-10-01 22:55:31 +00:00
|
|
|
import {BUILD_MANIFEST, ROUTE_NAME_REGEX, IS_BUNDLED_PAGE_REGEX, CLIENT_STATIC_FILES_RUNTIME_MAIN} from 'next-server/constants'
|
2018-04-12 07:47:42 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
export default class BuildManifestPlugin {
|
2018-11-21 15:04:37 +00:00
|
|
|
apply (compiler) {
|
2018-07-24 09:24:40 +00:00
|
|
|
compiler.hooks.emit.tapAsync('NextJsBuildManifest', (compilation, callback) => {
|
2018-04-12 07:47:42 +00:00
|
|
|
const {chunks} = compilation
|
2018-08-15 19:42:56 +00:00
|
|
|
const assetMap = {devFiles: [], pages: {}}
|
2018-04-12 07:47:42 +00:00
|
|
|
|
2018-07-27 17:29:25 +00:00
|
|
|
const mainJsChunk = chunks.find((c) => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN)
|
2018-07-24 09:24:40 +00:00
|
|
|
const mainJsFiles = mainJsChunk && mainJsChunk.files.length > 0 ? mainJsChunk.files.filter((file) => /\.js$/.test(file)) : []
|
|
|
|
|
2018-08-13 22:09:05 +00:00
|
|
|
for (const filePath of Object.keys(compilation.assets)) {
|
2018-08-15 23:24:56 +00:00
|
|
|
const path = filePath.replace(/\\/g, '/')
|
2018-09-02 23:40:21 +00:00
|
|
|
if (/^static\/development\/dll\//.test(path)) {
|
2018-08-15 23:24:56 +00:00
|
|
|
assetMap.devFiles.push(path)
|
2018-08-13 22:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
// compilation.entrypoints is a Map object, so iterating over it 0 is the key and 1 is the value
|
|
|
|
for (const [, entrypoint] of compilation.entrypoints.entries()) {
|
|
|
|
const result = ROUTE_NAME_REGEX.exec(entrypoint.name)
|
|
|
|
if (!result) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const pagePath = result[1]
|
|
|
|
|
|
|
|
if (!pagePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const filesForEntry = []
|
|
|
|
for (const chunk of entrypoint.chunks) {
|
2018-08-13 22:09:05 +00:00
|
|
|
// If there's no name or no files
|
2018-07-24 09:24:40 +00:00
|
|
|
if (!chunk.name || !chunk.files) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const file of chunk.files) {
|
2018-07-30 13:48:02 +00:00
|
|
|
if (/\.map$/.test(file) || /\.hot-update\.js$/.test(file)) {
|
2018-07-24 09:24:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-07-30 13:48:02 +00:00
|
|
|
// Only `.js` and `.css` files are added for now. In the future we can also handle other file types.
|
|
|
|
if (!/\.js$/.test(file) && !/\.css$/.test(file)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// The page bundles are manually added to _document.js as they need extra properties
|
2018-07-24 09:24:40 +00:00
|
|
|
if (IS_BUNDLED_PAGE_REGEX.exec(file)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filesForEntry.push(file.replace(/\\/g, '/'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assetMap.pages[`/${pagePath.replace(/\\/g, '/')}`] = [...filesForEntry, ...mainJsFiles]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof assetMap.pages['/index'] !== 'undefined') {
|
|
|
|
assetMap.pages['/'] = assetMap.pages['/index']
|
|
|
|
}
|
|
|
|
|
|
|
|
compilation.assets[BUILD_MANIFEST] = new RawSource(JSON.stringify(assetMap, null, 2))
|
2018-04-12 07:47:42 +00:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|