Commit 6104a71a authored by Tim Neutkens's avatar Tim Neutkens Committed by Joe Haddad

Make react-loadable-manifest only have modules that are part of dynamic chunks (#6622)

* Make react-loadable-manifest only have modules that are part of dynamic chunks

* Add manifest sorting
parent 87bd1c3b
...@@ -28,15 +28,28 @@ function buildManifest (compiler, compilation) { ...@@ -28,15 +28,28 @@ function buildManifest (compiler, compilation) {
let manifest = {} let manifest = {}
compilation.chunks.forEach(chunk => { compilation.chunks.forEach(chunk => {
chunk.files.forEach(file => { // If chunk is not an entry point skip them
for (const module of chunk.modulesIterable) { if (chunk.hasEntryModule()) {
let id = module.id const dynamicChunks = chunk.getAllAsyncChunks()
let name = typeof module.libIdent === 'function' ? module.libIdent({ context }) : null if (dynamicChunks.size !== 0) {
for (const dynamicChunk of dynamicChunks) {
for (const file of dynamicChunk.files) {
// If it doesn't end in `.js` Next.js can't handle it right now. // If it doesn't end in `.js` Next.js can't handle it right now.
if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) { if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) {
return continue
} }
let publicPath = url.resolve(compilation.outputOptions.publicPath || '', file)
let publicPath = url.resolve(
compilation.outputOptions.publicPath || '',
file
)
for (const module of dynamicChunk.modulesIterable) {
let id = module.id
let name =
typeof module.libIdent === 'function'
? module.libIdent({ context })
: null
let currentModule = module let currentModule = module
if (module.constructor.name === 'ConcatenatedModule') { if (module.constructor.name === 'ConcatenatedModule') {
...@@ -46,11 +59,33 @@ function buildManifest (compiler, compilation) { ...@@ -46,11 +59,33 @@ function buildManifest (compiler, compilation) {
manifest[currentModule.rawRequest] = [] manifest[currentModule.rawRequest] = []
} }
manifest[currentModule.rawRequest].push({ id, name, file, publicPath }) // Avoid duplicate files
if (
manifest[currentModule.rawRequest].some(
item => item.file === file
)
) {
continue
} }
manifest[currentModule.rawRequest].push({
id,
name,
file,
publicPath
}) })
}
}
}
}
}
}) })
manifest = Object.keys(manifest)
.sort()
// eslint-disable-next-line
.reduce((a, c) => ((a[c] = manifest[c]), a), {})
return manifest return manifest
} }
...@@ -60,7 +95,9 @@ export class ReactLoadablePlugin { ...@@ -60,7 +95,9 @@ export class ReactLoadablePlugin {
} }
apply (compiler) { apply (compiler) {
compiler.hooks.emit.tapAsync('ReactLoadableManifest', (compilation, callback) => { compiler.hooks.emit.tapAsync(
'ReactLoadableManifest',
(compilation, callback) => {
const manifest = buildManifest(compiler, compilation) const manifest = buildManifest(compiler, compilation)
var json = JSON.stringify(manifest, null, 2) var json = JSON.stringify(manifest, null, 2)
compilation.assets[this.filename] = { compilation.assets[this.filename] = {
...@@ -72,6 +109,7 @@ export class ReactLoadablePlugin { ...@@ -72,6 +109,7 @@ export class ReactLoadablePlugin {
} }
} }
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