solid-optimizer 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -1
- package/dist/{compile-BroGlagZ.d.mts → compile-BQuBDA0Q.d.mts} +29 -2
- package/dist/compile-BQuBDA0Q.d.mts.map +1 -0
- package/dist/{compile-ocDYvJUW.mjs → compile-CqhOfBvG.mjs} +422 -71
- package/dist/compile-CqhOfBvG.mjs.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +19 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +419 -141
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/ast.ts +12 -4
- package/src/compile.ts +64 -12
- package/src/constants.ts +27 -8
- package/src/context.ts +29 -0
- package/src/index.ts +1 -0
- package/src/inline.ts +225 -16
- package/src/memo.ts +43 -10
- package/src/provider.ts +65 -8
- package/src/size.ts +2 -0
- package/src/vite/index.ts +277 -34
- package/src/vite/inject.ts +41 -2
- package/src/vite/refs.ts +99 -0
- package/src/vite/report.ts +74 -0
- package/src/vite/runtime.ts +0 -20
- package/dist/compile-BroGlagZ.d.mts.map +0 -1
- package/dist/compile-ocDYvJUW.mjs.map +0 -1
package/dist/vite.mjs
CHANGED
|
@@ -1,11 +1,53 @@
|
|
|
1
|
-
import { a as lookup, c as forEachChild, i as analyzeScopes, l as
|
|
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-CqhOfBvG.mjs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import remapping from "@jridgewell/remapping";
|
|
4
4
|
import MagicString from "magic-string";
|
|
5
5
|
import { parseSync } from "oxc-parser";
|
|
6
6
|
import { readFile } from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
7
8
|
import { createFilter, transformWithOxc } from "vite";
|
|
8
9
|
import solidPlugin from "vite-plugin-solid";
|
|
10
|
+
//#region src/vite/report.ts
|
|
11
|
+
/**
|
|
12
|
+
* Groups skips by pass and reason, most frequent first.
|
|
13
|
+
*/
|
|
14
|
+
function summarizeSkips(skips) {
|
|
15
|
+
const groups = /* @__PURE__ */ new Map();
|
|
16
|
+
for (const skip of skips) {
|
|
17
|
+
const key = `${skip.pass}\0${skip.reason}`;
|
|
18
|
+
const group = groups.get(key) ?? {
|
|
19
|
+
pass: skip.pass,
|
|
20
|
+
reason: skip.reason,
|
|
21
|
+
names: /* @__PURE__ */ new Map()
|
|
22
|
+
};
|
|
23
|
+
group.names.set(skip.name, (group.names.get(skip.name) ?? 0) + 1);
|
|
24
|
+
groups.set(key, group);
|
|
25
|
+
}
|
|
26
|
+
return [...groups.values()].map(({ pass, reason, names }) => ({
|
|
27
|
+
pass,
|
|
28
|
+
reason,
|
|
29
|
+
count: [...names.values()].reduce((sum, count) => sum + count, 0),
|
|
30
|
+
names: [...names].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
31
|
+
})).sort((a, b) => b.count - a.count || a.pass.localeCompare(b.pass) || a.reason.localeCompare(b.reason));
|
|
32
|
+
}
|
|
33
|
+
/** The most names listed for a reason. */
|
|
34
|
+
const NAMES_SHOWN = 6;
|
|
35
|
+
/**
|
|
36
|
+
* The report as text, one line per reason.
|
|
37
|
+
*/
|
|
38
|
+
function formatReport(report) {
|
|
39
|
+
const lines = [`solid-optimizer: what stays as written in ${report.environment}`];
|
|
40
|
+
const summaries = summarizeSkips(report.skips);
|
|
41
|
+
if (summaries.length === 0) lines.push(" nothing");
|
|
42
|
+
const reasonWidth = Math.max(0, ...summaries.map((summary) => summary.reason.length));
|
|
43
|
+
for (const summary of summaries) {
|
|
44
|
+
const shown = summary.names.slice(0, NAMES_SHOWN).map(([name, count]) => count > 1 ? `${name}×${String(count)}` : name);
|
|
45
|
+
if (summary.names.length > NAMES_SHOWN) shown.push(`${String(summary.names.length - NAMES_SHOWN)} more`);
|
|
46
|
+
lines.push(` ${summary.pass.padEnd(8)} ${summary.reason.padEnd(reasonWidth)} ${String(summary.count).padStart(4)} ${shown.join(", ")}`);
|
|
47
|
+
}
|
|
48
|
+
return lines.join("\n");
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
9
51
|
//#region src/vite/runtime.ts
|
|
10
52
|
/**
|
|
11
53
|
* Keeps Solid's runtime helpers reachable while JSX waits in a bundled chunk.
|
|
@@ -34,7 +76,7 @@ var MissingHelperError = class extends Error {
|
|
|
34
76
|
};
|
|
35
77
|
const HELPER_PREFIX = "helper:";
|
|
36
78
|
const BUILT_IN_PREFIX = "builtin:";
|
|
37
|
-
function generateMap$
|
|
79
|
+
function generateMap$2(s, filename) {
|
|
38
80
|
return s.generateMap({
|
|
39
81
|
source: filename,
|
|
40
82
|
hires: true,
|
|
@@ -62,19 +104,6 @@ function exportedName(specifier) {
|
|
|
62
104
|
return specifier.imported.type === "Literal" ? specifier.imported.value : specifier.imported.name;
|
|
63
105
|
}
|
|
64
106
|
/**
|
|
65
|
-
* The helpers Solid's JSX transform imported from `source` into its output.
|
|
66
|
-
* The module's own imports, including type imports, are left out.
|
|
67
|
-
*/
|
|
68
|
-
function generatedImports(code, filename, source) {
|
|
69
|
-
const program = parse(filename, code);
|
|
70
|
-
const names = [];
|
|
71
|
-
for (const statement of program.body) {
|
|
72
|
-
if (statement.type !== "ImportDeclaration" || statement.source.value !== source) continue;
|
|
73
|
-
for (const specifier of statement.specifiers) if (specifier.type === "ImportSpecifier" && isGeneratedSpecifier(specifier)) names.push(exportedName(specifier));
|
|
74
|
-
}
|
|
75
|
-
return names;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
107
|
* The functions declared at the top level of a module, with the statement
|
|
79
108
|
* that declares each: `function A() {}`, `const A = () => ...`, and their
|
|
80
109
|
* `export` forms.
|
|
@@ -156,7 +185,7 @@ function addMarker(code, filename, options) {
|
|
|
156
185
|
s.append(`\nimport { ${specifiers.join(", ")} } from ${JSON.stringify(options.moduleName)};\n` + (outside ? `${call};\n` : ""));
|
|
157
186
|
return {
|
|
158
187
|
code: s.toString(),
|
|
159
|
-
map: generateMap$
|
|
188
|
+
map: generateMap$2(s, filename)
|
|
160
189
|
};
|
|
161
190
|
}
|
|
162
191
|
/**
|
|
@@ -185,7 +214,7 @@ function repairJSXSequences(code, filename) {
|
|
|
185
214
|
}
|
|
186
215
|
return {
|
|
187
216
|
code: s.toString(),
|
|
188
|
-
map: generateMap$
|
|
217
|
+
map: generateMap$2(s, filename)
|
|
189
218
|
};
|
|
190
219
|
}
|
|
191
220
|
function propertyKey(node) {
|
|
@@ -226,7 +255,7 @@ function readMarkers(code, filename) {
|
|
|
226
255
|
if (markers.length === 0) return;
|
|
227
256
|
return {
|
|
228
257
|
code: s.toString(),
|
|
229
|
-
map: generateMap$
|
|
258
|
+
map: generateMap$2(s, filename),
|
|
230
259
|
helpers,
|
|
231
260
|
builtInAliases
|
|
232
261
|
};
|
|
@@ -259,7 +288,7 @@ function linkHelpers(code, filename, moduleName, helpers) {
|
|
|
259
288
|
}
|
|
260
289
|
return {
|
|
261
290
|
code: s.toString(),
|
|
262
|
-
map: generateMap$
|
|
291
|
+
map: generateMap$2(s, filename)
|
|
263
292
|
};
|
|
264
293
|
}
|
|
265
294
|
//#endregion
|
|
@@ -293,7 +322,7 @@ function isContext(binding, scopes, moduleSources) {
|
|
|
293
322
|
}
|
|
294
323
|
/** The prefix of the name a module exports a local binding under. */
|
|
295
324
|
const LOCAL_EXPORT = "__so_local$";
|
|
296
|
-
function generateMap(s, filename) {
|
|
325
|
+
function generateMap$1(s, filename) {
|
|
297
326
|
return s.generateMap({
|
|
298
327
|
source: filename,
|
|
299
328
|
hires: true,
|
|
@@ -386,10 +415,29 @@ function exposeLocals(code, filename) {
|
|
|
386
415
|
s.append(`\nexport { ${specifiers.join(", ")} };\n`);
|
|
387
416
|
return {
|
|
388
417
|
code: s.toString(),
|
|
389
|
-
map: generateMap(s, filename)
|
|
418
|
+
map: generateMap$1(s, filename)
|
|
390
419
|
};
|
|
391
420
|
}
|
|
392
421
|
/**
|
|
422
|
+
* Whether a function returns JSX whose root calls a component declared at
|
|
423
|
+
* the top of its own module.
|
|
424
|
+
*/
|
|
425
|
+
function returnsLocalCall(fn, scopes) {
|
|
426
|
+
if (fn.type !== "FunctionDeclaration" && fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression") return false;
|
|
427
|
+
const { body } = fn;
|
|
428
|
+
if (!body) return false;
|
|
429
|
+
let root;
|
|
430
|
+
if (body.type === "BlockStatement") {
|
|
431
|
+
const last = body.body.at(-1);
|
|
432
|
+
root = last?.type === "ReturnStatement" ? last.argument : void 0;
|
|
433
|
+
} else root = body;
|
|
434
|
+
if (!root) return false;
|
|
435
|
+
const inner = unwrap(root);
|
|
436
|
+
if (inner.type !== "JSXElement" || inner.openingElement.name.type !== "JSXIdentifier") return false;
|
|
437
|
+
const binding = scopes.references.get(inner.openingElement.name);
|
|
438
|
+
return binding !== void 0 && binding.scope === scopes.root && binding.kind !== "import";
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
393
441
|
* Reads the component a module exports under `exportName`, from the module
|
|
394
442
|
* as the bundler loaded it. `undefined` when that export is not a function
|
|
395
443
|
* component, or it refers to a top-level binding the module cannot export.
|
|
@@ -448,7 +496,8 @@ function readExportedComponent(code, filename, exportName, moduleSources) {
|
|
|
448
496
|
};
|
|
449
497
|
return {
|
|
450
498
|
dependencies,
|
|
451
|
-
copy
|
|
499
|
+
copy,
|
|
500
|
+
wrapsLocal: returnsLocalCall(component.fn, scopes)
|
|
452
501
|
};
|
|
453
502
|
}
|
|
454
503
|
//#endregion
|
|
@@ -500,6 +549,72 @@ function nameNamespaceTags(code, filename) {
|
|
|
500
549
|
};
|
|
501
550
|
}
|
|
502
551
|
//#endregion
|
|
552
|
+
//#region src/vite/refs.ts
|
|
553
|
+
const REF_MARKER = "__SO_REF__";
|
|
554
|
+
function generateMap(s, filename) {
|
|
555
|
+
return s.generateMap({
|
|
556
|
+
source: filename,
|
|
557
|
+
hires: true,
|
|
558
|
+
includeContent: true
|
|
559
|
+
}).toString();
|
|
560
|
+
}
|
|
561
|
+
function isRefAttribute(node) {
|
|
562
|
+
return node.type === "JSXAttribute" && node.name.type === "JSXIdentifier" && node.name.name === "ref";
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Wraps each `ref` whose value is a `let` or `var` binding, so the bundler
|
|
566
|
+
* sees it written. `undefined` when there is none.
|
|
567
|
+
*/
|
|
568
|
+
function protectRefs(code, filename) {
|
|
569
|
+
if (!code.includes("ref")) return;
|
|
570
|
+
const program = parse(filename, code);
|
|
571
|
+
const scopes = analyzeScopes(program);
|
|
572
|
+
const s = new MagicString(code);
|
|
573
|
+
const visit = (node) => {
|
|
574
|
+
if (isRefAttribute(node) && node.value?.type === "JSXExpressionContainer") {
|
|
575
|
+
const { expression } = node.value;
|
|
576
|
+
const binding = expression.type === "Identifier" ? scopes.references.get(expression) : void 0;
|
|
577
|
+
if (expression.type === "Identifier" && (binding?.kind === "let" || binding?.kind === "var")) {
|
|
578
|
+
const { name } = expression;
|
|
579
|
+
s.overwrite(expression.start, expression.end, `${REF_MARKER}(${name}, (__so_ref) => (${name} = __so_ref))`);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
forEachChild(node, visit);
|
|
583
|
+
};
|
|
584
|
+
visit(program);
|
|
585
|
+
return s.hasChanged() ? {
|
|
586
|
+
code: s.toString(),
|
|
587
|
+
map: generateMap(s, filename)
|
|
588
|
+
} : void 0;
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Turns each wrapped `ref` back into its variable. `undefined` when there
|
|
592
|
+
* is none.
|
|
593
|
+
*/
|
|
594
|
+
function restoreRefs(code, filename) {
|
|
595
|
+
if (!code.includes("__SO_REF__")) return;
|
|
596
|
+
const program = parse(filename, code);
|
|
597
|
+
const s = new MagicString(code);
|
|
598
|
+
const visit = (node) => {
|
|
599
|
+
if (isRefAttribute(node) && node.value?.type === "JSXExpressionContainer") {
|
|
600
|
+
const { expression } = node.value;
|
|
601
|
+
if (isMarkerCall(expression)) {
|
|
602
|
+
const target = expression.arguments.at(0);
|
|
603
|
+
if (target?.type === "Identifier") s.overwrite(expression.start, expression.end, target.name);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
forEachChild(node, visit);
|
|
607
|
+
};
|
|
608
|
+
visit(program);
|
|
609
|
+
return s.hasChanged() ? {
|
|
610
|
+
code: s.toString(),
|
|
611
|
+
map: generateMap(s, filename)
|
|
612
|
+
} : void 0;
|
|
613
|
+
}
|
|
614
|
+
function isMarkerCall(node) {
|
|
615
|
+
return node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "__SO_REF__";
|
|
616
|
+
}
|
|
617
|
+
//#endregion
|
|
503
618
|
//#region src/vite/usage.ts
|
|
504
619
|
/** The name a use counts under when it is not inside an exported declaration. */
|
|
505
620
|
const UNEXPORTED = "";
|
|
@@ -752,6 +867,32 @@ function withRelated(helpers) {
|
|
|
752
867
|
for (const helper of helpers) for (const related of RELATED_HELPERS[helper] ?? []) result.add(related);
|
|
753
868
|
return result;
|
|
754
869
|
}
|
|
870
|
+
/**
|
|
871
|
+
* The common kinds of attribute: a class, any other attribute, events, and
|
|
872
|
+
* children. Inlining a component turns a spread onto an element into
|
|
873
|
+
* attributes like these, in a module that may not use their helpers itself.
|
|
874
|
+
* A rarer kind, like `style`, can still need a helper no module imports.
|
|
875
|
+
* The chunk is then lowered without optimizing it.
|
|
876
|
+
*/
|
|
877
|
+
const ATTRIBUTE_SAMPLE = `const a = () => 0;
|
|
878
|
+
export const Sample = () => (
|
|
879
|
+
<div class={a()} id={a()} onClick={a()} onScroll={a()}>{a()}</div>
|
|
880
|
+
);`;
|
|
881
|
+
/** Whether the code spreads anything onto an intrinsic element. */
|
|
882
|
+
function spreadsOntoElement(code, filename) {
|
|
883
|
+
if (!code.includes("...")) return false;
|
|
884
|
+
let found = false;
|
|
885
|
+
const visit = (node) => {
|
|
886
|
+
if (found) return;
|
|
887
|
+
if (node.type === "JSXOpeningElement" && node.name.type === "JSXIdentifier" && isIntrinsicTag(node.name.name) && node.attributes.some((attribute) => attribute.type === "JSXSpreadAttribute")) {
|
|
888
|
+
found = true;
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
forEachChild(node, visit);
|
|
892
|
+
};
|
|
893
|
+
visit(parse(filename, code));
|
|
894
|
+
return found;
|
|
895
|
+
}
|
|
755
896
|
function loadCompiler() {
|
|
756
897
|
const require = createRequire(import.meta.url);
|
|
757
898
|
const pluginRequire = createRequire(require.resolve("vite-plugin-solid"));
|
|
@@ -766,13 +907,13 @@ function loadCompiler() {
|
|
|
766
907
|
/**
|
|
767
908
|
* Lowers JSX with `babel-preset-solid`, as `vite-plugin-solid` does.
|
|
768
909
|
*/
|
|
769
|
-
function lower(compiler, code, filename, options) {
|
|
910
|
+
function lower(compiler, code, filename, options, sourceMaps) {
|
|
770
911
|
const result = compiler.babel.transformSync(code, {
|
|
771
912
|
filename,
|
|
772
913
|
sourceFileName: filename,
|
|
773
914
|
presets: [[compiler.preset, options]],
|
|
774
915
|
ast: false,
|
|
775
|
-
sourceMaps
|
|
916
|
+
sourceMaps,
|
|
776
917
|
configFile: false,
|
|
777
918
|
babelrc: false,
|
|
778
919
|
parserOpts: { plugins: /\.[mc]?tsx$/i.test(filename) ? ["jsx", "typescript"] : ["jsx"] }
|
|
@@ -783,12 +924,40 @@ function lower(compiler, code, filename, options) {
|
|
|
783
924
|
map: result.map ? JSON.stringify(result.map) : void 0
|
|
784
925
|
};
|
|
785
926
|
}
|
|
927
|
+
/**
|
|
928
|
+
* The helpers `babel-preset-solid` imports from `moduleName` to lower `code`.
|
|
929
|
+
* Only the AST is built, since its code would only be read for its imports.
|
|
930
|
+
*/
|
|
931
|
+
function helpersOf(compiler, code, filename, options, moduleName) {
|
|
932
|
+
const result = compiler.babel.transformSync(code, {
|
|
933
|
+
filename,
|
|
934
|
+
presets: [[compiler.preset, options]],
|
|
935
|
+
ast: true,
|
|
936
|
+
code: false,
|
|
937
|
+
configFile: false,
|
|
938
|
+
babelrc: false,
|
|
939
|
+
parserOpts: { plugins: /\.[mc]?tsx$/i.test(filename) ? ["jsx", "typescript"] : ["jsx"] }
|
|
940
|
+
});
|
|
941
|
+
const names = [];
|
|
942
|
+
for (const statement of result?.ast?.program.body ?? []) {
|
|
943
|
+
if (statement.type !== "ImportDeclaration" || statement.source.value !== moduleName) continue;
|
|
944
|
+
for (const specifier of statement.specifiers) if (specifier.type === "ImportSpecifier" && specifier.local.name.startsWith("_$")) {
|
|
945
|
+
const { imported } = specifier;
|
|
946
|
+
names.push(imported.type === "StringLiteral" ? imported.value : imported.name);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return names;
|
|
950
|
+
}
|
|
786
951
|
function stripQuery(id) {
|
|
787
952
|
return id.replace(/\?.*$/, "");
|
|
788
953
|
}
|
|
789
|
-
|
|
954
|
+
/**
|
|
955
|
+
* Chains the maps of each step into one, or `null` when the build writes no
|
|
956
|
+
* source maps, since chaining the maps of a large chunk takes a while.
|
|
957
|
+
*/
|
|
958
|
+
function combineMaps(wanted, maps) {
|
|
790
959
|
const present = maps.filter((map) => typeof map === "string");
|
|
791
|
-
if (present.length === 0) return null;
|
|
960
|
+
if (!wanted || present.length === 0) return null;
|
|
792
961
|
return remapping(present.reverse(), () => null).toString();
|
|
793
962
|
}
|
|
794
963
|
/**
|
|
@@ -838,9 +1007,31 @@ function solidOptimizer(options = {}) {
|
|
|
838
1007
|
memos: optimizer.memos,
|
|
839
1008
|
maxPasses: optimizer.maxPasses,
|
|
840
1009
|
builtIns: [...builtIns],
|
|
841
|
-
moduleSources
|
|
1010
|
+
moduleSources,
|
|
1011
|
+
report: optimizer.report !== void 0 && optimizer.report !== false
|
|
842
1012
|
};
|
|
843
1013
|
let config;
|
|
1014
|
+
/**
|
|
1015
|
+
* A skip's name with the module it names, like `Card (/app/src/card.tsx)`,
|
|
1016
|
+
* relative to the project root, or to `node_modules` for a package.
|
|
1017
|
+
*/
|
|
1018
|
+
const shortName = (name) => name.replace(/\((\/.*)\)$/, (whole, file) => {
|
|
1019
|
+
const index = file.lastIndexOf("/node_modules/");
|
|
1020
|
+
if (index !== -1) return `(${file.slice(index + 14)})`;
|
|
1021
|
+
return config ? `(${path.relative(config.root, file)})` : whole;
|
|
1022
|
+
});
|
|
1023
|
+
/** What the final compile of each chunk or module left alone, by environment. */
|
|
1024
|
+
const skips = /* @__PURE__ */ new Map();
|
|
1025
|
+
const addSkips = (context, list) => {
|
|
1026
|
+
if (!list) return;
|
|
1027
|
+
const environment = context.environment?.name ?? "client";
|
|
1028
|
+
const entries = skips.get(environment) ?? [];
|
|
1029
|
+
entries.push(...list.map((skip) => ({
|
|
1030
|
+
...skip,
|
|
1031
|
+
name: shortName(skip.name)
|
|
1032
|
+
})));
|
|
1033
|
+
skips.set(environment, entries);
|
|
1034
|
+
};
|
|
844
1035
|
let compiler;
|
|
845
1036
|
let filter;
|
|
846
1037
|
const getCompiler = () => {
|
|
@@ -848,6 +1039,8 @@ function solidOptimizer(options = {}) {
|
|
|
848
1039
|
return compiler;
|
|
849
1040
|
};
|
|
850
1041
|
const isBuild = () => config?.command === "build";
|
|
1042
|
+
/** Whether the output needs source maps: while serving, or when the build writes them. */
|
|
1043
|
+
const wantsMaps = () => !isBuild() || Boolean(config?.build.sourcemap);
|
|
851
1044
|
/**
|
|
852
1045
|
* The JSX options the official plugin passes for a posture, so the
|
|
853
1046
|
* lowering here matches it. Mirrors the `transform` hook of `vite-plugin-solid`.
|
|
@@ -866,6 +1059,12 @@ function solidOptimizer(options = {}) {
|
|
|
866
1059
|
...solidOptions.solid
|
|
867
1060
|
};
|
|
868
1061
|
};
|
|
1062
|
+
let sampleHelpers;
|
|
1063
|
+
/** The helpers the JSX transform uses for attributes in chunk mode. */
|
|
1064
|
+
const attributeHelpers = () => {
|
|
1065
|
+
sampleHelpers ??= helpersOf(getCompiler(), ATTRIBUTE_SAMPLE, "sample.jsx", jsxOptions(false), moduleName);
|
|
1066
|
+
return sampleHelpers;
|
|
1067
|
+
};
|
|
869
1068
|
/**
|
|
870
1069
|
* Whether client builds keep JSX until chunks are rendered.
|
|
871
1070
|
*/
|
|
@@ -945,12 +1144,14 @@ function solidOptimizer(options = {}) {
|
|
|
945
1144
|
const filename = stripQuery(id);
|
|
946
1145
|
if (optimizer.fold === false || !isBuild()) return {
|
|
947
1146
|
...compileOptions,
|
|
948
|
-
filename
|
|
1147
|
+
filename,
|
|
1148
|
+
sourceMap: wantsMaps()
|
|
949
1149
|
};
|
|
950
1150
|
const { imports } = readModuleConstants(code, { filename });
|
|
951
1151
|
return {
|
|
952
1152
|
...compileOptions,
|
|
953
1153
|
filename,
|
|
1154
|
+
sourceMap: wantsMaps(),
|
|
954
1155
|
importedConstants: await importedConstants(context, imports, filename, /* @__PURE__ */ new Set([filename]))
|
|
955
1156
|
};
|
|
956
1157
|
}
|
|
@@ -1119,14 +1320,44 @@ function solidOptimizer(options = {}) {
|
|
|
1119
1320
|
* needs into the importer. `undefined` when it cannot be copied.
|
|
1120
1321
|
*/
|
|
1121
1322
|
async function copyImported(context, id, source, imported, state) {
|
|
1323
|
+
const skip = (reason) => {
|
|
1324
|
+
state.skips?.push({
|
|
1325
|
+
pass: "inline",
|
|
1326
|
+
reason,
|
|
1327
|
+
name: `${imported} (${source})`
|
|
1328
|
+
});
|
|
1329
|
+
};
|
|
1122
1330
|
const resolved = await context.resolve(source, id);
|
|
1123
|
-
if (!resolved || resolved.external
|
|
1331
|
+
if (!resolved || resolved.external) {
|
|
1332
|
+
skip("copy-external");
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
if (resolved.id.includes("/node_modules/")) {
|
|
1336
|
+
skip("copy-package");
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
if (resolved.id === id) return;
|
|
1340
|
+
if (!isJSXModule(resolved.id)) {
|
|
1341
|
+
skip("copy-not-jsx");
|
|
1342
|
+
return;
|
|
1343
|
+
}
|
|
1124
1344
|
const key = `${resolved.id}\0${imported}`;
|
|
1125
1345
|
if (state.excluded.has(key)) return;
|
|
1126
1346
|
const loaded = await state.load(resolved.id);
|
|
1127
|
-
|
|
1347
|
+
if (loaded === void 0) {
|
|
1348
|
+
state.excluded.add(key);
|
|
1349
|
+
skip("copy-cycle");
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
const component = readExportedComponent(loaded, stripQuery(resolved.id), imported, moduleSources);
|
|
1128
1353
|
if (!component) {
|
|
1129
1354
|
state.excluded.add(key);
|
|
1355
|
+
skip("copy-shape");
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
if (component.wrapsLocal) {
|
|
1359
|
+
state.excluded.add(key);
|
|
1360
|
+
skip("copy-wrapper");
|
|
1130
1361
|
return;
|
|
1131
1362
|
}
|
|
1132
1363
|
const targets = /* @__PURE__ */ new Map();
|
|
@@ -1134,6 +1365,7 @@ function solidOptimizer(options = {}) {
|
|
|
1134
1365
|
const target = await context.resolve(dependency.source, resolved.id);
|
|
1135
1366
|
if (!target) {
|
|
1136
1367
|
state.excluded.add(key);
|
|
1368
|
+
skip("copy-external");
|
|
1137
1369
|
return;
|
|
1138
1370
|
}
|
|
1139
1371
|
targets.set(dependency.source, target.external ? dependency.source : target.id);
|
|
@@ -1163,9 +1395,10 @@ function solidOptimizer(options = {}) {
|
|
|
1163
1395
|
* every call is left out, so a component is never in both places. Copies
|
|
1164
1396
|
* can import components in turn, which a few more rounds take in.
|
|
1165
1397
|
*/
|
|
1166
|
-
async function inlineImports(context, source, id, compileOptionsOfModule, load) {
|
|
1398
|
+
async function inlineImports(context, source, id, compileOptionsOfModule, load, copySkips) {
|
|
1167
1399
|
const filename = stripQuery(id);
|
|
1168
1400
|
const maps = [];
|
|
1401
|
+
let skipped;
|
|
1169
1402
|
const excluded = /* @__PURE__ */ new Set();
|
|
1170
1403
|
const usage = await usageIndex(context);
|
|
1171
1404
|
const absorbed = /* @__PURE__ */ new Map();
|
|
@@ -1175,6 +1408,7 @@ function solidOptimizer(options = {}) {
|
|
|
1175
1408
|
};
|
|
1176
1409
|
const contexts = /* @__PURE__ */ new Map();
|
|
1177
1410
|
let code = source;
|
|
1411
|
+
const copyNames = /* @__PURE__ */ new Map();
|
|
1178
1412
|
for (let round = 0; round < 4; round += 1) {
|
|
1179
1413
|
const program = parse(filename, code);
|
|
1180
1414
|
const scopes = analyzeScopes(program);
|
|
@@ -1212,8 +1446,10 @@ function solidOptimizer(options = {}) {
|
|
|
1212
1446
|
excluded,
|
|
1213
1447
|
contexts,
|
|
1214
1448
|
importAs,
|
|
1215
|
-
load
|
|
1449
|
+
load,
|
|
1450
|
+
skips: copySkips
|
|
1216
1451
|
});
|
|
1452
|
+
if (copy && copySkips) copyNames.set(copy.key, `${binding.imported} (${statement.source.value})`);
|
|
1217
1453
|
if (copy) {
|
|
1218
1454
|
const name = fresh(specifier.local.name);
|
|
1219
1455
|
copies.push({
|
|
@@ -1238,7 +1474,7 @@ function solidOptimizer(options = {}) {
|
|
|
1238
1474
|
result = compile(edit.toString(), {
|
|
1239
1475
|
...compileOptionsOfModule,
|
|
1240
1476
|
filename,
|
|
1241
|
-
sourceMap:
|
|
1477
|
+
sourceMap: wantsMaps(),
|
|
1242
1478
|
importedContexts: Object.fromEntries([...contexts].map(([module, exported]) => [module, [...exported]])),
|
|
1243
1479
|
sharedComponents: Object.fromEntries(copies.map((copy) => [copy.name, usersOf(copy)]).filter(([, users]) => users !== 1))
|
|
1244
1480
|
});
|
|
@@ -1248,6 +1484,12 @@ function solidOptimizer(options = {}) {
|
|
|
1248
1484
|
for (const copy of stuck) {
|
|
1249
1485
|
excluded.add(copy.key);
|
|
1250
1486
|
copies.splice(copies.indexOf(copy), 1);
|
|
1487
|
+
const reason = result.skipped?.find((entry) => entry.name === copy.name)?.reason;
|
|
1488
|
+
copySkips?.push({
|
|
1489
|
+
pass: "inline",
|
|
1490
|
+
reason: `copy-${reason ?? "left"}`,
|
|
1491
|
+
name: copyNames.get(copy.key) ?? copy.name
|
|
1492
|
+
});
|
|
1251
1493
|
}
|
|
1252
1494
|
result = void 0;
|
|
1253
1495
|
}
|
|
@@ -1256,17 +1498,19 @@ function solidOptimizer(options = {}) {
|
|
|
1256
1498
|
const [module = "", exported = ""] = copy.key.split("\0");
|
|
1257
1499
|
absorbed.set(module, /* @__PURE__ */ new Set([...absorbed.get(module) ?? [], exported]));
|
|
1258
1500
|
}
|
|
1259
|
-
maps.push(edit.generateMap({
|
|
1501
|
+
if (wantsMaps()) maps.push(edit.generateMap({
|
|
1260
1502
|
source: filename,
|
|
1261
1503
|
hires: true,
|
|
1262
1504
|
includeContent: true
|
|
1263
1505
|
}).toString());
|
|
1264
1506
|
if (result.map) maps.push(result.map.toString());
|
|
1265
1507
|
code = result.code;
|
|
1508
|
+
({skipped} = result);
|
|
1266
1509
|
}
|
|
1267
1510
|
return code === source ? void 0 : {
|
|
1268
1511
|
code,
|
|
1269
|
-
maps
|
|
1512
|
+
maps,
|
|
1513
|
+
skipped
|
|
1270
1514
|
};
|
|
1271
1515
|
}
|
|
1272
1516
|
/**
|
|
@@ -1288,7 +1532,9 @@ function solidOptimizer(options = {}) {
|
|
|
1288
1532
|
maps.push(named.map);
|
|
1289
1533
|
}
|
|
1290
1534
|
if (moduleCompileOptions.inline !== false) {
|
|
1291
|
-
const
|
|
1535
|
+
const copySkips = compileOptions.report ? [] : void 0;
|
|
1536
|
+
const imported = await inlineImports(context, code, id, moduleCompileOptions, async (target) => loadModule(context, id, target), copySkips);
|
|
1537
|
+
addSkips(context, copySkips);
|
|
1292
1538
|
if (imported) {
|
|
1293
1539
|
code = imported.code;
|
|
1294
1540
|
maps.push(...imported.maps);
|
|
@@ -1304,14 +1550,17 @@ function solidOptimizer(options = {}) {
|
|
|
1304
1550
|
sourceMap: false
|
|
1305
1551
|
});
|
|
1306
1552
|
const needed = /* @__PURE__ */ new Set();
|
|
1307
|
-
for (const version of /* @__PURE__ */ new Set([code, inlined?.code ?? code]))
|
|
1308
|
-
|
|
1309
|
-
|
|
1553
|
+
for (const version of /* @__PURE__ */ new Set([code, inlined?.code ?? code])) for (const helper of helpersOf(getCompiler(), version, filename, jsxOptions(false), moduleName)) needed.add(helper);
|
|
1554
|
+
if (moduleCompileOptions.inline !== false && spreadsOntoElement(code, filename)) for (const helper of attributeHelpers()) needed.add(helper);
|
|
1555
|
+
const protectedRefs = protectRefs(code, filename);
|
|
1556
|
+
if (protectedRefs) {
|
|
1557
|
+
code = protectedRefs.code;
|
|
1558
|
+
maps.push(protectedRefs.map);
|
|
1310
1559
|
}
|
|
1311
1560
|
preserved.add(id);
|
|
1312
1561
|
if (needed.size === 0) return code === source ? null : {
|
|
1313
1562
|
code,
|
|
1314
|
-
map: combineMaps(maps)
|
|
1563
|
+
map: combineMaps(wantsMaps(), maps)
|
|
1315
1564
|
};
|
|
1316
1565
|
const marked = addMarker(code, filename, {
|
|
1317
1566
|
moduleName,
|
|
@@ -1325,11 +1574,11 @@ function solidOptimizer(options = {}) {
|
|
|
1325
1574
|
});
|
|
1326
1575
|
if (!marked) return code === source ? null : {
|
|
1327
1576
|
code,
|
|
1328
|
-
map: combineMaps(maps)
|
|
1577
|
+
map: combineMaps(wantsMaps(), maps)
|
|
1329
1578
|
};
|
|
1330
1579
|
return {
|
|
1331
1580
|
code: marked.code,
|
|
1332
|
-
map: combineMaps([...maps, marked.map])
|
|
1581
|
+
map: combineMaps(wantsMaps(), [...maps, marked.map])
|
|
1333
1582
|
};
|
|
1334
1583
|
}
|
|
1335
1584
|
const originalTransform = main.transform;
|
|
@@ -1338,54 +1587,137 @@ function solidOptimizer(options = {}) {
|
|
|
1338
1587
|
if (chunkMode() && isClient(this) && isJSXModule(id)) return keepJSX(this, code, id);
|
|
1339
1588
|
return originalHandler.call(this, code, id, transformOptions);
|
|
1340
1589
|
};
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1590
|
+
const modulePlugin = {
|
|
1591
|
+
name: "solid-optimizer:module",
|
|
1592
|
+
enforce: "pre",
|
|
1593
|
+
configResolved(resolved) {
|
|
1594
|
+
config = resolved;
|
|
1595
|
+
},
|
|
1596
|
+
buildStart() {
|
|
1597
|
+
moduleConstants.clear();
|
|
1598
|
+
jsxImports.clear();
|
|
1599
|
+
usageIndexes.clear();
|
|
1600
|
+
skips.delete(this.environment.name);
|
|
1601
|
+
},
|
|
1602
|
+
watchChange() {
|
|
1603
|
+
moduleConstants.clear();
|
|
1604
|
+
jsxImports.clear();
|
|
1605
|
+
usageIndexes.clear();
|
|
1606
|
+
},
|
|
1607
|
+
async transform(source, id) {
|
|
1608
|
+
if (!isBuild() && optimizer.dev !== true) return null;
|
|
1609
|
+
if (chunkMode() && isClient(this) || !isJSXModule(id)) return null;
|
|
1610
|
+
const filename = stripQuery(id);
|
|
1611
|
+
const sourceOptions = {
|
|
1612
|
+
...await moduleOptions(this, source, id),
|
|
1613
|
+
server: optimizer.server !== false && !isClient(this)
|
|
1614
|
+
};
|
|
1615
|
+
const result = compile(source, sourceOptions);
|
|
1616
|
+
let code = result.code;
|
|
1617
|
+
const maps = [result.map?.toString()];
|
|
1618
|
+
let { skipped } = result;
|
|
1619
|
+
if (isBuild() && sourceOptions.inline !== false) {
|
|
1620
|
+
const copySkips = compileOptions.report ? [] : void 0;
|
|
1621
|
+
const imported = await inlineImports(this, code, id, sourceOptions, async (target) => loadRecorded(this, id, target), copySkips);
|
|
1622
|
+
addSkips(this, copySkips);
|
|
1623
|
+
if (imported) {
|
|
1624
|
+
code = imported.code;
|
|
1625
|
+
maps.push(...imported.maps);
|
|
1626
|
+
skipped = imported.skipped ?? skipped;
|
|
1627
|
+
}
|
|
1628
|
+
const exposed = exposeLocals(code, filename);
|
|
1629
|
+
if (exposed) {
|
|
1630
|
+
code = exposed.code;
|
|
1631
|
+
maps.push(exposed.map);
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
addSkips(this, skipped);
|
|
1635
|
+
recorded.set(environmentKey(this, id), code);
|
|
1636
|
+
if (code === source) return null;
|
|
1637
|
+
return {
|
|
1638
|
+
code,
|
|
1639
|
+
map: combineMaps(wantsMaps(), maps)
|
|
1640
|
+
};
|
|
1641
|
+
}
|
|
1642
|
+
};
|
|
1643
|
+
const chunkPlugin = {
|
|
1644
|
+
name: "solid-optimizer:chunk",
|
|
1645
|
+
enforce: "post",
|
|
1646
|
+
config(_, env) {
|
|
1647
|
+
if (env.command !== "build") return;
|
|
1648
|
+
return {
|
|
1649
|
+
oxc: { jsx: "preserve" },
|
|
1650
|
+
build: { rolldownOptions: { transform: { jsx: "preserve" } } }
|
|
1651
|
+
};
|
|
1652
|
+
},
|
|
1653
|
+
renderChunk: {
|
|
1654
|
+
order: "pre",
|
|
1655
|
+
handler(code, chunk) {
|
|
1656
|
+
if (!chunkMode() || !isClient(this) || !code.includes("__SOLID_OPTIMIZER_KEEP__")) return null;
|
|
1657
|
+
const filename = chunk.fileName;
|
|
1658
|
+
let repaired;
|
|
1659
|
+
let runtime;
|
|
1660
|
+
try {
|
|
1661
|
+
runtime = readMarkers(code, filename);
|
|
1662
|
+
} catch (error) {
|
|
1663
|
+
repaired = error instanceof SyntaxError ? repairJSXSequences(code, filename) : void 0;
|
|
1664
|
+
if (!repaired) throw error;
|
|
1665
|
+
runtime = readMarkers(repaired.code, filename);
|
|
1666
|
+
}
|
|
1667
|
+
if (!runtime) return null;
|
|
1668
|
+
const restored = restoreRefs(runtime.code, filename);
|
|
1669
|
+
const chunkCode = restored?.code ?? runtime.code;
|
|
1670
|
+
let optimized = compile(chunkCode, {
|
|
1671
|
+
...compileOptions,
|
|
1672
|
+
filename,
|
|
1673
|
+
sourceMap: wantsMaps(),
|
|
1674
|
+
constantVars: true,
|
|
1675
|
+
builtInAliases: runtime.builtInAliases
|
|
1676
|
+
});
|
|
1677
|
+
let lowered = lower(getCompiler(), optimized.code, filename, jsxOptions(false), wantsMaps());
|
|
1678
|
+
let linked;
|
|
1679
|
+
try {
|
|
1680
|
+
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1681
|
+
addSkips(this, optimized.skipped);
|
|
1682
|
+
} catch (error) {
|
|
1683
|
+
if (!(error instanceof MissingHelperError)) throw error;
|
|
1684
|
+
this.warn(`${error.message} ${filename} is not optimized.`);
|
|
1685
|
+
optimized = void 0;
|
|
1686
|
+
addSkips(this, [{
|
|
1687
|
+
pass: "inline",
|
|
1688
|
+
reason: "missing-helper",
|
|
1689
|
+
name: filename
|
|
1690
|
+
}]);
|
|
1691
|
+
lowered = lower(getCompiler(), chunkCode, filename, jsxOptions(false), wantsMaps());
|
|
1692
|
+
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1380
1693
|
}
|
|
1381
|
-
recorded.set(environmentKey(this, id), code);
|
|
1382
|
-
if (code === source) return null;
|
|
1383
1694
|
return {
|
|
1384
|
-
code,
|
|
1385
|
-
map: combineMaps(
|
|
1695
|
+
code: linked.code,
|
|
1696
|
+
map: combineMaps(wantsMaps(), [
|
|
1697
|
+
repaired?.map,
|
|
1698
|
+
runtime.map,
|
|
1699
|
+
restored?.map,
|
|
1700
|
+
optimized?.map?.toString(),
|
|
1701
|
+
lowered.map,
|
|
1702
|
+
linked.map
|
|
1703
|
+
])
|
|
1386
1704
|
};
|
|
1387
1705
|
}
|
|
1388
|
-
}
|
|
1706
|
+
}
|
|
1707
|
+
};
|
|
1708
|
+
chunkPlugin.generateBundle = function generateBundle() {
|
|
1709
|
+
const { report } = optimizer;
|
|
1710
|
+
if (report === void 0 || report === false) return;
|
|
1711
|
+
const environment = this.environment.name;
|
|
1712
|
+
const result = {
|
|
1713
|
+
environment,
|
|
1714
|
+
skips: skips.get(environment) ?? []
|
|
1715
|
+
};
|
|
1716
|
+
if (typeof report === "function") report(result);
|
|
1717
|
+
else this.environment.logger.info(formatReport(result));
|
|
1718
|
+
};
|
|
1719
|
+
return [
|
|
1720
|
+
modulePlugin,
|
|
1389
1721
|
main,
|
|
1390
1722
|
{
|
|
1391
1723
|
name: "solid-optimizer:module-type",
|
|
@@ -1402,61 +1734,7 @@ function solidOptimizer(options = {}) {
|
|
|
1402
1734
|
};
|
|
1403
1735
|
}
|
|
1404
1736
|
},
|
|
1405
|
-
|
|
1406
|
-
name: "solid-optimizer:chunk",
|
|
1407
|
-
enforce: "post",
|
|
1408
|
-
config(_, env) {
|
|
1409
|
-
if (env.command !== "build") return;
|
|
1410
|
-
return {
|
|
1411
|
-
oxc: { jsx: "preserve" },
|
|
1412
|
-
build: { rolldownOptions: { transform: { jsx: "preserve" } } }
|
|
1413
|
-
};
|
|
1414
|
-
},
|
|
1415
|
-
renderChunk: {
|
|
1416
|
-
order: "pre",
|
|
1417
|
-
handler(code, chunk) {
|
|
1418
|
-
if (!chunkMode() || !isClient(this) || !code.includes("__SOLID_OPTIMIZER_KEEP__")) return null;
|
|
1419
|
-
const filename = chunk.fileName;
|
|
1420
|
-
let repaired;
|
|
1421
|
-
let runtime;
|
|
1422
|
-
try {
|
|
1423
|
-
runtime = readMarkers(code, filename);
|
|
1424
|
-
} catch (error) {
|
|
1425
|
-
repaired = error instanceof SyntaxError ? repairJSXSequences(code, filename) : void 0;
|
|
1426
|
-
if (!repaired) throw error;
|
|
1427
|
-
runtime = readMarkers(repaired.code, filename);
|
|
1428
|
-
}
|
|
1429
|
-
if (!runtime) return null;
|
|
1430
|
-
let optimized = compile(runtime.code, {
|
|
1431
|
-
...compileOptions,
|
|
1432
|
-
filename,
|
|
1433
|
-
constantVars: true,
|
|
1434
|
-
builtInAliases: runtime.builtInAliases
|
|
1435
|
-
});
|
|
1436
|
-
let lowered = lower(getCompiler(), optimized.code, filename, jsxOptions(false));
|
|
1437
|
-
let linked;
|
|
1438
|
-
try {
|
|
1439
|
-
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1440
|
-
} catch (error) {
|
|
1441
|
-
if (!(error instanceof MissingHelperError)) throw error;
|
|
1442
|
-
this.warn(`${error.message} ${filename} is not optimized.`);
|
|
1443
|
-
optimized = void 0;
|
|
1444
|
-
lowered = lower(getCompiler(), runtime.code, filename, jsxOptions(false));
|
|
1445
|
-
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1446
|
-
}
|
|
1447
|
-
return {
|
|
1448
|
-
code: linked.code,
|
|
1449
|
-
map: combineMaps([
|
|
1450
|
-
repaired?.map,
|
|
1451
|
-
runtime.map,
|
|
1452
|
-
optimized?.map?.toString(),
|
|
1453
|
-
lowered.map,
|
|
1454
|
-
linked.map
|
|
1455
|
-
])
|
|
1456
|
-
};
|
|
1457
|
-
}
|
|
1458
|
-
}
|
|
1459
|
-
}
|
|
1737
|
+
chunkPlugin
|
|
1460
1738
|
];
|
|
1461
1739
|
}
|
|
1462
1740
|
//#endregion
|