solid-optimizer 1.0.0-next.1 → 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-BpoiBa6b.mjs → compile-Cvjy2e11.mjs} +1910 -387
- 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 +5 -2
- package/src/ast.ts +4 -3
- package/src/compile.ts +93 -14
- package/src/conditional.ts +547 -0
- package/src/constants.ts +31 -5
- 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-BpoiBa6b.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -8,9 +8,11 @@
|
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|
|
|
11
|
+
The website, [solid-optimizer.vercel.app](https://solid-optimizer.vercel.app), shows each pass and the benchmarks for the example apps.
|
|
12
|
+
|
|
11
13
|
## Install
|
|
12
14
|
|
|
13
|
-
The Solid 2.0 version is published under the `next` tag. The `latest` version is for Solid 1.x.
|
|
15
|
+
The Solid 2.0 version is published under the `next` tag. The `latest` version is for Solid 1.x, which only gets fixes now, from the [`0.x` branch](https://github.com/lxsmnsyc/solid-optimizer/tree/0.x).
|
|
14
16
|
|
|
15
17
|
```bash
|
|
16
18
|
npm i -D solid-optimizer@next
|
|
@@ -45,9 +47,11 @@ The input and the output both keep their JSX. Run the JSX transform after `compi
|
|
|
45
47
|
- `fold` turns constant folding and control-flow resolution on or off. Defaults to `true`.
|
|
46
48
|
- `inline` turns component inlining on or off. Defaults to `true`.
|
|
47
49
|
- `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).
|
|
50
|
+
- `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
51
|
- `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
52
|
- `contexts` turns context provider removal on or off. Defaults to `true`.
|
|
50
53
|
- `memos` turns memo inlining on or off. Defaults to `true`.
|
|
54
|
+
- `conditionals` turns `<Show>` and `<Switch>` lowering on or off. A `<Show>` without `keyed` becomes a ternary, which Solid's JSX transform compiles to one memo instead of a component and three memos. A callback child like `{(user) => <b>{user().name}</b>}` becomes its body, with `user()` read as `when` again, when `when` reads names, members, and signal or memo getters. A `<Switch>` of such `<Match>`es becomes a chain of ternaries. Defaults to `true`.
|
|
51
55
|
- `builtIns` lists the names of Solid's built-in components. A tag only folds when it is one of them. An empty list turns control-flow folding off.
|
|
52
56
|
- `moduleSources` lists the modules Solid's built-ins are imported from. Defaults to `['solid-js', '@solidjs/web']`.
|
|
53
57
|
- `importedConstants` holds the constants of imported modules, keyed by the import specifier as the code writes it, then by export name. An import of one of them folds like a local `const`. See [Constants from other modules](#constants-from-other-modules).
|
|
@@ -95,7 +99,7 @@ export default defineConfig({
|
|
|
95
99
|
|
|
96
100
|
The plugin optimizes in one of two modes.
|
|
97
101
|
|
|
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.
|
|
102
|
+
- **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
103
|
- **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
104
|
|
|
101
105
|
Both modes fold constants imported from other modules in the project.
|
|
@@ -112,10 +116,11 @@ The optimizer is off while serving. Set `optimizer.dev` to run module mode in de
|
|
|
112
116
|
The plugin takes every option of `@solidjs/vite-plugin`, plus `optimizer`.
|
|
113
117
|
|
|
114
118
|
- `optimizer: false` uses `@solidjs/vite-plugin` as it is.
|
|
115
|
-
- `optimizer.fold`, `optimizer.inline`, `optimizer.alwaysInline`, `optimizer.contexts`, `optimizer.memos`, and `optimizer.maxPasses` work like the `compile` options.
|
|
119
|
+
- `optimizer.fold`, `optimizer.inline`, `optimizer.alwaysInline`, `optimizer.contexts`, `optimizer.memos`, `optimizer.conditionals`, and `optimizer.maxPasses` work like the `compile` options.
|
|
116
120
|
- `optimizer.mode` is `'auto'` by default. Set it to `'module'` to never keep JSX through bundling.
|
|
117
121
|
- `optimizer.dev` also optimizes while serving. Defaults to `false`.
|
|
118
122
|
- `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`.
|
|
123
|
+
- `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
124
|
|
|
120
125
|
### Report
|
|
121
126
|
|
|
@@ -128,24 +133,25 @@ solid-optimizer: what stays as written in client
|
|
|
128
133
|
inline no-host 1 Home
|
|
129
134
|
```
|
|
130
135
|
|
|
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:
|
|
136
|
+
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
137
|
|
|
133
138
|
- `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
139
|
- `component-params`, `component-body`, `component-return`, `component-async`, `component-recursive`, and `component-this` mean the component's own shape is not supported.
|
|
135
|
-
- `
|
|
140
|
+
- `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.
|
|
141
|
+
- `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
142
|
- `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
143
|
- `size` means the copies would be larger than the calls.
|
|
138
144
|
- `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
|
|
145
|
+
- 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 component's module imports the importer back, directly or through other modules, a listed package, or a virtual module, `copy-virtual` means the copy would import a virtual module that its specifier does not reach from the importer, `copy-wrapper` means the component only returns a call of another component in its module, and `copy-size` means the copy was larger.
|
|
140
146
|
|
|
141
|
-
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.
|
|
147
|
+
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, and `signal-constant` for a signal that nothing writes but that is used other than by calling it.
|
|
142
148
|
|
|
143
149
|
### Limits
|
|
144
150
|
|
|
145
151
|
- Chunk mode is skipped when the `babel` option is set or `compiler` is `'babel'`, since those passes need each module.
|
|
146
152
|
- 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
153
|
- 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
|
|
154
|
+
- Components from dependencies in `node_modules` are only copied from the packages `optimizer.packages` lists.
|
|
149
155
|
- `.tsrx` modules are lowered by the official plugin and are not optimized.
|
|
150
156
|
|
|
151
157
|
## Features
|
|
@@ -191,16 +197,16 @@ Solid then creates one template for `<main>` instead of two.
|
|
|
191
197
|
- the child callback of `<Show>` or `<Match>`, when it takes the value;
|
|
192
198
|
- the callback given to `render`.
|
|
193
199
|
- Bindings declared in the component get fresh names in every copy.
|
|
194
|
-
- A component nothing else uses is removed.
|
|
200
|
+
- A component nothing else uses is removed, with any alias of it that nothing uses.
|
|
195
201
|
|
|
196
202
|
A component inlines when all of these hold:
|
|
197
203
|
|
|
198
|
-
- It is declared once at the top level, with a capitalized name.
|
|
204
|
+
- 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. So is a call through a compound component, like Kobalte's `const Dialog = Object.assign(DialogRoot, { Trigger: DialogTrigger })`: `<Dialog>` calls `DialogRoot` and `<Dialog.Trigger>` calls `DialogTrigger`, as long as nothing writes to a member of either.
|
|
199
205
|
- 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
206
|
- Its body ends in the only `return`, and that `return` returns JSX.
|
|
201
207
|
- It does not use `this`, `arguments`, `super`, or `new.target`.
|
|
202
208
|
- 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.
|
|
209
|
+
- 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
210
|
|
|
205
211
|
The props can go through `merge()` and `omit()` first, declared as a `const` in the component:
|
|
206
212
|
|
|
@@ -226,7 +232,7 @@ Inlining accepts two differences:
|
|
|
226
232
|
- A prop called as `props.name()` runs with its own receiver as `this`, not the props object.
|
|
227
233
|
- Moved statements run before the host's JSX is created. The order of effect registration between siblings can change.
|
|
228
234
|
|
|
229
|
-
|
|
235
|
+
The source map points each copied character at the one it copies, so inlined JSX maps to the component's own JSX and a prop value to the call site's attribute. Code a pass writes itself, such as a folded constant or a renamed local, maps to what it replaces.
|
|
230
236
|
|
|
231
237
|
#### Only where it is smaller
|
|
232
238
|
|
|
@@ -236,9 +242,12 @@ Every copy repeats the component's own code, so inlining a component at many cal
|
|
|
236
242
|
- 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
243
|
- When every call inlines and nothing else uses the component, its declaration goes away too.
|
|
238
244
|
- A call in its children that can only inline once it does, because that component has statements to hoist, counts too.
|
|
245
|
+
- 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
246
|
|
|
240
247
|
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
248
|
|
|
249
|
+
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.
|
|
250
|
+
|
|
242
251
|
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
252
|
|
|
244
253
|
Set `alwaysInline` to inline every component that can be.
|
|
@@ -299,6 +308,8 @@ becomes `<p style={{ color: color() }} />`. See [strategy.md](https://github.com
|
|
|
299
308
|
|
|
300
309
|
A memo whose computation folds to a literal never changes. Every call of it becomes the literal, however many there are, when each call comes after the declaration and nothing else uses the memo.
|
|
301
310
|
|
|
311
|
+
So does a signal that nothing writes: `const [size] = createSignal(3)`, or one whose setter nothing references. With a literal and no options, every call of the getter becomes the literal, under the same conditions. This also works outside a function, since a signal takes no owner.
|
|
312
|
+
|
|
302
313
|
```jsx
|
|
303
314
|
const size = createMemo(() => 1 + 2);
|
|
304
315
|
return <p>{size() * 2}</p>;
|
|
@@ -306,6 +317,8 @@ return <p>{size() * 2}</p>;
|
|
|
306
317
|
|
|
307
318
|
becomes `<p>{6}</p>`.
|
|
308
319
|
|
|
320
|
+
A plain arrow that only returns an expression, like `const label = () => (props.open ? 'Close' : 'Open')`, has no cache and no owner, so each call becomes its body. That needs every call to come after the declaration with no arguments, nothing else to use the arrow, and every name in the body to mean the same at the call. With more than one call, the body is copied only when the copies are no larger than the arrow and its calls, minified.
|
|
321
|
+
|
|
309
322
|
Solid's `children` helper over JSX, read once as an element's child, becomes that JSX. The helper resolves JSX the same way inserting it does, and nothing makes it run again.
|
|
310
323
|
|
|
311
324
|
```jsx
|
|
@@ -340,11 +353,12 @@ This follows the `optimize` option of `@solidjs/compiler` ([solidjs/solid#3231](
|
|
|
340
353
|
- `<Repeat count>` becomes its `fallback` when `count` is below one.
|
|
341
354
|
- `<Switch>` drops each `<Match>` that is always false, and becomes the first `<Match>` that is always true.
|
|
342
355
|
- `<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.
|
|
356
|
+
- `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
357
|
- 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
358
|
|
|
345
359
|
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.
|
|
346
360
|
|
|
347
|
-
A call to Solid's `lazy` is marked `/* @__PURE__ */`, so the bundler drops one that a folded branch left unused, along with its chunk.
|
|
361
|
+
A call to Solid's `lazy` is marked `/* @__PURE__ */`, so the bundler drops one that a folded branch left unused, along with its chunk. `createContext` is marked too, and so are `createSignal` and `createStore` whose first argument is a literal, an object, or an array.
|
|
348
362
|
|
|
349
363
|
A condition that is discarded but has side effects still runs. `[effect()] && value` becomes `([effect()], value)`.
|
|
350
364
|
|