Skip to content

Commit 8150d2b

Browse files
refactor: drop icss utils
1 parent ebdaf84 commit 8150d2b

File tree

2 files changed

+33
-101
lines changed

2 files changed

+33
-101
lines changed

lib/loader.js

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -62,66 +62,48 @@ module.exports = function(content, map) {
6262
})
6363
.then(function(result) {
6464
var cssAsString = JSON.stringify(result.css);
65-
// for importing CSS
66-
var importUrlPrefix = getImportPrefix(loaderContext, query);
67-
var alreadyImported = {};
6865
var importItems = parserOptions.importItems;
69-
var importJs = importItems
70-
.filter(function(imp) {
71-
if (!imp.mediaQuery) {
72-
if (alreadyImported[imp.url]) {
73-
return false;
66+
67+
if (query.import !== false && importItems.length > 0) {
68+
var alreadyImported = {};
69+
var importJs = importItems
70+
.filter(function(imp) {
71+
if (!imp.mediaQuery) {
72+
if (alreadyImported[imp.url]) {
73+
return false;
74+
}
75+
76+
alreadyImported[imp.url] = true;
7477
}
7578

76-
alreadyImported[imp.url] = true;
77-
}
79+
return true;
80+
})
81+
.map(function(imp) {
82+
if (!loaderUtils.isUrlRequest(imp.url)) {
83+
return (
84+
"exports.push([module.id, " +
85+
JSON.stringify("@import url(" + imp.url + ");") +
86+
", " +
87+
JSON.stringify(imp.mediaQuery) +
88+
"]);"
89+
);
90+
}
91+
92+
// for importing CSS
93+
var importUrlPrefix = getImportPrefix(loaderContext, query);
94+
var importUrl = importUrlPrefix + imp.url;
7895

79-
return true;
80-
})
81-
.map(function(imp) {
82-
if (!loaderUtils.isUrlRequest(imp.url)) {
8396
return (
84-
"exports.push([module.id, " +
85-
JSON.stringify("@import url(" + imp.url + ");") +
86-
", " +
97+
"exports.i(require(" +
98+
loaderUtils.stringifyRequest(this, importUrl) +
99+
"), " +
87100
JSON.stringify(imp.mediaQuery) +
88-
"]);"
101+
");"
89102
);
90-
}
91-
92-
var importUrl = importUrlPrefix + imp.url;
93-
94-
return (
95-
"exports.i(require(" +
96-
loaderUtils.stringifyRequest(this, importUrl) +
97-
"), " +
98-
JSON.stringify(imp.mediaQuery) +
99-
");"
100-
);
101-
}, loaderContext)
102-
.join("\n");
103-
104-
function importItemMatcher(item) {
105-
var match = /___CSS_LOADER_IMPORT___([0-9]+)___/.exec(item);
106-
var idx = +match[1];
107-
var importItem = parserOptions.importItems[idx];
108-
var importUrl = importUrlPrefix + importItem.url;
109-
110-
return (
111-
'" + require(' +
112-
loaderUtils.stringifyRequest(this, importUrl) +
113-
").locals" +
114-
"[" +
115-
JSON.stringify(importItem.export) +
116-
'] + "'
117-
);
103+
}, loaderContext)
104+
.join("\n");
118105
}
119106

120-
cssAsString = cssAsString.replace(
121-
/___CSS_LOADER_IMPORT___([0-9]+)___/g,
122-
importItemMatcher
123-
);
124-
125107
// helper for ensuring valid CSS strings from requires
126108
var urlEscapeHelper = "";
127109
var urlItems = parserOptions.urlItems;

lib/plugin.js

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,12 @@ var postcss = require("postcss");
22
var valueParser = require("postcss-value-parser");
33
var Tokenizer = require("css-selector-tokenizer");
44
var loaderUtils = require("loader-utils");
5-
var icssUtils = require("icss-utils");
65

76
module.exports = postcss.plugin("css-loader-parser", function(options) {
87
return function(css) {
9-
var imports = {};
10-
var exports = {};
118
var importItems = [];
129
var urlItems = [];
1310

14-
function replaceImportsInString(str) {
15-
if (options.import) {
16-
var tokens = valueParser(str);
17-
tokens.walk(function(node) {
18-
if (node.type !== "word") {
19-
return;
20-
}
21-
var token = node.value;
22-
var importIndex = imports["$" + token];
23-
if (typeof importIndex === "number") {
24-
node.value = "___CSS_LOADER_IMPORT___" + importIndex + "___";
25-
}
26-
});
27-
return tokens.toString();
28-
}
29-
return str;
30-
}
31-
3211
if (options.import) {
3312
css.walkAtRules(/^import$/i, function(rule) {
3413
var values = Tokenizer.parseValues(rule.params);
@@ -56,23 +35,6 @@ module.exports = postcss.plugin("css-loader-parser", function(options) {
5635
});
5736
}
5837

59-
var icss = icssUtils.extractICSS(css);
60-
exports = icss.icssExports;
61-
Object.keys(icss.icssImports).forEach(function(key) {
62-
var url = loaderUtils.parseString(key);
63-
Object.keys(icss.icssImports[key]).forEach(function(prop) {
64-
imports["$" + prop] = importItems.length;
65-
importItems.push({
66-
url: url,
67-
export: icss.icssImports[key][prop]
68-
});
69-
});
70-
});
71-
72-
Object.keys(exports).forEach(function(exportName) {
73-
exports[exportName] = replaceImportsInString(exports[exportName]);
74-
});
75-
7638
function processNode(item) {
7739
switch (item.type) {
7840
case "value":
@@ -81,12 +43,6 @@ module.exports = postcss.plugin("css-loader-parser", function(options) {
8143
case "nested-item":
8244
item.nodes.forEach(processNode);
8345
break;
84-
case "item":
85-
var importIndex = imports["$" + item.name];
86-
if (typeof importIndex === "number") {
87-
item.name = "___CSS_LOADER_IMPORT___" + importIndex + "___";
88-
}
89-
break;
9046
case "url":
9147
if (
9248
options.url &&
@@ -116,14 +72,8 @@ module.exports = postcss.plugin("css-loader-parser", function(options) {
11672
});
11773
decl.value = Tokenizer.stringifyValues(values);
11874
});
119-
css.walkAtRules(function(atrule) {
120-
if (typeof atrule.params === "string") {
121-
atrule.params = replaceImportsInString(atrule.params);
122-
}
123-
});
12475

12576
options.importItems = importItems;
12677
options.urlItems = urlItems;
127-
options.exports = exports;
12878
};
12979
});

0 commit comments

Comments
 (0)