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 Factory = require("enhanced-resolve").ResolverFactory;
|
||||||
const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
|
const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
|
||||||
const {
|
const {
|
||||||
cleverMerge,
|
|
||||||
cachedCleverMerge,
|
cachedCleverMerge,
|
||||||
removeOperations
|
removeOperations,
|
||||||
|
resolveByProperty
|
||||||
} = require("./util/cleverMerge");
|
} = require("./util/cleverMerge");
|
||||||
|
|
||||||
/** @typedef {import("enhanced-resolve").ResolveOptions} ResolveOptions */
|
/** @typedef {import("enhanced-resolve").ResolveOptions} ResolveOptions */
|
||||||
|
|
@ -34,12 +34,7 @@ const EMPTY_RESOLVE_OPTIONS = {};
|
||||||
* @returns {ResolveOptions} merged options
|
* @returns {ResolveOptions} merged options
|
||||||
*/
|
*/
|
||||||
const convertToResolveOptions = resolveOptionsWithDepType => {
|
const convertToResolveOptions = resolveOptionsWithDepType => {
|
||||||
const {
|
const { dependencyType, plugins, ...remaining } = resolveOptionsWithDepType;
|
||||||
dependencyType,
|
|
||||||
byDependency,
|
|
||||||
plugins,
|
|
||||||
...remaining
|
|
||||||
} = resolveOptionsWithDepType;
|
|
||||||
|
|
||||||
// check type compat
|
// check type compat
|
||||||
/** @type {Partial<ResolveOptions>} */
|
/** @type {Partial<ResolveOptions>} */
|
||||||
|
|
@ -60,16 +55,9 @@ const convertToResolveOptions = resolveOptionsWithDepType => {
|
||||||
// These weird types validate that we checked all non-optional properties
|
// These weird types validate that we checked all non-optional properties
|
||||||
const options = /** @type {Partial<ResolveOptions> & Pick<ResolveOptions, "fileSystem">} */ (partialOptions);
|
const options = /** @type {Partial<ResolveOptions> & Pick<ResolveOptions, "fileSystem">} */ (partialOptions);
|
||||||
|
|
||||||
if (!resolveOptionsWithDepType.byDependency) {
|
return removeOperations(
|
||||||
return options;
|
resolveByProperty(options, "byDependency", dependencyType)
|
||||||
}
|
);
|
||||||
|
|
||||||
const usedDependencyType =
|
|
||||||
dependencyType in byDependency ? `${dependencyType}` : "default";
|
|
||||||
|
|
||||||
const depDependentOptions = byDependency[usedDependencyType];
|
|
||||||
if (!depDependentOptions) return options;
|
|
||||||
return removeOperations(cleverMerge(options, depDependentOptions));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -448,8 +448,39 @@ const removeOperations = obj => {
|
||||||
return newObj;
|
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.cachedSetProperty = cachedSetProperty;
|
||||||
exports.cachedCleverMerge = cachedCleverMerge;
|
exports.cachedCleverMerge = cachedCleverMerge;
|
||||||
exports.cleverMerge = cleverMerge;
|
exports.cleverMerge = cleverMerge;
|
||||||
|
exports.resolveByProperty = resolveByProperty;
|
||||||
exports.removeOperations = removeOperations;
|
exports.removeOperations = removeOperations;
|
||||||
exports.DELETE = DELETE;
|
exports.DELETE = DELETE;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue