-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathutils.js
84 lines (68 loc) · 1.96 KB
/
utils.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import path from 'path';
import loaderUtils from 'loader-utils';
const placholderRegExps = {
importItemG: /___CSS_LOADER_IMPORT___([0-9]+)___/g,
importItem: /___CSS_LOADER_IMPORT___([0-9]+)___/,
};
function getImportPrefix(loaderContext, importLoaders) {
if (importLoaders === false) {
return '';
}
const numberImportedLoaders = parseInt(importLoaders, 10) || 0;
const loadersRequest = loaderContext.loaders
.slice(
loaderContext.loaderIndex,
loaderContext.loaderIndex + 1 + numberImportedLoaders
)
.map((x) => x.request)
.join('!');
return `-!${loadersRequest}!`;
}
function dashesCamelCase(str) {
return str.replace(/-+(\w)/g, (match, firstLetter) =>
firstLetter.toUpperCase()
);
}
function getLocalIdent(loaderContext, localIdentName, localName, options) {
if (!options.context) {
// eslint-disable-next-line no-param-reassign
options.context = loaderContext.rootContext;
}
const request = path
.relative(options.context, loaderContext.resourcePath)
.replace(/\\/g, '/');
// eslint-disable-next-line no-param-reassign
options.content = `${options.hashPrefix + request}+${localName}`;
// eslint-disable-next-line no-param-reassign
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
const hash = loaderUtils.interpolateName(
loaderContext,
localIdentName,
options
);
return hash
.replace(new RegExp('[^a-zA-Z0-9\\-_\u00A0-\uFFFF]', 'g'), '-')
.replace(/^((-?[0-9])|--)/, '_$1');
}
function getFilter(filter, resourcePath, defaultFilter = null) {
return (content) => {
if (defaultFilter && !defaultFilter(content)) {
return false;
}
if (typeof filter === 'function') {
return !filter(content, resourcePath);
}
return true;
};
}
export {
getImportPrefix,
getLocalIdent,
placholderRegExps,
dashesCamelCase,
getFilter,
};