-
Notifications
You must be signed in to change notification settings - Fork 19
/
externals.js
57 lines (41 loc) · 1.68 KB
/
externals.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
"use strict";
const selector = require("postcss-selector-parser");
const parser = require("../parsers/external.js");
// Find :external(<rule> from <file>) references and update them to be
// the namespaced selector instead
module.exports = (css, { opts }) => {
const { files, resolve, from } = opts;
const process = (rule, pseudo) => {
const params = pseudo.nodes.toString();
const { source, ref } = parser.parse(params);
const { name } = ref;
const file = files[resolve(from, source)];
if(!file.exports[name]) {
throw rule.error(`Invalid external reference: ${name}`, { word : name });
}
// This was a... poor naming choice
const s = selector.selector();
file.exports[name].forEach((value) =>
s.append(selector.className({ value }))
);
const root = selector.root();
root.append(s);
pseudo.replaceWith(root);
};
css.walkRules(/:external/, (rule) => {
const externals = selector((selectors) => {
const found = [];
selectors.walkPseudos((pseudo) => {
// Need to ensure we only process :external pseudos, see #261
if(pseudo.value !== ":external") {
return;
}
// Can't replace here, see postcss/postcss-selector-parser#105
found.push(pseudo);
});
found.forEach((pseudo) => process(rule, pseudo));
});
rule.selector = externals.processSync(rule);
});
};
module.exports.postcssPlugin = "modular-css-externals";