forked from webpack-contrib/css-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (143 loc) · 4.46 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
145
146
147
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var csso = require("csso");
var SourceNode = require("source-map").SourceNode;
var loaderUtils = require("loader-utils");
module.exports = function(content) {
this.cacheable && this.cacheable();
var result = [];
var query = loaderUtils.parseQuery(this.query);
var root = query.root;
if(root !== undefined && root !== false && typeof root !== "string")
throw new Error("Invalid value to query parameter root");
var tree = csso.parse(content, "stylesheet");
if(tree && this && this.minimize) {
tree = csso.compress(tree);
tree = csso.cleanInfo(tree);
}
if(tree) {
var imports = extractImports(tree);
annotateUrls(tree);
imports.forEach(function(imp) {
if(/^data:|^(https?:)?\/\//.test(imp.url)) {
result.push(JSON.stringify("@import url(" + imp.url + ")" + imp.media.join("") + ";"));
} else {
if(imp.media.length > 0) {
result.push(JSON.stringify("@media " + imp.media.join("") + "{"));
}
result.push("require(" + JSON.stringify("!" + __filename + "!" + urlToRequire(imp.url)) + ")");
if(imp.media.length > 0) {
result.push(JSON.stringify("}"));
}
}
});
}
var css = JSON.stringify(tree ? csso.translate(tree) : "");
var uriRegExp = /%CSSURL\[%(.*?)%\]CSSURL%/g;
css = css.replace(uriRegExp, function(str) {
var match = /^%CSSURL\[%(.*?)%\]CSSURL%$/.exec(str);
if(/^data:|^(https?:)?\/\//.test(match[1])) return match[1];
if((root === undefined || root === false) && /^\//.test(match[1])) return match[1];
var idx = match[1].indexOf("?");
if(idx < 0) idx = match[1].indexOf("#");
if(idx > 0) {
// in cases like url('webfont.eot?#iefix')
var url = JSON.parse("\"" + match[1].substr(0, idx) + "\"");
return "\"+require(" + JSON.stringify(urlToRequire(url, root)) + ")+\"" + match[1].substr(idx);
} else if(idx === 0) {
// only hash
return match[1];
}
var url = JSON.parse("\"" + match[1] + "\"");
return "\"+require(" + JSON.stringify(urlToRequire(url, root)) + ")+\"";
});
result.push(css);
var cssRequest = loaderUtils.getRemainingRequest(this);
var node = new SourceNode(1, 0,
cssRequest,
"module.exports =\n\t" + result.join(" +\n\t") + ";");
var stringWithMap = node.toStringWithSourceMap({
file: loaderUtils.getCurrentRequest(this)
});
stringWithMap.map.setSourceContent(cssRequest, content);
this.callback(null, stringWithMap.code, stringWithMap.map.toJSON());
}
function urlToRequire(url, root) {
if(/^~/.test(url))
return url.substring(1);
else if(root !== undefined && /^\//.test(url))
return root.replace(/\/$/, '')+'/'+url.replace(/^\//, '');
else
return "./"+url;
}
function extractImports(tree) {
var results = [];
var removes = [];
for(var i = 1; i < tree.length; i++) {
var rule = tree[i];
if(rule[0] === "atrules" &&
rule[1][0] === "atkeyword" &&
rule[1][1][0] === "ident" &&
rule[1][1][1] === "import") {
var imp = {
url: null,
media: []
};
for(var j = 2; j < rule.length; j++) {
var item = rule[j];
if(item[0] === "string") {
imp.url = JSON.parse(item[1]);
} else if(item[0] === "uri") {
imp.url = item[1][0] === "string" ? JSON.parse(item[1][1]) : item[1][1];
} else if(item[0] === "ident" && item[1] !== "url") {
imp.media.push(csso.translate(item));
} else if(item[0] !== "s" || imp.media.length > 0) {
imp.media.push(csso.translate(item));
}
}
while(imp.media.length > 0 &&
/^\s*$/.test(imp.media[imp.media.length-1]))
imp.media.pop();
if(imp.url !== null) {
results.push(imp);
removes.push(i);
}
}
}
removes.reverse().forEach(function(i) {
tree.splice(i, 1);
});
return results;
}
function annotateUrls(tree) {
function iterateChildren() {
for(var i = 1; i < tree.length; i++) {
annotateUrls(tree[i]);
}
}
switch(tree[0]) {
case "stylesheet": return iterateChildren();
case "ruleset": return iterateChildren();
case "block": return iterateChildren();
case "atruleb": return iterateChildren();
case "atruler": return iterateChildren();
case "atrulers": return iterateChildren();
case "declaration": return iterateChildren();
case "value": return iterateChildren();
case "uri":
for(var i = 1; i < tree.length; i++) {
var item = tree[i];
switch(item[0]) {
case "ident":
case "raw":
item[1] = "%CSSURL[%" + item[1] + "%]CSSURL%";
return;
case "string":
item[1] = "%CSSURL[%" + item[1].substring(1, item[1].length-1) + "%]CSSURL%";
return;
}
}
}
}