Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Forked from marcneander/LoadableWebpackPlugin.js
Created August 13, 2019 20:49
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 PatrickJS/10f7b59bb444fc97ab85ca51600405b5 to your computer and use it in GitHub Desktop.
Save PatrickJS/10f7b59bb444fc97ab85ca51600405b5 to your computer and use it in GitHub Desktop.
/* eslint-disable no-param-reassign */
const path = require('path');
const fse = require('fs-extra');
class LoadablePlugin {
constructor({ filename = 'loadable-stats.json', writeToFileEmit = false } = {}) {
this.opts = { filename, writeToFileEmit };
}
apply(compiler) {
// Add a custom output.jsonpFunction: __LOADABLE_LOADED_CHUNKS__
compiler.options.output.jsonpFunction = '__LOADABLE_LOADED_CHUNKS__';
compiler.hooks.emit.tap('@loadable/webpack-plugin', hookCompiler => {
const stats = hookCompiler.getStats().toJson({
hash: true,
publicPath: true,
assets: true,
chunks: false,
modules: false,
source: false,
errorDetails: false,
timings: false
});
const result = JSON.stringify(stats, null, 2);
hookCompiler.assets[this.opts.filename] = {
source() {
return result;
},
size() {
return result.length;
}
};
if (this.opts.writeToFileEmit) {
fse.outputFileSync(path.resolve(compiler.options.output.path, this.opts.filename), result);
}
});
}
}
module.exports = LoadablePlugin;
module.exports.default = LoadablePlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment