-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathprocessCss.js
110 lines (101 loc) · 3.12 KB
/
processCss.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
const postcss = require('postcss');
const loaderUtils = require('loader-utils');
const localByDefault = require('postcss-modules-local-by-default');
const extractImports = require('postcss-modules-extract-imports');
const modulesScope = require('postcss-modules-scope');
const modulesValues = require('postcss-modules-values');
const cssLoaderParser = require('./postcss-css-loader-parser');
const Warning = require('./Warning');
const CssSyntaxError = require('./CssSyntaxError');
const { getLocalIdent } = require('./utils');
module.exports = function processCss(inputSource, inputMap, options, callback) {
const { query } = options;
const { context, localIdentRegExp } = query;
const localIdentName = query.localIdentName || '[hash:base64]';
const customGetLocalIdent = query.getLocalIdent || getLocalIdent;
const parserOptions = {
mode: options.mode,
url: query.url !== false,
import: query.import !== false,
resolve: options.resolve,
};
const pipeline = postcss([
modulesValues,
localByDefault({
mode: options.mode,
rewriteUrl(global, url) {
if (parserOptions.url) {
// eslint-disable-next-line no-param-reassign
url = url.trim();
if (
!url.replace(/\s/g, '').length ||
!loaderUtils.isUrlRequest(url)
) {
return url;
}
if (global) {
return loaderUtils.urlToRequest(url);
}
}
return url;
},
}),
extractImports(),
modulesScope({
generateScopedName: function generateScopedName(exportName) {
return customGetLocalIdent(
options.loaderContext,
localIdentName,
exportName,
{
regExp: localIdentRegExp,
hashPrefix: query.hashPrefix || '',
context,
}
);
},
}),
cssLoaderParser(parserOptions),
]);
pipeline
.process(inputSource, {
// we need a prefix to avoid path rewriting of PostCSS
from: `/css-loader!${options.from}`,
to: options.to,
map: options.sourceMap
? {
prev: inputMap,
sourcesContent: true,
inline: false,
annotation: false,
}
: null,
})
.then((result) => {
result
.warnings()
.forEach((warning) =>
options.loaderContext.emitWarning(new Warning(warning))
);
callback(null, {
source: result.css,
map: result.map && result.map.toJSON(),
exports: parserOptions.exports,
importItems: parserOptions.importItems,
importItemRegExpG: /___CSS_LOADER_IMPORT___([0-9]+)___/g,
importItemRegExp: /___CSS_LOADER_IMPORT___([0-9]+)___/,
urlItems: parserOptions.urlItems,
urlItemRegExpG: /___CSS_LOADER_URL___([0-9]+)___/g,
urlItemRegExp: /___CSS_LOADER_URL___([0-9]+)___/,
});
})
.catch((error) => {
callback(
error.name === 'CssSyntaxError' ? new CssSyntaxError(error) : error
);
});
};