source: trip-planner-front/node_modules/webpack/lib/ResolverFactory.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Factory = require("enhanced-resolve").ResolverFactory;
9const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
10const {
11 cachedCleverMerge,
12 removeOperations,
13 resolveByProperty
14} = require("./util/cleverMerge");
15
16/** @typedef {import("enhanced-resolve").ResolveOptions} ResolveOptions */
17/** @typedef {import("enhanced-resolve").Resolver} Resolver */
18/** @typedef {import("../declarations/WebpackOptions").ResolveOptions} WebpackResolveOptions */
19/** @typedef {import("../declarations/WebpackOptions").ResolvePluginInstance} ResolvePluginInstance */
20
21/** @typedef {WebpackResolveOptions & {dependencyType?: string, resolveToContext?: boolean }} ResolveOptionsWithDependencyType */
22/**
23 * @typedef {Object} WithOptions
24 * @property {function(Partial<ResolveOptionsWithDependencyType>): ResolverWithOptions} withOptions create a resolver with additional/different options
25 */
26
27/** @typedef {Resolver & WithOptions} ResolverWithOptions */
28
29// need to be hoisted on module level for caching identity
30const EMPTY_RESOLVE_OPTIONS = {};
31
32/**
33 * @param {ResolveOptionsWithDependencyType} resolveOptionsWithDepType enhanced options
34 * @returns {ResolveOptions} merged options
35 */
36const convertToResolveOptions = resolveOptionsWithDepType => {
37 const { dependencyType, plugins, ...remaining } = resolveOptionsWithDepType;
38
39 // check type compat
40 /** @type {Partial<ResolveOptions>} */
41 const partialOptions = {
42 ...remaining,
43 plugins:
44 plugins &&
45 /** @type {ResolvePluginInstance[]} */ (
46 plugins.filter(item => item !== "...")
47 )
48 };
49
50 if (!partialOptions.fileSystem) {
51 throw new Error(
52 "fileSystem is missing in resolveOptions, but it's required for enhanced-resolve"
53 );
54 }
55 // These weird types validate that we checked all non-optional properties
56 const options =
57 /** @type {Partial<ResolveOptions> & Pick<ResolveOptions, "fileSystem">} */ (
58 partialOptions
59 );
60
61 return removeOperations(
62 resolveByProperty(options, "byDependency", dependencyType)
63 );
64};
65
66/**
67 * @typedef {Object} ResolverCache
68 * @property {WeakMap<Object, ResolverWithOptions>} direct
69 * @property {Map<string, ResolverWithOptions>} stringified
70 */
71
72module.exports = class ResolverFactory {
73 constructor() {
74 this.hooks = Object.freeze({
75 /** @type {HookMap<SyncWaterfallHook<[ResolveOptionsWithDependencyType]>>} */
76 resolveOptions: new HookMap(
77 () => new SyncWaterfallHook(["resolveOptions"])
78 ),
79 /** @type {HookMap<SyncHook<[Resolver, ResolveOptions, ResolveOptionsWithDependencyType]>>} */
80 resolver: new HookMap(
81 () => new SyncHook(["resolver", "resolveOptions", "userResolveOptions"])
82 )
83 });
84 /** @type {Map<string, ResolverCache>} */
85 this.cache = new Map();
86 }
87
88 /**
89 * @param {string} type type of resolver
90 * @param {ResolveOptionsWithDependencyType=} resolveOptions options
91 * @returns {ResolverWithOptions} the resolver
92 */
93 get(type, resolveOptions = EMPTY_RESOLVE_OPTIONS) {
94 let typedCaches = this.cache.get(type);
95 if (!typedCaches) {
96 typedCaches = {
97 direct: new WeakMap(),
98 stringified: new Map()
99 };
100 this.cache.set(type, typedCaches);
101 }
102 const cachedResolver = typedCaches.direct.get(resolveOptions);
103 if (cachedResolver) {
104 return cachedResolver;
105 }
106 const ident = JSON.stringify(resolveOptions);
107 const resolver = typedCaches.stringified.get(ident);
108 if (resolver) {
109 typedCaches.direct.set(resolveOptions, resolver);
110 return resolver;
111 }
112 const newResolver = this._create(type, resolveOptions);
113 typedCaches.direct.set(resolveOptions, newResolver);
114 typedCaches.stringified.set(ident, newResolver);
115 return newResolver;
116 }
117
118 /**
119 * @param {string} type type of resolver
120 * @param {ResolveOptionsWithDependencyType} resolveOptionsWithDepType options
121 * @returns {ResolverWithOptions} the resolver
122 */
123 _create(type, resolveOptionsWithDepType) {
124 /** @type {ResolveOptionsWithDependencyType} */
125 const originalResolveOptions = { ...resolveOptionsWithDepType };
126
127 const resolveOptions = convertToResolveOptions(
128 this.hooks.resolveOptions.for(type).call(resolveOptionsWithDepType)
129 );
130 const resolver = /** @type {ResolverWithOptions} */ (
131 Factory.createResolver(resolveOptions)
132 );
133 if (!resolver) {
134 throw new Error("No resolver created");
135 }
136 /** @type {WeakMap<Partial<ResolveOptionsWithDependencyType>, ResolverWithOptions>} */
137 const childCache = new WeakMap();
138 resolver.withOptions = options => {
139 const cacheEntry = childCache.get(options);
140 if (cacheEntry !== undefined) return cacheEntry;
141 const mergedOptions = cachedCleverMerge(originalResolveOptions, options);
142 const resolver = this.get(type, mergedOptions);
143 childCache.set(options, resolver);
144 return resolver;
145 };
146 this.hooks.resolver
147 .for(type)
148 .call(resolver, resolveOptions, originalResolveOptions);
149 return resolver;
150 }
151};
Note: See TracBrowser for help on using the repository browser.