-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (37 loc) · 1.1 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
const assert = require('assert');
const { createMacro, MacroError } = require('babel-plugin-macros');
const parseCSS = require('./parse-css');
module.exports = createMacro(cssToJSMacro, {
configName: 'cssToJS'
});
const IMPORT_NAMES = ['css', 'keyframes'];
function replaceReferences(references) {
if (!references) return;
// TODO references are currently replaced by plain nodes during
// transformation. Iterating backwards is a temporary solution
// Also affects keyframes defined in CSS tag
references
.slice()
.reverse()
.forEach(ref => {
assert(
ref.parentPath.isTaggedTemplateExpression(),
ref.parentPath.buildCodeFrameError(
'Import must be called as a tagged template literal'
)
);
parseCSS(ref.parentPath);
});
}
function cssToJSMacro({ references }) {
for (const name in references) {
assert(
IMPORT_NAMES.includes(name),
new MacroError(
`Import ${name} not supported. Use ${IMPORT_NAMES.join(' or ')}`
)
);
}
replaceReferences(references.keyframes);
replaceReferences(references.css);
}