Created
November 10, 2020 08:14
-
-
Save rosylilly/59baf4e58647693a868eac0999c2fc4d to your computer and use it in GitHub Desktop.
WebPack 5 ready manifest plugin.
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
import { Compilation, Compiler, sources } from 'webpack'; | |
interface Options { | |
filename: string; | |
} | |
interface Manifest { | |
[key: string]: string[]; | |
} | |
export class ManifestPlugin { | |
private options: Options; | |
constructor(options: Partial<Options> = {}) { | |
this.options = { | |
filename: 'manifest.json', | |
...options, | |
}; | |
} | |
public apply(compiler: Compiler) { | |
let manifest: Manifest = {}; | |
compiler.hooks.compilation.tap('ManifestPlugin', (compilation) => { | |
compilation.hooks.processAssets.tap({ | |
name: 'ManifestPlugin', | |
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, | |
}, (assets) => { | |
const stats = compilation.getStats(); | |
for (const chunk of stats.compilation.chunks) { | |
manifest[chunk.name] = Array.from(chunk.files.values()); | |
} | |
assets[this.options.filename] = new sources.RawSource(JSON.stringify(manifest), true); | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment