Skip to content

Instantly share code, notes, and snippets.

@LukasBombach
Created February 14, 2024 15:20
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 LukasBombach/0987a711fe1e11ff6c200b47bc244a90 to your computer and use it in GitHub Desktop.
Save LukasBombach/0987a711fe1e11ff6c200b47bc244a90 to your computer and use it in GitHub Desktop.
class CompileToStringPlugin {
constructor() {
this.PLUGIN_NAME = this.constructor.name;
}
apply(compiler) {
if (!this.options.isServer) {
compiler.hooks.normalModuleFactory.tap(this.PLUGIN_NAME, nmf => {
nmf.hooks.beforeResolve.tap(this.PLUGIN_NAME, result => {
// how can I use webpack here to compile the file result.request
// with its dependencies to and get the resulting source
// so that I can return the sourcecode as export
const compiledSourceCode = compileAndBundle(result.request);
return `module.exports = ${JSON.stringify(compiledSourceCode)}`;
});
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment