solid-optimizer 1.0.0-next.4 → 1.0.0-next.6
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 +8 -3
- package/dist/{compile-Dx577-sp.mjs → compile-DnfP-Quf.mjs} +720 -157
- package/dist/compile-DnfP-Quf.mjs.map +1 -0
- package/dist/{compile-CwH9mvHD.d.mts → compile-X_SMDFr2.d.mts} +9 -1
- package/dist/compile-X_SMDFr2.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +2 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +195 -74
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/ast.ts +4 -3
- package/src/compile.ts +39 -6
- package/src/conditional.ts +540 -0
- package/src/constants.ts +9 -5
- package/src/context.ts +1 -1
- package/src/fold.ts +135 -58
- package/src/inline.ts +274 -112
- package/src/jsx.ts +13 -9
- package/src/memo.ts +209 -26
- package/src/provider.ts +62 -40
- package/src/size.ts +6 -1
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +169 -66
- package/src/vite/inject.ts +27 -15
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-CwH9mvHD.d.mts.map +0 -1
- package/dist/compile-Dx577-sp.mjs.map +0 -1
|
@@ -62,6 +62,14 @@ interface CompileOptions {
|
|
|
62
62
|
* @default true
|
|
63
63
|
*/
|
|
64
64
|
memos?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Replace a `<Show>` or `<Switch>` without callback children or `keyed`
|
|
67
|
+
* with a ternary, which Solid's JSX transform compiles to memos without a
|
|
68
|
+
* component.
|
|
69
|
+
*
|
|
70
|
+
* @default true
|
|
71
|
+
*/
|
|
72
|
+
conditionals?: boolean;
|
|
65
73
|
/**
|
|
66
74
|
* The names of Solid's built-in components. A tag only folds when it is
|
|
67
75
|
* one of these, and an empty list turns control-flow folding off.
|
|
@@ -195,4 +203,4 @@ interface ModuleConstants {
|
|
|
195
203
|
declare function readModuleConstants(code: string, options?: Pick<CompileOptions, 'filename' | 'importedConstants' | 'globalConstants'>): ModuleConstants;
|
|
196
204
|
//#endregion
|
|
197
205
|
export { readModuleConstants as a, SkipPass as c, compile as i, Primitive as l, CompileResult as n, ImportedConstants as o, ModuleConstants as r, Skip as s, CompileOptions as t };
|
|
198
|
-
//# sourceMappingURL=compile-
|
|
206
|
+
//# sourceMappingURL=compile-X_SMDFr2.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-X_SMDFr2.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC4DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC9DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCD/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;;EAQA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,kBAAkB,SAAS,eAAe;;;;;;EAM1C,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;EAMA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAqGI,QAAQ,cAAc,UAAS,iBAAsB;UAiFpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as readModuleConstants, c as SkipPass, i as compile, l as Primitive, n as CompileResult, o as ImportedConstants, r as ModuleConstants, s as Skip, t as CompileOptions } from "./compile-
|
|
1
|
+
import { a as readModuleConstants, c as SkipPass, i as compile, l as Primitive, n as CompileResult, o as ImportedConstants, r as ModuleConstants, s as Skip, t as CompileOptions } from "./compile-X_SMDFr2.mjs";
|
|
2
2
|
export { type CompileOptions, type CompileResult, type ImportedConstants, type ModuleConstants, type Primitive, type Skip, type SkipPass, compile, readModuleConstants };
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as readModuleConstants, t as compile } from "./compile-
|
|
1
|
+
import { n as readModuleConstants, t as compile } from "./compile-DnfP-Quf.mjs";
|
|
2
2
|
export { compile, readModuleConstants };
|
package/dist/vite.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { s as Skip, t as CompileOptions } from "./compile-
|
|
1
|
+
import { s as Skip, t as CompileOptions } from "./compile-X_SMDFr2.mjs";
|
|
2
2
|
import { Options as Options$1 } from "@solidjs/vite-plugin";
|
|
3
3
|
import { Plugin } from "vite";
|
|
4
4
|
//#region src/vite/report.d.ts
|
|
@@ -10,7 +10,7 @@ interface OptimizerReport {
|
|
|
10
10
|
}
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/vite/index.d.ts
|
|
13
|
-
export interface OptimizerOptions extends Pick<CompileOptions, 'fold' | 'inline' | 'alwaysInline' | 'contexts' | 'memos' | 'maxPasses'> {
|
|
13
|
+
export interface OptimizerOptions extends Pick<CompileOptions, 'fold' | 'inline' | 'alwaysInline' | 'contexts' | 'memos' | 'conditionals' | 'maxPasses'> {
|
|
14
14
|
/**
|
|
15
15
|
* Where the optimizer runs.
|
|
16
16
|
*
|
package/dist/vite.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite.d.mts","names":["SolidPluginOptions"],"sources":["../src/vite/report.ts","../src/vite/index.ts"],"mappings":";;;;;UAUiB;;WAEN;WACA,gBAAgB;;;;iBCyCV,yBAAyB,KACxC;;;;;;;;;EAWA;;;;;;EAMA;;;;;;;;;EASA,qBAAqB,QAAQ;;;;;;;;;;EAU7B;;iBAKe,gBAAgB,QAAQA;;EAEvC,YAAY;;
|
|
1
|
+
{"version":3,"file":"vite.d.mts","names":["SolidPluginOptions"],"sources":["../src/vite/report.ts","../src/vite/index.ts"],"mappings":";;;;;UAUiB;;WAEN;WACA,gBAAgB;;;;iBCyCV,yBAAyB,KACxC;;;;;;;;;EAWA;;;;;;EAMA;;;;;;;;;EASA,qBAAqB,QAAQ;;;;;;;;;;EAU7B;;iBAKe,gBAAgB,QAAQA;;EAEvC,YAAY;;wBAiNU,eAAe,UAAS,UAAe"}
|
package/dist/vite.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-
|
|
1
|
+
import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-DnfP-Quf.mjs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import remapping from "@jridgewell/remapping";
|
|
4
4
|
import MagicString from "magic-string";
|
|
@@ -19,12 +19,23 @@ function sharedGlobals(config) {
|
|
|
19
19
|
const globals = {};
|
|
20
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;
|
|
21
21
|
const environments = Object.values(config.environments);
|
|
22
|
-
const defines =
|
|
23
|
-
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);
|
|
24
27
|
for (const key of keys) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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;
|
|
28
39
|
}
|
|
29
40
|
return globals;
|
|
30
41
|
}
|
|
@@ -58,12 +69,18 @@ function summarizeSkips(skips) {
|
|
|
58
69
|
group.names.set(skip.name, (group.names.get(skip.name) ?? 0) + 1);
|
|
59
70
|
groups.set(key, group);
|
|
60
71
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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));
|
|
67
84
|
}
|
|
68
85
|
/** The most names listed for a reason. */
|
|
69
86
|
const NAMES_SHOWN = 6;
|
|
@@ -74,9 +91,11 @@ function formatReport(report) {
|
|
|
74
91
|
const lines = [`solid-optimizer: what stays as written in ${report.environment}`];
|
|
75
92
|
const summaries = summarizeSkips(report.skips);
|
|
76
93
|
if (summaries.length === 0) lines.push(" nothing");
|
|
77
|
-
|
|
94
|
+
let reasonWidth = 0;
|
|
95
|
+
for (const summary of summaries) reasonWidth = Math.max(reasonWidth, summary.reason.length);
|
|
78
96
|
for (const summary of summaries) {
|
|
79
|
-
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);
|
|
80
99
|
if (summary.names.length > NAMES_SHOWN) shown.push(`${String(summary.names.length - NAMES_SHOWN)} more`);
|
|
81
100
|
lines.push(` ${summary.pass.padEnd(8)} ${summary.reason.padEnd(reasonWidth)} ${String(summary.count).padStart(4)} ${shown.join(", ")}`);
|
|
82
101
|
}
|
|
@@ -128,6 +147,14 @@ function freshName(scopes, base) {
|
|
|
128
147
|
scopes.names.add(name);
|
|
129
148
|
return name;
|
|
130
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
|
+
}
|
|
131
158
|
/**
|
|
132
159
|
* Whether an import specifier was added by Solid's JSX transform, which
|
|
133
160
|
* names every helper it imports `_$name`.
|
|
@@ -218,7 +245,8 @@ function addMarker(code, filename, options) {
|
|
|
218
245
|
}
|
|
219
246
|
const call = `${MARKER}({ ${entries.join(", ")} })`;
|
|
220
247
|
const s = new MagicString(code);
|
|
221
|
-
const functions =
|
|
248
|
+
const functions = [];
|
|
249
|
+
for (const entry of topLevelFunctions(program)) if (containsJSX(entry.fn)) functions.push(entry);
|
|
222
250
|
for (const { fn } of functions) {
|
|
223
251
|
if (fn.type !== "FunctionDeclaration" && fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression") continue;
|
|
224
252
|
const { body } = fn;
|
|
@@ -229,7 +257,11 @@ function addMarker(code, filename, options) {
|
|
|
229
257
|
s.appendLeft(body.end, "; }");
|
|
230
258
|
}
|
|
231
259
|
}
|
|
232
|
-
|
|
260
|
+
let outside = false;
|
|
261
|
+
for (const statement of program.body) if (!hasStatement(functions, statement) && containsJSX(statement)) {
|
|
262
|
+
outside = true;
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
233
265
|
s.append(`\nimport { ${specifiers.join(", ")} } from ${JSON.stringify(options.moduleName)};\n` + (outside ? `${call};\n` : ""));
|
|
234
266
|
return {
|
|
235
267
|
code: s.toString(),
|
|
@@ -318,7 +350,7 @@ function linkHelpers(code, filename, moduleName, helpers) {
|
|
|
318
350
|
const parents = collectParents(program);
|
|
319
351
|
const s = new MagicString(code);
|
|
320
352
|
for (const statement of program.body) {
|
|
321
|
-
if (statement.type !== "ImportDeclaration" || statement.source.value !== moduleName || !statement.specifiers
|
|
353
|
+
if (statement.type !== "ImportDeclaration" || statement.source.value !== moduleName || !isAllGenerated(statement.specifiers)) continue;
|
|
322
354
|
for (const specifier of statement.specifiers) {
|
|
323
355
|
if (specifier.type !== "ImportSpecifier") continue;
|
|
324
356
|
const imported = exportedName(specifier);
|
|
@@ -384,7 +416,7 @@ function isCapitalized(name) {
|
|
|
384
416
|
* The function a top-level binding holds: `function A() {}`, or
|
|
385
417
|
* `const A = () => ...`.
|
|
386
418
|
*/
|
|
387
|
-
function
|
|
419
|
+
function findTopLevelFunction(binding) {
|
|
388
420
|
const { declaration } = binding;
|
|
389
421
|
if (declaration.type === "FunctionDeclaration") return {
|
|
390
422
|
binding,
|
|
@@ -436,10 +468,16 @@ function readReexport(code, filename, exportName) {
|
|
|
436
468
|
}
|
|
437
469
|
}
|
|
438
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
|
+
}
|
|
439
477
|
/**
|
|
440
478
|
* The names a module exports, each with the top-level binding it exports.
|
|
441
479
|
*/
|
|
442
|
-
function
|
|
480
|
+
function collectExports(program, scopes) {
|
|
443
481
|
const exports = /* @__PURE__ */ new Map();
|
|
444
482
|
const { bindings } = scopes.root;
|
|
445
483
|
for (const statement of program.body) if (statement.type === "ExportNamedDeclaration" && !statement.source) {
|
|
@@ -447,10 +485,7 @@ function exportsOf(program, scopes) {
|
|
|
447
485
|
if (declaration?.type === "FunctionDeclaration" && declaration.id) {
|
|
448
486
|
const binding = bindings.get(declaration.id.name);
|
|
449
487
|
if (binding) exports.set(declaration.id.name, binding);
|
|
450
|
-
} else if (declaration?.type === "VariableDeclaration")
|
|
451
|
-
const declared = declaration.declarations.map((declarator) => declarator.id.type === "Identifier" ? bindings.get(declarator.id.name) : void 0);
|
|
452
|
-
for (const binding of declared.filter((item) => item !== void 0)) exports.set(binding.name, binding);
|
|
453
|
-
}
|
|
488
|
+
} else if (declaration?.type === "VariableDeclaration") addDeclared(exports, declaration.declarations, bindings);
|
|
454
489
|
for (const specifier of statement.specifiers) {
|
|
455
490
|
const local = specifier.local.type === "Identifier" ? specifier.local.name : void 0;
|
|
456
491
|
const exported = specifier.exported.type === "Identifier" ? specifier.exported.name : specifier.exported.value;
|
|
@@ -485,17 +520,18 @@ function outerBindings(fn, self, scopes) {
|
|
|
485
520
|
function exposeLocals(code, filename) {
|
|
486
521
|
const program = parse(filename, code);
|
|
487
522
|
const scopes = analyzeScopes(program);
|
|
488
|
-
const exports =
|
|
523
|
+
const exports = collectExports(program, scopes);
|
|
489
524
|
const exported = new Set(exports.values());
|
|
490
525
|
const needed = /* @__PURE__ */ new Set();
|
|
491
526
|
for (const [name, binding] of exports) {
|
|
492
|
-
const component = isCapitalized(binding.name) || name === "default" ?
|
|
527
|
+
const component = isCapitalized(binding.name) || name === "default" ? findTopLevelFunction(binding) : void 0;
|
|
493
528
|
if (!component) continue;
|
|
494
529
|
for (const outer of outerBindings(component.fn, binding, scopes).values()) if (outer.kind !== "import" && !exported.has(outer)) needed.add(outer);
|
|
495
530
|
}
|
|
496
531
|
if (needed.size === 0) return;
|
|
497
532
|
const s = new MagicString(code);
|
|
498
|
-
const specifiers = [
|
|
533
|
+
const specifiers = [];
|
|
534
|
+
for (const binding of needed) specifiers.push(`${binding.name} as ${LOCAL_EXPORT}${binding.name}`);
|
|
499
535
|
s.append(`\nexport { ${specifiers.join(", ")} };\n`);
|
|
500
536
|
return {
|
|
501
537
|
code: s.toString(),
|
|
@@ -530,10 +566,10 @@ function returnsLocalCall(fn, scopes, moduleSources) {
|
|
|
530
566
|
function readExportedComponent(code, filename, exportName, moduleSources) {
|
|
531
567
|
const program = parse(filename, code);
|
|
532
568
|
const scopes = analyzeScopes(program);
|
|
533
|
-
const exports =
|
|
569
|
+
const exports = collectExports(program, scopes);
|
|
534
570
|
const named = exports.get(exportName);
|
|
535
571
|
const binding = named ? aliasedBinding(named) : void 0;
|
|
536
|
-
const component = binding ?
|
|
572
|
+
const component = binding ? findTopLevelFunction(binding) : void 0;
|
|
537
573
|
if (!binding || !component) return;
|
|
538
574
|
const exportNames = /* @__PURE__ */ new Map();
|
|
539
575
|
for (const [name, exported] of exports) if (!exportNames.has(exported)) exportNames.set(exported, name);
|
|
@@ -755,11 +791,16 @@ var Usage = class {
|
|
|
755
791
|
const users = /* @__PURE__ */ new Set([user]);
|
|
756
792
|
for (const [importer, containers] of usage.importers) {
|
|
757
793
|
const moved = absorbed.get(importer);
|
|
758
|
-
users.add(
|
|
794
|
+
users.add(hasEvery(containers, moved) ? user : importer);
|
|
759
795
|
}
|
|
760
|
-
|
|
796
|
+
for (const id of users) if (this.reaches(module, id)) return 0;
|
|
797
|
+
return users.size;
|
|
761
798
|
}
|
|
762
799
|
};
|
|
800
|
+
function hasEvery(names, set) {
|
|
801
|
+
for (const name of names) if (set?.has(name) !== true) return false;
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
763
804
|
function isTagName(reference, parents) {
|
|
764
805
|
const parent = parents.get(reference);
|
|
765
806
|
return parent?.type === "JSXOpeningElement" && parent.name === reference;
|
|
@@ -771,7 +812,11 @@ function isClosingTagName(reference, parents) {
|
|
|
771
812
|
/** The names a declaration exported with `export` declares. */
|
|
772
813
|
function declaredNames(declaration) {
|
|
773
814
|
if (declaration?.type === "FunctionDeclaration" && declaration.id) return [declaration.id.name];
|
|
774
|
-
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
|
+
}
|
|
775
820
|
return [];
|
|
776
821
|
}
|
|
777
822
|
/** The top-level bindings a module exports, each with its export names. */
|
|
@@ -799,7 +844,7 @@ function recordModule(usage, id, program, targets) {
|
|
|
799
844
|
const scopes = analyzeScopes(program);
|
|
800
845
|
const parents = collectParents(program);
|
|
801
846
|
const exported = exportedBindings(program, scopes);
|
|
802
|
-
const
|
|
847
|
+
const findContainers = (reference) => {
|
|
803
848
|
let node = reference;
|
|
804
849
|
let parent = parents.get(node);
|
|
805
850
|
while (parent && parent.type !== "Program") {
|
|
@@ -812,7 +857,8 @@ function recordModule(usage, id, program, targets) {
|
|
|
812
857
|
else if (declaration.type === "VariableDeclaration") {
|
|
813
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));
|
|
814
859
|
}
|
|
815
|
-
const names =
|
|
860
|
+
const names = [];
|
|
861
|
+
for (const binding of bindings) if (binding) names.push(...exported.get(binding) ?? []);
|
|
816
862
|
return names.length > 0 ? names : [UNEXPORTED];
|
|
817
863
|
};
|
|
818
864
|
const record = (binding, target) => {
|
|
@@ -820,13 +866,13 @@ function recordModule(usage, id, program, targets) {
|
|
|
820
866
|
for (const reference of binding.references) {
|
|
821
867
|
if (isClosingTagName(reference, parents)) continue;
|
|
822
868
|
if (imported !== "*") {
|
|
823
|
-
if (isTagName(reference, parents)) usage.tag(target, imported, id,
|
|
869
|
+
if (isTagName(reference, parents)) usage.tag(target, imported, id, findContainers(reference));
|
|
824
870
|
else usage.other(target, imported);
|
|
825
871
|
continue;
|
|
826
872
|
}
|
|
827
873
|
const member = parents.get(reference);
|
|
828
874
|
if (member?.type === "JSXMemberExpression" && member.object === reference) {
|
|
829
|
-
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));
|
|
830
876
|
else usage.other(target, member.property.name);
|
|
831
877
|
} else if (member?.type === "MemberExpression" && member.object === reference && !member.computed && member.property.type === "Identifier") usage.other(target, member.property.name);
|
|
832
878
|
else usage.all(target);
|
|
@@ -846,13 +892,20 @@ function recordModule(usage, id, program, targets) {
|
|
|
846
892
|
if (target !== void 0) usage.other(target, name);
|
|
847
893
|
}
|
|
848
894
|
}
|
|
849
|
-
for (const [binding, names] of exported)
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
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
|
+
}
|
|
853
906
|
}
|
|
854
907
|
/** The import specifiers a module loads, statically or with `import()`. */
|
|
855
|
-
function
|
|
908
|
+
function collectSpecifiers(program) {
|
|
856
909
|
const statics = /* @__PURE__ */ new Set();
|
|
857
910
|
const dynamics = /* @__PURE__ */ new Set();
|
|
858
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") {
|
|
@@ -877,10 +930,11 @@ async function buildUsageIndex(entries, host) {
|
|
|
877
930
|
const usage = new Usage();
|
|
878
931
|
for (const entry of entries) usage.all(entry);
|
|
879
932
|
const seen = /* @__PURE__ */ new Set();
|
|
880
|
-
let frontier =
|
|
933
|
+
let frontier = [];
|
|
934
|
+
for (const id of entries) if (SCRIPT.test(id.replace(/\?.*$/, ""))) frontier.push(id);
|
|
881
935
|
while (frontier.length > 0) {
|
|
882
936
|
for (const id of frontier) seen.add(id);
|
|
883
|
-
|
|
937
|
+
async function readModule(id) {
|
|
884
938
|
const code = await host.read(id);
|
|
885
939
|
let program;
|
|
886
940
|
try {
|
|
@@ -888,12 +942,15 @@ async function buildUsageIndex(entries, host) {
|
|
|
888
942
|
} catch {
|
|
889
943
|
return [];
|
|
890
944
|
}
|
|
891
|
-
const { static: statics, dynamic } =
|
|
945
|
+
const { static: statics, dynamic } = collectSpecifiers(program);
|
|
892
946
|
const targets = /* @__PURE__ */ new Map();
|
|
893
|
-
|
|
947
|
+
async function resolveSource(source) {
|
|
894
948
|
const target = await host.resolve(source, id);
|
|
895
949
|
if (target !== void 0) targets.set(source, target);
|
|
896
|
-
}
|
|
950
|
+
}
|
|
951
|
+
const resolving = [];
|
|
952
|
+
for (const source of [...statics, ...dynamic]) resolving.push(resolveSource(source));
|
|
953
|
+
await Promise.all(resolving);
|
|
897
954
|
for (const source of dynamic) {
|
|
898
955
|
const target = targets.get(source);
|
|
899
956
|
if (target !== void 0) usage.all(target);
|
|
@@ -901,8 +958,12 @@ async function buildUsageIndex(entries, host) {
|
|
|
901
958
|
recordModule(usage, id, program, targets);
|
|
902
959
|
usage.imports.set(id, [...targets.values()]);
|
|
903
960
|
return [...targets.values()];
|
|
904
|
-
}
|
|
905
|
-
|
|
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);
|
|
906
967
|
}
|
|
907
968
|
return usage;
|
|
908
969
|
}
|
|
@@ -957,6 +1018,22 @@ export const Sample = () => (
|
|
|
957
1018
|
<a href="/" title={a()} ref={a} />
|
|
958
1019
|
</div>
|
|
959
1020
|
);`;
|
|
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
|
+
}
|
|
960
1037
|
/**
|
|
961
1038
|
* Whether the code spreads anything onto an intrinsic element, or onto
|
|
962
1039
|
* `<Dynamic>`, which becomes one once its `component` is known.
|
|
@@ -966,7 +1043,7 @@ function spreadsOntoElement(code, filename) {
|
|
|
966
1043
|
let found = false;
|
|
967
1044
|
const visit = (node) => {
|
|
968
1045
|
if (found) return;
|
|
969
|
-
if (node.type === "JSXOpeningElement" && node.name.type === "JSXIdentifier" && (isIntrinsicTag(node.name.name) || node.name.name === "Dynamic") && node.attributes
|
|
1046
|
+
if (node.type === "JSXOpeningElement" && node.name.type === "JSXIdentifier" && (isIntrinsicTag(node.name.name) || node.name.name === "Dynamic") && hasSpreadAttribute(node.attributes)) {
|
|
970
1047
|
found = true;
|
|
971
1048
|
return;
|
|
972
1049
|
}
|
|
@@ -1004,7 +1081,8 @@ function stripQuery(id) {
|
|
|
1004
1081
|
* source maps, since chaining the maps of a large chunk takes a while.
|
|
1005
1082
|
*/
|
|
1006
1083
|
function combineMaps(wanted, maps) {
|
|
1007
|
-
const present =
|
|
1084
|
+
const present = [];
|
|
1085
|
+
for (const map of maps) if (typeof map === "string") present.push(map);
|
|
1008
1086
|
if (!wanted || present.length === 0) return null;
|
|
1009
1087
|
return remapping(present.reverse(), () => null).toString();
|
|
1010
1088
|
}
|
|
@@ -1064,6 +1142,7 @@ function solidOptimizer(options = {}) {
|
|
|
1064
1142
|
alwaysInline: optimizer.alwaysInline,
|
|
1065
1143
|
contexts: optimizer.contexts,
|
|
1066
1144
|
memos: optimizer.memos,
|
|
1145
|
+
conditionals: optimizer.conditionals,
|
|
1067
1146
|
maxPasses: optimizer.maxPasses,
|
|
1068
1147
|
builtIns: [...builtIns],
|
|
1069
1148
|
moduleSources,
|
|
@@ -1195,20 +1274,25 @@ function solidOptimizer(options = {}) {
|
|
|
1195
1274
|
*/
|
|
1196
1275
|
async function importedConstants(context, imports, importer, visiting, transform) {
|
|
1197
1276
|
let partial = false;
|
|
1198
|
-
|
|
1277
|
+
async function readImport(specifier) {
|
|
1199
1278
|
const target = await resolveConstantModule(context, specifier, importer);
|
|
1200
1279
|
if (target === void 0 || visiting.has(target.id)) return;
|
|
1201
|
-
const result = await
|
|
1280
|
+
const result = await readConstants(context, target, visiting, transform);
|
|
1202
1281
|
partial ||= result.partial;
|
|
1203
1282
|
if (!result.constants || Object.keys(result.constants.exports).length === 0) return;
|
|
1204
1283
|
return [specifier, result.constants.exports];
|
|
1205
|
-
}
|
|
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];
|
|
1206
1290
|
return {
|
|
1207
|
-
constants
|
|
1291
|
+
constants,
|
|
1208
1292
|
partial
|
|
1209
1293
|
};
|
|
1210
1294
|
}
|
|
1211
|
-
async function
|
|
1295
|
+
async function readConstants(context, target, visiting, transform) {
|
|
1212
1296
|
const { id } = target;
|
|
1213
1297
|
const known = moduleConstants.get(id);
|
|
1214
1298
|
if (known) return known;
|
|
@@ -1337,7 +1421,7 @@ function solidOptimizer(options = {}) {
|
|
|
1337
1421
|
* modules are transformed in. Modules without JSX count too, since reading
|
|
1338
1422
|
* their constants can load a module with JSX.
|
|
1339
1423
|
*/
|
|
1340
|
-
async function
|
|
1424
|
+
async function findLocalImports(context, id) {
|
|
1341
1425
|
let pending = localImports.get(id);
|
|
1342
1426
|
if (!pending) {
|
|
1343
1427
|
pending = (async () => {
|
|
@@ -1358,7 +1442,12 @@ function solidOptimizer(options = {}) {
|
|
|
1358
1442
|
const value = statement.source?.value;
|
|
1359
1443
|
if (value !== void 0 && !moduleSources.includes(value)) sources.add(value);
|
|
1360
1444
|
}
|
|
1361
|
-
|
|
1445
|
+
const resolving = [];
|
|
1446
|
+
for (const source of sources) resolving.push(context.resolve(source, id));
|
|
1447
|
+
const resolved = await Promise.all(resolving);
|
|
1448
|
+
const local = [];
|
|
1449
|
+
for (const target of resolved) if (target && !target.external && !target.id.includes("/node_modules/")) local.push(target.id);
|
|
1450
|
+
return local;
|
|
1362
1451
|
})();
|
|
1363
1452
|
localImports.set(id, pending);
|
|
1364
1453
|
}
|
|
@@ -1371,8 +1460,11 @@ function solidOptimizer(options = {}) {
|
|
|
1371
1460
|
while (frontier.length > 0) {
|
|
1372
1461
|
if (frontier.includes(to)) return true;
|
|
1373
1462
|
for (const id of frontier) seen.add(id);
|
|
1374
|
-
const
|
|
1375
|
-
|
|
1463
|
+
const reading = [];
|
|
1464
|
+
for (const id of frontier) reading.push(findLocalImports(context, id));
|
|
1465
|
+
const next = await Promise.all(reading);
|
|
1466
|
+
frontier = [];
|
|
1467
|
+
for (const id of new Set(next.flat())) if (!seen.has(id)) frontier.push(id);
|
|
1376
1468
|
}
|
|
1377
1469
|
return false;
|
|
1378
1470
|
}
|
|
@@ -1439,8 +1531,19 @@ function solidOptimizer(options = {}) {
|
|
|
1439
1531
|
let pending = usageIndexes.get(key);
|
|
1440
1532
|
if (!pending) {
|
|
1441
1533
|
pending = (async () => {
|
|
1442
|
-
const
|
|
1443
|
-
|
|
1534
|
+
const reading = [];
|
|
1535
|
+
for (const id of context.getModuleIds()) if (context.getModuleInfo(id)?.isEntry === true) reading.push(scriptEntries(context, id));
|
|
1536
|
+
const entries = await Promise.all(reading);
|
|
1537
|
+
const scriptIds = [];
|
|
1538
|
+
let known = true;
|
|
1539
|
+
for (const scripts of entries) {
|
|
1540
|
+
if (scripts === void 0) {
|
|
1541
|
+
known = false;
|
|
1542
|
+
break;
|
|
1543
|
+
}
|
|
1544
|
+
scriptIds.push(...scripts);
|
|
1545
|
+
}
|
|
1546
|
+
return buildUsageIndex(known ? scriptIds : [], {
|
|
1444
1547
|
resolve: async (source, importer) => {
|
|
1445
1548
|
const resolved = await context.resolve(source, importer);
|
|
1446
1549
|
return !resolved || resolved.external || resolved.id.includes("/node_modules/") ? void 0 : resolved.id;
|
|
@@ -1474,7 +1577,8 @@ function solidOptimizer(options = {}) {
|
|
|
1474
1577
|
const isCopyable = (id) => {
|
|
1475
1578
|
const file = stripQuery(id).replaceAll("\\", "/");
|
|
1476
1579
|
if (!file.includes("/node_modules/")) return true;
|
|
1477
|
-
|
|
1580
|
+
for (const name of optimizer.packages ?? []) if (file.includes(`/node_modules/${name}/`)) return true;
|
|
1581
|
+
return false;
|
|
1478
1582
|
};
|
|
1479
1583
|
/** The listed packages already warned about, by environment. */
|
|
1480
1584
|
const warnedPackages = /* @__PURE__ */ new Set();
|
|
@@ -1484,7 +1588,11 @@ function solidOptimizer(options = {}) {
|
|
|
1484
1588
|
* copies them would hydrate different markup.
|
|
1485
1589
|
*/
|
|
1486
1590
|
const warnExternalPackage = (context, source) => {
|
|
1487
|
-
|
|
1591
|
+
let name;
|
|
1592
|
+
for (const item of optimizer.packages ?? []) if (source === item || source.startsWith(`${item}/`)) {
|
|
1593
|
+
name = item;
|
|
1594
|
+
break;
|
|
1595
|
+
}
|
|
1488
1596
|
const key = environmentKey(context, name ?? "");
|
|
1489
1597
|
if (name === void 0 || warnedPackages.has(key)) return;
|
|
1490
1598
|
warnedPackages.add(key);
|
|
@@ -1591,7 +1699,7 @@ function solidOptimizer(options = {}) {
|
|
|
1591
1699
|
const excluded = /* @__PURE__ */ new Set();
|
|
1592
1700
|
const usage = await usageIndex(context);
|
|
1593
1701
|
const absorbed = /* @__PURE__ */ new Map();
|
|
1594
|
-
const
|
|
1702
|
+
const countUsers = (copy) => {
|
|
1595
1703
|
const [module = "", exported = ""] = copy.key.split("\0");
|
|
1596
1704
|
return usage.users(module, exported, id, absorbed);
|
|
1597
1705
|
};
|
|
@@ -1626,10 +1734,7 @@ function solidOptimizer(options = {}) {
|
|
|
1626
1734
|
if (statement.type !== "ImportDeclaration" || statement.importKind === "type" || moduleSources.includes(statement.source.value)) continue;
|
|
1627
1735
|
for (const specifier of statement.specifiers) {
|
|
1628
1736
|
const binding = scopes.root.bindings.get(specifier.local.name);
|
|
1629
|
-
const tags = (binding?.references ?? [])
|
|
1630
|
-
const parent = parents.get(reference);
|
|
1631
|
-
return (parent?.type === "JSXOpeningElement" || parent?.type === "JSXClosingElement") && parent.name === reference;
|
|
1632
|
-
});
|
|
1737
|
+
const tags = findTags(binding?.references ?? [], parents);
|
|
1633
1738
|
if (specifier.type === "ImportNamespaceSpecifier" || binding?.imported === void 0 || tags.length === 0) continue;
|
|
1634
1739
|
const copy = await copyImported(context, id, statement.source.value, binding.imported, {
|
|
1635
1740
|
excluded,
|
|
@@ -1662,21 +1767,31 @@ function solidOptimizer(options = {}) {
|
|
|
1662
1767
|
edit.append(`\n${copy.text}\n`);
|
|
1663
1768
|
}
|
|
1664
1769
|
edit.append(`\n${imports.join("\n")}\n`);
|
|
1770
|
+
const importedContexts = {};
|
|
1771
|
+
for (const [module, exported] of contexts) importedContexts[module] = [...exported];
|
|
1772
|
+
const sharedComponents = {};
|
|
1773
|
+
const alwaysInlineComponents = [];
|
|
1774
|
+
for (const copy of copies) {
|
|
1775
|
+
const users = countUsers(copy);
|
|
1776
|
+
if (users !== 1) sharedComponents[copy.name] = users;
|
|
1777
|
+
if (copy.fromPackage) alwaysInlineComponents.push(copy.name);
|
|
1778
|
+
}
|
|
1665
1779
|
result = compile(edit.toString(), {
|
|
1666
1780
|
...compileOptionsOfModule,
|
|
1667
1781
|
filename,
|
|
1668
1782
|
sourceMap: wantsMaps(),
|
|
1669
|
-
importedContexts
|
|
1670
|
-
sharedComponents
|
|
1671
|
-
alwaysInlineComponents
|
|
1783
|
+
importedContexts,
|
|
1784
|
+
sharedComponents,
|
|
1785
|
+
alwaysInlineComponents
|
|
1672
1786
|
});
|
|
1673
1787
|
const left = analyzeScopes(parse(filename, result.code)).root.bindings;
|
|
1674
|
-
const stuck =
|
|
1788
|
+
const stuck = [];
|
|
1789
|
+
for (const copy of copies) if (left.has(copy.name)) stuck.push(copy);
|
|
1675
1790
|
if (stuck.length === 0) break;
|
|
1676
1791
|
for (const copy of stuck) {
|
|
1677
1792
|
excluded.add(copy.key);
|
|
1678
1793
|
copies.splice(copies.indexOf(copy), 1);
|
|
1679
|
-
const reason = result.skipped
|
|
1794
|
+
const reason = findSkipReason(result.skipped ?? [], copy.name);
|
|
1680
1795
|
copySkips?.push({
|
|
1681
1796
|
pass: "inline",
|
|
1682
1797
|
reason: `copy-${reason ?? "left"}`,
|
|
@@ -1686,7 +1801,9 @@ function solidOptimizer(options = {}) {
|
|
|
1686
1801
|
result = void 0;
|
|
1687
1802
|
}
|
|
1688
1803
|
if (!result || !edit) break;
|
|
1689
|
-
|
|
1804
|
+
const sole = [];
|
|
1805
|
+
for (const copy of copies) if (countUsers(copy) === 1) sole.push(copy);
|
|
1806
|
+
for (const copy of sole) {
|
|
1690
1807
|
const [module = "", exported = ""] = copy.key.split("\0");
|
|
1691
1808
|
absorbed.set(module, /* @__PURE__ */ new Set([...absorbed.get(module) ?? [], exported]));
|
|
1692
1809
|
}
|
|
@@ -1791,7 +1908,11 @@ function solidOptimizer(options = {}) {
|
|
|
1791
1908
|
map: combineMaps(wantsMaps(), maps)
|
|
1792
1909
|
};
|
|
1793
1910
|
}
|
|
1794
|
-
|
|
1911
|
+
let main;
|
|
1912
|
+
for (const plugin of plugins) if (plugin.name === "solid") {
|
|
1913
|
+
main = plugin;
|
|
1914
|
+
break;
|
|
1915
|
+
}
|
|
1795
1916
|
const originalTransform = main?.transform;
|
|
1796
1917
|
const originalHandler = typeof originalTransform === "function" ? originalTransform : originalTransform?.handler;
|
|
1797
1918
|
if (main && originalHandler) main.transform = async function transform(code, id, transformOptions) {
|