solid-optimizer 1.0.0-next.0 → 1.0.0-next.2
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 +15 -8
- package/dist/{compile-BRCj4UN7.d.mts → compile-CwH9mvHD.d.mts} +7 -1
- package/dist/compile-CwH9mvHD.d.mts.map +1 -0
- package/dist/{compile-Cz6hbiQv.mjs → compile-sCZdwEKG.mjs} +304 -45
- 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 +151 -27
- package/dist/vite.mjs.map +1 -1
- package/package.json +4 -4
- package/src/compile.ts +10 -2
- package/src/constants.ts +27 -8
- 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 +174 -32
- package/src/vite/inject.ts +64 -1
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-Cz6hbiQv.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -45,6 +45,7 @@ The input and the output both keep their JSX. Run the JSX transform after `compi
|
|
|
45
45
|
- `fold` turns constant folding and control-flow resolution on or off. Defaults to `true`.
|
|
46
46
|
- `inline` turns component inlining on or off. Defaults to `true`.
|
|
47
47
|
- `alwaysInline` inlines every component that can be, even where the copies are larger than the calls they replace. Defaults to `false`. See [Only where it is smaller](#only-where-it-is-smaller).
|
|
48
|
+
- `alwaysInlineComponents` lists top-level components to inline wherever they can be, like `alwaysInline` does for every component. The Vite plugin lists the copies from `optimizer.packages` here.
|
|
48
49
|
- `sharedComponents` maps top-level components that other modules use too to the number of modules that use them, this one included. Inlining one only counts its share of the declaration. `0` keeps the declaration whatever is inlined.
|
|
49
50
|
- `contexts` turns context provider removal on or off. Defaults to `true`.
|
|
50
51
|
- `memos` turns memo inlining on or off. Defaults to `true`.
|
|
@@ -95,7 +96,7 @@ export default defineConfig({
|
|
|
95
96
|
|
|
96
97
|
The plugin optimizes in one of two modes.
|
|
97
98
|
|
|
98
|
-
- **Chunk mode** is for client builds that do not hydrate. Constants fold and memos inline in each module first, so a branch that folds away takes its imports and `lazy()` chunks out of the bundle. Components imported from other modules are inlined there too, so the bundler drops them and the runtime code only they used. JSX is then kept through bundling. Each chunk is optimized as a whole and then lowered by Solid's JSX transform, so a component inlines anywhere in its chunk.
|
|
99
|
+
- **Chunk mode** is for client builds that do not hydrate. Constants fold and memos inline in each module first, so a branch that folds away takes its imports and `lazy()` chunks out of the bundle. Components imported from other modules are inlined there too, so the bundler drops them and the runtime code only they used. JSX is then kept through bundling. Each chunk is optimized as a whole and then lowered by Solid's JSX transform, so a component inlines anywhere in its chunk. Each chunk is also built with every component inlined that can be, and the smaller of the two, minified and gzipped, is kept. See [Only where it is smaller](#only-where-it-is-smaller).
|
|
99
100
|
- **Module mode** is for everything else. Each module is optimized before the official plugin lowers it, with the components it imports from other modules copied in first. A server build and its client build split chunks differently, and hydration needs both to render the same tree, so every decision depends only on the modules. Two modules that import each other do not copy each other's components.
|
|
100
101
|
|
|
101
102
|
Both modes fold constants imported from other modules in the project.
|
|
@@ -116,6 +117,7 @@ The plugin takes every option of `@solidjs/vite-plugin`, plus `optimizer`.
|
|
|
116
117
|
- `optimizer.mode` is `'auto'` by default. Set it to `'module'` to never keep JSX through bundling.
|
|
117
118
|
- `optimizer.dev` also optimizes while serving. Defaults to `false`.
|
|
118
119
|
- `optimizer.report` reports what stays as written in each build, and why. `true` prints it. A function receives it instead, once per build environment. Defaults to `false`.
|
|
120
|
+
- `optimizer.packages` lists packages whose components are copied into the modules that use them, like `['@kobalte/core']`. A package has to publish its JSX, as most Solid libraries do through the `solid` export condition. Re-exports and aliases like `const Button = ButtonRoot` are followed. Their copies inline wherever they can be, whatever the estimate says, since it cannot see what the package's other modules and the runtime code only they use add up to. The bundler then drops the originals, even when the package lands in another chunk. A listed package has to be bundled in every build, or the server and client builds copy different components. Defaults to `[]`.
|
|
119
121
|
|
|
120
122
|
### Report
|
|
121
123
|
|
|
@@ -128,15 +130,16 @@ solid-optimizer: what stays as written in client
|
|
|
128
130
|
inline no-host 1 Home
|
|
129
131
|
```
|
|
130
132
|
|
|
131
|
-
Each line is a pass, a reason, how many times it happened, and the names it happened to. The reasons for the inline pass:
|
|
133
|
+
Each line is a pass, a reason, how many times it happened, and the names it happened to. A package's own modules are compiled too, but what they leave alone is not reported, since the app cannot change it. The app's calls of a package's components are. In chunk mode, a chunk is reported as a whole, so it also lists the package code bundled into it. The reasons for the inline pass:
|
|
132
134
|
|
|
133
135
|
- `props-use` and `view-use` mean the component uses its props in a way other than `props.name`, a spread onto an element, or a `merge()` or `omit()` view, such as passing them to a function.
|
|
134
136
|
- `component-params`, `component-body`, `component-return`, `component-async`, `component-recursive`, and `component-this` mean the component's own shape is not supported.
|
|
135
|
-
- `
|
|
137
|
+
- `component-write` means the component's statements assign to something declared outside it, like a counter at the top of the module. A copy's statements run before the JSX around the call, so that JSX would see the write too early.
|
|
138
|
+
- `call-spread` and `call-namespace` mean the call site passes a spread or a namespaced attribute. `call-ref` means its `ref` is an expression other than a name, a function, or an array of them, and `ref-use` means the component uses `props.ref` other than as a `ref`.
|
|
136
139
|
- `no-host` means the component has statements, and the call is not in JSX that a component or one of Solid's callbacks returns. A `<Show>` child or a `fallback` is one such place.
|
|
137
140
|
- `size` means the copies would be larger than the calls.
|
|
138
141
|
- `lazy`, `imported`, `nested`, and `not-a-function` mean the tag is not a component declared at the top of the same module or chunk.
|
|
139
|
-
- A reason that starts with `copy-` is about copying a component from another module. `copy-package` means it comes from `node_modules
|
|
142
|
+
- A reason that starts with `copy-` is about copying a component from another module. `copy-package` means it comes from a package in `node_modules` that `optimizer.packages` does not list, `copy-cycle` means the two modules import each other, `copy-wrapper` means the component only returns a call of another component in its module, and `copy-size` means the copy was larger.
|
|
140
143
|
|
|
141
144
|
The provider pass reports `open-component`, `open-call`, `open-new`, `open-spread`, and `open-outer` for code under the provider that it cannot see into, and `provider-value`, `provider-undefined`, and `provider-shape` for a value or provider it cannot handle. The memo pass reports `memo-reads`, `memo-read-place`, `memo-constant`, and a few more for a memo it keeps.
|
|
142
145
|
|
|
@@ -145,7 +148,7 @@ The provider pass reports `open-component`, `open-call`, `open-new`, `open-sprea
|
|
|
145
148
|
- Chunk mode is skipped when the `babel` option is set or `compiler` is `'babel'`, since those passes need each module.
|
|
146
149
|
- An export named `__so_local$name` is added to a module for each local binding its exported components need, so a copy in another module can import it. In chunk mode, entry modules get none.
|
|
147
150
|
- A component used in several chunks is copied into each of them, where each copy is no larger than its call.
|
|
148
|
-
- Components from dependencies in `node_modules` are
|
|
151
|
+
- Components from dependencies in `node_modules` are only copied from the packages `optimizer.packages` lists.
|
|
149
152
|
- `.tsrx` modules are lowered by the official plugin and are not optimized.
|
|
150
153
|
|
|
151
154
|
## Features
|
|
@@ -191,16 +194,16 @@ Solid then creates one template for `<main>` instead of two.
|
|
|
191
194
|
- the child callback of `<Show>` or `<Match>`, when it takes the value;
|
|
192
195
|
- the callback given to `render`.
|
|
193
196
|
- Bindings declared in the component get fresh names in every copy.
|
|
194
|
-
- A component nothing else uses is removed.
|
|
197
|
+
- A component nothing else uses is removed, with any alias of it that nothing uses.
|
|
195
198
|
|
|
196
199
|
A component inlines when all of these hold:
|
|
197
200
|
|
|
198
|
-
- It is declared once at the top level, with a capitalized name.
|
|
201
|
+
- It is declared once at the top level, with a capitalized name. A call through an alias, like `const Separator = SeparatorRoot`, is a call of the component it names.
|
|
199
202
|
- Its parameter is one identifier that is only read as `props.name`, spread onto an element as `{...props}`, or passed to `merge()` and `omit()`. See below.
|
|
200
203
|
- Its body ends in the only `return`, and that `return` returns JSX.
|
|
201
204
|
- It does not use `this`, `arguments`, `super`, or `new.target`.
|
|
202
205
|
- The call site has no spread attribute and no namespaced attribute.
|
|
203
|
-
- A `ref` at the call site moves to the element the component gives `props.ref` to, when its value is a name, a function, or an array of them, and the component only uses `props.ref` as the `ref` of an element, of another component, or in a spread. Both run while the element is made, untracked, so the element gets the same ref.
|
|
206
|
+
- A `ref` at the call site moves to the element the component gives `props.ref` to, when its value is a name, a function, or an array of them, and the component only uses `props.ref` as the `ref` of an element, of another component, or in a spread. Both run while the element is made, untracked, so the element gets the same ref. In an array, `void 0` and `untrack(() => ...)` around a name or a function count too: Solid skips an empty entry, and runs the whole `ref` untracked.
|
|
204
207
|
|
|
205
208
|
The props can go through `merge()` and `omit()` first, declared as a `const` in the component:
|
|
206
209
|
|
|
@@ -236,9 +239,12 @@ Every copy repeats the component's own code, so inlining a component at many cal
|
|
|
236
239
|
- A copy costs the component's statements, its dynamic attributes and children, and the elements that cannot join a template. Constant props are folded first, so a branch they rule out costs nothing.
|
|
237
240
|
- When every call inlines and nothing else uses the component, its declaration goes away too.
|
|
238
241
|
- A call in its children that can only inline once it does, because that component has statements to hoist, counts too.
|
|
242
|
+
- A call in its body that can only inline in a copy counts too, such as a call that spreads a `merge()` or `omit()` view, which each call site resolves. The copy is measured with that call inlined, and the called component's declaration goes away when nothing else uses it.
|
|
239
243
|
|
|
240
244
|
A component inlines only when its copies are no larger. A small component, or one used once, inlines at every call. A wrapper used many times, whose root is another component, like most wrappers of a component library, stays a component.
|
|
241
245
|
|
|
246
|
+
The estimate weighs each component on its own, so it cannot see runtime code that only goes once every call that needs it is inlined, like `<Dynamic>`, `merge()`, and `omit()` behind a headless library's components. In chunk mode, the Vite plugin also builds each chunk with every component inlined that can be, as `alwaysInline` does, minifies and gzips both, and keeps the smaller one. The runtime only shrinks when it is in the same chunk as the calls.
|
|
247
|
+
|
|
242
248
|
In the Vite plugin, a component copied from another module counts the module's share of its declaration: all of it when the importer is its only user, and one part in n when n modules use it, since it goes away once every one of them copies it. The plugin reads which modules use each export from the files the entries reach, and from the module scripts of an HTML entry. A re-export, a dynamic import, a use as a value, or a user the component's module imports back keeps the declaration whole.
|
|
243
249
|
|
|
244
250
|
Set `alwaysInline` to inline every component that can be.
|
|
@@ -340,6 +346,7 @@ This follows the `optimize` option of `@solidjs/compiler` ([solidjs/solid#3231](
|
|
|
340
346
|
- `<Repeat count>` becomes its `fallback` when `count` is below one.
|
|
341
347
|
- `<Switch>` drops each `<Match>` that is always false, and becomes the first `<Match>` that is always true.
|
|
342
348
|
- `<Dynamic component="div">` becomes `<div>`. `<Dynamic component={Card}>` becomes `<Card>` when `Card` is a capitalized component declared at the top level, as a function or with `lazy`, that nothing reassigns. An imported component stays, since an import could hold a tag name.
|
|
349
|
+
- `untrack(() => value)` becomes the value when the value is a constant. A component inlined with a literal prop can leave one, like `untrack(() => props.as)`.
|
|
343
350
|
- A call of an arrow function whose body is JSX, such as a render prop left by inlining, becomes the JSX when it is an element's child and each argument is a constant or a binding nothing reassigns. `{((n) => <li>{n}</li>)(1)}` becomes `<li>{1}</li>`. A `ref` or a spread in the JSX keeps the call, since both run while the JSX is made.
|
|
344
351
|
|
|
345
352
|
A built-in only folds when the tag resolves to Solid's component. The tag must bind to nothing, or be imported from one of `moduleSources`. A tag with a spread attribute or a function child never folds. `<Portal>`, `<Loading>`, `<Errored>`, and `<Reveal>` never fold.
|
|
@@ -123,6 +123,12 @@ interface CompileOptions {
|
|
|
123
123
|
* @default false
|
|
124
124
|
*/
|
|
125
125
|
alwaysInline?: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* Top-level components to inline wherever they can be, like
|
|
128
|
+
* `alwaysInline` does for every component. The Vite plugin lists the
|
|
129
|
+
* copies of components from `optimizer.packages` here.
|
|
130
|
+
*/
|
|
131
|
+
alwaysInlineComponents?: readonly string[];
|
|
126
132
|
/**
|
|
127
133
|
* The most passes to run. One pass can expose work for the next, such as
|
|
128
134
|
* an inlined component whose props now fold. Compilation stops early once
|
|
@@ -189,4 +195,4 @@ interface ModuleConstants {
|
|
|
189
195
|
declare function readModuleConstants(code: string, options?: Pick<CompileOptions, 'filename' | 'importedConstants' | 'globalConstants'>): ModuleConstants;
|
|
190
196
|
//#endregion
|
|
191
197
|
export { readModuleConstants as a, SkipPass as c, compile as i, Primitive as l, CompileResult as n, ImportedConstants as o, ModuleConstants as r, Skip as s, CompileOptions as t };
|
|
192
|
-
//# sourceMappingURL=compile-
|
|
198
|
+
//# sourceMappingURL=compile-CwH9mvHD.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-CwH9mvHD.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC4DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC9DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;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;;;;;;EAMA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAoGI,QAAQ,cAAc,UAAS,iBAAsB;UA0DpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}
|