const webpack = require("webpack");
const { createFsFromVolume, Volume } = require("memfs");
const DemoPlugin = require("./demo-plugin");

// The compiler helper accepts filenames should be in the output
// so it's possible to assert the output easily.
function compile(config, filenames = []) {
  return new Promise((resolve, reject) => {
    const compiler = webpack(config);

    compiler.outputFileSystem = createFsFromVolume(new Volume());
    const memfs = compiler.outputFileSystem;

    compiler.run((err, stats) => {
      if (err) {
        return reject(err);
      }

      // Now only errors are captured from stats.
      // It's possible to capture more to assert.
      if (stats.hasErrors()) {
        return reject(stats.toString("errors-only"));
      }

      const ret = {};

      filenames.forEach((filename) => {
        // The assumption is that webpack outputs behind ./dist.
        ret[filename] = memfs.readFileSync(`./dist/${filename}`, {
          encoding: "utf-8",
        });
      });

      return resolve(ret);
    });
  });
}

async function test() {
  const result = await compile(
    {
      entry: "./test-entry.js",
      plugins: [new DemoPlugin({ name: "demo" })],
    },
    ["demo"]
  );

  console.log(result);
}

test();

@@ -89,53 +89,8 @@
 s%22);
-%0Aconst DemoPlugin = require(%22./demo-plugin%22);
 %0A%0A//
@@ -326,17 +326,16 @@
 onfig);%0A
-%0A
     comp
@@ -743,17 +743,16 @@
 t = %7B%7D;%0A
-%0A
       fi
@@ -966,17 +966,16 @@
     %7D);%0A
-%0A
       re
@@ -1044,18 +1044,20 @@
 cons
-t result =
+ole.log(%0A   
  awa
@@ -1067,21 +1067,16 @@
 compile(
-%0A    
 %7B%0A      
@@ -1109,101 +1109,27 @@
     
-  plugins: %5Bnew DemoPlugin(%7B name: %22demo%22 %7D)%5D,%0A    %7D,%0A    %5B%22demo%22%5D%0A  );%0A%0A  console.log(result
+%7D),%0A    %5B%22demo%22%5D%0A  
 );%0A%7D

Update plugin test