solid-optimizer 0.2.0 → 0.4.0

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.
Files changed (53) hide show
  1. package/README.md +294 -14
  2. package/dist/compile-BQuBDA0Q.d.mts +199 -0
  3. package/dist/compile-BQuBDA0Q.d.mts.map +1 -0
  4. package/dist/compile-D3caSUtw.mjs +4244 -0
  5. package/dist/compile-D3caSUtw.mjs.map +1 -0
  6. package/dist/index.d.mts +2 -0
  7. package/dist/index.mjs +2 -0
  8. package/dist/vite.d.mts +53 -0
  9. package/dist/vite.d.mts.map +1 -0
  10. package/dist/vite.mjs +1743 -0
  11. package/dist/vite.mjs.map +1 -0
  12. package/package.json +60 -58
  13. package/src/ast.ts +271 -0
  14. package/src/compile.ts +355 -0
  15. package/src/constants.ts +249 -0
  16. package/src/context.ts +116 -0
  17. package/src/fold.ts +850 -0
  18. package/src/index.ts +5 -107
  19. package/src/inline.ts +2237 -0
  20. package/src/jsx.ts +170 -0
  21. package/src/memo.ts +416 -0
  22. package/src/provider.ts +899 -0
  23. package/src/scope.ts +588 -0
  24. package/src/server.ts +275 -0
  25. package/src/size.ts +309 -0
  26. package/src/value.ts +386 -0
  27. package/src/vite/index.ts +1378 -0
  28. package/src/vite/inject.ts +350 -0
  29. package/src/vite/namespace.ts +66 -0
  30. package/src/vite/refs.ts +99 -0
  31. package/src/vite/report.ts +74 -0
  32. package/src/vite/runtime.ts +370 -0
  33. package/src/vite/usage.ts +405 -0
  34. package/dist/cjs/development/index.cjs +0 -228
  35. package/dist/cjs/development/index.cjs.map +0 -7
  36. package/dist/cjs/production/index.cjs +0 -1
  37. package/dist/esm/development/index.mjs +0 -197
  38. package/dist/esm/development/index.mjs.map +0 -7
  39. package/dist/esm/production/index.mjs +0 -1
  40. package/dist/types/checks.d.ts +0 -9
  41. package/dist/types/checks.d.ts.map +0 -1
  42. package/dist/types/index.d.ts +0 -3
  43. package/dist/types/index.d.ts.map +0 -1
  44. package/dist/types/path-references-import.d.ts +0 -3
  45. package/dist/types/path-references-import.d.ts.map +0 -1
  46. package/dist/types/unwrap-node.d.ts +0 -7
  47. package/dist/types/unwrap-node.d.ts.map +0 -1
  48. package/dist/types/unwrap-path.d.ts +0 -6
  49. package/dist/types/unwrap-path.d.ts.map +0 -1
  50. package/src/checks.ts +0 -42
  51. package/src/path-references-import.ts +0 -82
  52. package/src/unwrap-node.ts +0 -27
  53. package/src/unwrap-path.ts +0 -18
package/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # solid-optimizer
2
2
 
3
- > Experimental compile-time optimizer for SolidJS
3
+ > Experimental compile-time optimizer for SolidJS 1.x
4
4
 
