refactor: improve chunk name aesthetics

This commit is contained in:
Evan You
2018-07-26 17:12:36 -04:00
parent 2a60169fc1
commit 625d541715
+12 -3
View File
@@ -74,15 +74,24 @@ module.exports = (api, options) => {
})
// keep chunk ids stable so async chunks have consistent hash (#1916)
const seen = new Set()
const nameLength = 4
webpackConfig
.plugin('named-chunks')
.use(require('webpack/lib/NamedChunksPlugin'), [chunk => {
if (chunk.name) {
return chunk.name
}
return `chunk-` + Array.from(chunk.modulesIterable, m => {
return m.id
}).join('_')
const modules = Array.from(chunk.modulesIterable)
if (modules.length > 1) {
const hash = require('hash-sum')
const joinedHash = hash(modules.map(m => m.id).join('_'))
let len = nameLength
while (seen.has(joinedHash.substr(0, len))) len++
return `chunk-${joinedHash.substr(0, len)}`
} else {
return modules[0].id
}
}])
}