-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
loader.js
183 lines (167 loc) · 5.32 KB
/
loader.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
const loaderUtils = require('loader-utils');
const processCss = require('./processCss');
const { getImportPrefix, compileExports } = require('./utils');
module.exports = function loader(content, map) {
const callback = this.async();
const options = loaderUtils.getOptions(this) || {};
const sourceMap = options.sourceMap || false;
/* eslint-disable no-param-reassign */
if (sourceMap) {
if (map) {
if (typeof map === 'string') {
map = JSON.stringify(map);
}
if (map.sources) {
map.sources = map.sources.map((source) => source.replace(/\\/g, '/'));
map.sourceRoot = '';
}
}
} else {
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
map = null;
}
/* eslint-enable no-param-reassign */
processCss(
content,
map,
{
loaderContext: this,
loaderOptions: options,
sourceMap,
},
(err, result) => {
if (err) {
return callback(err);
}
let cssAsString = JSON.stringify(result.source);
// for importing CSS
const importUrlPrefix = getImportPrefix(this, options);
const alreadyImported = {};
const importJs = result.importItems
.map((imp) => {
// fixes #781 when importing `url(filename.css )`
// eslint-disable-next-line no-param-reassign
imp.url = imp.url.trim();
return imp;
})
.filter((imp) => {
if (!imp.mediaQuery) {
if (alreadyImported[imp.url]) {
return false;
}
alreadyImported[imp.url] = true;
}
return true;
})
.map((imp) => {
if (!loaderUtils.isUrlRequest(imp.url)) {
return `exports.push([module.id, ${JSON.stringify(
`@import url(${imp.url});`
)}, ${JSON.stringify(imp.mediaQuery)}]);`;
}
const importUrl = importUrlPrefix + imp.url;
return `exports.i(require(${loaderUtils.stringifyRequest(
this,
importUrl
)}), ${JSON.stringify(imp.mediaQuery)});`;
}, this)
.join('\n');
function importItemMatcher(item) {
const match = result.importItemRegExp.exec(item);
const idx = +match[1];
const importItem = result.importItems[idx];
const importUrl = importUrlPrefix + importItem.url;
return `" + require(${loaderUtils.stringifyRequest(
this,
importUrl
)}).locals[${JSON.stringify(importItem.export)}] + "`;
}
cssAsString = cssAsString.replace(
result.importItemRegExpG,
importItemMatcher.bind(this)
);
// helper for ensuring valid CSS strings from requires
let urlEscapeHelper = '';
if (options.url !== false && result.urlItems.length > 0) {
urlEscapeHelper = `var escape = require(${loaderUtils.stringifyRequest(
this,
require.resolve('./runtime/escape.js')
)});\n`;
cssAsString = cssAsString.replace(result.urlItemRegExpG, (item) => {
const match = result.urlItemRegExp.exec(item);
let idx = +match[1];
const urlItem = result.urlItems[idx];
const { url } = urlItem;
idx = url.indexOf('?#');
if (idx < 0) {
idx = url.indexOf('#');
}
let urlRequest;
if (idx > 0) {
// idx === 0 is catched by isUrlRequest
// in cases like url('webfont.eot?#iefix')
urlRequest = url.substr(0, idx);
return `" + escape(require(${loaderUtils.stringifyRequest(
this,
urlRequest
)}) + "${url.substr(idx)}") + "`;
}
urlRequest = url;
return `" + escape(require(${loaderUtils.stringifyRequest(
this,
urlRequest
)})) + "`;
});
}
let exportJs = compileExports(
result,
importItemMatcher.bind(this),
options.camelCase
);
if (exportJs) {
exportJs = `exports.locals = ${exportJs};`;
}
let moduleJs;
if (sourceMap && result.map) {
/* eslint-disable no-param-reassign */
// Add a SourceMap
({ map } = result);
if (map.sources) {
map.sources = map.sources.map(
(source) =>
source
.split('!')
.pop()
.replace(/\\/g, '/'),
this
);
map.sourceRoot = '';
}
map.file = map.file
.split('!')
.pop()
.replace(/\\/g, '/');
map = JSON.stringify(map);
/* eslint-enable no-param-reassign */
moduleJs = `exports.push([module.id, ${cssAsString}, "", ${map}]);`;
} else {
moduleJs = `exports.push([module.id, ${cssAsString}, ""]);`;
}
// embed runtime
return callback(
null,
`${urlEscapeHelper}exports = module.exports = require(${loaderUtils.stringifyRequest(
this,
require.resolve('./runtime/api.js')
)})(${sourceMap});\n` +
`// imports\n${importJs}\n\n` +
`// module\n${moduleJs}\n\n` +
`// exports\n${exportJs}`
);
}
);
};