mirror of https://github.com/webpack/webpack.git
add resolveByProperty to cleverMerge
This commit is contained in:
parent
957013f557
commit
67d2e227f4
|
|
@ -8,9 +8,9 @@
|
|||
const Factory = require("enhanced-resolve").ResolverFactory;
|
||||
const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
|
||||
const {
|
||||
cleverMerge,
|
||||
cachedCleverMerge,
|
||||
removeOperations
|
||||
removeOperations,
|
||||
resolveByProperty
|
||||
} = require("./util/cleverMerge");
|
||||
|
||||
/** @typedef {import("enhanced-resolve").ResolveOptions} ResolveOptions */
|
||||
|
|
@ -34,12 +34,7 @@ const EMPTY_RESOLVE_OPTIONS = {};
|
|||
* @returns {ResolveOptions} merged options
|
||||
*/
|
||||
const convertToResolveOptions = resolveOptionsWithDepType => {
|
||||
const {
|
||||
dependencyType,
|
||||
byDependency,
|
||||
plugins,
|
||||
...remaining
|
||||
} = resolveOptionsWithDepType;
|
||||
const { dependencyType, plugins, ...remaining } = resolveOptionsWithDepType;
|
||||
|
||||
// check type compat
|
||||
/** @type {Partial<ResolveOptions>} */
|
||||
|
|
@ -60,16 +55,9 @@ const convertToResolveOptions = resolveOptionsWithDepType => {
|
|||
// These weird types validate that we checked all non-optional properties
|
||||
const options = /** @type {Partial<ResolveOptions> & Pick<ResolveOptions, "fileSystem">} */ (partialOptions);
|
||||
|
||||
if (!resolveOptionsWithDepType.byDependency) {
|
||||
return options;
|
||||
}
|
||||
|
||||
const usedDependencyType =
|
||||
dependencyType in byDependency ? `${dependencyType}` : "default";
|
||||
|
||||
const depDependentOptions = byDependency[usedDependencyType];
|
||||
if (!depDependentOptions) return options;
|
||||
return removeOperations(cleverMerge(options, depDependentOptions));
|
||||
return removeOperations(
|
||||
resolveByProperty(options, "byDependency", dependencyType)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -448,8 +448,39 @@ const removeOperations = obj => {
|
|||
return newObj;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object} obj the object
|
||||
* @param {string} byProperty the by description
|
||||
* @param {...any} values values
|
||||
* @returns {object} object with merged byProperty
|
||||
*/
|
||||
const resolveByProperty = (obj, byProperty, ...values) => {
|
||||
if (!(byProperty in obj)) {
|
||||
return obj;
|
||||
}
|
||||
const { [byProperty]: byValue, ...remaining } = obj;
|
||||
if (typeof byValue === "object") {
|
||||
const key = values[0];
|
||||
if (key in byValue) {
|
||||
return cleverMerge(remaining, byValue[key]);
|
||||
} else if ("default" in byValue) {
|
||||
return cleverMerge(remaining, byValue.default);
|
||||
} else {
|
||||
return remaining;
|
||||
}
|
||||
} else if (typeof byValue === "function") {
|
||||
const result = resolveByProperty(
|
||||
byValue.apply(null, values),
|
||||
byProperty,
|
||||
...values
|
||||
);
|
||||
return cleverMerge(remaining, result);
|
||||
}
|
||||
};
|
||||
|
||||
exports.cachedSetProperty = cachedSetProperty;
|
||||
exports.cachedCleverMerge = cachedCleverMerge;
|
||||
exports.cleverMerge = cleverMerge;
|
||||
exports.resolveByProperty = resolveByProperty;
|
||||
exports.removeOperations = removeOperations;
|
||||
exports.DELETE = DELETE;
|
||||
|
|
|
|||
Loading…
Reference in New Issue