Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KiriRea opened this issue Jul 14, 2022 · 0 comments

Comments

@KiriRea
Copy link

KiriRea commented Jul 14, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant