-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjadelet-react-preprocessor.js
62 lines (61 loc) · 1.91 KB
/
jadelet-react-preprocessor.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
58
59
60
61
62
import { parse } from '@babel/parser';
import * as t from '@babel/types';
export default function JadeletReactPreprocessor() {
return {
visitor: {
Program(path, { file }) {
path.node.body.unshift(
t.importDeclaration(
[
t.importSpecifier(
t.identifier('runtime'),
t.identifier('runtime')
),
t.importSpecifier(
t.identifier('useForceUpdate'),
t.identifier('useForceUpdate')
)
],
t.stringLiteral('../')
)
);
},
ExpressionStatement(path) {
if (!t.isAssignmentExpression(path.node.expression)) return;
const { left, right } = path.node.expression;
if (
t.isMemberExpression(left) &&
left.object.name === "module" &&
left.property.name === "exports" &&
t.isFunctionExpression(right)
) {
path.replaceWith(
t.exportDefaultDeclaration(
t.functionDeclaration(
t.identifier('template'),
right.params,
right.body,
right.generator,
right.async
)
)
);
path.insertAfter(parse(`
export function ReactWrapper(props) {
const presenter = Presenter();
const root = template({ ...presenter, ...props });
const forceUpdate = useForceUpdate();
React.useEffect(() => {
root.observed.forEach(observable => observable.observe(forceUpdate));
return () => {
root.observed.forEach(observable => observable.stopObserving(forceUpdate));
};
});
return root.root;
}
`, { sourceType: 'module', plugins: [] }).program.body);
}
}
}
};
}