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 | (plugins.filter(
|
46 | 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 = (partialOptions);
|
57 |
|
58 | return removeOperations(
|
59 | resolveByProperty(options, "byDependency", dependencyType)
|
60 | );
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | module.exports = class ResolverFactory {
|
70 | constructor() {
|
71 | this.hooks = Object.freeze({
|
72 |
|
73 | resolveOptions: new HookMap(
|
74 | () => new SyncWaterfallHook(["resolveOptions"])
|
75 | ),
|
76 |
|
77 | resolver: new HookMap(
|
78 | () => new SyncHook(["resolver", "resolveOptions", "userResolveOptions"])
|
79 | )
|
80 | });
|
81 |
|
82 | this.cache = new Map();
|
83 | }
|
84 |
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | get(type, resolveOptions = EMPTY_RESOLVE_OPTIONS) {
|
91 | let typedCaches = this.cache.get(type);
|
92 | if (!typedCaches) {
|
93 | typedCaches = {
|
94 | direct: new WeakMap(),
|
95 | stringified: new Map()
|
96 | };
|
97 | this.cache.set(type, typedCaches);
|
98 | }
|
99 | const cachedResolver = typedCaches.direct.get(resolveOptions);
|
100 | if (cachedResolver) {
|
101 | return cachedResolver;
|
102 | }
|
103 | const ident = JSON.stringify(resolveOptions);
|
104 | const resolver = typedCaches.stringified.get(ident);
|
105 | if (resolver) {
|
106 | typedCaches.direct.set(resolveOptions, resolver);
|
107 | return resolver;
|
108 | }
|
109 | const newResolver = this._create(type, resolveOptions);
|
110 | typedCaches.direct.set(resolveOptions, newResolver);
|
111 | typedCaches.stringified.set(ident, newResolver);
|
112 | return newResolver;
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 | _create(type, resolveOptionsWithDepType) {
|
121 |
|
122 | const originalResolveOptions = { ...resolveOptionsWithDepType };
|
123 |
|
124 | const resolveOptions = convertToResolveOptions(
|
125 | this.hooks.resolveOptions.for(type).call(resolveOptionsWithDepType)
|
126 | );
|
127 | const resolver = (Factory.createResolver(
|
128 | resolveOptions
|
129 | ));
|
130 | if (!resolver) {
|
131 | throw new Error("No resolver created");
|
132 | }
|
133 |
|
134 | const childCache = new WeakMap();
|
135 | resolver.withOptions = options => {
|
136 | const cacheEntry = childCache.get(options);
|
137 | if (cacheEntry !== undefined) return cacheEntry;
|
138 | const mergedOptions = cachedCleverMerge(originalResolveOptions, options);
|
139 | const resolver = this.get(type, mergedOptions);
|
140 | childCache.set(options, resolver);
|
141 | return resolver;
|
142 | };
|
143 | this.hooks.resolver
|
144 | .for(type)
|
145 | .call(resolver, resolveOptions, originalResolveOptions);
|
146 | return resolver;
|
147 | }
|
148 | };
|