Skip to content

Commit 3b35fd4

Browse files
committed
use module.unsafeCache only for node_modules
remove unsafe resolve cache remainings
1 parent 4ed5622 commit 3b35fd4

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

lib/NormalModuleFactory.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ class NormalModuleFactory {
7878
});
7979
this.resolverFactory = resolverFactory;
8080
this.ruleSet = new RuleSet(options.defaultRules.concat(options.rules));
81+
this.unsafeCache = !!options.unsafeCache;
8182
this.cachePredicate =
8283
typeof options.unsafeCache === "function"
8384
? options.unsafeCache
84-
: Boolean.bind(null, options.unsafeCache);
85+
: () => true;
8586
this.context = context || "";
8687
this.parserCache = Object.create(null);
8788
this.generatorCache = Object.create(null);
@@ -333,8 +334,10 @@ class NormalModuleFactory {
333334

334335
create(data, callback) {
335336
const dependencies = data.dependencies;
336-
const cacheEntry = dependencyCache.get(dependencies[0]);
337-
if (cacheEntry) return callback(null, cacheEntry);
337+
if (this.unsafeCache) {
338+
const cacheEntry = dependencyCache.get(dependencies[0]);
339+
if (cacheEntry) return callback(null, cacheEntry);
340+
}
338341
const context = data.context || this.context;
339342
const resolveOptions = data.resolveOptions || EMPTY_RESOLVE_OPTIONS;
340343
const request = dependencies[0].request;
@@ -361,7 +364,7 @@ class NormalModuleFactory {
361364
factory(result, (err, module) => {
362365
if (err) return callback(err);
363366

364-
if (module && this.cachePredicate(module)) {
367+
if (this.unsafeCache && module && this.cachePredicate(module)) {
365368
for (const d of dependencies) {
366369
dependencyCache.set(d, module);
367370
}

lib/WebpackOptionsDefaulter.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"use strict";
77

88
const path = require("path");
9-
109
const OptionsDefaulter = require("./OptionsDefaulter");
1110
const Template = require("./Template");
1211

12+
const NODE_MODULES_REGEXP = /[\\/]node_modules[\\/]/i;
13+
1314
const isProductionLikeMode = options => {
1415
return options.mode === "production" || !options.mode;
1516
};
@@ -78,7 +79,14 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
7879
this.set("module.wrappedContextCritical", false);
7980
this.set("module.strictExportPresence", false);
8081
this.set("module.strictThisContextOnImports", false);
81-
this.set("module.unsafeCache", "make", options => !!options.cache);
82+
this.set("module.unsafeCache", "make", options => {
83+
if (options.cache) {
84+
return module => {
85+
const name = module.nameForCondition();
86+
return name && NODE_MODULES_REGEXP.test(name);
87+
};
88+
}
89+
});
8290
this.set("module.rules", []);
8391
this.set("module.defaultRules", "make", options => [
8492
{
@@ -273,7 +281,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
273281
});
274282
this.set("optimization.splitChunks.cacheGroups.defaultVendors", {
275283
automaticNamePrefix: "vendors",
276-
test: /[\\/]node_modules[\\/]/,
284+
test: NODE_MODULES_REGEXP,
277285
priority: -10
278286
});
279287
this.set("optimization.runtimeChunk", "call", value => {
@@ -360,24 +368,11 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
360368
return ["module", "main"];
361369
}
362370
});
363-
this.set("resolve.cacheWithContext", "make", options => {
364-
return (
365-
Array.isArray(options.resolve.plugins) &&
366-
options.resolve.plugins.length > 0
367-
);
368-
});
369371

370372
this.set("resolveLoader", "call", value => Object.assign({}, value));
371-
this.set("resolveLoader.unsafeCache", true);
372373
this.set("resolveLoader.mainFields", ["loader", "main"]);
373374
this.set("resolveLoader.extensions", [".js", ".json"]);
374375
this.set("resolveLoader.mainFiles", ["index"]);
375-
this.set("resolveLoader.cacheWithContext", "make", options => {
376-
return (
377-
Array.isArray(options.resolveLoader.plugins) &&
378-
options.resolveLoader.plugins.length > 0
379-
);
380-
});
381376
}
382377
}
383378

0 commit comments

Comments
 (0)