diff --git a/lib/ContextModule.js b/lib/ContextModule.js index 297b1de3c43..100eda5188b 100644 --- a/lib/ContextModule.js +++ b/lib/ContextModule.js @@ -61,7 +61,7 @@ const makeSerializable = require("./util/makeSerializable"); /** * @typedef {Object} ContextModuleOptionsExtras - * @property {string|string[]} resource + * @property {false|string|string[]} resource * @property {string=} resourceQuery * @property {string=} resourceFragment * @property {TODO} resolveOptions @@ -170,8 +170,9 @@ class ContextModule extends Module { _createIdentifier() { let identifier = this.context || - (typeof this.options.resource === "string" - ? this.options.resource + (typeof this.options.resource === "string" || + this.options.resource === false + ? `${this.options.resource}` : this.options.resource.join("|")); if (this.options.resourceQuery) { identifier += `|${this.options.resourceQuery}`; @@ -240,8 +241,11 @@ class ContextModule extends Module { let identifier; if (this.context) { identifier = requestShortener.shorten(this.context) + "/"; - } else if (typeof this.options.resource === "string") { - identifier = requestShortener.shorten(this.options.resource) + "/"; + } else if ( + typeof this.options.resource === "string" || + this.options.resource === false + ) { + identifier = requestShortener.shorten(`${this.options.resource}`) + "/"; } else { identifier = this.options.resource .map(r => requestShortener.shorten(r) + "/") @@ -310,6 +314,8 @@ class ContextModule extends Module { this.options.resource, options.associatedObjectForCache ); + } else if (this.options.resource === false) { + identifier = "false"; } else { const arr = []; for (const res of this.options.resource) { @@ -484,6 +490,8 @@ class ContextModule extends Module { ); return; } + if (!this.context || !this.options.resource) return callback(); + compilation.fileSystemInfo.createSnapshot( startTime, null, @@ -491,7 +499,7 @@ class ContextModule extends Module { ? [this.context] : typeof this.options.resource === "string" ? [this.options.resource] - : this.options.resource, + : /** @type {string[]} */ (this.options.resource), null, SNAPSHOT_OPTIONS, (err, snapshot) => { @@ -519,6 +527,8 @@ class ContextModule extends Module { contextDependencies.add(this.context); } else if (typeof this.options.resource === "string") { contextDependencies.add(this.options.resource); + } else if (this.options.resource === false) { + return; } else { for (const res of this.options.resource) contextDependencies.add(res); } diff --git a/lib/cache/ResolverCachePlugin.js b/lib/cache/ResolverCachePlugin.js index 7af5e848f0c..f53626b63d0 100644 --- a/lib/cache/ResolverCachePlugin.js +++ b/lib/cache/ResolverCachePlugin.js @@ -129,8 +129,10 @@ class ResolverCachePlugin { contextDependencies: new LazySet() }; let yieldResult; + let withYield = false; if (typeof newResolveContext.yield === "function") { yieldResult = []; + withYield = true; newResolveContext.yield = obj => yieldResult.push(obj); } const propagate = key => { @@ -160,7 +162,10 @@ class ResolverCachePlugin { snapshotOptions, (err, snapshot) => { if (err) return callback(err); - const resolveResult = result || yieldResult; + const resolveResult = withYield ? yieldResult : result; + // since we intercept resolve hook + // we still can get result in callback + if (withYield && result) yieldResult.push(result); if (!snapshot) { if (resolveResult) return callback(null, resolveResult); return callback(); @@ -242,11 +247,15 @@ class ResolverCachePlugin { yields = undefined; callbacks = false; } else { - for (let i = 0; i < callbacks.length; i++) { - const cb = callbacks[i]; - const yield_ = yields[i]; - if (result) for (const r of result) yield_(r); - cb(null, null); + if (err) { + for (const cb of callbacks) cb(err); + } else { + for (let i = 0; i < callbacks.length; i++) { + const cb = callbacks[i]; + const yield_ = yields[i]; + if (result) for (const r of result) yield_(r); + cb(null, null); + } } activeRequestsWithYield.delete(identifier); yields = undefined; diff --git a/test/configCases/resolve/empty-context-module/index.js b/test/configCases/resolve/empty-context-module/index.js new file mode 100644 index 00000000000..0679d569bd5 --- /dev/null +++ b/test/configCases/resolve/empty-context-module/index.js @@ -0,0 +1,7 @@ +const id = () => Math.random(); + +it("should compile", () => { + expect(/* webpackMode: "lazy" */ import(`foo/${id()}`)).rejects.toBeTruthy(); + expect(/* webpackMode: "lazy" */ import(`foo/${id()}`)).rejects.toBeTruthy(); + expect(/* webpackMode: "lazy" */ import(`foo/${id()}`)).rejects.toBeTruthy(); +}); diff --git a/test/configCases/resolve/empty-context-module/webpack.config.js b/test/configCases/resolve/empty-context-module/webpack.config.js new file mode 100644 index 00000000000..bfefc9c737a --- /dev/null +++ b/test/configCases/resolve/empty-context-module/webpack.config.js @@ -0,0 +1,20 @@ +/** @type {import("../../../../").Configuration[]} */ +module.exports = [ + { + cache: true, + resolve: { + alias: { + foo: false + }, + unsafeCache: true + } + }, + { + resolve: { + alias: { + foo: false + }, + unsafeCache: true + } + } +];