solid-optimizer 1.0.0-next.1 → 1.0.0-next.3
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 +16 -9
- package/dist/{compile-BRCj4UN7.d.mts → compile-CwH9mvHD.d.mts} +7 -1
- package/dist/compile-CwH9mvHD.d.mts.map +1 -0
- package/dist/{compile-BpoiBa6b.mjs → compile-sCZdwEKG.mjs} +283 -41
- package/dist/compile-sCZdwEKG.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +11 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +165 -28
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/compile.ts +7 -0
- package/src/context.ts +2 -0
- package/src/copy-size.ts +64 -24
- package/src/fold.ts +32 -3
- package/src/inline.ts +343 -11
- package/src/vite/index.ts +196 -33
- package/src/vite/inject.ts +64 -1
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-BpoiBa6b.mjs.map +0 -1
package/src/vite/index.ts
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import { readFile } from 'node:fs/promises';
|
|
18
18
|
import { createRequire } from 'node:module';
|
|
19
19
|
import path from 'node:path';
|
|
20
|
+
import { gzipSync } from 'node:zlib';
|
|
20
21
|
import remapping from '@jridgewell/remapping';
|
|
21
22
|
import MagicString from 'magic-string';
|
|
22
23
|
import type * as SolidCompiler from '@solidjs/compiler';
|
|
@@ -24,7 +25,7 @@ import type { Options as SolidPluginOptions } from '@solidjs/vite-plugin';
|
|
|
24
25
|
import solidPlugin from '@solidjs/vite-plugin';
|
|
25
26
|
import type { Node, Program } from 'oxc-parser';
|
|
26
27
|
import type { Plugin, ResolvedConfig, Rollup } from 'vite';
|
|
27
|
-
import { createFilter, transformWithOxc } from 'vite';
|
|
28
|
+
import { createFilter, minifySync, transformWithOxc } from 'vite';
|
|
28
29
|
import type { CompileOptions, CompileResult, ModuleConstants } from '../compile';
|
|
29
30
|
import { compile, readModuleConstants } from '../compile';
|
|
30
31
|
import { collectParents, forEachChild, isIntrinsicTag, parse } from '../ast';
|
|
@@ -36,7 +37,7 @@ import { sharedGlobals } from './globals';
|
|
|
36
37
|
import type { Skip } from '../context';
|
|
37
38
|
import type { OptimizerReport } from './report';
|
|
38
39
|
import { formatReport } from './report';
|
|
39
|
-
import { exposeLocals, readExportedComponent } from './inject';
|
|
40
|
+
import { exposeLocals, readExportedComponent, readReexport } from './inject';
|
|
40
41
|
import { nameNamespaceTags } from './namespace';
|
|
41
42
|
import { protectRefs, restoreRefs } from './refs';
|
|
42
43
|
import type { UsageIndex } from './usage';
|
|
@@ -79,6 +80,16 @@ export interface OptimizerOptions extends Pick<
|
|
|
79
80
|
* @default false
|
|
80
81
|
*/
|
|
81
82
|
report?: boolean | ((report: OptimizerReport) => void);
|
|
83
|
+
/**
|
|
84
|
+
* Packages whose components can be copied into the modules that use
|
|
85
|
+
* them, like `['@kobalte/core']`. A package has to publish its JSX, as
|
|
86
|
+
* most Solid libraries do through the `solid` export condition. A copy
|
|
87
|
+
* lets the bundler drop the original, and the runtime code only it used,
|
|
88
|
+
* even when the package lands in another chunk.
|
|
89
|
+
*
|
|
90
|
+
* @default []
|
|
91
|
+
*/
|
|
92
|
+
packages?: readonly string[];
|
|
82
93
|
}
|
|
83
94
|
|
|
84
95
|
export type { OptimizerReport } from './report';
|
|
@@ -122,15 +133,22 @@ const LAZY_PLACEHOLDER = /"__SOLID_LAZY_MODULE__:([^"]+)"/g;
|
|
|
122
133
|
* The common kinds of attribute: a class, any other attribute, events, and
|
|
123
134
|
* children. Inlining a component turns a spread onto an element into
|
|
124
135
|
* attributes like these, in a module that may not use their helpers itself.
|
|
125
|
-
* A
|
|
126
|
-
*
|
|
136
|
+
* A `ref` can come through a spread too, and a link with a dynamic attribute
|
|
137
|
+
* claims its element. A rarer kind, like `style`, can still need a helper no
|
|
138
|
+
* module imports. The chunk is then lowered without optimizing it.
|
|
127
139
|
*/
|
|
128
140
|
const ATTRIBUTE_SAMPLE = `const a = () => 0;
|
|
129
141
|
export const Sample = () => (
|
|
130
|
-
<div class={a()} id={a()} onClick={a()} onScroll={a()}>
|
|
142
|
+
<div class={a()} id={a()} onClick={a()} onScroll={a()}>
|
|
143
|
+
{a()}
|
|
144
|
+
<a href="/" title={a()} ref={a} />
|
|
145
|
+
</div>
|
|
131
146
|
);`;
|
|
132
147
|
|
|
133
|
-
/**
|
|
148
|
+
/**
|
|
149
|
+
* Whether the code spreads anything onto an intrinsic element, or onto
|
|
150
|
+
* `<Dynamic>`, which becomes one once its `component` is known.
|
|
151
|
+
*/
|
|
134
152
|
function spreadsOntoElement(code: string, filename: string): boolean {
|
|
135
153
|
if (!code.includes('...')) {
|
|
136
154
|
return false;
|
|
@@ -143,7 +161,7 @@ function spreadsOntoElement(code: string, filename: string): boolean {
|
|
|
143
161
|
if (
|
|
144
162
|
node.type === 'JSXOpeningElement' &&
|
|
145
163
|
node.name.type === 'JSXIdentifier' &&
|
|
146
|
-
isIntrinsicTag(node.name.name) &&
|
|
164
|
+
(isIntrinsicTag(node.name.name) || node.name.name === 'Dynamic') &&
|
|
147
165
|
node.attributes.some((attribute) => attribute.type === 'JSXSpreadAttribute')
|
|
148
166
|
) {
|
|
149
167
|
found = true;
|
|
@@ -155,6 +173,32 @@ function spreadsOntoElement(code: string, filename: string): boolean {
|
|
|
155
173
|
return found;
|
|
156
174
|
}
|
|
157
175
|
|
|
176
|
+
/** A chunk optimized and lowered one way, ready to be compared with another. */
|
|
177
|
+
interface ChunkCandidate {
|
|
178
|
+
readonly optimized: CompileResult;
|
|
179
|
+
readonly lowered: SolidCompiler.TransformResult;
|
|
180
|
+
readonly linked: { code: string; map: string };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* How many bytes a chunk takes once minified and gzipped. Minifying drops
|
|
185
|
+
* the runtime code nothing calls anymore, which is what the comparison is
|
|
186
|
+
* for, so it counts even when the build does not minify.
|
|
187
|
+
*/
|
|
188
|
+
function shippedSize(candidate: ChunkCandidate, filename: string): number {
|
|
189
|
+
const minified = minifySync(filename, candidate.linked.code, {
|
|
190
|
+
module: true,
|
|
191
|
+
compress: true,
|
|
192
|
+
mangle: true,
|
|
193
|
+
});
|
|
194
|
+
return gzipSync(minified.code).length;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Whether a module, or the module of a copy's key, is in `node_modules`. */
|
|
198
|
+
function isPackageModule(id: string): boolean {
|
|
199
|
+
return stripQuery(id).replaceAll('\\', '/').includes('/node_modules/');
|
|
200
|
+
}
|
|
201
|
+
|
|
158
202
|
function loadCompiler(): Compiler {
|
|
159
203
|
// Use the compiler `@solidjs/vite-plugin` uses, so both lower JSX the same way.
|
|
160
204
|
const require = createRequire(import.meta.url);
|
|
@@ -257,8 +301,15 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
257
301
|
|
|
258
302
|
/** What the final compile of each chunk or module left alone, by environment. */
|
|
259
303
|
const skips = new Map<string, Skip[]>();
|
|
260
|
-
const addSkips = (
|
|
261
|
-
|
|
304
|
+
const addSkips = (
|
|
305
|
+
context: { environment?: { name: string } },
|
|
306
|
+
list?: readonly Skip[],
|
|
307
|
+
module?: string,
|
|
308
|
+
): void => {
|
|
309
|
+
// A package's own modules are compiled too, but what they leave alone
|
|
310
|
+
// is not something the app can change. A call of a package's component
|
|
311
|
+
// in the app is still reported, from the app's module.
|
|
312
|
+
if (!list || (module !== undefined && isPackageModule(module))) {
|
|
262
313
|
return;
|
|
263
314
|
}
|
|
264
315
|
const environment = context.environment?.name ?? 'client';
|
|
@@ -693,6 +744,27 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
693
744
|
return stripped.code;
|
|
694
745
|
}
|
|
695
746
|
|
|
747
|
+
/**
|
|
748
|
+
* The scripts a virtual entry runs, like the server entry `start` mode
|
|
749
|
+
* generates: the app's modules it imports. It has no file to read, and
|
|
750
|
+
* the server and client builds have to see the same uses of each export.
|
|
751
|
+
* `undefined` when the entry has not been loaded or imports none.
|
|
752
|
+
*/
|
|
753
|
+
function virtualEntryScripts(context: Rollup.PluginContext, id: string): string[] | undefined {
|
|
754
|
+
const info = context.getModuleInfo(id);
|
|
755
|
+
if (!info || info.code === null) {
|
|
756
|
+
return undefined;
|
|
757
|
+
}
|
|
758
|
+
const scripts: string[] = [];
|
|
759
|
+
for (const imported of [...info.importedIds, ...info.dynamicallyImportedIds]) {
|
|
760
|
+
// Packages and other virtual modules hold none of the app's components.
|
|
761
|
+
if (!imported.startsWith('\0') && !imported.includes('/node_modules/')) {
|
|
762
|
+
scripts.push(imported);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
return scripts.length > 0 ? scripts : undefined;
|
|
766
|
+
}
|
|
767
|
+
|
|
696
768
|
/**
|
|
697
769
|
* The scripts an entry runs: the entry itself, or the module scripts of an
|
|
698
770
|
* HTML entry. `undefined` when an HTML entry has an inline module script,
|
|
@@ -709,7 +781,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
709
781
|
try {
|
|
710
782
|
html = await readFile(stripQuery(id), 'utf8');
|
|
711
783
|
} catch {
|
|
712
|
-
return
|
|
784
|
+
return virtualEntryScripts(context, id);
|
|
713
785
|
}
|
|
714
786
|
const scripts: string[] = [];
|
|
715
787
|
for (const [tag] of html.matchAll(/<script\b[^>]*>/gi)) {
|
|
@@ -786,6 +858,8 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
786
858
|
interface Copy {
|
|
787
859
|
/** The module and export the copy comes from. */
|
|
788
860
|
readonly key: string;
|
|
861
|
+
/** Whether it comes from a package in `optimizer.packages`. */
|
|
862
|
+
readonly fromPackage: boolean;
|
|
789
863
|
readonly name: string;
|
|
790
864
|
/** The tags in the importer that call it. */
|
|
791
865
|
readonly tags: Node[];
|
|
@@ -805,6 +879,40 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
805
879
|
readonly skips: Skip[] | undefined;
|
|
806
880
|
}
|
|
807
881
|
|
|
882
|
+
/**
|
|
883
|
+
* Whether components can be copied from a module: one of the app's, or
|
|
884
|
+
* one of a package listed in `optimizer.packages`.
|
|
885
|
+
*/
|
|
886
|
+
const isCopyable = (id: string): boolean => {
|
|
887
|
+
const file = stripQuery(id).replaceAll('\\', '/');
|
|
888
|
+
if (!file.includes('/node_modules/')) {
|
|
889
|
+
return true;
|
|
890
|
+
}
|
|
891
|
+
return (optimizer.packages ?? []).some((name) => file.includes(`/node_modules/${name}/`));
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
/** The listed packages already warned about, by environment. */
|
|
895
|
+
const warnedPackages = new Set<string>();
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* Warns once when a listed package is external, like a server build that
|
|
899
|
+
* leaves it to Node. Its components are not copied there, so a build that
|
|
900
|
+
* copies them would hydrate different markup.
|
|
901
|
+
*/
|
|
902
|
+
const warnExternalPackage = (context: Rollup.PluginContext, source: string): void => {
|
|
903
|
+
const name = (optimizer.packages ?? []).find(
|
|
904
|
+
(item) => source === item || source.startsWith(`${item}/`),
|
|
905
|
+
);
|
|
906
|
+
const key = environmentKey(context, name ?? '');
|
|
907
|
+
if (name === undefined || warnedPackages.has(key)) {
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
warnedPackages.add(key);
|
|
911
|
+
context.warn(
|
|
912
|
+
`[solid-optimizer] ${name} is external in this build, so its components are not copied. Bundle it, like with \`ssr.noExternal\`, so every build copies the same components.`,
|
|
913
|
+
);
|
|
914
|
+
};
|
|
915
|
+
|
|
808
916
|
/**
|
|
809
917
|
* Reads the component `source` exports as `imported`, and imports what it
|
|
810
918
|
* needs into the importer. `undefined` when it cannot be copied.
|
|
@@ -813,21 +921,43 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
813
921
|
context: Rollup.PluginContext,
|
|
814
922
|
id: string,
|
|
815
923
|
source: string,
|
|
816
|
-
|
|
924
|
+
requested: string,
|
|
817
925
|
state: CopyState,
|
|
818
926
|
): Promise<{ key: string; text: (name: string) => string } | undefined> {
|
|
819
927
|
const skip = (reason: string): void => {
|
|
820
|
-
state.skips?.push({ pass: 'inline', reason, name: `${
|
|
928
|
+
state.skips?.push({ pass: 'inline', reason, name: `${requested} (${source})` });
|
|
821
929
|
};
|
|
822
|
-
|
|
930
|
+
// The export's name in the module that declares it, past any re-exports.
|
|
931
|
+
let imported = requested;
|
|
932
|
+
let resolved = await context.resolve(source, id);
|
|
823
933
|
if (!resolved || resolved.external) {
|
|
934
|
+
warnExternalPackage(context, source);
|
|
824
935
|
skip('copy-external');
|
|
825
936
|
return undefined;
|
|
826
937
|
}
|
|
827
|
-
if (resolved.id
|
|
938
|
+
if (!isCopyable(resolved.id)) {
|
|
828
939
|
skip('copy-package');
|
|
829
940
|
return undefined;
|
|
830
941
|
}
|
|
942
|
+
// A module that re-exports the component by name, like a library's
|
|
943
|
+
// entry, leads to the module that declares it.
|
|
944
|
+
for (let depth = 0; depth < 8 && isJSXModule(resolved.id); depth += 1) {
|
|
945
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
946
|
+
const code = await state.load(resolved.id);
|
|
947
|
+
const reexport =
|
|
948
|
+
code === undefined ? undefined : readReexport(code, stripQuery(resolved.id), imported);
|
|
949
|
+
if (!reexport) {
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
952
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
953
|
+
const next = await context.resolve(reexport.source, resolved.id);
|
|
954
|
+
if (!next || next.external || !isCopyable(next.id)) {
|
|
955
|
+
skip('copy-package');
|
|
956
|
+
return undefined;
|
|
957
|
+
}
|
|
958
|
+
resolved = next;
|
|
959
|
+
imported = reexport.imported;
|
|
960
|
+
}
|
|
831
961
|
if (resolved.id === id) {
|
|
832
962
|
return undefined;
|
|
833
963
|
}
|
|
@@ -998,7 +1128,8 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
998
1128
|
}
|
|
999
1129
|
if (copy) {
|
|
1000
1130
|
const name = fresh(specifier.local.name);
|
|
1001
|
-
|
|
1131
|
+
const fromPackage = isPackageModule(copy.key);
|
|
1132
|
+
copies.push({ key: copy.key, fromPackage, name, tags, text: copy.text(name) });
|
|
1002
1133
|
}
|
|
1003
1134
|
}
|
|
1004
1135
|
}
|
|
@@ -1031,6 +1162,13 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1031
1162
|
.map((copy): [string, number] => [copy.name, usersOf(copy)])
|
|
1032
1163
|
.filter(([, users]) => users !== 1),
|
|
1033
1164
|
),
|
|
1165
|
+
// The estimate cannot see what a package's own modules use, so a
|
|
1166
|
+
// copy of its component never counts its declaration, or the
|
|
1167
|
+
// runtime code only it used, as removed. Listing the package asks
|
|
1168
|
+
// for its components to inline wherever they can be.
|
|
1169
|
+
alwaysInlineComponents: copies
|
|
1170
|
+
.filter((copy) => copy.fromPackage)
|
|
1171
|
+
.map((copy) => copy.name),
|
|
1034
1172
|
});
|
|
1035
1173
|
const left = analyzeScopes(parse(filename, result.code)).root.bindings;
|
|
1036
1174
|
const stuck = copies.filter((copy) => left.has(copy.name));
|
|
@@ -1117,7 +1255,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1117
1255
|
copySkips,
|
|
1118
1256
|
);
|
|
1119
1257
|
// The rest of the report comes from the chunk this module ends up in.
|
|
1120
|
-
addSkips(context, copySkips);
|
|
1258
|
+
addSkips(context, copySkips, id);
|
|
1121
1259
|
if (imported) {
|
|
1122
1260
|
code = imported.code;
|
|
1123
1261
|
maps.push(...imported.maps);
|
|
@@ -1248,7 +1386,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1248
1386
|
async (target) => loadRecorded(this, id, target),
|
|
1249
1387
|
copySkips,
|
|
1250
1388
|
);
|
|
1251
|
-
addSkips(this, copySkips);
|
|
1389
|
+
addSkips(this, copySkips, id);
|
|
1252
1390
|
if (imported) {
|
|
1253
1391
|
code = imported.code;
|
|
1254
1392
|
maps.push(...imported.maps);
|
|
@@ -1262,7 +1400,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1262
1400
|
maps.push(exposed.map);
|
|
1263
1401
|
}
|
|
1264
1402
|
}
|
|
1265
|
-
addSkips(this, skipped);
|
|
1403
|
+
addSkips(this, skipped, id);
|
|
1266
1404
|
recorded.set(environmentKey(this, id), code);
|
|
1267
1405
|
if (code === source) {
|
|
1268
1406
|
return null;
|
|
@@ -1315,29 +1453,54 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1315
1453
|
filename: `${filename}.jsx`,
|
|
1316
1454
|
sourceMap: wantsMaps(),
|
|
1317
1455
|
});
|
|
1318
|
-
|
|
1456
|
+
const chunkOptions: CompileOptions = {
|
|
1319
1457
|
...compileOptions,
|
|
1320
1458
|
filename,
|
|
1321
1459
|
sourceMap: wantsMaps(),
|
|
1322
1460
|
constantVars: true,
|
|
1323
1461
|
builtInAliases: runtime.builtInAliases,
|
|
1324
|
-
}
|
|
1462
|
+
};
|
|
1463
|
+
// Each module keeps the helpers its own JSX needs, and a merged tree
|
|
1464
|
+
// can need one none of them did. Such a candidate is left out.
|
|
1465
|
+
let missing: MissingHelperError | undefined;
|
|
1466
|
+
const lowerCandidate = (optimized: CompileResult): ChunkCandidate | undefined => {
|
|
1467
|
+
const lowered = lowerChunk(optimized.code);
|
|
1468
|
+
try {
|
|
1469
|
+
const linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1470
|
+
return { optimized, lowered, linked };
|
|
1471
|
+
} catch (error) {
|
|
1472
|
+
if (!(error instanceof MissingHelperError)) {
|
|
1473
|
+
throw error;
|
|
1474
|
+
}
|
|
1475
|
+
missing ??= error;
|
|
1476
|
+
return undefined;
|
|
1477
|
+
}
|
|
1478
|
+
};
|
|
1479
|
+
let chosen = lowerCandidate(compile(chunkCode, chunkOptions));
|
|
1480
|
+
// The estimate weighs each component on its own. It cannot see the
|
|
1481
|
+
// runtime code that only leaves the chunk once every call that needs
|
|
1482
|
+
// it is inlined, like `<Dynamic>`, `merge()`, and `omit()` behind a
|
|
1483
|
+
// headless library's components. So the chunk is also built with
|
|
1484
|
+
// every component inlined that can be, and the smaller one is kept,
|
|
1485
|
+
// as minified and gzipped.
|
|
1486
|
+
if (compileOptions.alwaysInline !== true) {
|
|
1487
|
+
const always = compile(chunkCode, { ...chunkOptions, alwaysInline: true });
|
|
1488
|
+
const other = always.code === chosen?.optimized.code ? undefined : lowerCandidate(always);
|
|
1489
|
+
if (other && (!chosen || shippedSize(other, filename) < shippedSize(chosen, filename))) {
|
|
1490
|
+
chosen = other;
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
let optimized: CompileResult | undefined;
|
|
1325
1494
|
let linked: { code: string; map: string };
|
|
1326
1495
|
let lowered: SolidCompiler.TransformResult;
|
|
1327
|
-
|
|
1328
|
-
lowered =
|
|
1329
|
-
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1496
|
+
if (chosen) {
|
|
1497
|
+
({ optimized, lowered, linked } = chosen);
|
|
1330
1498
|
addSkips(this, optimized.skipped);
|
|
1331
|
-
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
// tree can need one none of them did. Without it, the chunk is
|
|
1337
|
-
// lowered as its modules were written. Chunk mode never hydrates,
|
|
1338
|
-
// so the chunks do not have to match anything else.
|
|
1339
|
-
this.warn(`${error.message} ${filename} is not optimized.`);
|
|
1340
|
-
optimized = undefined;
|
|
1499
|
+
} else {
|
|
1500
|
+
// Without the helper, the chunk is lowered as its modules were
|
|
1501
|
+
// written. Chunk mode never hydrates, so the chunks do not have to
|
|
1502
|
+
// match anything else.
|
|
1503
|
+
this.warn(`${missing?.message ?? ''} ${filename} is not optimized.`);
|
|
1341
1504
|
addSkips(this, [{ pass: 'inline', reason: 'missing-helper', name: filename }]);
|
|
1342
1505
|
lowered = lowerChunk(chunkCode);
|
|
1343
1506
|
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
package/src/vite/inject.ts
CHANGED
|
@@ -94,6 +94,68 @@ function functionOf(binding: Binding): TopLevelFunction | undefined {
|
|
|
94
94
|
return undefined;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
/**
|
|
98
|
+
* The binding a top-level alias like `const Button = ButtonRoot` names, or
|
|
99
|
+
* the binding itself when it is not one. Libraries often export a component
|
|
100
|
+
* under such a second name.
|
|
101
|
+
*/
|
|
102
|
+
function aliasedBinding(binding: Binding): Binding {
|
|
103
|
+
let current = binding;
|
|
104
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
105
|
+
const { declaration } = current;
|
|
106
|
+
if (
|
|
107
|
+
declaration.type !== 'VariableDeclarator' ||
|
|
108
|
+
current.kind !== 'const' ||
|
|
109
|
+
current.mutated ||
|
|
110
|
+
declaration.id !== current.identifier ||
|
|
111
|
+
!declaration.init
|
|
112
|
+
) {
|
|
113
|
+
return current;
|
|
114
|
+
}
|
|
115
|
+
const init = unwrap(declaration.init);
|
|
116
|
+
const target = init.type === 'Identifier' ? current.scope.bindings.get(init.name) : undefined;
|
|
117
|
+
if (!target || target === current || target.mutated) {
|
|
118
|
+
return current;
|
|
119
|
+
}
|
|
120
|
+
current = target;
|
|
121
|
+
}
|
|
122
|
+
return current;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Where a module re-exports `exportName` from, like
|
|
127
|
+
* `export { t as Separator } from './chunk.jsx'`, or `undefined` when it
|
|
128
|
+
* does not re-export it by name.
|
|
129
|
+
*/
|
|
130
|
+
export function readReexport(
|
|
131
|
+
code: string,
|
|
132
|
+
filename: string,
|
|
133
|
+
exportName: string,
|
|
134
|
+
): { source: string; imported: string } | undefined {
|
|
135
|
+
const program = parse(filename, code);
|
|
136
|
+
for (const statement of program.body) {
|
|
137
|
+
if (
|
|
138
|
+
statement.type !== 'ExportNamedDeclaration' ||
|
|
139
|
+
!statement.source ||
|
|
140
|
+
statement.exportKind === 'type'
|
|
141
|
+
) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
for (const specifier of statement.specifiers) {
|
|
145
|
+
const exported =
|
|
146
|
+
specifier.exported.type === 'Identifier'
|
|
147
|
+
? specifier.exported.name
|
|
148
|
+
: specifier.exported.value;
|
|
149
|
+
const local =
|
|
150
|
+
specifier.local.type === 'Identifier' ? specifier.local.name : specifier.local.value;
|
|
151
|
+
if (exported === exportName && specifier.exportKind !== 'type') {
|
|
152
|
+
return { source: statement.source.value, imported: local };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
97
159
|
/**
|
|
98
160
|
* The names a module exports, each with the top-level binding it exports.
|
|
99
161
|
*/
|
|
@@ -278,7 +340,8 @@ export function readExportedComponent(
|
|
|
278
340
|
const program = parse(filename, code);
|
|
279
341
|
const scopes = analyzeScopes(program);
|
|
280
342
|
const exports = exportsOf(program, scopes);
|
|
281
|
-
const
|
|
343
|
+
const named = exports.get(exportName);
|
|
344
|
+
const binding = named ? aliasedBinding(named) : undefined;
|
|
282
345
|
const component = binding ? functionOf(binding) : undefined;
|
|
283
346
|
if (!binding || !component) {
|
|
284
347
|
return undefined;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compile-BRCj4UN7.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC0DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC5DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCF/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,kBAAkB,SAAS,eAAe;;;;;;EAM1C,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAmGI,QAAQ,cAAc,UAAS,iBAAsB;UA0DpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}
|