css-loader 2.1.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -1
- package/README.md +252 -98
- package/dist/index.js +39 -178
- package/dist/options.json +43 -36
- package/dist/plugins/postcss-icss-parser.js +48 -62
- package/dist/plugins/postcss-import-parser.js +4 -6
- package/dist/plugins/postcss-url-parser.js +60 -40
- package/dist/runtime/api.js +15 -10
- package/dist/runtime/{url-escape.js → getUrl.js} +3 -2
- package/dist/utils.js +263 -30
- package/package.json +48 -79
|
@@ -9,6 +9,8 @@ var _postcss = _interopRequireDefault(require("postcss"));
|
|
|
9
9
|
|
|
10
10
|
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
|
11
11
|
|
|
12
|
+
var _utils = require("../utils");
|
|
13
|
+
|
|
12
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
15
|
|
|
14
16
|
const pluginName = 'postcss-url-parser';
|
|
@@ -54,68 +56,73 @@ function walkUrls(parsed, callback) {
|
|
|
54
56
|
});
|
|
55
57
|
}
|
|
56
58
|
|
|
57
|
-
function
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
function getUrlsFromValue(value, result, filter, decl = null) {
|
|
60
|
+
if (!needParseDecl.test(value)) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const parsed = (0, _postcssValueParser.default)(value);
|
|
65
|
+
const urls = [];
|
|
66
|
+
walkUrls(parsed, (node, url, needQuotes) => {
|
|
67
|
+
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
|
|
68
|
+
result.warn(`Unable to find uri in '${decl ? decl.toString() : value}'`, decl ? {
|
|
69
|
+
node: decl
|
|
70
|
+
} : {});
|
|
61
71
|
return;
|
|
62
72
|
}
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
|
|
68
|
-
result.warn(`Unable to find uri in '${decl.toString()}'`, {
|
|
69
|
-
node: decl
|
|
70
|
-
});
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (filter && !filter(url)) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
74
|
+
if (filter && !filter(url)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
78
|
+
urls.push({
|
|
79
|
+
url,
|
|
80
|
+
needQuotes
|
|
82
81
|
});
|
|
82
|
+
}); // eslint-disable-next-line consistent-return
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
parsed,
|
|
86
|
+
urls
|
|
87
|
+
};
|
|
88
|
+
}
|
|
83
89
|
|
|
84
|
-
|
|
90
|
+
function walkDeclsWithUrl(css, result, filter) {
|
|
91
|
+
const items = [];
|
|
92
|
+
css.walkDecls(decl => {
|
|
93
|
+
const item = getUrlsFromValue(decl.value, result, filter, decl);
|
|
94
|
+
|
|
95
|
+
if (!item) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (item.urls.length === 0) {
|
|
85
100
|
return;
|
|
86
101
|
}
|
|
87
102
|
|
|
88
103
|
items.push({
|
|
89
104
|
decl,
|
|
90
|
-
parsed,
|
|
91
|
-
urls
|
|
105
|
+
parsed: item.parsed,
|
|
106
|
+
urls: item.urls
|
|
92
107
|
});
|
|
93
108
|
});
|
|
94
109
|
return items;
|
|
95
110
|
}
|
|
96
111
|
|
|
97
|
-
function uniqWith(array, comparator) {
|
|
98
|
-
return array.reduce((acc, d) => !acc.some(item => comparator(d, item)) ? [...acc, d] : acc, []);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function flatten(array) {
|
|
102
|
-
return array.reduce((a, b) => a.concat(b), []);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function isEqual(value, other) {
|
|
106
|
-
return value.url === other.url && value.needQuotes === other.needQuotes;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
112
|
var _default = _postcss.default.plugin(pluginName, (options = {}) => function process(css, result) {
|
|
110
113
|
const traversed = walkDeclsWithUrl(css, result, options.filter);
|
|
111
|
-
const paths = uniqWith(flatten(traversed.map(item => item.urls)),
|
|
114
|
+
const paths = (0, _utils.uniqWith)((0, _utils.flatten)(traversed.map(item => item.urls)), (value, other) => value.url === other.url && value.needQuotes === other.needQuotes);
|
|
112
115
|
|
|
113
116
|
if (paths.length === 0) {
|
|
114
117
|
return;
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
const placeholders = [];
|
|
121
|
+
let hasUrlHelper = false;
|
|
118
122
|
paths.forEach((path, index) => {
|
|
123
|
+
const {
|
|
124
|
+
loaderContext
|
|
125
|
+
} = options;
|
|
119
126
|
const placeholder = `___CSS_LOADER_URL___${index}___`;
|
|
120
127
|
const {
|
|
121
128
|
url,
|
|
@@ -125,14 +132,27 @@ var _default = _postcss.default.plugin(pluginName, (options = {}) => function pr
|
|
|
125
132
|
placeholder,
|
|
126
133
|
path
|
|
127
134
|
});
|
|
135
|
+
|
|
136
|
+
if (!hasUrlHelper) {
|
|
137
|
+
result.messages.push({
|
|
138
|
+
pluginName,
|
|
139
|
+
type: 'import',
|
|
140
|
+
import: (0, _utils.getUrlHelperCode)(loaderContext)
|
|
141
|
+
}); // eslint-disable-next-line no-param-reassign
|
|
142
|
+
|
|
143
|
+
hasUrlHelper = true;
|
|
144
|
+
}
|
|
145
|
+
|
|
128
146
|
result.messages.push({
|
|
129
147
|
pluginName,
|
|
130
|
-
type: '
|
|
131
|
-
|
|
148
|
+
type: 'import',
|
|
149
|
+
import: (0, _utils.getUrlItemCode)({
|
|
132
150
|
url,
|
|
133
151
|
placeholder,
|
|
134
152
|
needQuotes
|
|
135
|
-
}
|
|
153
|
+
}, loaderContext),
|
|
154
|
+
importType: 'url',
|
|
155
|
+
placeholder
|
|
136
156
|
});
|
|
137
157
|
});
|
|
138
158
|
traversed.forEach(item => {
|
package/dist/runtime/api.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
Author Tobias Koppers @sokra
|
|
6
6
|
*/
|
|
7
7
|
// css base code, injected by the css-loader
|
|
8
|
+
// eslint-disable-next-line func-names
|
|
8
9
|
module.exports = function (useSourceMap) {
|
|
9
10
|
var list = []; // return the list of modules as css string
|
|
10
11
|
|
|
@@ -13,22 +14,25 @@ module.exports = function (useSourceMap) {
|
|
|
13
14
|
var content = cssWithMappingToString(item, useSourceMap);
|
|
14
15
|
|
|
15
16
|
if (item[2]) {
|
|
16
|
-
return
|
|
17
|
-
} else {
|
|
18
|
-
return content;
|
|
17
|
+
return "@media ".concat(item[2], "{").concat(content, "}");
|
|
19
18
|
}
|
|
19
|
+
|
|
20
|
+
return content;
|
|
20
21
|
}).join('');
|
|
21
22
|
}; // import a list of modules into the list
|
|
23
|
+
// eslint-disable-next-line func-names
|
|
22
24
|
|
|
23
25
|
|
|
24
26
|
list.i = function (modules, mediaQuery) {
|
|
25
27
|
if (typeof modules === 'string') {
|
|
28
|
+
// eslint-disable-next-line no-param-reassign
|
|
26
29
|
modules = [[null, modules, '']];
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
var alreadyImportedModules = {};
|
|
30
33
|
|
|
31
34
|
for (var i = 0; i < this.length; i++) {
|
|
35
|
+
// eslint-disable-next-line prefer-destructuring
|
|
32
36
|
var id = this[i][0];
|
|
33
37
|
|
|
34
38
|
if (id != null) {
|
|
@@ -36,8 +40,8 @@ module.exports = function (useSourceMap) {
|
|
|
36
40
|
}
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
for (
|
|
40
|
-
var item = modules[
|
|
43
|
+
for (var _i = 0; _i < modules.length; _i++) {
|
|
44
|
+
var item = modules[_i]; // skip already imported module
|
|
41
45
|
// this implementation is not 100% perfect for weird media query combinations
|
|
42
46
|
// when a module is imported multiple times with different media queries.
|
|
43
47
|
// I hope this will never occur (Hey this way we have smaller bundles)
|
|
@@ -46,7 +50,7 @@ module.exports = function (useSourceMap) {
|
|
|
46
50
|
if (mediaQuery && !item[2]) {
|
|
47
51
|
item[2] = mediaQuery;
|
|
48
52
|
} else if (mediaQuery) {
|
|
49
|
-
item[2] =
|
|
53
|
+
item[2] = "(".concat(item[2], ") and (").concat(mediaQuery, ")");
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
list.push(item);
|
|
@@ -58,7 +62,8 @@ module.exports = function (useSourceMap) {
|
|
|
58
62
|
};
|
|
59
63
|
|
|
60
64
|
function cssWithMappingToString(item, useSourceMap) {
|
|
61
|
-
var content = item[1] || '';
|
|
65
|
+
var content = item[1] || ''; // eslint-disable-next-line prefer-destructuring
|
|
66
|
+
|
|
62
67
|
var cssMapping = item[3];
|
|
63
68
|
|
|
64
69
|
if (!cssMapping) {
|
|
@@ -68,7 +73,7 @@ function cssWithMappingToString(item, useSourceMap) {
|
|
|
68
73
|
if (useSourceMap && typeof btoa === 'function') {
|
|
69
74
|
var sourceMapping = toComment(cssMapping);
|
|
70
75
|
var sourceURLs = cssMapping.sources.map(function (source) {
|
|
71
|
-
return
|
|
76
|
+
return "/*# sourceURL=".concat(cssMapping.sourceRoot).concat(source, " */");
|
|
72
77
|
});
|
|
73
78
|
return [content].concat(sourceURLs).concat([sourceMapping]).join('\n');
|
|
74
79
|
}
|
|
@@ -80,6 +85,6 @@ function cssWithMappingToString(item, useSourceMap) {
|
|
|
80
85
|
function toComment(sourceMap) {
|
|
81
86
|
// eslint-disable-next-line no-undef
|
|
82
87
|
var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
|
|
83
|
-
var data =
|
|
84
|
-
return
|
|
88
|
+
var data = "sourceMappingURL=data:application/json;charset=utf-8;base64,".concat(base64);
|
|
89
|
+
return "/*# ".concat(data, " */");
|
|
85
90
|
}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
module.exports = function
|
|
3
|
+
module.exports = function (url, needQuotes) {
|
|
4
4
|
if (typeof url !== 'string') {
|
|
5
5
|
return url;
|
|
6
6
|
} // If url is already wrapped in quotes, remove them
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
if (/^['"].*['"]$/.test(url)) {
|
|
10
|
+
// eslint-disable-next-line no-param-reassign
|
|
10
11
|
url = url.slice(1, -1);
|
|
11
12
|
} // Should url be wrapped?
|
|
12
13
|
// See https://drafts.csswg.org/css-values-3/#urls
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
if (/["'() \t\n]/.test(url) || needQuotes) {
|
|
16
|
-
return
|
|
17
|
+
return "\"".concat(url.replace(/"/g, '\\"').replace(/\n/g, '\\n'), "\"");
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
return url;
|
package/dist/utils.js
CHANGED
|
@@ -3,18 +3,43 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.uniqWith = uniqWith;
|
|
7
|
+
exports.flatten = flatten;
|
|
8
|
+
exports.dashesCamelCase = dashesCamelCase;
|
|
6
9
|
exports.getImportPrefix = getImportPrefix;
|
|
7
10
|
exports.getLocalIdent = getLocalIdent;
|
|
8
|
-
exports.camelCase = camelCase;
|
|
9
|
-
exports.dashesCamelCase = dashesCamelCase;
|
|
10
11
|
exports.getFilter = getFilter;
|
|
11
|
-
exports.
|
|
12
|
+
exports.getModulesPlugins = getModulesPlugins;
|
|
13
|
+
exports.normalizeSourceMap = normalizeSourceMap;
|
|
14
|
+
exports.getImportItemCode = getImportItemCode;
|
|
15
|
+
exports.getUrlHelperCode = getUrlHelperCode;
|
|
16
|
+
exports.getUrlItemCode = getUrlItemCode;
|
|
17
|
+
exports.getApiCode = getApiCode;
|
|
18
|
+
exports.getImportCode = getImportCode;
|
|
19
|
+
exports.getModuleCode = getModuleCode;
|
|
20
|
+
exports.getExportItemCode = getExportItemCode;
|
|
21
|
+
exports.getExportCode = getExportCode;
|
|
22
|
+
exports.prepareCode = prepareCode;
|
|
12
23
|
|
|
13
24
|
var _path = _interopRequireDefault(require("path"));
|
|
14
25
|
|
|
26
|
+
var _loaderUtils = _interopRequireWildcard(require("loader-utils"));
|
|
27
|
+
|
|
28
|
+
var _normalizePath = _interopRequireDefault(require("normalize-path"));
|
|
29
|
+
|
|
30
|
+
var _cssesc = _interopRequireDefault(require("cssesc"));
|
|
31
|
+
|
|
32
|
+
var _postcssModulesValues = _interopRequireDefault(require("postcss-modules-values"));
|
|
33
|
+
|
|
34
|
+
var _postcssModulesLocalByDefault = _interopRequireDefault(require("postcss-modules-local-by-default"));
|
|
35
|
+
|
|
36
|
+
var _postcssModulesExtractImports = _interopRequireDefault(require("postcss-modules-extract-imports"));
|
|
37
|
+
|
|
38
|
+
var _postcssModulesScope = _interopRequireDefault(require("postcss-modules-scope"));
|
|
39
|
+
|
|
15
40
|
var _camelcase = _interopRequireDefault(require("camelcase"));
|
|
16
41
|
|
|
17
|
-
var
|
|
42
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
|
18
43
|
|
|
19
44
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
45
|
|
|
@@ -22,13 +47,17 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
22
47
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
23
48
|
Author Tobias Koppers @sokra
|
|
24
49
|
*/
|
|
50
|
+
function uniqWith(array, comparator) {
|
|
51
|
+
return array.reduce((acc, d) => !acc.some(item => comparator(d, item)) ? [...acc, d] : acc, []);
|
|
52
|
+
}
|
|
25
53
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
54
|
+
function flatten(array) {
|
|
55
|
+
return array.reduce((a, b) => a.concat(b), []);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function dashesCamelCase(str) {
|
|
59
|
+
return str.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase());
|
|
60
|
+
}
|
|
32
61
|
|
|
33
62
|
function getImportPrefix(loaderContext, importLoaders) {
|
|
34
63
|
if (importLoaders === false) {
|
|
@@ -40,20 +69,14 @@ function getImportPrefix(loaderContext, importLoaders) {
|
|
|
40
69
|
return `-!${loadersRequest}!`;
|
|
41
70
|
}
|
|
42
71
|
|
|
43
|
-
function camelCase(str) {
|
|
44
|
-
return (0, _camelcase.default)(str);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function dashesCamelCase(str) {
|
|
48
|
-
return str.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase());
|
|
49
|
-
}
|
|
50
|
-
|
|
51
72
|
const whitespace = '[\\x20\\t\\r\\n\\f]';
|
|
52
73
|
const unescapeRegExp = new RegExp(`\\\\([\\da-f]{1,6}${whitespace}?|(${whitespace})|.)`, 'ig');
|
|
53
74
|
|
|
54
75
|
function unescape(str) {
|
|
55
76
|
return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
|
|
56
|
-
const high = `0x${escaped}` - 0x10000;
|
|
77
|
+
const high = `0x${escaped}` - 0x10000;
|
|
78
|
+
/* eslint-disable line-comment-position */
|
|
79
|
+
// NaN means non-codepoint
|
|
57
80
|
// Workaround erroneous numeric interpretation of +"0x"
|
|
58
81
|
// eslint-disable-next-line no-self-compare
|
|
59
82
|
|
|
@@ -61,6 +84,7 @@ function unescape(str) {
|
|
|
61
84
|
String.fromCharCode(high + 0x10000) : // Supplemental Plane codepoint (surrogate pair)
|
|
62
85
|
// eslint-disable-next-line no-bitwise
|
|
63
86
|
String.fromCharCode(high >> 10 | 0xd800, high & 0x3ff | 0xdc00);
|
|
87
|
+
/* eslint-enable line-comment-position */
|
|
64
88
|
});
|
|
65
89
|
}
|
|
66
90
|
|
|
@@ -70,28 +94,237 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
|
|
|
70
94
|
options.context = loaderContext.rootContext;
|
|
71
95
|
}
|
|
72
96
|
|
|
73
|
-
const request = _path.default.relative(options.context, loaderContext.resourcePath)
|
|
74
|
-
|
|
97
|
+
const request = (0, _normalizePath.default)(_path.default.relative(options.context || '', loaderContext.resourcePath)); // eslint-disable-next-line no-param-reassign
|
|
75
98
|
|
|
76
|
-
options.content = `${options.hashPrefix + request}+${unescape(localName)}`; //
|
|
99
|
+
options.content = `${options.hashPrefix + request}+${unescape(localName)}`; // Using `[path]` placeholder outputs `/` we need escape their
|
|
100
|
+
// Also directories can contains invalid characters for css we need escape their too
|
|
77
101
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return hash.replace(new RegExp('[^a-zA-Z0-9\\-_\u00A0-\uFFFF]', 'g'), '-').replace(/^((-?[0-9])|--)/, '_$1');
|
|
102
|
+
return (0, _cssesc.default)(_loaderUtils.default.interpolateName(loaderContext, localIdentName, options) // For `[hash]` placeholder
|
|
103
|
+
.replace(/^((-?[0-9])|--)/, '_$1'), {
|
|
104
|
+
isIdentifier: true
|
|
105
|
+
}).replace(/\\\[local\\\]/gi, localName);
|
|
83
106
|
}
|
|
84
107
|
|
|
85
108
|
function getFilter(filter, resourcePath, defaultFilter = null) {
|
|
86
|
-
return
|
|
87
|
-
if (defaultFilter && !defaultFilter(
|
|
109
|
+
return item => {
|
|
110
|
+
if (defaultFilter && !defaultFilter(item)) {
|
|
88
111
|
return false;
|
|
89
112
|
}
|
|
90
113
|
|
|
91
114
|
if (typeof filter === 'function') {
|
|
92
|
-
return
|
|
115
|
+
return filter(item, resourcePath);
|
|
93
116
|
}
|
|
94
117
|
|
|
95
118
|
return true;
|
|
96
119
|
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function getModulesPlugins(options, loaderContext) {
|
|
123
|
+
let modulesOptions = {
|
|
124
|
+
mode: 'local',
|
|
125
|
+
localIdentName: '[hash:base64]',
|
|
126
|
+
getLocalIdent,
|
|
127
|
+
context: null,
|
|
128
|
+
hashPrefix: '',
|
|
129
|
+
localIdentRegExp: null
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
if (typeof options.modules === 'boolean' || typeof options.modules === 'string') {
|
|
133
|
+
modulesOptions.mode = typeof options.modules === 'string' ? options.modules : 'local';
|
|
134
|
+
} else {
|
|
135
|
+
modulesOptions = Object.assign({}, modulesOptions, options.modules);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return [_postcssModulesValues.default, (0, _postcssModulesLocalByDefault.default)({
|
|
139
|
+
mode: modulesOptions.mode
|
|
140
|
+
}), (0, _postcssModulesExtractImports.default)(), (0, _postcssModulesScope.default)({
|
|
141
|
+
generateScopedName: function generateScopedName(exportName) {
|
|
142
|
+
return modulesOptions.getLocalIdent(loaderContext, modulesOptions.localIdentName, exportName, {
|
|
143
|
+
context: modulesOptions.context,
|
|
144
|
+
hashPrefix: modulesOptions.hashPrefix,
|
|
145
|
+
regExp: modulesOptions.localIdentRegExp
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
})];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function normalizeSourceMap(map) {
|
|
152
|
+
let newMap = map; // Some loader emit source map as string
|
|
153
|
+
// Strip any JSON XSSI avoidance prefix from the string (as documented in the source maps specification), and then parse the string as JSON.
|
|
154
|
+
|
|
155
|
+
if (typeof newMap === 'string') {
|
|
156
|
+
newMap = JSON.parse(newMap.replace(/^\)]}'[^\n]*\n/, ''));
|
|
157
|
+
} // Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
|
|
158
|
+
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
if (newMap.file) {
|
|
162
|
+
newMap.file = (0, _normalizePath.default)(newMap.file);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (newMap.sourceRoot) {
|
|
166
|
+
newMap.sourceRoot = (0, _normalizePath.default)(newMap.sourceRoot);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (newMap.sources) {
|
|
170
|
+
newMap.sources = newMap.sources.map(source => (0, _normalizePath.default)(source));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return newMap;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function getImportItemCode(item, loaderContext, importPrefix) {
|
|
177
|
+
const {
|
|
178
|
+
url
|
|
179
|
+
} = item;
|
|
180
|
+
const media = item.media || '';
|
|
181
|
+
|
|
182
|
+
if (!(0, _loaderUtils.isUrlRequest)(url)) {
|
|
183
|
+
return `exports.push([module.id, ${JSON.stringify(`@import url(${url});`)}, ${JSON.stringify(media)}]);`;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const importUrl = importPrefix + (0, _loaderUtils.urlToRequest)(url);
|
|
187
|
+
return `exports.i(require(${(0, _loaderUtils.stringifyRequest)(loaderContext, importUrl)}), ${JSON.stringify(media)});`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function getUrlHelperCode(loaderContext) {
|
|
191
|
+
return `var getUrl = require(${(0, _loaderUtils.stringifyRequest)(loaderContext, require.resolve('./runtime/getUrl.js'))});`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function getUrlItemCode(item, loaderContext) {
|
|
195
|
+
const {
|
|
196
|
+
url,
|
|
197
|
+
placeholder,
|
|
198
|
+
needQuotes
|
|
199
|
+
} = item; // Remove `#hash` and `?#hash` from `require`
|
|
200
|
+
|
|
201
|
+
const [normalizedUrl, singleQuery, hashValue] = url.split(/(\?)?#/);
|
|
202
|
+
const hash = singleQuery || hashValue ? `"${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}"` : '';
|
|
203
|
+
return `var ${placeholder} = getUrl(require(${(0, _loaderUtils.stringifyRequest)(loaderContext, (0, _loaderUtils.urlToRequest)(normalizedUrl))})${hash ? ` + ${hash}` : ''}${needQuotes ? ', true' : ''});`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function getApiCode(loaderContext, sourceMap, onlyLocals) {
|
|
207
|
+
if (onlyLocals) {
|
|
208
|
+
return '';
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return `exports = module.exports = require(${(0, _loaderUtils.stringifyRequest)(loaderContext, require.resolve('./runtime/api'))})(${sourceMap});\n`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function getImportCode(importItems, onlyLocals) {
|
|
215
|
+
if (importItems.length === 0 || onlyLocals) {
|
|
216
|
+
return '';
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return `// Imports\n${importItems.join('\n')}\n`;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function getModuleCode(result, sourceMap, onlyLocals) {
|
|
223
|
+
if (onlyLocals) {
|
|
224
|
+
return '';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return `// Module\nexports.push([module.id, ${JSON.stringify(result.css)}, ""${sourceMap && result.map ? `,${result.map}` : ''}]);\n`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function getExportItemCode(key, value, localsConvention) {
|
|
231
|
+
let targetKey;
|
|
232
|
+
const items = [];
|
|
233
|
+
|
|
234
|
+
function addEntry(k) {
|
|
235
|
+
items.push(`\t${JSON.stringify(k)}: ${JSON.stringify(value)}`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
switch (localsConvention) {
|
|
239
|
+
case 'camelCase':
|
|
240
|
+
addEntry(key);
|
|
241
|
+
targetKey = (0, _camelcase.default)(key);
|
|
242
|
+
|
|
243
|
+
if (targetKey !== key) {
|
|
244
|
+
addEntry(targetKey);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
break;
|
|
248
|
+
|
|
249
|
+
case 'camelCaseOnly':
|
|
250
|
+
addEntry((0, _camelcase.default)(key));
|
|
251
|
+
break;
|
|
252
|
+
|
|
253
|
+
case 'dashes':
|
|
254
|
+
addEntry(key);
|
|
255
|
+
targetKey = dashesCamelCase(key);
|
|
256
|
+
|
|
257
|
+
if (targetKey !== key) {
|
|
258
|
+
addEntry(targetKey);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
break;
|
|
262
|
+
|
|
263
|
+
case 'dashesOnly':
|
|
264
|
+
addEntry(dashesCamelCase(key));
|
|
265
|
+
break;
|
|
266
|
+
|
|
267
|
+
case 'asIs':
|
|
268
|
+
default:
|
|
269
|
+
addEntry(key);
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return items.join(',\n');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function getExportCode(exportItems, onlyLocals) {
|
|
277
|
+
if (exportItems.length === 0) {
|
|
278
|
+
return '';
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return `// Exports\n${onlyLocals ? 'module.exports' : 'exports.locals'} = {\n${exportItems.join(',\n')}\n};`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function getIcssReplacer(item, loaderContext, importPrefix, onlyLocals) {
|
|
285
|
+
const importUrl = importPrefix + (0, _loaderUtils.urlToRequest)(item.url);
|
|
286
|
+
return () => onlyLocals ? `" + require(${(0, _loaderUtils.stringifyRequest)(loaderContext, importUrl)})[${JSON.stringify(item.export)}] + "` : `" + require(${(0, _loaderUtils.stringifyRequest)(loaderContext, importUrl)}).locals[${JSON.stringify(item.export)}] + "`;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function prepareCode(file, messages, loaderContext, importPrefix, onlyLocals) {
|
|
290
|
+
const {
|
|
291
|
+
apiCode,
|
|
292
|
+
importCode
|
|
293
|
+
} = file;
|
|
294
|
+
let {
|
|
295
|
+
moduleCode,
|
|
296
|
+
exportCode
|
|
297
|
+
} = file;
|
|
298
|
+
messages.filter(message => message.type === 'icss-import' || message.type === 'import' && message.importType === 'url').forEach(message => {
|
|
299
|
+
// Replace all urls on `require`
|
|
300
|
+
if (message.type === 'import') {
|
|
301
|
+
const {
|
|
302
|
+
placeholder
|
|
303
|
+
} = message;
|
|
304
|
+
|
|
305
|
+
if (moduleCode) {
|
|
306
|
+
// eslint-disable-next-line no-param-reassign
|
|
307
|
+
moduleCode = moduleCode.replace(new RegExp(placeholder, 'g'), () => `" + ${placeholder} + "`);
|
|
308
|
+
}
|
|
309
|
+
} // Replace external ICSS import on `require`
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
if (message.type === 'icss-import') {
|
|
313
|
+
const {
|
|
314
|
+
item
|
|
315
|
+
} = message;
|
|
316
|
+
const replacer = getIcssReplacer(item, loaderContext, importPrefix, onlyLocals);
|
|
317
|
+
|
|
318
|
+
if (moduleCode) {
|
|
319
|
+
// eslint-disable-next-line no-param-reassign
|
|
320
|
+
moduleCode = moduleCode.replace(new RegExp(`___CSS_LOADER_IMPORT___(${item.index})___`, 'g'), replacer);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (exportCode) {
|
|
324
|
+
// eslint-disable-next-line no-param-reassign
|
|
325
|
+
exportCode = exportCode.replace(new RegExp(`___CSS_LOADER_IMPORT___(${item.index})___`, 'g'), replacer);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
return [apiCode, importCode, moduleCode, exportCode].filter(Boolean).join('');
|
|
97
330
|
}
|