Skip to content

Instantly share code, notes, and snippets.

@LukasBombach
Created February 14, 2024 15:20
Show Gist options
  • Select an option

  • Save LukasBombach/0987a711fe1e11ff6c200b47bc244a90 to your computer and use it in GitHub Desktop.

Select an option

Save LukasBombach/0987a711fe1e11ff6c200b47bc244a90 to your computer and use it in GitHub Desktop.

Revisions

  1. LukasBombach created this gist Feb 14, 2024.
    21 changes: 21 additions & 0 deletions CompileToStringPlugin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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)}`;
    });
    });
    }
    }
    }