1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Tobias Koppers @sokra
|
4 |
|
|
*/
|
5 |
|
|
"use strict";
|
6 |
|
|
|
7 |
|
|
const createHash = require("./util/createHash");
|
8 |
|
|
|
9 |
|
|
const ModuleFilenameHelpers = exports;
|
10 |
|
|
|
11 |
|
|
ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
|
12 |
|
|
ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE = /\[all-?loaders\]\[resource\]/gi;
|
13 |
|
|
ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
|
14 |
|
|
ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE = /\[loaders\]\[resource\]/gi;
|
15 |
|
|
ModuleFilenameHelpers.RESOURCE = "[resource]";
|
16 |
|
|
ModuleFilenameHelpers.REGEXP_RESOURCE = /\[resource\]/gi;
|
17 |
|
|
ModuleFilenameHelpers.ABSOLUTE_RESOURCE_PATH = "[absolute-resource-path]";
|
18 |
|
|
ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH = /\[abs(olute)?-?resource-?path\]/gi;
|
19 |
|
|
ModuleFilenameHelpers.RESOURCE_PATH = "[resource-path]";
|
20 |
|
|
ModuleFilenameHelpers.REGEXP_RESOURCE_PATH = /\[resource-?path\]/gi;
|
21 |
|
|
ModuleFilenameHelpers.ALL_LOADERS = "[all-loaders]";
|
22 |
|
|
ModuleFilenameHelpers.REGEXP_ALL_LOADERS = /\[all-?loaders\]/gi;
|
23 |
|
|
ModuleFilenameHelpers.LOADERS = "[loaders]";
|
24 |
|
|
ModuleFilenameHelpers.REGEXP_LOADERS = /\[loaders\]/gi;
|
25 |
|
|
ModuleFilenameHelpers.QUERY = "[query]";
|
26 |
|
|
ModuleFilenameHelpers.REGEXP_QUERY = /\[query\]/gi;
|
27 |
|
|
ModuleFilenameHelpers.ID = "[id]";
|
28 |
|
|
ModuleFilenameHelpers.REGEXP_ID = /\[id\]/gi;
|
29 |
|
|
ModuleFilenameHelpers.HASH = "[hash]";
|
30 |
|
|
ModuleFilenameHelpers.REGEXP_HASH = /\[hash\]/gi;
|
31 |
|
|
ModuleFilenameHelpers.NAMESPACE = "[namespace]";
|
32 |
|
|
ModuleFilenameHelpers.REGEXP_NAMESPACE = /\[namespace\]/gi;
|
33 |
|
|
|
34 |
|
|
const getAfter = (str, token) => {
|
35 |
|
|
const idx = str.indexOf(token);
|
36 |
|
|
return idx < 0 ? "" : str.substr(idx);
|
37 |
|
|
};
|
38 |
|
|
|
39 |
|
|
const getBefore = (str, token) => {
|
40 |
|
|
const idx = str.lastIndexOf(token);
|
41 |
|
|
return idx < 0 ? "" : str.substr(0, idx);
|
42 |
|
|
};
|
43 |
|
|
|
44 |
|
|
const getHash = str => {
|
45 |
|
|
const hash = createHash("md4");
|
46 |
|
|
hash.update(str);
|
47 |
|
|
const digest = /** @type {string} */ (hash.digest("hex"));
|
48 |
|
|
return digest.substr(0, 4);
|
49 |
|
|
};
|
50 |
|
|
|
51 |
|
|
const asRegExp = test => {
|
52 |
|
|
if (typeof test === "string") {
|
53 |
|
|
test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
|
54 |
|
|
}
|
55 |
|
|
return test;
|
56 |
|
|
};
|
57 |
|
|
|
58 |
|
|
ModuleFilenameHelpers.createFilename = (module, options, requestShortener) => {
|
59 |
|
|
const opts = Object.assign(
|
60 |
|
|
{
|
61 |
|
|
namespace: "",
|
62 |
|
|
moduleFilenameTemplate: ""
|
63 |
|
|
},
|
64 |
|
|
typeof options === "object"
|
65 |
|
|
? options
|
66 |
|
|
: {
|
67 |
|
|
moduleFilenameTemplate: options
|
68 |
|
|
}
|
69 |
|
|
);
|
70 |
|
|
|
71 |
|
|
let absoluteResourcePath;
|
72 |
|
|
let hash;
|
73 |
|
|
let identifier;
|
74 |
|
|
let moduleId;
|
75 |
|
|
let shortIdentifier;
|
76 |
|
|
if (module === undefined) module = "";
|
77 |
|
|
if (typeof module === "string") {
|
78 |
|
|
shortIdentifier = requestShortener.shorten(module);
|
79 |
|
|
identifier = shortIdentifier;
|
80 |
|
|
moduleId = "";
|
81 |
|
|
absoluteResourcePath = module.split("!").pop();
|
82 |
|
|
hash = getHash(identifier);
|
83 |
|
|
} else {
|
84 |
|
|
shortIdentifier = module.readableIdentifier(requestShortener);
|
85 |
|
|
identifier = requestShortener.shorten(module.identifier());
|
86 |
|
|
moduleId = module.id;
|
87 |
|
|
absoluteResourcePath = module
|
88 |
|
|
.identifier()
|
89 |
|
|
.split("!")
|
90 |
|
|
.pop();
|
91 |
|
|
hash = getHash(identifier);
|
92 |
|
|
}
|
93 |
|
|
const resource = shortIdentifier.split("!").pop();
|
94 |
|
|
const loaders = getBefore(shortIdentifier, "!");
|
95 |
|
|
const allLoaders = getBefore(identifier, "!");
|
96 |
|
|
const query = getAfter(resource, "?");
|
97 |
|
|
const resourcePath = resource.substr(0, resource.length - query.length);
|
98 |
|
|
if (typeof opts.moduleFilenameTemplate === "function") {
|
99 |
|
|
return opts.moduleFilenameTemplate({
|
100 |
|
|
identifier: identifier,
|
101 |
|
|
shortIdentifier: shortIdentifier,
|
102 |
|
|
resource: resource,
|
103 |
|
|
resourcePath: resourcePath,
|
104 |
|
|
absoluteResourcePath: absoluteResourcePath,
|
105 |
|
|
allLoaders: allLoaders,
|
106 |
|
|
query: query,
|
107 |
|
|
moduleId: moduleId,
|
108 |
|
|
hash: hash,
|
109 |
|
|
namespace: opts.namespace
|
110 |
|
|
});
|
111 |
|
|
}
|
112 |
|
|
return opts.moduleFilenameTemplate
|
113 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, identifier)
|
114 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE, shortIdentifier)
|
115 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_RESOURCE, resource)
|
116 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_RESOURCE_PATH, resourcePath)
|
117 |
|
|
.replace(
|
118 |
|
|
ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH,
|
119 |
|
|
absoluteResourcePath
|
120 |
|
|
)
|
121 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS, allLoaders)
|
122 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_LOADERS, loaders)
|
123 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_QUERY, query)
|
124 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_ID, moduleId)
|
125 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_HASH, hash)
|
126 |
|
|
.replace(ModuleFilenameHelpers.REGEXP_NAMESPACE, opts.namespace);
|
127 |
|
|
};
|
128 |
|
|
|
129 |
|
|
ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {
|
130 |
|
|
const countMap = Object.create(null);
|
131 |
|
|
const posMap = Object.create(null);
|
132 |
|
|
array.forEach((item, idx) => {
|
133 |
|
|
countMap[item] = countMap[item] || [];
|
134 |
|
|
countMap[item].push(idx);
|
135 |
|
|
posMap[item] = 0;
|
136 |
|
|
});
|
137 |
|
|
if (comparator) {
|
138 |
|
|
Object.keys(countMap).forEach(item => {
|
139 |
|
|
countMap[item].sort(comparator);
|
140 |
|
|
});
|
141 |
|
|
}
|
142 |
|
|
return array.map((item, i) => {
|
143 |
|
|
if (countMap[item].length > 1) {
|
144 |
|
|
if (comparator && countMap[item][0] === i) return item;
|
145 |
|
|
return fn(item, i, posMap[item]++);
|
146 |
|
|
} else {
|
147 |
|
|
return item;
|
148 |
|
|
}
|
149 |
|
|
});
|
150 |
|
|
};
|
151 |
|
|
|
152 |
|
|
ModuleFilenameHelpers.matchPart = (str, test) => {
|
153 |
|
|
if (!test) return true;
|
154 |
|
|
test = asRegExp(test);
|
155 |
|
|
if (Array.isArray(test)) {
|
156 |
|
|
return test.map(asRegExp).some(regExp => regExp.test(str));
|
157 |
|
|
} else {
|
158 |
|
|
return test.test(str);
|
159 |
|
|
}
|
160 |
|
|
};
|
161 |
|
|
|
162 |
|
|
ModuleFilenameHelpers.matchObject = (obj, str) => {
|
163 |
|
|
if (obj.test) {
|
164 |
|
|
if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {
|
165 |
|
|
return false;
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
if (obj.include) {
|
169 |
|
|
if (!ModuleFilenameHelpers.matchPart(str, obj.include)) {
|
170 |
|
|
return false;
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
if (obj.exclude) {
|
174 |
|
|
if (ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
|
175 |
|
|
return false;
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
return true;
|
179 |
|
|
};
|