-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
interpolateName.test.js
340 lines (324 loc) · 8.5 KB
/
interpolateName.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
'use strict';
const loaderUtils = require('../');
const emojiRegex = /[\uD800-\uDFFF]./;
describe('interpolateName()', () => {
function run(tests) {
tests.forEach((test) => {
const args = test[0];
const expected = test[1];
const message = test[2];
it(message, () => {
const result = loaderUtils.interpolateName.apply(loaderUtils, args);
if (typeof expected === 'function') {
expected(result);
} else {
expect(result).toBe(expected);
}
});
});
}
[
[
'/app/js/javascript.js',
'js/[hash].script.[ext]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js',
],
[
'/app/js/javascript.js',
'js/[contenthash].script.[ext]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js',
],
[
'/app/page.html',
'html-[hash:6].html',
'test content',
'html-9473fd.html',
],
[
'/app/page.html',
'html-[contenthash:6].html',
'test content',
'html-9473fd.html',
],
[
'/app/flash.txt',
'[hash]',
'test content',
'9473fdd0d880a43c21b7778d34872157',
],
[
'/app/flash.txt',
'[contenthash]',
'test content',
'9473fdd0d880a43c21b7778d34872157',
],
[
'/app/img/image.png',
'[sha512:hash:base64:7].[ext]',
'test content',
'2BKDTjl.png',
],
[
'/app/img/image.png',
'[sha512:contenthash:base64:7].[ext]',
'test content',
'2BKDTjl.png',
],
[
'/app/dir/file.png',
'[path][name].[ext]?[hash]',
'test content',
'/app/dir/file.png?9473fdd0d880a43c21b7778d34872157',
],
[
'/app/dir/file.png',
'[path][name].[ext]?[contenthash]',
'test content',
'/app/dir/file.png?9473fdd0d880a43c21b7778d34872157',
],
[
'/vendor/test/images/loading.gif',
(path) => path.replace(/\/?vendor\/?/, ''),
'test content',
'test/images/loading.gif',
],
[
'/pathWith.period/filename.js',
'js/[name].[ext]',
'test content',
'js/filename.js',
],
[
'/pathWith.period/filenameWithoutExt',
'js/[name].[ext]',
'test content',
'js/filenameWithoutExt.bin',
],
[
'/lib/components/modal/modal.css',
'[name]__modalTitle___[sha1:hash:hex:4]',
'test content',
'modal__modalTitle___1eeb',
],
[
'/lib/components/modal/modal.css',
'[name]__modalTitle___[sha1:contenthash:hex:4]',
'test content',
'modal__modalTitle___1eeb',
],
[
'/lib/components/modal/modal.css',
'[name].[md5:hash:base64:20].[ext]',
'test content',
'modal.1n8osQznuT8jOAwdzg_n.css',
],
[
'/lib/components/modal/modal.css',
'[name].[md5:contenthash:base64:20].[ext]',
'test content',
'modal.1n8osQznuT8jOAwdzg_n.css',
],
// Should not interpret without `hash` or `contenthash`
[
'/lib/components/modal/modal.css',
'[name].[md5::base64:20].[ext]',
'test content',
'modal.[md5::base64:20].css',
],
[
'/app/js/javascript.js?foo=bar',
'js/[hash].script.[ext][query]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar',
],
[
'/app/js/javascript.js?foo=bar&bar=baz',
'js/[hash].script.[ext][query]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar&bar=baz',
],
[
'/app/js/javascript.js?foo',
'js/[hash].script.[ext][query]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo',
],
[
'/app/js/javascript.js?',
'js/[hash].script.[ext][query]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js',
],
[
'/app/js/javascript.js?a',
'js/[hash].script.[ext][query]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js?a',
],
[
'/app/js/javascript.js?foo=bar#hash',
'js/[hash].script.[ext][query]',
'test content',
'js/9473fdd0d880a43c21b7778d34872157.script.js?foo=bar',
],
].forEach((test) => {
it('should interpolate ' + test[0] + ' ' + test[1], () => {
let resourcePath = '';
let resourceQuery = '';
const queryIdx = test[0].indexOf('?');
if (queryIdx >= 0) {
resourcePath = test[0].substr(0, queryIdx);
resourceQuery = test[0].substr(queryIdx);
} else {
resourcePath = test[0];
}
const interpolatedName = loaderUtils.interpolateName(
{ resourcePath, resourceQuery },
test[1],
{ content: test[2] }
);
expect(interpolatedName).toBe(test[3]);
});
});
[
'sha1fakename',
'9dxfakename',
'RSA-SHA256-fakename',
'ecdsa-with-SHA1-fakename',
'tls1.1-sha512-fakename',
].forEach((hashName) => {
it('should pick hash algorithm by name ' + hashName, () => {
expect(() => {
const interpolatedName = loaderUtils.interpolateName(
{},
'[' + hashName + ':hash:base64:10]',
{ content: 'a' }
);
// if for any reason the system we're running on has a hash
// algorithm matching any of our bogus names, at least make sure
// the output is not the unmodified name:
expect(interpolatedName[0]).not.toBe('[');
}).toThrow(/digest method not supported/i);
});
});
run([
[
[{}, '', { content: 'test string' }],
'6f8db599de986fab7a21625b7916589c.bin',
'should interpolate default tokens',
],
[
[{}, '[hash:base64]', { content: 'test string' }],
'2sm1pVmS8xuGJLCdWpJoRL',
'should interpolate [hash] token with options',
],
[
[{}, '[unrecognized]', { content: 'test string' }],
'[unrecognized]',
'should not interpolate unrecognized token',
],
[
[{}, '[emoji]', { content: 'test' }],
(result) => {
expect(emojiRegex.test(result)).toBe(true);
},
'should interpolate [emoji]',
],
[
[{}, '[emoji:3]', { content: 'string' }],
(result) => {
expect(emojiRegex.test(result)).toBe(true);
expect(result.length).toBeDefined();
},
'should interpolate [emoji:3]',
],
]);
it('should return the same emoji for the same string', () => {
const args = [{}, '[emoji:5]', { content: 'same_emoji' }];
const result1 = loaderUtils.interpolateName.apply(loaderUtils, args);
const result2 = loaderUtils.interpolateName.apply(loaderUtils, args);
expect(result1).toBe(result2);
});
it('should throw error when out of emoji', () => {
expect(() => {
loaderUtils.interpolateName.apply(loaderUtils, [
{},
'[emoji:5000]',
{ content: 'foo' },
]);
}).toThrow('Ran out of emoji');
});
describe('no loader context', () => {
const loaderContext = {};
run([
[[loaderContext, '[ext]', {}], 'bin', 'should interpolate [ext] token'],
[
[loaderContext, '[name]', {}],
'file',
'should interpolate [name] token',
],
[[loaderContext, '[path]', {}], '', 'should interpolate [path] token'],
[
[loaderContext, '[folder]', {}],
'',
'should interpolate [folder] token',
],
]);
});
describe('with loader context', () => {
const loaderContext = { resourcePath: '/path/to/file.exe' };
run([
[[loaderContext, '[ext]', {}], 'exe', 'should interpolate [ext] token'],
[
[loaderContext, '[name]', {}],
'file',
'should interpolate [name] token',
],
[
[loaderContext, '[path]', {}],
'/path/to/',
'should interpolate [path] token',
],
[
[loaderContext, '[folder]', {}],
'to',
'should interpolate [folder] token',
],
]);
});
run([
[
[
{
resourcePath: '/xyz',
options: {
customInterpolateName(str, name, options) {
return str + '-' + name + '-' + options.special;
},
},
},
'[name]',
{
special: 'special',
},
],
'xyz-[name]-special',
'should provide a custom interpolateName function in options',
],
[
[
{
resourcePath: '/foo/xyz.png',
},
'[1]-[name].[ext]',
{
regExp: /\/([a-z0-9]+)\/[a-z0-9]+\.png$/,
},
],
'foo-xyz.png',
'should support regExp in options',
],
]);
});