source: imaps-frontend/node_modules/webpack/lib/ResolverFactory.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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