codymikol / karma-webpack

Karma webpack Middleware
MIT License
830 stars 221 forks source link

Webpack file cache isn't generated when webacpack watch option is false/nothing #537

Open KiriRea opened 2 years ago

KiriRea commented 2 years ago

Expected Behavior

It generates webpack file cache regardless of webpack watch option(true/false) when execute karma test.

Actual Behavior

I want to generate webpack file cache when I execute karma test. https://webpack.js.org/configuration/cache/

When I don't write webpack watch option(or write "false" to watch option) on webpack config, webapack file cache is not generated. After investigate this problem, I found below issue, and this issue says that call compiler.close() when it generates webpack file cache. https://github.com/webpack/webpack/issues/15411 ->you need to call compiler.close() in callback to store cache.

However "karma-webpack/controller.js" doesn't call compiler.close() when watch option is false/nothing. Therefore, it looks like this causes webpack can't generate file cache.

  setupExitHandler(compiler) {
    this.karmaEmitter.once('exit', (done) => {
      compiler.close(() => {
        console.log('Webpack stopped watching.');
        done();
      });
    });
  }

  ...

  _bundle() {
    this.isActive = true;

    return new Promise((resolve) => {
      if (this.webpackOptions.watch === true) {
        console.log('Webpack starts watching...');
        this.compiler = webpack(this.webpackOptions, (err, stats) =>
          this.handleBuildResult(err, stats, resolve)
        );

        this.setupExitHandler(this.compiler);
      } else {
        this.compiler = webpack(this.webpackOptions).run((err, stats) =>
          this.handleBuildResult(err, stats, resolve)
        );
      }
    });
  }

why it doesn't call compiler.close() if watch option is false/nothing?

Code

How Do We Reproduce?

Write watch option(false) and cache config to webpack.config.js like above code. Then execute karma(karma start karma.conf.js).