Last active
October 12, 2023 22:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
plugins: [new EmitAllPlugin()] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment