Skip to content

Instantly share code, notes, and snippets.

@rosylilly
Created November 10, 2020 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosylilly/59baf4e58647693a868eac0999c2fc4d to your computer and use it in GitHub Desktop.
Save rosylilly/59baf4e58647693a868eac0999c2fc4d to your computer and use it in GitHub Desktop.
WebPack 5 ready manifest plugin.
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