|
1 | 1 | /*
|
2 |
| - MIT License http://www.opensource.org/licenses/mit-license.php |
3 |
| - Author Tobias Koppers @sokra |
4 |
| -*/ |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Tobias Koppers @sokra |
| 4 | + */ |
5 | 5 | var formatCodeFrame = require("babel-code-frame");
|
6 |
| -var Tokenizer = require("css-selector-tokenizer"); |
7 | 6 | var postcss = require("postcss");
|
8 | 7 | var loaderUtils = require("loader-utils");
|
9 | 8 | var assign = require("object-assign");
|
@@ -42,15 +41,19 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
|
42 | 41 |
|
43 | 42 | if(options.import) {
|
44 | 43 | css.walkAtRules(/import/i, function(rule) {
|
45 |
| - var values = Tokenizer.parseValues(rule.params); |
46 |
| - var url = values.nodes[0].nodes[0]; |
47 |
| - if(url.type === "url") { |
48 |
| - url = url.url; |
49 |
| - } else if(url.type === "string") { |
50 |
| - url = url.value; |
| 44 | + var tokens = valueParser(rule.params); |
| 45 | + var importNode = tokens.nodes[0]; |
| 46 | + var url = null; |
| 47 | + if (importNode.type === "function" && importNode.value === 'url') { |
| 48 | + url = importNode.nodes[0].value; |
| 49 | + } else if(importNode.type === "string") { |
| 50 | + url = importNode.value; |
51 | 51 | } else throw rule.error("Unexpected format" + rule.params);
|
52 |
| - values.nodes[0].nodes.shift(); |
53 |
| - var mediaQuery = Tokenizer.stringifyValues(values); |
| 52 | + if (!url) { |
| 53 | + return; |
| 54 | + } |
| 55 | + tokens.nodes.shift(); |
| 56 | + var mediaQuery = tokens.toString().trim(); |
54 | 57 | if(loaderUtils.isUrlRequest(url, options.root) && options.mode === "global") {
|
55 | 58 | url = loaderUtils.urlToRequest(url, options.root);
|
56 | 59 | }
|
@@ -86,45 +89,45 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
|
86 | 89 | exports[exportName] = replaceImportsInString(exports[exportName]);
|
87 | 90 | });
|
88 | 91 |
|
89 |
| - function processNode(item) { |
90 |
| - switch (item.type) { |
91 |
| - case "value": |
92 |
| - item.nodes.forEach(processNode); |
93 |
| - break; |
94 |
| - case "nested-item": |
95 |
| - item.nodes.forEach(processNode); |
96 |
| - break; |
97 |
| - case "item": |
98 |
| - var importIndex = imports["$" + item.name]; |
| 92 | + css.walkDecls(function(decl) { |
| 93 | + var tokens = valueParser(decl.value); |
| 94 | + tokens.walk(function (node) { |
| 95 | + if (node.type === 'word') { |
| 96 | + var importIndex = imports["$" + node.value]; |
99 | 97 | if (typeof importIndex === "number") {
|
100 |
| - item.name = "___CSS_LOADER_IMPORT___" + importIndex + "___"; |
| 98 | + node.value = "___CSS_LOADER_IMPORT___" + importIndex + "___"; |
101 | 99 | }
|
102 |
| - break; |
103 |
| - case "url": |
104 |
| - if (options.url && !/^#/.test(item.url) && loaderUtils.isUrlRequest(item.url, options.root)) { |
105 |
| - // Don't remove quotes around url when contain space |
106 |
| - if (item.url.indexOf(" ") === -1) { |
107 |
| - item.stringType = ""; |
108 |
| - } |
109 |
| - delete item.innerSpacingBefore; |
110 |
| - delete item.innerSpacingAfter; |
111 |
| - var url = item.url; |
112 |
| - item.url = "___CSS_LOADER_URL___" + urlItems.length + "___"; |
113 |
| - urlItems.push({ |
114 |
| - url: url |
115 |
| - }); |
| 100 | + } |
| 101 | + |
| 102 | + if (options.url && node.type === 'function' && node.value === 'url') { |
| 103 | + delete node.before; |
| 104 | + delete node.after; |
| 105 | + if (!node.nodes) { |
| 106 | + return; |
116 | 107 | }
|
117 |
| - break; |
118 |
| - } |
119 |
| - } |
120 | 108 |
|
121 |
| - css.walkDecls(function(decl) { |
122 |
| - var values = Tokenizer.parseValues(decl.value); |
123 |
| - values.nodes.forEach(function(value) { |
124 |
| - value.nodes.forEach(processNode); |
| 109 | + node.nodes.forEach(function (nestedNode) { |
| 110 | + if (nestedNode.type !== 'word' && nestedNode.type !== 'string') { |
| 111 | + return; |
| 112 | + } |
| 113 | + var value = nestedNode.value; |
| 114 | + if (!/^#/.test(value) && loaderUtils.isUrlRequest(value, options.root)) { |
| 115 | + // Don't remove quotes around url when contain space |
| 116 | + if (value.indexOf(" ") === -1) { |
| 117 | + nestedNode.quote = ""; |
| 118 | + } |
| 119 | + var url = value; |
| 120 | + nestedNode.value = "___CSS_LOADER_URL___" + urlItems.length + "___"; |
| 121 | + urlItems.push({ |
| 122 | + url: url |
| 123 | + }); |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
125 | 127 | });
|
126 |
| - decl.value = Tokenizer.stringifyValues(values); |
| 128 | + decl.value = tokens.toString(); |
127 | 129 | });
|
| 130 | + |
128 | 131 | css.walkAtRules(function(atrule) {
|
129 | 132 | if(typeof atrule.params === "string") {
|
130 | 133 | atrule.params = replaceImportsInString(atrule.params);
|
|
0 commit comments