Skip to content

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

@KiriRea

Description

@KiriRea
  • Operating System: Windows 10
  • Node Version: 16.15.0
  • NPM Version: 8.5.5
  • webpack Version: 5.73.0
  • karma-webpack Version: 5.0.0

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.
webpack/webpack#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

  • script
karma start karma.conf.js
  • karma.config.js
var webpackConfig = require('webpack.config');
...
module.exports = {
    ...
    webpack: webpackConfig,
    ...
}
  • webpack.config.js
...
module.exports = {
    devtool: 'inline-source-map',
    mode: 'development',
    ...
    watch: false,
    ...
    cache: {
        type: 'filesystem',
        name: 'sample',

        buildDependencies: {
            config: [__filename],
        }
    }
}

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions