1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const createHash = require("./util/createHash");
|
9 | const memoize = require("./util/memoize");
|
10 |
|
11 | const ModuleFilenameHelpers = exports;
|
12 |
|
13 |
|
14 | ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
|
15 | ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE = /\[all-?loaders\]\[resource\]/gi;
|
16 | ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
|
17 | ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
|
18 | ModuleFilenameHelpers.RESOURCE = "[resource]";
|
19 | ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
|
20 | ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
|
21 |
|
22 | ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH = /\[abs(olute)?-?resource-?path\]/gi;
|
23 | ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
|
24 | ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
|
25 | ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
|
26 | ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
|
27 | ModuleFilenameHelpers.LOADERS = "[loaders]";
|
28 | ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
|
29 | ModuleFilenameHelpers.QUERY = "[query]";
|
30 | ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
|
31 | ModuleFilenameHelpers.ID = "[id]";
|
32 | ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
|
33 | ModuleFilenameHelpers.HASH = "[hash]";
|
34 | ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
|
35 | ModuleFilenameHelpers.NAMESPACE = "[namespace]";
|
36 | ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
|
37 |
|
38 | const getAfter = (strFn, token) => {
|
39 | return () => {
|
40 | const str = strFn();
|
41 | const idx = str.indexOf(token);
|
42 | return idx < 0 ? "" : str.substr(idx);
|
43 | };
|
44 | };
|
45 |
|
46 | const getBefore = (strFn, token) => {
|
47 | return () => {
|
48 | const str = strFn();
|
49 | const idx = str.lastIndexOf(token);
|
50 | return idx < 0 ? "" : str.substr(0, idx);
|
51 | };
|
52 | };
|
53 |
|
54 | const getHash = strFn => {
|
55 | return () => {
|
56 | const hash = createHash("md4");
|
57 | hash.update(strFn());
|
58 | const digest = (hash.digest("hex"));
|
59 | return digest.substr(0, 4);
|
60 | };
|
61 | };
|
62 |
|
63 | const asRegExp = test => {
|
64 | if (typeof test === "string") {
|
65 | test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
|
66 | }
|
67 | return test;
|
68 | };
|
69 |
|
70 | const lazyObject = obj => {
|
71 | const newObj = {};
|
72 | for (const key of Object.keys(obj)) {
|
73 | const fn = obj[key];
|
74 | Object.defineProperty(newObj, key, {
|
75 | get: () => fn(),
|
76 | set: v => {
|
77 | Object.defineProperty(newObj, key, {
|
78 | value: v,
|
79 | enumerable: true,
|
80 | writable: true
|
81 | });
|
82 | },
|
83 | enumerable: true,
|
84 | configurable: true
|
85 | });
|
86 | }
|
87 | return newObj;
|
88 | };
|
89 |
|
90 | const REGEXP = /\[\\*([\w-]+)\\*\]/gi;
|
91 |
|
92 | ModuleFilenameHelpers.createFilename = (
|
93 | module,
|
94 | options,
|
95 | { requestShortener, chunkGraph }
|
96 | ) => {
|
97 | const opts = {
|
98 | namespace: "",
|
99 | moduleFilenameTemplate: "",
|
100 | ...(typeof options === "object"
|
101 | ? options
|
102 | : {
|
103 | moduleFilenameTemplate: options
|
104 | })
|
105 | };
|
106 |
|
107 | let absoluteResourcePath;
|
108 | let hash;
|
109 | let identifier;
|
110 | let moduleId;
|
111 | let shortIdentifier;
|
112 | if (module === undefined) module = "";
|
113 | if (typeof module === "string") {
|
114 | shortIdentifier = memoize(() => requestShortener.shorten(module));
|
115 | identifier = shortIdentifier;
|
116 | moduleId = () => "";
|
117 | absoluteResourcePath = () => module.split("!").pop();
|
118 | hash = getHash(identifier);
|
119 | } else {
|
120 | shortIdentifier = memoize(() =>
|
121 | module.readableIdentifier(requestShortener)
|
122 | );
|
123 | identifier = memoize(() => requestShortener.shorten(module.identifier()));
|
124 | moduleId = () => chunkGraph.getModuleId(module);
|
125 | absoluteResourcePath = () => module.identifier().split("!").pop();
|
126 | hash = getHash(identifier);
|
127 | }
|
128 | const resource = memoize(() => shortIdentifier().split("!").pop());
|
129 |
|
130 | const loaders = getBefore(shortIdentifier, "!");
|
131 | const allLoaders = getBefore(identifier, "!");
|
132 | const query = getAfter(resource, "?");
|
133 | const resourcePath = () => {
|
134 | const q = query().length;
|
135 | return q === 0 ? resource() : resource().slice(0, -q);
|
136 | };
|
137 | if (typeof opts.moduleFilenameTemplate === "function") {
|
138 | return opts.moduleFilenameTemplate(
|
139 | lazyObject({
|
140 | identifier: identifier,
|
141 | shortIdentifier: shortIdentifier,
|
142 | resource: resource,
|
143 | resourcePath: memoize(resourcePath),
|
144 | absoluteResourcePath: memoize(absoluteResourcePath),
|
145 | allLoaders: memoize(allLoaders),
|
146 | query: memoize(query),
|
147 | moduleId: memoize(moduleId),
|
148 | hash: memoize(hash),
|
149 | namespace: () => opts.namespace
|
150 | })
|
151 | );
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 | const replacements = new Map([
|
157 | ["identifier", identifier],
|
158 | ["short-identifier", shortIdentifier],
|
159 | ["resource", resource],
|
160 | ["resource-path", resourcePath],
|
161 |
|
162 | ["resourcepath", resourcePath],
|
163 | ["absolute-resource-path", absoluteResourcePath],
|
164 | ["abs-resource-path", absoluteResourcePath],
|
165 |
|
166 | ["absoluteresource-path", absoluteResourcePath],
|
167 |
|
168 | ["absresource-path", absoluteResourcePath],
|
169 |
|
170 | ["absolute-resourcepath", absoluteResourcePath],
|
171 |
|
172 | ["abs-resourcepath", absoluteResourcePath],
|
173 |
|
174 | ["absoluteresourcepath", absoluteResourcePath],
|
175 |
|
176 | ["absresourcepath", absoluteResourcePath],
|
177 | ["all-loaders", allLoaders],
|
178 |
|
179 | ["allloaders", allLoaders],
|
180 | ["loaders", loaders],
|
181 | ["query", query],
|
182 | ["id", moduleId],
|
183 | ["hash", hash],
|
184 | ["namespace", () => opts.namespace]
|
185 | ]);
|
186 |
|
187 |
|
188 | return opts.moduleFilenameTemplate
|
189 | .replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, "[identifier]")
|
190 | .replace(
|
191 | ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
|
192 | "[short-identifier]"
|
193 | )
|
194 | .replace(REGEXP, (match, content) => {
|
195 | if (content.length + 2 === match.length) {
|
196 | const replacement = replacements.get(content.toLowerCase());
|
197 | if (replacement !== undefined) {
|
198 | return replacement();
|
199 | }
|
200 | } else if (match.startsWith("[\\") && match.endsWith("\\]")) {
|
201 | return `[${match.slice(2, -2)}]`;
|
202 | }
|
203 | return match;
|
204 | });
|
205 | };
|
206 |
|
207 | ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
|
208 | const countMap = Object.create(null);
|
209 | const posMap = Object.create(null);
|
210 | array.forEach((item, idx) => {
|
211 | countMap[item] = countMap[item] || [];
|
212 | countMap[item].push(idx);
|
213 | posMap[item] = 0;
|
214 | });
|
215 | if (comparator) {
|
216 | Object.keys(countMap).forEach(item => {
|
217 | countMap[item].sort(comparator);
|
218 | });
|
219 | }
|
220 | return array.map((item, i) => {
|
221 | if (countMap[item].length > 1) {
|
222 | if (comparator && countMap[item][0] === i) return item;
|
223 | return fn(item, i, posMap[item]++);
|
224 | } else {
|
225 | return item;
|
226 | }
|
227 | });
|
228 | };
|
229 |
|
230 | ModuleFilenameHelpers.matchPart = (str, test) => {
|
231 | if (!test) return true;
|
232 | test = asRegExp(test);
|
233 | if (Array.isArray(test)) {
|
234 | return test.map(asRegExp).some(regExp => regExp.test(str));
|
235 | } else {
|
236 | return test.test(str);
|
237 | }
|
238 | };
|
239 |
|
240 | ModuleFilenameHelpers.matchObject = (obj, str) => {
|
241 | if (obj.test) {
|
242 | if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {
|
243 | return false;
|
244 | }
|
245 | }
|
246 | if (obj.include) {
|
247 | if (!ModuleFilenameHelpers.matchPart(str, obj.include)) {
|
248 | return false;
|
249 | }
|
250 | }
|
251 | if (obj.exclude) {
|
252 | if (ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
|
253 | return false;
|
254 | }
|
255 | }
|
256 | return true;
|
257 | };
|