forked from solidjs/solid-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel.js
418 lines (412 loc) · 20.1 KB
/
babel.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
'use strict';
var t = require('@babel/types');
var generator = require('@babel/generator');
var helperModuleImports = require('@babel/helper-module-imports');
var crypto = require('crypto');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var t__namespace = /*#__PURE__*/_interopNamespace(t);
var generator__default = /*#__PURE__*/_interopDefaultLegacy(generator);
var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto);
function isComponentishName(name) {
return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
}
function getModuleIdentifier(hooks, path, name, mod) {
const target = `${mod}[${name}]`;
const current = hooks.get(target);
if (current) {
return current;
}
const newID = helperModuleImports.addNamed(path, name, mod);
hooks.set(target, newID);
return newID;
}
function getSolidRefreshIdentifier(hooks, path, name) {
return getModuleIdentifier(hooks, path, name, "solid-refresh");
}
function isESMHMR(bundler) {
// The currently known ESM HMR implementations
// esm - the original ESM HMR Spec
// vite - Vite's implementation
return bundler === "esm" || bundler === "vite";
}
function getHotIdentifier(bundler) {
// vite/esm uses `import.meta.hot`
if (isESMHMR(bundler)) {
return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier("import"), t__namespace.identifier("meta")), t__namespace.identifier("hot"));
}
// webpack 5 uses `import.meta.webpackHot`
if (bundler === "webpack5") {
return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier("import"), t__namespace.identifier("meta")), t__namespace.identifier("webpackHot"));
}
// `module.hot` is the default.
return t__namespace.memberExpression(t__namespace.identifier("module"), t__namespace.identifier("hot"));
}
function getStatementPath(path) {
if (t__namespace.isStatement(path.node)) {
return path;
}
if (path.parentPath) {
return getStatementPath(path.parentPath);
}
return null;
}
function createHotMap(hooks, path, name) {
const current = hooks.get(name);
if (current) {
return current;
}
const newID = t__namespace.identifier(name);
path.insertBefore(t__namespace.exportNamedDeclaration(t__namespace.variableDeclaration("const", [t__namespace.variableDeclarator(newID, t__namespace.objectExpression([]))])));
hooks.set(name, newID);
return newID;
}
function createSignatureValue(node) {
const code = generator__default["default"](node);
const result = crypto__default["default"].createHash("sha256").update(code.code).digest("base64");
return result;
}
function isForeignBinding(source, current, name) {
if (source === current) {
return true;
}
if (current.scope.hasOwnBinding(name)) {
return false;
}
if (current.parentPath) {
return isForeignBinding(source, current.parentPath, name);
}
return true;
}
function createHotSignature(component, sign, deps) {
if (sign && deps) {
return t__namespace.objectExpression([
t__namespace.objectProperty(t__namespace.identifier("component"), component),
t__namespace.objectProperty(t__namespace.identifier("id"), t__namespace.stringLiteral(component.name)),
t__namespace.objectProperty(t__namespace.identifier("signature"), sign),
t__namespace.objectProperty(t__namespace.identifier("dependencies"), t__namespace.arrayExpression(deps))
]);
}
return t__namespace.objectExpression([
t__namespace.objectProperty(t__namespace.identifier("component"), component),
t__namespace.objectProperty(t__namespace.identifier("id"), t__namespace.stringLiteral(component.name))
]);
}
function getBindings(path) {
const identifiers = new Set();
path.traverse({
Expression(p) {
if (t__namespace.isIdentifier(p.node) &&
!t__namespace.isTypeScript(p.parentPath.node) &&
isForeignBinding(path, p, p.node.name)) {
identifiers.add(p.node.name);
}
if (t__namespace.isJSXElement(p.node) && t__namespace.isJSXMemberExpression(p.node.openingElement.name)) {
let base = p.node.openingElement.name;
while (t__namespace.isJSXMemberExpression(base)) {
base = base.object;
}
if (isForeignBinding(path, p, base.name)) {
identifiers.add(base.name);
}
}
}
});
return [...identifiers].map(value => t__namespace.identifier(value));
}
function createStandardHot(path, state, HotComponent, rename) {
const HotImport = getSolidRefreshIdentifier(state.hooks, path, "standard");
const pathToHot = getHotIdentifier(state.opts.bundler);
const statementPath = getStatementPath(path);
if (statementPath) {
statementPath.insertBefore(rename);
}
return t__namespace.callExpression(HotImport, [
createHotSignature(HotComponent, state.granular.value ? t__namespace.stringLiteral(createSignatureValue(rename)) : undefined, state.granular.value ? getBindings(path) : undefined),
pathToHot
]);
}
function createESMHot(path, state, HotComponent, rename) {
const HotImport = getSolidRefreshIdentifier(state.hooks, path, "esm");
const pathToHot = getHotIdentifier(state.opts.bundler);
const handlerId = path.scope.generateUidIdentifier("handler");
const componentId = path.scope.generateUidIdentifier("Component");
const statementPath = getStatementPath(path);
if (statementPath) {
const registrationMap = createHotMap(state.hooks, statementPath, "$$registrations");
statementPath.insertBefore(rename);
statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.assignmentExpression("=", t__namespace.memberExpression(registrationMap, HotComponent), createHotSignature(HotComponent, state.granular.value ? t__namespace.stringLiteral(createSignatureValue(rename)) : undefined, state.granular.value ? getBindings(path) : undefined))));
statementPath.insertBefore(t__namespace.variableDeclaration("const", [
t__namespace.variableDeclarator(t__namespace.objectPattern([
t__namespace.objectProperty(t__namespace.identifier("handler"), handlerId, false, true),
t__namespace.objectProperty(t__namespace.identifier("Component"), componentId, false, true)
]), t__namespace.callExpression(HotImport, [
t__namespace.memberExpression(registrationMap, HotComponent),
t__namespace.unaryExpression("!", t__namespace.unaryExpression("!", pathToHot))
]))
]));
const mod = path.scope.generateUidIdentifier("mod");
statementPath.insertBefore(t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("accept")), [
t__namespace.arrowFunctionExpression([mod], t__namespace.blockStatement([
t__namespace.expressionStatement(t__namespace.logicalExpression("&&", t__namespace.callExpression(handlerId, [
// Vite interprets this differently
state.opts.bundler === "esm"
? t__namespace.memberExpression(mod, t__namespace.identifier("module"))
: mod
]), t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("invalidate")), [])))
]))
]))));
}
return componentId;
}
function createHot(path, state, name, expression) {
const HotComponent = name
? path.scope.generateUidIdentifier(`Hot$$${name.name}`)
: path.scope.generateUidIdentifier("HotComponent");
const rename = t__namespace.variableDeclaration("const", [t__namespace.variableDeclarator(HotComponent, expression)]);
if (isESMHMR(state.opts.bundler)) {
return createESMHot(path, state, HotComponent, rename);
}
return createStandardHot(path, state, HotComponent, rename);
}
const SOURCE_MODULE = "solid-js/web";
function isValidSpecifier(specifier, keyword) {
return ((t__namespace.isIdentifier(specifier.imported) && specifier.imported.name === keyword) ||
(t__namespace.isStringLiteral(specifier.imported) && specifier.imported.value === keyword));
}
function captureValidIdentifiers(path) {
const validIdentifiers = new Set();
path.traverse({
ImportDeclaration(p) {
if (p.node.source.value === SOURCE_MODULE) {
for (let i = 0, len = p.node.specifiers.length; i < len; i += 1) {
const specifier = p.node.specifiers[i];
if (t__namespace.isImportSpecifier(specifier) &&
(isValidSpecifier(specifier, "render") || isValidSpecifier(specifier, "hydrate"))) {
validIdentifiers.add(specifier.local);
}
}
}
}
});
return validIdentifiers;
}
function captureValidNamespaces(path) {
const validNamespaces = new Set();
path.traverse({
ImportDeclaration(p) {
if (p.node.source.value === SOURCE_MODULE) {
for (let i = 0, len = p.node.specifiers.length; i < len; i += 1) {
const specifier = p.node.specifiers[i];
if (t__namespace.isImportNamespaceSpecifier(specifier)) {
validNamespaces.add(specifier.local);
}
}
}
}
});
return validNamespaces;
}
function isValidCallee(path, { callee }, validIdentifiers, validNamespaces) {
if (t__namespace.isIdentifier(callee)) {
const binding = path.scope.getBinding(callee.name);
return binding && validIdentifiers.has(binding.identifier);
}
if (t__namespace.isMemberExpression(callee) &&
!callee.computed &&
t__namespace.isIdentifier(callee.object) &&
t__namespace.isIdentifier(callee.property)) {
const binding = path.scope.getBinding(callee.object.name);
return (binding &&
validNamespaces.has(binding.identifier) &&
(callee.property.name === "render" || callee.property.name === "hydrate"));
}
return false;
}
function checkValidRenderCall(path) {
let currentPath = path.parentPath;
while (currentPath) {
if (t__namespace.isProgram(currentPath.node)) {
return true;
}
if (!t__namespace.isStatement(currentPath.node)) {
return false;
}
currentPath = currentPath.parentPath;
}
return false;
}
function fixRenderCalls(path, opts) {
const validIdentifiers = captureValidIdentifiers(path);
const validNamespaces = captureValidNamespaces(path);
path.traverse({
ExpressionStatement(p) {
if (t__namespace.isCallExpression(p.node.expression) &&
checkValidRenderCall(p) &&
isValidCallee(p, p.node.expression, validIdentifiers, validNamespaces)) {
// Replace with variable declaration
const id = p.scope.generateUidIdentifier("cleanup");
p.replaceWith(t__namespace.variableDeclaration("const", [t__namespace.variableDeclarator(id, p.node.expression)]));
const pathToHot = getHotIdentifier(opts.bundler);
p.insertAfter(t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("dispose")), [id]))));
p.skip();
}
}
});
}
function getHMRDecline(opts, pathToHot) {
if (isESMHMR(opts.bundler)) {
return t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), [])));
}
if (opts.bundler === "webpack5") {
return t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), [])));
}
return t__namespace.ifStatement(pathToHot, t__namespace.expressionStatement(t__namespace.conditionalExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier("decline")), []), t__namespace.callExpression(t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier("window"), t__namespace.identifier("location")), t__namespace.identifier("reload")), []))));
}
function createDevWarning(path, hooks, opts) {
path.pushContainer("body", t__namespace.ifStatement(t__namespace.callExpression(getModuleIdentifier(hooks, path, "shouldWarnAndDecline", "solid-refresh"), []), getHMRDecline(opts, getHotIdentifier(opts.bundler))));
}
function solidRefreshPlugin() {
return {
name: "Solid Refresh",
pre() {
this.hooks = new Map();
this.processed = {
value: false
};
this.granular = {
value: false
};
},
visitor: {
Program(path, { file, opts, processed, granular, hooks }) {
var _a;
let shouldReload = false;
let shouldSkip = false;
const comments = file.ast.comments;
if (comments) {
for (let i = 0; i < comments.length; i++) {
const comment = comments[i].value;
if (/^\s*@refresh granular\s*$/.test(comment)) {
granular.value = true;
break;
}
if (/^\s*@refresh skip\s*$/.test(comment)) {
processed.value = true;
shouldSkip = true;
break;
}
if (/^\s*@refresh reload\s*$/.test(comment)) {
processed.value = true;
shouldReload = true;
const pathToHot = getHotIdentifier(opts.bundler);
path.pushContainer("body", getHMRDecline(opts, pathToHot));
break;
}
}
}
if (!shouldReload && ((_a = opts.fixRender) !== null && _a !== void 0 ? _a : true)) {
fixRenderCalls(path, opts);
}
if (!shouldSkip) {
createDevWarning(path, hooks, opts);
}
},
ExportNamedDeclaration(path, state) {
if (state.processed.value) {
return;
}
const decl = path.node.declaration;
// Check if declaration is FunctionDeclaration
if (t__namespace.isFunctionDeclaration(decl) &&
!(decl.generator || decl.async) &&
// Might be component-like, but the only valid components
// have zero or one parameter
decl.params.length < 2) {
// Check if the declaration has an identifier, and then check
// if the name is component-ish
if (decl.id && isComponentishName(decl.id.name)) {
path.node.declaration = t__namespace.variableDeclaration("const", [
t__namespace.variableDeclarator(decl.id, createHot(path, state, decl.id, t__namespace.functionExpression(decl.id, decl.params, decl.body)))
]);
}
}
},
VariableDeclarator(path, state) {
var _a, _b;
if (state.processed.value) {
return;
}
const grandParentNode = (_b = (_a = path.parentPath) === null || _a === void 0 ? void 0 : _a.parentPath) === null || _b === void 0 ? void 0 : _b.node;
// Check if the parent of the VariableDeclaration
// is either a Program or an ExportNamedDeclaration
if (t__namespace.isProgram(grandParentNode) || t__namespace.isExportNamedDeclaration(grandParentNode)) {
const identifier = path.node.id;
const init = path.node.init;
if (t__namespace.isIdentifier(identifier) &&
isComponentishName(identifier.name) &&
// Check for valid FunctionExpression
((t__namespace.isFunctionExpression(init) && !(init.async || init.generator)) ||
// Check for valid ArrowFunctionExpression
(t__namespace.isArrowFunctionExpression(init) && !(init.async || init.generator))) &&
// Might be component-like, but the only valid components
// have zero or one parameter
init.params.length < 2) {
path.node.init = createHot(path, state, identifier, init);
}
}
},
FunctionDeclaration(path, state) {
if (state.processed.value) {
return;
}
if (!(t__namespace.isProgram(path.parentPath.node) || t__namespace.isExportDefaultDeclaration(path.parentPath.node))) {
return;
}
const decl = path.node;
// Check if declaration is FunctionDeclaration
if (!(decl.generator || decl.async) &&
// Might be component-like, but the only valid components
// have zero or one parameter
decl.params.length < 2) {
// Check if the declaration has an identifier, and then check
// if the name is component-ish
if (decl.id && isComponentishName(decl.id.name)) {
const replacement = createHot(path, state, decl.id, t__namespace.functionExpression(decl.id, decl.params, decl.body));
if (t__namespace.isExportDefaultDeclaration(path.parentPath.node)) {
path.replaceWith(replacement);
}
else {
path.replaceWith(t__namespace.variableDeclaration("var", [t__namespace.variableDeclarator(decl.id, replacement)]));
}
}
else if (!decl.id &&
decl.params.length === 1 &&
t__namespace.isIdentifier(decl.params[0]) &&
decl.params[0].name === "props" &&
t__namespace.isExportDefaultDeclaration(path.parentPath.node)) {
const replacement = createHot(path, state, undefined, t__namespace.functionExpression(null, decl.params, decl.body));
path.replaceWith(replacement);
}
}
}
}
};
}
module.exports = solidRefreshPlugin;