solid-optimizer 1.0.0-next.0 → 1.0.0-next.10
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 +27 -13
- package/dist/{compile-Cz6hbiQv.mjs → compile-Cvjy2e11.mjs} +1925 -385
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-BRCj4UN7.d.mts → compile-ioIkBKzR.d.mts} +15 -2
- package/dist/compile-ioIkBKzR.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +12 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +401 -116
- package/dist/vite.mjs.map +1 -1
- package/package.json +8 -5
- package/src/ast.ts +4 -3
- package/src/compile.ts +96 -16
- package/src/conditional.ts +547 -0
- package/src/constants.ts +58 -13
- package/src/context.ts +5 -3
- package/src/copy-size.ts +68 -27
- package/src/fold.ts +334 -107
- package/src/inline.ts +832 -177
- package/src/jsx.ts +55 -34
- package/src/memo.ts +309 -38
- package/src/nesting.ts +325 -0
- package/src/provider.ts +62 -40
- package/src/scope.ts +115 -9
- package/src/size.ts +6 -1
- package/src/terminate.ts +66 -0
- package/src/text.ts +297 -0
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +419 -117
- package/src/vite/inject.ts +91 -16
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-Cz6hbiQv.mjs.map +0 -1
package/dist/vite.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as analyzeScopes, c as collectParents, d as parse, f as unwrap, i as runtimeImportSources, l as forEachChild, n as readModuleConstants, o as lookup, r as CONTEXT_SAFE_PRIMITIVES, s as scopeAt, t as compile, u as isIntrinsicTag } from "./compile-Cvjy2e11.mjs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import remapping from "@jridgewell/remapping";
|
|
4
|
-
import MagicString from "magic-string";
|
|
5
4
|
import { parseSync } from "oxc-parser";
|
|
5
|
+
import MagicString from "magic-string";
|
|
6
6
|
import { readFile } from "node:fs/promises";
|
|
7
7
|
import path from "node:path";
|
|
8
|
+
import { gzipSync } from "node:zlib";
|
|
8
9
|
import solidPlugin from "@solidjs/vite-plugin";
|
|
9
|
-
import { createFilter, transformWithOxc } from "vite";
|
|
10
|
+
import { createFilter, minifySync, transformWithOxc } from "vite";
|
|
10
11
|
//#region src/vite/globals.ts
|
|
11
12
|
/**
|
|
12
13
|
* The `define` entries and `import.meta.env` values that are the same in
|
|
@@ -18,12 +19,23 @@ function sharedGlobals(config) {
|
|
|
18
19
|
const globals = {};
|
|
19
20
|
for (const [key, value] of Object.entries(config.env)) if (key !== "SSR" && (typeof value === "string" || typeof value === "number" || typeof value === "boolean")) globals[`import.meta.env.${key}`] = value;
|
|
20
21
|
const environments = Object.values(config.environments);
|
|
21
|
-
const defines =
|
|
22
|
-
const
|
|
22
|
+
const defines = [];
|
|
23
|
+
if (environments.length > 0) for (const environment of environments) defines.push(environment.define ?? config.define ?? {});
|
|
24
|
+
else defines.push(config.define ?? {});
|
|
25
|
+
const keys = /* @__PURE__ */ new Set();
|
|
26
|
+
for (const define of defines) for (const key of Object.keys(define)) keys.add(key);
|
|
23
27
|
for (const key of keys) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
let first;
|
|
29
|
+
let same = true;
|
|
30
|
+
for (const define of defines) {
|
|
31
|
+
const value = defineValue(define[key]);
|
|
32
|
+
first ??= value;
|
|
33
|
+
if (value === void 0 || !Object.is(value.value, first?.value)) {
|
|
34
|
+
same = false;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (first !== void 0 && same) globals[key] = first.value;
|
|
27
39
|
}
|
|
28
40
|
return globals;
|
|
29
41
|
}
|
|
@@ -57,12 +69,18 @@ function summarizeSkips(skips) {
|
|
|
57
69
|
group.names.set(skip.name, (group.names.get(skip.name) ?? 0) + 1);
|
|
58
70
|
groups.set(key, group);
|
|
59
71
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
const summaries = [];
|
|
73
|
+
for (const { pass, reason, names } of groups.values()) {
|
|
74
|
+
let count = 0;
|
|
75
|
+
for (const times of names.values()) count += times;
|
|
76
|
+
summaries.push({
|
|
77
|
+
pass,
|
|
78
|
+
reason,
|
|
79
|
+
count,
|
|
80
|
+
names: [...names].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return summaries.sort((a, b) => b.count - a.count || a.pass.localeCompare(b.pass) || a.reason.localeCompare(b.reason));
|
|
66
84
|
}
|
|
67
85
|
/** The most names listed for a reason. */
|
|
68
86
|
const NAMES_SHOWN = 6;
|
|
@@ -73,9 +91,11 @@ function formatReport(report) {
|
|
|
73
91
|
const lines = [`solid-optimizer: what stays as written in ${report.environment}`];
|
|
74
92
|
const summaries = summarizeSkips(report.skips);
|
|
75
93
|
if (summaries.length === 0) lines.push(" nothing");
|
|
76
|
-
|
|
94
|
+
let reasonWidth = 0;
|
|
95
|
+
for (const summary of summaries) reasonWidth = Math.max(reasonWidth, summary.reason.length);
|
|
77
96
|
for (const summary of summaries) {
|
|
78
|
-
const shown =
|
|
97
|
+
const shown = [];
|
|
98
|
+
for (const [name, count] of summary.names.slice(0, NAMES_SHOWN)) shown.push(count > 1 ? `${name}×${String(count)}` : name);
|
|
79
99
|
if (summary.names.length > NAMES_SHOWN) shown.push(`${String(summary.names.length - NAMES_SHOWN)} more`);
|
|
80
100
|
lines.push(` ${summary.pass.padEnd(8)} ${summary.reason.padEnd(reasonWidth)} ${String(summary.count).padStart(4)} ${shown.join(", ")}`);
|
|
81
101
|
}
|
|
@@ -127,6 +147,14 @@ function freshName(scopes, base) {
|
|
|
127
147
|
scopes.names.add(name);
|
|
128
148
|
return name;
|
|
129
149
|
}
|
|
150
|
+
function isAllGenerated(specifiers) {
|
|
151
|
+
for (const specifier of specifiers) if (!isGeneratedSpecifier(specifier)) return false;
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
function hasStatement(functions, statement) {
|
|
155
|
+
for (const entry of functions) if (entry.statement === statement) return true;
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
130
158
|
/**
|
|
131
159
|
* Whether an import specifier was added by Solid's JSX transform, which
|
|
132
160
|
* names every helper it imports `_$name`.
|
|
@@ -217,7 +245,8 @@ function addMarker(code, filename, options) {
|
|
|
217
245
|
}
|
|
218
246
|
const call = `${MARKER}({ ${entries.join(", ")} })`;
|
|
219
247
|
const s = new MagicString(code);
|
|
220
|
-
const functions =
|
|
248
|
+
const functions = [];
|
|
249
|
+
for (const entry of topLevelFunctions(program)) if (containsJSX(entry.fn)) functions.push(entry);
|
|
221
250
|
for (const { fn } of functions) {
|
|
222
251
|
if (fn.type !== "FunctionDeclaration" && fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression") continue;
|
|
223
252
|
const { body } = fn;
|
|
@@ -228,7 +257,11 @@ function addMarker(code, filename, options) {
|
|
|
228
257
|
s.appendLeft(body.end, "; }");
|
|
229
258
|
}
|
|
230
259
|
}
|
|
231
|
-
|
|
260
|
+
let outside = false;
|
|
261
|
+
for (const statement of program.body) if (!hasStatement(functions, statement) && containsJSX(statement)) {
|
|
262
|
+
outside = true;
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
232
265
|
s.append(`\nimport { ${specifiers.join(", ")} } from ${JSON.stringify(options.moduleName)};\n` + (outside ? `${call};\n` : ""));
|
|
233
266
|
return {
|
|
234
267
|
code: s.toString(),
|
|
@@ -317,7 +350,7 @@ function linkHelpers(code, filename, moduleName, helpers) {
|
|
|
317
350
|
const parents = collectParents(program);
|
|
318
351
|
const s = new MagicString(code);
|
|
319
352
|
for (const statement of program.body) {
|
|
320
|
-
if (statement.type !== "ImportDeclaration" || statement.source.value !== moduleName || !statement.specifiers
|
|
353
|
+
if (statement.type !== "ImportDeclaration" || statement.source.value !== moduleName || !isAllGenerated(statement.specifiers)) continue;
|
|
321
354
|
for (const specifier of statement.specifiers) {
|
|
322
355
|
if (specifier.type !== "ImportSpecifier") continue;
|
|
323
356
|
const imported = exportedName(specifier);
|
|
@@ -383,7 +416,7 @@ function isCapitalized(name) {
|
|
|
383
416
|
* The function a top-level binding holds: `function A() {}`, or
|
|
384
417
|
* `const A = () => ...`.
|
|
385
418
|
*/
|
|
386
|
-
function
|
|
419
|
+
function findTopLevelFunction(binding) {
|
|
387
420
|
const { declaration } = binding;
|
|
388
421
|
if (declaration.type === "FunctionDeclaration") return {
|
|
389
422
|
binding,
|
|
@@ -400,9 +433,51 @@ function functionOf(binding) {
|
|
|
400
433
|
}
|
|
401
434
|
}
|
|
402
435
|
/**
|
|
436
|
+
* The binding a top-level alias like `const Button = ButtonRoot` names, or
|
|
437
|
+
* the binding itself when it is not one. Libraries often export a component
|
|
438
|
+
* under such a second name.
|
|
439
|
+
*/
|
|
440
|
+
function aliasedBinding(binding) {
|
|
441
|
+
let current = binding;
|
|
442
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
443
|
+
const { declaration } = current;
|
|
444
|
+
if (declaration.type !== "VariableDeclarator" || current.kind !== "const" || current.mutated || declaration.id !== current.identifier || !declaration.init) return current;
|
|
445
|
+
const init = unwrap(declaration.init);
|
|
446
|
+
const target = init.type === "Identifier" ? current.scope.bindings.get(init.name) : void 0;
|
|
447
|
+
if (!target || target === current || target.mutated) return current;
|
|
448
|
+
current = target;
|
|
449
|
+
}
|
|
450
|
+
return current;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Where a module re-exports `exportName` from, like
|
|
454
|
+
* `export { t as Separator } from './chunk.jsx'`, or `undefined` when it
|
|
455
|
+
* does not re-export it by name.
|
|
456
|
+
*/
|
|
457
|
+
function readReexport(code, filename, exportName) {
|
|
458
|
+
const program = parse(filename, code);
|
|
459
|
+
for (const statement of program.body) {
|
|
460
|
+
if (statement.type !== "ExportNamedDeclaration" || !statement.source || statement.exportKind === "type") continue;
|
|
461
|
+
for (const specifier of statement.specifiers) {
|
|
462
|
+
const exported = specifier.exported.type === "Identifier" ? specifier.exported.name : specifier.exported.value;
|
|
463
|
+
const local = specifier.local.type === "Identifier" ? specifier.local.name : specifier.local.value;
|
|
464
|
+
if (exported === exportName && specifier.exportKind !== "type") return {
|
|
465
|
+
source: statement.source.value,
|
|
466
|
+
imported: local
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
function addDeclared(exports, declarators, bindings) {
|
|
472
|
+
for (const declarator of declarators) {
|
|
473
|
+
const binding = declarator.id.type === "Identifier" && declarator.id.name !== void 0 ? bindings.get(declarator.id.name) : void 0;
|
|
474
|
+
if (binding !== void 0) exports.set(binding.name, binding);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
403
478
|
* The names a module exports, each with the top-level binding it exports.
|
|
404
479
|
*/
|
|
405
|
-
function
|
|
480
|
+
function collectExports(program, scopes) {
|
|
406
481
|
const exports = /* @__PURE__ */ new Map();
|
|
407
482
|
const { bindings } = scopes.root;
|
|
408
483
|
for (const statement of program.body) if (statement.type === "ExportNamedDeclaration" && !statement.source) {
|
|
@@ -410,10 +485,7 @@ function exportsOf(program, scopes) {
|
|
|
410
485
|
if (declaration?.type === "FunctionDeclaration" && declaration.id) {
|
|
411
486
|
const binding = bindings.get(declaration.id.name);
|
|
412
487
|
if (binding) exports.set(declaration.id.name, binding);
|
|
413
|
-
} else if (declaration?.type === "VariableDeclaration")
|
|
414
|
-
const declared = declaration.declarations.map((declarator) => declarator.id.type === "Identifier" ? bindings.get(declarator.id.name) : void 0);
|
|
415
|
-
for (const binding of declared.filter((item) => item !== void 0)) exports.set(binding.name, binding);
|
|
416
|
-
}
|
|
488
|
+
} else if (declaration?.type === "VariableDeclaration") addDeclared(exports, declaration.declarations, bindings);
|
|
417
489
|
for (const specifier of statement.specifiers) {
|
|
418
490
|
const local = specifier.local.type === "Identifier" ? specifier.local.name : void 0;
|
|
419
491
|
const exported = specifier.exported.type === "Identifier" ? specifier.exported.name : specifier.exported.value;
|
|
@@ -448,17 +520,18 @@ function outerBindings(fn, self, scopes) {
|
|
|
448
520
|
function exposeLocals(code, filename) {
|
|
449
521
|
const program = parse(filename, code);
|
|
450
522
|
const scopes = analyzeScopes(program);
|
|
451
|
-
const exports =
|
|
523
|
+
const exports = collectExports(program, scopes);
|
|
452
524
|
const exported = new Set(exports.values());
|
|
453
525
|
const needed = /* @__PURE__ */ new Set();
|
|
454
526
|
for (const [name, binding] of exports) {
|
|
455
|
-
const component = isCapitalized(binding.name) || name === "default" ?
|
|
527
|
+
const component = isCapitalized(binding.name) || name === "default" ? findTopLevelFunction(binding) : void 0;
|
|
456
528
|
if (!component) continue;
|
|
457
529
|
for (const outer of outerBindings(component.fn, binding, scopes).values()) if (outer.kind !== "import" && !exported.has(outer)) needed.add(outer);
|
|
458
530
|
}
|
|
459
531
|
if (needed.size === 0) return;
|
|
460
532
|
const s = new MagicString(code);
|
|
461
|
-
const specifiers = [
|
|
533
|
+
const specifiers = [];
|
|
534
|
+
for (const binding of needed) specifiers.push(`${binding.name} as ${LOCAL_EXPORT}${binding.name}`);
|
|
462
535
|
s.append(`\nexport { ${specifiers.join(", ")} };\n`);
|
|
463
536
|
return {
|
|
464
537
|
code: s.toString(),
|
|
@@ -493,9 +566,10 @@ function returnsLocalCall(fn, scopes, moduleSources) {
|
|
|
493
566
|
function readExportedComponent(code, filename, exportName, moduleSources) {
|
|
494
567
|
const program = parse(filename, code);
|
|
495
568
|
const scopes = analyzeScopes(program);
|
|
496
|
-
const exports =
|
|
497
|
-
const
|
|
498
|
-
const
|
|
569
|
+
const exports = collectExports(program, scopes);
|
|
570
|
+
const named = exports.get(exportName);
|
|
571
|
+
const binding = named ? aliasedBinding(named) : void 0;
|
|
572
|
+
const component = binding ? findTopLevelFunction(binding) : void 0;
|
|
499
573
|
if (!binding || !component) return;
|
|
500
574
|
const exportNames = /* @__PURE__ */ new Map();
|
|
501
575
|
for (const [name, exported] of exports) if (!exportNames.has(exported)) exportNames.set(exported, name);
|
|
@@ -717,11 +791,16 @@ var Usage = class {
|
|
|
717
791
|
const users = /* @__PURE__ */ new Set([user]);
|
|
718
792
|
for (const [importer, containers] of usage.importers) {
|
|
719
793
|
const moved = absorbed.get(importer);
|
|
720
|
-
users.add(
|
|
794
|
+
users.add(hasEvery(containers, moved) ? user : importer);
|
|
721
795
|
}
|
|
722
|
-
|
|
796
|
+
for (const id of users) if (this.reaches(module, id)) return 0;
|
|
797
|
+
return users.size;
|
|
723
798
|
}
|
|
724
799
|
};
|
|
800
|
+
function hasEvery(names, set) {
|
|
801
|
+
for (const name of names) if (set?.has(name) !== true) return false;
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
725
804
|
function isTagName(reference, parents) {
|
|
726
805
|
const parent = parents.get(reference);
|
|
727
806
|
return parent?.type === "JSXOpeningElement" && parent.name === reference;
|
|
@@ -733,7 +812,11 @@ function isClosingTagName(reference, parents) {
|
|
|
733
812
|
/** The names a declaration exported with `export` declares. */
|
|
734
813
|
function declaredNames(declaration) {
|
|
735
814
|
if (declaration?.type === "FunctionDeclaration" && declaration.id) return [declaration.id.name];
|
|
736
|
-
if (declaration?.type === "VariableDeclaration")
|
|
815
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
816
|
+
const names = [];
|
|
817
|
+
for (const declarator of declaration.declarations) if (declarator.id.type === "Identifier") names.push(declarator.id.name);
|
|
818
|
+
return names;
|
|
819
|
+
}
|
|
737
820
|
return [];
|
|
738
821
|
}
|
|
739
822
|
/** The top-level bindings a module exports, each with its export names. */
|
|
@@ -761,7 +844,7 @@ function recordModule(usage, id, program, targets) {
|
|
|
761
844
|
const scopes = analyzeScopes(program);
|
|
762
845
|
const parents = collectParents(program);
|
|
763
846
|
const exported = exportedBindings(program, scopes);
|
|
764
|
-
const
|
|
847
|
+
const findContainers = (reference) => {
|
|
765
848
|
let node = reference;
|
|
766
849
|
let parent = parents.get(node);
|
|
767
850
|
while (parent && parent.type !== "Program") {
|
|
@@ -774,7 +857,8 @@ function recordModule(usage, id, program, targets) {
|
|
|
774
857
|
else if (declaration.type === "VariableDeclaration") {
|
|
775
858
|
for (const declarator of declaration.declarations) if (declarator.id.type === "Identifier" && reference.start >= declarator.start && reference.end <= declarator.end) bindings.push(scopes.root.bindings.get(declarator.id.name));
|
|
776
859
|
}
|
|
777
|
-
const names =
|
|
860
|
+
const names = [];
|
|
861
|
+
for (const binding of bindings) if (binding) names.push(...exported.get(binding) ?? []);
|
|
778
862
|
return names.length > 0 ? names : [UNEXPORTED];
|
|
779
863
|
};
|
|
780
864
|
const record = (binding, target) => {
|
|
@@ -782,13 +866,13 @@ function recordModule(usage, id, program, targets) {
|
|
|
782
866
|
for (const reference of binding.references) {
|
|
783
867
|
if (isClosingTagName(reference, parents)) continue;
|
|
784
868
|
if (imported !== "*") {
|
|
785
|
-
if (isTagName(reference, parents)) usage.tag(target, imported, id,
|
|
869
|
+
if (isTagName(reference, parents)) usage.tag(target, imported, id, findContainers(reference));
|
|
786
870
|
else usage.other(target, imported);
|
|
787
871
|
continue;
|
|
788
872
|
}
|
|
789
873
|
const member = parents.get(reference);
|
|
790
874
|
if (member?.type === "JSXMemberExpression" && member.object === reference) {
|
|
791
|
-
if (isTagName(member, parents)) usage.tag(target, member.property.name, id,
|
|
875
|
+
if (isTagName(member, parents)) usage.tag(target, member.property.name, id, findContainers(reference));
|
|
792
876
|
else usage.other(target, member.property.name);
|
|
793
877
|
} else if (member?.type === "MemberExpression" && member.object === reference && !member.computed && member.property.type === "Identifier") usage.other(target, member.property.name);
|
|
794
878
|
else usage.all(target);
|
|
@@ -808,13 +892,20 @@ function recordModule(usage, id, program, targets) {
|
|
|
808
892
|
if (target !== void 0) usage.other(target, name);
|
|
809
893
|
}
|
|
810
894
|
}
|
|
811
|
-
for (const [binding, names] of exported)
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
895
|
+
for (const [binding, names] of exported) {
|
|
896
|
+
let used = false;
|
|
897
|
+
for (const reference of binding.references) {
|
|
898
|
+
const parent = parents.get(reference);
|
|
899
|
+
if (parent?.type !== "ExportSpecifier" && parent?.type !== "ExportDefaultDeclaration" && !isClosingTagName(reference, parents)) {
|
|
900
|
+
used = true;
|
|
901
|
+
break;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
if (used) for (const name of names) usage.other(id, name);
|
|
905
|
+
}
|
|
815
906
|
}
|
|
816
907
|
/** The import specifiers a module loads, statically or with `import()`. */
|
|
817
|
-
function
|
|
908
|
+
function collectSpecifiers(program) {
|
|
818
909
|
const statics = /* @__PURE__ */ new Set();
|
|
819
910
|
const dynamics = /* @__PURE__ */ new Set();
|
|
820
911
|
for (const statement of program.body) if (statement.type === "ImportDeclaration" && statement.importKind !== "type" || (statement.type === "ExportNamedDeclaration" || statement.type === "ExportAllDeclaration") && statement.source && statement.exportKind !== "type") {
|
|
@@ -839,10 +930,11 @@ async function buildUsageIndex(entries, host) {
|
|
|
839
930
|
const usage = new Usage();
|
|
840
931
|
for (const entry of entries) usage.all(entry);
|
|
841
932
|
const seen = /* @__PURE__ */ new Set();
|
|
842
|
-
let frontier =
|
|
933
|
+
let frontier = [];
|
|
934
|
+
for (const id of entries) if (SCRIPT.test(id.replace(/\?.*$/, ""))) frontier.push(id);
|
|
843
935
|
while (frontier.length > 0) {
|
|
844
936
|
for (const id of frontier) seen.add(id);
|
|
845
|
-
|
|
937
|
+
async function readModule(id) {
|
|
846
938
|
const code = await host.read(id);
|
|
847
939
|
let program;
|
|
848
940
|
try {
|
|
@@ -850,12 +942,15 @@ async function buildUsageIndex(entries, host) {
|
|
|
850
942
|
} catch {
|
|
851
943
|
return [];
|
|
852
944
|
}
|
|
853
|
-
const { static: statics, dynamic } =
|
|
945
|
+
const { static: statics, dynamic } = collectSpecifiers(program);
|
|
854
946
|
const targets = /* @__PURE__ */ new Map();
|
|
855
|
-
|
|
947
|
+
async function resolveSource(source) {
|
|
856
948
|
const target = await host.resolve(source, id);
|
|
857
949
|
if (target !== void 0) targets.set(source, target);
|
|
858
|
-
}
|
|
950
|
+
}
|
|
951
|
+
const resolving = [];
|
|
952
|
+
for (const source of [...statics, ...dynamic]) resolving.push(resolveSource(source));
|
|
953
|
+
await Promise.all(resolving);
|
|
859
954
|
for (const source of dynamic) {
|
|
860
955
|
const target = targets.get(source);
|
|
861
956
|
if (target !== void 0) usage.all(target);
|
|
@@ -863,8 +958,12 @@ async function buildUsageIndex(entries, host) {
|
|
|
863
958
|
recordModule(usage, id, program, targets);
|
|
864
959
|
usage.imports.set(id, [...targets.values()]);
|
|
865
960
|
return [...targets.values()];
|
|
866
|
-
}
|
|
867
|
-
|
|
961
|
+
}
|
|
962
|
+
const reading = [];
|
|
963
|
+
for (const id of frontier) reading.push(readModule(id));
|
|
964
|
+
const next = await Promise.all(reading);
|
|
965
|
+
frontier = [];
|
|
966
|
+
for (const id of new Set(next.flat())) if (!seen.has(id) && SCRIPT.test(id.replace(/\?.*$/, ""))) frontier.push(id);
|
|
868
967
|
}
|
|
869
968
|
return usage;
|
|
870
969
|
}
|
|
@@ -908,20 +1007,43 @@ const LAZY_PLACEHOLDER = /"__SOLID_LAZY_MODULE__:([^"]+)"/g;
|
|
|
908
1007
|
* The common kinds of attribute: a class, any other attribute, events, and
|
|
909
1008
|
* children. Inlining a component turns a spread onto an element into
|
|
910
1009
|
* attributes like these, in a module that may not use their helpers itself.
|
|
911
|
-
* A
|
|
912
|
-
*
|
|
1010
|
+
* A `ref` can come through a spread too, and a link with a dynamic attribute
|
|
1011
|
+
* claims its element. A rarer kind, like `style`, can still need a helper no
|
|
1012
|
+
* module imports. The chunk is then lowered without optimizing it.
|
|
913
1013
|
*/
|
|
914
1014
|
const ATTRIBUTE_SAMPLE = `const a = () => 0;
|
|
915
1015
|
export const Sample = () => (
|
|
916
|
-
<div class={a()} id={a()} onClick={a()} onScroll={a()}>
|
|
1016
|
+
<div class={a()} id={a()} onClick={a()} onScroll={a()}>
|
|
1017
|
+
{a()}
|
|
1018
|
+
<a href="/" title={a()} ref={a} />
|
|
1019
|
+
</div>
|
|
917
1020
|
);`;
|
|
918
|
-
/**
|
|
1021
|
+
/** The references that are the name of a JSX tag. */
|
|
1022
|
+
function findTags(references, parents) {
|
|
1023
|
+
const tags = [];
|
|
1024
|
+
for (const reference of references) {
|
|
1025
|
+
const parent = parents.get(reference);
|
|
1026
|
+
if ((parent?.type === "JSXOpeningElement" || parent?.type === "JSXClosingElement") && parent.name === reference) tags.push(reference);
|
|
1027
|
+
}
|
|
1028
|
+
return tags;
|
|
1029
|
+
}
|
|
1030
|
+
function findSkipReason(skipped, name) {
|
|
1031
|
+
for (const entry of skipped) if (entry.name === name) return entry.reason;
|
|
1032
|
+
}
|
|
1033
|
+
function hasSpreadAttribute(attributes) {
|
|
1034
|
+
for (const attribute of attributes) if (attribute.type === "JSXSpreadAttribute") return true;
|
|
1035
|
+
return false;
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Whether the code spreads anything onto an intrinsic element, or onto
|
|
1039
|
+
* `<Dynamic>`, which becomes one once its `component` is known.
|
|
1040
|
+
*/
|
|
919
1041
|
function spreadsOntoElement(code, filename) {
|
|
920
1042
|
if (!code.includes("...")) return false;
|
|
921
1043
|
let found = false;
|
|
922
1044
|
const visit = (node) => {
|
|
923
1045
|
if (found) return;
|
|
924
|
-
if (node.type === "JSXOpeningElement" && node.name.type === "JSXIdentifier" && isIntrinsicTag(node.name.name)
|
|
1046
|
+
if (node.type === "JSXOpeningElement" && node.name.type === "JSXIdentifier" && (isIntrinsicTag(node.name.name) || node.name.name === "Dynamic") && hasSpreadAttribute(node.attributes)) {
|
|
925
1047
|
found = true;
|
|
926
1048
|
return;
|
|
927
1049
|
}
|
|
@@ -930,6 +1052,23 @@ function spreadsOntoElement(code, filename) {
|
|
|
930
1052
|
visit(parse(filename, code));
|
|
931
1053
|
return found;
|
|
932
1054
|
}
|
|
1055
|
+
/**
|
|
1056
|
+
* How many bytes a chunk takes once minified and gzipped. Minifying drops
|
|
1057
|
+
* the runtime code nothing calls anymore, which is what the comparison is
|
|
1058
|
+
* for, so it counts even when the build does not minify.
|
|
1059
|
+
*/
|
|
1060
|
+
function shippedSize(candidate, filename) {
|
|
1061
|
+
const minified = minifySync(filename, candidate.linked.code, {
|
|
1062
|
+
module: true,
|
|
1063
|
+
compress: true,
|
|
1064
|
+
mangle: true
|
|
1065
|
+
});
|
|
1066
|
+
return gzipSync(minified.code).length;
|
|
1067
|
+
}
|
|
1068
|
+
/** Whether a module, or the module of a copy's key, is in `node_modules`. */
|
|
1069
|
+
function isPackageModule(id) {
|
|
1070
|
+
return stripQuery(id).replaceAll("\\", "/").includes("/node_modules/");
|
|
1071
|
+
}
|
|
933
1072
|
function loadCompiler() {
|
|
934
1073
|
const require = createRequire(import.meta.url);
|
|
935
1074
|
return createRequire(require.resolve("@solidjs/vite-plugin"))("@solidjs/compiler");
|
|
@@ -942,7 +1081,8 @@ function stripQuery(id) {
|
|
|
942
1081
|
* source maps, since chaining the maps of a large chunk takes a while.
|
|
943
1082
|
*/
|
|
944
1083
|
function combineMaps(wanted, maps) {
|
|
945
|
-
const present =
|
|
1084
|
+
const present = [];
|
|
1085
|
+
for (const map of maps) if (typeof map === "string") present.push(map);
|
|
946
1086
|
if (!wanted || present.length === 0) return null;
|
|
947
1087
|
return remapping(present.reverse(), () => null).toString();
|
|
948
1088
|
}
|
|
@@ -1002,6 +1142,7 @@ function solidOptimizer(options = {}) {
|
|
|
1002
1142
|
alwaysInline: optimizer.alwaysInline,
|
|
1003
1143
|
contexts: optimizer.contexts,
|
|
1004
1144
|
memos: optimizer.memos,
|
|
1145
|
+
conditionals: optimizer.conditionals,
|
|
1005
1146
|
maxPasses: optimizer.maxPasses,
|
|
1006
1147
|
builtIns: [...builtIns],
|
|
1007
1148
|
moduleSources,
|
|
@@ -1009,8 +1150,8 @@ function solidOptimizer(options = {}) {
|
|
|
1009
1150
|
};
|
|
1010
1151
|
/** What the final compile of each chunk or module left alone, by environment. */
|
|
1011
1152
|
const skips = /* @__PURE__ */ new Map();
|
|
1012
|
-
const addSkips = (context, list) => {
|
|
1013
|
-
if (!list) return;
|
|
1153
|
+
const addSkips = (context, list, module) => {
|
|
1154
|
+
if (!list || module !== void 0 && isPackageModule(module)) return;
|
|
1014
1155
|
const environment = context.environment?.name ?? "client";
|
|
1015
1156
|
const entries = skips.get(environment) ?? [];
|
|
1016
1157
|
entries.push(...list);
|
|
@@ -1133,20 +1274,25 @@ function solidOptimizer(options = {}) {
|
|
|
1133
1274
|
*/
|
|
1134
1275
|
async function importedConstants(context, imports, importer, visiting, transform) {
|
|
1135
1276
|
let partial = false;
|
|
1136
|
-
|
|
1277
|
+
async function readImport(specifier) {
|
|
1137
1278
|
const target = await resolveConstantModule(context, specifier, importer);
|
|
1138
1279
|
if (target === void 0 || visiting.has(target.id)) return;
|
|
1139
|
-
const result = await
|
|
1280
|
+
const result = await readConstants(context, target, visiting, transform);
|
|
1140
1281
|
partial ||= result.partial;
|
|
1141
1282
|
if (!result.constants || Object.keys(result.constants.exports).length === 0) return;
|
|
1142
1283
|
return [specifier, result.constants.exports];
|
|
1143
|
-
}
|
|
1284
|
+
}
|
|
1285
|
+
const reading = [];
|
|
1286
|
+
for (const specifier of imports) reading.push(readImport(specifier));
|
|
1287
|
+
const entries = await Promise.all(reading);
|
|
1288
|
+
const constants = {};
|
|
1289
|
+
for (const entry of entries) if (entry !== void 0) constants[entry[0]] = entry[1];
|
|
1144
1290
|
return {
|
|
1145
|
-
constants
|
|
1291
|
+
constants,
|
|
1146
1292
|
partial
|
|
1147
1293
|
};
|
|
1148
1294
|
}
|
|
1149
|
-
async function
|
|
1295
|
+
async function readConstants(context, target, visiting, transform) {
|
|
1150
1296
|
const { id } = target;
|
|
1151
1297
|
const known = moduleConstants.get(id);
|
|
1152
1298
|
if (known) return known;
|
|
@@ -1267,50 +1413,86 @@ function solidOptimizer(options = {}) {
|
|
|
1267
1413
|
* copies components from it.
|
|
1268
1414
|
*/
|
|
1269
1415
|
const recorded = /* @__PURE__ */ new Map();
|
|
1270
|
-
/**
|
|
1416
|
+
/**
|
|
1417
|
+
* Whether components can be copied from a module: one of the app's, or
|
|
1418
|
+
* one of a package listed in `optimizer.packages`.
|
|
1419
|
+
*/
|
|
1420
|
+
const isCopyable = (id) => {
|
|
1421
|
+
const file = stripQuery(id).replaceAll("\\", "/");
|
|
1422
|
+
if (!file.includes("/node_modules/")) return true;
|
|
1423
|
+
for (const name of optimizer.packages ?? []) if (file.includes(`/node_modules/${name}/`)) return true;
|
|
1424
|
+
return false;
|
|
1425
|
+
};
|
|
1426
|
+
/** The modules each module imports, read from its file. */
|
|
1271
1427
|
const localImports = /* @__PURE__ */ new Map();
|
|
1272
1428
|
/**
|
|
1273
|
-
* The
|
|
1274
|
-
*
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1429
|
+
* The code of a module as its file holds it. A module with no file, like
|
|
1430
|
+
* a virtual module a plugin generates, is loaded instead, unless it has
|
|
1431
|
+
* JSX: this plugin transforms such a module, so loading it could wait for
|
|
1432
|
+
* the transform that asks. Its imports are then not followed.
|
|
1433
|
+
*/
|
|
1434
|
+
async function readModuleCode(context, id) {
|
|
1435
|
+
try {
|
|
1436
|
+
return await readFile(stripQuery(id), "utf8");
|
|
1437
|
+
} catch {
|
|
1438
|
+
if (isJSXModule(id)) return;
|
|
1439
|
+
}
|
|
1440
|
+
try {
|
|
1441
|
+
return (await context.load({ id })).code ?? void 0;
|
|
1442
|
+
} catch {
|
|
1443
|
+
return;
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
/**
|
|
1447
|
+
* The modules a module imports at runtime that a transform can wait for:
|
|
1448
|
+
* the app's, those of packages listed in `optimizer.packages`, and virtual
|
|
1449
|
+
* modules. A transform only waits for a module it copies from, and it only
|
|
1450
|
+
* copies from those, so a module of another package is never in a loop of
|
|
1451
|
+
* transforms that wait for each other. A virtual module can re-export the
|
|
1452
|
+
* app, so a listed package can import the app back through one, like a
|
|
1453
|
+
* docs framework that imports its theme's layout.
|
|
1454
|
+
*
|
|
1455
|
+
* Imports only used as types are left out, since TypeScript drops them,
|
|
1456
|
+
* and so are Solid's own modules and Node's built-ins. The imports come
|
|
1457
|
+
* from the file on disk, so they do not depend on the order modules are
|
|
1458
|
+
* transformed in. Modules without JSX count too, since reading their
|
|
1459
|
+
* constants can load a module with JSX.
|
|
1277
1460
|
*/
|
|
1278
|
-
async function
|
|
1461
|
+
async function findLocalImports(context, id) {
|
|
1279
1462
|
let pending = localImports.get(id);
|
|
1280
1463
|
if (!pending) {
|
|
1281
1464
|
pending = (async () => {
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
code = await readFile(stripQuery(id), "utf8");
|
|
1285
|
-
} catch {
|
|
1286
|
-
return [];
|
|
1287
|
-
}
|
|
1465
|
+
const code = await readModuleCode(context, id);
|
|
1466
|
+
if (code === void 0) return [];
|
|
1288
1467
|
let program;
|
|
1289
1468
|
try {
|
|
1290
|
-
program = parse(stripQuery(id)
|
|
1469
|
+
program = parse(/\.[cm]?[jt]sx?$/.test(stripQuery(id)) ? stripQuery(id) : `${id}.js`, code);
|
|
1291
1470
|
} catch {
|
|
1292
1471
|
return [];
|
|
1293
1472
|
}
|
|
1294
|
-
const
|
|
1295
|
-
for (const
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
return
|
|
1473
|
+
const resolving = [];
|
|
1474
|
+
for (const source of runtimeImportSources(program)) if (!moduleSources.includes(source) && !source.startsWith("node:")) resolving.push(context.resolve(source, id));
|
|
1475
|
+
const resolved = await Promise.all(resolving);
|
|
1476
|
+
const local = [];
|
|
1477
|
+
for (const target of resolved) if (target && !target.external && isCopyable(target.id)) local.push(target.id);
|
|
1478
|
+
return local;
|
|
1300
1479
|
})();
|
|
1301
1480
|
localImports.set(id, pending);
|
|
1302
1481
|
}
|
|
1303
1482
|
return pending;
|
|
1304
1483
|
}
|
|
1305
|
-
/** Whether `from` imports `to`, directly or through other modules
|
|
1484
|
+
/** Whether `from` imports `to`, directly or through other modules. */
|
|
1306
1485
|
async function reaches(context, from, to) {
|
|
1307
1486
|
const seen = /* @__PURE__ */ new Set();
|
|
1308
1487
|
let frontier = [from];
|
|
1309
1488
|
while (frontier.length > 0) {
|
|
1310
1489
|
if (frontier.includes(to)) return true;
|
|
1311
1490
|
for (const id of frontier) seen.add(id);
|
|
1312
|
-
const
|
|
1313
|
-
|
|
1491
|
+
const reading = [];
|
|
1492
|
+
for (const id of frontier) reading.push(findLocalImports(context, id));
|
|
1493
|
+
const next = await Promise.all(reading);
|
|
1494
|
+
frontier = [];
|
|
1495
|
+
for (const id of new Set(next.flat())) if (!seen.has(id)) frontier.push(id);
|
|
1314
1496
|
}
|
|
1315
1497
|
return false;
|
|
1316
1498
|
}
|
|
@@ -1331,6 +1513,19 @@ function solidOptimizer(options = {}) {
|
|
|
1331
1513
|
})).code;
|
|
1332
1514
|
}
|
|
1333
1515
|
/**
|
|
1516
|
+
* The scripts a virtual entry runs, like the server entry `start` mode
|
|
1517
|
+
* generates: the app's modules it imports. It has no file to read, and
|
|
1518
|
+
* the server and client builds have to see the same uses of each export.
|
|
1519
|
+
* `undefined` when the entry has not been loaded or imports none.
|
|
1520
|
+
*/
|
|
1521
|
+
function virtualEntryScripts(context, id) {
|
|
1522
|
+
const info = context.getModuleInfo(id);
|
|
1523
|
+
if (!info || info.code === null) return;
|
|
1524
|
+
const scripts = [];
|
|
1525
|
+
for (const imported of [...info.importedIds, ...info.dynamicallyImportedIds]) if (!imported.startsWith("\0") && !imported.includes("/node_modules/")) scripts.push(imported);
|
|
1526
|
+
return scripts.length > 0 ? scripts : void 0;
|
|
1527
|
+
}
|
|
1528
|
+
/**
|
|
1334
1529
|
* The scripts an entry runs: the entry itself, or the module scripts of an
|
|
1335
1530
|
* HTML entry. `undefined` when an HTML entry has an inline module script,
|
|
1336
1531
|
* whose imports the usage index cannot read.
|
|
@@ -1341,7 +1536,7 @@ function solidOptimizer(options = {}) {
|
|
|
1341
1536
|
try {
|
|
1342
1537
|
html = await readFile(stripQuery(id), "utf8");
|
|
1343
1538
|
} catch {
|
|
1344
|
-
return;
|
|
1539
|
+
return virtualEntryScripts(context, id);
|
|
1345
1540
|
}
|
|
1346
1541
|
const scripts = [];
|
|
1347
1542
|
for (const [tag] of html.matchAll(/<script\b[^>]*>/gi)) {
|
|
@@ -1364,8 +1559,19 @@ function solidOptimizer(options = {}) {
|
|
|
1364
1559
|
let pending = usageIndexes.get(key);
|
|
1365
1560
|
if (!pending) {
|
|
1366
1561
|
pending = (async () => {
|
|
1367
|
-
const
|
|
1368
|
-
|
|
1562
|
+
const reading = [];
|
|
1563
|
+
for (const id of context.getModuleIds()) if (context.getModuleInfo(id)?.isEntry === true) reading.push(scriptEntries(context, id));
|
|
1564
|
+
const entries = await Promise.all(reading);
|
|
1565
|
+
const scriptIds = [];
|
|
1566
|
+
let known = true;
|
|
1567
|
+
for (const scripts of entries) {
|
|
1568
|
+
if (scripts === void 0) {
|
|
1569
|
+
known = false;
|
|
1570
|
+
break;
|
|
1571
|
+
}
|
|
1572
|
+
scriptIds.push(...scripts);
|
|
1573
|
+
}
|
|
1574
|
+
return buildUsageIndex(known ? scriptIds : [], {
|
|
1369
1575
|
resolve: async (source, importer) => {
|
|
1370
1576
|
const resolved = await context.resolve(source, importer);
|
|
1371
1577
|
return !resolved || resolved.external || resolved.id.includes("/node_modules/") ? void 0 : resolved.id;
|
|
@@ -1392,27 +1598,59 @@ function solidOptimizer(options = {}) {
|
|
|
1392
1598
|
if (imported === "default") return `import ${local} from ${from};`;
|
|
1393
1599
|
return `import { ${imported} as ${local} } from ${from};`;
|
|
1394
1600
|
}
|
|
1601
|
+
/** The listed packages already warned about, by environment. */
|
|
1602
|
+
const warnedPackages = /* @__PURE__ */ new Set();
|
|
1603
|
+
/**
|
|
1604
|
+
* Warns once when a listed package is external, like a server build that
|
|
1605
|
+
* leaves it to Node. Its components are not copied there, so a build that
|
|
1606
|
+
* copies them would hydrate different markup.
|
|
1607
|
+
*/
|
|
1608
|
+
const warnExternalPackage = (context, source) => {
|
|
1609
|
+
let name;
|
|
1610
|
+
for (const item of optimizer.packages ?? []) if (source === item || source.startsWith(`${item}/`)) {
|
|
1611
|
+
name = item;
|
|
1612
|
+
break;
|
|
1613
|
+
}
|
|
1614
|
+
const key = environmentKey(context, name ?? "");
|
|
1615
|
+
if (name === void 0 || warnedPackages.has(key)) return;
|
|
1616
|
+
warnedPackages.add(key);
|
|
1617
|
+
context.warn(`[solid-optimizer] ${name} is external in this build, so its components are not copied. Bundle it, like with \`ssr.noExternal\`, so every build copies the same components.`);
|
|
1618
|
+
};
|
|
1395
1619
|
/**
|
|
1396
1620
|
* Reads the component `source` exports as `imported`, and imports what it
|
|
1397
1621
|
* needs into the importer. `undefined` when it cannot be copied.
|
|
1398
1622
|
*/
|
|
1399
|
-
async function copyImported(context, id, source,
|
|
1623
|
+
async function copyImported(context, id, source, requested, state) {
|
|
1400
1624
|
const skip = (reason) => {
|
|
1401
1625
|
state.skips?.push({
|
|
1402
1626
|
pass: "inline",
|
|
1403
1627
|
reason,
|
|
1404
|
-
name: `${
|
|
1628
|
+
name: `${requested} (${source})`
|
|
1405
1629
|
});
|
|
1406
1630
|
};
|
|
1407
|
-
|
|
1631
|
+
let imported = requested;
|
|
1632
|
+
let resolved = await context.resolve(source, id);
|
|
1408
1633
|
if (!resolved || resolved.external) {
|
|
1634
|
+
warnExternalPackage(context, source);
|
|
1409
1635
|
skip("copy-external");
|
|
1410
1636
|
return;
|
|
1411
1637
|
}
|
|
1412
|
-
if (resolved.id
|
|
1638
|
+
if (!isCopyable(resolved.id)) {
|
|
1413
1639
|
skip("copy-package");
|
|
1414
1640
|
return;
|
|
1415
1641
|
}
|
|
1642
|
+
for (let depth = 0; depth < 8 && isJSXModule(resolved.id); depth += 1) {
|
|
1643
|
+
const code = await state.load(resolved.id);
|
|
1644
|
+
const reexport = code === void 0 ? void 0 : readReexport(code, stripQuery(resolved.id), imported);
|
|
1645
|
+
if (!reexport) break;
|
|
1646
|
+
const next = await context.resolve(reexport.source, resolved.id);
|
|
1647
|
+
if (!next || next.external || !isCopyable(next.id)) {
|
|
1648
|
+
skip("copy-package");
|
|
1649
|
+
return;
|
|
1650
|
+
}
|
|
1651
|
+
resolved = next;
|
|
1652
|
+
imported = reexport.imported;
|
|
1653
|
+
}
|
|
1416
1654
|
if (resolved.id === id) return;
|
|
1417
1655
|
if (!isJSXModule(resolved.id)) {
|
|
1418
1656
|
skip("copy-not-jsx");
|
|
@@ -1445,7 +1683,16 @@ function solidOptimizer(options = {}) {
|
|
|
1445
1683
|
skip("copy-external");
|
|
1446
1684
|
return;
|
|
1447
1685
|
}
|
|
1448
|
-
|
|
1686
|
+
if (!target.id.startsWith("\0")) {
|
|
1687
|
+
targets.set(dependency.source, target.external ? dependency.source : target.id);
|
|
1688
|
+
continue;
|
|
1689
|
+
}
|
|
1690
|
+
if ((await context.resolve(dependency.source, id))?.id !== target.id) {
|
|
1691
|
+
state.excluded.add(key);
|
|
1692
|
+
skip("copy-virtual");
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
targets.set(dependency.source, dependency.source);
|
|
1449
1696
|
}
|
|
1450
1697
|
const rename = /* @__PURE__ */ new Map();
|
|
1451
1698
|
for (const [local, dependency] of component.dependencies) {
|
|
@@ -1479,7 +1726,7 @@ function solidOptimizer(options = {}) {
|
|
|
1479
1726
|
const excluded = /* @__PURE__ */ new Set();
|
|
1480
1727
|
const usage = await usageIndex(context);
|
|
1481
1728
|
const absorbed = /* @__PURE__ */ new Map();
|
|
1482
|
-
const
|
|
1729
|
+
const countUsers = (copy) => {
|
|
1483
1730
|
const [module = "", exported = ""] = copy.key.split("\0");
|
|
1484
1731
|
return usage.users(module, exported, id, absorbed);
|
|
1485
1732
|
};
|
|
@@ -1514,10 +1761,7 @@ function solidOptimizer(options = {}) {
|
|
|
1514
1761
|
if (statement.type !== "ImportDeclaration" || statement.importKind === "type" || moduleSources.includes(statement.source.value)) continue;
|
|
1515
1762
|
for (const specifier of statement.specifiers) {
|
|
1516
1763
|
const binding = scopes.root.bindings.get(specifier.local.name);
|
|
1517
|
-
const tags = (binding?.references ?? [])
|
|
1518
|
-
const parent = parents.get(reference);
|
|
1519
|
-
return (parent?.type === "JSXOpeningElement" || parent?.type === "JSXClosingElement") && parent.name === reference;
|
|
1520
|
-
});
|
|
1764
|
+
const tags = findTags(binding?.references ?? [], parents);
|
|
1521
1765
|
if (specifier.type === "ImportNamespaceSpecifier" || binding?.imported === void 0 || tags.length === 0) continue;
|
|
1522
1766
|
const copy = await copyImported(context, id, statement.source.value, binding.imported, {
|
|
1523
1767
|
excluded,
|
|
@@ -1529,8 +1773,10 @@ function solidOptimizer(options = {}) {
|
|
|
1529
1773
|
if (copy && copySkips) copyNames.set(copy.key, `${binding.imported} (${statement.source.value})`);
|
|
1530
1774
|
if (copy) {
|
|
1531
1775
|
const name = fresh(specifier.local.name);
|
|
1776
|
+
const fromPackage = isPackageModule(copy.key);
|
|
1532
1777
|
copies.push({
|
|
1533
1778
|
key: copy.key,
|
|
1779
|
+
fromPackage,
|
|
1534
1780
|
name,
|
|
1535
1781
|
tags,
|
|
1536
1782
|
text: copy.text(name)
|
|
@@ -1548,20 +1794,31 @@ function solidOptimizer(options = {}) {
|
|
|
1548
1794
|
edit.append(`\n${copy.text}\n`);
|
|
1549
1795
|
}
|
|
1550
1796
|
edit.append(`\n${imports.join("\n")}\n`);
|
|
1797
|
+
const importedContexts = {};
|
|
1798
|
+
for (const [module, exported] of contexts) importedContexts[module] = [...exported];
|
|
1799
|
+
const sharedComponents = {};
|
|
1800
|
+
const alwaysInlineComponents = [];
|
|
1801
|
+
for (const copy of copies) {
|
|
1802
|
+
const users = countUsers(copy);
|
|
1803
|
+
if (users !== 1) sharedComponents[copy.name] = users;
|
|
1804
|
+
if (copy.fromPackage) alwaysInlineComponents.push(copy.name);
|
|
1805
|
+
}
|
|
1551
1806
|
result = compile(edit.toString(), {
|
|
1552
1807
|
...compileOptionsOfModule,
|
|
1553
1808
|
filename,
|
|
1554
1809
|
sourceMap: wantsMaps(),
|
|
1555
|
-
importedContexts
|
|
1556
|
-
sharedComponents
|
|
1810
|
+
importedContexts,
|
|
1811
|
+
sharedComponents,
|
|
1812
|
+
alwaysInlineComponents
|
|
1557
1813
|
});
|
|
1558
1814
|
const left = analyzeScopes(parse(filename, result.code)).root.bindings;
|
|
1559
|
-
const stuck =
|
|
1815
|
+
const stuck = [];
|
|
1816
|
+
for (const copy of copies) if (left.has(copy.name)) stuck.push(copy);
|
|
1560
1817
|
if (stuck.length === 0) break;
|
|
1561
1818
|
for (const copy of stuck) {
|
|
1562
1819
|
excluded.add(copy.key);
|
|
1563
1820
|
copies.splice(copies.indexOf(copy), 1);
|
|
1564
|
-
const reason = result.skipped
|
|
1821
|
+
const reason = findSkipReason(result.skipped ?? [], copy.name);
|
|
1565
1822
|
copySkips?.push({
|
|
1566
1823
|
pass: "inline",
|
|
1567
1824
|
reason: `copy-${reason ?? "left"}`,
|
|
@@ -1571,7 +1828,9 @@ function solidOptimizer(options = {}) {
|
|
|
1571
1828
|
result = void 0;
|
|
1572
1829
|
}
|
|
1573
1830
|
if (!result || !edit) break;
|
|
1574
|
-
|
|
1831
|
+
const sole = [];
|
|
1832
|
+
for (const copy of copies) if (countUsers(copy) === 1) sole.push(copy);
|
|
1833
|
+
for (const copy of sole) {
|
|
1575
1834
|
const [module = "", exported = ""] = copy.key.split("\0");
|
|
1576
1835
|
absorbed.set(module, /* @__PURE__ */ new Set([...absorbed.get(module) ?? [], exported]));
|
|
1577
1836
|
}
|
|
@@ -1615,7 +1874,7 @@ function solidOptimizer(options = {}) {
|
|
|
1615
1874
|
if (moduleCompileOptions.inline !== false) {
|
|
1616
1875
|
const copySkips = compileOptions.report ? [] : void 0;
|
|
1617
1876
|
const imported = await inlineImports(context, code, id, moduleCompileOptions, async (target) => loadModule(context, id, target), copySkips);
|
|
1618
|
-
addSkips(context, copySkips);
|
|
1877
|
+
addSkips(context, copySkips, id);
|
|
1619
1878
|
if (imported) {
|
|
1620
1879
|
code = imported.code;
|
|
1621
1880
|
maps.push(...imported.maps);
|
|
@@ -1676,7 +1935,11 @@ function solidOptimizer(options = {}) {
|
|
|
1676
1935
|
map: combineMaps(wantsMaps(), maps)
|
|
1677
1936
|
};
|
|
1678
1937
|
}
|
|
1679
|
-
|
|
1938
|
+
let main;
|
|
1939
|
+
for (const plugin of plugins) if (plugin.name === "solid") {
|
|
1940
|
+
main = plugin;
|
|
1941
|
+
break;
|
|
1942
|
+
}
|
|
1680
1943
|
const originalTransform = main?.transform;
|
|
1681
1944
|
const originalHandler = typeof originalTransform === "function" ? originalTransform : originalTransform?.handler;
|
|
1682
1945
|
if (main && originalHandler) main.transform = async function transform(code, id, transformOptions) {
|
|
@@ -1713,7 +1976,7 @@ function solidOptimizer(options = {}) {
|
|
|
1713
1976
|
if (isBuild() && sourceOptions.inline !== false) {
|
|
1714
1977
|
const copySkips = compileOptions.report ? [] : void 0;
|
|
1715
1978
|
const imported = await inlineImports(this, code, id, sourceOptions, async (target) => loadRecorded(this, id, target), copySkips);
|
|
1716
|
-
addSkips(this, copySkips);
|
|
1979
|
+
addSkips(this, copySkips, id);
|
|
1717
1980
|
if (imported) {
|
|
1718
1981
|
code = imported.code;
|
|
1719
1982
|
maps.push(...imported.maps);
|
|
@@ -1725,7 +1988,7 @@ function solidOptimizer(options = {}) {
|
|
|
1725
1988
|
maps.push(exposed.map);
|
|
1726
1989
|
}
|
|
1727
1990
|
}
|
|
1728
|
-
addSkips(this, skipped);
|
|
1991
|
+
addSkips(this, skipped, id);
|
|
1729
1992
|
recorded.set(environmentKey(this, id), code);
|
|
1730
1993
|
if (code === source) return null;
|
|
1731
1994
|
return {
|
|
@@ -1766,23 +2029,45 @@ function solidOptimizer(options = {}) {
|
|
|
1766
2029
|
filename: `${filename}.jsx`,
|
|
1767
2030
|
sourceMap: wantsMaps()
|
|
1768
2031
|
});
|
|
1769
|
-
|
|
2032
|
+
const chunkOptions = {
|
|
1770
2033
|
...compileOptions,
|
|
1771
2034
|
filename,
|
|
1772
2035
|
sourceMap: wantsMaps(),
|
|
1773
2036
|
constantVars: true,
|
|
1774
2037
|
builtInAliases: runtime.builtInAliases
|
|
1775
|
-
}
|
|
2038
|
+
};
|
|
2039
|
+
let missing;
|
|
2040
|
+
const lowerCandidate = (optimized) => {
|
|
2041
|
+
const lowered = lowerChunk(optimized.code);
|
|
2042
|
+
try {
|
|
2043
|
+
return {
|
|
2044
|
+
optimized,
|
|
2045
|
+
lowered,
|
|
2046
|
+
linked: linkHelpers(lowered.code, filename, moduleName, runtime.helpers)
|
|
2047
|
+
};
|
|
2048
|
+
} catch (error) {
|
|
2049
|
+
if (!(error instanceof MissingHelperError)) throw error;
|
|
2050
|
+
missing ??= error;
|
|
2051
|
+
return;
|
|
2052
|
+
}
|
|
2053
|
+
};
|
|
2054
|
+
let chosen = lowerCandidate(compile(chunkCode, chunkOptions));
|
|
2055
|
+
if (compileOptions.alwaysInline !== true) {
|
|
2056
|
+
const always = compile(chunkCode, {
|
|
2057
|
+
...chunkOptions,
|
|
2058
|
+
alwaysInline: true
|
|
2059
|
+
});
|
|
2060
|
+
const other = always.code === chosen?.optimized.code ? void 0 : lowerCandidate(always);
|
|
2061
|
+
if (other && (!chosen || shippedSize(other, filename) < shippedSize(chosen, filename))) chosen = other;
|
|
2062
|
+
}
|
|
2063
|
+
let optimized;
|
|
1776
2064
|
let linked;
|
|
1777
2065
|
let lowered;
|
|
1778
|
-
|
|
1779
|
-
lowered =
|
|
1780
|
-
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
2066
|
+
if (chosen) {
|
|
2067
|
+
({optimized, lowered, linked} = chosen);
|
|
1781
2068
|
addSkips(this, optimized.skipped);
|
|
1782
|
-
}
|
|
1783
|
-
|
|
1784
|
-
this.warn(`${error.message} ${filename} is not optimized.`);
|
|
1785
|
-
optimized = void 0;
|
|
2069
|
+
} else {
|
|
2070
|
+
this.warn(`${missing?.message ?? ""} ${filename} is not optimized.`);
|
|
1786
2071
|
addSkips(this, [{
|
|
1787
2072
|
pass: "inline",
|
|
1788
2073
|
reason: "missing-helper",
|