solid-optimizer 0.4.1 → 1.0.0-next.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 +74 -47
- package/dist/{compile-BQuBDA0Q.d.mts → compile-BRCj4UN7.d.mts} +15 -22
- package/dist/compile-BRCj4UN7.d.mts.map +1 -0
- package/dist/{compile-CqhOfBvG.mjs → compile-BpoiBa6b.mjs} +943 -670
- package/dist/compile-BpoiBa6b.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +3 -10
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +305 -204
- package/dist/vite.mjs.map +1 -1
- package/package.json +10 -11
- package/src/compile.ts +16 -40
- package/src/constants.ts +42 -2
- package/src/context.ts +3 -0
- package/src/copy-size.ts +73 -0
- package/src/fold.ts +619 -17
- package/src/inline.ts +352 -292
- package/src/memo.ts +161 -30
- package/src/provider.ts +40 -75
- package/src/scope.ts +14 -1
- package/src/size.ts +35 -75
- package/src/vite/globals.ts +67 -0
- package/src/vite/index.ts +279 -264
- package/src/vite/inject.ts +14 -4
- package/src/vite/runtime.ts +23 -3
- package/dist/compile-BQuBDA0Q.d.mts.map +0 -1
- package/dist/compile-CqhOfBvG.mjs.map +0 -1
- package/src/server.ts +0 -275
package/dist/vite.mjs
CHANGED
|
@@ -1,12 +1,46 @@
|
|
|
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-BpoiBa6b.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
7
|
import path from "node:path";
|
|
8
|
+
import solidPlugin from "@solidjs/vite-plugin";
|
|
8
9
|
import { createFilter, transformWithOxc } from "vite";
|
|
9
|
-
|
|
10
|
+
//#region src/vite/globals.ts
|
|
11
|
+
/**
|
|
12
|
+
* The `define` entries and `import.meta.env` values that are the same in
|
|
13
|
+
* every build environment, as global constants. A value that differs, like
|
|
14
|
+
* `import.meta.env.SSR`, could make the server and client builds fold, and
|
|
15
|
+
* then inline, differently, so their hydration keys would not match.
|
|
16
|
+
*/
|
|
17
|
+
function sharedGlobals(config) {
|
|
18
|
+
const globals = {};
|
|
19
|
+
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
|
+
const environments = Object.values(config.environments);
|
|
21
|
+
const defines = environments.length > 0 ? environments.map((environment) => environment.define ?? config.define ?? {}) : [config.define ?? {}];
|
|
22
|
+
const keys = new Set(defines.flatMap((define) => Object.keys(define)));
|
|
23
|
+
for (const key of keys) {
|
|
24
|
+
const values = defines.map((define) => defineValue(define[key]));
|
|
25
|
+
const first = values.at(0);
|
|
26
|
+
if (first !== void 0 && values.every((value) => value !== void 0 && Object.is(value.value, first.value))) globals[key] = first.value;
|
|
27
|
+
}
|
|
28
|
+
return globals;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The primitive a `define` entry replaces its key with. A string is code,
|
|
32
|
+
* so only one that is a JSON literal counts.
|
|
33
|
+
*/
|
|
34
|
+
function defineValue(value) {
|
|
35
|
+
let parsed = value;
|
|
36
|
+
if (typeof value === "string") try {
|
|
37
|
+
parsed = JSON.parse(value);
|
|
38
|
+
} catch {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (parsed === null || typeof parsed === "string" || typeof parsed === "number" || typeof parsed === "boolean") return { value: parsed };
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
10
44
|
//#region src/vite/report.ts
|
|
11
45
|
/**
|
|
12
46
|
* Groups skips by pass and reason, most frequent first.
|
|
@@ -53,7 +87,7 @@ function formatReport(report) {
|
|
|
53
87
|
* Keeps Solid's runtime helpers reachable while JSX waits in a bundled chunk.
|
|
54
88
|
*
|
|
55
89
|
* Solid's JSX transform imports helpers like `template` and `insert` from
|
|
56
|
-
*
|
|
90
|
+
* `@solidjs/web`. When the transform runs on a chunk, the bundle is already
|
|
57
91
|
* built, so a new bare import cannot be resolved or bundled anymore. Instead,
|
|
58
92
|
* every module imports the helpers it will need before bundling, and passes
|
|
59
93
|
* them to a marker call. The bundler keeps the call, since it could have side
|
|
@@ -104,6 +138,19 @@ function exportedName(specifier) {
|
|
|
104
138
|
return specifier.imported.type === "Literal" ? specifier.imported.value : specifier.imported.name;
|
|
105
139
|
}
|
|
106
140
|
/**
|
|
141
|
+
* The helpers Solid's JSX transform imported from `source` into its output.
|
|
142
|
+
* The module's own imports, including type imports, are left out.
|
|
143
|
+
*/
|
|
144
|
+
function generatedImports(code, filename, source) {
|
|
145
|
+
const program = parse(filename, code);
|
|
146
|
+
const names = [];
|
|
147
|
+
for (const statement of program.body) {
|
|
148
|
+
if (statement.type !== "ImportDeclaration" || statement.source.value !== source) continue;
|
|
149
|
+
for (const specifier of statement.specifiers) if (specifier.type === "ImportSpecifier" && isGeneratedSpecifier(specifier)) names.push(exportedName(specifier));
|
|
150
|
+
}
|
|
151
|
+
return names;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
107
154
|
* The functions declared at the top level of a module, with the statement
|
|
108
155
|
* that declares each: `function A() {}`, `const A = () => ...`, and their
|
|
109
156
|
* `export` forms.
|
|
@@ -420,9 +467,10 @@ function exposeLocals(code, filename) {
|
|
|
420
467
|
}
|
|
421
468
|
/**
|
|
422
469
|
* Whether a function returns JSX whose root calls a component declared at
|
|
423
|
-
* the top of its own module.
|
|
470
|
+
* the top of its own module. A context's provider does not count, since the
|
|
471
|
+
* provider pass can remove it from a copy.
|
|
424
472
|
*/
|
|
425
|
-
function returnsLocalCall(fn, scopes) {
|
|
473
|
+
function returnsLocalCall(fn, scopes, moduleSources) {
|
|
426
474
|
if (fn.type !== "FunctionDeclaration" && fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression") return false;
|
|
427
475
|
const { body } = fn;
|
|
428
476
|
if (!body) return false;
|
|
@@ -435,7 +483,7 @@ function returnsLocalCall(fn, scopes) {
|
|
|
435
483
|
const inner = unwrap(root);
|
|
436
484
|
if (inner.type !== "JSXElement" || inner.openingElement.name.type !== "JSXIdentifier") return false;
|
|
437
485
|
const binding = scopes.references.get(inner.openingElement.name);
|
|
438
|
-
return binding !== void 0 && binding.scope === scopes.root && binding.kind !== "import";
|
|
486
|
+
return binding !== void 0 && binding.scope === scopes.root && binding.kind !== "import" && !isContext(binding, scopes, moduleSources);
|
|
439
487
|
}
|
|
440
488
|
/**
|
|
441
489
|
* Reads the component a module exports under `exportName`, from the module
|
|
@@ -497,7 +545,7 @@ function readExportedComponent(code, filename, exportName, moduleSources) {
|
|
|
497
545
|
return {
|
|
498
546
|
dependencies,
|
|
499
547
|
copy,
|
|
500
|
-
wrapsLocal: returnsLocalCall(component.fn, scopes)
|
|
548
|
+
wrapsLocal: returnsLocalCall(component.fn, scopes, moduleSources)
|
|
501
549
|
};
|
|
502
550
|
}
|
|
503
551
|
//#endregion
|
|
@@ -823,10 +871,10 @@ async function buildUsageIndex(entries, host) {
|
|
|
823
871
|
//#endregion
|
|
824
872
|
//#region src/vite/index.ts
|
|
825
873
|
/**
|
|
826
|
-
* `solid-optimizer/vite`:
|
|
874
|
+
* `solid-optimizer/vite`: `@solidjs/vite-plugin` with the optimizer built in.
|
|
827
875
|
*
|
|
828
|
-
* It takes the same options as
|
|
829
|
-
* returns the official
|
|
876
|
+
* It takes the same options as `@solidjs/vite-plugin`, plus `optimizer`, and
|
|
877
|
+
* returns the official plugins with the optimizer wired in. It runs in one of
|
|
830
878
|
* two modes.
|
|
831
879
|
*
|
|
832
880
|
* - Chunk mode keeps JSX through bundling. Each chunk is optimized as a whole
|
|
@@ -838,19 +886,7 @@ async function buildUsageIndex(entries, host) {
|
|
|
838
886
|
* differently, and hydration needs both to render the same tree, so
|
|
839
887
|
* hydrating builds only optimize within a module.
|
|
840
888
|
*/
|
|
841
|
-
const DEFAULT_MODULE_NAME = "
|
|
842
|
-
const DEFAULT_BUILT_INS = [
|
|
843
|
-
"For",
|
|
844
|
-
"Show",
|
|
845
|
-
"Switch",
|
|
846
|
-
"Match",
|
|
847
|
-
"Suspense",
|
|
848
|
-
"SuspenseList",
|
|
849
|
-
"Portal",
|
|
850
|
-
"Index",
|
|
851
|
-
"Dynamic",
|
|
852
|
-
"ErrorBoundary"
|
|
853
|
-
];
|
|
889
|
+
const DEFAULT_MODULE_NAME = "@solidjs/web";
|
|
854
890
|
const JSX_MODULE = /\.[mc]?[jt]sx$/i;
|
|
855
891
|
/**
|
|
856
892
|
* Helpers the JSX transform picks between for the same attribute. Inlining
|
|
@@ -867,6 +903,7 @@ function withRelated(helpers) {
|
|
|
867
903
|
for (const helper of helpers) for (const related of RELATED_HELPERS[helper] ?? []) result.add(related);
|
|
868
904
|
return result;
|
|
869
905
|
}
|
|
906
|
+
const LAZY_PLACEHOLDER = /"__SOLID_LAZY_MODULE__:([^"]+)"/g;
|
|
870
907
|
/**
|
|
871
908
|
* The common kinds of attribute: a class, any other attribute, events, and
|
|
872
909
|
* children. Inlining a component turns a spread onto an element into
|
|
@@ -895,58 +932,7 @@ function spreadsOntoElement(code, filename) {
|
|
|
895
932
|
}
|
|
896
933
|
function loadCompiler() {
|
|
897
934
|
const require = createRequire(import.meta.url);
|
|
898
|
-
|
|
899
|
-
const babel = createRequire(pluginRequire.resolve("babel-preset-solid"))("@babel/core");
|
|
900
|
-
const preset = pluginRequire("babel-preset-solid");
|
|
901
|
-
if (typeof preset !== "function") throw new TypeError("[solid-optimizer] babel-preset-solid did not load");
|
|
902
|
-
return {
|
|
903
|
-
babel,
|
|
904
|
-
preset
|
|
905
|
-
};
|
|
906
|
-
}
|
|
907
|
-
/**
|
|
908
|
-
* Lowers JSX with `babel-preset-solid`, as `vite-plugin-solid` does.
|
|
909
|
-
*/
|
|
910
|
-
function lower(compiler, code, filename, options, sourceMaps) {
|
|
911
|
-
const result = compiler.babel.transformSync(code, {
|
|
912
|
-
filename,
|
|
913
|
-
sourceFileName: filename,
|
|
914
|
-
presets: [[compiler.preset, options]],
|
|
915
|
-
ast: false,
|
|
916
|
-
sourceMaps,
|
|
917
|
-
configFile: false,
|
|
918
|
-
babelrc: false,
|
|
919
|
-
parserOpts: { plugins: /\.[mc]?tsx$/i.test(filename) ? ["jsx", "typescript"] : ["jsx"] }
|
|
920
|
-
});
|
|
921
|
-
if (typeof result?.code !== "string") throw new Error(`[solid-optimizer] babel-preset-solid produced no code for ${filename}`);
|
|
922
|
-
return {
|
|
923
|
-
code: result.code,
|
|
924
|
-
map: result.map ? JSON.stringify(result.map) : void 0
|
|
925
|
-
};
|
|
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;
|
|
935
|
+
return createRequire(require.resolve("@solidjs/vite-plugin"))("@solidjs/compiler");
|
|
950
936
|
}
|
|
951
937
|
function stripQuery(id) {
|
|
952
938
|
return id.replace(/\?.*$/, "");
|
|
@@ -989,8 +975,8 @@ function isClient(context) {
|
|
|
989
975
|
}
|
|
990
976
|
function solidOptimizer(options = {}) {
|
|
991
977
|
const { optimizer: optimizerOption, ...solidOptions } = options;
|
|
992
|
-
const
|
|
993
|
-
if (optimizerOption === false) return
|
|
978
|
+
const plugins = solidPlugin(solidOptions);
|
|
979
|
+
if (optimizerOption === false) return plugins;
|
|
994
980
|
const optimizer = optimizerOption ?? {};
|
|
995
981
|
const moduleName = solidOptions.solid?.moduleName ?? DEFAULT_MODULE_NAME;
|
|
996
982
|
const moduleSources = [.../* @__PURE__ */ new Set([
|
|
@@ -998,7 +984,18 @@ function solidOptimizer(options = {}) {
|
|
|
998
984
|
DEFAULT_MODULE_NAME,
|
|
999
985
|
moduleName
|
|
1000
986
|
])];
|
|
1001
|
-
const builtIns = new Set(solidOptions.solid?.builtIns ??
|
|
987
|
+
const builtIns = new Set(solidOptions.solid?.builtIns ?? [
|
|
988
|
+
"For",
|
|
989
|
+
"Show",
|
|
990
|
+
"Switch",
|
|
991
|
+
"Match",
|
|
992
|
+
"Loading",
|
|
993
|
+
"Reveal",
|
|
994
|
+
"Portal",
|
|
995
|
+
"Repeat",
|
|
996
|
+
"Dynamic",
|
|
997
|
+
"Errored"
|
|
998
|
+
]);
|
|
1002
999
|
const compileOptions = {
|
|
1003
1000
|
fold: optimizer.fold,
|
|
1004
1001
|
inline: optimizer.inline,
|
|
@@ -1010,28 +1007,18 @@ function solidOptimizer(options = {}) {
|
|
|
1010
1007
|
moduleSources,
|
|
1011
1008
|
report: optimizer.report !== void 0 && optimizer.report !== false
|
|
1012
1009
|
};
|
|
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
1010
|
/** What the final compile of each chunk or module left alone, by environment. */
|
|
1024
1011
|
const skips = /* @__PURE__ */ new Map();
|
|
1025
1012
|
const addSkips = (context, list) => {
|
|
1026
1013
|
if (!list) return;
|
|
1027
1014
|
const environment = context.environment?.name ?? "client";
|
|
1028
1015
|
const entries = skips.get(environment) ?? [];
|
|
1029
|
-
entries.push(...list
|
|
1030
|
-
...skip,
|
|
1031
|
-
name: shortName(skip.name)
|
|
1032
|
-
})));
|
|
1016
|
+
entries.push(...list);
|
|
1033
1017
|
skips.set(environment, entries);
|
|
1034
1018
|
};
|
|
1019
|
+
let config;
|
|
1020
|
+
/** The `define` entries and `import.meta.env` values every environment shares. */
|
|
1021
|
+
let globals = {};
|
|
1035
1022
|
let compiler;
|
|
1036
1023
|
let filter;
|
|
1037
1024
|
const getCompiler = () => {
|
|
@@ -1043,26 +1030,49 @@ function solidOptimizer(options = {}) {
|
|
|
1043
1030
|
const wantsMaps = () => !isBuild() || Boolean(config?.build.sourcemap);
|
|
1044
1031
|
/**
|
|
1045
1032
|
* The JSX options the official plugin passes for a posture, so the
|
|
1046
|
-
* lowering here matches it. Mirrors
|
|
1033
|
+
* lowering here matches it. Mirrors `getSolidOptions` in `@solidjs/vite-plugin`.
|
|
1047
1034
|
*/
|
|
1048
1035
|
const jsxOptions = (isSsr) => {
|
|
1049
|
-
let posture
|
|
1050
|
-
|
|
1036
|
+
let posture;
|
|
1037
|
+
if (solidOptions.start && !solidOptions.ssr) posture = {
|
|
1038
|
+
generate: isSsr ? "ssr" : "dom",
|
|
1051
1039
|
hydratable: false
|
|
1052
1040
|
};
|
|
1053
|
-
if (solidOptions.ssr) posture = {
|
|
1041
|
+
else if (solidOptions.ssr) posture = {
|
|
1054
1042
|
generate: isSsr ? "ssr" : "dom",
|
|
1055
1043
|
hydratable: true
|
|
1056
1044
|
};
|
|
1045
|
+
else posture = {
|
|
1046
|
+
generate: "dom",
|
|
1047
|
+
hydratable: false
|
|
1048
|
+
};
|
|
1049
|
+
const dev = solidOptions.dev === true || solidOptions.dev !== false && !isBuild();
|
|
1050
|
+
const names = dev || solidOptions.observe === true;
|
|
1051
|
+
const { sourceNames, ...rest } = solidOptions.solid ?? {};
|
|
1052
|
+
let resolvedNames = false;
|
|
1053
|
+
if (typeof sourceNames === "boolean") resolvedNames = sourceNames;
|
|
1054
|
+
else if (sourceNames !== void 0 || names) {
|
|
1055
|
+
const components = sourceNames?.components ?? names;
|
|
1056
|
+
const bindings = sourceNames?.bindings ?? names;
|
|
1057
|
+
resolvedNames = components || bindings ? {
|
|
1058
|
+
components,
|
|
1059
|
+
bindings
|
|
1060
|
+
} : false;
|
|
1061
|
+
}
|
|
1057
1062
|
return {
|
|
1058
1063
|
...posture,
|
|
1059
|
-
|
|
1064
|
+
dev,
|
|
1065
|
+
sourceNames: resolvedNames,
|
|
1066
|
+
...rest
|
|
1060
1067
|
};
|
|
1061
1068
|
};
|
|
1062
1069
|
let sampleHelpers;
|
|
1063
1070
|
/** The helpers the JSX transform uses for attributes in chunk mode. */
|
|
1064
1071
|
const attributeHelpers = () => {
|
|
1065
|
-
sampleHelpers ??=
|
|
1072
|
+
sampleHelpers ??= generatedImports(getCompiler().transform(ATTRIBUTE_SAMPLE, {
|
|
1073
|
+
...jsxOptions(false),
|
|
1074
|
+
filename: "sample.jsx"
|
|
1075
|
+
}).code, "sample.jsx", moduleName);
|
|
1066
1076
|
return sampleHelpers;
|
|
1067
1077
|
};
|
|
1068
1078
|
/**
|
|
@@ -1070,72 +1080,118 @@ function solidOptimizer(options = {}) {
|
|
|
1070
1080
|
*/
|
|
1071
1081
|
const chunkMode = () => {
|
|
1072
1082
|
if (!isBuild() || (optimizer.mode ?? "auto") !== "auto") return false;
|
|
1073
|
-
if (solidOptions.babel) return false;
|
|
1083
|
+
if (solidOptions.babel || solidOptions.compiler === "babel") return false;
|
|
1074
1084
|
const client = jsxOptions(false);
|
|
1075
1085
|
return client.generate === "dom" && client.hydratable !== true;
|
|
1076
1086
|
};
|
|
1077
1087
|
const isJSXModule = (id) => {
|
|
1078
|
-
filter ??= createFilter(solidOptions.include, solidOptions.exclude);
|
|
1088
|
+
filter ??= createFilter(solidOptions.include, solidOptions.exclude, { resolve: config?.root });
|
|
1079
1089
|
return filter(id) && JSX_MODULE.test(stripQuery(id));
|
|
1080
1090
|
};
|
|
1081
1091
|
/** Modules that keep their JSX, which the bundler must parse as JSX. */
|
|
1082
1092
|
const preserved = /* @__PURE__ */ new Set();
|
|
1083
|
-
/** What each module imports and the constants it exports, by module id. */
|
|
1084
|
-
const moduleConstants = /* @__PURE__ */ new Map();
|
|
1085
1093
|
/**
|
|
1086
|
-
*
|
|
1094
|
+
* What each module imports and the constants it exports, by module id,
|
|
1095
|
+
* once read in full. A module whose constants needed a module with JSX
|
|
1096
|
+
* that could not be loaded, to avoid a cycle, is not kept, since another
|
|
1097
|
+
* transform may load it.
|
|
1087
1098
|
*
|
|
1088
|
-
* A
|
|
1089
|
-
*
|
|
1090
|
-
*
|
|
1099
|
+
* A read in progress is never shared. It can wait for a module with JSX,
|
|
1100
|
+
* and a read that waited for it would wait for that module too, without
|
|
1101
|
+
* the cycle checks knowing. Two reads of modules that import each other
|
|
1102
|
+
* would also wait for each other.
|
|
1103
|
+
*/
|
|
1104
|
+
const moduleConstants = /* @__PURE__ */ new Map();
|
|
1105
|
+
/**
|
|
1106
|
+
* The module an import resolves to, when its constants can be read, and
|
|
1107
|
+
* whether it has JSX. Dependencies are skipped to keep the build fast.
|
|
1091
1108
|
*/
|
|
1092
1109
|
async function resolveConstantModule(context, specifier, importer) {
|
|
1093
1110
|
if (moduleSources.includes(specifier)) return;
|
|
1094
1111
|
const resolved = await context.resolve(specifier, importer);
|
|
1095
|
-
if (!resolved || resolved.external || resolved.id.includes("/node_modules/")
|
|
1096
|
-
return
|
|
1112
|
+
if (!resolved || resolved.external || resolved.id.includes("/node_modules/")) return;
|
|
1113
|
+
return {
|
|
1114
|
+
id: resolved.id,
|
|
1115
|
+
jsx: isJSXModule(resolved.id)
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* The code of a module with JSX to read constants from, as this plugin's
|
|
1120
|
+
* transform returns it. `transform` is the module whose transform waits for
|
|
1121
|
+
* it, which the cycle checks are about. `undefined` when that could wait
|
|
1122
|
+
* for itself.
|
|
1123
|
+
*/
|
|
1124
|
+
async function loadJSXModule(context, transform, target) {
|
|
1125
|
+
return chunkMode() && isClient(context) ? loadModule(context, transform, target) : loadRecorded(context, transform, target);
|
|
1097
1126
|
}
|
|
1098
1127
|
/**
|
|
1099
1128
|
* The constants of the modules `code` imports, keyed by import specifier.
|
|
1100
1129
|
*
|
|
1101
1130
|
* Each module is read as the bundler loads it, after every plugin has
|
|
1102
|
-
* transformed it
|
|
1103
|
-
*
|
|
1104
|
-
* leaves the modules in it without them.
|
|
1131
|
+
* transformed it. A module with JSX is read from this plugin's own output
|
|
1132
|
+
* for it, unless its transform could be waiting for `transform`.
|
|
1105
1133
|
*/
|
|
1106
|
-
async function importedConstants(context, imports, importer, visiting) {
|
|
1134
|
+
async function importedConstants(context, imports, importer, visiting, transform) {
|
|
1135
|
+
let partial = false;
|
|
1107
1136
|
const entries = await Promise.all(imports.map(async (specifier) => {
|
|
1108
|
-
const
|
|
1109
|
-
if (
|
|
1110
|
-
const
|
|
1111
|
-
|
|
1112
|
-
|
|
1137
|
+
const target = await resolveConstantModule(context, specifier, importer);
|
|
1138
|
+
if (target === void 0 || visiting.has(target.id)) return;
|
|
1139
|
+
const result = await constantsOf(context, target, visiting, transform);
|
|
1140
|
+
partial ||= result.partial;
|
|
1141
|
+
if (!result.constants || Object.keys(result.constants.exports).length === 0) return;
|
|
1142
|
+
return [specifier, result.constants.exports];
|
|
1113
1143
|
}));
|
|
1114
|
-
return
|
|
1144
|
+
return {
|
|
1145
|
+
constants: Object.fromEntries(entries.filter((entry) => entry !== void 0)),
|
|
1146
|
+
partial
|
|
1147
|
+
};
|
|
1115
1148
|
}
|
|
1116
|
-
async function constantsOf(context,
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1149
|
+
async function constantsOf(context, target, visiting, transform) {
|
|
1150
|
+
const { id } = target;
|
|
1151
|
+
const known = moduleConstants.get(id);
|
|
1152
|
+
if (known) return known;
|
|
1153
|
+
const result = await (async () => {
|
|
1154
|
+
let code;
|
|
1155
|
+
if (target.jsx) {
|
|
1156
|
+
code = await loadJSXModule(context, transform, id);
|
|
1157
|
+
if (code === void 0) return {
|
|
1158
|
+
constants: void 0,
|
|
1159
|
+
partial: true
|
|
1160
|
+
};
|
|
1161
|
+
} else ({code} = await context.load({ id }));
|
|
1162
|
+
if (code === null) return {
|
|
1163
|
+
constants: void 0,
|
|
1164
|
+
partial: false
|
|
1165
|
+
};
|
|
1166
|
+
const filename = `${stripQuery(id)}.${target.jsx ? "jsx" : "js"}`;
|
|
1167
|
+
try {
|
|
1168
|
+
const own = readModuleConstants(code, {
|
|
1169
|
+
filename,
|
|
1170
|
+
globalConstants: globals
|
|
1171
|
+
});
|
|
1172
|
+
if (own.imports.length === 0) return {
|
|
1173
|
+
constants: own,
|
|
1174
|
+
partial: false
|
|
1175
|
+
};
|
|
1176
|
+
const imported = await importedConstants(context, own.imports, id, /* @__PURE__ */ new Set([...visiting, id]), transform);
|
|
1177
|
+
return {
|
|
1178
|
+
constants: readModuleConstants(code, {
|
|
1128
1179
|
filename,
|
|
1129
|
-
importedConstants: imported
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1180
|
+
importedConstants: imported.constants,
|
|
1181
|
+
globalConstants: globals
|
|
1182
|
+
}),
|
|
1183
|
+
partial: imported.partial
|
|
1184
|
+
};
|
|
1185
|
+
} catch (error) {
|
|
1186
|
+
if (error instanceof SyntaxError) return {
|
|
1187
|
+
constants: void 0,
|
|
1188
|
+
partial: false
|
|
1189
|
+
};
|
|
1190
|
+
throw error;
|
|
1191
|
+
}
|
|
1192
|
+
})();
|
|
1193
|
+
if (!result.partial) moduleConstants.set(id, result);
|
|
1194
|
+
return result;
|
|
1139
1195
|
}
|
|
1140
1196
|
/**
|
|
1141
1197
|
* The compile options for a module, with the constants of its imports.
|
|
@@ -1148,13 +1204,32 @@ function solidOptimizer(options = {}) {
|
|
|
1148
1204
|
sourceMap: wantsMaps()
|
|
1149
1205
|
};
|
|
1150
1206
|
const { imports } = readModuleConstants(code, { filename });
|
|
1207
|
+
const imported = await importedConstants(context, imports, filename, /* @__PURE__ */ new Set([filename]), id);
|
|
1151
1208
|
return {
|
|
1152
1209
|
...compileOptions,
|
|
1153
1210
|
filename,
|
|
1154
1211
|
sourceMap: wantsMaps(),
|
|
1155
|
-
importedConstants:
|
|
1212
|
+
importedConstants: imported.constants,
|
|
1213
|
+
globalConstants: globals
|
|
1156
1214
|
};
|
|
1157
1215
|
}
|
|
1216
|
+
async function resolveLazyModuleUrls(context, code, importer) {
|
|
1217
|
+
const root = config?.root ?? process.cwd();
|
|
1218
|
+
let result = code;
|
|
1219
|
+
for (const match of code.matchAll(LAZY_PLACEHOLDER)) {
|
|
1220
|
+
const specifier = match.at(1);
|
|
1221
|
+
if (specifier === void 0) continue;
|
|
1222
|
+
const resolved = await context.resolve(specifier, importer);
|
|
1223
|
+
if (resolved) {
|
|
1224
|
+
const queryIndex = resolved.id.indexOf("?");
|
|
1225
|
+
const file = queryIndex === -1 ? resolved.id : resolved.id.slice(0, queryIndex);
|
|
1226
|
+
const query = queryIndex === -1 ? "" : resolved.id.slice(queryIndex);
|
|
1227
|
+
const relative = path.relative(root, file).split(path.sep).join("/") + query;
|
|
1228
|
+
result = result.replace(match[0], JSON.stringify(relative));
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
return result;
|
|
1232
|
+
}
|
|
1158
1233
|
/** The modules each transform waits for, so two never wait for each other. */
|
|
1159
1234
|
const waiting = /* @__PURE__ */ new Map();
|
|
1160
1235
|
/** Whether the transform of `from` waits, directly or not, for `to`. */
|
|
@@ -1192,14 +1267,16 @@ function solidOptimizer(options = {}) {
|
|
|
1192
1267
|
* copies components from it.
|
|
1193
1268
|
*/
|
|
1194
1269
|
const recorded = /* @__PURE__ */ new Map();
|
|
1195
|
-
/** The modules
|
|
1196
|
-
const
|
|
1270
|
+
/** The modules of the project each module imports, read from its file. */
|
|
1271
|
+
const localImports = /* @__PURE__ */ new Map();
|
|
1197
1272
|
/**
|
|
1198
|
-
* The modules
|
|
1199
|
-
* so they do not depend on the order
|
|
1273
|
+
* The modules of the project a module imports, leaving out dependencies.
|
|
1274
|
+
* They come from the file on disk, so they do not depend on the order
|
|
1275
|
+
* modules are transformed in. Modules without JSX count too, since reading
|
|
1276
|
+
* their constants can load a module with JSX.
|
|
1200
1277
|
*/
|
|
1201
|
-
async function
|
|
1202
|
-
let pending =
|
|
1278
|
+
async function localImportsOf(context, id) {
|
|
1279
|
+
let pending = localImports.get(id);
|
|
1203
1280
|
if (!pending) {
|
|
1204
1281
|
pending = (async () => {
|
|
1205
1282
|
let code;
|
|
@@ -1219,20 +1296,20 @@ function solidOptimizer(options = {}) {
|
|
|
1219
1296
|
const value = statement.source?.value;
|
|
1220
1297
|
if (value !== void 0 && !moduleSources.includes(value)) sources.add(value);
|
|
1221
1298
|
}
|
|
1222
|
-
return (await Promise.all([...sources].map(async (source) => context.resolve(source, id)))).filter((target) => target && !target.external &&
|
|
1299
|
+
return (await Promise.all([...sources].map(async (source) => context.resolve(source, id)))).filter((target) => target && !target.external && !target.id.includes("/node_modules/")).map((target) => target?.id ?? "");
|
|
1223
1300
|
})();
|
|
1224
|
-
|
|
1301
|
+
localImports.set(id, pending);
|
|
1225
1302
|
}
|
|
1226
1303
|
return pending;
|
|
1227
1304
|
}
|
|
1228
|
-
/** Whether `from` imports `to`, directly or through other modules
|
|
1305
|
+
/** Whether `from` imports `to`, directly or through other modules of the project. */
|
|
1229
1306
|
async function reaches(context, from, to) {
|
|
1230
1307
|
const seen = /* @__PURE__ */ new Set();
|
|
1231
1308
|
let frontier = [from];
|
|
1232
1309
|
while (frontier.length > 0) {
|
|
1233
1310
|
if (frontier.includes(to)) return true;
|
|
1234
1311
|
for (const id of frontier) seen.add(id);
|
|
1235
|
-
const next = await Promise.all(frontier.map(async (id) =>
|
|
1312
|
+
const next = await Promise.all(frontier.map(async (id) => localImportsOf(context, id)));
|
|
1236
1313
|
frontier = [...new Set(next.flat())].filter((id) => !seen.has(id));
|
|
1237
1314
|
}
|
|
1238
1315
|
return false;
|
|
@@ -1514,18 +1591,22 @@ function solidOptimizer(options = {}) {
|
|
|
1514
1591
|
};
|
|
1515
1592
|
}
|
|
1516
1593
|
/**
|
|
1517
|
-
* Prepares a client module for chunk mode. It
|
|
1518
|
-
*
|
|
1594
|
+
* Prepares a client module for chunk mode. It runs the passes the official
|
|
1595
|
+
* plugin runs before lowering JSX, and adds the helper marker instead of
|
|
1596
|
+
* lowering.
|
|
1519
1597
|
*/
|
|
1520
1598
|
async function keepJSX(context, source, id) {
|
|
1521
1599
|
const filename = stripQuery(id);
|
|
1522
|
-
const
|
|
1523
|
-
const
|
|
1600
|
+
const solidCompiler = getCompiler();
|
|
1601
|
+
const maps = [];
|
|
1602
|
+
let code = source;
|
|
1603
|
+
const moduleCompileOptions = await moduleOptions(context, code, id);
|
|
1604
|
+
const local = compile(code, {
|
|
1524
1605
|
...moduleCompileOptions,
|
|
1525
1606
|
inline: false
|
|
1526
1607
|
});
|
|
1527
|
-
|
|
1528
|
-
|
|
1608
|
+
code = local.code;
|
|
1609
|
+
if (local.map) maps.push(local.map.toString());
|
|
1529
1610
|
const named = nameNamespaceTags(code, filename);
|
|
1530
1611
|
if (named) {
|
|
1531
1612
|
code = named.code;
|
|
@@ -1545,45 +1626,60 @@ function solidOptimizer(options = {}) {
|
|
|
1545
1626
|
code = exposed.code;
|
|
1546
1627
|
maps.push(exposed.map);
|
|
1547
1628
|
}
|
|
1629
|
+
const lazy = await solidCompiler.transformLazyAsync(code, {
|
|
1630
|
+
filename,
|
|
1631
|
+
sourceMap: wantsMaps()
|
|
1632
|
+
});
|
|
1633
|
+
code = lazy.code;
|
|
1634
|
+
maps.push(lazy.map);
|
|
1635
|
+
const lowering = jsxOptions(false);
|
|
1636
|
+
const needed = /* @__PURE__ */ new Set();
|
|
1548
1637
|
const inlined = moduleCompileOptions.inline === false ? void 0 : compile(code, {
|
|
1549
1638
|
...moduleCompileOptions,
|
|
1550
1639
|
sourceMap: false
|
|
1551
1640
|
});
|
|
1552
|
-
const
|
|
1553
|
-
|
|
1641
|
+
for (const version of /* @__PURE__ */ new Set([code, inlined?.code ?? code])) {
|
|
1642
|
+
const dryRun = await solidCompiler.transformAsync(version, {
|
|
1643
|
+
...lowering,
|
|
1644
|
+
filename
|
|
1645
|
+
});
|
|
1646
|
+
for (const helper of generatedImports(dryRun.code, filename, moduleName)) needed.add(helper);
|
|
1647
|
+
}
|
|
1554
1648
|
if (moduleCompileOptions.inline !== false && spreadsOntoElement(code, filename)) for (const helper of attributeHelpers()) needed.add(helper);
|
|
1649
|
+
if (needed.size > 0) {
|
|
1650
|
+
const marked = addMarker(code, filename, {
|
|
1651
|
+
moduleName,
|
|
1652
|
+
helpers: withRelated([...needed]),
|
|
1653
|
+
moduleSources,
|
|
1654
|
+
builtIns: /* @__PURE__ */ new Set([
|
|
1655
|
+
...builtIns,
|
|
1656
|
+
"createContext",
|
|
1657
|
+
"render",
|
|
1658
|
+
"children",
|
|
1659
|
+
...CONTEXT_SAFE_PRIMITIVES
|
|
1660
|
+
])
|
|
1661
|
+
});
|
|
1662
|
+
if (marked) {
|
|
1663
|
+
code = marked.code;
|
|
1664
|
+
maps.push(marked.map);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1555
1667
|
const protectedRefs = protectRefs(code, filename);
|
|
1556
1668
|
if (protectedRefs) {
|
|
1557
1669
|
code = protectedRefs.code;
|
|
1558
1670
|
maps.push(protectedRefs.map);
|
|
1559
1671
|
}
|
|
1672
|
+
code = await resolveLazyModuleUrls(context, code, filename);
|
|
1560
1673
|
preserved.add(id);
|
|
1561
|
-
|
|
1562
|
-
code,
|
|
1563
|
-
map: combineMaps(wantsMaps(), maps)
|
|
1564
|
-
};
|
|
1565
|
-
const marked = addMarker(code, filename, {
|
|
1566
|
-
moduleName,
|
|
1567
|
-
helpers: withRelated([...needed]),
|
|
1568
|
-
moduleSources,
|
|
1569
|
-
builtIns: /* @__PURE__ */ new Set([
|
|
1570
|
-
...builtIns,
|
|
1571
|
-
"createContext",
|
|
1572
|
-
...CONTEXT_SAFE_PRIMITIVES
|
|
1573
|
-
])
|
|
1574
|
-
});
|
|
1575
|
-
if (!marked) return code === source ? null : {
|
|
1674
|
+
return {
|
|
1576
1675
|
code,
|
|
1577
1676
|
map: combineMaps(wantsMaps(), maps)
|
|
1578
1677
|
};
|
|
1579
|
-
return {
|
|
1580
|
-
code: marked.code,
|
|
1581
|
-
map: combineMaps(wantsMaps(), [...maps, marked.map])
|
|
1582
|
-
};
|
|
1583
1678
|
}
|
|
1584
|
-
const
|
|
1679
|
+
const main = plugins.find((plugin) => plugin.name === "solid");
|
|
1680
|
+
const originalTransform = main?.transform;
|
|
1585
1681
|
const originalHandler = typeof originalTransform === "function" ? originalTransform : originalTransform?.handler;
|
|
1586
|
-
if (originalHandler) main.transform = async function transform(code, id, transformOptions) {
|
|
1682
|
+
if (main && originalHandler) main.transform = async function transform(code, id, transformOptions) {
|
|
1587
1683
|
if (chunkMode() && isClient(this) && isJSXModule(id)) return keepJSX(this, code, id);
|
|
1588
1684
|
return originalHandler.call(this, code, id, transformOptions);
|
|
1589
1685
|
};
|
|
@@ -1592,26 +1688,24 @@ function solidOptimizer(options = {}) {
|
|
|
1592
1688
|
enforce: "pre",
|
|
1593
1689
|
configResolved(resolved) {
|
|
1594
1690
|
config = resolved;
|
|
1691
|
+
globals = sharedGlobals(resolved);
|
|
1595
1692
|
},
|
|
1596
1693
|
buildStart() {
|
|
1597
1694
|
moduleConstants.clear();
|
|
1598
|
-
|
|
1695
|
+
localImports.clear();
|
|
1599
1696
|
usageIndexes.clear();
|
|
1600
1697
|
skips.delete(this.environment.name);
|
|
1601
1698
|
},
|
|
1602
1699
|
watchChange() {
|
|
1603
1700
|
moduleConstants.clear();
|
|
1604
|
-
|
|
1701
|
+
localImports.clear();
|
|
1605
1702
|
usageIndexes.clear();
|
|
1606
1703
|
},
|
|
1607
1704
|
async transform(source, id) {
|
|
1608
1705
|
if (!isBuild() && optimizer.dev !== true) return null;
|
|
1609
1706
|
if (chunkMode() && isClient(this) || !isJSXModule(id)) return null;
|
|
1610
1707
|
const filename = stripQuery(id);
|
|
1611
|
-
const sourceOptions =
|
|
1612
|
-
...await moduleOptions(this, source, id),
|
|
1613
|
-
server: optimizer.server !== false && !isClient(this)
|
|
1614
|
-
};
|
|
1708
|
+
const sourceOptions = await moduleOptions(this, source, id);
|
|
1615
1709
|
const result = compile(source, sourceOptions);
|
|
1616
1710
|
let code = result.code;
|
|
1617
1711
|
const maps = [result.map?.toString()];
|
|
@@ -1667,6 +1761,11 @@ function solidOptimizer(options = {}) {
|
|
|
1667
1761
|
if (!runtime) return null;
|
|
1668
1762
|
const restored = restoreRefs(runtime.code, filename);
|
|
1669
1763
|
const chunkCode = restored?.code ?? runtime.code;
|
|
1764
|
+
const lowerChunk = (source) => getCompiler().transform(source, {
|
|
1765
|
+
...jsxOptions(false),
|
|
1766
|
+
filename: `${filename}.jsx`,
|
|
1767
|
+
sourceMap: wantsMaps()
|
|
1768
|
+
});
|
|
1670
1769
|
let optimized = compile(chunkCode, {
|
|
1671
1770
|
...compileOptions,
|
|
1672
1771
|
filename,
|
|
@@ -1674,9 +1773,10 @@ function solidOptimizer(options = {}) {
|
|
|
1674
1773
|
constantVars: true,
|
|
1675
1774
|
builtInAliases: runtime.builtInAliases
|
|
1676
1775
|
});
|
|
1677
|
-
let lowered = lower(getCompiler(), optimized.code, filename, jsxOptions(false), wantsMaps());
|
|
1678
1776
|
let linked;
|
|
1777
|
+
let lowered;
|
|
1679
1778
|
try {
|
|
1779
|
+
lowered = lowerChunk(optimized.code);
|
|
1680
1780
|
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1681
1781
|
addSkips(this, optimized.skipped);
|
|
1682
1782
|
} catch (error) {
|
|
@@ -1688,7 +1788,7 @@ function solidOptimizer(options = {}) {
|
|
|
1688
1788
|
reason: "missing-helper",
|
|
1689
1789
|
name: filename
|
|
1690
1790
|
}]);
|
|
1691
|
-
lowered =
|
|
1791
|
+
lowered = lowerChunk(chunkCode);
|
|
1692
1792
|
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1693
1793
|
}
|
|
1694
1794
|
return {
|
|
@@ -1716,24 +1816,25 @@ function solidOptimizer(options = {}) {
|
|
|
1716
1816
|
if (typeof report === "function") report(result);
|
|
1717
1817
|
else this.environment.logger.info(formatReport(result));
|
|
1718
1818
|
};
|
|
1819
|
+
const moduleTypePlugin = {
|
|
1820
|
+
name: "solid-optimizer:module-type",
|
|
1821
|
+
transform(code, id) {
|
|
1822
|
+
if (!preserved.has(id)) return null;
|
|
1823
|
+
const repaired = repairJSXSequences(code, stripQuery(id));
|
|
1824
|
+
return repaired ? {
|
|
1825
|
+
code: repaired.code,
|
|
1826
|
+
map: repaired.map,
|
|
1827
|
+
moduleType: "jsx"
|
|
1828
|
+
} : {
|
|
1829
|
+
code,
|
|
1830
|
+
moduleType: "jsx"
|
|
1831
|
+
};
|
|
1832
|
+
}
|
|
1833
|
+
};
|
|
1719
1834
|
return [
|
|
1720
1835
|
modulePlugin,
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
name: "solid-optimizer:module-type",
|
|
1724
|
-
transform(code, id) {
|
|
1725
|
-
if (!preserved.has(id)) return null;
|
|
1726
|
-
const repaired = repairJSXSequences(code, stripQuery(id));
|
|
1727
|
-
return repaired ? {
|
|
1728
|
-
code: repaired.code,
|
|
1729
|
-
map: repaired.map,
|
|
1730
|
-
moduleType: "jsx"
|
|
1731
|
-
} : {
|
|
1732
|
-
code,
|
|
1733
|
-
moduleType: "jsx"
|
|
1734
|
-
};
|
|
1735
|
-
}
|
|
1736
|
-
},
|
|
1836
|
+
...plugins,
|
|
1837
|
+
moduleTypePlugin,
|
|
1737
1838
|
chunkPlugin
|
|
1738
1839
|
];
|
|
1739
1840
|
}
|