what-compiler 0.13.11 → 0.15.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.
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/babel.d.ts +78 -2
- package/dist/babel/auto-selector.min.js +94 -0
- package/dist/babel/common.min.js +2053 -0
- package/dist/babel/dev-metadata.min.js +106 -0
- package/dist/babel/dom.min.js +1669 -0
- package/dist/babel/jsx-root.min.js +13 -0
- package/dist/babel/lists.min.js +492 -0
- package/dist/babel/nesting.min.js +328 -0
- package/dist/babel/reactivity.min.js +1218 -0
- package/dist/babel/server-actions.min.js +85 -0
- package/dist/babel/ssr.min.js +496 -0
- package/dist/babel/types.min.js +9 -0
- package/dist/babel-plugin.min.js +101 -1
- package/dist/file-router.min.js +368 -12
- package/dist/framework-files.min.js +62 -0
- package/dist/hot.min.js +315 -0
- package/dist/index.min.js +13 -414
- package/dist/release.min.js +78 -0
- package/dist/runtime.min.js +9 -1
- package/dist/tables/import-changes.min.js +272 -0
- package/dist/tables/markup-rules.min.js +213 -0
- package/dist/vite-plugin.min.js +432 -399
- package/file-router.d.ts +3 -1
- package/index.d.ts +1 -1
- package/package.json +11 -2
- package/runtime.d.ts +2 -1
- package/src/babel/auto-selector.js +248 -0
- package/src/babel/common.js +3800 -0
- package/src/babel/dev-metadata.js +167 -0
- package/src/babel/dom.js +3386 -0
- package/src/babel/jsx-root.js +27 -0
- package/src/babel/lists.js +704 -0
- package/src/babel/nesting.js +260 -0
- package/src/babel/reactivity.js +1998 -0
- package/src/babel/server-actions.js +144 -0
- package/src/babel/ssr.js +870 -0
- package/src/babel/types.js +10 -0
- package/src/babel-plugin.js +185 -3598
- package/src/file-router.js +58 -38
- package/src/framework-files.js +106 -0
- package/src/hot.js +523 -0
- package/src/release.js +127 -0
- package/src/runtime.js +7 -7
- package/src/tables/import-changes.js +157 -0
- package/src/tables/markup-rules.js +388 -0
- package/src/vite-plugin.js +460 -175
- package/vite.d.ts +47 -3
- package/src/error-overlay.js +0 -428
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present ZVN DEV
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# what-compiler
|
|
2
2
|
|
|
3
|
-
JSX compiler for [What Framework](https://whatfw.com). Transforms JSX into optimized DOM operations via a Babel plugin, with a Vite plugin for seamless integration. Also provides file-based routing via `virtual:what-routes`.
|
|
3
|
+
JSX compiler for [What Framework](https://whatfw.com). Transforms JSX into optimized DOM operations for the browser, and into string templates for the server, via a Babel plugin, with a Vite plugin for seamless integration. Also provides file-based routing via `virtual:what-routes`.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -8,6 +8,13 @@ JSX compiler for [What Framework](https://whatfw.com). Transforms JSX into optim
|
|
|
8
8
|
npm install what-compiler --save-dev
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
Install it at the release of what-framework and what-core: its output runs
|
|
12
|
+
only on the runtime of its own release. The packages declare each other as
|
|
13
|
+
peers, so npm refuses an install that mixes releases. Where the install allows
|
|
14
|
+
one (pnpm, Yarn, Bun, `--legacy-peer-deps`), the Vite plugin, and the Babel
|
|
15
|
+
plugin on Node 20.16+ and 22.3+, stop before compiling with
|
|
16
|
+
`ERR_COMPILER_RUNTIME_MISMATCH`, naming both versions.
|
|
17
|
+
|
|
11
18
|
## Vite Plugin
|
|
12
19
|
|
|
13
20
|
The recommended way to use the compiler. Add it to your Vite config:
|
|
@@ -27,13 +34,135 @@ export default defineConfig({
|
|
|
27
34
|
```js
|
|
28
35
|
what({
|
|
29
36
|
include: /\.[jt]sx$/, // File extensions to process (default: .jsx/.tsx)
|
|
30
|
-
exclude:
|
|
37
|
+
exclude: /\/node_modules\//, // Files to exclude (default: a node_modules directory)
|
|
38
|
+
compileDependencies: null, // Additional dependency paths to compile as app source
|
|
31
39
|
sourceMaps: true, // Enable source maps (default: true)
|
|
32
40
|
production: false, // Enable production optimizations (default: auto)
|
|
33
41
|
pages: 'src/pages', // Pages directory for file-based routing
|
|
42
|
+
autoSelector: true, // Rewrite selected() === row.id in keyed rows to a selector (default: true)
|
|
43
|
+
ssr: true, // Compile server transforms with the server target (default: true)
|
|
44
|
+
ssrGuard: true, // false: compile every server transform for the DOM, whatever ssr says (default: true)
|
|
45
|
+
hydratable: 'auto', // Client output that hydrate() adopts server markup with (default: 'auto')
|
|
46
|
+
hot: true, // Hot replacement in the dev server (default: on in development)
|
|
47
|
+
verbose: false, // Also print notes on correct code: auto-selector rewrites, <For> (default: false)
|
|
34
48
|
})
|
|
35
49
|
```
|
|
36
50
|
|
|
51
|
+
### Hot replacement
|
|
52
|
+
|
|
53
|
+
In the dev server a saved edit is applied in place, without a reload. Each
|
|
54
|
+
mounted instance of a component whose code changed is built again where it
|
|
55
|
+
stands, under the same parent, context and error boundaries; its parent, its
|
|
56
|
+
siblings and every node outside it keep their state, and the focus and typed
|
|
57
|
+
text stay. The edited component keeps its own state while its state calls are
|
|
58
|
+
unchanged, as React's Fast Refresh does: each `signal()`, `useState()`,
|
|
59
|
+
`useSignal()` and `useReducer()` call in its body, and in the custom hooks of
|
|
60
|
+
its file it calls, takes back its value; a changed one (its initial value,
|
|
61
|
+
what it is assigned to, one added or removed) starts them all again, and
|
|
62
|
+
`// @refresh reset` in the file starts them again on every edit. A
|
|
63
|
+
module-scope value that is not a function (`export const count = signal(0)`,
|
|
64
|
+
a store, a `new Map()`) keeps its value while its own line is unchanged, so
|
|
65
|
+
editing a store's action keeps the state it acts on, and the components that
|
|
66
|
+
import the action are built again with the new one. A file router page edit
|
|
67
|
+
replaces the page and keeps its layouts; a layout edit replaces the layout
|
|
68
|
+
and keeps the page and the layouts inside it, with the focus and the
|
|
69
|
+
selection of a field there, unless a context the page can read has another
|
|
70
|
+
value in the new layout. With the server rendering in
|
|
71
|
+
development, the next request renders the new code. An instance whose output
|
|
72
|
+
is one element is that element, as in production (`<Comp />` is the element,
|
|
73
|
+
with no markers); one that renders more, or that a region, a list row or a
|
|
74
|
+
component's output of several nodes holds, has `<!--hot-->` markers.
|
|
75
|
+
|
|
76
|
+
The page reloads only where running the new code in place cannot be correct:
|
|
77
|
+
a module that runs statements at its top level (the entry that calls
|
|
78
|
+
`mount()`, a module that starts an `effect()` when it loads), an update that
|
|
79
|
+
would run one again on its way up, a page's `loader` or route exports, and an
|
|
80
|
+
update no module above it takes (Vite's rule). An edit that fails to compile,
|
|
81
|
+
or whose new code throws while it runs or renders, shows in Vite's overlay and
|
|
82
|
+
as `ERR_HOT_UPDATE_FAILED`; the page keeps the code it had, and the next save
|
|
83
|
+
that works applies. A component's instances take an edit together: new code
|
|
84
|
+
that throws for one of them is applied to none, nor to one mounted after. Each update is an `hmr:update` event in the devtools and
|
|
85
|
+
in `what_events` (`types: ["hmr"]`). Nothing of it reaches a server module or
|
|
86
|
+
a production build. `hot: false` turns it off: every edit then goes to Vite,
|
|
87
|
+
which reloads the page.
|
|
88
|
+
|
|
89
|
+
### Dependencies
|
|
90
|
+
|
|
91
|
+
Published files of What itself keep the framework transform rules. A
|
|
92
|
+
published JSX file such as `what-devtools/panel` is compiled for its JSX when
|
|
93
|
+
linked from source. Vite also compiles third-party dependency
|
|
94
|
+
files containing JSX when the package declares `what-framework` or
|
|
95
|
+
`what-core` in `dependencies` or `peerDependencies`. This includes linked
|
|
96
|
+
packages resolved under `node_modules` with `resolve.preserveSymlinks`.
|
|
97
|
+
Other dependency files containing JSX get `ERR_DEPENDENCY_JSX`. If their JSX
|
|
98
|
+
is written for What, opt them in with a path matcher:
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
what({ compileDependencies: [/node_modules\/my-ui-kit\//] })
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
For server builds, put source packages in Vite's `ssr.noExternal` so Vite
|
|
105
|
+
passes their files through the compiler before Node loads them.
|
|
106
|
+
|
|
107
|
+
### Hydration
|
|
108
|
+
|
|
109
|
+
`hydrate()` adopts server-rendered markup when the client is compiled with the
|
|
110
|
+
`hydratable` option: templates claim the server's nodes in document order
|
|
111
|
+
instead of cloning new ones, components keep the server's owner paths, and
|
|
112
|
+
keyed lists adopt their rows. The same output still mounts with `mount()`. The
|
|
113
|
+
Vite plugin's `'auto'` turns it on when the config says the app renders on the
|
|
114
|
+
server (`appType: 'custom'`, `build.ssr` or `build.ssrManifest`) and leaves a
|
|
115
|
+
client-only project on plain output; pass `true` or `false` to decide. With the
|
|
116
|
+
Babel plugin, pass `hydratable: true` (off by default). Client output built
|
|
117
|
+
without it renders the page again under `hydrate()` and reports one
|
|
118
|
+
`ERR_HYDRATION_MISMATCH` of kind `not-hydratable`.
|
|
119
|
+
|
|
120
|
+
### Keyed lists
|
|
121
|
+
|
|
122
|
+
`items().map((item) => <li key={item.id}>…</li>)` is lowered to keyed rows. A
|
|
123
|
+
row keeps its DOM and state for as long as its key is in the list, and a row
|
|
124
|
+
that reads its item for more than its key reads it through the row's item cell,
|
|
125
|
+
so a new object under the same key (an immutable update, a refetch) shows in
|
|
126
|
+
place; a row that hands the item to a component, which takes its props once,
|
|
127
|
+
is built again from the new item.
|
|
128
|
+
|
|
129
|
+
### Moved imports
|
|
130
|
+
|
|
131
|
+
Every script module the plugin transforms (`.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`,
|
|
132
|
+
`.tsx`, ... outside `exclude`) is checked for names the root entry
|
|
133
|
+
(`what-framework`, `what-core`) no longer exports, in imports and re-exports
|
|
134
|
+
alike. One fails the build, and the dev server's transform, with
|
|
135
|
+
`ERR_MOVED_IMPORT` at the first such name's `path:line:column`, listing each
|
|
136
|
+
name with where it went or what replaces it: the answer
|
|
137
|
+
[UPGRADING.md](../what/UPGRADING.md) and eslint-plugin-what's
|
|
138
|
+
`no-moved-imports` rule give, which `eslint --fix` applies. Type-only imports,
|
|
139
|
+
subpath imports and import statements quoted in a string are left alone. It
|
|
140
|
+
has no option: the bundler would fail on the same import, with only "is not
|
|
141
|
+
exported".
|
|
142
|
+
|
|
143
|
+
### Server builds
|
|
144
|
+
|
|
145
|
+
Modules Vite transforms for the server (`vite build --ssr`, SSR in the dev
|
|
146
|
+
server, the `ssr` environment) are compiled with the server target: string
|
|
147
|
+
templates that every what-server renderer accepts (`renderToString`,
|
|
148
|
+
`renderToHydratableString`, `renderToStream`, `renderDocument`, ...), so the
|
|
149
|
+
same components render on both sides. `ssr: false` compiles server transforms
|
|
150
|
+
as client output and refuses one that would build DOM with
|
|
151
|
+
`ERR_COMPILED_JSX_IN_SSR`; `ssrGuard: false` compiles every server transform
|
|
152
|
+
for the DOM, with or without `ssr: false`, for a test runner that applies SSR
|
|
153
|
+
transforms in a process with a DOM.
|
|
154
|
+
|
|
155
|
+
Compiled server JSX means what the same JSX means as `h()` calls: a plain hole
|
|
156
|
+
(`{label}`, `id={props.id}`) is read when the JSX expression runs, a reactive one
|
|
157
|
+
(`{count()}`, `title={t()}`) is rendered where the markup is placed, and a hole
|
|
158
|
+
that throws or suspends is handled as `h()` handles it, in every renderer. A
|
|
159
|
+
string is always escaped, including one a `.map()` or `<For>` row returns; only
|
|
160
|
+
compiled markup is written as HTML. The text of `<script>` and `<style>` is
|
|
161
|
+
written as it is, with the sequences that could end the element encoded.
|
|
162
|
+
|
|
163
|
+
The dev server's output carries component, signal and binding names with their source
|
|
164
|
+
locations (`"Row@src/App.jsx:12:5"`) for devtools; production builds do not.
|
|
165
|
+
|
|
37
166
|
## Babel Plugin
|
|
38
167
|
|
|
39
168
|
Use the Babel plugin directly for custom build setups:
|
|
@@ -41,26 +170,59 @@ Use the Babel plugin directly for custom build setups:
|
|
|
41
170
|
```js
|
|
42
171
|
// babel.config.js
|
|
43
172
|
export default {
|
|
173
|
+
parserOpts: { plugins: ['jsx'] },
|
|
44
174
|
plugins: [
|
|
45
175
|
['what-compiler/babel', { production: false }],
|
|
46
176
|
],
|
|
47
177
|
};
|
|
48
178
|
```
|
|
49
179
|
|
|
180
|
+
| Option | Default | |
|
|
181
|
+
|---|---|---|
|
|
182
|
+
| `generate` | `'dom'` | `'dom'`: cloned templates and fine-grained bindings. `'ssr'`: server string templates, imported from `what-framework/ssr-runtime`. |
|
|
183
|
+
| `production` | `false` | No development warnings or metadata. |
|
|
184
|
+
| `devMetadata` | `false` | Development only: names and `path:line:column` locations for components, What's `signal`/`effect`, and bindings. Format in `babel.d.ts`. |
|
|
185
|
+
| `projectRoot` | Babel's cwd | Root that locations and server action ids are relative to. |
|
|
186
|
+
| `autoSelector` | `true` | DOM target: `selected() === row.id` in keyed rows becomes a `createSelector` lookup. |
|
|
187
|
+
| `verbose` | `false` | Development: print notes on correct code (each auto-selector rewrite, that `<For>` and a keyed `.map()` compile alike) once per file. Warnings print either way. |
|
|
188
|
+
| `onActionId` | none | Called with every server action id. |
|
|
189
|
+
|
|
190
|
+
Both targets reject markup the HTML parser would restructure (a `<div>` in a
|
|
191
|
+
`<p>`, a row directly in a `<table>`, ...) with `ERR_INVALID_HTML_NESTING` at
|
|
192
|
+
`path:line:column`; the server target also checks the JSX it writes into its
|
|
193
|
+
parent's markup (`<p>{ok && <div/>}</p>`, the rows of
|
|
194
|
+
`<table>{rows.map(r => <tr/>)}</table>`). Both targets reject static values the
|
|
195
|
+
runtime refuses on every path (a `javascript:`, `vbscript:` or `data:` URL,
|
|
196
|
+
`srcdoc`, `outerHTML`, a string `innerHTML`, `<script>`/`<style>` text that could
|
|
197
|
+
end the element, a namespaced tag such as `<svg:rect>`) with
|
|
198
|
+
`ERR_UNSAFE_STATIC_MARKUP`, and JSX as an island prop with
|
|
199
|
+
`ERR_JSX_ISLAND_PROP`, since island props are serialized to JSON.
|
|
200
|
+
|
|
50
201
|
## File-Based Routing
|
|
51
202
|
|
|
52
203
|
The compiler scans a pages directory and generates a virtual routes module.
|
|
53
204
|
|
|
54
205
|
```
|
|
55
206
|
src/pages/
|
|
207
|
+
_layout.jsx -> the layout around every page
|
|
56
208
|
index.jsx -> /
|
|
57
209
|
about.jsx -> /about
|
|
58
210
|
blog/
|
|
211
|
+
_layout.jsx -> the layout around every page under /blog, inside the root's
|
|
59
212
|
index.jsx -> /blog
|
|
60
213
|
[slug].jsx -> /blog/:slug
|
|
214
|
+
(auth)/
|
|
215
|
+
_layout.jsx -> the layout around the group's pages only, inside the root's
|
|
216
|
+
login.jsx -> /login
|
|
61
217
|
[...all].jsx -> /*
|
|
62
218
|
```
|
|
63
219
|
|
|
220
|
+
A `_layout` goes around the pages of its directory and the directories below
|
|
221
|
+
it, nested in the layouts of the directories above, as Next.js's nested
|
|
222
|
+
layouts do: `/blog/:slug` renders inside the root layout, then the blog
|
|
223
|
+
layout. Moving between two pages keeps the layouts they share (`/blog` to
|
|
224
|
+
`/blog/hello` keeps both; `/about` to `/blog` keeps the root's) with their state.
|
|
225
|
+
|
|
64
226
|
Import the generated routes in your app:
|
|
65
227
|
|
|
66
228
|
```jsx
|
|
@@ -71,6 +233,23 @@ import { mount } from 'what-framework';
|
|
|
71
233
|
mount(<FileRouter routes={routes} />, '#app');
|
|
72
234
|
```
|
|
73
235
|
|
|
236
|
+
A page may export `preload` and `middleware`, which are its route's as on a
|
|
237
|
+
route object (what-router): the navigation waits for what `preload` returns,
|
|
238
|
+
and `middleware` decides whether the page shows. Both are in the browser's
|
|
239
|
+
module; a `loader` never is.
|
|
240
|
+
|
|
241
|
+
A server entry imports `virtual:what-routes/server`: the same routes, each with
|
|
242
|
+
its page's `loader`, `getStaticPaths` and `page` config as live bindings, which
|
|
243
|
+
the browser's module leaves out so no loader reaches the client bundle.
|
|
244
|
+
|
|
245
|
+
```jsx
|
|
246
|
+
// src/entry-server.jsx (vite build --ssr src/entry-server.jsx)
|
|
247
|
+
import { routes as pages } from 'virtual:what-routes/server';
|
|
248
|
+
import App from './App.jsx'; // renders <FileRouter routes={routes} />
|
|
249
|
+
|
|
250
|
+
export const routes = pages.map((r) => ({ path: r.path, component: App, loader: r.loader, page: r.page, mode: r.mode }));
|
|
251
|
+
```
|
|
252
|
+
|
|
74
253
|
## Sub-path Exports
|
|
75
254
|
|
|
76
255
|
| Path | Contents |
|
package/babel.d.ts
CHANGED
|
@@ -1,5 +1,81 @@
|
|
|
1
|
-
// what-compiler/babel — Babel plugin that
|
|
2
|
-
//
|
|
1
|
+
// what-compiler/babel — Babel plugin that lowers What JSX (src/babel-plugin.js).
|
|
2
|
+
// Add to a Babel config's `plugins`, with the `jsx` parser plugin enabled.
|
|
3
|
+
|
|
4
|
+
export interface ServerActionIdMetadata {
|
|
5
|
+
id: string;
|
|
6
|
+
key: string;
|
|
7
|
+
bindingName: string;
|
|
8
|
+
filename: string;
|
|
9
|
+
/** "path:line:column" of the action() call. */
|
|
10
|
+
source: string;
|
|
11
|
+
loc: { line: number; column: number };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface WhatBabelPluginOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Which back end lowers JSX. 'dom' (the default) emits cloned templates and
|
|
17
|
+
* fine-grained bindings (what-framework/render). 'ssr' emits server string
|
|
18
|
+
* templates whose holes are evaluated as h() evaluates its arguments,
|
|
19
|
+
* rendered by every what-server renderer (what-framework/ssr-runtime). The
|
|
20
|
+
* Vite plugin picks 'ssr' for server transforms.
|
|
21
|
+
*/
|
|
22
|
+
generate?: 'dom' | 'ssr';
|
|
23
|
+
/** Production build: no development warnings and no development metadata. */
|
|
24
|
+
production?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Development builds only (ignored when `production` is true): pass names
|
|
27
|
+
* and source locations to the runtime. Every string has the form
|
|
28
|
+
* "name@path:line:column" (path relative to `projectRoot`, forward slashes,
|
|
29
|
+
* 1-based line and column, positions of the original source):
|
|
30
|
+
*
|
|
31
|
+
* _$createComponent(Row, props, children, "Row@src/App.jsx:12:5")
|
|
32
|
+
* _$componentVNode(App, props, children, "App@src/main.jsx:4:9")
|
|
33
|
+
* _$ssrComponent(Row, props, children, "Row@src/App.jsx:12:5") (generate: 'ssr')
|
|
34
|
+
* signal(0) -> signal(0, "count@src/store.js:3:15") (What's signal, unnamed)
|
|
35
|
+
* effect(fn) -> effect(fn, { name: "effect@src/store.js:9:1" }) (What's effect)
|
|
36
|
+
* _$effect(() => ..., { name: "li.title@src/App.jsx:14:9" }) (attribute bindings)
|
|
37
|
+
* _$insert(el, value, marker, "li.children@src/App.jsx:15:11") (child bindings)
|
|
38
|
+
*
|
|
39
|
+
* The name part is the component's JSX tag, the variable or property a
|
|
40
|
+
* signal or effect is assigned to (else the primitive's name), and
|
|
41
|
+
* "tag.prop" / "tag.children" for bindings. A production build is
|
|
42
|
+
* byte-identical with the option on or off. Defaults to false; the Vite
|
|
43
|
+
* plugin turns it on in the dev server (`vite`), never in `vite build`.
|
|
44
|
+
*/
|
|
45
|
+
devMetadata?: boolean;
|
|
46
|
+
/** Root that source locations and server action ids are relative to. Defaults to Babel's cwd. */
|
|
47
|
+
projectRoot?: string;
|
|
48
|
+
/**
|
|
49
|
+
* DOM back end: rewrite `selected() === row.id` inside keyed `.map()` rows to
|
|
50
|
+
* a createSelector lookup. Defaults to true.
|
|
51
|
+
*/
|
|
52
|
+
autoSelector?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Development builds: print the compiler's notes on correct code with
|
|
55
|
+
* `console.info` (each comparison the auto-selector rewrote, and that a
|
|
56
|
+
* `<For>` compiles to the same keyed rows as a `.map()` with a key), once
|
|
57
|
+
* per file. Warnings about code that misbehaves print either way. Defaults
|
|
58
|
+
* to false.
|
|
59
|
+
*/
|
|
60
|
+
verbose?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* DOM back end: output that `hydrate()` adopts server-rendered markup with.
|
|
63
|
+
* Each template claims the node the hydration walk stands on instead of
|
|
64
|
+
* cloning one, and is reached in document order rather than by path.
|
|
65
|
+
* Defaults to false: such output still renders on the client, a little
|
|
66
|
+
* larger and slower than the default.
|
|
67
|
+
*/
|
|
68
|
+
hydratable?: boolean;
|
|
69
|
+
/** Called with every server action id the plugin assigns or reads. Throwing fails the build. */
|
|
70
|
+
onActionId?: (metadata: ServerActionIdMetadata) => void;
|
|
71
|
+
/**
|
|
72
|
+
* Compile a file one of What's own packages publishes as the app's source.
|
|
73
|
+
* Such a file is the framework: by default only its JSX is compiled, with no
|
|
74
|
+
* development names, no server action ids, and no function of it without
|
|
75
|
+
* JSX taken for a component. Defaults to false.
|
|
76
|
+
*/
|
|
77
|
+
appSource?: boolean;
|
|
78
|
+
}
|
|
3
79
|
|
|
4
80
|
export interface BabelPluginPass {
|
|
5
81
|
name?: string;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/babel/auto-selector.js
|
|
2
|
+
import { t as r } from "./types.min.js";
|
|
3
|
+
import { collectPatternNames as A, namesInvisibleToKeyFn as J } from "./lists.min.js";
|
|
4
|
+
import { controlFlowTag as O, getAttrName as y, helper as X, isComponentElement as b, isEventAttrName as k, uniqueName as x } from "./common.min.js";
|
|
5
|
+
var S = /* @__PURE__ */ new Set(), L = /@what-no-selector\b/;
|
|
6
|
+
function v(c) {
|
|
7
|
+
return !c || typeof c != "object" ? !1 : [c.leadingComments, c.innerComments].some((s) => s && s.some((a) => L.test(a.value)));
|
|
8
|
+
}
|
|
9
|
+
function B(c, p, s) {
|
|
10
|
+
let a = J(p);
|
|
11
|
+
for (let e of p.params) A(e, a);
|
|
12
|
+
let m = (e) => {
|
|
13
|
+
if (!r.isCallExpression(e) || e.arguments.length !== 0) return null;
|
|
14
|
+
let i = e.callee;
|
|
15
|
+
if (r.isMemberExpression(i) && !i.computed && r.isIdentifier(i.property) && r.isIdentifier(i.object) && !a.has(i.object.name) && s.signalNames && s.signalNames.propsObjects && s.signalNames.propsObjects.has(i.object.name)) {
|
|
16
|
+
let n = s._jsxScope && s._jsxScope.getBinding(i.object.name);
|
|
17
|
+
return n && n.kind === "param" && n.constant && n.path.parentPath.node._whatPropsPattern === !0 ? `${i.object.name}.${i.property.name}` : null;
|
|
18
|
+
}
|
|
19
|
+
if (!r.isIdentifier(i)) return null;
|
|
20
|
+
let o = i.name;
|
|
21
|
+
if (a.has(o) || !(s.signalNames && s.signalNames.has(o) || s.importedIdentifiers && s.importedIdentifiers.has(o))) return null;
|
|
22
|
+
let l = s._jsxScope && s._jsxScope.getBinding(o);
|
|
23
|
+
return l && l.constant ? o : null;
|
|
24
|
+
}, d = (e) => {
|
|
25
|
+
let i = !1, o = (t) => r.isIdentifier(t) ? (a.has(t.name) && (i = !0), !0) : r.isMemberExpression(t) || r.isOptionalMemberExpression(t) ? o(t.object) && (!t.computed || o(t.property)) : r.isStringLiteral(t) || r.isNumericLiteral(t) || r.isBooleanLiteral(t) || r.isNullLiteral(t) || r.isBigIntLiteral(t) ? !0 : r.isTemplateLiteral(t) ? t.expressions.every(o) : r.isUnaryExpression(t) ? t.operator !== "delete" && o(t.argument) : r.isBinaryExpression(t) ? /^(?:[-+*/%]|\*\*)$/.test(t.operator) && o(t.left) && o(t.right) : !1;
|
|
26
|
+
return o(e) && i;
|
|
27
|
+
}, h = s.signalNames && s.signalNames.fromDestructuredProps, w = (e) => s.signalNames.has(e) && !(h && h.has(e)) && !(s.importedIdentifiers && s.importedIdentifiers.has(e)), N = (e) => {
|
|
28
|
+
let i = s._selectorIds.get(e);
|
|
29
|
+
if (i === void 0) {
|
|
30
|
+
i = x(s, "_sel$"), s._selectorIds.set(e, i);
|
|
31
|
+
let o = () => {
|
|
32
|
+
let [n, E] = e.split(".");
|
|
33
|
+
return E === void 0 ? r.identifier(e) : r.memberExpression(r.identifier(n), r.identifier(E));
|
|
34
|
+
}, t = r.callExpression(X(s, "_$createSelector"), [o()]), l = t;
|
|
35
|
+
if (e.includes(".") || !w(e)) {
|
|
36
|
+
let n = r.identifier(x(s, "_k$"));
|
|
37
|
+
l = r.conditionalExpression(
|
|
38
|
+
r.binaryExpression("===", r.unaryExpression("typeof", o()), r.stringLiteral("function")),
|
|
39
|
+
t,
|
|
40
|
+
r.arrowFunctionExpression([n], r.binaryExpression(
|
|
41
|
+
"===",
|
|
42
|
+
r.callExpression(o(), []),
|
|
43
|
+
r.cloneNode(n)
|
|
44
|
+
))
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
s._selectorDecls.push(r.variableDeclaration("const", [r.variableDeclarator(r.identifier(i), l)]));
|
|
48
|
+
}
|
|
49
|
+
return i;
|
|
50
|
+
}, _ = (e, i) => {
|
|
51
|
+
let o = e.loc ? e.loc.start : null, t = s.file && typeof s.file.code == "string" && e.end != null ? s.file.code.slice(e.start, e.end) : `${i}() === \u2026`, l = { source: i, comparison: t, line: o ? o.line : null, column: o ? o.column : null };
|
|
52
|
+
s.file && s.file.metadata && (s.file.metadata.whatAutoSelectors ||= []).push(l);
|
|
53
|
+
}, j = (e) => r.isArrowFunctionExpression(e) && e.params.length === 0 && r.isExpression(e.body), u = (e) => {
|
|
54
|
+
if (!e || typeof e != "object" || v(e) || r.isFunction(e)) return e;
|
|
55
|
+
if (r.isJSXElement(e) || r.isJSXFragment(e))
|
|
56
|
+
return f(e), e;
|
|
57
|
+
if (r.isBinaryExpression(e) && (e.operator === "===" || e.operator === "!==")) {
|
|
58
|
+
let i = m(e.left), o = e.right;
|
|
59
|
+
if ((!i || !d(o)) && (i = m(e.right), o = e.left, i && !d(o) && (i = null)), i) {
|
|
60
|
+
_(e, i);
|
|
61
|
+
let t = r.identifier(N(i));
|
|
62
|
+
t._whatSelector = !0;
|
|
63
|
+
let l = r.callExpression(t, [o]);
|
|
64
|
+
return e.operator === "===" ? l : r.unaryExpression("!", l);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (let i of r.VISITOR_KEYS[e.type] || []) {
|
|
68
|
+
let o = e[i];
|
|
69
|
+
if (Array.isArray(o))
|
|
70
|
+
for (let t = 0; t < o.length; t++) o[t] = u(o[t]);
|
|
71
|
+
else o && typeof o == "object" && (e[i] = u(o));
|
|
72
|
+
}
|
|
73
|
+
return e;
|
|
74
|
+
}, g = (e) => j(e) && !v(e) ? (e.body = u(e.body), e) : u(e), I = (e) => {
|
|
75
|
+
for (let i of e)
|
|
76
|
+
r.isJSXElement(i) || r.isJSXFragment(i) ? f(i) : r.isJSXExpressionContainer(i) && !r.isJSXEmptyExpression(i.expression) && (i.expression = g(i.expression));
|
|
77
|
+
}, $ = (e) => O(e, s) !== null ? !1 : !e.openingElement.attributes.some((i) => r.isJSXAttribute(i) && y(i).startsWith("client:"));
|
|
78
|
+
function f(e) {
|
|
79
|
+
if (r.isJSXElement(e) && (!b(e) || $(e))) {
|
|
80
|
+
let i = b(e);
|
|
81
|
+
for (let o of e.openingElement.attributes) {
|
|
82
|
+
if (!r.isJSXAttribute(o) || !r.isJSXExpressionContainer(o.value)) continue;
|
|
83
|
+
let t = y(o);
|
|
84
|
+
t === "key" || t === "ref" || k(t) || t.includes(":") || t.includes("|") || i && (t === "children" || r.isFunction(o.value.expression)) || (o.value.expression = i ? u(o.value.expression) : g(o.value.expression));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
I(e.children);
|
|
88
|
+
}
|
|
89
|
+
f(c);
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
v as hasNoSelectorPragma,
|
|
93
|
+
B as lowerRowSelectors
|
|
94
|
+
};
|