Skip to content

Commit

Permalink
Revert "Merge pull request #15332 from webpack/fix/context-resolve-is…
Browse files Browse the repository at this point in the history
…sue-11335"

This reverts commit 89c92bf, reversing
changes made to a28af55.
  • Loading branch information
sokra committed Feb 17, 2022
1 parent 9d21401 commit dd53923
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 408 deletions.
103 changes: 24 additions & 79 deletions lib/ContextModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const makeSerializable = require("./util/makeSerializable");

/**
* @typedef {Object} ContextModuleOptionsExtras
* @property {string|string[]} resource
* @property {string} resource
* @property {string=} resourceQuery
* @property {string=} resourceFragment
* @property {TODO} resolveOptions
Expand Down Expand Up @@ -92,36 +92,23 @@ class ContextModule extends Module {
* @param {ContextModuleOptions} options options object
*/
constructor(resolveDependencies, options) {
if (!options || typeof options.resource === "string") {
const parsed = parseResource(
options ? /** @type {string} */ (options.resource) : ""
);
const resource = parsed.path;
const resourceQuery = (options && options.resourceQuery) || parsed.query;
const resourceFragment =
(options && options.resourceFragment) || parsed.fragment;

super("javascript/dynamic", resource);
/** @type {ContextModuleOptions} */
this.options = {
...options,
resource,
resourceQuery,
resourceFragment
};
} else {
super("javascript/dynamic");
/** @type {ContextModuleOptions} */
this.options = {
...options,
resource: options.resource,
resourceQuery: options.resourceQuery || "",
resourceFragment: options.resourceFragment || ""
};
}
const parsed = parseResource(options ? options.resource : "");
const resource = parsed.path;
const resourceQuery = (options && options.resourceQuery) || parsed.query;
const resourceFragment =
(options && options.resourceFragment) || parsed.fragment;

super("javascript/dynamic", resource);

// Info from Factory
this.resolveDependencies = resolveDependencies;
/** @type {ContextModuleOptions} */
this.options = {
...options,
resource,
resourceQuery,
resourceFragment
};
if (options && options.resolveOptions !== undefined) {
this.resolveOptions = options.resolveOptions;
}
Expand Down Expand Up @@ -168,11 +155,7 @@ class ContextModule extends Module {
}

_createIdentifier() {
let identifier =
this.context ||
(typeof this.options.resource === "string"
? this.options.resource
: this.options.resource.join("|"));
let identifier = this.context;
if (this.options.resourceQuery) {
identifier += `|${this.options.resourceQuery}`;
}
Expand Down Expand Up @@ -237,16 +220,7 @@ class ContextModule extends Module {
* @returns {string} a user readable identifier of the module
*/
readableIdentifier(requestShortener) {
let identifier;
if (this.context) {
identifier = requestShortener.shorten(this.context) + "/";
} else if (typeof this.options.resource === "string") {
identifier = requestShortener.shorten(this.options.resource) + "/";
} else {
identifier = this.options.resource
.map(r => requestShortener.shorten(r) + "/")
.join(" ");
}
let identifier = requestShortener.shorten(this.context) + "/";
if (this.options.resourceQuery) {
identifier += ` ${this.options.resourceQuery}`;
}
Expand Down Expand Up @@ -296,30 +270,11 @@ class ContextModule extends Module {
* @returns {string | null} an identifier for library inclusion
*/
libIdent(options) {
let identifier;

if (this.context) {
identifier = contextify(
options.context,
this.context,
options.associatedObjectForCache
);
} else if (typeof this.options.resource === "string") {
identifier = contextify(
options.context,
this.options.resource,
options.associatedObjectForCache
);
} else {
const arr = [];
for (const res of this.options.resource) {
arr.push(
contextify(options.context, res, options.associatedObjectForCache)
);
}
identifier = arr.join(" ");
}

let identifier = contextify(
options.context,
this.context,
options.associatedObjectForCache
);
if (this.layer) identifier = `(${this.layer})/${identifier}`;
if (this.options.mode) {
identifier += ` ${this.options.mode}`;
Expand Down Expand Up @@ -487,11 +442,7 @@ class ContextModule extends Module {
compilation.fileSystemInfo.createSnapshot(
startTime,
null,
this.context
? [this.context]
: typeof this.options.resource === "string"
? [this.options.resource]
: this.options.resource,
[this.context],
null,
SNAPSHOT_OPTIONS,
(err, snapshot) => {
Expand All @@ -515,13 +466,7 @@ class ContextModule extends Module {
missingDependencies,
buildDependencies
) {
if (this.context) {
contextDependencies.add(this.context);
} else if (typeof this.options.resource === "string") {
contextDependencies.add(this.options.resource);
} else {
for (const res of this.options.resource) contextDependencies.add(res);
}
contextDependencies.add(this.context);
}

/**
Expand Down
81 changes: 21 additions & 60 deletions lib/ContextModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,18 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
asyncLib.parallel(
[
callback => {
const results = [];
const yield_ = obj => results.push(obj);

contextResolver.resolve(
{},
context,
resource,
{
fileDependencies,
missingDependencies,
contextDependencies,
yield: yield_
contextDependencies
},
err => {
(err, result) => {
if (err) return callback(err);
callback(null, results);
callback(null, result);
}
);
},
Expand Down Expand Up @@ -217,20 +213,15 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
contextDependencies
});
}
const [contextResult, loaderResult] = result;

this.hooks.afterResolve.callAsync(
{
addon:
loadersPrefix +
loaderResult.join("!") +
(loaderResult.length > 0 ? "!" : ""),
resource:
contextResult.length > 1
? contextResult.map(r => r.path)
: contextResult[0].path,
result[1].join("!") +
(result[1].length > 0 ? "!" : ""),
resource: result[0],
resolveDependencies: this.resolveDependencies.bind(this),
resourceQuery: contextResult[0].query,
resourceFragment: contextResult[0].fragment,
...beforeResolveResult
},
(err, result) => {
Expand Down Expand Up @@ -287,28 +278,26 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
} = options;
if (!regExp || !resource) return callback(null, []);

let severalContexts = false;
const addDirectoryChecked = (ctx, directory, visited, callback) => {
const addDirectoryChecked = (directory, visited, callback) => {
fs.realpath(directory, (err, realPath) => {
if (err) return callback(err);
if (visited.has(realPath)) return callback(null, []);
let recursionStack;
addDirectory(
ctx,
directory,
(_, dir, callback) => {
(dir, callback) => {
if (recursionStack === undefined) {
recursionStack = new Set(visited);
recursionStack.add(realPath);
}
addDirectoryChecked(ctx, dir, recursionStack, callback);
addDirectoryChecked(dir, recursionStack, callback);
},
callback
);
});
};

const addDirectory = (ctx, directory, addSubDirectory, callback) => {
const addDirectory = (directory, addSubDirectory, callback) => {
fs.readdir(directory, (err, files) => {
if (err) return callback(err);
const processedFiles = cmf.hooks.contextModuleFiles.call(
Expand All @@ -335,15 +324,16 @@ module.exports = class ContextModuleFactory extends ModuleFactory {

if (stat.isDirectory()) {
if (!recursive) return callback();
addSubDirectory(ctx, subResource, callback);
addSubDirectory(subResource, callback);
} else if (
stat.isFile() &&
(!include || subResource.match(include))
) {
const obj = {
context: ctx,
context: resource,
request:
"." + subResource.substr(ctx.length).replace(/\\/g, "/")
"." +
subResource.substr(resource.length).replace(/\\/g, "/")
};

this.hooks.alternativeRequests.callAsync(
Expand All @@ -354,11 +344,8 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
alternatives = alternatives
.filter(obj => regExp.test(obj.request))
.map(obj => {
const request = severalContexts
? join(fs, obj.context, obj.request)
: obj.request;
const dep = new ContextElementDependency(
request + resourceQuery + resourceFragment,
obj.request + resourceQuery + resourceFragment,
obj.request,
typePrefix,
category,
Expand Down Expand Up @@ -395,38 +382,12 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
});
};

const addSubDirectory = (ctx, dir, callback) =>
addDirectory(ctx, dir, addSubDirectory, callback);

const visitResource = (resource, callback) => {
if (typeof fs.realpath === "function") {
addDirectoryChecked(resource, resource, new Set(), callback);
} else {
addDirectory(resource, resource, addSubDirectory, callback);
}
};

if (typeof resource === "string") {
visitResource(resource, callback);
if (typeof fs.realpath === "function") {
addDirectoryChecked(resource, new Set(), callback);
} else {
severalContexts = true;
asyncLib.map(resource, visitResource, (err, result) => {
if (err) return callback(err);

// result dependencies should have unique userRequest
// ordered by resolve result
const temp = new Set();
const res = [];
for (let i = 0; i < result.length; i++) {
const inner = result[i];
for (const el of inner) {
if (temp.has(el.userRequest)) continue;
res.push(el);
temp.add(el.userRequest);
}
}
callback(null, res);
});
const addSubDirectory = (dir, callback) =>
addDirectory(dir, addSubDirectory, callback);
addDirectory(resource, addSubDirectory, callback);
}
}
};
Loading

0 comments on commit dd53923

Please sign in to comment.