5
- [![NPM](https://img.shields.io/npm/v/solid-optimizer.svg)](https://www.npmjs.com/package/solid-optimizer) [![JavaScript Style Guide](https://badgen.net/badge/code%20style/airbnb/ff5a5f?icon=airbnb)](https://github.com/airbnb/javascript)
5
+ [![NPM](https://img.shields.io/npm/v/solid-optimizer.svg)](https://www.npmjs.com/package/solid-optimizer)
6
+
7
+ `solid-optimizer` rewrites JSX before Solid's JSX transform lowers it. It inlines components and resolves control flow whose outcome is known at build time, so more markup lands in fewer templates.
6
8
 
7
9
  ## Install
8
10
 
@@ -18,33 +20,311 @@ yarn add -D solid-optimizer
18
20
  pnpm add -D solid-optimizer
19
21
  ```
20
22
 
21
- ## Features
23
+ ## Usage
24
+
25
+ ```js
26
+ import { compile } from 'solid-optimizer';
27
+
28
+ const result = compile(code, { filename: 'app.jsx' });
29
+
30
+ result.code; // JSX, ready for Solid's JSX transform
31
+ result.map; // a source map, or `null` when nothing changed
32
+ ```
33
+
34
+ The input and the output both keep their JSX. Run the JSX transform after `compile`.
35
+
36
+ `compile` works on a single module or on a bundled chunk that kept its JSX. A chunk holds more components in one scope, so more of them inline.
37
+
38
+ ### Options
39
+
40
+ - `filename` sets the parser and the source map source. `.ts` and `.tsx` parse as TypeScript. Anything else parses as JSX. Defaults to `input.jsx`.
41
+ - `fold` turns constant folding and control-flow resolution on or off. Defaults to `true`.
42
+ - `inline` turns component inlining on or off. Defaults to `true`.
43
+ - `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).
44
+ - `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.
45
+ - `contexts` turns context provider removal on or off. Defaults to `true`.
46
+ - `memos` turns memo inlining on or off. Defaults to `true`.
47
+ - `server` compiles for the server. It removes `createEffect` and `onMount`, and reduces `untrack`, `batch`, `startTransition`, `createDeferred`, `getListener`, `createMemo`, `createRenderEffect`, and `createComputed` to what they do on the server. Defaults to `false`.
48
+ - `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.
49
+ - `moduleSources` lists the modules Solid's built-ins are imported from. Defaults to `['solid-js', 'solid-js/web']`.
50
+ - `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).
51
+ - `maxPasses` limits how many rounds run. Compilation stops early once a round changes nothing. Defaults to `10`.
52
+ - `sourceMap` turns the source map on or off. Defaults to `true`.
53
+ - `report` records what the inline, provider, and memo passes leave alone, and why. The result lists them in `skipped`, each with its pass, a reason code, and a name. Defaults to `false`.
54
+
55
+ ### Constants from other modules
56
+
57
+ `readModuleConstants` reads the constants a module exports, and the modules it imports from.
58
+
59
+ ```js
60
+ import { compile, readModuleConstants } from 'solid-optimizer';
61
+
62
+ const config = readModuleConstants(configCode, { filename: 'config.ts' });
63
+ config.exports; // { SHOW_BANNER: true, THEME: 'light' }
64
+
65
+ compile(appCode, {
66
+ filename: 'app.tsx',
67
+ importedConstants: { './config': config.exports },
68
+ });
69
+ ```
70
+
71
+ It keeps `export const` bindings whose value folds, a `let` the module never writes to, `export default` of a constant, and names exported from them. `import * as config` also works, as `config.NAME`. A module that re-exports constants from another module takes that module's constants in its own `importedConstants`.
22
72
 
23
73
  > **Warning**
24
- > The following features are only for SSR
74
+ > The output changes the shape of the rendered tree, and with it the hydration keys. Compile a server build and its client build from the same code with the same options.
75
+
76
+ ## Vite
77
+
78
+ `solid-optimizer/vite` replaces `vite-plugin-solid`. It takes the same options and wraps the official plugin, so SSR and HMR keep working.
79
+
80
+ ```js
81
+ // vite.config.js
82
+ import { defineConfig } from 'vite';
83
+ import solid from 'solid-optimizer/vite';
84
+
85
+ export default defineConfig({
86
+ plugins: [solid({ ssr: true })],
87
+ });
88
+ ```
25
89
 
26
- ### Trimming no-op
90
+ `vite-plugin-solid` and `vite` are peer dependencies.
27
91
 
28
- The following are no-op functions in SSR, their calls are removed to enable tree-shaking unwanted code.
92
+ The plugin optimizes in one of two modes.
29
93
 
30
- - `createEffect`
31
- - `onMount`
94
+ - **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.
95
+ - **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.
32
96
 
33
- ### `untrack`, `batch` and `startTransition`
97
+ Both modes fold constants imported from other modules in the project.
34
98
 
35
- Passed argument is inlined and called synchronously. For arrow functions, if the function doesn't have the body, it's return expression is inlined instead.
99
+ - Each import is resolved and loaded by the bundler, so aliases, virtual modules, and plugins that rewrite the module are respected.
100
+ - A module with JSX is not loaded, since its transform could be waiting for the importer. Its constants still fold in chunk mode.
101
+ - `define` and `import.meta.env` values only fold in chunk mode.
102
+ - Dependencies are not read, and nothing is read while serving.
36
103
 
37
- ### `createDeferred`
104
+ The optimizer is off while serving. Set `optimizer.dev` to run module mode in dev too.
38
105
 
39
- Passed argument is inlined.
106
+ ### Options
107
+
108
+ The plugin takes every option of `vite-plugin-solid`, plus `optimizer`.
109
+
110
+ - `optimizer: false` uses `vite-plugin-solid` as it is.
111
+ - `optimizer.fold`, `optimizer.inline`, `optimizer.alwaysInline`, `optimizer.contexts`, `optimizer.memos`, and `optimizer.maxPasses` work like the `compile` options.
112
+ - `optimizer.mode` is `'auto'` by default. Set it to `'module'` to never keep JSX through bundling.
113
+ - `optimizer.dev` also optimizes while serving. Defaults to `false`.
114
+ - `optimizer.server` applies the `server` option in server builds. Defaults to `true`.
115
+ - `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`.
116
+
117
+ ### Report
118
+
119
+ `optimizer.report` lists each component call, provider, and memo the optimizer left alone, grouped by reason:
120
+
121
+ ```
122
+ solid-optimizer: what stays as written in client
123
+ inline copy-size 3 Table (../components/Table)×2, Card (../components/Card)
124
+ inline imported 2 Table (./Table-*.js)×2
125
+ inline no-host 2 App, Home
126
+ ```
127
+
128
+ Each line is a pass, a reason, how many times it happened, and the names it happened to. The reasons for the inline pass:
129
+
130
+ - `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.
131
+ - `component-params`, `component-body`, `component-return`, `component-async`, `component-recursive`, and `component-this` mean the component's own shape is not supported.
132
+ - `call-spread`, `call-ref`, and `call-namespace` mean the call site passes a spread, a `ref`, or a namespaced attribute.
133
+ - `no-host` means the component has statements, and the call is not in JSX a component returns, such as a `<For>` callback.
134
+ - `size` means the copies would be larger than the calls.
135
+ - `lazy`, `imported`, `nested`, and `not-a-function` mean the tag is not a component declared at the top of the same module or chunk.
136
+ - A reason that starts with `copy-` is about copying a component from another module. `copy-package` means it comes from `node_modules`, `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.
137
+
138
+ 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.
139
+
140
+ ### Limits
141
+
142
+ - Chunk mode is skipped when the `babel` option is set, since those Babel plugins need each module.
143
+ - 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.
144
+ - A component used in several chunks is copied into each of them, where each copy is no larger than its call.
145
+ - Components from dependencies in `node_modules` are not copied.
146
+ - Modules matched by the `extensions` option are lowered by the official plugin and are not optimized.
147
+
148
+ ## Features
149
+
150
+ ### Component inlining
151
+
152
+ A component call is replaced with the component's JSX.
153
+
154
+ ```jsx
155
+ function Title(props) {
156
+ return <h1 class="title">{props.label}</h1>;
157
+ }
158
+
159
+ export function App() {
160
+ return (
161
+ <main>
162
+ <Title label="Hello" />
163
+ </main>
164
+ );
165
+ }
166
+ ```
167
+
168
+ becomes
169
+
170
+ ```jsx
171
+ export function App() {
172
+ return (
173
+ <main>
174
+ <h1 class="title">{'Hello'}</h1>
175
+ </main>
176
+ );
177
+ }
178
+ ```
40
179
 
41
- ### `getListener`
180
+ Solid then creates one template for `<main>` instead of two.
42
181
 
43
- `getListener` calls are replaced with `null`.
182
+ - A read of `props.name` becomes the attribute's expression. Solid passes dynamic props as getters, so the expression runs at every read, as before.
183
+ - A static prop that must be evaluated once, like a function read twice, is stored in a `const` first.
184
+ - `{props.children}`, and a prop whose value is JSX, splice their JSX in place so it joins the template.
185
+ - Statements in the component body move into the component whose returned JSX holds the call. The call must sit directly in that JSX, with only intrinsic elements and fragments around it.
186
+ - Bindings declared in the component get fresh names in every copy.
187
+ - A component nothing else uses is removed.
188
+
189
+ A component inlines when all of these hold:
190
+
191
+ - It is declared once at the top level, with a capitalized name.
192
+ - Its parameter is one identifier that is only read as `props.name`, spread onto an element as `{...props}`, or passed to `mergeProps()` and `splitProps()`. See below.
193
+ - Its body ends in the only `return`, and that `return` returns JSX.
194
+ - It does not use `this`, `arguments`, `super`, or `new.target`.
195
+ - The call site has no spread attribute, no `ref`, and no namespaced attribute.
196
+
197
+ The props can go through `mergeProps()` and `splitProps()` first, declared as a `const` in the component:
198
+
199
+ ```jsx
200
+ function Button(props) {
201
+ const merged = mergeProps({ type: 'button' }, props);
202
+ const [local, rest] = splitProps(merged, ['label']);
203
+ return (
204
+ <button type={merged.type} {...rest}>
205
+ {local.label}
206
+ </button>
207
+ );
208
+ }
209
+ ```
210
+
211
+ - A default has to be a literal, like `'button'`, `0`, or `false`.
212
+ - `splitProps()` has to name its keys in array literals.
213
+ - A view can only be read as `view.name`, spread onto an element, or passed to another `mergeProps()` or `splitProps()`.
214
+ - A prop that can be `undefined` falls back to the default behind it, the way `mergeProps()` reads it.
215
+ - A spread becomes the attributes the call site passes, and it cannot repeat an attribute the element already has. Its `children` become the element's children, so the element must have none of its own.
216
+
217
+ Inlining accepts two differences:
218
+
219
+ - A prop called as `props.name()` runs with its own receiver as `this`, not the props object.
220
+ - Moved statements run before the host's JSX is created. The order of effect registration between siblings can change.
221
+
222
+ Inlined JSX is a copy, so its source map points at the call site it replaced.
223
+
224
+ #### Only where it is smaller
225
+
226
+ Every copy repeats the component's own code, so inlining a component at many calls can make the output larger than the calls were. Before inlining, each component's copies are compared with what they replace, as Solid's JSX transform would lower them:
227
+
228
+ - A copy saves the component call, and the getters of its props. Elements it puts directly in another element join that element's template.
229
+ - 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.
230
+ - When every call inlines and nothing else uses the component, its declaration goes away too.
231
+ - A call in its children that can only inline once it does, because that component has statements to hoist, counts too.
232
+
233
+ 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.
234
+
235
+ 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.
236
+
237
+ Set `alwaysInline` to inline every component that can be.
238
+
239
+ ### Context inlining
240
+
241
+ A context provider, `<Ctx.Provider>`, whose readers are all visible is removed, and each read gets the provider's value.
242
+
243
+ ```jsx
244
+ const Theme = createContext();
245
+
246
+ function Label() {
247
+ const theme = useContext(Theme);
248
+ return <span class={theme}>label</span>;
249
+ }
250
+
251
+ export function App() {
252
+ return (
253
+ <main>
254
+ <Theme.Provider value="dark">
255
+ <Label />
256
+ </Theme.Provider>
257
+ </main>
258
+ );
259
+ }
260
+ ```
261
+
262
+ becomes
263
+
264
+ ```jsx
265
+ export function App() {
266
+ const theme$1 = 'dark';
267
+ return (
268
+ <main>
269
+ <span class={'dark'}>label</span>
270
+ </main>
271
+ );
272
+ }
273
+ ```
274
+
275
+ - Everything under the provider has to be visible: intrinsic elements, Solid's built-ins, and components that inline. A call to an unknown function, or a read of a component's `props`, keeps the provider.
276
+ - An object value is stored in a `const` once, so every reader gets the same object.
277
+ - The value must never be `undefined`.
278
+ - Removing the provider removes its owner, so the hydration keys change. Compile a server build and its client build with the same options.
279
+
280
+ See [strategy.md](https://github.com/lxsmnsyc/solid-optimizer/blob/main/strategy.md) for the exact rules.
281
+
282
+ ### Memo inlining
283
+
284
+ A `createMemo` whose result is new on every run, or that sets `equals: false` in its options,, such as an object, array, or JSX, never stops an update. When it is also read once, as the whole of a tracked JSX expression, the read runs the computation itself.
285
+
286
+ ```jsx
287
+ const style = createMemo(() => ({ color: color() }));
288
+ return <p style={style()} />;
289
+ ```
290
+
291
+ becomes `<p style={{ color: color() }} />`. The computation must take no `prev` parameter, so an initial value, the second argument in Solid 1, is ignored.
292
+
293
+ ### Server builds
294
+
295
+ On the server, Solid renders once and never updates, so its reactive primitives reduce to plain calls.
296
+
297
+ - `createEffect` and `onMount` are removed. Whatever only they used, like a charting library, drops out of the server bundle.
298
+ - `untrack(fn)` and `batch(fn)` become `fn()`, and `startTransition(fn)` calls `fn()` and returns nothing.
299
+ - `createDeferred(source)` becomes `source`, and `getListener()` becomes `null`.
300
+ - `createMemo`, `createRenderEffect`, and `createComputed` run their function once, in place.
301
+
302
+ This runs after the other optimizations, while modules are transformed. The server and client build then make the same inlining decisions, so hydration still matches.
303
+
304
+ Two differences are accepted. A function passed to `createMemo`, `createRenderEffect`, or `createComputed` no longer gets its own owner, so an `onCleanup` inside it registers on the enclosing one. Inside `catchError` or `<ErrorBoundary>`, an error it throws stops the component instead of leaving the memo `undefined`.
305
+
306
+ ### Constant folding and control flow
307
+
308
+ This follows the `optimize` option of Solid 2.0's `@solidjs/compiler` ([solidjs/solid#3231](https://github.com/solidjs/solid/pull/3231)), applied to Solid 1's components.
309
+
310
+ - Constant expressions fold, including `const` bindings and `let` bindings nothing writes to, at any scope.
311
+ - Branches that a constant condition never takes are removed. So are statements after a `return`, `throw`, `break`, or `continue`.
312
+ - `<Show when>` becomes its children or its `fallback`.
313
+ - `<For each>` becomes its `fallback` when `each` is an empty array literal or a falsy constant.
314
+ - `<Index each>` becomes its `fallback` under the same conditions as `<For each>`.
315
+ - `<Switch>` drops each `<Match>` that is always false, and becomes the first `<Match>` that is always true.
316
+ - `<Dynamic component="div">` becomes `<div>`.
317
+
318
+ 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>`, `<Suspense>`, `<SuspenseList>`, and `<ErrorBoundary>` never fold.
319
+
320
+ A call to Solid's `lazy` is marked `/* @__PURE__ */`, so the bundler drops one that a folded branch left unused, along with its chunk.
321
+
322
+ A condition that is discarded but has side effects still runs. `[effect()] && value` becomes `([effect()], value)`.
44
323
 
45
324
  ## Sponsors
46
325
 
47
326
  ![Sponsors](https://github.com/lxsmnsyc/sponsors/blob/main/sponsors.svg?raw=true)
327
+
48
328
  ## License
49
329
 
50
330
  MIT © [lxsmnsyc](https://github.com/lxsmnsyc)
@@ -0,0 +1,199 @@
1
+ import { SourceMap } from "@jridgewell/remapping";
2
+ import MagicString from "magic-string";
3
+ import "oxc-parser";
4
+ //#region src/context.d.ts
5
+ /** The passes that record what they leave alone. */
6
+ type SkipPass = 'inline' | 'contexts' | 'memos';
7
+ /**
8
+ * Something a pass left alone, and why. The `compile` option `report`
9
+ * collects them.
10
+ */
11
+ interface Skip {
12
+ readonly pass: SkipPass;
13
+ /** A short code for the reason, like `call-spread`. */
14
+ readonly reason: string;
15
+ /** The component, context, or memo it is about. */
16
+ readonly name: string;
17
+ }
18
+ //#endregion
19
+ //#region src/value.d.ts
20
+ type Primitive = string | number | boolean | null | undefined;
21
+ //#endregion
22
+ //#region src/constants.d.ts
23
+ /**
24
+ * The constants each imported module exports, keyed by the import specifier
25
+ * as the module writes it, then by export name.
26
+ */
27
+ type ImportedConstants = Readonly<Record<string, Readonly<Record<string, Primitive>>>>;
28
+ //#endregion
29
+ //#region src/compile.d.ts
30
+ interface CompileOptions {
31
+ /**
32
+ * The file name, used to pick the parser (`.ts` and `.tsx` parse as
33
+ * TypeScript, anything else as JSX) and as the source in the source map.
34
+ *
35
+ * @default 'input.jsx'
36
+ */
37
+ filename?: string;
38
+ /**
39
+ * Fold constants, remove unreachable code, and resolve control-flow
40
+ * components whose props are constants.
41
+ *
42
+ * @default true
43
+ */
44
+ fold?: boolean;
45
+ /**
46
+ * Replace component calls with the component's body.
47
+ *
48
+ * @default true
49
+ */
50
+ inline?: boolean;
51
+ /**
52
+ * Compile for the server: remove `createEffect` and `onMount`, and reduce
53
+ * `untrack`, `batch`, `startTransition`, `createDeferred`, `getListener`,
54
+ * `createMemo`, `createRenderEffect`, and `createComputed` to what they do
55
+ * on the server.
56
+ *
57
+ * This runs once, after the other passes. The server and client build
58
+ * make the same folding and inlining decisions from the same code, so the
59
+ * server renders the tree the client hydrates.
60
+ *
61
+ * @default false
62
+ */
63
+ server?: boolean;
64
+ /**
65
+ * Remove context providers whose reads are all visible, and give each
66
+ * read the provider's value.
67
+ *
68
+ * @default true
69
+ */
70
+ contexts?: boolean;
71
+ /**
72
+ * Replace a `createMemo` that always produces a new value and is read once
73
+ * with its computation.
74
+ *
75
+ * @default true
76
+ */
77
+ memos?: boolean;
78
+ /**
79
+ * The names of Solid's built-in components. A tag only folds when it is
80
+ * one of these, and an empty list turns control-flow folding off.
81
+ *
82
+ * @default ['For', 'Show', 'Switch', 'Match', 'Suspense', 'SuspenseList', 'Portal', 'Index', 'Dynamic', 'ErrorBoundary']
83
+ */
84
+ builtIns?: readonly string[];
85
+ /**
86
+ * The modules that export Solid's built-in components. A tag imported from
87
+ * anywhere else is a different component.
88
+ *
89
+ * @default ['solid-js', 'solid-js/web']
90
+ */
91
+ moduleSources?: readonly string[];
92
+ /**
93
+ * Top-level names that refer to Solid's built-in components, mapped to the
94
+ * built-in's name. A bundler renames bindings, so a chunk can call `Show`
95
+ * something else, or declare it instead of importing it.
96
+ */
97
+ builtInAliases?: Readonly<Record<string, string>>;
98
+ /**
99
+ * Treat a top-level `var` that nothing writes to as a constant. Bundlers
100
+ * turn top-level `const` into `var`, so this is on for bundled chunks.
101
+ *
102
+ * @default false
103
+ */
104
+ constantVars?: boolean;
105
+ /**
106
+ * The constants imported modules export, keyed by the import specifier as
107
+ * the code writes it, then by export name. An import of one of them folds
108
+ * like a local `const`. `constantExports` reads them from a module.
109
+ */
110
+ importedConstants?: ImportedConstants;
111
+ /**
112
+ * The exports of imported modules that are contexts made by Solid's
113
+ * `createContext`, keyed by the import specifier as the code writes it.
114
+ * A provider of an imported context can be removed like a local one.
115
+ */
116
+ importedContexts?: Readonly<Record<string, readonly string[]>>;
117
+ /**
118
+ * Top-level components that other modules use too, by the number of
119
+ * modules that use them, this one included. Inlining one here only counts
120
+ * its share of the declaration, which goes away once every one of them
121
+ * inlines it. `0` keeps it whatever is inlined, like a component that is
122
+ * also passed as a value.
123
+ */
124
+ sharedComponents?: Readonly<Record<string, number>>;
125
+ /**
126
+ * Inline every component that can be, even where the copies are larger
127
+ * than the calls and declaration they replace. By default, a component is
128
+ * only inlined where its copies are estimated to be no larger.
129
+ *
130
+ * @default false
131
+ */
132
+ alwaysInline?: boolean;
133
+ /**
134
+ * The most passes to run. One pass can expose work for the next, such as
135
+ * an inlined component whose props now fold. Compilation stops early once
136
+ * a pass changes nothing.
137
+ *
138
+ * @default 10
139
+ */
140
+ maxPasses?: number;
141
+ /**
142
+ * Generate a source map.
143
+ *
144
+ * @default true
145
+ */
146
+ sourceMap?: boolean;
147
+ /**
148
+ * Record what the inline, provider, and memo passes leave alone, and why.
149
+ * The result lists them in `skipped`.
150
+ *
151
+ * @default false
152
+ */
153
+ report?: boolean;
154
+ }
155
+ interface CompileResult {
156
+ code: string;
157
+ /** The map from the output back to the input, or `null` when nothing changed or maps are off. */
158
+ map: SourceMap | null;
159
+ /**
160
+ * What the last round left alone, and why, when `report` is set. The last
161
+ * round changes nothing, unless `maxPasses` stopped the rounds first.
162
+ */
163
+ skipped?: Skip[];
164
+ }
165
+ /**
166
+ * Optimizes JSX before it is lowered by Solid's JSX transform.
167
+ *
168
+ * The input is JavaScript or TypeScript with JSX, such as a module, or a
169
+ * bundled chunk that kept its JSX. The output keeps JSX too, so the JSX
170
+ * transform runs after this.
171
+ *
172
+ * The output changes the shape of the rendered tree, and with it the
173
+ * hydration keys. A server build and its client build must compile the same
174
+ * code with the same options.
175
+ */
176
+ declare function compile(code: string, options?: CompileOptions): CompileResult;
177
+ interface ModuleConstants {
178
+ /**
179
+ * The modules this module imports or re-exports named bindings from, as
180
+ * written. `importedConstants` of `compile` takes their constants.
181
+ */
182
+ imports: string[];
183
+ /**
184
+ * The constants this module exports, by export name: `export const`
185
+ * bindings whose value folds, names exported from them, and names
186
+ * re-exported from `importedConstants`.
187
+ */
188
+ exports: Record<string, Primitive>;
189
+ }
190
+ /**
191
+ * Reads what a module imports and the constants it exports, so that
192
+ * `compile` can fold them in the modules that import it.
193
+ *
194
+ * Only `filename` and `importedConstants` of the options are used.
195
+ */
196
+ declare function readModuleConstants(code: string, options?: Pick<CompileOptions, 'filename' | 'importedConstants'>): ModuleConstants;
197
+ //#endregion
198
+ export { readModuleConstants as a, Skip as c, compile as i, SkipPass as l, CompileResult as n, ImportedConstants as o, ModuleConstants as r, Primitive as s, CompileOptions as t };
199
+ //# sourceMappingURL=compile-BQuBDA0Q.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile-BQuBDA0Q.d.mts","names":[],"sources":["../src/context.ts","../src/value.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;;KAiEY;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;KClEC;;;;;;;KCSA,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCD/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;;;;;;;EAaA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAkGI,QAAQ,cAAc,UAAS,iBAAsB;UA2EpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,oDACb"}