feat: refactoring project
This commit is contained in:
133
node_modules/webpack/lib/ContextModuleFactory.js
generated
vendored
133
node_modules/webpack/lib/ContextModuleFactory.js
generated
vendored
@@ -22,8 +22,14 @@ const { join } = require("./util/fs");
|
||||
/** @typedef {import("./ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */
|
||||
/** @typedef {import("./ResolverFactory")} ResolverFactory */
|
||||
/** @typedef {import("./dependencies/ContextDependency")} ContextDependency */
|
||||
/** @template T @typedef {import("./util/deprecation").FakeHook<T>} FakeHook<T> */
|
||||
/** @typedef {import("enhanced-resolve").ResolveRequest} ResolveRequest */
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import("./util/deprecation").FakeHook<T>} FakeHook<T>
|
||||
*/
|
||||
/** @typedef {import("./util/fs").IStats} IStats */
|
||||
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
||||
/** @typedef {{ context: string, request: string }} ContextAlternativeRequest */
|
||||
|
||||
const EMPTY_RESOLVE_OPTIONS = {};
|
||||
|
||||
@@ -33,7 +39,7 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
*/
|
||||
constructor(resolverFactory) {
|
||||
super();
|
||||
/** @type {AsyncSeriesWaterfallHook<[TODO[], ContextModuleOptions]>} */
|
||||
/** @type {AsyncSeriesWaterfallHook<[ContextAlternativeRequest[], ContextModuleOptions]>} */
|
||||
const alternativeRequests = new AsyncSeriesWaterfallHook([
|
||||
"modules",
|
||||
"options"
|
||||
@@ -45,27 +51,27 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
afterResolve: new AsyncSeriesWaterfallHook(["data"]),
|
||||
/** @type {SyncWaterfallHook<[string[]]>} */
|
||||
contextModuleFiles: new SyncWaterfallHook(["files"]),
|
||||
/** @type {FakeHook<Pick<AsyncSeriesWaterfallHook<[TODO[]]>, "tap" | "tapAsync" | "tapPromise" | "name">>} */
|
||||
/** @type {FakeHook<Pick<AsyncSeriesWaterfallHook<[ContextAlternativeRequest[]]>, "tap" | "tapAsync" | "tapPromise" | "name">>} */
|
||||
alternatives: createFakeHook(
|
||||
{
|
||||
name: "alternatives",
|
||||
/** @type {AsyncSeriesWaterfallHook<[TODO[]]>["intercept"]} */
|
||||
/** @type {AsyncSeriesWaterfallHook<[ContextAlternativeRequest[]]>["intercept"]} */
|
||||
intercept: interceptor => {
|
||||
throw new Error(
|
||||
"Intercepting fake hook ContextModuleFactory.hooks.alternatives is not possible, use ContextModuleFactory.hooks.alternativeRequests instead"
|
||||
);
|
||||
},
|
||||
/** @type {AsyncSeriesWaterfallHook<[TODO[]]>["tap"]} */
|
||||
/** @type {AsyncSeriesWaterfallHook<[ContextAlternativeRequest[]]>["tap"]} */
|
||||
tap: (options, fn) => {
|
||||
alternativeRequests.tap(options, fn);
|
||||
},
|
||||
/** @type {AsyncSeriesWaterfallHook<[TODO[]]>["tapAsync"]} */
|
||||
/** @type {AsyncSeriesWaterfallHook<[ContextAlternativeRequest[]]>["tapAsync"]} */
|
||||
tapAsync: (options, fn) => {
|
||||
alternativeRequests.tapAsync(options, (items, _options, callback) =>
|
||||
fn(items, callback)
|
||||
);
|
||||
},
|
||||
/** @type {AsyncSeriesWaterfallHook<[TODO[]]>["tapPromise"]} */
|
||||
/** @type {AsyncSeriesWaterfallHook<[ContextAlternativeRequest[]]>["tapPromise"]} */
|
||||
tapPromise: (options, fn) => {
|
||||
alternativeRequests.tapPromise(options, fn);
|
||||
}
|
||||
@@ -93,8 +99,8 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
const contextDependencies = new LazySet();
|
||||
this.hooks.beforeResolve.callAsync(
|
||||
{
|
||||
context: context,
|
||||
dependencies: dependencies,
|
||||
context,
|
||||
dependencies,
|
||||
layer: data.contextInfo.issuerLayer,
|
||||
resolveOptions,
|
||||
fileDependencies,
|
||||
@@ -124,9 +130,9 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
const request = beforeResolveResult.request;
|
||||
const resolveOptions = beforeResolveResult.resolveOptions;
|
||||
|
||||
let loaders,
|
||||
resource,
|
||||
loadersPrefix = "";
|
||||
let loaders;
|
||||
let resource;
|
||||
let loadersPrefix = "";
|
||||
const idx = request.lastIndexOf("!");
|
||||
if (idx >= 0) {
|
||||
let loadersRequest = request.slice(0, idx + 1);
|
||||
@@ -142,11 +148,7 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
.slice(i)
|
||||
.replace(/!+$/, "")
|
||||
.replace(/!!+/g, "!");
|
||||
if (loadersRequest === "") {
|
||||
loaders = [];
|
||||
} else {
|
||||
loaders = loadersRequest.split("!");
|
||||
}
|
||||
loaders = loadersRequest === "" ? [] : loadersRequest.split("!");
|
||||
resource = request.slice(idx + 1);
|
||||
} else {
|
||||
loaders = [];
|
||||
@@ -168,8 +170,14 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
asyncLib.parallel(
|
||||
[
|
||||
callback => {
|
||||
const results = [];
|
||||
const yield_ = obj => results.push(obj);
|
||||
const results = /** @type ResolveRequest[] */ ([]);
|
||||
/**
|
||||
* @param {ResolveRequest} obj obj
|
||||
* @returns {void}
|
||||
*/
|
||||
const yield_ = obj => {
|
||||
results.push(obj);
|
||||
};
|
||||
|
||||
contextResolver.resolve(
|
||||
{},
|
||||
@@ -202,7 +210,7 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
},
|
||||
(err, result) => {
|
||||
if (err) return callback(err);
|
||||
callback(null, result);
|
||||
callback(null, /** @type {string} */ (result));
|
||||
}
|
||||
);
|
||||
},
|
||||
@@ -218,7 +226,8 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
contextDependencies
|
||||
});
|
||||
}
|
||||
let [contextResult, loaderResult] = result;
|
||||
let [contextResult, loaderResult] =
|
||||
/** @type {[ResolveRequest[], string[]]} */ (result);
|
||||
if (contextResult.length > 1) {
|
||||
const first = contextResult[0];
|
||||
contextResult = contextResult.filter(r => r.path);
|
||||
@@ -294,10 +303,19 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
} = options;
|
||||
if (!regExp || !resource) return callback(null, []);
|
||||
|
||||
/**
|
||||
* @param {string} ctx context
|
||||
* @param {string} directory directory
|
||||
* @param {Set<string>} visited visited
|
||||
* @param {ResolveDependenciesCallback} callback callback
|
||||
*/
|
||||
const addDirectoryChecked = (ctx, directory, visited, callback) => {
|
||||
fs.realpath(directory, (err, realPath) => {
|
||||
/** @type {NonNullable<InputFileSystem["realpath"]>} */
|
||||
(fs.realpath)(directory, (err, _realPath) => {
|
||||
if (err) return callback(err);
|
||||
const realPath = /** @type {string} */ (_realPath);
|
||||
if (visited.has(realPath)) return callback(null, []);
|
||||
/** @type {Set<string> | undefined} */
|
||||
let recursionStack;
|
||||
addDirectory(
|
||||
ctx,
|
||||
@@ -314,6 +332,12 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} ctx context
|
||||
* @param {string} directory directory
|
||||
* @param {function(string, string, function(): void): void} addSubDirectory addSubDirectoryFn
|
||||
* @param {ResolveDependenciesCallback} callback callback
|
||||
*/
|
||||
const addDirectory = (ctx, directory, addSubDirectory, callback) => {
|
||||
fs.readdir(directory, (err, files) => {
|
||||
if (err) return callback(err);
|
||||
@@ -328,17 +352,18 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
const subResource = join(fs, directory, segment);
|
||||
|
||||
if (!exclude || !subResource.match(exclude)) {
|
||||
fs.stat(subResource, (err, stat) => {
|
||||
fs.stat(subResource, (err, _stat) => {
|
||||
if (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
// ENOENT is ok here because the file may have been deleted between
|
||||
// the readdir and stat calls.
|
||||
return callback();
|
||||
} else {
|
||||
return callback(err);
|
||||
}
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
const stat = /** @type {IStats} */ (_stat);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
if (!recursive) return callback();
|
||||
addSubDirectory(ctx, subResource, callback);
|
||||
@@ -346,10 +371,10 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
stat.isFile() &&
|
||||
(!include || subResource.match(include))
|
||||
) {
|
||||
/** @type {{ context: string, request: string }} */
|
||||
const obj = {
|
||||
context: ctx,
|
||||
request:
|
||||
"." + subResource.slice(ctx.length).replace(/\\/g, "/")
|
||||
request: `.${subResource.slice(ctx.length).replace(/\\/g, "/")}`
|
||||
};
|
||||
|
||||
this.hooks.alternativeRequests.callAsync(
|
||||
@@ -357,22 +382,29 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
options,
|
||||
(err, alternatives) => {
|
||||
if (err) return callback(err);
|
||||
alternatives = alternatives
|
||||
.filter(obj => regExp.test(obj.request))
|
||||
.map(obj => {
|
||||
const dep = new ContextElementDependency(
|
||||
`${obj.request}${resourceQuery}${resourceFragment}`,
|
||||
obj.request,
|
||||
typePrefix,
|
||||
category,
|
||||
referencedExports,
|
||||
obj.context,
|
||||
attributes
|
||||
);
|
||||
dep.optional = true;
|
||||
return dep;
|
||||
});
|
||||
callback(null, alternatives);
|
||||
callback(
|
||||
null,
|
||||
/** @type {ContextAlternativeRequest[]} */
|
||||
(alternatives)
|
||||
.filter(obj =>
|
||||
regExp.test(/** @type {string} */ (obj.request))
|
||||
)
|
||||
.map(obj => {
|
||||
const dep = new ContextElementDependency(
|
||||
`${obj.request}${resourceQuery}${resourceFragment}`,
|
||||
obj.request,
|
||||
typePrefix,
|
||||
/** @type {string} */
|
||||
(category),
|
||||
referencedExports,
|
||||
/** @type {TODO} */
|
||||
(obj.context),
|
||||
attributes
|
||||
);
|
||||
dep.optional = true;
|
||||
return dep;
|
||||
})
|
||||
);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
@@ -400,9 +432,19 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} ctx context
|
||||
* @param {string} dir dir
|
||||
* @param {ResolveDependenciesCallback} callback callback
|
||||
* @returns {void}
|
||||
*/
|
||||
const addSubDirectory = (ctx, dir, callback) =>
|
||||
addDirectory(ctx, dir, addSubDirectory, callback);
|
||||
|
||||
/**
|
||||
* @param {string} resource resource
|
||||
* @param {ResolveDependenciesCallback} callback callback
|
||||
*/
|
||||
const visitResource = (resource, callback) => {
|
||||
if (typeof fs.realpath === "function") {
|
||||
addDirectoryChecked(resource, resource, new Set(), callback);
|
||||
@@ -414,12 +456,15 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
|
||||
if (typeof resource === "string") {
|
||||
visitResource(resource, callback);
|
||||
} else {
|
||||
asyncLib.map(resource, visitResource, (err, result) => {
|
||||
asyncLib.map(resource, visitResource, (err, _result) => {
|
||||
if (err) return callback(err);
|
||||
const result = /** @type {ContextElementDependency[][]} */ (_result);
|
||||
|
||||
// result dependencies should have unique userRequest
|
||||
// ordered by resolve result
|
||||
/** @type {Set<string>} */
|
||||
const temp = new Set();
|
||||
/** @type {ContextElementDependency[]} */
|
||||
const res = [];
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
const inner = result[i];
|
||||
|
||||
Reference in New Issue
Block a user