Skip to content

Instantly share code, notes, and snippets.

@a-ignatov-parc
Created February 13, 2017 16:46
Show Gist options
  • Save a-ignatov-parc/64d6fe5dbd08b9ddb931544d8c1c72e7 to your computer and use it in GitHub Desktop.
Save a-ignatov-parc/64d6fe5dbd08b9ddb931544d8c1c72e7 to your computer and use it in GitHub Desktop.
const defaults = {
filename: 'versions.json',
};
class HashDictionaryPlugin {
constructor(options) {
this.options = Object.assign({}, defaults, options);
}
apply(compiler) {
compiler.plugin('emit', (compilation, done) => {
const {
context,
outputPath,
} = compilation.compiler;
const stats = compilation
.getStats()
.toJson({
source: false,
modules: false,
publicPath: true,
});
const files = stats.chunks
.reduce((result, chunk) => {
const {hash} = chunk;
const files = chunk.files.map(basename => {
const filepath = path.join(outputPath, basename);
return {
hash,
basename,
cwd: context,
path: filepath,
dirname: outputPath,
relative: path.relative(context, filepath),
};
});
return result.concat(files);
}, [])
.reduce((result, record) => {
result[record.relative] = record;
return result;
}, {});
const contents = JSON.stringify(files);
compilation.assets[this.options.filename] = {
source() {
return contents;
},
size() {
return contents.length;
},
};
done();
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment