package.lib.ResolverFactory.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webpack Show documentation
Show all versions of webpack Show documentation
Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.
The newest version!
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const Factory = require("enhanced-resolve").ResolverFactory;
const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
const {
cachedCleverMerge,
removeOperations,
resolveByProperty
} = require("./util/cleverMerge");
/** @typedef {import("enhanced-resolve").ResolveContext} ResolveContext */
/** @typedef {import("enhanced-resolve").ResolveOptions} ResolveOptions */
/** @typedef {import("enhanced-resolve").ResolveRequest} ResolveRequest */
/** @typedef {import("enhanced-resolve").Resolver} Resolver */
/** @typedef {import("../declarations/WebpackOptions").ResolveOptions} WebpackResolveOptions */
/** @typedef {import("../declarations/WebpackOptions").ResolvePluginInstance} ResolvePluginInstance */
/** @typedef {WebpackResolveOptions & {dependencyType?: string, resolveToContext?: boolean }} ResolveOptionsWithDependencyType */
/**
* @typedef {object} WithOptions
* @property {function(Partial): ResolverWithOptions} withOptions create a resolver with additional/different options
*/
/** @typedef {Resolver & WithOptions} ResolverWithOptions */
// need to be hoisted on module level for caching identity
const EMPTY_RESOLVE_OPTIONS = {};
/**
* @param {ResolveOptionsWithDependencyType} resolveOptionsWithDepType enhanced options
* @returns {ResolveOptions} merged options
*/
const convertToResolveOptions = resolveOptionsWithDepType => {
const { dependencyType, plugins, ...remaining } = resolveOptionsWithDepType;
// check type compat
/** @type {Partial} */
const partialOptions = {
...remaining,
plugins:
plugins &&
/** @type {ResolvePluginInstance[]} */ (
plugins.filter(item => item !== "...")
)
};
if (!partialOptions.fileSystem) {
throw new Error(
"fileSystem is missing in resolveOptions, but it's required for enhanced-resolve"
);
}
// These weird types validate that we checked all non-optional properties
const options =
/** @type {Partial & Pick} */ (
partialOptions
);
return removeOperations(
resolveByProperty(options, "byDependency", dependencyType),
// Keep the `unsafeCache` because it can be a `Proxy`
["unsafeCache"]
);
};
/**
* @typedef {object} ResolverCache
* @property {WeakMap