solid-optimizer 1.0.0-next.7 → 1.0.0-next.8
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 +4 -2
- package/dist/{compile-BuPR3pVT.mjs → compile-O1Z60VsW.mjs} +164 -10
- package/dist/compile-O1Z60VsW.mjs.map +1 -0
- package/dist/compile-X_SMDFr2.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +59 -32
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/compile.ts +21 -1
- package/src/constants.ts +22 -0
- package/src/inline.ts +165 -11
- package/src/terminate.ts +66 -0
- package/src/vite/index.ts +83 -47
- package/dist/compile-BuPR3pVT.mjs.map +0 -1
package/package.json
CHANGED
package/src/compile.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { removeProviders } from './provider';
|
|
|
15
15
|
import { lowerConditionals } from './conditional';
|
|
16
16
|
import type { ScopeAnalysis } from './scope';
|
|
17
17
|
import { analyzeScopes } from './scope';
|
|
18
|
+
import { terminateStatements } from './terminate';
|
|
18
19
|
import type { Primitive } from './value';
|
|
19
20
|
|
|
20
21
|
export interface CompileOptions {
|
|
@@ -288,6 +289,21 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
288
289
|
}
|
|
289
290
|
return parsed;
|
|
290
291
|
};
|
|
292
|
+
const terminated = runPass(
|
|
293
|
+
parsedCurrent(),
|
|
294
|
+
filename,
|
|
295
|
+
resolved,
|
|
296
|
+
sourceMap,
|
|
297
|
+
terminateStatements,
|
|
298
|
+
undefined,
|
|
299
|
+
);
|
|
300
|
+
if (terminated) {
|
|
301
|
+
current = terminated.code;
|
|
302
|
+
if (terminated.map !== undefined) {
|
|
303
|
+
maps.push(terminated.map);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const start = current;
|
|
291
307
|
const maxPasses = options.maxPasses ?? 10;
|
|
292
308
|
let skipped: Skip[] | undefined;
|
|
293
309
|
for (let round = 0; round < maxPasses; round += 1) {
|
|
@@ -335,7 +351,7 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
335
351
|
// A pass can put an element straight inside a parent that cannot hold it,
|
|
336
352
|
// which Solid's JSX transform rejects. Only a changed module can have one
|
|
337
353
|
// that its source did not.
|
|
338
|
-
if (current !==
|
|
354
|
+
if (current !== start) {
|
|
339
355
|
const result = runPass(
|
|
340
356
|
parsedCurrent(),
|
|
341
357
|
filename,
|
|
@@ -353,6 +369,10 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
353
369
|
}
|
|
354
370
|
|
|
355
371
|
const report = skipped ? { skipped } : {};
|
|
372
|
+
// Semicolons alone are no reason to change a module.
|
|
373
|
+
if (current === start) {
|
|
374
|
+
return { code, map: null, ...report };
|
|
375
|
+
}
|
|
356
376
|
if (maps.length === 0) {
|
|
357
377
|
return { code: current, map: null, ...report };
|
|
358
378
|
}
|
package/src/constants.ts
CHANGED
|
@@ -202,6 +202,28 @@ export function namedImportSources(
|
|
|
202
202
|
return [...sources];
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
+
/**
|
|
206
|
+
* The modules a module still imports once its types are stripped: the
|
|
207
|
+
* sources of `namedImportSources`, and imports for side effects only, like
|
|
208
|
+
* `import './setup'`.
|
|
209
|
+
*/
|
|
210
|
+
export function runtimeImportSources(
|
|
211
|
+
program: Program,
|
|
212
|
+
scopes: ScopeAnalysis = analyzeScopes(program),
|
|
213
|
+
): string[] {
|
|
214
|
+
const sources = new Set(namedImportSources(program, scopes));
|
|
215
|
+
for (const statement of program.body) {
|
|
216
|
+
if (
|
|
217
|
+
statement.type === 'ImportDeclaration' &&
|
|
218
|
+
statement.importKind !== 'type' &&
|
|
219
|
+
statement.specifiers.length === 0
|
|
220
|
+
) {
|
|
221
|
+
sources.add(statement.source.value);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return [...sources];
|
|
225
|
+
}
|
|
226
|
+
|
|
205
227
|
/**
|
|
206
228
|
* Whether the module reads one of these import bindings as a value. Scope
|
|
207
229
|
* analysis skips type positions, so a binding used only in types has no
|
package/src/inline.ts
CHANGED
|
@@ -2374,22 +2374,155 @@ class Inliner {
|
|
|
2374
2374
|
: undefined;
|
|
2375
2375
|
}
|
|
2376
2376
|
|
|
2377
|
+
/**
|
|
2378
|
+
* The parts of a compound component like Kobalte's
|
|
2379
|
+
* `const Dialog = Object.assign(DialogRoot, { Trigger: DialogTrigger })`,
|
|
2380
|
+
* or `undefined` when the binding is not one. `<Dialog>` is `<DialogRoot>`,
|
|
2381
|
+
* since `Object.assign` returns its first argument, and `<Dialog.Trigger>`
|
|
2382
|
+
* is `<DialogTrigger>` while nothing writes to a member of either.
|
|
2383
|
+
*/
|
|
2384
|
+
private compoundTarget(
|
|
2385
|
+
binding: Binding,
|
|
2386
|
+
): { root: Binding; parts: ReadonlyMap<string, Binding> } | undefined {
|
|
2387
|
+
const { declaration } = binding;
|
|
2388
|
+
if (
|
|
2389
|
+
!isCapitalized(binding.name) ||
|
|
2390
|
+
!isConstantDeclaration(binding, this.context.options) ||
|
|
2391
|
+
declaration.type !== 'VariableDeclarator' ||
|
|
2392
|
+
declaration.id !== binding.identifier ||
|
|
2393
|
+
!declaration.init
|
|
2394
|
+
) {
|
|
2395
|
+
return undefined;
|
|
2396
|
+
}
|
|
2397
|
+
const init = unwrap(declaration.init);
|
|
2398
|
+
if (
|
|
2399
|
+
init.type !== 'CallExpression' ||
|
|
2400
|
+
init.optional ||
|
|
2401
|
+
init.arguments.length !== 2 ||
|
|
2402
|
+
!this.isObjectAssign(init.callee)
|
|
2403
|
+
) {
|
|
2404
|
+
return undefined;
|
|
2405
|
+
}
|
|
2406
|
+
const [first, second] = init.arguments;
|
|
2407
|
+
if (first.type !== 'Identifier' || second.type !== 'ObjectExpression') {
|
|
2408
|
+
return undefined;
|
|
2409
|
+
}
|
|
2410
|
+
const root = this.topLevelComponent(first);
|
|
2411
|
+
if (!root || root === binding || this.hasMemberWrite(binding) || this.hasMemberWrite(root)) {
|
|
2412
|
+
return undefined;
|
|
2413
|
+
}
|
|
2414
|
+
const parts = new Map<string, Binding>();
|
|
2415
|
+
for (const property of second.properties) {
|
|
2416
|
+
if (
|
|
2417
|
+
property.type !== 'Property' ||
|
|
2418
|
+
property.kind !== 'init' ||
|
|
2419
|
+
property.computed ||
|
|
2420
|
+
property.method ||
|
|
2421
|
+
property.value.type !== 'Identifier'
|
|
2422
|
+
) {
|
|
2423
|
+
return undefined;
|
|
2424
|
+
}
|
|
2425
|
+
const key = propertyKeyName(property.key);
|
|
2426
|
+
if (key === undefined) {
|
|
2427
|
+
return undefined;
|
|
2428
|
+
}
|
|
2429
|
+
// Only parts with a capitalized name can be tags. The others stay reads.
|
|
2430
|
+
const part = this.topLevelComponent(property.value);
|
|
2431
|
+
if (part) {
|
|
2432
|
+
parts.set(key, part);
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
return { root, parts };
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2438
|
+
/** Whether `callee` is the global `Object.assign`. */
|
|
2439
|
+
private isObjectAssign(callee: Node): boolean {
|
|
2440
|
+
return (
|
|
2441
|
+
callee.type === 'MemberExpression' &&
|
|
2442
|
+
!callee.computed &&
|
|
2443
|
+
!callee.optional &&
|
|
2444
|
+
callee.object.type === 'Identifier' &&
|
|
2445
|
+
callee.object.name === 'Object' &&
|
|
2446
|
+
!this.context.scopes.references.get(callee.object) &&
|
|
2447
|
+
callee.property.type === 'Identifier' &&
|
|
2448
|
+
callee.property.name === 'assign'
|
|
2449
|
+
);
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
/** The top-level binding with a capitalized name that `reference` reads, when nothing reassigns it. */
|
|
2453
|
+
private topLevelComponent(reference: Node): Binding | undefined {
|
|
2454
|
+
const target = this.context.scopes.references.get(reference);
|
|
2455
|
+
return target &&
|
|
2456
|
+
target.scope.parent === undefined &&
|
|
2457
|
+
!target.mutated &&
|
|
2458
|
+
isCapitalized(target.name)
|
|
2459
|
+
? target
|
|
2460
|
+
: undefined;
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
/** Whether anything assigns to, updates, or deletes a member of the binding. */
|
|
2464
|
+
private hasMemberWrite(binding: Binding): boolean {
|
|
2465
|
+
for (const reference of binding.references) {
|
|
2466
|
+
const member = this.context.parents.get(reference);
|
|
2467
|
+
if (member?.type !== 'MemberExpression' || member.object !== reference) {
|
|
2468
|
+
continue;
|
|
2469
|
+
}
|
|
2470
|
+
const use = this.context.parents.get(member);
|
|
2471
|
+
if (
|
|
2472
|
+
(use?.type === 'AssignmentExpression' && use.left === member) ||
|
|
2473
|
+
use?.type === 'UpdateExpression' ||
|
|
2474
|
+
(use?.type === 'UnaryExpression' && use.operator === 'delete')
|
|
2475
|
+
) {
|
|
2476
|
+
return true;
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
return false;
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
/** Whether the code reads a member of the binding, like `DialogRoot.Trigger`. */
|
|
2483
|
+
private hasMemberRead(binding: Binding): boolean {
|
|
2484
|
+
for (const reference of binding.references) {
|
|
2485
|
+
const member = this.context.parents.get(reference);
|
|
2486
|
+
if (
|
|
2487
|
+
(member?.type === 'MemberExpression' || member?.type === 'JSXMemberExpression') &&
|
|
2488
|
+
member.object === reference
|
|
2489
|
+
) {
|
|
2490
|
+
return true;
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
return false;
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2377
2496
|
/**
|
|
2378
2497
|
* The statement of an alias nothing uses that `reference` is the value
|
|
2379
2498
|
* of, like `const Separator = SeparatorRoot` once its tags are renamed.
|
|
2380
|
-
* It does not keep the component, and goes away with it.
|
|
2499
|
+
* It does not keep the component, and goes away with it. A compound
|
|
2500
|
+
* component counts too, when its tags are renamed and nothing reads the
|
|
2501
|
+
* members `Object.assign` gave its root.
|
|
2381
2502
|
*/
|
|
2382
2503
|
private unusedAlias(reference: Node): Node | undefined {
|
|
2383
|
-
|
|
2504
|
+
let declarator = this.context.parents.get(reference);
|
|
2505
|
+
if (declarator?.type === 'Property') {
|
|
2506
|
+
declarator = this.context.parents.get(declarator);
|
|
2507
|
+
}
|
|
2508
|
+
if (declarator?.type === 'ObjectExpression' || declarator?.type === 'CallExpression') {
|
|
2509
|
+
const call =
|
|
2510
|
+
declarator.type === 'CallExpression' ? declarator : this.context.parents.get(declarator);
|
|
2511
|
+
declarator = call ? this.context.parents.get(call) : undefined;
|
|
2512
|
+
}
|
|
2384
2513
|
if (declarator?.type !== 'VariableDeclarator' || declarator.id.type !== 'Identifier') {
|
|
2385
2514
|
return undefined;
|
|
2386
2515
|
}
|
|
2387
2516
|
const alias = this.context.scopes.root.bindings.get(declarator.id.name);
|
|
2517
|
+
if (alias?.declaration !== declarator || alias.references.length > 0) {
|
|
2518
|
+
return undefined;
|
|
2519
|
+
}
|
|
2520
|
+
const compound = this.compoundTarget(alias);
|
|
2521
|
+
if (this.aliasTarget(alias) === undefined && (!compound || this.hasMemberRead(compound.root))) {
|
|
2522
|
+
return undefined;
|
|
2523
|
+
}
|
|
2388
2524
|
const statement = this.context.parents.get(declarator);
|
|
2389
|
-
return
|
|
2390
|
-
this.aliasTarget(alias) !== undefined &&
|
|
2391
|
-
alias.references.length === 0 &&
|
|
2392
|
-
statement?.type === 'VariableDeclaration' &&
|
|
2525
|
+
return statement?.type === 'VariableDeclaration' &&
|
|
2393
2526
|
statement.declarations.length === 1 &&
|
|
2394
2527
|
this.context.parents.get(statement)?.type === 'Program'
|
|
2395
2528
|
? statement
|
|
@@ -2404,23 +2537,36 @@ class Inliner {
|
|
|
2404
2537
|
private renameAliasTags(): boolean {
|
|
2405
2538
|
let renamed = false;
|
|
2406
2539
|
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
2407
|
-
const
|
|
2540
|
+
const compound = this.compoundTarget(binding);
|
|
2541
|
+
const target = this.aliasTarget(binding) ?? compound?.root;
|
|
2408
2542
|
if (!target) {
|
|
2409
2543
|
continue;
|
|
2410
2544
|
}
|
|
2411
2545
|
for (const reference of binding.references) {
|
|
2412
2546
|
const parent = this.context.parents.get(reference);
|
|
2547
|
+
// `<Dialog.Trigger>` is the part `Object.assign` gave the root.
|
|
2548
|
+
let tag: Node = reference;
|
|
2549
|
+
let renamedTo = target;
|
|
2550
|
+
if (parent?.type === 'JSXMemberExpression' && parent.object === reference) {
|
|
2551
|
+
const part = compound?.parts.get(parent.property.name);
|
|
2552
|
+
if (!part) {
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2555
|
+
tag = parent;
|
|
2556
|
+
renamedTo = part;
|
|
2557
|
+
}
|
|
2558
|
+
const element = this.context.parents.get(tag);
|
|
2413
2559
|
if (
|
|
2414
|
-
(
|
|
2415
|
-
|
|
2560
|
+
(element?.type !== 'JSXOpeningElement' && element?.type !== 'JSXClosingElement') ||
|
|
2561
|
+
element.name !== tag
|
|
2416
2562
|
) {
|
|
2417
2563
|
continue;
|
|
2418
2564
|
}
|
|
2419
2565
|
const scope = scopeAt(this.context.scopes, this.context.parents, reference);
|
|
2420
|
-
if (lookup(scope,
|
|
2566
|
+
if (lookup(scope, renamedTo.name) !== renamedTo) {
|
|
2421
2567
|
continue;
|
|
2422
2568
|
}
|
|
2423
|
-
this.context.s.overwrite(
|
|
2569
|
+
this.context.s.overwrite(tag.start, tag.end, renamedTo.name);
|
|
2424
2570
|
renamed = true;
|
|
2425
2571
|
}
|
|
2426
2572
|
}
|
|
@@ -2786,3 +2932,11 @@ function hasChild(children: readonly Node[], child: Node): boolean {
|
|
|
2786
2932
|
}
|
|
2787
2933
|
return false;
|
|
2788
2934
|
}
|
|
2935
|
+
|
|
2936
|
+
/** The name a non-computed object key spells, like `Trigger` or `'Trigger'`. */
|
|
2937
|
+
function propertyKeyName(key: Node): string | undefined {
|
|
2938
|
+
if (key.type === 'Identifier') {
|
|
2939
|
+
return key.name;
|
|
2940
|
+
}
|
|
2941
|
+
return key.type === 'Literal' && typeof key.value === 'string' ? key.value : undefined;
|
|
2942
|
+
}
|
package/src/terminate.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends each statement that relies on a line break.
|
|
3
|
+
*
|
|
4
|
+
* The other passes replace a read with text that can start with `(` or a
|
|
5
|
+
* template literal, like `(p.close)()` for `props.close()`. After a statement
|
|
6
|
+
* with no semicolon, that text continues the statement instead of starting a
|
|
7
|
+
* new one, so `open()` and `(p.close)()` on separate lines call what `open()`
|
|
8
|
+
* returned. A semicolon keeps the statements the parser found.
|
|
9
|
+
*/
|
|
10
|
+
import type { Node } from 'oxc-parser';
|
|
11
|
+
import { forEachChild } from './ast';
|
|
12
|
+
import type { PassContext } from './context';
|
|
13
|
+
|
|
14
|
+
const TERMINATED = new Set([
|
|
15
|
+
'BreakStatement',
|
|
16
|
+
'ContinueStatement',
|
|
17
|
+
'DebuggerStatement',
|
|
18
|
+
'DoWhileStatement',
|
|
19
|
+
'ExportAllDeclaration',
|
|
20
|
+
'ExpressionStatement',
|
|
21
|
+
'ImportDeclaration',
|
|
22
|
+
'ReturnStatement',
|
|
23
|
+
'ThrowStatement',
|
|
24
|
+
'TSTypeAliasDeclaration',
|
|
25
|
+
'VariableDeclaration',
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
const LOOP_HEADS = new Set(['ForStatement', 'ForInStatement', 'ForOfStatement']);
|
|
29
|
+
|
|
30
|
+
function isTerminated(node: Node, context: PassContext): boolean {
|
|
31
|
+
if (node.type === 'VariableDeclaration') {
|
|
32
|
+
const parent = context.parents.get(node);
|
|
33
|
+
return !(parent && LOOP_HEADS.has(parent.type) && Reflect.get(parent, 'body') !== node);
|
|
34
|
+
}
|
|
35
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
36
|
+
return !node.declaration;
|
|
37
|
+
}
|
|
38
|
+
if (node.type === 'ExportDefaultDeclaration') {
|
|
39
|
+
const { type } = node.declaration;
|
|
40
|
+
return type !== 'FunctionDeclaration' && type !== 'ClassDeclaration';
|
|
41
|
+
}
|
|
42
|
+
return TERMINATED.has(node.type);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const TRIVIA = /(?:\s|\/\/[^\n\r\u2028\u2029]*|\/\*[\s\S]*?\*\/)*/y;
|
|
46
|
+
|
|
47
|
+
/** Whether code other than the closing brace of a block follows `position`. */
|
|
48
|
+
function hasCodeAfter(code: string, position: number): boolean {
|
|
49
|
+
TRIVIA.lastIndex = position;
|
|
50
|
+
TRIVIA.exec(code);
|
|
51
|
+
return TRIVIA.lastIndex < code.length && code[TRIVIA.lastIndex] !== '}';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function terminateStatements(context: PassContext): boolean {
|
|
55
|
+
const { code, s } = context;
|
|
56
|
+
let changed = false;
|
|
57
|
+
const visit = (node: Node): void => {
|
|
58
|
+
if (isTerminated(node, context) && code[node.end - 1] !== ';' && hasCodeAfter(code, node.end)) {
|
|
59
|
+
s.appendLeft(node.end, ';');
|
|
60
|
+
changed = true;
|
|
61
|
+
}
|
|
62
|
+
forEachChild(node, visit);
|
|
63
|
+
};
|
|
64
|
+
visit(context.program);
|
|
65
|
+
return changed;
|
|
66
|
+
}
|
package/src/vite/index.ts
CHANGED
|
@@ -32,6 +32,7 @@ import { collectParents, forEachChild, isIntrinsicTag, parse } from '../ast';
|
|
|
32
32
|
import { analyzeScopes } from '../scope';
|
|
33
33
|
import { CONTEXT_SAFE_PRIMITIVES } from '../provider';
|
|
34
34
|
import type { ImportedConstants } from '../constants';
|
|
35
|
+
import { runtimeImportSources } from '../constants';
|
|
35
36
|
import type { Primitive } from '../value';
|
|
36
37
|
import { sharedGlobals } from './globals';
|
|
37
38
|
import type { Skip } from '../context';
|
|
@@ -694,54 +695,93 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
694
695
|
*/
|
|
695
696
|
const recorded = new Map<string, string>();
|
|
696
697
|
|
|
697
|
-
/**
|
|
698
|
+
/**
|
|
699
|
+
* Whether components can be copied from a module: one of the app's, or
|
|
700
|
+
* one of a package listed in `optimizer.packages`.
|
|
701
|
+
*/
|
|
702
|
+
const isCopyable = (id: string): boolean => {
|
|
703
|
+
const file = stripQuery(id).replaceAll('\\', '/');
|
|
704
|
+
if (!file.includes('/node_modules/')) {
|
|
705
|
+
return true;
|
|
706
|
+
}
|
|
707
|
+
for (const name of optimizer.packages ?? []) {
|
|
708
|
+
if (file.includes(`/node_modules/${name}/`)) {
|
|
709
|
+
return true;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return false;
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
/** The modules each module imports, read from its file. */
|
|
698
716
|
const localImports = new Map<string, Promise<string[]>>();
|
|
699
717
|
|
|
700
718
|
/**
|
|
701
|
-
* The
|
|
702
|
-
*
|
|
703
|
-
*
|
|
704
|
-
*
|
|
719
|
+
* The code of a module as its file holds it. A module with no file, like
|
|
720
|
+
* a virtual module a plugin generates, is loaded instead, unless it has
|
|
721
|
+
* JSX: this plugin transforms such a module, so loading it could wait for
|
|
722
|
+
* the transform that asks. Its imports are then not followed.
|
|
723
|
+
*/
|
|
724
|
+
async function readModuleCode(
|
|
725
|
+
context: Rollup.PluginContext,
|
|
726
|
+
id: string,
|
|
727
|
+
): Promise<string | undefined> {
|
|
728
|
+
try {
|
|
729
|
+
return await readFile(stripQuery(id), 'utf8');
|
|
730
|
+
} catch {
|
|
731
|
+
if (isJSXModule(id)) {
|
|
732
|
+
return undefined;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
try {
|
|
736
|
+
return (await context.load({ id })).code ?? undefined;
|
|
737
|
+
} catch {
|
|
738
|
+
return undefined;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* The modules a module imports at runtime that a transform can wait for:
|
|
744
|
+
* the app's, those of packages listed in `optimizer.packages`, and virtual
|
|
745
|
+
* modules. A transform only waits for a module it copies from, and it only
|
|
746
|
+
* copies from those, so a module of another package is never in a loop of
|
|
747
|
+
* transforms that wait for each other. A virtual module can re-export the
|
|
748
|
+
* app, so a listed package can import the app back through one, like a
|
|
749
|
+
* docs framework that imports its theme's layout.
|
|
750
|
+
*
|
|
751
|
+
* Imports only used as types are left out, since TypeScript drops them,
|
|
752
|
+
* and so are Solid's own modules and Node's built-ins. The imports come
|
|
753
|
+
* from the file on disk, so they do not depend on the order modules are
|
|
754
|
+
* transformed in. Modules without JSX count too, since reading their
|
|
755
|
+
* constants can load a module with JSX.
|
|
705
756
|
*/
|
|
706
757
|
async function findLocalImports(context: Rollup.PluginContext, id: string): Promise<string[]> {
|
|
707
758
|
let pending = localImports.get(id);
|
|
708
759
|
if (!pending) {
|
|
709
760
|
pending = (async () => {
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
code = await readFile(stripQuery(id), 'utf8');
|
|
713
|
-
} catch {
|
|
761
|
+
const code = await readModuleCode(context, id);
|
|
762
|
+
if (code === undefined) {
|
|
714
763
|
return [];
|
|
715
764
|
}
|
|
716
765
|
let program: Program;
|
|
717
766
|
try {
|
|
718
|
-
|
|
767
|
+
// A loaded module is JavaScript, whatever its id says.
|
|
768
|
+
program = parse(
|
|
769
|
+
/\.[cm]?[jt]sx?$/.test(stripQuery(id)) ? stripQuery(id) : `${id}.js`,
|
|
770
|
+
code,
|
|
771
|
+
);
|
|
719
772
|
} catch {
|
|
720
773
|
return [];
|
|
721
774
|
}
|
|
722
|
-
const sources = new Set<string>();
|
|
723
|
-
for (const statement of program.body) {
|
|
724
|
-
if (
|
|
725
|
-
(statement.type === 'ImportDeclaration' && statement.importKind !== 'type') ||
|
|
726
|
-
((statement.type === 'ExportNamedDeclaration' ||
|
|
727
|
-
statement.type === 'ExportAllDeclaration') &&
|
|
728
|
-
statement.source &&
|
|
729
|
-
statement.exportKind !== 'type')
|
|
730
|
-
) {
|
|
731
|
-
const value = statement.source?.value;
|
|
732
|
-
if (value !== undefined && !moduleSources.includes(value)) {
|
|
733
|
-
sources.add(value);
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
775
|
const resolving = [];
|
|
738
|
-
for (const source of
|
|
739
|
-
|
|
776
|
+
for (const source of runtimeImportSources(program)) {
|
|
777
|
+
if (!moduleSources.includes(source) && !source.startsWith('node:')) {
|
|
778
|
+
resolving.push(context.resolve(source, id));
|
|
779
|
+
}
|
|
740
780
|
}
|
|
741
781
|
const resolved = await Promise.all(resolving);
|
|
742
782
|
const local: string[] = [];
|
|
743
783
|
for (const target of resolved) {
|
|
744
|
-
if (target && !target.external &&
|
|
784
|
+
if (target && !target.external && isCopyable(target.id)) {
|
|
745
785
|
local.push(target.id);
|
|
746
786
|
}
|
|
747
787
|
}
|
|
@@ -752,7 +792,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
752
792
|
return pending;
|
|
753
793
|
}
|
|
754
794
|
|
|
755
|
-
/** Whether `from` imports `to`, directly or through other modules
|
|
795
|
+
/** Whether `from` imports `to`, directly or through other modules. */
|
|
756
796
|
async function reaches(
|
|
757
797
|
context: Rollup.PluginContext,
|
|
758
798
|
from: string,
|
|
@@ -954,23 +994,6 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
954
994
|
readonly skips: Skip[] | undefined;
|
|
955
995
|
}
|
|
956
996
|
|
|
957
|
-
/**
|
|
958
|
-
* Whether components can be copied from a module: one of the app's, or
|
|
959
|
-
* one of a package listed in `optimizer.packages`.
|
|
960
|
-
*/
|
|
961
|
-
const isCopyable = (id: string): boolean => {
|
|
962
|
-
const file = stripQuery(id).replaceAll('\\', '/');
|
|
963
|
-
if (!file.includes('/node_modules/')) {
|
|
964
|
-
return true;
|
|
965
|
-
}
|
|
966
|
-
for (const name of optimizer.packages ?? []) {
|
|
967
|
-
if (file.includes(`/node_modules/${name}/`)) {
|
|
968
|
-
return true;
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
return false;
|
|
972
|
-
};
|
|
973
|
-
|
|
974
997
|
/** The listed packages already warned about, by environment. */
|
|
975
998
|
const warnedPackages = new Set<string>();
|
|
976
999
|
|
|
@@ -1089,7 +1112,20 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1089
1112
|
skip('copy-external');
|
|
1090
1113
|
return undefined;
|
|
1091
1114
|
}
|
|
1092
|
-
|
|
1115
|
+
if (!target.id.startsWith('\0')) {
|
|
1116
|
+
targets.set(dependency.source, target.external ? dependency.source : target.id);
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
// A virtual module's id cannot be imported again, so the copy keeps
|
|
1120
|
+
// the specifier, when it means the same module from the importer.
|
|
1121
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
1122
|
+
const fromImporter = await context.resolve(dependency.source, id);
|
|
1123
|
+
if (fromImporter?.id !== target.id) {
|
|
1124
|
+
state.excluded.add(key);
|
|
1125
|
+
skip('copy-virtual');
|
|
1126
|
+
return undefined;
|
|
1127
|
+
}
|
|
1128
|
+
targets.set(dependency.source, dependency.source);
|
|
1093
1129
|
}
|
|
1094
1130
|
}
|
|
1095
1131
|
const rename = new Map<string, string>();
|