Skip to content

Instantly share code, notes, and snippets.

@DrewML
Last active October 12, 2023 22:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DrewML/af4f7dd957c38c8f0447ec9f531ea802 to your computer and use it in GitHub Desktop.
Save DrewML/af4f7dd957c38c8f0447ec9f531ea802 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