solid-optimizer 1.0.0-next.0 → 1.0.0-next.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -13
- package/dist/{compile-Cz6hbiQv.mjs → compile-Cvjy2e11.mjs} +1925 -385
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-BRCj4UN7.d.mts → compile-ioIkBKzR.d.mts} +15 -2
- package/dist/compile-ioIkBKzR.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +12 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +401 -116
- package/dist/vite.mjs.map +1 -1
- package/package.json +8 -5
- package/src/ast.ts +4 -3
- package/src/compile.ts +96 -16
- package/src/conditional.ts +547 -0
- package/src/constants.ts +58 -13
- package/src/context.ts +5 -3
- package/src/copy-size.ts +68 -27
- package/src/fold.ts +334 -107
- package/src/inline.ts +832 -177
- package/src/jsx.ts +55 -34
- package/src/memo.ts +309 -38
- package/src/nesting.ts +325 -0
- package/src/provider.ts +62 -40
- package/src/scope.ts +115 -9
- package/src/size.ts +6 -1
- package/src/terminate.ts +66 -0
- package/src/text.ts +297 -0
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +419 -117
- package/src/vite/inject.ts +91 -16
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-Cz6hbiQv.mjs.map +0 -1
package/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,19 +25,20 @@ 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';
|
|
31
32
|
import { analyzeScopes } from '../scope';
|
|
32
33
|
import { CONTEXT_SAFE_PRIMITIVES } from '../provider';
|
|
33
34
|
import type { ImportedConstants } from '../constants';
|
|
35
|
+
import { runtimeImportSources } from '../constants';
|
|
34
36
|
import type { Primitive } from '../value';
|
|
35
37
|
import { sharedGlobals } from './globals';
|
|
36
38
|
import type { Skip } from '../context';
|
|
37
39
|
import type { OptimizerReport } from './report';
|
|
38
40
|
import { formatReport } from './report';
|
|
39
|
-
import { exposeLocals, readExportedComponent } from './inject';
|
|
41
|
+
import { exposeLocals, readExportedComponent, readReexport } from './inject';
|
|
40
42
|
import { nameNamespaceTags } from './namespace';
|
|
41
43
|
import { protectRefs, restoreRefs } from './refs';
|
|
42
44
|
import type { UsageIndex } from './usage';
|
|
@@ -53,7 +55,7 @@ import {
|
|
|
53
55
|
|
|
54
56
|
export interface OptimizerOptions extends Pick<
|
|
55
57
|
CompileOptions,
|
|
56
|
-
'fold' | 'inline' | 'alwaysInline' | 'contexts' | 'memos' | 'maxPasses'
|
|
58
|
+
'fold' | 'inline' | 'alwaysInline' | 'contexts' | 'memos' | 'conditionals' | 'maxPasses'
|
|
57
59
|
> {
|
|
58
60
|
/**
|
|
59
61
|
* Where the optimizer runs.
|
|
@@ -79,6 +81,16 @@ export interface OptimizerOptions extends Pick<
|
|
|
79
81
|
* @default false
|
|
80
82
|
*/
|
|
81
83
|
report?: boolean | ((report: OptimizerReport) => void);
|
|
84
|
+
/**
|
|
85
|
+
* Packages whose components can be copied into the modules that use
|
|
86
|
+
* them, like `['@kobalte/core']`. A package has to publish its JSX, as
|
|
87
|
+
* most Solid libraries do through the `solid` export condition. A copy
|
|
88
|
+
* lets the bundler drop the original, and the runtime code only it used,
|
|
89
|
+
* even when the package lands in another chunk.
|
|
90
|
+
*
|
|
91
|
+
* @default []
|
|
92
|
+
*/
|
|
93
|
+
packages?: readonly string[];
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
export type { OptimizerReport } from './report';
|
|
@@ -122,15 +134,58 @@ const LAZY_PLACEHOLDER = /"__SOLID_LAZY_MODULE__:([^"]+)"/g;
|
|
|
122
134
|
* The common kinds of attribute: a class, any other attribute, events, and
|
|
123
135
|
* children. Inlining a component turns a spread onto an element into
|
|
124
136
|
* attributes like these, in a module that may not use their helpers itself.
|
|
125
|
-
* A
|
|
126
|
-
*
|
|
137
|
+
* A `ref` can come through a spread too, and a link with a dynamic attribute
|
|
138
|
+
* claims its element. A rarer kind, like `style`, can still need a helper no
|
|
139
|
+
* module imports. The chunk is then lowered without optimizing it.
|
|
127
140
|
*/
|
|
128
141
|
const ATTRIBUTE_SAMPLE = `const a = () => 0;
|
|
129
142
|
export const Sample = () => (
|
|
130
|
-
<div class={a()} id={a()} onClick={a()} onScroll={a()}>
|
|
143
|
+
<div class={a()} id={a()} onClick={a()} onScroll={a()}>
|
|
144
|
+
{a()}
|
|
145
|
+
<a href="/" title={a()} ref={a} />
|
|
146
|
+
</div>
|
|
131
147
|
);`;
|
|
132
148
|
|
|
133
|
-
/**
|
|
149
|
+
/** The references that are the name of a JSX tag. */
|
|
150
|
+
function findTags(references: readonly Node[], parents: ReturnType<typeof collectParents>): Node[] {
|
|
151
|
+
const tags: Node[] = [];
|
|
152
|
+
for (const reference of references) {
|
|
153
|
+
const parent = parents.get(reference);
|
|
154
|
+
if (
|
|
155
|
+
(parent?.type === 'JSXOpeningElement' || parent?.type === 'JSXClosingElement') &&
|
|
156
|
+
parent.name === reference
|
|
157
|
+
) {
|
|
158
|
+
tags.push(reference);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return tags;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function findSkipReason(
|
|
165
|
+
skipped: readonly { name: string; reason: string }[],
|
|
166
|
+
name: string,
|
|
167
|
+
): string | undefined {
|
|
168
|
+
for (const entry of skipped) {
|
|
169
|
+
if (entry.name === name) {
|
|
170
|
+
return entry.reason;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function hasSpreadAttribute(attributes: readonly { type: string }[]): boolean {
|
|
177
|
+
for (const attribute of attributes) {
|
|
178
|
+
if (attribute.type === 'JSXSpreadAttribute') {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Whether the code spreads anything onto an intrinsic element, or onto
|
|
187
|
+
* `<Dynamic>`, which becomes one once its `component` is known.
|
|
188
|
+
*/
|
|
134
189
|
function spreadsOntoElement(code: string, filename: string): boolean {
|
|
135
190
|
if (!code.includes('...')) {
|
|
136
191
|
return false;
|
|
@@ -143,8 +198,8 @@ function spreadsOntoElement(code: string, filename: string): boolean {
|
|
|
143
198
|
if (
|
|
144
199
|
node.type === 'JSXOpeningElement' &&
|
|
145
200
|
node.name.type === 'JSXIdentifier' &&
|
|
146
|
-
isIntrinsicTag(node.name.name) &&
|
|
147
|
-
node.attributes
|
|
201
|
+
(isIntrinsicTag(node.name.name) || node.name.name === 'Dynamic') &&
|
|
202
|
+
hasSpreadAttribute(node.attributes)
|
|
148
203
|
) {
|
|
149
204
|
found = true;
|
|
150
205
|
return;
|
|
@@ -155,6 +210,32 @@ function spreadsOntoElement(code: string, filename: string): boolean {
|
|
|
155
210
|
return found;
|
|
156
211
|
}
|
|
157
212
|
|
|
213
|
+
/** A chunk optimized and lowered one way, ready to be compared with another. */
|
|
214
|
+
interface ChunkCandidate {
|
|
215
|
+
readonly optimized: CompileResult;
|
|
216
|
+
readonly lowered: SolidCompiler.TransformResult;
|
|
217
|
+
readonly linked: { code: string; map: string };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* How many bytes a chunk takes once minified and gzipped. Minifying drops
|
|
222
|
+
* the runtime code nothing calls anymore, which is what the comparison is
|
|
223
|
+
* for, so it counts even when the build does not minify.
|
|
224
|
+
*/
|
|
225
|
+
function shippedSize(candidate: ChunkCandidate, filename: string): number {
|
|
226
|
+
const minified = minifySync(filename, candidate.linked.code, {
|
|
227
|
+
module: true,
|
|
228
|
+
compress: true,
|
|
229
|
+
mangle: true,
|
|
230
|
+
});
|
|
231
|
+
return gzipSync(minified.code).length;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Whether a module, or the module of a copy's key, is in `node_modules`. */
|
|
235
|
+
function isPackageModule(id: string): boolean {
|
|
236
|
+
return stripQuery(id).replaceAll('\\', '/').includes('/node_modules/');
|
|
237
|
+
}
|
|
238
|
+
|
|
158
239
|
function loadCompiler(): Compiler {
|
|
159
240
|
// Use the compiler `@solidjs/vite-plugin` uses, so both lower JSX the same way.
|
|
160
241
|
const require = createRequire(import.meta.url);
|
|
@@ -172,7 +253,12 @@ function stripQuery(id: string): string {
|
|
|
172
253
|
* source maps, since chaining the maps of a large chunk takes a while.
|
|
173
254
|
*/
|
|
174
255
|
function combineMaps(wanted: boolean, maps: (string | null | undefined)[]): string | null {
|
|
175
|
-
const present
|
|
256
|
+
const present: string[] = [];
|
|
257
|
+
for (const map of maps) {
|
|
258
|
+
if (typeof map === 'string') {
|
|
259
|
+
present.push(map);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
176
262
|
if (!wanted || present.length === 0) {
|
|
177
263
|
return null;
|
|
178
264
|
}
|
|
@@ -249,6 +335,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
249
335
|
alwaysInline: optimizer.alwaysInline,
|
|
250
336
|
contexts: optimizer.contexts,
|
|
251
337
|
memos: optimizer.memos,
|
|
338
|
+
conditionals: optimizer.conditionals,
|
|
252
339
|
maxPasses: optimizer.maxPasses,
|
|
253
340
|
builtIns: [...builtIns],
|
|
254
341
|
moduleSources,
|
|
@@ -257,8 +344,15 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
257
344
|
|
|
258
345
|
/** What the final compile of each chunk or module left alone, by environment. */
|
|
259
346
|
const skips = new Map<string, Skip[]>();
|
|
260
|
-
const addSkips = (
|
|
261
|
-
|
|
347
|
+
const addSkips = (
|
|
348
|
+
context: { environment?: { name: string } },
|
|
349
|
+
list?: readonly Skip[],
|
|
350
|
+
module?: string,
|
|
351
|
+
): void => {
|
|
352
|
+
// A package's own modules are compiled too, but what they leave alone
|
|
353
|
+
// is not something the app can change. A call of a package's component
|
|
354
|
+
// in the app is still reported, from the app's module.
|
|
355
|
+
if (!list || (module !== undefined && isPackageModule(module))) {
|
|
262
356
|
return;
|
|
263
357
|
}
|
|
264
358
|
const environment = context.environment?.name ?? 'client';
|
|
@@ -414,27 +508,35 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
414
508
|
transform: string,
|
|
415
509
|
): Promise<{ constants: ImportedConstants; partial: boolean }> {
|
|
416
510
|
let partial = false;
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
511
|
+
async function readImport(
|
|
512
|
+
specifier: string,
|
|
513
|
+
): Promise<readonly [string, Readonly<Record<string, Primitive>>] | undefined> {
|
|
514
|
+
const target = await resolveConstantModule(context, specifier, importer);
|
|
515
|
+
if (target === undefined || visiting.has(target.id)) {
|
|
516
|
+
return undefined;
|
|
517
|
+
}
|
|
518
|
+
const result = await readConstants(context, target, visiting, transform);
|
|
519
|
+
partial ||= result.partial;
|
|
520
|
+
if (!result.constants || Object.keys(result.constants.exports).length === 0) {
|
|
521
|
+
return undefined;
|
|
522
|
+
}
|
|
523
|
+
return [specifier, result.constants.exports] as const;
|
|
524
|
+
}
|
|
525
|
+
const reading = [];
|
|
526
|
+
for (const specifier of imports) {
|
|
527
|
+
reading.push(readImport(specifier));
|
|
528
|
+
}
|
|
529
|
+
const entries = await Promise.all(reading);
|
|
530
|
+
const constants: Record<string, Readonly<Record<string, Primitive>>> = {};
|
|
531
|
+
for (const entry of entries) {
|
|
532
|
+
if (entry !== undefined) {
|
|
533
|
+
constants[entry[0]] = entry[1];
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return { constants, partial };
|
|
435
537
|
}
|
|
436
538
|
|
|
437
|
-
async function
|
|
539
|
+
async function readConstants(
|
|
438
540
|
context: Rollup.PluginContext,
|
|
439
541
|
target: { id: string; jsx: boolean },
|
|
440
542
|
visiting: ReadonlySet<string>,
|
|
@@ -593,59 +695,104 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
593
695
|
*/
|
|
594
696
|
const recorded = new Map<string, string>();
|
|
595
697
|
|
|
596
|
-
/**
|
|
698
|
+
/**
|
|
699
|
+
* Whether components can be copied from a module: one of the app's, or
|
|
700
|
+
* one of a package listed in `optimizer.packages`.
|
|
701
|
+
*/
|
|
702
|
+
const isCopyable = (id: string): boolean => {
|
|
703
|
+
const file = stripQuery(id).replaceAll('\\', '/');
|
|
704
|
+
if (!file.includes('/node_modules/')) {
|
|
705
|
+
return true;
|
|
706
|
+
}
|
|
707
|
+
for (const name of optimizer.packages ?? []) {
|
|
708
|
+
if (file.includes(`/node_modules/${name}/`)) {
|
|
709
|
+
return true;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return false;
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
/** The modules each module imports, read from its file. */
|
|
597
716
|
const localImports = new Map<string, Promise<string[]>>();
|
|
598
717
|
|
|
599
718
|
/**
|
|
600
|
-
* The
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
719
|
+
* The code of a module as its file holds it. A module with no file, like
|
|
720
|
+
* a virtual module a plugin generates, is loaded instead, unless it has
|
|
721
|
+
* JSX: this plugin transforms such a module, so loading it could wait for
|
|
722
|
+
* the transform that asks. Its imports are then not followed.
|
|
604
723
|
*/
|
|
605
|
-
async function
|
|
724
|
+
async function readModuleCode(
|
|
725
|
+
context: Rollup.PluginContext,
|
|
726
|
+
id: string,
|
|
727
|
+
): Promise<string | undefined> {
|
|
728
|
+
try {
|
|
729
|
+
return await readFile(stripQuery(id), 'utf8');
|
|
730
|
+
} catch {
|
|
731
|
+
if (isJSXModule(id)) {
|
|
732
|
+
return undefined;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
try {
|
|
736
|
+
return (await context.load({ id })).code ?? undefined;
|
|
737
|
+
} catch {
|
|
738
|
+
return undefined;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* The modules a module imports at runtime that a transform can wait for:
|
|
744
|
+
* the app's, those of packages listed in `optimizer.packages`, and virtual
|
|
745
|
+
* modules. A transform only waits for a module it copies from, and it only
|
|
746
|
+
* copies from those, so a module of another package is never in a loop of
|
|
747
|
+
* transforms that wait for each other. A virtual module can re-export the
|
|
748
|
+
* app, so a listed package can import the app back through one, like a
|
|
749
|
+
* docs framework that imports its theme's layout.
|
|
750
|
+
*
|
|
751
|
+
* Imports only used as types are left out, since TypeScript drops them,
|
|
752
|
+
* and so are Solid's own modules and Node's built-ins. The imports come
|
|
753
|
+
* from the file on disk, so they do not depend on the order modules are
|
|
754
|
+
* transformed in. Modules without JSX count too, since reading their
|
|
755
|
+
* constants can load a module with JSX.
|
|
756
|
+
*/
|
|
757
|
+
async function findLocalImports(context: Rollup.PluginContext, id: string): Promise<string[]> {
|
|
606
758
|
let pending = localImports.get(id);
|
|
607
759
|
if (!pending) {
|
|
608
760
|
pending = (async () => {
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
code = await readFile(stripQuery(id), 'utf8');
|
|
612
|
-
} catch {
|
|
761
|
+
const code = await readModuleCode(context, id);
|
|
762
|
+
if (code === undefined) {
|
|
613
763
|
return [];
|
|
614
764
|
}
|
|
615
765
|
let program: Program;
|
|
616
766
|
try {
|
|
617
|
-
|
|
767
|
+
// A loaded module is JavaScript, whatever its id says.
|
|
768
|
+
program = parse(
|
|
769
|
+
/\.[cm]?[jt]sx?$/.test(stripQuery(id)) ? stripQuery(id) : `${id}.js`,
|
|
770
|
+
code,
|
|
771
|
+
);
|
|
618
772
|
} catch {
|
|
619
773
|
return [];
|
|
620
774
|
}
|
|
621
|
-
const
|
|
622
|
-
for (const
|
|
623
|
-
if (
|
|
624
|
-
(
|
|
625
|
-
((statement.type === 'ExportNamedDeclaration' ||
|
|
626
|
-
statement.type === 'ExportAllDeclaration') &&
|
|
627
|
-
statement.source &&
|
|
628
|
-
statement.exportKind !== 'type')
|
|
629
|
-
) {
|
|
630
|
-
const value = statement.source?.value;
|
|
631
|
-
if (value !== undefined && !moduleSources.includes(value)) {
|
|
632
|
-
sources.add(value);
|
|
633
|
-
}
|
|
775
|
+
const resolving = [];
|
|
776
|
+
for (const source of runtimeImportSources(program)) {
|
|
777
|
+
if (!moduleSources.includes(source) && !source.startsWith('node:')) {
|
|
778
|
+
resolving.push(context.resolve(source, id));
|
|
634
779
|
}
|
|
635
780
|
}
|
|
636
|
-
const resolved = await Promise.all(
|
|
637
|
-
|
|
638
|
-
)
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
781
|
+
const resolved = await Promise.all(resolving);
|
|
782
|
+
const local: string[] = [];
|
|
783
|
+
for (const target of resolved) {
|
|
784
|
+
if (target && !target.external && isCopyable(target.id)) {
|
|
785
|
+
local.push(target.id);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
return local;
|
|
642
789
|
})();
|
|
643
790
|
localImports.set(id, pending);
|
|
644
791
|
}
|
|
645
792
|
return pending;
|
|
646
793
|
}
|
|
647
794
|
|
|
648
|
-
/** Whether `from` imports `to`, directly or through other modules
|
|
795
|
+
/** Whether `from` imports `to`, directly or through other modules. */
|
|
649
796
|
async function reaches(
|
|
650
797
|
context: Rollup.PluginContext,
|
|
651
798
|
from: string,
|
|
@@ -660,9 +807,18 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
660
807
|
for (const id of frontier) {
|
|
661
808
|
seen.add(id);
|
|
662
809
|
}
|
|
810
|
+
const reading = [];
|
|
811
|
+
for (const id of frontier) {
|
|
812
|
+
reading.push(findLocalImports(context, id));
|
|
813
|
+
}
|
|
663
814
|
// oxlint-disable-next-line no-await-in-loop
|
|
664
|
-
const next = await Promise.all(
|
|
665
|
-
frontier = [
|
|
815
|
+
const next = await Promise.all(reading);
|
|
816
|
+
frontier = [];
|
|
817
|
+
for (const id of new Set(next.flat())) {
|
|
818
|
+
if (!seen.has(id)) {
|
|
819
|
+
frontier.push(id);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
666
822
|
}
|
|
667
823
|
return false;
|
|
668
824
|
}
|
|
@@ -693,6 +849,27 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
693
849
|
return stripped.code;
|
|
694
850
|
}
|
|
695
851
|
|
|
852
|
+
/**
|
|
853
|
+
* The scripts a virtual entry runs, like the server entry `start` mode
|
|
854
|
+
* generates: the app's modules it imports. It has no file to read, and
|
|
855
|
+
* the server and client builds have to see the same uses of each export.
|
|
856
|
+
* `undefined` when the entry has not been loaded or imports none.
|
|
857
|
+
*/
|
|
858
|
+
function virtualEntryScripts(context: Rollup.PluginContext, id: string): string[] | undefined {
|
|
859
|
+
const info = context.getModuleInfo(id);
|
|
860
|
+
if (!info || info.code === null) {
|
|
861
|
+
return undefined;
|
|
862
|
+
}
|
|
863
|
+
const scripts: string[] = [];
|
|
864
|
+
for (const imported of [...info.importedIds, ...info.dynamicallyImportedIds]) {
|
|
865
|
+
// Packages and other virtual modules hold none of the app's components.
|
|
866
|
+
if (!imported.startsWith('\0') && !imported.includes('/node_modules/')) {
|
|
867
|
+
scripts.push(imported);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
return scripts.length > 0 ? scripts : undefined;
|
|
871
|
+
}
|
|
872
|
+
|
|
696
873
|
/**
|
|
697
874
|
* The scripts an entry runs: the entry itself, or the module scripts of an
|
|
698
875
|
* HTML entry. `undefined` when an HTML entry has an inline module script,
|
|
@@ -709,7 +886,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
709
886
|
try {
|
|
710
887
|
html = await readFile(stripQuery(id), 'utf8');
|
|
711
888
|
} catch {
|
|
712
|
-
return
|
|
889
|
+
return virtualEntryScripts(context, id);
|
|
713
890
|
}
|
|
714
891
|
const scripts: string[] = [];
|
|
715
892
|
for (const [tag] of html.matchAll(/<script\b[^>]*>/gi)) {
|
|
@@ -741,14 +918,24 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
741
918
|
let pending = usageIndexes.get(key);
|
|
742
919
|
if (!pending) {
|
|
743
920
|
pending = (async () => {
|
|
744
|
-
const
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
.
|
|
748
|
-
|
|
921
|
+
const reading = [];
|
|
922
|
+
for (const id of context.getModuleIds()) {
|
|
923
|
+
if (context.getModuleInfo(id)?.isEntry === true) {
|
|
924
|
+
reading.push(scriptEntries(context, id));
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
const entries = await Promise.all(reading);
|
|
749
928
|
// A script the index cannot read could use any export.
|
|
750
|
-
const
|
|
751
|
-
|
|
929
|
+
const scriptIds: string[] = [];
|
|
930
|
+
let known = true;
|
|
931
|
+
for (const scripts of entries) {
|
|
932
|
+
if (scripts === undefined) {
|
|
933
|
+
known = false;
|
|
934
|
+
break;
|
|
935
|
+
}
|
|
936
|
+
scriptIds.push(...scripts);
|
|
937
|
+
}
|
|
938
|
+
return buildUsageIndex(known ? scriptIds : [], {
|
|
752
939
|
resolve: async (source, importer) => {
|
|
753
940
|
const resolved = await context.resolve(source, importer);
|
|
754
941
|
return !resolved || resolved.external || resolved.id.includes('/node_modules/')
|
|
@@ -786,6 +973,8 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
786
973
|
interface Copy {
|
|
787
974
|
/** The module and export the copy comes from. */
|
|
788
975
|
readonly key: string;
|
|
976
|
+
/** Whether it comes from a package in `optimizer.packages`. */
|
|
977
|
+
readonly fromPackage: boolean;
|
|
789
978
|
readonly name: string;
|
|
790
979
|
/** The tags in the importer that call it. */
|
|
791
980
|
readonly tags: Node[];
|
|
@@ -805,6 +994,32 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
805
994
|
readonly skips: Skip[] | undefined;
|
|
806
995
|
}
|
|
807
996
|
|
|
997
|
+
/** The listed packages already warned about, by environment. */
|
|
998
|
+
const warnedPackages = new Set<string>();
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* Warns once when a listed package is external, like a server build that
|
|
1002
|
+
* leaves it to Node. Its components are not copied there, so a build that
|
|
1003
|
+
* copies them would hydrate different markup.
|
|
1004
|
+
*/
|
|
1005
|
+
const warnExternalPackage = (context: Rollup.PluginContext, source: string): void => {
|
|
1006
|
+
let name: string | undefined;
|
|
1007
|
+
for (const item of optimizer.packages ?? []) {
|
|
1008
|
+
if (source === item || source.startsWith(`${item}/`)) {
|
|
1009
|
+
name = item;
|
|
1010
|
+
break;
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
const key = environmentKey(context, name ?? '');
|
|
1014
|
+
if (name === undefined || warnedPackages.has(key)) {
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
warnedPackages.add(key);
|
|
1018
|
+
context.warn(
|
|
1019
|
+
`[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.`,
|
|
1020
|
+
);
|
|
1021
|
+
};
|
|
1022
|
+
|
|
808
1023
|
/**
|
|
809
1024
|
* Reads the component `source` exports as `imported`, and imports what it
|
|
810
1025
|
* needs into the importer. `undefined` when it cannot be copied.
|
|
@@ -813,21 +1028,43 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
813
1028
|
context: Rollup.PluginContext,
|
|
814
1029
|
id: string,
|
|
815
1030
|
source: string,
|
|
816
|
-
|
|
1031
|
+
requested: string,
|
|
817
1032
|
state: CopyState,
|
|
818
1033
|
): Promise<{ key: string; text: (name: string) => string } | undefined> {
|
|
819
1034
|
const skip = (reason: string): void => {
|
|
820
|
-
state.skips?.push({ pass: 'inline', reason, name: `${
|
|
1035
|
+
state.skips?.push({ pass: 'inline', reason, name: `${requested} (${source})` });
|
|
821
1036
|
};
|
|
822
|
-
|
|
1037
|
+
// The export's name in the module that declares it, past any re-exports.
|
|
1038
|
+
let imported = requested;
|
|
1039
|
+
let resolved = await context.resolve(source, id);
|
|
823
1040
|
if (!resolved || resolved.external) {
|
|
1041
|
+
warnExternalPackage(context, source);
|
|
824
1042
|
skip('copy-external');
|
|
825
1043
|
return undefined;
|
|
826
1044
|
}
|
|
827
|
-
if (resolved.id
|
|
1045
|
+
if (!isCopyable(resolved.id)) {
|
|
828
1046
|
skip('copy-package');
|
|
829
1047
|
return undefined;
|
|
830
1048
|
}
|
|
1049
|
+
// A module that re-exports the component by name, like a library's
|
|
1050
|
+
// entry, leads to the module that declares it.
|
|
1051
|
+
for (let depth = 0; depth < 8 && isJSXModule(resolved.id); depth += 1) {
|
|
1052
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
1053
|
+
const code = await state.load(resolved.id);
|
|
1054
|
+
const reexport =
|
|
1055
|
+
code === undefined ? undefined : readReexport(code, stripQuery(resolved.id), imported);
|
|
1056
|
+
if (!reexport) {
|
|
1057
|
+
break;
|
|
1058
|
+
}
|
|
1059
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
1060
|
+
const next = await context.resolve(reexport.source, resolved.id);
|
|
1061
|
+
if (!next || next.external || !isCopyable(next.id)) {
|
|
1062
|
+
skip('copy-package');
|
|
1063
|
+
return undefined;
|
|
1064
|
+
}
|
|
1065
|
+
resolved = next;
|
|
1066
|
+
imported = reexport.imported;
|
|
1067
|
+
}
|
|
831
1068
|
if (resolved.id === id) {
|
|
832
1069
|
return undefined;
|
|
833
1070
|
}
|
|
@@ -875,7 +1112,20 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
875
1112
|
skip('copy-external');
|
|
876
1113
|
return undefined;
|
|
877
1114
|
}
|
|
878
|
-
|
|
1115
|
+
if (!target.id.startsWith('\0')) {
|
|
1116
|
+
targets.set(dependency.source, target.external ? dependency.source : target.id);
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
// A virtual module's id cannot be imported again, so the copy keeps
|
|
1120
|
+
// the specifier, when it means the same module from the importer.
|
|
1121
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
1122
|
+
const fromImporter = await context.resolve(dependency.source, id);
|
|
1123
|
+
if (fromImporter?.id !== target.id) {
|
|
1124
|
+
state.excluded.add(key);
|
|
1125
|
+
skip('copy-virtual');
|
|
1126
|
+
return undefined;
|
|
1127
|
+
}
|
|
1128
|
+
targets.set(dependency.source, dependency.source);
|
|
879
1129
|
}
|
|
880
1130
|
}
|
|
881
1131
|
const rename = new Map<string, string>();
|
|
@@ -917,7 +1167,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
917
1167
|
// The components copied here that leave their module, by module.
|
|
918
1168
|
const absorbed = new Map<string, Set<string>>();
|
|
919
1169
|
// How many modules use the original of a copy, this one included.
|
|
920
|
-
const
|
|
1170
|
+
const countUsers = (copy: Copy): number => {
|
|
921
1171
|
const [module = '', exported = ''] = copy.key.split('\0');
|
|
922
1172
|
return usage.users(module, exported, id, absorbed);
|
|
923
1173
|
};
|
|
@@ -970,13 +1220,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
970
1220
|
for (const specifier of statement.specifiers) {
|
|
971
1221
|
const binding = scopes.root.bindings.get(specifier.local.name);
|
|
972
1222
|
// Only tags are copied. The import stays for any other use.
|
|
973
|
-
const tags = (binding?.references ?? [])
|
|
974
|
-
const parent = parents.get(reference);
|
|
975
|
-
return (
|
|
976
|
-
(parent?.type === 'JSXOpeningElement' || parent?.type === 'JSXClosingElement') &&
|
|
977
|
-
parent.name === reference
|
|
978
|
-
);
|
|
979
|
-
});
|
|
1223
|
+
const tags = findTags(binding?.references ?? [], parents);
|
|
980
1224
|
if (
|
|
981
1225
|
specifier.type === 'ImportNamespaceSpecifier' ||
|
|
982
1226
|
binding?.imported === undefined ||
|
|
@@ -998,7 +1242,8 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
998
1242
|
}
|
|
999
1243
|
if (copy) {
|
|
1000
1244
|
const name = fresh(specifier.local.name);
|
|
1001
|
-
|
|
1245
|
+
const fromPackage = isPackageModule(copy.key);
|
|
1246
|
+
copies.push({ key: copy.key, fromPackage, name, tags, text: copy.text(name) });
|
|
1002
1247
|
}
|
|
1003
1248
|
}
|
|
1004
1249
|
}
|
|
@@ -1018,29 +1263,48 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1018
1263
|
edit.append(`\n${copy.text}\n`);
|
|
1019
1264
|
}
|
|
1020
1265
|
edit.append(`\n${imports.join('\n')}\n`);
|
|
1266
|
+
const importedContexts: Record<string, string[]> = {};
|
|
1267
|
+
for (const [module, exported] of contexts) {
|
|
1268
|
+
importedContexts[module] = [...exported];
|
|
1269
|
+
}
|
|
1270
|
+
const sharedComponents: Record<string, number> = {};
|
|
1271
|
+
const alwaysInlineComponents: string[] = [];
|
|
1272
|
+
for (const copy of copies) {
|
|
1273
|
+
const users = countUsers(copy);
|
|
1274
|
+
if (users !== 1) {
|
|
1275
|
+
sharedComponents[copy.name] = users;
|
|
1276
|
+
}
|
|
1277
|
+
if (copy.fromPackage) {
|
|
1278
|
+
alwaysInlineComponents.push(copy.name);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1021
1281
|
result = compile(edit.toString(), {
|
|
1022
1282
|
...compileOptionsOfModule,
|
|
1023
1283
|
filename,
|
|
1024
1284
|
sourceMap: wantsMaps(),
|
|
1025
|
-
importedContexts
|
|
1026
|
-
[...contexts].map(([module, exported]) => [module, [...exported]]),
|
|
1027
|
-
),
|
|
1285
|
+
importedContexts,
|
|
1028
1286
|
// The original of a copy stays in its module for its other users.
|
|
1029
|
-
sharedComponents
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1287
|
+
sharedComponents,
|
|
1288
|
+
// The estimate cannot see what a package's own modules use, so a
|
|
1289
|
+
// copy of its component never counts its declaration, or the
|
|
1290
|
+
// runtime code only it used, as removed. Listing the package asks
|
|
1291
|
+
// for its components to inline wherever they can be.
|
|
1292
|
+
alwaysInlineComponents,
|
|
1034
1293
|
});
|
|
1035
1294
|
const left = analyzeScopes(parse(filename, result.code)).root.bindings;
|
|
1036
|
-
const stuck =
|
|
1295
|
+
const stuck = [];
|
|
1296
|
+
for (const copy of copies) {
|
|
1297
|
+
if (left.has(copy.name)) {
|
|
1298
|
+
stuck.push(copy);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1037
1301
|
if (stuck.length === 0) {
|
|
1038
1302
|
break;
|
|
1039
1303
|
}
|
|
1040
1304
|
for (const copy of stuck) {
|
|
1041
1305
|
excluded.add(copy.key);
|
|
1042
1306
|
copies.splice(copies.indexOf(copy), 1);
|
|
1043
|
-
const reason = result.skipped
|
|
1307
|
+
const reason = findSkipReason(result.skipped ?? [], copy.name);
|
|
1044
1308
|
copySkips?.push({
|
|
1045
1309
|
pass: 'inline',
|
|
1046
1310
|
reason: `copy-${reason ?? 'left'}`,
|
|
@@ -1052,8 +1316,15 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1052
1316
|
if (!result || !edit) {
|
|
1053
1317
|
break;
|
|
1054
1318
|
}
|
|
1055
|
-
// Their uses of other components move here with them.
|
|
1056
|
-
|
|
1319
|
+
// Their uses of other components move here with them. Count every copy's
|
|
1320
|
+
// users first, since moving one changes what the next one counts.
|
|
1321
|
+
const sole: Copy[] = [];
|
|
1322
|
+
for (const copy of copies) {
|
|
1323
|
+
if (countUsers(copy) === 1) {
|
|
1324
|
+
sole.push(copy);
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
for (const copy of sole) {
|
|
1057
1328
|
const [module = '', exported = ''] = copy.key.split('\0');
|
|
1058
1329
|
absorbed.set(module, new Set([...(absorbed.get(module) ?? []), exported]));
|
|
1059
1330
|
}
|
|
@@ -1117,7 +1388,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1117
1388
|
copySkips,
|
|
1118
1389
|
);
|
|
1119
1390
|
// The rest of the report comes from the chunk this module ends up in.
|
|
1120
|
-
addSkips(context, copySkips);
|
|
1391
|
+
addSkips(context, copySkips, id);
|
|
1121
1392
|
if (imported) {
|
|
1122
1393
|
code = imported.code;
|
|
1123
1394
|
maps.push(...imported.maps);
|
|
@@ -1192,7 +1463,13 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1192
1463
|
return { code, map: combineMaps(wantsMaps(), maps) };
|
|
1193
1464
|
}
|
|
1194
1465
|
|
|
1195
|
-
|
|
1466
|
+
let main: Plugin | undefined;
|
|
1467
|
+
for (const plugin of plugins) {
|
|
1468
|
+
if (plugin.name === 'solid') {
|
|
1469
|
+
main = plugin;
|
|
1470
|
+
break;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1196
1473
|
const originalTransform = main?.transform;
|
|
1197
1474
|
const originalHandler =
|
|
1198
1475
|
typeof originalTransform === 'function' ? originalTransform : originalTransform?.handler;
|
|
@@ -1248,7 +1525,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1248
1525
|
async (target) => loadRecorded(this, id, target),
|
|
1249
1526
|
copySkips,
|
|
1250
1527
|
);
|
|
1251
|
-
addSkips(this, copySkips);
|
|
1528
|
+
addSkips(this, copySkips, id);
|
|
1252
1529
|
if (imported) {
|
|
1253
1530
|
code = imported.code;
|
|
1254
1531
|
maps.push(...imported.maps);
|
|
@@ -1262,7 +1539,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1262
1539
|
maps.push(exposed.map);
|
|
1263
1540
|
}
|
|
1264
1541
|
}
|
|
1265
|
-
addSkips(this, skipped);
|
|
1542
|
+
addSkips(this, skipped, id);
|
|
1266
1543
|
recorded.set(environmentKey(this, id), code);
|
|
1267
1544
|
if (code === source) {
|
|
1268
1545
|
return null;
|
|
@@ -1315,29 +1592,54 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1315
1592
|
filename: `${filename}.jsx`,
|
|
1316
1593
|
sourceMap: wantsMaps(),
|
|
1317
1594
|
});
|
|
1318
|
-
|
|
1595
|
+
const chunkOptions: CompileOptions = {
|
|
1319
1596
|
...compileOptions,
|
|
1320
1597
|
filename,
|
|
1321
1598
|
sourceMap: wantsMaps(),
|
|
1322
1599
|
constantVars: true,
|
|
1323
1600
|
builtInAliases: runtime.builtInAliases,
|
|
1324
|
-
}
|
|
1601
|
+
};
|
|
1602
|
+
// Each module keeps the helpers its own JSX needs, and a merged tree
|
|
1603
|
+
// can need one none of them did. Such a candidate is left out.
|
|
1604
|
+
let missing: MissingHelperError | undefined;
|
|
1605
|
+
const lowerCandidate = (optimized: CompileResult): ChunkCandidate | undefined => {
|
|
1606
|
+
const lowered = lowerChunk(optimized.code);
|
|
1607
|
+
try {
|
|
1608
|
+
const linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1609
|
+
return { optimized, lowered, linked };
|
|
1610
|
+
} catch (error) {
|
|
1611
|
+
if (!(error instanceof MissingHelperError)) {
|
|
1612
|
+
throw error;
|
|
1613
|
+
}
|
|
1614
|
+
missing ??= error;
|
|
1615
|
+
return undefined;
|
|
1616
|
+
}
|
|
1617
|
+
};
|
|
1618
|
+
let chosen = lowerCandidate(compile(chunkCode, chunkOptions));
|
|
1619
|
+
// The estimate weighs each component on its own. It cannot see the
|
|
1620
|
+
// runtime code that only leaves the chunk once every call that needs
|
|
1621
|
+
// it is inlined, like `<Dynamic>`, `merge()`, and `omit()` behind a
|
|
1622
|
+
// headless library's components. So the chunk is also built with
|
|
1623
|
+
// every component inlined that can be, and the smaller one is kept,
|
|
1624
|
+
// as minified and gzipped.
|
|
1625
|
+
if (compileOptions.alwaysInline !== true) {
|
|
1626
|
+
const always = compile(chunkCode, { ...chunkOptions, alwaysInline: true });
|
|
1627
|
+
const other = always.code === chosen?.optimized.code ? undefined : lowerCandidate(always);
|
|
1628
|
+
if (other && (!chosen || shippedSize(other, filename) < shippedSize(chosen, filename))) {
|
|
1629
|
+
chosen = other;
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
let optimized: CompileResult | undefined;
|
|
1325
1633
|
let linked: { code: string; map: string };
|
|
1326
1634
|
let lowered: SolidCompiler.TransformResult;
|
|
1327
|
-
|
|
1328
|
-
lowered =
|
|
1329
|
-
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1635
|
+
if (chosen) {
|
|
1636
|
+
({ optimized, lowered, linked } = chosen);
|
|
1330
1637
|
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;
|
|
1638
|
+
} else {
|
|
1639
|
+
// Without the helper, the chunk is lowered as its modules were
|
|
1640
|
+
// written. Chunk mode never hydrates, so the chunks do not have to
|
|
1641
|
+
// match anything else.
|
|
1642
|
+
this.warn(`${missing?.message ?? ''} ${filename} is not optimized.`);
|
|
1341
1643
|
addSkips(this, [{ pass: 'inline', reason: 'missing-helper', name: filename }]);
|
|
1342
1644
|
lowered = lowerChunk(chunkCode);
|
|
1343
1645
|
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|