-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathindex.js
144 lines (126 loc) · 3.59 KB
/
index.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import validateOptions from 'schema-utils';
import postcss from 'postcss';
import postcssPkg from 'postcss/package.json';
import { getOptions, isUrlRequest } from 'loader-utils';
import schema from './options.json';
import { importParser, icssParser, urlParser } from './plugins';
import {
normalizeSourceMap,
getModulesPlugins,
getFilter,
getImportCode,
getModuleCode,
getExportCode,
} from './utils';
import Warning from './Warning';
import CssSyntaxError from './CssSyntaxError';
export default function loader(content, map, meta) {
const options = getOptions(this) || {};
validateOptions(schema, options, {
name: 'CSS Loader',
baseDataPath: 'options',
});
const callback = this.async();
const sourceMap = options.sourceMap || false;
const plugins = [];
if (options.modules) {
plugins.push(...getModulesPlugins(options, this));
}
const exportType = options.onlyLocals ? 'locals' : 'full';
plugins.push(icssParser());
if (options.import !== false && exportType === 'full') {
plugins.push(
importParser({
filter: getFilter(options.import, this.resourcePath),
})
);
}
if (options.url !== false && exportType === 'full') {
plugins.push(
urlParser({
filter: getFilter(options.url, this.resourcePath, (value) =>
isUrlRequest(value)
),
})
);
}
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
if (meta) {
const { ast } = meta;
if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) {
// eslint-disable-next-line no-param-reassign
content = ast.root;
}
}
postcss(plugins)
.process(content, {
from: this.remainingRequest.split('!').pop(),
to: this.currentRequest.split('!').pop(),
map: options.sourceMap
? {
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
prev: sourceMap && map ? normalizeSourceMap(map) : null,
inline: false,
annotation: false,
}
: false,
})
.then((result) => {
result
.warnings()
.forEach((warning) => this.emitWarning(new Warning(warning)));
const imports = [];
const exports = [];
const replacers = [];
for (const message of result.messages) {
// eslint-disable-next-line default-case
switch (message.type) {
case 'import':
imports.push(message.value);
break;
case 'export':
exports.push(message.value);
break;
case 'replacer':
replacers.push(message.value);
break;
}
}
const { importLoaders, localsConvention } = options;
const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : false;
const importCode = getImportCode(
this,
imports,
exportType,
sourceMap,
importLoaders,
esModule
);
const moduleCode = getModuleCode(
this,
result,
exportType,
sourceMap,
replacers
);
const exportCode = getExportCode(
this,
exports,
exportType,
replacers,
localsConvention,
esModule
);
return callback(null, [importCode, moduleCode, exportCode].join(''));
})
.catch((error) => {
callback(
error.name === 'CssSyntaxError' ? new CssSyntaxError(error) : error
);
});
}