Created
June 5, 2018 14:09
-
-
Save iwyg/bca229c2464244f074c95ab2ad49d043 to your computer and use it in GitHub Desktop.
Webpack manifest generator hook that includes dependent chunks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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