Skip to content

Instantly share code, notes, and snippets.

@iwyg
Created June 5, 2018 14:09
Show Gist options
  • Save iwyg/bca229c2464244f074c95ab2ad49d043 to your computer and use it in GitHub Desktop.
Save iwyg/bca229c2464244f074c95ab2ad49d043 to your computer and use it in GitHub Desktop.
Webpack manifest generator hook that includes dependent chunks
const getExtension = (file) => {
if (/\.css$/.test(file)) {
return 'css'
}
if (/\.js$/.test(file)) {
return 'js'
}
return 'asset'
}
const mapFiles = (chunk, files) => {
return chunk.files
.map(file => files.filter(f => f.isChunk && !f.isAsync && f.chunk.name === chunk.name).map(file => file.path))
.reduce((files, fileArray) => {
return [...files, ...fileArray.filter(f => !files.includes(f))]
}, [])
.reduce((files, file) => {
let ex = getExtension(file)
files[ex] = files[ex] || []
files[ex].push(file)
return files
}, {})
}
const mapChunks = (chunk, files) => {
return Array.from(
chunk.groupsIterable,
(group) => {
return group.chunks.map(chunk => ({name: chunk.name, files: mapFiles(chunk, files)}))
}
).reduce((list, entry) => {
return [...list, ...entry]
}, [])
}
module.exports = (seed, files) => {
return files.reduce((manifest, {name, path, isChunk, isInitial, chunk}) => {
let dependencies = []
let groups = []
if (isChunk) {
dependencies = mapChunks(chunk, files)
}
return {...manifest, [name]: {path, dependencies}}
}, seed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment