Skip to content

Commit

Permalink
feat: remove special handling of cache configuration
Browse files Browse the repository at this point in the history
In webpack verion 4 special code was necessary to make the cache work with grunts multi-target system. The cache plugin in version 4 was storing one global cache for all compilers it was attached to. In webpack 5 this is not neccessary anymore as the cache now creates a new storage for each compiler.
  • Loading branch information
danez committed Sep 3, 2024
1 parent 21d90dc commit 68d94b2
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 82 deletions.
18 changes: 1 addition & 17 deletions src/options/WebpackOptionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,7 @@ class WebpackOptionHelper extends OptionHelper {
const options = this.getOptions();

if (Array.isArray(options)) {
return options.map((opt) => this.filterOptions(opt));
}

return this.filterOptions(options);
}

filterOptions(options) {
if (!options.watch) {
// ensure cache is disabled in non watch mode, as we add our own CachePlugin to support
// multiple targets in one run with different caches
options.cache = false;
}

// ensure that when we send the cache option to webpack in watch mode it is not the function
// TODO: this is workaround and should be handled in a more generic way
if (typeof options.cache === "function") {
options.cache = options.cache(options);
return options.map((opt) => this.filterGruntOptions(opt));
}

return this.filterGruntOptions(options);
Expand Down
36 changes: 0 additions & 36 deletions src/options/__tests__/WebpackOptionHelper.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
import WebpackOptionHelper from "../WebpackOptionHelper";

describe("WebpackOptionHelper", () => {
test("cache stays enabled in watch mode", () => {
const options = { watch: true, cache: true };
const helper = new WebpackOptionHelper();

const result = helper.filterOptions(options);

expect(result.cache).toBe(true);
});

test("cache is enabled in watch mode by default", () => {
const options = { watch: true, cache: () => true };
const helper = new WebpackOptionHelper();

const result = helper.filterOptions(options);

expect(result.cache).toBe(true);
});

test("cache is disabled in normal mode by default", () => {
const options = { watch: false, cache: () => true };
const helper = new WebpackOptionHelper();

const result = helper.filterOptions(options);

expect(result.cache).toBe(false);
});

test("cache is disabled in normal mode", () => {
const options = { watch: false, cache: true };
const helper = new WebpackOptionHelper();

const result = helper.filterOptions(options);

expect(result.cache).toBe(false);
});

test("watch options is part of webpack options", () => {
const options = {
watch: true,
Expand Down
6 changes: 0 additions & 6 deletions src/options/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ const webpackOptions = {
cachedAssets: false,
colors: true,
},
cache: (options) => {
// if watch enabled also default to cache true
return Array.isArray(options)
? options.some((option) => option.watch)
: !!options.watch;
},
};

const webpackDevServerOptions = {
Expand Down
18 changes: 0 additions & 18 deletions src/plugins/CachePluginFactory.js

This file was deleted.

5 changes: 0 additions & 5 deletions tasks/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
const webpack = require("webpack");
const pkg = require("../package.json");
const OptionHelper = require("../src/options/WebpackOptionHelper");
const CachePluginFactory = require("../src/plugins/CachePluginFactory");
const ProgressPluginFactory = require("../src/plugins/ProgressPluginFactory");

module.exports = (grunt) => {
const cachePluginFactory = new CachePluginFactory();
const processPluginFactory = new ProgressPluginFactory(grunt);

grunt.registerTask(
Expand Down Expand Up @@ -59,9 +57,6 @@ module.exports = (grunt) => {

const compiler = webpack(webpackOptions);

if (opts.cache) {
cachePluginFactory.addPlugin(target, compiler);
}
if (opts.progress) {
processPluginFactory.addPlugin(compiler, webpackOptions);
}
Expand Down

0 comments on commit 68d94b2

Please sign in to comment.