2019-06-24 15:48:26 +02:00
|
|
|
import { Compiler, Plugin } from 'webpack'
|
2019-03-15 02:38:57 +01:00
|
|
|
import { createHash } from 'crypto'
|
|
|
|
|
|
|
|
export class HashedChunkIdsPlugin implements Plugin {
|
|
|
|
buildId: string
|
|
|
|
|
|
|
|
constructor(buildId: string) {
|
|
|
|
this.buildId = buildId
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(compiler: Compiler) {
|
|
|
|
compiler.hooks.compilation.tap('HashedChunkIdsPlugin', compilation => {
|
|
|
|
compilation.hooks.beforeChunkIds.tap('HashedChunkIdsPlugin', chunks => {
|
|
|
|
for (const chunk of chunks) {
|
|
|
|
if (chunk.id === null && chunk.name) {
|
|
|
|
const id = chunk.name.replace(this.buildId, '')
|
|
|
|
|
|
|
|
chunk.id = createHash('md4')
|
|
|
|
.update(id)
|
|
|
|
.digest('hex')
|
|
|
|
.substr(0, 4)
|
2019-03-26 17:30:31 +01:00
|
|
|
continue
|
2019-03-15 02:38:57 +01:00
|
|
|
}
|
2019-03-26 17:30:31 +01:00
|
|
|
|
|
|
|
const ids = [...chunk.modulesIterable]
|
|
|
|
.map(m => m.id)
|
|
|
|
.sort() as string[]
|
|
|
|
|
|
|
|
const h = createHash('md4')
|
|
|
|
ids.forEach(id => h.update(id))
|
|
|
|
chunk.id = h.digest('hex').substr(0, 4)
|
2019-03-15 02:38:57 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|