Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Forked from DrewML/emit-all-plugin.js
Created February 28, 2022 23:20
Show Gist options
  • Select an option

  • Save PatrickJS/acf839822079be8b80bc607a24b3db12 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickJS/acf839822079be8b80bc607a24b3db12 to your computer and use it in GitHub Desktop.
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