rsnext/packages/next/build/webpack/plugins/hashed-chunk-ids-plugin.ts
Joe Haddad 6902afbd3f
Stabilize Chunk ID generation (#6663)
* Add a new field to webpack types

* Revert "Add a new field to webpack types"

This reverts commit d35fa02207fbfd0085da0fc56aac42c4ff7c34c9.

* Add HashedChunkIdsPlugin to make consistent chunk ids

* Revert "Revert "Add a new field to webpack types""

This reverts commit 338219049e1432038f90c91928b010bbb1267999.

* Make it optional

* Remove record ids

* Revert "Remove record ids"

This reverts commit 15c22dbcda72466c382397c91d02295620f62326.
2019-03-14 21:38:57 -04:00

27 lines
719 B
TypeScript

import { Compiler, Plugin } from 'webpack'
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)
}
}
})
})
}
}