Skip to content

Instantly share code, notes, and snippets.

@DrewML DrewML/emit-all-plugin.js
Last active Aug 5, 2018

Embed
What would you like to do?
Output every file in a webpack build to the specified dist directory in the webpack config. Each file is output after having each loader run against it, but before the webpack module wrapper is added.
const path = require('path');
module.exports = class EmitAllPlugin {
constructor(opts = {}) {
this.ignorePattern = opts.ignorePattern || /node_modules/;
}
shouldIgnore(path) {
return this.ignorePattern.test(path);
}
apply(compiler) {
compiler.plugin('after-compile', (compilation, cb) => {
const { modules } = compilation;
modules.forEach(mod => {
const absolutePath = mod.resource;
if (this.shouldIgnore(absolutePath)) return;
// Used for vendor chunk
if (mod.constructor.name === 'MultiModule') return;
const source = mod._source._value;
const projectRoot = compiler.context;
const out = compiler.options.output.path;
const dest = path.join(
out,
absolutePath.replace(projectRoot, '')
);
compiler.outputFileSystem.mkdirp(path.dirname(dest), err => {
if (err) throw err;
compiler.outputFileSystem.writeFile(dest, source, err => {
if (err) throw err;
});
});
});
cb();
});
}
};
{
plugins: [new EmitAllPlugin()]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.