1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const Factory = require("enhanced-resolve").ResolverFactory;
|
9 | const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
|
10 | const {
|
11 | cachedCleverMerge,
|
12 | removeOperations,
|
13 | resolveByProperty
|
14 | } = require("./util/cleverMerge");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const EMPTY_RESOLVE_OPTIONS = {};
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | const convertToResolveOptions = resolveOptionsWithDepType => {
|
37 | const { dependencyType, plugins, ...remaining } = resolveOptionsWithDepType;
|
38 |
|
39 |
|
40 |
|
41 | const partialOptions = {
|
42 | ...remaining,
|
43 | plugins:
|
44 | plugins &&
|
45 | (
|
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 |
|
56 | const options =
|
57 | (
|
58 | partialOptions
|
59 | );
|
60 |
|
61 | return removeOperations(
|
62 | resolveByProperty(options, "byDependency", dependencyType)
|
63 | );
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | module.exports = class ResolverFactory {
|
73 | constructor() {
|
74 | this.hooks = Object.freeze({
|
75 |
|
76 | resolveOptions: new HookMap(
|
77 | () => new SyncWaterfallHook(["resolveOptions"])
|
78 | ),
|
79 |
|
80 | resolver: new HookMap(
|
81 | () => new SyncHook(["resolver", "resolveOptions", "userResolveOptions"])
|
82 | )
|
83 | });
|
84 |
|
85 | this.cache = new Map();
|
86 | }
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
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 |
|
120 |
|
121 |
|
122 |
|
123 | _create(type, resolveOptionsWithDepType) {
|
124 |
|
125 | const originalResolveOptions = { ...resolveOptionsWithDepType };
|
126 |
|
127 | const resolveOptions = convertToResolveOptions(
|
128 | this.hooks.resolveOptions.for(type).call(resolveOptionsWithDepType)
|
129 | );
|
130 | const resolver = (
|
131 | Factory.createResolver(resolveOptions)
|
132 | );
|
133 | if (!resolver) {
|
134 | throw new Error("No resolver created");
|
135 | }
|
136 |
|
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 | };
|