@sigil-dev/compiler 0.11.1 → 0.11.3
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/package.json +1 -1
- package/src/analysis/hydration.ts +1112 -1012
- package/src/babel/index.ts +24 -9
- package/src/babel/util/css.ts +217 -208
- package/src/babel/util/dead-code.ts +15 -0
- package/src/babel/util/static-env.ts +7 -6
- package/test/compiles-to-itself.test.ts +72 -0
- package/test/static-env.test.ts +8 -5
|
@@ -1,1012 +1,1112 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Which components need JavaScript in the browser, and why.
|
|
3
|
-
*
|
|
4
|
-
* A component that has no state, effects, handlers, bindings or refs, and
|
|
5
|
-
* whose every child is also like that, only produces HTML. Hydrating it means
|
|
6
|
-
* claiming nodes and attaching nothing, so it is work the browser could skip.
|
|
7
|
-
*
|
|
8
|
-
* This module only *decides and explains*. It changes no generated code; the
|
|
9
|
-
* report (`sigil explain`) and the `static` assertion use it, so the analysis
|
|
10
|
-
* can be checked against real projects before anything relies on it.
|
|
11
|
-
*
|
|
12
|
-
* The rule is conservative: **hydrate when unsure.** A wrong "static" is a
|
|
13
|
-
* page that looks right and does nothing, which is much worse than shipping a
|
|
14
|
-
* little JavaScript, so anything the analysis cannot see into counts as
|
|
15
|
-
* dynamic, and says what it could not see:
|
|
16
|
-
*
|
|
17
|
-
* - a function imported from a package, or from a project file that reads
|
|
18
|
-
* state (`calls formatDate, which reads state`)
|
|
19
|
-
* - a component from a package, from props, or `Window`/`Head` and friends
|
|
20
|
-
* - a spread, a directive, a ref
|
|
21
|
-
*
|
|
22
|
-
* Whether a particular *use* of a static component is static also depends on
|
|
23
|
-
* what the use passes it (a reactive prop makes that use dynamic). That is the
|
|
24
|
-
* use-site check, `useSiteReasons`.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
28
|
-
import { dirname, join, resolve as resolvePath } from "node:path";
|
|
29
|
-
import type { NodePath } from "@babel/core";
|
|
30
|
-
import { parseSync, types as t, traverse } from "@babel/core";
|
|
31
|
-
|
|
32
|
-
export type ReasonKind =
|
|
33
|
-
| "state"
|
|
34
|
-
| "effect"
|
|
35
|
-
| "handler"
|
|
36
|
-
| "directive"
|
|
37
|
-
| "ref"
|
|
38
|
-
| "spread"
|
|
39
|
-
| "imported-state"
|
|
40
|
-
| "imported-call"
|
|
41
|
-
| "unknown-import"
|
|
42
|
-
| "child"
|
|
43
|
-
| "unknown-child"
|
|
44
|
-
| "global"
|
|
45
|
-
| "async"
|
|
46
|
-
| "recursive"
|
|
47
|
-
| "reactive-prop"
|
|
48
|
-
| "parent-prop";
|
|
49
|
-
|
|
50
|
-
export interface Reason {
|
|
51
|
-
kind: ReasonKind;
|
|
52
|
-
line: number;
|
|
53
|
-
detail: string;
|
|
54
|
-
/** The file the reason is in, when it is not the component's own. */
|
|
55
|
-
file?: string;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* - `static`: produces only HTML.
|
|
60
|
-
* - `conditional`: static as long as the children it is given are. It renders
|
|
61
|
-
* `children`, so each use decides.
|
|
62
|
-
* - `dynamic`: needs JavaScript; `reasons` says why.
|
|
63
|
-
*/
|
|
64
|
-
export type Verdict = "static" | "conditional" | "dynamic";
|
|
65
|
-
|
|
66
|
-
export interface ComponentReport {
|
|
67
|
-
name: string;
|
|
68
|
-
file: string;
|
|
69
|
-
line: number;
|
|
70
|
-
verdict: Verdict;
|
|
71
|
-
reasons: Reason[];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Resolve an import specifier from a file to a project file, or null. */
|
|
75
|
-
export type Resolver = (spec: string, from: string) => string | null;
|
|
76
|
-
|
|
77
|
-
const EXTENSIONS = [
|
|
78
|
-
"",
|
|
79
|
-
".tsx",
|
|
80
|
-
".ts",
|
|
81
|
-
".jsx",
|
|
82
|
-
".js",
|
|
83
|
-
"/index.tsx",
|
|
84
|
-
"/index.ts",
|
|
85
|
-
];
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Relative imports, then whatever Bun resolves (tsconfig `paths` such as
|
|
89
|
-
* `$lib`). Anything that lands in `node_modules`, or does not resolve, is a
|
|
90
|
-
* package: the analysis cannot see inside it.
|
|
91
|
-
*/
|
|
92
|
-
function probe(base: string): string | null {
|
|
93
|
-
for (const ext of EXTENSIONS) {
|
|
94
|
-
const candidate = base + ext;
|
|
95
|
-
if (/\.[jt]sx?$/.test(candidate) && existsSync(candidate)) return candidate;
|
|
96
|
-
}
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export const defaultResolver: Resolver = (spec, from) => {
|
|
101
|
-
if (spec.startsWith(".")) return probe(resolvePath(dirname(from), spec));
|
|
102
|
-
try {
|
|
103
|
-
const found = (
|
|
104
|
-
globalThis as { Bun?: { resolveSync(s: string, d: string): string } }
|
|
105
|
-
).Bun?.resolveSync(spec, dirname(from));
|
|
106
|
-
if (!found || found.replace(/\\/g, "/").includes("/node_modules/"))
|
|
107
|
-
return null;
|
|
108
|
-
return /\.[jt]sx?$/.test(found) ? found : null;
|
|
109
|
-
} catch {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* The default resolver plus import aliases (`$lib` and friends): prefix to
|
|
116
|
-
* absolute directory, as grimoire's config defines them. Bun has no alias
|
|
117
|
-
* option of its own, so these are not visible to `Bun.resolveSync`.
|
|
118
|
-
*/
|
|
119
|
-
export function createResolver(aliases: Record<string, string> = {}): Resolver {
|
|
120
|
-
const entries = Object.entries(aliases);
|
|
121
|
-
if (entries.length === 0) return defaultResolver;
|
|
122
|
-
return (spec, from) => {
|
|
123
|
-
for (const [prefix, dir] of entries) {
|
|
124
|
-
if (spec !== prefix && !spec.startsWith(`${prefix}/`)) continue;
|
|
125
|
-
return probe(join(dir, spec.slice(prefix.length)));
|
|
126
|
-
}
|
|
127
|
-
return defaultResolver(spec, from);
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const LIFECYCLE_MACROS = new Set(["$effect", "$inspect", "$debug"]);
|
|
132
|
-
const GLOBAL_COMPONENTS = new Set(["Window", "Document", "Body"]);
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* `<Head>` from grimoire only puts its children in `<head>`, and the server
|
|
136
|
-
* already did that, so for hydration it is as inert as a native element: only
|
|
137
|
-
* its children can need JavaScript. Matched on where it was imported from, so a
|
|
138
|
-
* component of the same name that is the project's own is analysed like any
|
|
139
|
-
* other.
|
|
140
|
-
*/
|
|
141
|
-
const isGrimoireHead = (info: ModuleInfo, tag: string): boolean =>
|
|
142
|
-
tag === "Head" &&
|
|
143
|
-
!!info.imports.get("Head")?.source.startsWith("@sigil-dev/grimoire");
|
|
144
|
-
|
|
145
|
-
interface ImportInfo {
|
|
146
|
-
source: string;
|
|
147
|
-
imported: string;
|
|
148
|
-
typeOnly: boolean;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
interface ModuleInfo {
|
|
152
|
-
file: string;
|
|
153
|
-
ast: t.File;
|
|
154
|
-
imports: Map<string, ImportInfo>;
|
|
155
|
-
/** Names declared with a state macro anywhere in the file. */
|
|
156
|
-
stateNames: Set<string>;
|
|
157
|
-
/** Module-level state that is exported, by exported name. */
|
|
158
|
-
exportedState: Set<string>;
|
|
159
|
-
/** Module-level `$derived` names (a subset of exportedState). */
|
|
160
|
-
derivedNames: Set<string>;
|
|
161
|
-
/** `export { a as b } from "./x"` and `export * from "./x"` ("*"). */
|
|
162
|
-
reexports: { source: string; imported: string; exported: string }[];
|
|
163
|
-
/** Functions that read state (directly or through another such function). */
|
|
164
|
-
reactiveFns: Set<string>;
|
|
165
|
-
/** Exported name to local name. */
|
|
166
|
-
exports: Map<string, string>;
|
|
167
|
-
components: Map<string, NodePath<t.Function>>;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const modules = new Map<string, ModuleInfo | null>();
|
|
171
|
-
const mtimes = new Map<string, number>();
|
|
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
|
-
return
|
|
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
|
-
const
|
|
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
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const
|
|
451
|
-
if (
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
},
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
if (
|
|
551
|
-
add("
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
-
if (
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
if (
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
if (
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
*
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
const
|
|
747
|
-
|
|
748
|
-
const
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
const
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
const
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
const
|
|
862
|
-
const
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
"
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
)
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Which components need JavaScript in the browser, and why.
|
|
3
|
+
*
|
|
4
|
+
* A component that has no state, effects, handlers, bindings or refs, and
|
|
5
|
+
* whose every child is also like that, only produces HTML. Hydrating it means
|
|
6
|
+
* claiming nodes and attaching nothing, so it is work the browser could skip.
|
|
7
|
+
*
|
|
8
|
+
* This module only *decides and explains*. It changes no generated code; the
|
|
9
|
+
* report (`sigil explain`) and the `static` assertion use it, so the analysis
|
|
10
|
+
* can be checked against real projects before anything relies on it.
|
|
11
|
+
*
|
|
12
|
+
* The rule is conservative: **hydrate when unsure.** A wrong "static" is a
|
|
13
|
+
* page that looks right and does nothing, which is much worse than shipping a
|
|
14
|
+
* little JavaScript, so anything the analysis cannot see into counts as
|
|
15
|
+
* dynamic, and says what it could not see:
|
|
16
|
+
*
|
|
17
|
+
* - a function imported from a package, or from a project file that reads
|
|
18
|
+
* state (`calls formatDate, which reads state`)
|
|
19
|
+
* - a component from a package, from props, or `Window`/`Head` and friends
|
|
20
|
+
* - a spread, a directive, a ref
|
|
21
|
+
*
|
|
22
|
+
* Whether a particular *use* of a static component is static also depends on
|
|
23
|
+
* what the use passes it (a reactive prop makes that use dynamic). That is the
|
|
24
|
+
* use-site check, `useSiteReasons`.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
28
|
+
import { dirname, join, resolve as resolvePath } from "node:path";
|
|
29
|
+
import type { NodePath } from "@babel/core";
|
|
30
|
+
import { parseSync, types as t, traverse } from "@babel/core";
|
|
31
|
+
|
|
32
|
+
export type ReasonKind =
|
|
33
|
+
| "state"
|
|
34
|
+
| "effect"
|
|
35
|
+
| "handler"
|
|
36
|
+
| "directive"
|
|
37
|
+
| "ref"
|
|
38
|
+
| "spread"
|
|
39
|
+
| "imported-state"
|
|
40
|
+
| "imported-call"
|
|
41
|
+
| "unknown-import"
|
|
42
|
+
| "child"
|
|
43
|
+
| "unknown-child"
|
|
44
|
+
| "global"
|
|
45
|
+
| "async"
|
|
46
|
+
| "recursive"
|
|
47
|
+
| "reactive-prop"
|
|
48
|
+
| "parent-prop";
|
|
49
|
+
|
|
50
|
+
export interface Reason {
|
|
51
|
+
kind: ReasonKind;
|
|
52
|
+
line: number;
|
|
53
|
+
detail: string;
|
|
54
|
+
/** The file the reason is in, when it is not the component's own. */
|
|
55
|
+
file?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* - `static`: produces only HTML.
|
|
60
|
+
* - `conditional`: static as long as the children it is given are. It renders
|
|
61
|
+
* `children`, so each use decides.
|
|
62
|
+
* - `dynamic`: needs JavaScript; `reasons` says why.
|
|
63
|
+
*/
|
|
64
|
+
export type Verdict = "static" | "conditional" | "dynamic";
|
|
65
|
+
|
|
66
|
+
export interface ComponentReport {
|
|
67
|
+
name: string;
|
|
68
|
+
file: string;
|
|
69
|
+
line: number;
|
|
70
|
+
verdict: Verdict;
|
|
71
|
+
reasons: Reason[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Resolve an import specifier from a file to a project file, or null. */
|
|
75
|
+
export type Resolver = (spec: string, from: string) => string | null;
|
|
76
|
+
|
|
77
|
+
const EXTENSIONS = [
|
|
78
|
+
"",
|
|
79
|
+
".tsx",
|
|
80
|
+
".ts",
|
|
81
|
+
".jsx",
|
|
82
|
+
".js",
|
|
83
|
+
"/index.tsx",
|
|
84
|
+
"/index.ts",
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Relative imports, then whatever Bun resolves (tsconfig `paths` such as
|
|
89
|
+
* `$lib`). Anything that lands in `node_modules`, or does not resolve, is a
|
|
90
|
+
* package: the analysis cannot see inside it.
|
|
91
|
+
*/
|
|
92
|
+
function probe(base: string): string | null {
|
|
93
|
+
for (const ext of EXTENSIONS) {
|
|
94
|
+
const candidate = base + ext;
|
|
95
|
+
if (/\.[jt]sx?$/.test(candidate) && existsSync(candidate)) return candidate;
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const defaultResolver: Resolver = (spec, from) => {
|
|
101
|
+
if (spec.startsWith(".")) return probe(resolvePath(dirname(from), spec));
|
|
102
|
+
try {
|
|
103
|
+
const found = (
|
|
104
|
+
globalThis as { Bun?: { resolveSync(s: string, d: string): string } }
|
|
105
|
+
).Bun?.resolveSync(spec, dirname(from));
|
|
106
|
+
if (!found || found.replace(/\\/g, "/").includes("/node_modules/"))
|
|
107
|
+
return null;
|
|
108
|
+
return /\.[jt]sx?$/.test(found) ? found : null;
|
|
109
|
+
} catch {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The default resolver plus import aliases (`$lib` and friends): prefix to
|
|
116
|
+
* absolute directory, as grimoire's config defines them. Bun has no alias
|
|
117
|
+
* option of its own, so these are not visible to `Bun.resolveSync`.
|
|
118
|
+
*/
|
|
119
|
+
export function createResolver(aliases: Record<string, string> = {}): Resolver {
|
|
120
|
+
const entries = Object.entries(aliases);
|
|
121
|
+
if (entries.length === 0) return defaultResolver;
|
|
122
|
+
return (spec, from) => {
|
|
123
|
+
for (const [prefix, dir] of entries) {
|
|
124
|
+
if (spec !== prefix && !spec.startsWith(`${prefix}/`)) continue;
|
|
125
|
+
return probe(join(dir, spec.slice(prefix.length)));
|
|
126
|
+
}
|
|
127
|
+
return defaultResolver(spec, from);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const LIFECYCLE_MACROS = new Set(["$effect", "$inspect", "$debug"]);
|
|
132
|
+
const GLOBAL_COMPONENTS = new Set(["Window", "Document", "Body"]);
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* `<Head>` from grimoire only puts its children in `<head>`, and the server
|
|
136
|
+
* already did that, so for hydration it is as inert as a native element: only
|
|
137
|
+
* its children can need JavaScript. Matched on where it was imported from, so a
|
|
138
|
+
* component of the same name that is the project's own is analysed like any
|
|
139
|
+
* other.
|
|
140
|
+
*/
|
|
141
|
+
const isGrimoireHead = (info: ModuleInfo, tag: string): boolean =>
|
|
142
|
+
tag === "Head" &&
|
|
143
|
+
!!info.imports.get("Head")?.source.startsWith("@sigil-dev/grimoire");
|
|
144
|
+
|
|
145
|
+
interface ImportInfo {
|
|
146
|
+
source: string;
|
|
147
|
+
imported: string;
|
|
148
|
+
typeOnly: boolean;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface ModuleInfo {
|
|
152
|
+
file: string;
|
|
153
|
+
ast: t.File;
|
|
154
|
+
imports: Map<string, ImportInfo>;
|
|
155
|
+
/** Names declared with a state macro anywhere in the file. */
|
|
156
|
+
stateNames: Set<string>;
|
|
157
|
+
/** Module-level state that is exported, by exported name. */
|
|
158
|
+
exportedState: Set<string>;
|
|
159
|
+
/** Module-level `$derived` names (a subset of exportedState). */
|
|
160
|
+
derivedNames: Set<string>;
|
|
161
|
+
/** `export { a as b } from "./x"` and `export * from "./x"` ("*"). */
|
|
162
|
+
reexports: { source: string; imported: string; exported: string }[];
|
|
163
|
+
/** Functions that read state (directly or through another such function). */
|
|
164
|
+
reactiveFns: Set<string>;
|
|
165
|
+
/** Exported name to local name. */
|
|
166
|
+
exports: Map<string, string>;
|
|
167
|
+
components: Map<string, NodePath<t.Function>>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const modules = new Map<string, ModuleInfo | null>();
|
|
171
|
+
const mtimes = new Map<string, number>();
|
|
172
|
+
/** The code each cached module was parsed from. */
|
|
173
|
+
const codes = new Map<string, string>();
|
|
174
|
+
/**
|
|
175
|
+
* Modules parsed from code handed in (the file being compiled), by file:
|
|
176
|
+
* the last one each. A file compiled in three modes, or looked at by more
|
|
177
|
+
* than one check in one compile, is parsed once while its code is the same.
|
|
178
|
+
*/
|
|
179
|
+
const given = new Map<string, { code: string; info: ModuleInfo }>();
|
|
180
|
+
|
|
181
|
+
function mtimeOf(file: string): number {
|
|
182
|
+
try {
|
|
183
|
+
return statSync(file).mtimeMs;
|
|
184
|
+
} catch {
|
|
185
|
+
return -1;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Forget cached modules, for a long-lived process (dev server) after a file changes. */
|
|
190
|
+
export function clearAnalysisCache(file?: string): void {
|
|
191
|
+
if (file) modules.delete(file);
|
|
192
|
+
else modules.clear();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function parse(code: string, file: string): t.File | null {
|
|
196
|
+
try {
|
|
197
|
+
return parseSync(code, {
|
|
198
|
+
filename: file,
|
|
199
|
+
babelrc: false,
|
|
200
|
+
configFile: false,
|
|
201
|
+
parserOpts: {
|
|
202
|
+
// @ts-expect-error babel's option type does not list the tuple form
|
|
203
|
+
plugins: [["typescript", { isTSX: true }], "jsx"],
|
|
204
|
+
},
|
|
205
|
+
}) as t.File | null;
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const isMacroName = (name: string) =>
|
|
212
|
+
name === "$state" ||
|
|
213
|
+
name === "$state.raw" ||
|
|
214
|
+
name === "$store" ||
|
|
215
|
+
name === "$derived" ||
|
|
216
|
+
name === "$derived.by";
|
|
217
|
+
|
|
218
|
+
function macroNameOf(init: t.Node | null | undefined): string | null {
|
|
219
|
+
if (!init || !t.isCallExpression(init)) return null;
|
|
220
|
+
const c = init.callee;
|
|
221
|
+
if (t.isIdentifier(c)) return c.name;
|
|
222
|
+
if (
|
|
223
|
+
t.isMemberExpression(c) &&
|
|
224
|
+
t.isIdentifier(c.object) &&
|
|
225
|
+
t.isIdentifier(c.property)
|
|
226
|
+
) {
|
|
227
|
+
return `${c.object.name}.${c.property.name}`;
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function lineOf(node: t.Node): number {
|
|
233
|
+
return node.loc?.start.line ?? 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function loadModule(file: string, source?: string): ModuleInfo | null {
|
|
237
|
+
if (source !== undefined) {
|
|
238
|
+
// the same code as a module already parsed: the same module
|
|
239
|
+
const last = given.get(file);
|
|
240
|
+
if (last?.code === source) return last.info;
|
|
241
|
+
if (modules.has(file) && codes.get(file) === source) {
|
|
242
|
+
const info = modules.get(file);
|
|
243
|
+
if (info) return info;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (source === undefined && modules.has(file)) {
|
|
247
|
+
// a long-lived process (dev server) sees files change under it
|
|
248
|
+
if (mtimes.get(file) === mtimeOf(file)) return modules.get(file)!;
|
|
249
|
+
modules.delete(file);
|
|
250
|
+
}
|
|
251
|
+
let code = source;
|
|
252
|
+
if (code === undefined) {
|
|
253
|
+
try {
|
|
254
|
+
code = readFileSync(file, "utf8");
|
|
255
|
+
} catch {
|
|
256
|
+
modules.set(file, null);
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const ast = parse(code, file);
|
|
261
|
+
if (!ast) {
|
|
262
|
+
if (source === undefined) modules.set(file, null);
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const info: ModuleInfo = {
|
|
267
|
+
file,
|
|
268
|
+
ast,
|
|
269
|
+
imports: new Map(),
|
|
270
|
+
stateNames: new Set(),
|
|
271
|
+
exportedState: new Set(),
|
|
272
|
+
derivedNames: new Set(),
|
|
273
|
+
reexports: [],
|
|
274
|
+
reactiveFns: new Set(),
|
|
275
|
+
exports: new Map(),
|
|
276
|
+
components: new Map(),
|
|
277
|
+
};
|
|
278
|
+
const fns = new Map<string, NodePath>();
|
|
279
|
+
|
|
280
|
+
traverse(ast, {
|
|
281
|
+
ImportDeclaration(path) {
|
|
282
|
+
const typeOnly = path.node.importKind === "type";
|
|
283
|
+
for (const s of path.node.specifiers) {
|
|
284
|
+
const imported = t.isImportDefaultSpecifier(s)
|
|
285
|
+
? "default"
|
|
286
|
+
: t.isImportNamespaceSpecifier(s)
|
|
287
|
+
? "*"
|
|
288
|
+
: t.isIdentifier(s.imported)
|
|
289
|
+
? s.imported.name
|
|
290
|
+
: s.imported.value;
|
|
291
|
+
info.imports.set(s.local.name, {
|
|
292
|
+
source: path.node.source.value,
|
|
293
|
+
imported,
|
|
294
|
+
typeOnly:
|
|
295
|
+
typeOnly || (t.isImportSpecifier(s) && s.importKind === "type"),
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
VariableDeclarator(path) {
|
|
300
|
+
const id = path.node.id;
|
|
301
|
+
if (!t.isIdentifier(id)) return;
|
|
302
|
+
const macro = macroNameOf(path.node.init);
|
|
303
|
+
if (macro && isMacroName(macro)) {
|
|
304
|
+
info.stateNames.add(id.name);
|
|
305
|
+
if (
|
|
306
|
+
path.parentPath.parentPath?.isProgram() ||
|
|
307
|
+
path.parentPath.parentPath?.isExportNamedDeclaration()
|
|
308
|
+
) {
|
|
309
|
+
info.exportedState.add(id.name);
|
|
310
|
+
if (macro === "$derived") info.derivedNames.add(id.name);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const init = path.get("init");
|
|
314
|
+
if (init.isArrowFunctionExpression() || init.isFunctionExpression()) {
|
|
315
|
+
fns.set(id.name, init);
|
|
316
|
+
if (/^[A-Z]/.test(id.name))
|
|
317
|
+
info.components.set(id.name, init as NodePath<t.Function>);
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
FunctionDeclaration(path) {
|
|
321
|
+
if (!path.node.id) return;
|
|
322
|
+
fns.set(path.node.id.name, path);
|
|
323
|
+
if (/^[A-Z]/.test(path.node.id.name))
|
|
324
|
+
info.components.set(path.node.id.name, path);
|
|
325
|
+
},
|
|
326
|
+
ExportNamedDeclaration(path) {
|
|
327
|
+
const d = path.node.declaration;
|
|
328
|
+
if (d && t.isVariableDeclaration(d)) {
|
|
329
|
+
for (const decl of d.declarations) {
|
|
330
|
+
if (t.isIdentifier(decl.id))
|
|
331
|
+
info.exports.set(decl.id.name, decl.id.name);
|
|
332
|
+
}
|
|
333
|
+
} else if (d && t.isFunctionDeclaration(d) && d.id) {
|
|
334
|
+
info.exports.set(d.id.name, d.id.name);
|
|
335
|
+
}
|
|
336
|
+
if (path.node.source) {
|
|
337
|
+
for (const s of path.node.specifiers) {
|
|
338
|
+
if (!t.isExportSpecifier(s) || s.exportKind === "type") continue;
|
|
339
|
+
info.reexports.push({
|
|
340
|
+
source: path.node.source.value,
|
|
341
|
+
imported: t.isIdentifier(s.local) ? s.local.name : s.local.value,
|
|
342
|
+
exported: t.isIdentifier(s.exported)
|
|
343
|
+
? s.exported.name
|
|
344
|
+
: s.exported.value,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (!path.node.source) {
|
|
349
|
+
for (const s of path.node.specifiers) {
|
|
350
|
+
if (t.isExportSpecifier(s)) {
|
|
351
|
+
const exported = t.isIdentifier(s.exported)
|
|
352
|
+
? s.exported.name
|
|
353
|
+
: s.exported.value;
|
|
354
|
+
info.exports.set(
|
|
355
|
+
exported,
|
|
356
|
+
t.isIdentifier(s.local) ? s.local.name : s.local.value,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
ExportAllDeclaration(path) {
|
|
363
|
+
if (path.node.exportKind === "type") return;
|
|
364
|
+
info.reexports.push({
|
|
365
|
+
source: path.node.source.value,
|
|
366
|
+
imported: "*",
|
|
367
|
+
exported: "*",
|
|
368
|
+
});
|
|
369
|
+
},
|
|
370
|
+
ExportDefaultDeclaration(path) {
|
|
371
|
+
const d = path.node.declaration;
|
|
372
|
+
if (t.isIdentifier(d)) info.exports.set("default", d.name);
|
|
373
|
+
else if (
|
|
374
|
+
(t.isFunctionDeclaration(d) || t.isClassDeclaration(d)) &&
|
|
375
|
+
d.id
|
|
376
|
+
) {
|
|
377
|
+
info.exports.set("default", d.id.name);
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// functions that read state, to a fixed point (a helper may be declared later)
|
|
383
|
+
const readsState = (fn: NodePath): boolean => {
|
|
384
|
+
let found = false;
|
|
385
|
+
fn.traverse({
|
|
386
|
+
Identifier(p) {
|
|
387
|
+
if (
|
|
388
|
+
!found &&
|
|
389
|
+
p.isReferencedIdentifier() &&
|
|
390
|
+
info.stateNames.has(p.node.name)
|
|
391
|
+
)
|
|
392
|
+
found = true;
|
|
393
|
+
},
|
|
394
|
+
CallExpression(p) {
|
|
395
|
+
const c = p.node.callee;
|
|
396
|
+
if (!found && t.isIdentifier(c) && info.reactiveFns.has(c.name))
|
|
397
|
+
found = true;
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
return found;
|
|
401
|
+
};
|
|
402
|
+
let changed = true;
|
|
403
|
+
while (changed) {
|
|
404
|
+
changed = false;
|
|
405
|
+
for (const [name, fn] of fns) {
|
|
406
|
+
if (info.reactiveFns.has(name) || /^[A-Z]/.test(name)) continue;
|
|
407
|
+
if (readsState(fn)) {
|
|
408
|
+
info.reactiveFns.add(name);
|
|
409
|
+
changed = true;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (source === undefined) {
|
|
415
|
+
modules.set(file, info);
|
|
416
|
+
mtimes.set(file, mtimeOf(file));
|
|
417
|
+
codes.set(file, code);
|
|
418
|
+
} else {
|
|
419
|
+
given.set(file, { code, info });
|
|
420
|
+
}
|
|
421
|
+
return info;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// ---------------------------------------------------------------------------
|
|
425
|
+
|
|
426
|
+
interface Ctx {
|
|
427
|
+
resolve: Resolver;
|
|
428
|
+
/** `file#Name` in progress, to stop a cycle. */
|
|
429
|
+
stack: Set<string>;
|
|
430
|
+
memo: Map<string, ComponentReport>;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const newCtx = (resolve: Resolver): Ctx => ({
|
|
434
|
+
resolve,
|
|
435
|
+
stack: new Set(),
|
|
436
|
+
memo: new Map(),
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* The report for `<Tag>` as written in `info`'s file: a component declared
|
|
441
|
+
* there, or one imported from a project file. Anything else is a reason the
|
|
442
|
+
* analysis cannot see in.
|
|
443
|
+
*/
|
|
444
|
+
function reportForTag(
|
|
445
|
+
info: ModuleInfo,
|
|
446
|
+
tag: string,
|
|
447
|
+
ctx: Ctx,
|
|
448
|
+
): ComponentReport | { kind: ReasonKind; detail: string } {
|
|
449
|
+
if (info.components.has(tag)) return analyzeIn(info, tag, ctx);
|
|
450
|
+
const imp = info.imports.get(tag);
|
|
451
|
+
if (!imp) {
|
|
452
|
+
return {
|
|
453
|
+
kind: "unknown-child",
|
|
454
|
+
detail: `renders <${tag}>, which is not declared in this file`,
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
const target = ctx.resolve(imp.source, info.file);
|
|
458
|
+
const mod = target ? loadModule(target) : null;
|
|
459
|
+
if (!mod) {
|
|
460
|
+
return {
|
|
461
|
+
kind: "unknown-child",
|
|
462
|
+
detail: `renders <${tag}> from '${imp.source}', which the analysis cannot see inside`,
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
return analyzeIn(mod, mod.exports.get(imp.imported) ?? imp.imported, ctx);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function analyzeIn(info: ModuleInfo, name: string, ctx: Ctx): ComponentReport {
|
|
469
|
+
const key = `${info.file}#${name}`;
|
|
470
|
+
const cached = ctx.memo.get(key);
|
|
471
|
+
if (cached) return cached;
|
|
472
|
+
const fn = info.components.get(name);
|
|
473
|
+
const line = fn ? lineOf(fn.node) : 0;
|
|
474
|
+
if (!fn) {
|
|
475
|
+
return {
|
|
476
|
+
name,
|
|
477
|
+
file: info.file,
|
|
478
|
+
line,
|
|
479
|
+
verdict: "dynamic",
|
|
480
|
+
reasons: [
|
|
481
|
+
{
|
|
482
|
+
kind: "unknown-child",
|
|
483
|
+
line,
|
|
484
|
+
detail: `${name} is not a component the analysis can find in ${info.file}`,
|
|
485
|
+
},
|
|
486
|
+
],
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
if (ctx.stack.has(key)) {
|
|
490
|
+
return {
|
|
491
|
+
name,
|
|
492
|
+
file: info.file,
|
|
493
|
+
line,
|
|
494
|
+
verdict: "dynamic",
|
|
495
|
+
reasons: [{ kind: "recursive", line, detail: `${name} renders itself` }],
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
ctx.stack.add(key);
|
|
499
|
+
|
|
500
|
+
const reasons: Reason[] = [];
|
|
501
|
+
let conditional = false;
|
|
502
|
+
const add = (kind: ReasonKind, node: t.Node, detail: string, file?: string) =>
|
|
503
|
+
reasons.push({
|
|
504
|
+
kind,
|
|
505
|
+
line: lineOf(node),
|
|
506
|
+
detail,
|
|
507
|
+
...(file ? { file } : {}),
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
if (fn.node.async) add("async", fn.node, `${name} is async`);
|
|
511
|
+
|
|
512
|
+
// the props parameter: reads of it are fine, and its names are not "imports"
|
|
513
|
+
const propNames = new Set<string>();
|
|
514
|
+
for (const p of fn.node.params) {
|
|
515
|
+
if (t.isIdentifier(p)) propNames.add(p.name);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** True when `path` sits inside a function that is not the component itself. */
|
|
519
|
+
const inNested = (path: NodePath): boolean => {
|
|
520
|
+
const f = path.getFunctionParent();
|
|
521
|
+
return !!f && f.node !== fn.node;
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
fn.traverse({
|
|
525
|
+
VariableDeclarator(p) {
|
|
526
|
+
const macro = macroNameOf(p.node.init);
|
|
527
|
+
if (macro && isMacroName(macro))
|
|
528
|
+
add(
|
|
529
|
+
"state",
|
|
530
|
+
p.node,
|
|
531
|
+
`declares ${macro} \`${t.isIdentifier(p.node.id) ? p.node.id.name : "…"}\``,
|
|
532
|
+
);
|
|
533
|
+
},
|
|
534
|
+
CallExpression(p) {
|
|
535
|
+
const c = p.node.callee;
|
|
536
|
+
if (t.isIdentifier(c) && LIFECYCLE_MACROS.has(c.name)) {
|
|
537
|
+
add("effect", p.node, `calls ${c.name}`);
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
JSXAttribute(p) {
|
|
541
|
+
const n = p.node.name;
|
|
542
|
+
if (t.isJSXNamespacedName(n)) {
|
|
543
|
+
add(
|
|
544
|
+
"directive",
|
|
545
|
+
p.node,
|
|
546
|
+
`uses the \`${n.namespace.name}:${n.name.name}\` directive`,
|
|
547
|
+
);
|
|
548
|
+
} else if (/^on[A-Z]/.test(n.name)) {
|
|
549
|
+
add("handler", p.node, `has an ${n.name} handler`);
|
|
550
|
+
} else if (n.name === "ref") {
|
|
551
|
+
add("ref", p.node, "has a ref");
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
JSXSpreadAttribute(p) {
|
|
555
|
+
add("spread", p.node, "spreads props");
|
|
556
|
+
},
|
|
557
|
+
JSXOpeningElement(p) {
|
|
558
|
+
const nameNode = p.node.name;
|
|
559
|
+
if (t.isJSXMemberExpression(nameNode)) {
|
|
560
|
+
add(
|
|
561
|
+
"unknown-child",
|
|
562
|
+
p.node,
|
|
563
|
+
"renders a component from a member expression, which the analysis cannot resolve",
|
|
564
|
+
);
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
if (!t.isJSXIdentifier(nameNode) || !/^[A-Z]/.test(nameNode.name)) return;
|
|
568
|
+
const tag = nameNode.name;
|
|
569
|
+
if (isGrimoireHead(info, tag)) return;
|
|
570
|
+
if (GLOBAL_COMPONENTS.has(tag)) {
|
|
571
|
+
add("global", p.node, `renders <${tag}>, which touches the browser`);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
if (propNames.has(tag)) {
|
|
575
|
+
add(
|
|
576
|
+
"unknown-child",
|
|
577
|
+
p.node,
|
|
578
|
+
`renders <${tag}>, a component from props`,
|
|
579
|
+
);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
const found = reportForTag(info, tag, ctx);
|
|
583
|
+
if ("detail" in found) add(found.kind, p.node, found.detail);
|
|
584
|
+
else absorb(found, p.node, tag);
|
|
585
|
+
},
|
|
586
|
+
MemberExpression(p) {
|
|
587
|
+
// props.children: what this renders depends on what each use passes
|
|
588
|
+
const prop = p.node.property;
|
|
589
|
+
if (
|
|
590
|
+
!p.node.computed &&
|
|
591
|
+
t.isIdentifier(prop) &&
|
|
592
|
+
prop.name === "children"
|
|
593
|
+
) {
|
|
594
|
+
conditional = true;
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
Identifier(p) {
|
|
598
|
+
const name = p.node.name;
|
|
599
|
+
if (!p.isReferencedIdentifier()) return;
|
|
600
|
+
if (name === "children") conditional = true;
|
|
601
|
+
if (propNames.has(name)) return;
|
|
602
|
+
// module-level state read while rendering
|
|
603
|
+
if (
|
|
604
|
+
info.stateNames.has(name) &&
|
|
605
|
+
!p.scope.getBinding(name)?.path.findParent((x) => x === fn)
|
|
606
|
+
) {
|
|
607
|
+
add("state", p.node, `reads state \`${name}\``);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
const imp = info.imports.get(name);
|
|
611
|
+
if (!imp || imp.typeOnly) return;
|
|
612
|
+
if (p.findParent((x) => x.isTSType())) return;
|
|
613
|
+
// used as a JSX tag is a child component, handled above
|
|
614
|
+
if (
|
|
615
|
+
p.parentPath.isJSXOpeningElement() ||
|
|
616
|
+
p.parentPath.isJSXClosingElement()
|
|
617
|
+
)
|
|
618
|
+
return;
|
|
619
|
+
if (inNested(p) && !p.parentPath.isCallExpression()) return;
|
|
620
|
+
const target = ctx.resolve(imp.source, info.file);
|
|
621
|
+
const mod = target ? loadModule(target) : null;
|
|
622
|
+
if (!mod) {
|
|
623
|
+
// A bare value from a package could be anything. Calls are the
|
|
624
|
+
// risk (they may read state); a plain value is a constant.
|
|
625
|
+
if (p.parentPath.isCallExpression({ callee: p.node })) {
|
|
626
|
+
add(
|
|
627
|
+
"unknown-import",
|
|
628
|
+
p.node,
|
|
629
|
+
`calls \`${name}\` from '${imp.source}', which the analysis cannot see inside`,
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
const exportedAs = mod.exports.get(imp.imported) ?? imp.imported;
|
|
635
|
+
if (mod.exportedState.has(exportedAs) || mod.stateNames.has(exportedAs)) {
|
|
636
|
+
add(
|
|
637
|
+
"imported-state",
|
|
638
|
+
p.node,
|
|
639
|
+
`reads \`${name}\`, which is state in ${shortFile(mod.file)}`,
|
|
640
|
+
mod.file,
|
|
641
|
+
);
|
|
642
|
+
} else if (mod.reactiveFns.has(exportedAs)) {
|
|
643
|
+
add(
|
|
644
|
+
"imported-call",
|
|
645
|
+
p.node,
|
|
646
|
+
`uses \`${name}\`, which reads state in ${shortFile(mod.file)}`,
|
|
647
|
+
mod.file,
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
},
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
function absorb(sub: ComponentReport, node: t.Node, tag: string) {
|
|
654
|
+
if (sub.verdict === "dynamic") {
|
|
655
|
+
const first = sub.reasons[0];
|
|
656
|
+
add(
|
|
657
|
+
"child",
|
|
658
|
+
node,
|
|
659
|
+
`renders <${tag}>, which hydrates: ${first ? first.detail : "needs JavaScript"}`,
|
|
660
|
+
sub.file,
|
|
661
|
+
);
|
|
662
|
+
} else if (sub.verdict === "conditional") {
|
|
663
|
+
conditional = true;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
ctx.stack.delete(key);
|
|
668
|
+
const verdict: Verdict = reasons.length
|
|
669
|
+
? "dynamic"
|
|
670
|
+
: conditional
|
|
671
|
+
? "conditional"
|
|
672
|
+
: "static";
|
|
673
|
+
const report: ComponentReport = {
|
|
674
|
+
name,
|
|
675
|
+
file: info.file,
|
|
676
|
+
line,
|
|
677
|
+
verdict,
|
|
678
|
+
reasons,
|
|
679
|
+
};
|
|
680
|
+
ctx.memo.set(key, report);
|
|
681
|
+
return report;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const shortFile = (f: string) =>
|
|
685
|
+
f.replace(/\\/g, "/").split("/").slice(-2).join("/");
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Analyse every component declared in `file`.
|
|
689
|
+
*
|
|
690
|
+
* `source` overrides what is on disk, for code that is not saved yet or for
|
|
691
|
+
* tests; imports are still resolved against the real file system.
|
|
692
|
+
*/
|
|
693
|
+
export function analyzeFile(
|
|
694
|
+
file: string,
|
|
695
|
+
options: { resolve?: Resolver; source?: string } = {},
|
|
696
|
+
): ComponentReport[] {
|
|
697
|
+
const info = loadModule(file, options.source);
|
|
698
|
+
if (!info) return [];
|
|
699
|
+
const ctx = newCtx(options.resolve ?? defaultResolver);
|
|
700
|
+
return [...info.components.keys()].map((name) => analyzeIn(info, name, ctx));
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// ---------------------------------------------------------------------------
|
|
704
|
+
// use sites
|
|
705
|
+
// ---------------------------------------------------------------------------
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Why *this use* of a component cannot be static, whatever the component is.
|
|
709
|
+
*
|
|
710
|
+
* A static component given a reactive prop still changes, so each use is
|
|
711
|
+
* checked on what it passes: a spread, a prop that reads state, a prop that
|
|
712
|
+
* comes from the enclosing component's own props (which its parent may be
|
|
713
|
+
* driving), and children that are not static themselves.
|
|
714
|
+
*/
|
|
715
|
+
export function useSiteReasons(
|
|
716
|
+
element: t.JSXElement,
|
|
717
|
+
scope: {
|
|
718
|
+
stateNames: Set<string>;
|
|
719
|
+
reactiveFns: Set<string>;
|
|
720
|
+
propNames: Set<string>;
|
|
721
|
+
/** Describes an imported name that reads state, or null. */
|
|
722
|
+
importedReactive?: (name: string) => string | null;
|
|
723
|
+
},
|
|
724
|
+
): Reason[] {
|
|
725
|
+
const reasons: Reason[] = [];
|
|
726
|
+
const reads = (expr: t.Node): string | null => {
|
|
727
|
+
let found: string | null = null;
|
|
728
|
+
const visit = (n: t.Node | null | undefined): void => {
|
|
729
|
+
if (!n || found) return;
|
|
730
|
+
if (t.isArrowFunctionExpression(n) || t.isFunctionExpression(n)) return;
|
|
731
|
+
if (t.isIdentifier(n)) {
|
|
732
|
+
if (scope.stateNames.has(n.name)) found = `state \`${n.name}\``;
|
|
733
|
+
else if (scope.propNames.has(n.name))
|
|
734
|
+
found = `prop object \`${n.name}\``;
|
|
735
|
+
else found = scope.importedReactive?.(n.name) ?? null;
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
if (
|
|
739
|
+
t.isCallExpression(n) &&
|
|
740
|
+
t.isIdentifier(n.callee) &&
|
|
741
|
+
scope.reactiveFns.has(n.callee.name)
|
|
742
|
+
) {
|
|
743
|
+
found = `\`${n.callee.name}()\`, which reads state`;
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
for (const key of t.VISITOR_KEYS[n.type] ?? []) {
|
|
747
|
+
const child = (n as unknown as Record<string, unknown>)[key];
|
|
748
|
+
if (Array.isArray(child)) for (const c of child) visit(c as t.Node);
|
|
749
|
+
else visit(child as t.Node);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
visit(expr);
|
|
753
|
+
return found;
|
|
754
|
+
};
|
|
755
|
+
for (const attr of element.openingElement.attributes) {
|
|
756
|
+
if (t.isJSXSpreadAttribute(attr)) {
|
|
757
|
+
reasons.push({
|
|
758
|
+
kind: "spread",
|
|
759
|
+
line: lineOf(attr),
|
|
760
|
+
detail: "passes a spread",
|
|
761
|
+
});
|
|
762
|
+
} else if (
|
|
763
|
+
t.isJSXAttribute(attr) &&
|
|
764
|
+
t.isJSXExpressionContainer(attr.value)
|
|
765
|
+
) {
|
|
766
|
+
const hit = reads(attr.value.expression);
|
|
767
|
+
if (hit) {
|
|
768
|
+
const name = t.isJSXIdentifier(attr.name) ? attr.name.name : "prop";
|
|
769
|
+
reasons.push({
|
|
770
|
+
kind: hit.startsWith("prop") ? "parent-prop" : "reactive-prop",
|
|
771
|
+
line: lineOf(attr),
|
|
772
|
+
detail: `passes \`${name}\` from ${hit}, which can change`,
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
for (const child of element.children) {
|
|
778
|
+
if (
|
|
779
|
+
t.isJSXExpressionContainer(child) &&
|
|
780
|
+
!t.isJSXEmptyExpression(child.expression)
|
|
781
|
+
) {
|
|
782
|
+
const hit = reads(child.expression);
|
|
783
|
+
if (hit) {
|
|
784
|
+
reasons.push({
|
|
785
|
+
kind: "reactive-prop",
|
|
786
|
+
line: lineOf(child),
|
|
787
|
+
detail: `has a child that reads ${hit}`,
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return reasons;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Everything that stops `<X static />` being true, as reasons. Empty means the
|
|
797
|
+
* assertion holds.
|
|
798
|
+
*
|
|
799
|
+
* Three things have to be static together: the component, what this use passes
|
|
800
|
+
* it, and every element written inside the use. The inside matters because a
|
|
801
|
+
* static `<Card>` wrapping a `<Counter>` would otherwise skip Counter's
|
|
802
|
+
* hydration and leave it dead.
|
|
803
|
+
*/
|
|
804
|
+
export function checkStaticUse(
|
|
805
|
+
element: NodePath<t.JSXElement>,
|
|
806
|
+
file: string,
|
|
807
|
+
source: string,
|
|
808
|
+
resolve: Resolver = defaultResolver,
|
|
809
|
+
): Reason[] {
|
|
810
|
+
const info = loadModule(file, source);
|
|
811
|
+
if (!info) {
|
|
812
|
+
return [
|
|
813
|
+
{
|
|
814
|
+
kind: "unknown-child",
|
|
815
|
+
line: lineOf(element.node),
|
|
816
|
+
detail: "the file could not be analysed",
|
|
817
|
+
},
|
|
818
|
+
];
|
|
819
|
+
}
|
|
820
|
+
return useProblems(info, newCtx(resolve), element, resolve);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
function useProblems(
|
|
824
|
+
info: ModuleInfo,
|
|
825
|
+
ctx: Ctx,
|
|
826
|
+
element: NodePath<t.JSXElement>,
|
|
827
|
+
resolve: Resolver,
|
|
828
|
+
): Reason[] {
|
|
829
|
+
const file = info.file;
|
|
830
|
+
const at = (kind: ReasonKind, node: t.Node, detail: string): Reason => ({
|
|
831
|
+
kind,
|
|
832
|
+
line: lineOf(node),
|
|
833
|
+
detail,
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
const nameNode = element.node.openingElement.name;
|
|
837
|
+
if (!t.isJSXIdentifier(nameNode) || !/^[A-Z]/.test(nameNode.name)) {
|
|
838
|
+
return [
|
|
839
|
+
at(
|
|
840
|
+
"unknown-child",
|
|
841
|
+
element.node,
|
|
842
|
+
"`static` applies to components, not to HTML elements",
|
|
843
|
+
),
|
|
844
|
+
];
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
// the enclosing component's own props: a value from them may be driven by its parent
|
|
848
|
+
const propNames = new Set<string>();
|
|
849
|
+
const fn = element.getFunctionParent();
|
|
850
|
+
for (const p of fn?.node.params ?? []) {
|
|
851
|
+
if (t.isIdentifier(p)) propNames.add(p.name);
|
|
852
|
+
else if (t.isObjectPattern(p)) {
|
|
853
|
+
for (const prop of p.properties) {
|
|
854
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value))
|
|
855
|
+
propNames.add(prop.value.name);
|
|
856
|
+
else if (t.isRestElement(prop) && t.isIdentifier(prop.argument))
|
|
857
|
+
propNames.add(prop.argument.name);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
const importedReactive = (name: string): string | null => {
|
|
862
|
+
const imp = info.imports.get(name);
|
|
863
|
+
if (!imp || imp.typeOnly) return null;
|
|
864
|
+
const target = resolve(imp.source, file);
|
|
865
|
+
const mod = target ? loadModule(target) : null;
|
|
866
|
+
if (!mod) return null;
|
|
867
|
+
const exported = mod.exports.get(imp.imported) ?? imp.imported;
|
|
868
|
+
if (mod.exportedState.has(exported) || mod.stateNames.has(exported))
|
|
869
|
+
return `imported state \`${name}\``;
|
|
870
|
+
if (mod.reactiveFns.has(exported)) return `\`${name}\`, which reads state`;
|
|
871
|
+
return null;
|
|
872
|
+
};
|
|
873
|
+
const scope = {
|
|
874
|
+
stateNames: info.stateNames,
|
|
875
|
+
reactiveFns: info.reactiveFns,
|
|
876
|
+
propNames,
|
|
877
|
+
importedReactive,
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
const problems: Reason[] = [];
|
|
881
|
+
const checkElement = (el: t.JSXElement) => {
|
|
882
|
+
const open = el.openingElement;
|
|
883
|
+
problems.push(...useSiteReasons(el, scope));
|
|
884
|
+
for (const attr of open.attributes) {
|
|
885
|
+
if (t.isJSXAttribute(attr)) {
|
|
886
|
+
const n = attr.name;
|
|
887
|
+
if (t.isJSXNamespacedName(n))
|
|
888
|
+
problems.push(
|
|
889
|
+
at(
|
|
890
|
+
"directive",
|
|
891
|
+
attr,
|
|
892
|
+
`uses the \`${n.namespace.name}:${n.name.name}\` directive`,
|
|
893
|
+
),
|
|
894
|
+
);
|
|
895
|
+
else if (/^on[A-Z]/.test(n.name))
|
|
896
|
+
problems.push(at("handler", attr, `has an ${n.name} handler`));
|
|
897
|
+
else if (n.name === "ref") problems.push(at("ref", attr, "has a ref"));
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
const name = open.name;
|
|
901
|
+
if (!t.isJSXIdentifier(name) || !/^[A-Z]/.test(name.name)) return;
|
|
902
|
+
if (isGrimoireHead(info, name.name)) return;
|
|
903
|
+
if (GLOBAL_COMPONENTS.has(name.name)) {
|
|
904
|
+
problems.push(
|
|
905
|
+
at("global", el, `renders <${name.name}>, which touches the browser`),
|
|
906
|
+
);
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
const found = reportForTag(info, name.name, ctx);
|
|
910
|
+
if ("detail" in found) {
|
|
911
|
+
problems.push(at(found.kind, el, found.detail));
|
|
912
|
+
} else if (found.verdict === "dynamic") {
|
|
913
|
+
const first = found.reasons[0];
|
|
914
|
+
problems.push(
|
|
915
|
+
at(
|
|
916
|
+
"child",
|
|
917
|
+
el,
|
|
918
|
+
`<${name.name}> hydrates: ${first ? first.detail : "needs JavaScript"}`,
|
|
919
|
+
),
|
|
920
|
+
);
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
checkElement(element.node);
|
|
925
|
+
t.traverseFast(element.node, (n) => {
|
|
926
|
+
if (n !== element.node && t.isJSXElement(n)) checkElement(n);
|
|
927
|
+
});
|
|
928
|
+
return problems;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
export type { ModuleInfo };
|
|
932
|
+
/**
|
|
933
|
+
* The component uses in this file that produce only HTML, by node.
|
|
934
|
+
*
|
|
935
|
+
* Both the server and the browser build of a file ask this, and must get the
|
|
936
|
+
* same answer, because the server wraps exactly these uses in markers and the
|
|
937
|
+
* browser skips exactly these uses. It is computed from the file's own source
|
|
938
|
+
* and what it imports, never from the mode, so the two builds cannot drift.
|
|
939
|
+
*
|
|
940
|
+
* Only a use that is a direct child of an HTML element qualifies. That is the
|
|
941
|
+
* one position where hydration holds the server's nodes in a pool the use can
|
|
942
|
+
* be cut out of: a use inside a condition, a list, a component's children or a
|
|
943
|
+
* fragment hydrates through other machinery and is left alone.
|
|
944
|
+
*/
|
|
945
|
+
export const STATIC_USES = new WeakSet<t.JSXElement>();
|
|
946
|
+
|
|
947
|
+
/** Uses marked `static={false}`: the author's override, so they always hydrate. */
|
|
948
|
+
export const FORCE_HYDRATE = new WeakSet<t.JSXElement>();
|
|
949
|
+
|
|
950
|
+
const NO_SKIP_PARENTS = new Set(["script", "style", "textarea"]);
|
|
951
|
+
|
|
952
|
+
export function markStaticUses(
|
|
953
|
+
program: NodePath<t.Program>,
|
|
954
|
+
file: string,
|
|
955
|
+
source: string,
|
|
956
|
+
resolve: Resolver = defaultResolver,
|
|
957
|
+
): void {
|
|
958
|
+
// a use to mark is `<Component>` inside an element: without `<` and a
|
|
959
|
+
// capital anywhere in the file there's none, and nothing to parse
|
|
960
|
+
if (!/<[A-Z]/.test(source)) return;
|
|
961
|
+
const info = loadModule(file, source);
|
|
962
|
+
if (!info) return;
|
|
963
|
+
const ctx = newCtx(resolve);
|
|
964
|
+
program.traverse({
|
|
965
|
+
JSXElement(path) {
|
|
966
|
+
const name = path.node.openingElement.name;
|
|
967
|
+
if (!t.isJSXIdentifier(name) || !/^[A-Z]/.test(name.name)) return;
|
|
968
|
+
const parent = path.parent;
|
|
969
|
+
if (!t.isJSXElement(parent)) return;
|
|
970
|
+
const pn = parent.openingElement.name;
|
|
971
|
+
if (
|
|
972
|
+
!t.isJSXIdentifier(pn) ||
|
|
973
|
+
!/^[a-z]/.test(pn.name) ||
|
|
974
|
+
NO_SKIP_PARENTS.has(pn.name)
|
|
975
|
+
)
|
|
976
|
+
return;
|
|
977
|
+
if (isGrimoireHead(info, name.name)) return;
|
|
978
|
+
if (FORCE_HYDRATE.has(path.node)) return;
|
|
979
|
+
if (useProblems(info, ctx, path, resolve).length === 0) {
|
|
980
|
+
STATIC_USES.add(path.node);
|
|
981
|
+
}
|
|
982
|
+
},
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
export { loadModule as _loadModule };
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* What an import binds to, looked up in the module that declares it:
|
|
990
|
+
* `state` (a module-level `$state`/`$store`, read through a call),
|
|
991
|
+
* `derived` (a module-level `$derived`), `reactive-fn` (a function that
|
|
992
|
+
* reads state), or null. Follows `export { x }`, `export { x } from`,
|
|
993
|
+
* `export * from` and re-exported imports.
|
|
994
|
+
*/
|
|
995
|
+
export type ExportKind = "state" | "derived" | "reactive-fn" | null;
|
|
996
|
+
|
|
997
|
+
/**
|
|
998
|
+
* Whether a module's exports are plain values, seen from its text alone:
|
|
999
|
+
* state, derived values and functions reading them all need a rune
|
|
1000
|
+
* declared in the module, and anything else it could pass on comes through
|
|
1001
|
+
* `export {…}` or `export *`. Most of an app's modules are plain, and
|
|
1002
|
+
* asking this instead of parsing them was most of what compiling a plain
|
|
1003
|
+
* module cost.
|
|
1004
|
+
*/
|
|
1005
|
+
const plainModules = new Map<string, { mtime: number; plain: boolean }>();
|
|
1006
|
+
function plainModule(file: string): boolean {
|
|
1007
|
+
const mtime = mtimeOf(file);
|
|
1008
|
+
const known = plainModules.get(file);
|
|
1009
|
+
if (known?.mtime === mtime) return known.plain;
|
|
1010
|
+
let plain = false;
|
|
1011
|
+
try {
|
|
1012
|
+
const code = readFileSync(file, "utf8");
|
|
1013
|
+
plain =
|
|
1014
|
+
!/\$(state|store|derived)\b/.test(code) &&
|
|
1015
|
+
!/\bexport\s*(\*|\{)/.test(code);
|
|
1016
|
+
} catch {}
|
|
1017
|
+
plainModules.set(file, { mtime, plain });
|
|
1018
|
+
return plain;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
const IMPORT_DECL =
|
|
1022
|
+
/\bimport\s+(type\s+)?([\w$*{}\s,]+?)\s+from\s*["']([^"']+)["']|\bexport\s*(?:\*|\{[^}]*\})\s*from\s*["']([^"']+)["']|\bimport\s*\(\s*["']([^"']+)["']/g;
|
|
1023
|
+
|
|
1024
|
+
/**
|
|
1025
|
+
* Whether the compiler would leave a plain module (.ts: no JSX) as it is,
|
|
1026
|
+
* seen from its text: no rune or `<style>` in it, and nothing it imports is
|
|
1027
|
+
* state, derived or a function reading them (exportKind, which asks the
|
|
1028
|
+
* text of plain modules before parsing them). Then a server build can skip
|
|
1029
|
+
* Babel for it, which is most of an app's modules. Not for the browser: the
|
|
1030
|
+
* compiler also writes `$env/static` values into browser code.
|
|
1031
|
+
*/
|
|
1032
|
+
export function compilesToItself(
|
|
1033
|
+
file: string,
|
|
1034
|
+
source: string,
|
|
1035
|
+
resolve: Resolver,
|
|
1036
|
+
): boolean {
|
|
1037
|
+
if (!/\.[cm]?ts$/.test(file) || /\.d\.ts$/.test(file)) return false;
|
|
1038
|
+
if (
|
|
1039
|
+
/\$(state|store|derived|effect|props|inspect|bindable|host|debug)\b/.test(
|
|
1040
|
+
source,
|
|
1041
|
+
) ||
|
|
1042
|
+
source.includes("<style")
|
|
1043
|
+
)
|
|
1044
|
+
return false;
|
|
1045
|
+
for (const m of source.matchAll(IMPORT_DECL)) {
|
|
1046
|
+
const [, typeOnly, clause, from, reexportFrom, dynamicFrom] = m;
|
|
1047
|
+
const spec = from ?? reexportFrom ?? dynamicFrom!;
|
|
1048
|
+
const target = resolve(spec, file);
|
|
1049
|
+
if (!target || typeOnly) continue;
|
|
1050
|
+
// `export … from` and import(): anything of it may be read
|
|
1051
|
+
if (!from) {
|
|
1052
|
+
if (!plainModule(target)) return false;
|
|
1053
|
+
continue;
|
|
1054
|
+
}
|
|
1055
|
+
const names: string[] = [];
|
|
1056
|
+
const named = /\{([^}]*)\}/.exec(clause!);
|
|
1057
|
+
for (const part of named?.[1]?.split(",") ?? []) {
|
|
1058
|
+
const p = part.trim();
|
|
1059
|
+
if (!p || p.startsWith("type ")) continue;
|
|
1060
|
+
names.push(p.split(/\s+as\s+/)[0]!.trim());
|
|
1061
|
+
}
|
|
1062
|
+
const rest = clause!.replace(/\{[^}]*\}/, "").trim();
|
|
1063
|
+
if (/\*/.test(rest)) {
|
|
1064
|
+
// a namespace: any of its exports may be read
|
|
1065
|
+
if (!plainModule(target)) return false;
|
|
1066
|
+
} else if (/^[\w$]+/.test(rest)) names.push("default");
|
|
1067
|
+
for (const name of names)
|
|
1068
|
+
if (exportKind(target, name, resolve) !== null) return false;
|
|
1069
|
+
}
|
|
1070
|
+
return true;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
export function exportKind(
|
|
1074
|
+
file: string,
|
|
1075
|
+
name: string,
|
|
1076
|
+
resolve: Resolver,
|
|
1077
|
+
seen = new Set<string>(),
|
|
1078
|
+
): ExportKind {
|
|
1079
|
+
const key = `${file}#${name}`;
|
|
1080
|
+
if (seen.has(key)) return null;
|
|
1081
|
+
seen.add(key);
|
|
1082
|
+
if (plainModule(file)) return null;
|
|
1083
|
+
const mod = loadModule(file);
|
|
1084
|
+
if (!mod) return null;
|
|
1085
|
+
|
|
1086
|
+
const local = mod.exports.get(name);
|
|
1087
|
+
if (local !== undefined) {
|
|
1088
|
+
if (mod.derivedNames.has(local)) return "derived";
|
|
1089
|
+
if (mod.exportedState.has(local)) return "state";
|
|
1090
|
+
if (mod.reactiveFns.has(local)) return "reactive-fn";
|
|
1091
|
+
// `import { x } from "./a"; export { x }`
|
|
1092
|
+
const imp = mod.imports.get(local);
|
|
1093
|
+
if (imp && !imp.typeOnly && imp.imported !== "*") {
|
|
1094
|
+
const target = resolve(imp.source, file);
|
|
1095
|
+
if (target) return exportKind(target, imp.imported, resolve, seen);
|
|
1096
|
+
}
|
|
1097
|
+
return null;
|
|
1098
|
+
}
|
|
1099
|
+
for (const re of mod.reexports) {
|
|
1100
|
+
if (re.exported !== name && re.exported !== "*") continue;
|
|
1101
|
+
const target = resolve(re.source, file);
|
|
1102
|
+
if (!target) continue;
|
|
1103
|
+
const kind = exportKind(
|
|
1104
|
+
target,
|
|
1105
|
+
re.exported === "*" ? name : re.imported,
|
|
1106
|
+
resolve,
|
|
1107
|
+
seen,
|
|
1108
|
+
);
|
|
1109
|
+
if (kind) return kind;
|
|
1110
|
+
}
|
|
1111
|
+
return null;
|
|
1112
|
+
}
|