forked from solidjs/solid-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.old.js
114 lines (113 loc) · 4.24 KB
/
babel.old.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module.exports = ({ types: t }) => {
return {
name: "Solid Refresh",
visitor: {
Program(path, { opts }) {
const comments = path.hub.file.ast.comments;
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];
const index = comment.value.indexOf("@refresh");
if (index > -1) {
if (comment.value.slice(index).includes("skip")) {
path.hub.file.metadata.processedHot = true;
return;
}
if (comment.value.slice(index).includes("reload")) {
if (opts.bundler === "vite") opts.bundler = "esm";
path.hub.file.metadata.processedHot = true;
const pathToHot =
opts.bundler !== "esm"
? t.memberExpression(t.identifier("module"), t.identifier("hot"))
: t.memberExpression(
t.memberExpression(t.identifier("import"), t.identifier("meta")),
t.identifier("hot")
);
path.pushContainer(
"body",
t.ifStatement(
pathToHot,
t.expressionStatement(
t.callExpression(t.memberExpression(pathToHot, t.identifier("decline")), [])
)
)
);
return;
}
}
}
},
ExportDefaultDeclaration(path, { opts }) {
if (path.hub.file.metadata.processedHot) return;
if (
path.hub.file.opts.parserOpts.sourceFileName &&
!path.hub.file.opts.parserOpts.sourceFileName.endsWith(".jsx") &&
!path.hub.file.opts.parserOpts.sourceFileName.endsWith(".tsx")
)
return;
if (opts.bundler === "vite") opts.bundler = "esm";
path.hub.file.metadata.processedHot = true;
const decl = path.node.declaration;
const HotComponent = t.identifier("$HotComponent");
const HotImport = t.identifier("_$hot");
const pathToHot =
opts.bundler !== "esm"
? t.memberExpression(t.identifier("module"), t.identifier("hot"))
: t.memberExpression(
t.memberExpression(t.identifier("import"), t.identifier("meta")),
t.identifier("hot")
);
const rename = t.variableDeclaration("const", [
t.variableDeclarator(
HotComponent,
t.isFunctionDeclaration(decl)
? t.functionExpression(decl.id, decl.params, decl.body)
: decl
)
]);
let replacement;
if (opts.bundler === "esm") {
const handlerId = t.identifier("_$handler");
const componentId = t.identifier("_$Component");
replacement = [
t.importDeclaration(
[t.importSpecifier(HotImport, t.identifier(opts.bundler || "standard"))],
t.stringLiteral("solid-refresh")
),
t.exportNamedDeclaration(rename),
t.variableDeclaration("const", [
t.variableDeclarator(
t.objectPattern([
t.objectProperty(handlerId, handlerId, false, true),
t.objectProperty(componentId, componentId, false, true)
]),
t.callExpression(HotImport, [
HotComponent,
t.unaryExpression("!", t.unaryExpression("!", pathToHot))
])
)
]),
t.ifStatement(
pathToHot,
t.expressionStatement(
t.callExpression(t.memberExpression(pathToHot, t.identifier("accept")), [handlerId])
)
),
t.exportDefaultDeclaration(componentId)
];
} else {
replacement = [
t.importDeclaration(
[t.importSpecifier(HotImport, t.identifier(opts.bundler || "standard"))],
t.stringLiteral("solid-refresh")
),
rename,
t.exportDefaultDeclaration(t.callExpression(HotImport, [HotComponent, pathToHot]))
];
}
path
.replaceWithMultiple(replacement)
.forEach(declaration => path.scope.registerDeclaration(declaration));
}
}
};
};