solid-refresh 0.6.3 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -11
- package/dist/babel.cjs +583 -286
- package/dist/babel.mjs +583 -286
- package/dist/types/src/babel/core/checks.d.ts +4 -0
- package/dist/types/src/babel/core/checks.d.ts.map +1 -0
- package/dist/types/src/babel/core/constants.d.ts +8 -0
- package/dist/types/src/babel/core/constants.d.ts.map +1 -0
- package/dist/types/src/babel/core/create-registry.d.ts +5 -0
- package/dist/types/src/babel/core/create-registry.d.ts.map +1 -0
- package/dist/types/src/babel/core/generate-unique-name.d.ts +4 -0
- package/dist/types/src/babel/core/generate-unique-name.d.ts.map +1 -0
- package/dist/types/src/babel/core/generator.d.ts +3 -0
- package/dist/types/src/babel/core/generator.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-descriptive-name.d.ts +3 -0
- package/dist/types/src/babel/core/get-descriptive-name.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-foreign-bindings.d.ts +4 -0
- package/dist/types/src/babel/core/get-foreign-bindings.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-hmr-decline-call.d.ts +4 -0
- package/dist/types/src/babel/core/get-hmr-decline-call.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-hot-identifier.d.ts +4 -0
- package/dist/types/src/babel/core/get-hot-identifier.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-import-identifier.d.ts +5 -0
- package/dist/types/src/babel/core/get-import-identifier.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-root-statement-path.d.ts +3 -0
- package/dist/types/src/babel/core/get-root-statement-path.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-statement-path.d.ts +3 -0
- package/dist/types/src/babel/core/get-statement-path.d.ts.map +1 -0
- package/dist/types/src/babel/core/get-vite-hmr-requirement.d.ts +4 -0
- package/dist/types/src/babel/core/get-vite-hmr-requirement.d.ts.map +1 -0
- package/dist/types/src/babel/core/is-valid-callee.d.ts +5 -0
- package/dist/types/src/babel/core/is-valid-callee.d.ts.map +1 -0
- package/dist/types/src/babel/core/register-import-specifiers.d.ts +5 -0
- package/dist/types/src/babel/core/register-import-specifiers.d.ts.map +1 -0
- package/dist/types/src/babel/core/transform-jsx.d.ts +4 -0
- package/dist/types/src/babel/core/transform-jsx.d.ts.map +1 -0
- package/dist/types/src/babel/core/types.d.ts +40 -0
- package/dist/types/src/babel/core/types.d.ts.map +1 -0
- package/dist/types/src/babel/core/unwrap.d.ts +11 -0
- package/dist/types/src/babel/core/unwrap.d.ts.map +1 -0
- package/dist/types/src/babel/core/xxhash32.d.ts +7 -0
- package/dist/types/src/babel/core/xxhash32.d.ts.map +1 -0
- package/dist/types/src/babel/index.d.ts +2 -25
- package/dist/types/src/babel/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/babel.cjs
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var path = require('path');
|
|
4
4
|
var t = require('@babel/types');
|
|
5
|
-
var _generator = require('@babel/generator');
|
|
6
5
|
var helperModuleImports = require('@babel/helper-module-imports');
|
|
6
|
+
var _generator = require('@babel/generator');
|
|
7
7
|
|
|
8
8
|
function _interopNamespaceDefault(e) {
|
|
9
9
|
var n = Object.create(null);
|
|
@@ -211,79 +211,244 @@ function xxHash32(buffer, seed = 0) {
|
|
|
211
211
|
return acc < 0 ? acc + 4294967296 : acc;
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
-
// https://github.com/babel/babel/issues/15269
|
|
215
|
-
let generator;
|
|
216
|
-
if (typeof _generator !== 'function') {
|
|
217
|
-
generator = _generator.default;
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
generator = _generator;
|
|
221
|
-
}
|
|
222
|
-
const CWD = process.cwd();
|
|
223
|
-
function getFile(filename) {
|
|
224
|
-
return path.relative(CWD, filename);
|
|
225
|
-
}
|
|
226
214
|
// This is just a Pascal heuristic
|
|
227
215
|
// we only assume a function is a component
|
|
228
216
|
// if the first character is in uppercase
|
|
229
217
|
function isComponentishName(name) {
|
|
230
218
|
return name[0] >= 'A' && name[0] <= 'Z';
|
|
231
219
|
}
|
|
232
|
-
function
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return
|
|
220
|
+
function getImportSpecifierName(specifier) {
|
|
221
|
+
if (t__namespace.isIdentifier(specifier.imported)) {
|
|
222
|
+
return specifier.imported.name;
|
|
223
|
+
}
|
|
224
|
+
return specifier.imported.value;
|
|
237
225
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
context: '$$context',
|
|
246
|
-
decline: '$$decline',
|
|
247
|
-
};
|
|
248
|
-
function getSolidRefreshIdentifier(state, path, name) {
|
|
249
|
-
const target = `${name}`;
|
|
250
|
-
const current = state.hooks.get(target);
|
|
251
|
-
if (current) {
|
|
252
|
-
return current;
|
|
226
|
+
|
|
227
|
+
function registerImportSpecifier(state, id, specifier) {
|
|
228
|
+
if (t__namespace.isImportDefaultSpecifier(specifier)) {
|
|
229
|
+
if (id.definition.kind === 'default') {
|
|
230
|
+
state.registrations.identifiers.set(specifier.local, id);
|
|
231
|
+
}
|
|
232
|
+
return;
|
|
253
233
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
234
|
+
if (t__namespace.isImportSpecifier(specifier)) {
|
|
235
|
+
if ((id.definition.kind === 'named' &&
|
|
236
|
+
getImportSpecifierName(specifier) === id.definition.name) ||
|
|
237
|
+
(id.definition.kind === 'default' &&
|
|
238
|
+
getImportSpecifierName(specifier) === 'default')) {
|
|
239
|
+
state.registrations.identifiers.set(specifier.local, id);
|
|
240
|
+
}
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
let current = state.registrations.namespaces.get(specifier.local);
|
|
244
|
+
if (!current) {
|
|
245
|
+
current = [];
|
|
246
|
+
}
|
|
247
|
+
current.push(id);
|
|
248
|
+
state.registrations.namespaces.set(specifier.local, current);
|
|
257
249
|
}
|
|
250
|
+
function registerImportSpecifiers(state, path, definitions) {
|
|
251
|
+
for (let i = 0, len = definitions.length; i < len; i++) {
|
|
252
|
+
const id = definitions[i];
|
|
253
|
+
if (path.node.source.value === id.definition.source) {
|
|
254
|
+
for (let k = 0, klen = path.node.specifiers.length; k < klen; k++) {
|
|
255
|
+
registerImportSpecifier(state, id, path.node.specifiers[k]);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
258
261
|
function getHotIdentifier(state) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
switch (state.bundler) {
|
|
263
|
+
// vite/esm uses `import.meta.hot`
|
|
264
|
+
case 'esm':
|
|
265
|
+
case 'vite':
|
|
266
|
+
return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier('import'), t__namespace.identifier('meta')), t__namespace.identifier('hot'));
|
|
267
|
+
// webpack 5 uses `import.meta.webpackHot`
|
|
268
|
+
// rspack does as well
|
|
269
|
+
case 'webpack5':
|
|
270
|
+
case 'rspack-esm':
|
|
271
|
+
return t__namespace.memberExpression(t__namespace.memberExpression(t__namespace.identifier('import'), t__namespace.identifier('meta')), t__namespace.identifier('webpackHot'));
|
|
272
|
+
default:
|
|
273
|
+
// `module.hot` is the default.
|
|
274
|
+
return t__namespace.memberExpression(t__namespace.identifier('module'), t__namespace.identifier('hot'));
|
|
263
275
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function getImportIdentifier(state, path, registration) {
|
|
279
|
+
const name = registration.kind === 'named' ? registration.name : 'default';
|
|
280
|
+
const target = `${registration.source}[${name}]`;
|
|
281
|
+
const current = state.imports.get(target);
|
|
282
|
+
if (current) {
|
|
283
|
+
return current;
|
|
268
284
|
}
|
|
269
|
-
|
|
270
|
-
|
|
285
|
+
const newID = registration.kind === 'named'
|
|
286
|
+
? helperModuleImports.addNamed(path, registration.name, registration.source)
|
|
287
|
+
: helperModuleImports.addDefault(path, registration.source);
|
|
288
|
+
state.imports.set(target, newID);
|
|
289
|
+
return newID;
|
|
271
290
|
}
|
|
272
|
-
|
|
273
|
-
|
|
291
|
+
|
|
292
|
+
// Source of solid-refresh (for import)
|
|
293
|
+
const SOLID_REFRESH_MODULE = 'solid-refresh';
|
|
294
|
+
// Exported names from solid-refresh that will be imported
|
|
295
|
+
const IMPORT_REGISTRY = {
|
|
296
|
+
kind: 'named',
|
|
297
|
+
name: '$$registry',
|
|
298
|
+
source: SOLID_REFRESH_MODULE,
|
|
299
|
+
};
|
|
300
|
+
const IMPORT_REFRESH = {
|
|
301
|
+
kind: 'named',
|
|
302
|
+
name: '$$refresh',
|
|
303
|
+
source: SOLID_REFRESH_MODULE,
|
|
304
|
+
};
|
|
305
|
+
const IMPORT_COMPONENT = {
|
|
306
|
+
kind: 'named',
|
|
307
|
+
name: '$$component',
|
|
308
|
+
source: SOLID_REFRESH_MODULE,
|
|
309
|
+
};
|
|
310
|
+
const IMPORT_CONTEXT = {
|
|
311
|
+
kind: 'named',
|
|
312
|
+
name: '$$context',
|
|
313
|
+
source: SOLID_REFRESH_MODULE,
|
|
314
|
+
};
|
|
315
|
+
const IMPORT_DECLINE = {
|
|
316
|
+
kind: 'named',
|
|
317
|
+
name: '$$decline',
|
|
318
|
+
source: SOLID_REFRESH_MODULE,
|
|
319
|
+
};
|
|
320
|
+
const IMPORT_SPECIFIERS = [
|
|
321
|
+
{
|
|
322
|
+
type: 'render',
|
|
323
|
+
definition: { name: 'render', kind: 'named', source: 'solid-js/web' },
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
type: 'render',
|
|
327
|
+
definition: { name: 'hydrate', kind: 'named', source: 'solid-js/web' },
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
type: 'createContext',
|
|
331
|
+
definition: {
|
|
332
|
+
name: 'createContext',
|
|
333
|
+
kind: 'named',
|
|
334
|
+
source: 'solid-js',
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
type: 'createContext',
|
|
339
|
+
definition: {
|
|
340
|
+
name: 'createContext',
|
|
341
|
+
kind: 'named',
|
|
342
|
+
source: 'solid-js/web',
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
];
|
|
346
|
+
|
|
347
|
+
function generateViteHMRRequirement(state, statements, pathToHot) {
|
|
348
|
+
if (state.bundler === 'vite') {
|
|
274
349
|
// Vite requires that the owner module has an `import.meta.hot.accept()` call
|
|
275
350
|
statements.push(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(pathToHot, t__namespace.identifier('accept')), [])));
|
|
276
351
|
}
|
|
277
352
|
}
|
|
353
|
+
|
|
278
354
|
function getHMRDeclineCall(state, path) {
|
|
279
|
-
var _a;
|
|
280
355
|
const pathToHot = getHotIdentifier(state);
|
|
281
356
|
const statements = [
|
|
282
|
-
t__namespace.expressionStatement(t__namespace.callExpression(
|
|
357
|
+
t__namespace.expressionStatement(t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_DECLINE), [
|
|
358
|
+
t__namespace.stringLiteral(state.bundler),
|
|
359
|
+
pathToHot,
|
|
360
|
+
])),
|
|
283
361
|
];
|
|
284
|
-
|
|
362
|
+
generateViteHMRRequirement(state, statements, pathToHot);
|
|
285
363
|
return t__namespace.ifStatement(pathToHot, t__namespace.blockStatement(statements));
|
|
286
364
|
}
|
|
365
|
+
|
|
366
|
+
function isPathValid(path, key) {
|
|
367
|
+
return key(path.node);
|
|
368
|
+
}
|
|
369
|
+
function isNestedExpression(node) {
|
|
370
|
+
switch (node.type) {
|
|
371
|
+
case 'ParenthesizedExpression':
|
|
372
|
+
case 'TypeCastExpression':
|
|
373
|
+
case 'TSAsExpression':
|
|
374
|
+
case 'TSSatisfiesExpression':
|
|
375
|
+
case 'TSNonNullExpression':
|
|
376
|
+
case 'TSTypeAssertion':
|
|
377
|
+
case 'TSInstantiationExpression':
|
|
378
|
+
return true;
|
|
379
|
+
default:
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
function unwrapNode(node, key) {
|
|
384
|
+
if (key(node)) {
|
|
385
|
+
return node;
|
|
386
|
+
}
|
|
387
|
+
if (isNestedExpression(node)) {
|
|
388
|
+
return unwrapNode(node.expression, key);
|
|
389
|
+
}
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function isIdentifierValidCallee(state, path, callee, target) {
|
|
394
|
+
const binding = path.scope.getBindingIdentifier(callee.name);
|
|
395
|
+
if (binding) {
|
|
396
|
+
const result = state.registrations.identifiers.get(binding);
|
|
397
|
+
if (result && result.type === target) {
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
function isPropertyValidCallee(result, target, propName) {
|
|
404
|
+
for (let i = 0, len = result.length; i < len; i++) {
|
|
405
|
+
const registration = result[i];
|
|
406
|
+
if (registration.type === target) {
|
|
407
|
+
if (registration.definition.kind === 'named') {
|
|
408
|
+
if (registration.definition.name === propName) {
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
else if (propName === 'default') {
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
function isMemberExpressionValidCallee(state, path, member, target) {
|
|
420
|
+
if (!t__namespace.isIdentifier(member.property)) {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
const trueObject = unwrapNode(member.object, t__namespace.isIdentifier);
|
|
424
|
+
if (!trueObject) {
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
const binding = path.scope.getBindingIdentifier(trueObject.name);
|
|
428
|
+
if (!binding) {
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
const result = state.registrations.namespaces.get(binding);
|
|
432
|
+
if (!result) {
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
return isPropertyValidCallee(result, target, member.property.name);
|
|
436
|
+
}
|
|
437
|
+
function isValidCallee(state, path, { callee }, target) {
|
|
438
|
+
if (t__namespace.isV8IntrinsicIdentifier(callee)) {
|
|
439
|
+
return false;
|
|
440
|
+
}
|
|
441
|
+
const trueCallee = unwrapNode(callee, t__namespace.isIdentifier);
|
|
442
|
+
if (trueCallee) {
|
|
443
|
+
return isIdentifierValidCallee(state, path, trueCallee, target);
|
|
444
|
+
}
|
|
445
|
+
const trueMember = unwrapNode(callee, t__namespace.isMemberExpression);
|
|
446
|
+
if (trueMember && !trueMember.computed) {
|
|
447
|
+
return isMemberExpressionValidCallee(state, path, trueMember, target);
|
|
448
|
+
}
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
|
|
287
452
|
function getStatementPath(path) {
|
|
288
453
|
if (t__namespace.isStatement(path.node)) {
|
|
289
454
|
return path;
|
|
@@ -293,40 +458,46 @@ function getStatementPath(path) {
|
|
|
293
458
|
}
|
|
294
459
|
return null;
|
|
295
460
|
}
|
|
461
|
+
|
|
462
|
+
function getRootStatementPath(path) {
|
|
463
|
+
let current = path.parentPath;
|
|
464
|
+
while (current) {
|
|
465
|
+
const next = current.parentPath;
|
|
466
|
+
if (next && t__namespace.isProgram(next.node)) {
|
|
467
|
+
return current;
|
|
468
|
+
}
|
|
469
|
+
current = next;
|
|
470
|
+
}
|
|
471
|
+
return path;
|
|
472
|
+
}
|
|
473
|
+
|
|
296
474
|
const REGISTRY = 'REGISTRY';
|
|
297
475
|
function createRegistry(state, path) {
|
|
298
|
-
|
|
299
|
-
const current = state.hooks.get(REGISTRY);
|
|
476
|
+
const current = state.imports.get(REGISTRY);
|
|
300
477
|
if (current) {
|
|
301
478
|
return current;
|
|
302
479
|
}
|
|
303
|
-
const
|
|
304
|
-
const identifier =
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
init: t__namespace.callExpression(getSolidRefreshIdentifier(state, path, IMPORTS.registry), []),
|
|
309
|
-
});
|
|
480
|
+
const root = getRootStatementPath(path);
|
|
481
|
+
const identifier = path.scope.generateUidIdentifier(REGISTRY);
|
|
482
|
+
root.insertBefore(t__namespace.variableDeclaration('const', [
|
|
483
|
+
t__namespace.variableDeclarator(identifier, t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_REGISTRY), [])),
|
|
484
|
+
]));
|
|
310
485
|
const pathToHot = getHotIdentifier(state);
|
|
311
486
|
const statements = [
|
|
312
|
-
t__namespace.expressionStatement(t__namespace.callExpression(
|
|
313
|
-
t__namespace.stringLiteral(
|
|
487
|
+
t__namespace.expressionStatement(t__namespace.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
|
|
488
|
+
t__namespace.stringLiteral(state.bundler),
|
|
314
489
|
pathToHot,
|
|
315
490
|
identifier,
|
|
316
491
|
])),
|
|
317
492
|
];
|
|
318
|
-
|
|
319
|
-
|
|
493
|
+
generateViteHMRRequirement(state, statements, pathToHot);
|
|
494
|
+
path.scope.getProgramParent().path.pushContainer('body', [
|
|
320
495
|
t__namespace.ifStatement(pathToHot, t__namespace.blockStatement(statements)),
|
|
321
496
|
]);
|
|
322
|
-
state.
|
|
497
|
+
state.imports.set(REGISTRY, identifier);
|
|
323
498
|
return identifier;
|
|
324
499
|
}
|
|
325
|
-
|
|
326
|
-
const code = generator(node);
|
|
327
|
-
const result = xxHash32(code.code).toString(16);
|
|
328
|
-
return result;
|
|
329
|
-
}
|
|
500
|
+
|
|
330
501
|
function isForeignBinding(source, current, name) {
|
|
331
502
|
if (source === current) {
|
|
332
503
|
return true;
|
|
@@ -349,13 +520,15 @@ function isInTypescript(path) {
|
|
|
349
520
|
}
|
|
350
521
|
return false;
|
|
351
522
|
}
|
|
352
|
-
function
|
|
523
|
+
function getForeignBindings(path) {
|
|
353
524
|
const identifiers = new Set();
|
|
354
525
|
path.traverse({
|
|
355
526
|
ReferencedIdentifier(p) {
|
|
356
527
|
// Check identifiers that aren't in a TS expression
|
|
357
528
|
if (!isInTypescript(p) && isForeignBinding(path, p, p.node.name)) {
|
|
358
|
-
|
|
529
|
+
if (p.isIdentifier() || p.parentPath.isJSXMemberExpression()) {
|
|
530
|
+
identifiers.add(p.node.name);
|
|
531
|
+
}
|
|
359
532
|
}
|
|
360
533
|
},
|
|
361
534
|
});
|
|
@@ -365,149 +538,266 @@ function getBindings(path) {
|
|
|
365
538
|
}
|
|
366
539
|
return collected;
|
|
367
540
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
source: 'solid-js/web',
|
|
380
|
-
},
|
|
381
|
-
{ type: 'render', name: 'render', kind: 'named', source: 'solid-js/web' },
|
|
382
|
-
{ type: 'render', name: 'hydrate', kind: 'named', source: 'solid-js/web' },
|
|
383
|
-
];
|
|
384
|
-
function getImportSpecifierName(specifier) {
|
|
385
|
-
switch (specifier.imported.type) {
|
|
386
|
-
case 'Identifier':
|
|
387
|
-
return specifier.imported.name;
|
|
388
|
-
case 'StringLiteral':
|
|
389
|
-
return specifier.imported.value;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
function registerImportSpecifier(state, id, specifier) {
|
|
393
|
-
switch (specifier.type) {
|
|
394
|
-
case 'ImportDefaultSpecifier': {
|
|
395
|
-
if (id.kind === 'default') {
|
|
396
|
-
state.registrations.identifiers.set(specifier.local, id);
|
|
541
|
+
|
|
542
|
+
function getDescriptiveName(path, defaultName) {
|
|
543
|
+
let current = path;
|
|
544
|
+
while (current) {
|
|
545
|
+
switch (current.node.type) {
|
|
546
|
+
case 'FunctionDeclaration':
|
|
547
|
+
case 'FunctionExpression': {
|
|
548
|
+
if (current.node.id) {
|
|
549
|
+
return current.node.id.name;
|
|
550
|
+
}
|
|
551
|
+
break;
|
|
397
552
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
(id.kind === 'default' &&
|
|
404
|
-
getImportSpecifierName(specifier) === 'default')) {
|
|
405
|
-
state.registrations.identifiers.set(specifier.local, id);
|
|
553
|
+
case 'VariableDeclarator': {
|
|
554
|
+
if (current.node.id.type === 'Identifier') {
|
|
555
|
+
return current.node.id.name;
|
|
556
|
+
}
|
|
557
|
+
break;
|
|
406
558
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
559
|
+
case 'ClassPrivateMethod':
|
|
560
|
+
case 'ClassMethod':
|
|
561
|
+
case 'ObjectMethod': {
|
|
562
|
+
switch (current.node.key.type) {
|
|
563
|
+
case 'Identifier':
|
|
564
|
+
return current.node.key.name;
|
|
565
|
+
case 'PrivateName':
|
|
566
|
+
return current.node.key.id.name;
|
|
567
|
+
}
|
|
568
|
+
break;
|
|
413
569
|
}
|
|
414
|
-
current.push(id);
|
|
415
|
-
state.registrations.namespaces.set(specifier.local, current);
|
|
416
|
-
break;
|
|
417
570
|
}
|
|
571
|
+
current = current.parentPath;
|
|
418
572
|
}
|
|
573
|
+
return defaultName;
|
|
419
574
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
575
|
+
|
|
576
|
+
function generateUniqueName(path, name) {
|
|
577
|
+
let uid;
|
|
578
|
+
let i = 1;
|
|
579
|
+
do {
|
|
580
|
+
uid = name + '_' + i;
|
|
581
|
+
i++;
|
|
582
|
+
} while (path.scope.hasLabel(uid) ||
|
|
583
|
+
path.scope.hasBinding(uid) ||
|
|
584
|
+
path.scope.hasGlobal(uid) ||
|
|
585
|
+
path.scope.hasReference(uid));
|
|
586
|
+
const program = path.scope.getProgramParent();
|
|
587
|
+
program.references[uid] = true;
|
|
588
|
+
program.uids[uid] = true;
|
|
589
|
+
return t__namespace.identifier(uid);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const REFRESH_JSX_SKIP = /^\s*@refresh jsx-skip\s*$/;
|
|
593
|
+
function shouldSkipJSX(node) {
|
|
594
|
+
// Node without leading comments shouldn't be skipped
|
|
595
|
+
if (node.leadingComments) {
|
|
596
|
+
for (let i = 0, len = node.leadingComments.length; i < len; i++) {
|
|
597
|
+
if (REFRESH_JSX_SKIP.test(node.leadingComments[i].value)) {
|
|
598
|
+
return true;
|
|
426
599
|
}
|
|
427
600
|
}
|
|
428
601
|
}
|
|
602
|
+
return false;
|
|
429
603
|
}
|
|
430
|
-
function
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
604
|
+
function skippableJSX(node) {
|
|
605
|
+
return t__namespace.addComment(node, 'leading', '@refresh jsx-skip');
|
|
606
|
+
}
|
|
607
|
+
function pushAttribute(state, replacement) {
|
|
608
|
+
const key = 'v' + state.attributes.length;
|
|
609
|
+
state.attributes.push(t__namespace.jsxAttribute(t__namespace.jsxIdentifier(key), t__namespace.jsxExpressionContainer(replacement)));
|
|
610
|
+
return key;
|
|
611
|
+
}
|
|
612
|
+
function pushAttributeAndReplace(state, target, replacement) {
|
|
613
|
+
const key = pushAttribute(state, replacement);
|
|
614
|
+
target.replaceWith(t__namespace.memberExpression(state.props, t__namespace.identifier(key)));
|
|
615
|
+
}
|
|
616
|
+
function extractJSXExpressionFromNormalAttribute(state, attr) {
|
|
617
|
+
const value = attr.get('value');
|
|
618
|
+
if (isPathValid(value, t__namespace.isJSXElement) ||
|
|
619
|
+
isPathValid(value, t__namespace.isJSXFragment)) {
|
|
620
|
+
value.replaceWith(t__namespace.jsxExpressionContainer(value.node));
|
|
621
|
+
}
|
|
622
|
+
if (isPathValid(value, t__namespace.isJSXExpressionContainer)) {
|
|
623
|
+
extractJSXExpressionsFromJSXExpressionContainer(state, value);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
function extractJSXExpressionFromRef(state, attr) {
|
|
627
|
+
const value = attr.get('value');
|
|
628
|
+
if (isPathValid(value, t__namespace.isJSXExpressionContainer)) {
|
|
629
|
+
const expr = value.get('expression');
|
|
630
|
+
if (isPathValid(expr, t__namespace.isExpression)) {
|
|
631
|
+
const unwrappedIdentifier = unwrapNode(expr.node, t__namespace.isIdentifier);
|
|
632
|
+
let replacement;
|
|
633
|
+
if (unwrappedIdentifier) {
|
|
634
|
+
const arg = expr.scope.generateUidIdentifier('arg');
|
|
635
|
+
replacement = t__namespace.arrowFunctionExpression([arg], t__namespace.blockStatement([
|
|
636
|
+
t__namespace.ifStatement(t__namespace.binaryExpression('===', t__namespace.unaryExpression('typeof', unwrappedIdentifier), t__namespace.stringLiteral('function')), t__namespace.blockStatement([
|
|
637
|
+
t__namespace.expressionStatement(t__namespace.callExpression(unwrappedIdentifier, [arg])),
|
|
638
|
+
]), t__namespace.blockStatement([
|
|
639
|
+
t__namespace.expressionStatement(t__namespace.assignmentExpression('=', unwrappedIdentifier, arg)),
|
|
640
|
+
])),
|
|
641
|
+
]));
|
|
435
642
|
}
|
|
436
|
-
|
|
437
|
-
|
|
643
|
+
else {
|
|
644
|
+
replacement = expr.node;
|
|
645
|
+
}
|
|
646
|
+
pushAttributeAndReplace(state, expr, replacement);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
438
649
|
}
|
|
439
|
-
function
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
case 'TSAsExpression':
|
|
444
|
-
case 'TSSatisfiesExpression':
|
|
445
|
-
case 'TSNonNullExpression':
|
|
446
|
-
case 'TSInstantiationExpression':
|
|
447
|
-
case 'TSTypeAssertion':
|
|
448
|
-
return unwrapExpression(node.expression, key);
|
|
449
|
-
default:
|
|
450
|
-
return key(node) ? node : undefined;
|
|
650
|
+
function extractJSXExpressionFromUseDirective(state, id, attr) {
|
|
651
|
+
const value = attr.get('value');
|
|
652
|
+
if (isPathValid(value, t__namespace.isJSXExpressionContainer)) {
|
|
653
|
+
extractJSXExpressionsFromJSXExpressionContainer(state, value);
|
|
451
654
|
}
|
|
655
|
+
const key = pushAttribute(state, t__namespace.identifier(id.name));
|
|
656
|
+
state.vars.push(t__namespace.variableDeclarator(t__namespace.identifier(id.name), t__namespace.memberExpression(state.props, t__namespace.identifier(key))));
|
|
452
657
|
}
|
|
453
|
-
function
|
|
454
|
-
const
|
|
455
|
-
if (
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
658
|
+
function extractJSXExpressionFromAttribute(state, attr) {
|
|
659
|
+
const key = attr.get('name');
|
|
660
|
+
if (isPathValid(key, t__namespace.isJSXIdentifier)) {
|
|
661
|
+
if (key.node.name === 'ref') {
|
|
662
|
+
extractJSXExpressionFromRef(state, attr);
|
|
663
|
+
}
|
|
664
|
+
else {
|
|
665
|
+
extractJSXExpressionFromNormalAttribute(state, attr);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
else if (isPathValid(key, t__namespace.isJSXNamespacedName)) {
|
|
669
|
+
if (key.node.namespace.name === 'use') {
|
|
670
|
+
extractJSXExpressionFromUseDirective(state, key.node.name, attr);
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
extractJSXExpressionFromNormalAttribute(state, attr);
|
|
459
674
|
}
|
|
460
675
|
}
|
|
461
|
-
return false;
|
|
462
676
|
}
|
|
463
|
-
function
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
return true;
|
|
475
|
-
}
|
|
677
|
+
function extractJSXExpressionsFromAttributes(state, path) {
|
|
678
|
+
const openingElement = path.get('openingElement');
|
|
679
|
+
const attrs = openingElement.get('attributes');
|
|
680
|
+
for (let i = 0, len = attrs.length; i < len; i++) {
|
|
681
|
+
const attr = attrs[i];
|
|
682
|
+
if (isPathValid(attr, t__namespace.isJSXAttribute)) {
|
|
683
|
+
extractJSXExpressionFromAttribute(state, attr);
|
|
684
|
+
}
|
|
685
|
+
if (isPathValid(attr, t__namespace.isJSXSpreadAttribute)) {
|
|
686
|
+
const arg = attr.get('argument');
|
|
687
|
+
pushAttributeAndReplace(state, arg, arg.node);
|
|
476
688
|
}
|
|
477
689
|
}
|
|
478
|
-
return false;
|
|
479
690
|
}
|
|
480
|
-
function
|
|
481
|
-
if (
|
|
482
|
-
return
|
|
691
|
+
function convertJSXOpeningToExpression(node) {
|
|
692
|
+
if (t__namespace.isJSXIdentifier(node)) {
|
|
693
|
+
return t__namespace.identifier(node.name);
|
|
483
694
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
695
|
+
return t__namespace.memberExpression(convertJSXOpeningToExpression(node.object), convertJSXOpeningToExpression(node.property));
|
|
696
|
+
}
|
|
697
|
+
function extractJSXExpressionsFromJSXElement(state, path) {
|
|
698
|
+
const openingElement = path.get('openingElement');
|
|
699
|
+
const openingName = openingElement.get('name');
|
|
700
|
+
if ((isPathValid(openingName, t__namespace.isJSXIdentifier) &&
|
|
701
|
+
/^[A-Z_]/.test(openingName.node.name)) ||
|
|
702
|
+
isPathValid(openingName, t__namespace.isJSXMemberExpression)) {
|
|
703
|
+
const key = pushAttribute(state, convertJSXOpeningToExpression(openingName.node));
|
|
704
|
+
const replacement = t__namespace.jsxMemberExpression(t__namespace.jsxIdentifier(state.props.name), t__namespace.jsxIdentifier(key));
|
|
705
|
+
openingName.replaceWith(replacement);
|
|
706
|
+
const closingElement = path.get('closingElement');
|
|
707
|
+
if (isPathValid(closingElement, t__namespace.isJSXClosingElement)) {
|
|
708
|
+
closingElement.get('name').replaceWith(replacement);
|
|
709
|
+
}
|
|
487
710
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
711
|
+
extractJSXExpressionsFromAttributes(state, path);
|
|
712
|
+
}
|
|
713
|
+
function extractJSXExpressionsFromJSXExpressionContainer(state, child) {
|
|
714
|
+
const expr = child.get('expression');
|
|
715
|
+
if (isPathValid(expr, t__namespace.isExpression)) {
|
|
716
|
+
pushAttributeAndReplace(state, expr, expr.node);
|
|
491
717
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
718
|
+
}
|
|
719
|
+
function extractJSXExpressionsFromJSXSpreadChild(state, child) {
|
|
720
|
+
const arg = child.get('expression');
|
|
721
|
+
pushAttributeAndReplace(state, arg, arg.node);
|
|
722
|
+
}
|
|
723
|
+
function extractJSXExpressions(state, path) {
|
|
724
|
+
if (isPathValid(path, t__namespace.isJSXElement)) {
|
|
725
|
+
extractJSXExpressionsFromJSXElement(state, path);
|
|
726
|
+
}
|
|
727
|
+
const children = path.get('children');
|
|
728
|
+
for (let i = 0, len = children.length; i < len; i++) {
|
|
729
|
+
const child = children[i];
|
|
730
|
+
if (isPathValid(child, t__namespace.isJSXElement) ||
|
|
731
|
+
isPathValid(child, t__namespace.isJSXFragment)) {
|
|
732
|
+
extractJSXExpressions(state, child);
|
|
733
|
+
}
|
|
734
|
+
else if (isPathValid(child, t__namespace.isJSXExpressionContainer)) {
|
|
735
|
+
extractJSXExpressionsFromJSXExpressionContainer(state, child);
|
|
736
|
+
}
|
|
737
|
+
else if (isPathValid(child, t__namespace.isJSXSpreadChild)) {
|
|
738
|
+
extractJSXExpressionsFromJSXSpreadChild(state, child);
|
|
739
|
+
}
|
|
495
740
|
}
|
|
496
|
-
return isPropertyValidCallee(result, target, member.property.name);
|
|
497
741
|
}
|
|
498
|
-
function
|
|
499
|
-
if (
|
|
500
|
-
return
|
|
742
|
+
function transformJSX(path) {
|
|
743
|
+
if (shouldSkipJSX(path.node)) {
|
|
744
|
+
return;
|
|
501
745
|
}
|
|
502
|
-
const
|
|
503
|
-
|
|
504
|
-
|
|
746
|
+
const state = {
|
|
747
|
+
props: path.scope.generateUidIdentifier('props'),
|
|
748
|
+
attributes: [],
|
|
749
|
+
vars: [],
|
|
750
|
+
};
|
|
751
|
+
extractJSXExpressions(state, path);
|
|
752
|
+
const descriptiveName = getDescriptiveName(path, 'template');
|
|
753
|
+
const id = generateUniqueName(path, isComponentishName(descriptiveName)
|
|
754
|
+
? descriptiveName
|
|
755
|
+
: 'JSX_' + descriptiveName);
|
|
756
|
+
const rootPath = getRootStatementPath(path);
|
|
757
|
+
let template = skippableJSX(t__namespace.cloneNode(path.node));
|
|
758
|
+
if (state.vars.length) {
|
|
759
|
+
template = t__namespace.blockStatement([
|
|
760
|
+
t__namespace.variableDeclaration('const', state.vars),
|
|
761
|
+
t__namespace.returnStatement(template),
|
|
762
|
+
]);
|
|
505
763
|
}
|
|
506
|
-
const
|
|
507
|
-
if (
|
|
508
|
-
|
|
764
|
+
const templateComp = t__namespace.arrowFunctionExpression([state.props], template);
|
|
765
|
+
if (path.node.loc) {
|
|
766
|
+
templateComp.loc = path.node.loc;
|
|
509
767
|
}
|
|
510
|
-
|
|
768
|
+
rootPath.insertBefore(t__namespace.variableDeclaration('const', [t__namespace.variableDeclarator(id, templateComp)]));
|
|
769
|
+
path.replaceWith(skippableJSX(t__namespace.jsxElement(t__namespace.jsxOpeningElement(t__namespace.jsxIdentifier(id.name), [...state.attributes], true), t__namespace.jsxClosingElement(t__namespace.jsxIdentifier(id.name)), [], true)));
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// https://github.com/babel/babel/issues/15269
|
|
773
|
+
let generator;
|
|
774
|
+
if (typeof _generator !== 'function') {
|
|
775
|
+
generator = _generator.default;
|
|
776
|
+
}
|
|
777
|
+
else {
|
|
778
|
+
generator = _generator;
|
|
779
|
+
}
|
|
780
|
+
function generateCode(node) {
|
|
781
|
+
return generator(node).code;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
const CWD = process.cwd();
|
|
785
|
+
function getFile(filename) {
|
|
786
|
+
return path.relative(CWD, filename);
|
|
787
|
+
}
|
|
788
|
+
function createSignatureValue(node) {
|
|
789
|
+
const code = generateCode(node);
|
|
790
|
+
const result = xxHash32(code).toString(16);
|
|
791
|
+
return result;
|
|
792
|
+
}
|
|
793
|
+
function captureIdentifiers(state, path) {
|
|
794
|
+
path.traverse({
|
|
795
|
+
ImportDeclaration(p) {
|
|
796
|
+
if (p.node.importKind === 'value') {
|
|
797
|
+
registerImportSpecifiers(state, p, state.specifiers);
|
|
798
|
+
}
|
|
799
|
+
},
|
|
800
|
+
});
|
|
511
801
|
}
|
|
512
802
|
function checkValidRenderCall(path) {
|
|
513
803
|
let currentPath = path.parentPath;
|
|
@@ -525,7 +815,7 @@ function checkValidRenderCall(path) {
|
|
|
525
815
|
function fixRenderCalls(state, path) {
|
|
526
816
|
path.traverse({
|
|
527
817
|
ExpressionStatement(p) {
|
|
528
|
-
const trueCallExpr =
|
|
818
|
+
const trueCallExpr = unwrapNode(p.node.expression, t__namespace.isCallExpression);
|
|
529
819
|
if (trueCallExpr &&
|
|
530
820
|
checkValidRenderCall(p) &&
|
|
531
821
|
isValidCallee(state, p, trueCallExpr, 'render')) {
|
|
@@ -546,7 +836,7 @@ function wrapComponent(state, path, identifier, component, original = component)
|
|
|
546
836
|
if (statementPath) {
|
|
547
837
|
const registry = createRegistry(state, statementPath);
|
|
548
838
|
const hotName = t__namespace.stringLiteral(identifier.name);
|
|
549
|
-
const componentCall =
|
|
839
|
+
const componentCall = getImportIdentifier(state, statementPath, IMPORT_COMPONENT);
|
|
550
840
|
const properties = [];
|
|
551
841
|
if (state.filename && original.loc) {
|
|
552
842
|
const filePath = getFile(state.filename);
|
|
@@ -554,7 +844,7 @@ function wrapComponent(state, path, identifier, component, original = component)
|
|
|
554
844
|
}
|
|
555
845
|
if (state.granular) {
|
|
556
846
|
properties.push(t__namespace.objectProperty(t__namespace.identifier('signature'), t__namespace.stringLiteral(createSignatureValue(component))));
|
|
557
|
-
const dependencies =
|
|
847
|
+
const dependencies = getForeignBindings(path);
|
|
558
848
|
if (dependencies.length) {
|
|
559
849
|
const dependencyKeys = [];
|
|
560
850
|
let id;
|
|
@@ -564,18 +854,12 @@ function wrapComponent(state, path, identifier, component, original = component)
|
|
|
564
854
|
}
|
|
565
855
|
properties.push(t__namespace.objectProperty(t__namespace.identifier('dependencies'), t__namespace.arrowFunctionExpression([], t__namespace.objectExpression(dependencyKeys))));
|
|
566
856
|
}
|
|
567
|
-
return t__namespace.callExpression(componentCall, [
|
|
568
|
-
registry,
|
|
569
|
-
hotName,
|
|
570
|
-
component,
|
|
571
|
-
t__namespace.objectExpression(properties),
|
|
572
|
-
]);
|
|
573
857
|
}
|
|
574
858
|
return t__namespace.callExpression(componentCall, [
|
|
575
859
|
registry,
|
|
576
860
|
hotName,
|
|
577
861
|
component,
|
|
578
|
-
|
|
862
|
+
t__namespace.objectExpression(properties),
|
|
579
863
|
]);
|
|
580
864
|
}
|
|
581
865
|
return component;
|
|
@@ -585,67 +869,44 @@ function wrapContext(state, path, identifier, context) {
|
|
|
585
869
|
if (statementPath) {
|
|
586
870
|
const registry = createRegistry(state, statementPath);
|
|
587
871
|
const hotName = t__namespace.stringLiteral(identifier.name);
|
|
588
|
-
const contextCall =
|
|
872
|
+
const contextCall = getImportIdentifier(state, statementPath, IMPORT_CONTEXT);
|
|
589
873
|
return t__namespace.callExpression(contextCall, [registry, hotName, context]);
|
|
590
874
|
}
|
|
591
875
|
return context;
|
|
592
876
|
}
|
|
593
|
-
function setupProgram(state, path) {
|
|
594
|
-
var _a;
|
|
877
|
+
function setupProgram(state, path, comments) {
|
|
595
878
|
let shouldSkip = false;
|
|
596
|
-
|
|
879
|
+
let isDone = false;
|
|
597
880
|
if (comments) {
|
|
598
881
|
for (const { value: comment } of comments) {
|
|
599
|
-
if (/^\s*@refresh granular\s*$/.test(comment)) {
|
|
600
|
-
state.granular = true;
|
|
601
|
-
break;
|
|
602
|
-
}
|
|
603
882
|
if (/^\s*@refresh skip\s*$/.test(comment)) {
|
|
604
|
-
|
|
883
|
+
isDone = true;
|
|
605
884
|
shouldSkip = true;
|
|
606
885
|
break;
|
|
607
886
|
}
|
|
608
887
|
if (/^\s*@refresh reload\s*$/.test(comment)) {
|
|
609
|
-
|
|
888
|
+
isDone = true;
|
|
610
889
|
path.pushContainer('body', getHMRDeclineCall(state, path));
|
|
611
890
|
break;
|
|
612
891
|
}
|
|
613
892
|
}
|
|
614
893
|
}
|
|
615
|
-
|
|
616
|
-
|
|
894
|
+
if (!shouldSkip && state.fixRender) {
|
|
895
|
+
captureIdentifiers(state, path);
|
|
617
896
|
fixRenderCalls(state, path);
|
|
618
897
|
}
|
|
619
|
-
|
|
620
|
-
function transformExportNamedDeclaration(state, path) {
|
|
621
|
-
if (state.processed) {
|
|
622
|
-
return;
|
|
623
|
-
}
|
|
624
|
-
const decl = path.node.declaration;
|
|
625
|
-
// Check if declaration is FunctionDeclaration
|
|
626
|
-
if (t__namespace.isFunctionDeclaration(decl) &&
|
|
627
|
-
!(decl.generator || decl.async) &&
|
|
628
|
-
// Might be component-like, but the only valid components
|
|
629
|
-
// have zero or one parameter
|
|
630
|
-
decl.params.length < 2) {
|
|
631
|
-
// Check if the declaration has an identifier, and then check
|
|
632
|
-
// if the name is component-ish
|
|
633
|
-
if (decl.id && isComponentishName(decl.id.name)) {
|
|
634
|
-
path.node.declaration = t__namespace.variableDeclaration('const', [
|
|
635
|
-
t__namespace.variableDeclarator(decl.id, wrapComponent(state, path, decl.id, t__namespace.functionExpression(decl.id, decl.params, decl.body), decl)),
|
|
636
|
-
]);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
898
|
+
return isDone;
|
|
639
899
|
}
|
|
640
900
|
function isStatementTopLevel(path) {
|
|
901
|
+
let blockParent = path.scope.getBlockParent();
|
|
641
902
|
const programParent = path.scope.getProgramParent();
|
|
642
|
-
|
|
903
|
+
// a FunctionDeclaration binding refers to itself as the block parent
|
|
904
|
+
if (blockParent.path === path) {
|
|
905
|
+
blockParent = blockParent.parent;
|
|
906
|
+
}
|
|
643
907
|
return programParent === blockParent;
|
|
644
908
|
}
|
|
645
909
|
function transformVariableDeclarator(state, path) {
|
|
646
|
-
if (state.processed) {
|
|
647
|
-
return;
|
|
648
|
-
}
|
|
649
910
|
if (path.parentPath.isVariableDeclaration() &&
|
|
650
911
|
!isStatementTopLevel(path.parentPath)) {
|
|
651
912
|
return;
|
|
@@ -656,8 +917,8 @@ function transformVariableDeclarator(state, path) {
|
|
|
656
917
|
return;
|
|
657
918
|
}
|
|
658
919
|
if (isComponentishName(identifier.name)) {
|
|
659
|
-
const trueFuncExpr =
|
|
660
|
-
|
|
920
|
+
const trueFuncExpr = unwrapNode(init, t__namespace.isFunctionExpression) ||
|
|
921
|
+
unwrapNode(init, t__namespace.isArrowFunctionExpression);
|
|
661
922
|
// Check for valid FunctionExpression or ArrowFunctionExpression
|
|
662
923
|
if (trueFuncExpr &&
|
|
663
924
|
// Must not be async or generator
|
|
@@ -669,73 +930,109 @@ function transformVariableDeclarator(state, path) {
|
|
|
669
930
|
}
|
|
670
931
|
}
|
|
671
932
|
// For `createContext` calls
|
|
672
|
-
const trueCallExpr =
|
|
933
|
+
const trueCallExpr = unwrapNode(init, t__namespace.isCallExpression);
|
|
673
934
|
if (trueCallExpr &&
|
|
674
935
|
isValidCallee(state, path, trueCallExpr, 'createContext')) {
|
|
675
936
|
path.node.init = wrapContext(state, path, identifier, trueCallExpr);
|
|
676
937
|
}
|
|
938
|
+
path.skip();
|
|
939
|
+
}
|
|
940
|
+
function transformFunctionDeclaration(state, path) {
|
|
941
|
+
if (isStatementTopLevel(path)) {
|
|
942
|
+
const decl = path.node;
|
|
943
|
+
// Check if declaration is FunctionDeclaration
|
|
944
|
+
if (
|
|
945
|
+
// Check if the declaration has an identifier, and then check
|
|
946
|
+
decl.id &&
|
|
947
|
+
// if the name is component-ish
|
|
948
|
+
isComponentishName(decl.id.name) &&
|
|
949
|
+
!(decl.generator || decl.async) &&
|
|
950
|
+
// Might be component-like, but the only valid components
|
|
951
|
+
// have zero or one parameter
|
|
952
|
+
decl.params.length < 2) {
|
|
953
|
+
path.replaceWith(t__namespace.variableDeclaration('const', [
|
|
954
|
+
t__namespace.variableDeclarator(decl.id, wrapComponent(state, path, decl.id, t__namespace.functionExpression(decl.id, decl.params, decl.body), decl)),
|
|
955
|
+
]));
|
|
956
|
+
path.skip();
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
function bubbleFunctionDeclaration(program, path) {
|
|
961
|
+
if (isStatementTopLevel(path)) {
|
|
962
|
+
const decl = path.node;
|
|
963
|
+
// Check if declaration is FunctionDeclaration
|
|
964
|
+
if (
|
|
965
|
+
// Check if the declaration has an identifier, and then check
|
|
966
|
+
decl.id &&
|
|
967
|
+
// if the name is component-ish
|
|
968
|
+
isComponentishName(decl.id.name) &&
|
|
969
|
+
!(decl.generator || decl.async) &&
|
|
970
|
+
// Might be component-like, but the only valid components
|
|
971
|
+
// have zero or one parameter
|
|
972
|
+
decl.params.length < 2) {
|
|
973
|
+
const first = program.get('body')[0];
|
|
974
|
+
const [tmp] = first.insertBefore(decl);
|
|
975
|
+
tmp.skip();
|
|
976
|
+
if (path.parentPath.isExportNamedDeclaration()) {
|
|
977
|
+
path.parentPath.replaceWith(t__namespace.exportNamedDeclaration(undefined, [
|
|
978
|
+
t__namespace.exportSpecifier(decl.id, decl.id),
|
|
979
|
+
]));
|
|
980
|
+
}
|
|
981
|
+
else if (path.parentPath.isExportDefaultDeclaration()) {
|
|
982
|
+
path.replaceWith(decl.id);
|
|
983
|
+
}
|
|
984
|
+
else {
|
|
985
|
+
path.remove();
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
}
|
|
677
989
|
}
|
|
678
990
|
function solidRefreshPlugin() {
|
|
679
991
|
return {
|
|
680
|
-
name: '
|
|
681
|
-
pre() {
|
|
682
|
-
this.hooks = new Map();
|
|
683
|
-
this.processed = false;
|
|
684
|
-
this.granular = false;
|
|
685
|
-
this.registrations = {
|
|
686
|
-
identifiers: new Map(),
|
|
687
|
-
namespaces: new Map(),
|
|
688
|
-
};
|
|
689
|
-
this.imports = [...IMPORT_IDENTITIES, ...(this.opts.imports || [])];
|
|
690
|
-
},
|
|
992
|
+
name: 'solid-refresh',
|
|
691
993
|
visitor: {
|
|
692
|
-
Program(programPath,
|
|
693
|
-
|
|
994
|
+
Program(programPath, context) {
|
|
995
|
+
var _a, _b;
|
|
996
|
+
const state = {
|
|
997
|
+
granular: (_a = context.opts.granular) !== null && _a !== void 0 ? _a : true,
|
|
998
|
+
opts: context.opts,
|
|
999
|
+
specifiers: [...IMPORT_SPECIFIERS],
|
|
1000
|
+
imports: new Map(),
|
|
1001
|
+
registrations: {
|
|
1002
|
+
identifiers: new Map(),
|
|
1003
|
+
namespaces: new Map(),
|
|
1004
|
+
},
|
|
1005
|
+
filename: context.filename,
|
|
1006
|
+
bundler: context.opts.bundler || 'standard',
|
|
1007
|
+
fixRender: (_b = context.opts.fixRender) !== null && _b !== void 0 ? _b : true,
|
|
1008
|
+
};
|
|
1009
|
+
if (setupProgram(state, programPath, context.file.ast.comments)) {
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
programPath.traverse({
|
|
1013
|
+
FunctionDeclaration(path) {
|
|
1014
|
+
bubbleFunctionDeclaration(programPath, path);
|
|
1015
|
+
},
|
|
1016
|
+
});
|
|
1017
|
+
programPath.scope.crawl();
|
|
694
1018
|
programPath.traverse({
|
|
695
|
-
|
|
696
|
-
|
|
1019
|
+
JSXElement(path) {
|
|
1020
|
+
transformJSX(path);
|
|
1021
|
+
},
|
|
1022
|
+
JSXFragment(path) {
|
|
1023
|
+
transformJSX(path);
|
|
697
1024
|
},
|
|
1025
|
+
});
|
|
1026
|
+
programPath.scope.crawl();
|
|
1027
|
+
programPath.traverse({
|
|
698
1028
|
VariableDeclarator(path) {
|
|
699
1029
|
transformVariableDeclarator(state, path);
|
|
700
1030
|
},
|
|
701
1031
|
FunctionDeclaration(path) {
|
|
702
|
-
|
|
703
|
-
return;
|
|
704
|
-
}
|
|
705
|
-
if (path.parentPath.isProgram() ||
|
|
706
|
-
path.parentPath.isExportDefaultDeclaration()) {
|
|
707
|
-
const decl = path.node;
|
|
708
|
-
// Check if declaration is FunctionDeclaration
|
|
709
|
-
if (
|
|
710
|
-
// Check if the declaration has an identifier, and then check
|
|
711
|
-
decl.id &&
|
|
712
|
-
// if the name is component-ish
|
|
713
|
-
isComponentishName(decl.id.name) &&
|
|
714
|
-
!(decl.generator || decl.async) &&
|
|
715
|
-
// Might be component-like, but the only valid components
|
|
716
|
-
// have zero or one parameter
|
|
717
|
-
decl.params.length < 2) {
|
|
718
|
-
const replacement = wrapComponent(state, path, decl.id, t__namespace.functionExpression(decl.id, decl.params, decl.body), decl);
|
|
719
|
-
const newDecl = t__namespace.variableDeclaration('var', [
|
|
720
|
-
t__namespace.variableDeclarator(decl.id, replacement),
|
|
721
|
-
]);
|
|
722
|
-
if (path.parentPath.isExportDefaultDeclaration()) {
|
|
723
|
-
const parent = path.parentPath
|
|
724
|
-
.parentPath;
|
|
725
|
-
const first = parent.get('body')[0];
|
|
726
|
-
first.insertBefore(newDecl);
|
|
727
|
-
path.replaceWith(decl.id);
|
|
728
|
-
}
|
|
729
|
-
else {
|
|
730
|
-
const parent = path.parentPath;
|
|
731
|
-
const first = parent.get('body')[0];
|
|
732
|
-
first.insertBefore(newDecl);
|
|
733
|
-
path.remove();
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
}
|
|
1032
|
+
transformFunctionDeclaration(state, path);
|
|
737
1033
|
},
|
|
738
1034
|
});
|
|
1035
|
+
programPath.scope.crawl();
|
|
739
1036
|
},
|
|
740
1037
|
},
|
|
741
1038
|
};
|