-
Notifications
You must be signed in to change notification settings - Fork 414
/
preeval.ts
46 lines (43 loc) · 1.6 KB
/
preeval.ts
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
/**
* This file is a babel preset used to transform files inside evaluators.
* It works the same as main `babel/extract` preset, but do not evaluate lazy dependencies.
*/
import { NodePath } from '@babel/traverse';
import { types } from '@babel/core';
import GenerateClassNames from '../visitors/GenerateClassNames';
import DetectStyledImportName from '../visitors/DetectStyledImportName';
import { State, StrictOptions } from '../types';
import JSXElement from './visitors/JSXElement';
import ProcessStyled from './visitors/ProcessStyled';
import ProcessCSS from './visitors/ProcessCSS';
function preeval(_babel: any, options: StrictOptions) {
return {
visitor: {
Program: {
enter(path: NodePath<types.Program>, state: State) {
// Collect all the style rules from the styles we encounter
state.queue = [];
state.rules = {};
state.index = -1;
state.dependencies = [];
state.replacements = [];
// We need our transforms to run before anything else
// So we traverse here instead of a in a visitor
path.traverse({
ImportDeclaration: (p) => DetectStyledImportName(p, state),
TaggedTemplateExpression: (p) =>
GenerateClassNames(p, state, options),
JSXElement,
});
},
},
CallExpression: ProcessStyled,
TaggedTemplateExpression: ProcessCSS, // TaggedTemplateExpression is processed before CallExpression
},
};
}
export default function preset(context: any, options: StrictOptions) {
return {
plugins: [[preeval, options]],
};
}