storybook-nitroui 0.2.2 → 0.3.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/README.md +65 -12
- package/package.json +19 -17
- package/src/builder/changeDetection.ts +141 -0
- package/src/builder/codegen.ts +2 -0
- package/src/builder/index.ts +24 -3
- package/src/cli.ts +5 -2
- package/src/device/ManagerStore.ts +1 -1
- package/src/device/NitroView.ts +30 -6
- package/src/device/PreviewNitro.ts +24 -8
- package/src/device/hooks.ts +4 -4
- package/src/device/index.ts +23 -23
- package/src/device/runtime.ts +20 -15
- package/src/device/shims.ts +14 -1
- package/src/device/symbolicate.ts +93 -0
- package/src/device/ui/ArgsPanel.tsx +9 -9
- package/src/device/ui/DefaultManager.tsx +4 -4
- package/src/device/ui/Inspector.tsx +25 -14
- package/src/device/ui/StoryCanvas.tsx +56 -12
- package/src/device/ui/Storybook.tsx +8 -8
- package/src/device/ui/actions/ActionsPanel.tsx +6 -6
- package/src/device/ui/actions/actionsStore.ts +2 -2
- package/src/device/ui/controls/ArgRow.tsx +8 -8
- package/src/device/ui/controls/Field.tsx +1 -1
- package/src/device/ui/controls/ObjectControl.tsx +40 -19
- package/src/device/ui/controls/nativeControls.tsx +11 -9
- package/src/device/ui/controls.tsx +1 -1
- package/src/device/ui/globals/GlobalsPanel.tsx +14 -11
- package/src/device/ui/icons/managerIcons.ts +1 -1
- package/src/device/ui/icons/storybookIcons.ts +2 -1
- package/src/device/ui/layout/DesktopLayout.tsx +21 -17
- package/src/device/ui/layout/PhoneLayout.tsx +26 -19
- package/src/device/ui/layout/shared.tsx +16 -8
- package/src/device/ui/perf.ts +1 -4
- package/src/device/ui/preview/annotations.tsx +3 -3
- package/src/device/ui/preview/canvasInsets.ts +1 -1
- package/src/device/ui/primitives.tsx +31 -14
- package/src/device/ui/session.ts +2 -2
- package/src/device/ui/sidebar/Sidebar.tsx +6 -6
- package/src/device/ui/sidebar/StoryTree.tsx +72 -13
- package/src/device/virtual.d.ts +8 -0
- package/src/frameworkOptions.ts +5 -2
- package/src/index.ts +3 -3
- package/src/launch/app.ts +12 -1
- package/src/launch/init.ts +88 -25
- package/src/launch/net.ts +3 -0
- package/src/launch/run.ts +12 -10
- package/src/launch/tsconfig.ts +116 -0
- package/src/mcp/toolset.ts +7 -7
- package/src/preset.ts +27 -1
- package/src/public-types.ts +1 -1
- package/src/remote/device.ts +31 -9
- package/src/remote/installId.ts +4 -0
- package/src/remote/protocol.ts +10 -0
- package/src/renderer/applyDecorators.ts +1 -1
- package/src/renderer/entry-preview.ts +4 -4
- package/src/renderer/mount.ts +1 -1
- package/src/renderer/render.tsx +1 -1
- package/src/renderer/renderToCanvas.tsx +7 -2
- package/src/renderer/storyErrors.ts +46 -0
- package/src/renderer/types.ts +1 -1
- package/src/server/dev-server.ts +3 -2
- package/src/server/host-devices.ts +116 -0
- package/src/server/openInEditor.ts +29 -0
- package/src/server/proxy-iframe.ts +18 -4
- package/src/server/relay.ts +63 -15
- package/src/server/remote.ts +3 -2
- package/src/server/screenshot.ts +2 -37
- package/src/test-runner/cli.ts +33 -12
- package/src/test-runner/run.ts +4 -4
- package/types/device/ManagerStore.d.ts +43 -0
- package/types/device/ManagerStore.d.ts.map +1 -0
- package/types/device/MemorySelectionStore.d.ts +21 -0
- package/types/device/MemorySelectionStore.d.ts.map +1 -0
- package/types/device/NitroView.d.ts +48 -0
- package/types/device/NitroView.d.ts.map +1 -0
- package/types/device/PreviewNitro.d.ts +22 -0
- package/types/device/PreviewNitro.d.ts.map +1 -0
- package/types/device/Store.d.ts +12 -0
- package/types/device/Store.d.ts.map +1 -0
- package/types/device/hooks.d.ts +28 -0
- package/types/device/hooks.d.ts.map +1 -0
- package/types/device/index.d.ts +24 -0
- package/types/device/index.d.ts.map +1 -0
- package/types/device/runtime.d.ts +31 -0
- package/types/device/runtime.d.ts.map +1 -0
- package/types/device/shims.d.ts +2 -0
- package/types/device/shims.d.ts.map +1 -0
- package/types/device/symbolicate.d.ts +28 -0
- package/types/device/symbolicate.d.ts.map +1 -0
- package/types/device/ui/ArgsPanel.d.ts +12 -0
- package/types/device/ui/ArgsPanel.d.ts.map +1 -0
- package/types/device/ui/DefaultManager.d.ts +16 -0
- package/types/device/ui/DefaultManager.d.ts.map +1 -0
- package/types/device/ui/Inspector.d.ts +17 -0
- package/types/device/ui/Inspector.d.ts.map +1 -0
- package/types/device/ui/StoryCanvas.d.ts +13 -0
- package/types/device/ui/StoryCanvas.d.ts.map +1 -0
- package/types/device/ui/Storybook.d.ts +20 -0
- package/types/device/ui/Storybook.d.ts.map +1 -0
- package/types/device/ui/actions/ActionsPanel.d.ts +6 -0
- package/types/device/ui/actions/ActionsPanel.d.ts.map +1 -0
- package/types/device/ui/actions/actionsStore.d.ts +20 -0
- package/types/device/ui/actions/actionsStore.d.ts.map +1 -0
- package/types/device/ui/controls/ArgRow.d.ts +17 -0
- package/types/device/ui/controls/ArgRow.d.ts.map +1 -0
- package/types/device/ui/controls/Field.d.ts +27 -0
- package/types/device/ui/controls/Field.d.ts.map +1 -0
- package/types/device/ui/controls/ObjectControl.d.ts +3 -0
- package/types/device/ui/controls/ObjectControl.d.ts.map +1 -0
- package/types/device/ui/controls/argRows.d.ts +56 -0
- package/types/device/ui/controls/argRows.d.ts.map +1 -0
- package/types/device/ui/controls/color.d.ts +3 -0
- package/types/device/ui/controls/color.d.ts.map +1 -0
- package/types/device/ui/controls/nativeControls.d.ts +17 -0
- package/types/device/ui/controls/nativeControls.d.ts.map +1 -0
- package/types/device/ui/controls/useDebouncedCommit.d.ts +6 -0
- package/types/device/ui/controls/useDebouncedCommit.d.ts.map +1 -0
- package/types/device/ui/controls.d.ts +34 -0
- package/types/device/ui/controls.d.ts.map +1 -0
- package/types/device/ui/globals/GlobalsPanel.d.ts +9 -0
- package/types/device/ui/globals/GlobalsPanel.d.ts.map +1 -0
- package/types/device/ui/icons/Icon.d.ts +18 -0
- package/types/device/ui/icons/Icon.d.ts.map +1 -0
- package/types/device/ui/icons/managerIcons.d.ts +14 -0
- package/types/device/ui/icons/managerIcons.d.ts.map +1 -0
- package/types/device/ui/icons/storybookIcons.d.ts +24 -0
- package/types/device/ui/icons/storybookIcons.d.ts.map +1 -0
- package/types/device/ui/layout/DesktopLayout.d.ts +8 -0
- package/types/device/ui/layout/DesktopLayout.d.ts.map +1 -0
- package/types/device/ui/layout/PhoneLayout.d.ts +10 -0
- package/types/device/ui/layout/PhoneLayout.d.ts.map +1 -0
- package/types/device/ui/layout/shared.d.ts +20 -0
- package/types/device/ui/layout/shared.d.ts.map +1 -0
- package/types/device/ui/perf.d.ts +10 -0
- package/types/device/ui/perf.d.ts.map +1 -0
- package/types/device/ui/preview/annotations.d.ts +20 -0
- package/types/device/ui/preview/annotations.d.ts.map +1 -0
- package/types/device/ui/preview/canvasInsets.d.ts +21 -0
- package/types/device/ui/preview/canvasInsets.d.ts.map +1 -0
- package/types/device/ui/preview/environment.d.ts +62 -0
- package/types/device/ui/preview/environment.d.ts.map +1 -0
- package/types/device/ui/primitives.d.ts +66 -0
- package/types/device/ui/primitives.d.ts.map +1 -0
- package/types/device/ui/session.d.ts +17 -0
- package/types/device/ui/session.d.ts.map +1 -0
- package/types/device/ui/sidebar/Sidebar.d.ts +16 -0
- package/types/device/ui/sidebar/Sidebar.d.ts.map +1 -0
- package/types/device/ui/sidebar/StoryTree.d.ts +14 -0
- package/types/device/ui/sidebar/StoryTree.d.ts.map +1 -0
- package/types/device/ui/sidebar/coldListBind.d.ts +3 -0
- package/types/device/ui/sidebar/coldListBind.d.ts.map +1 -0
- package/types/device/ui/sidebar/tree.d.ts +76 -0
- package/types/device/ui/sidebar/tree.d.ts.map +1 -0
- package/types/device/ui/theme.d.ts +98 -0
- package/types/device/ui/theme.d.ts.map +1 -0
- package/types/frameworkOptions.d.ts +50 -0
- package/types/frameworkOptions.d.ts.map +1 -0
- package/types/index.d.ts +4 -0
- package/types/index.d.ts.map +1 -0
- package/types/public-types.d.ts +21 -0
- package/types/public-types.d.ts.map +1 -0
- package/types/remote/device.d.ts +68 -0
- package/types/remote/device.d.ts.map +1 -0
- package/types/remote/installId.d.ts +7 -0
- package/types/remote/installId.d.ts.map +1 -0
- package/types/remote/protocol.d.ts +169 -0
- package/types/remote/protocol.d.ts.map +1 -0
- package/types/renderer/CanvasRoot.d.ts +37 -0
- package/types/renderer/CanvasRoot.d.ts.map +1 -0
- package/types/renderer/applyDecorators.d.ts +4 -0
- package/types/renderer/applyDecorators.d.ts.map +1 -0
- package/types/renderer/entry-preview.d.ts +7 -0
- package/types/renderer/entry-preview.d.ts.map +1 -0
- package/types/renderer/mount.d.ts +4 -0
- package/types/renderer/mount.d.ts.map +1 -0
- package/types/renderer/render.d.ts +4 -0
- package/types/renderer/render.d.ts.map +1 -0
- package/types/renderer/renderToCanvas.d.ts +4 -0
- package/types/renderer/renderToCanvas.d.ts.map +1 -0
- package/types/renderer/storyErrors.d.ts +25 -0
- package/types/renderer/storyErrors.d.ts.map +1 -0
- package/types/renderer/types.d.ts +31 -0
- package/types/renderer/types.d.ts.map +1 -0
package/README.md
CHANGED
|
@@ -9,27 +9,36 @@ Pinned to `storybook@11.0.1` and NitroUI `0.3.0`.
|
|
|
9
9
|
|
|
10
10
|
## Setup
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
```sh
|
|
13
|
+
bunx storybook-nitroui@latest init
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`init` writes the three `.storybook` files below, adds the package scripts and adds `storybook-static` and
|
|
17
|
+
`*storybook.log` to the app's `.gitignore` if it has one. In an app without stories, it also adds an example `Button` with stories in
|
|
18
|
+
`src/stories/`. Then it installs the packages with the package manager of the app's lockfile (Bun, npm, pnpm
|
|
19
|
+
or Yarn). With `--skip-install` it prints the command instead. With Bun, that is:
|
|
14
20
|
|
|
15
21
|
```sh
|
|
16
|
-
bun add storybook-nitroui-controls storybook-nitroui-containers nitro-mmkv
|
|
17
|
-
bun add -
|
|
18
|
-
typescript@npm:@typescript/typescript6@^6.0.2 @typescript/native@npm:typescript@7.0.2
|
|
22
|
+
bun add --exact storybook-nitroui-controls@<version> storybook-nitroui-containers@<version> nitro-mmkv@0.3.0
|
|
23
|
+
bun add -D --exact storybook@11.0.1 storybook-nitroui@<version>
|
|
19
24
|
```
|
|
20
25
|
|
|
21
26
|
The first three contain native views, so they must be direct dependencies of the app for `nitro-ui prepare`
|
|
22
27
|
to link them. Rebuild the app after adding them.
|
|
23
28
|
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
Docgen uses the app's `typescript` when it has a TypeScript API: the compiler API (TypeScript 6) or the
|
|
30
|
+
native API (TypeScript 7.1 and newer). TypeScript 7.0, which new NitroUI apps get, has neither, so docgen
|
|
31
|
+
uses the TypeScript 6 that `storybook-nitroui` depends on. The app's `typescript` and `tsc` stay as they
|
|
32
|
+
are. To pick the TypeScript docgen uses, set Storybook's
|
|
33
|
+
[`typescript.docgenSpecifier`](https://storybook.js.org/docs/api/main-config/main-config-typescript) in
|
|
34
|
+
`main.ts`.
|
|
26
35
|
|
|
27
36
|
`.storybook/main.ts`:
|
|
28
37
|
|
|
29
38
|
```ts
|
|
30
39
|
import type { StorybookConfig } from 'storybook-nitroui'
|
|
31
40
|
|
|
32
|
-
const config: StorybookConfig
|
|
41
|
+
const config: StorybookConfig = {
|
|
33
42
|
stories: ['../src/**/*.stories.tsx'],
|
|
34
43
|
framework: { name: 'storybook-nitroui', options: {} },
|
|
35
44
|
}
|
|
@@ -59,6 +68,28 @@ export default meta
|
|
|
59
68
|
export const Primary: StoryObj<typeof meta> = { args: { variant: 'primary' } }
|
|
60
69
|
```
|
|
61
70
|
|
|
71
|
+
`init` also adds `.storybook/**/*` to the `include` in `tsconfig.json`, so the app's `typecheck` covers
|
|
72
|
+
Storybook's files. NitroUI's template includes only `src`, and TypeScript skips folders that start with a
|
|
73
|
+
dot unless a pattern names them, so a bare `.storybook` matches nothing. If the tsconfig has no `include`
|
|
74
|
+
(TypeScript then uses its defaults), init leaves it alone and prints the pattern to add:
|
|
75
|
+
|
|
76
|
+
```json
|
|
77
|
+
{ "extends": "nitro-ui-cli/tsconfig.json", "include": ["src", ".storybook/**/*"] }
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The three packages run from their TypeScript sources, as NitroUI's packages do: NitroUI's bundler compiles
|
|
81
|
+
them into the app's bundle. For type-checking they publish declaration files (`types/`, generated from the
|
|
82
|
+
sources under NitroUI's base config), and `package.json` points TypeScript at those. The app's compiler
|
|
83
|
+
options therefore don't reach the packages' internals, and stricter flags such as `noUncheckedIndexedAccess`,
|
|
84
|
+
`exactOptionalPropertyTypes` and `noPropertyAccessFromIndexSignature` report no errors in them while
|
|
85
|
+
`skipLibCheck` is on (NitroUI's base config sets it). NitroUI's own packages (`nitro-ui-renderer`,
|
|
86
|
+
`nitro-ui-navigation`, …) still ship only sources, so those flags report errors inside them, with or without
|
|
87
|
+
Storybook. The declarations use extensionless relative imports, so they need `"moduleResolution": "bundler"`,
|
|
88
|
+
which NitroUI's base config uses.
|
|
89
|
+
|
|
90
|
+
`main.ts` runs in Node, but this config types `process.env` for the NitroUI bundle (as a string). Read
|
|
91
|
+
environment variables through `process.env as unknown as Record<string, string | undefined>`.
|
|
92
|
+
|
|
62
93
|
## Running
|
|
63
94
|
|
|
64
95
|
```sh
|
|
@@ -79,6 +110,21 @@ next free port; `nitro-ui start` keeps 8081). Launch the installed Debug app aga
|
|
|
79
110
|
- On macOS the server also advertises itself over Bonjour (`_nitroui._tcp`) as "<project> Storybook", so it
|
|
80
111
|
shows in the NitroUI dev launcher's server list.
|
|
81
112
|
|
|
113
|
+
### Open in editor
|
|
114
|
+
|
|
115
|
+
NitroUI's dev error sheet (the error pill, then the source card or a stack frame) can open the source
|
|
116
|
+
in your editor, as it does under `nitro-ui start`. The device dev server opens it with
|
|
117
|
+
[`launch-editor`](https://github.com/vitejs/launch-editor), the same package NitroUI and web Storybook
|
|
118
|
+
use: it uses the editor in `LAUNCH_EDITOR` (for example `LAUNCH_EDITOR=cursor`), or else guesses from the
|
|
119
|
+
running editors, then `VISUAL` and `EDITOR`. Set it in the environment that starts Storybook:
|
|
120
|
+
|
|
121
|
+
```sh
|
|
122
|
+
LAUNCH_EDITOR=code bunx nitro-storybook ios
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The device never sends a path, only an id that `/symbolicate` handed out for a source-mapped location,
|
|
126
|
+
so the server only opens files that are in the bundle.
|
|
127
|
+
|
|
82
128
|
Adding, renaming or deleting a story file regenerates the index and hot-updates the device. Editing a
|
|
83
129
|
story or component goes through React Refresh, and editing `preview.tsx` swaps the project annotations.
|
|
84
130
|
|
|
@@ -122,9 +168,11 @@ browser Storybook. The device stays the source of truth:
|
|
|
122
168
|
- A story picked on the web opens it on the device.
|
|
123
169
|
- Arg edits on either side show on both.
|
|
124
170
|
|
|
125
|
-
- **Several devices.** Each one is
|
|
171
|
+
- **Several devices.** Each one is identified by a `/device/<id>` suffix on its server URL. For example, launch
|
|
126
172
|
with `-NitroUIDevServerURL http://localhost:8082/device/<simulator UDID>`, or `<adb serial>` on Android. The
|
|
127
|
-
proxy shows a device picker when more than one is connected, and it remembers the choice.
|
|
173
|
+
proxy shows a device picker when more than one is connected, and it remembers the choice. Devices show by
|
|
174
|
+
name ("iPhone 18 Pro · iOS"; the AVD name or phone model on Android), looked up on this computer when they
|
|
175
|
+
connect; two with the same name also show the start of their ID.
|
|
128
176
|
- **Mirror canvas.** This proxy checkbox shows a screenshot of the native canvas after each render. It works
|
|
129
177
|
only on simulators and emulators.
|
|
130
178
|
- **Idle cost.** The app opens the relay socket at startup. Until a web preview, an agent or a test run attaches,
|
|
@@ -156,7 +204,7 @@ HTTP API (no browser Origin needed):
|
|
|
156
204
|
|
|
157
205
|
## MCP
|
|
158
206
|
|
|
159
|
-
`storybook dev` serves an MCP endpoint at `http://
|
|
207
|
+
`storybook dev` serves an MCP endpoint at `http://127.0.0.1:6006/storybook-nitroui/mcp`. Add it to your MCP
|
|
160
208
|
client as a streamable HTTP server.
|
|
161
209
|
|
|
162
210
|
Tools:
|
|
@@ -366,15 +414,17 @@ registerApp(() => <Storybook manager={MyManager} />)
|
|
|
366
414
|
|
|
367
415
|
Each is marked with a `// TODO:` in the source.
|
|
368
416
|
|
|
369
|
-
- NitroUI's dev server has no plugin or route hook, so `src/server/dev-server.ts` re-implements its protocol (`/client.js`, `/app.js`, `/hmr`, `/symbolicate`, `/assets`) with Storybook's virtual modules added.
|
|
417
|
+
- NitroUI's dev server has no plugin or route hook, so `src/server/dev-server.ts` re-implements its protocol (`/client.js`, `/app.js`, `/hmr`, `/symbolicate`, `/open-in-editor`, `/assets`) with Storybook's virtual modules added.
|
|
370
418
|
- `refreshPlugin` and `optimizeNitroReexports` are not exported from `nitro-ui-bundler`. The first is copied and the second is loaded by file path.
|
|
371
419
|
- There is no API to host a second React root inside a view. `renderRoot` gets a proxy controller whose `view` is the canvas view.
|
|
372
420
|
- The console formatter `JSON.stringify`s whole objects, and Storybook objects run Hermes out of memory. Console output is bounded in `src/device/shims.ts`.
|
|
373
421
|
- Hermes lacks `EventTarget`, `Event`, `CustomEvent`, `crypto.getRandomValues`, `structuredClone` and an object-valued `process.env`, which Storybook's preview code needs. These are shimmed in `src/device/shims.ts`.
|
|
422
|
+
- `setTimeout` and `setInterval` take only `(callback, ms)` and drop extra arguments. The timer guards in `src/remote/device.ts` bind them, so they reach the callback while a story render is captured, but not otherwise.
|
|
374
423
|
- `TextInput` has no `smartQuotesType`, so raw JSON fields straighten quotes and dashes in `onBeforeTextChange`.
|
|
375
424
|
- The story root lays itself out only on its own commits, not when the canvas view moves. The canvas fills the screen, and the space the bar, sheets, sidebar and panel cover is passed to the story through a store that its environment decorator reads.
|
|
376
425
|
- NitroUI has no incoming URL API, so there are no deep links.
|
|
377
426
|
- `TextInput` has no return-key event, return key type or `keyboardDismissMode`, so the sheets in `storybook-nitroui-containers` dismiss the keyboard natively (taps and drags outside the input, Return, collapsing and closing), and a "Done" button calls `Keyboard.dismiss()`.
|
|
427
|
+
- `Text` is not selectable and has no monospace font, so the canvas error card shows its message detail and stack in a read-only native `CodeEditor`, which can be selected and copied.
|
|
378
428
|
- `TextInput` has no font, text color or placeholder color props, so the search field and text controls keep the system text style. The object control's raw JSON mode uses the native `CodeEditor` from `storybook-nitroui-controls` for a monospace font.
|
|
379
429
|
- A `borderWidth` around a `TextInput` that measures its own height breaks the layout, so text fields draw their 1 px border as padding on a border-colored wrapper.
|
|
380
430
|
- Text has no letter spacing, so section headers space their uppercase letters with thin spaces.
|
|
@@ -383,4 +433,7 @@ Each is marked with a `// TODO:` in the source.
|
|
|
383
433
|
- There are no dynamic system colors yet (#895), so the theme picks colors from `useColorScheme`.
|
|
384
434
|
- Storybook's `core/docgen` service is internal-only. It is read with `{ internal: true }`.
|
|
385
435
|
- There is no native view snapshot API, so screenshots use `simctl`/`adb` and crop to the canvas frame. They do not work on physical devices and include anything drawn over the canvas, such as the manager sheet or the error toast.
|
|
436
|
+
- NitroUI's dev client reports every `console.error` in its dev error pill, with no way to log an error without a report or to remove one report. A story's render error shows in the canvas, as on web, so its logs go to the native log only (`src/device/shims.ts`, `src/renderer/storyErrors.ts`). This gives up the pill's source card and open-in-editor for story render errors; the canvas error card shows the symbolicated stack. Other console errors and build errors still show.
|
|
437
|
+
- `View` has no accessibility props (label, role, state). Icon buttons, tabs and text buttons in the manager use the `Accessible` view from `storybook-nitroui-containers`, which sets them natively.
|
|
438
|
+
- NitroUI has no JS API for the device's own name, so `src/server/host-devices.ts` looks it up with `simctl`, `devicectl` or `adb` from the UDID or serial in the server URL. A physical phone launched without one, for example from Xcode, shows its ID instead.
|
|
386
439
|
- The web preview's browser-only addons (`@storybook/addon-*` preview annotations, such as addon-mcp's) are left out of the device bundle because they touch `window`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "storybook-nitroui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Storybook framework for NitroUI apps: on-device preview and manager, Node-side indexing and docgen.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,18 +24,21 @@
|
|
|
24
24
|
],
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
|
-
"
|
|
27
|
+
"storybook-nitroui-source": "./src/index.ts",
|
|
28
|
+
"types": "./types/index.d.ts",
|
|
28
29
|
"default": "./src/index.ts"
|
|
29
30
|
},
|
|
30
31
|
"./preset": "./src/preset.ts",
|
|
31
32
|
"./renderer-preset": "./src/renderer-preset.ts",
|
|
32
33
|
"./builder": "./src/builder/index.ts",
|
|
33
34
|
"./device": {
|
|
34
|
-
"
|
|
35
|
+
"storybook-nitroui-source": "./src/device/index.ts",
|
|
36
|
+
"types": "./types/device/index.d.ts",
|
|
35
37
|
"default": "./src/device/index.ts"
|
|
36
38
|
},
|
|
37
39
|
"./entry-preview": {
|
|
38
|
-
"
|
|
40
|
+
"storybook-nitroui-source": "./src/renderer/entry-preview.ts",
|
|
41
|
+
"types": "./types/renderer/entry-preview.d.ts",
|
|
39
42
|
"default": "./src/renderer/entry-preview.ts"
|
|
40
43
|
},
|
|
41
44
|
"./package.json": "./package.json"
|
|
@@ -46,16 +49,21 @@
|
|
|
46
49
|
},
|
|
47
50
|
"files": [
|
|
48
51
|
"src",
|
|
52
|
+
"types",
|
|
49
53
|
"README.md",
|
|
50
54
|
"LICENSE"
|
|
51
55
|
],
|
|
52
56
|
"scripts": {
|
|
53
|
-
"typecheck": "tsc --noEmit"
|
|
57
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.device.json",
|
|
58
|
+
"build:types": "rm -rf types && tsc -p tsconfig.build.json",
|
|
59
|
+
"prepack": "bun run build:types"
|
|
54
60
|
},
|
|
55
61
|
"dependencies": {
|
|
56
62
|
"@storybook/react": "11.0.1",
|
|
57
63
|
"@tmcp/adapter-valibot": "^0.1.5",
|
|
58
64
|
"@tmcp/transport-http": "^0.9.1",
|
|
65
|
+
"@typescript/typescript6": "^6.0.2",
|
|
66
|
+
"launch-editor": "^2.14.1",
|
|
59
67
|
"nitro-ui-bundler": "0.3.0",
|
|
60
68
|
"pixelmatch": "^7.1.0",
|
|
61
69
|
"pngjs": "^7.0.0",
|
|
@@ -71,10 +79,9 @@
|
|
|
71
79
|
"nitro-ui-renderer": "0.3.0",
|
|
72
80
|
"react": "19.3.0",
|
|
73
81
|
"nitro-ui-navigation": "0.3.0",
|
|
74
|
-
"typescript": ">=6",
|
|
75
82
|
"nitro-mmkv": "0.3.0",
|
|
76
|
-
"storybook-nitroui-containers": "^0.
|
|
77
|
-
"storybook-nitroui-controls": "^0.
|
|
83
|
+
"storybook-nitroui-containers": "^0.3.0",
|
|
84
|
+
"storybook-nitroui-controls": "^0.3.0",
|
|
78
85
|
"nitro-networking": "0.3.0",
|
|
79
86
|
"nitro-ui-dev": "0.3.0"
|
|
80
87
|
},
|
|
@@ -85,22 +92,17 @@
|
|
|
85
92
|
"@types/pngjs": "^6",
|
|
86
93
|
"@types/react": "19.3.0",
|
|
87
94
|
"@types/ws": "^8",
|
|
88
|
-
"@typescript/native": "npm:typescript@7.0.2",
|
|
89
95
|
"nitro-mmkv": "0.3.0",
|
|
90
96
|
"nitro-networking": "0.3.0",
|
|
97
|
+
"nitro-ui-cli": "0.3.0",
|
|
91
98
|
"nitro-ui-core": "0.3.0",
|
|
92
99
|
"nitro-ui-dev": "0.3.0",
|
|
93
100
|
"nitro-ui-navigation": "0.3.0",
|
|
94
101
|
"nitro-ui-renderer": "0.3.0",
|
|
95
102
|
"react": "19.3.0",
|
|
96
103
|
"storybook": "11.0.1",
|
|
97
|
-
"storybook-nitroui-containers": "0.
|
|
98
|
-
"storybook-nitroui-controls": "0.
|
|
99
|
-
"typescript": "
|
|
100
|
-
},
|
|
101
|
-
"peerDependenciesMeta": {
|
|
102
|
-
"typescript": {
|
|
103
|
-
"optional": true
|
|
104
|
-
}
|
|
104
|
+
"storybook-nitroui-containers": "0.3.0",
|
|
105
|
+
"storybook-nitroui-controls": "0.3.0",
|
|
106
|
+
"typescript": "7.0.2"
|
|
105
107
|
}
|
|
106
108
|
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storybook's change detection (the "new", "modified" and "affected" story statuses in the web
|
|
3
|
+
* sidebar) builds its own import graph of the story files. A builder only says how imports resolve
|
|
4
|
+
* and which files changed; `@storybook/builder-vite` takes both from its Vite server. The device
|
|
5
|
+
* bundles are built by Rolldown in the Bun process, so this adapter resolves like that bundle and
|
|
6
|
+
* watches the project from the Storybook process.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, readdirSync, statSync, watch, type FSWatcher } from 'node:fs'
|
|
9
|
+
import { join, normalize, relative, sep } from 'node:path'
|
|
10
|
+
import { logger } from 'storybook/internal/node-logger'
|
|
11
|
+
import type { Builder } from 'storybook/internal/types'
|
|
12
|
+
|
|
13
|
+
type Adapter = ReturnType<NonNullable<Builder<unknown>['changeDetectionAdapter']>>
|
|
14
|
+
type FileChangeEvent = Parameters<Parameters<Adapter['onFileChange']>[0]>[0]
|
|
15
|
+
|
|
16
|
+
/** Top-level directories without story imports: native projects and generated output. */
|
|
17
|
+
const SKIPPED_TOP_LEVEL = new Set(['ios', 'android', 'storybook-static', 'build', 'dist', 'coverage'])
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Whether a path relative to the project root is outside the watch: `node_modules` at any depth,
|
|
21
|
+
* dot directories (`.git`, `.nitro`, `.yarn`, `.expo`, …) except `.storybook`, and the top-level
|
|
22
|
+
* native and output directories.
|
|
23
|
+
*/
|
|
24
|
+
function skipped(relativePath: string) {
|
|
25
|
+
const segments = relativePath.split(sep)
|
|
26
|
+
if (SKIPPED_TOP_LEVEL.has(segments[0]!)) return true
|
|
27
|
+
// The last segment is the file itself; only its directories decide.
|
|
28
|
+
return segments.slice(0, -1).some((segment) => segment === 'node_modules' || (segment.startsWith('.') && segment !== '.storybook'))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const skippedDirectory = (root: string, directory: string) => skipped(join(relative(root, directory), 'file'))
|
|
32
|
+
|
|
33
|
+
/** `watch: false` (Storybook without a dev server) only resolves, as builder-vite's headless adapter does. */
|
|
34
|
+
export function createChangeDetectionAdapter(root: string, { watch: watchFiles }: { watch: boolean }): Adapter {
|
|
35
|
+
return {
|
|
36
|
+
async getResolveConfig() {
|
|
37
|
+
// The device bundle uses Rolldown's `neutral` platform with no extra conditions. Storybook's
|
|
38
|
+
// resolver reads tsconfig `paths` itself.
|
|
39
|
+
return { projectRoot: root, conditions: ['import'], requireConditions: ['require'] }
|
|
40
|
+
},
|
|
41
|
+
onFileChange(handler) {
|
|
42
|
+
if (!watchFiles) return () => {}
|
|
43
|
+
const watcher = projectWatcher(root)
|
|
44
|
+
watcher.handlers.add(handler)
|
|
45
|
+
return () => watcher.handlers.delete(handler)
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type ProjectWatcher = { root: string; handlers: Set<(event: FileChangeEvent) => void>; close: () => void }
|
|
51
|
+
let shared: ProjectWatcher | undefined
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* One set of watchers for the process, shared by every subscriber. Core subscribes twice (during
|
|
55
|
+
* its first graph build, then for good), and recreating the watchers in between could drop events.
|
|
56
|
+
*/
|
|
57
|
+
function projectWatcher(root: string): ProjectWatcher {
|
|
58
|
+
if (shared?.root === root) return shared
|
|
59
|
+
shared?.close()
|
|
60
|
+
const handlers = new Set<(event: FileChangeEvent) => void>()
|
|
61
|
+
const watchers: FSWatcher[] = []
|
|
62
|
+
const emit = (path: string) => {
|
|
63
|
+
if (skipped(relative(root, path))) return
|
|
64
|
+
// Report every write to an existing file as `change`: macOS reports atomic saves (and writes
|
|
65
|
+
// to new files) as `rename`, and core only re-reads a file's imports on `change`. New story
|
|
66
|
+
// files reach core through the story index instead.
|
|
67
|
+
const event: FileChangeEvent = { kind: existsSync(path) ? 'change' : 'unlink', path }
|
|
68
|
+
for (const handler of handlers) handler(event)
|
|
69
|
+
}
|
|
70
|
+
let warned = false
|
|
71
|
+
const add = (directory: string, recursive: boolean, onEvent: (path: string) => void) => {
|
|
72
|
+
try {
|
|
73
|
+
const watcher = watch(directory, { recursive }, (_eventType, filename) => {
|
|
74
|
+
if (filename !== null) onEvent(normalize(join(directory, filename.toString())))
|
|
75
|
+
})
|
|
76
|
+
watcher.on('error', () => watcher.close())
|
|
77
|
+
// Storybook's own shutdown ends the process; the watchers must not keep it alive.
|
|
78
|
+
watcher.unref()
|
|
79
|
+
watchers.push(watcher)
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (warned) return
|
|
82
|
+
warned = true
|
|
83
|
+
logger.warn(`storybook-nitroui: change detection can't watch ${directory} (${error instanceof Error ? error.message : String(error)}); edits there won't update story statuses`)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (process.platform === 'darwin' || process.platform === 'win32') {
|
|
88
|
+
// One native recursive watch (FSEvents, ReadDirectoryChangesW); skipped paths are filtered.
|
|
89
|
+
add(root, true, emit)
|
|
90
|
+
} else {
|
|
91
|
+
// Node's recursive watch on Linux adds an inotify watch per directory, `node_modules` included,
|
|
92
|
+
// so watch each kept directory on its own and pick up directories created later.
|
|
93
|
+
const watched = new Set<string>()
|
|
94
|
+
const walk = (directory: string) => {
|
|
95
|
+
if (watched.has(directory)) return
|
|
96
|
+
watched.add(directory)
|
|
97
|
+
add(directory, false, (path) => {
|
|
98
|
+
if (isDirectory(path)) {
|
|
99
|
+
if (!skippedDirectory(root, path)) walk(path)
|
|
100
|
+
} else {
|
|
101
|
+
emit(path)
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
for (const entry of safeReaddir(directory)) {
|
|
105
|
+
const path = join(directory, entry.name)
|
|
106
|
+
if (entry.isDirectory() && !skippedDirectory(root, path)) walk(path)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
walk(root)
|
|
110
|
+
}
|
|
111
|
+
shared = {
|
|
112
|
+
root,
|
|
113
|
+
handlers,
|
|
114
|
+
close: () => {
|
|
115
|
+
for (const watcher of watchers) watcher.close()
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
return shared
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Stops watching (the builder's `bail()`). */
|
|
122
|
+
export function closeChangeDetection() {
|
|
123
|
+
shared?.close()
|
|
124
|
+
shared = undefined
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const isDirectory = (path: string) => {
|
|
128
|
+
try {
|
|
129
|
+
return statSync(path).isDirectory()
|
|
130
|
+
} catch {
|
|
131
|
+
return false
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const safeReaddir = (directory: string) => {
|
|
136
|
+
try {
|
|
137
|
+
return readdirSync(directory, { withFileTypes: true })
|
|
138
|
+
} catch {
|
|
139
|
+
return []
|
|
140
|
+
}
|
|
141
|
+
}
|
package/src/builder/codegen.ts
CHANGED
|
@@ -62,6 +62,8 @@ export type DeviceConfig = {
|
|
|
62
62
|
initialStoryId?: string
|
|
63
63
|
features: Record<string, unknown>
|
|
64
64
|
frameworkOptions: FrameworkOptions
|
|
65
|
+
/** The main config file relative to the project root (`.storybook/main.ts`), for messages on the device. */
|
|
66
|
+
mainConfig?: string
|
|
65
67
|
/** The relay in `storybook dev`; absent in a static build. */
|
|
66
68
|
remote?: { port: number; token: string }
|
|
67
69
|
}
|
package/src/builder/index.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { spawn, type ChildProcess } from 'node:child_process'
|
|
2
2
|
import { existsSync } from 'node:fs'
|
|
3
3
|
import { readFile } from 'node:fs/promises'
|
|
4
|
-
import { join, resolve } from 'node:path'
|
|
4
|
+
import { join, relative, resolve } from 'node:path'
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
|
+
import { findConfigFile } from 'storybook/internal/common'
|
|
6
7
|
import { getService } from 'storybook/internal/core-server'
|
|
7
8
|
import type { StoryIndexGenerator } from 'storybook/internal/core-server'
|
|
8
9
|
import { logger } from 'storybook/internal/node-logger'
|
|
9
10
|
import type { Builder, Options, StoryIndex } from 'storybook/internal/types'
|
|
10
11
|
import { DEFAULT_DEVICE_PORT, ENV, getFrameworkOptions, type FrameworkOptions } from '../frameworkOptions.ts'
|
|
11
|
-
import { freePort, isPortFree } from '../launch/net.ts'
|
|
12
|
+
import { freePort, isPortFree, lanAddress } from '../launch/net.ts'
|
|
12
13
|
import { removeServerState, writeServerState } from '../launch/state.ts'
|
|
13
14
|
import {
|
|
14
15
|
argTypesCode,
|
|
@@ -22,11 +23,14 @@ import {
|
|
|
22
23
|
writeGenerated,
|
|
23
24
|
} from './codegen.ts'
|
|
24
25
|
import { startRemote, type RemoteSetup } from '../server/remote.ts'
|
|
26
|
+
import { closeChangeDetection, createChangeDetectionAdapter } from './changeDetection.ts'
|
|
25
27
|
|
|
26
28
|
type NitroBuilder = Builder<Record<string, never>>
|
|
27
29
|
|
|
28
30
|
let server: ChildProcess | undefined
|
|
29
31
|
let cleanup: (() => void) | undefined
|
|
32
|
+
/** The project root of the running dev server, set by `start()`. */
|
|
33
|
+
let startedRoot: string | undefined
|
|
30
34
|
|
|
31
35
|
const serverScript = fileURLToPath(import.meta.resolve('../server/dev-server.ts'))
|
|
32
36
|
const buildScript = fileURLToPath(import.meta.resolve('../server/build.ts'))
|
|
@@ -78,6 +82,7 @@ async function generate(options: Options, generator: StoryIndexGenerator, framew
|
|
|
78
82
|
initialStoryId: frameworkOptions.initialStoryId,
|
|
79
83
|
features: (features ?? {}) as Record<string, unknown>,
|
|
80
84
|
frameworkOptions,
|
|
85
|
+
mainConfig: relative(root, findConfigFile('main', options.configDir) ?? join(options.configDir, 'main.ts')),
|
|
81
86
|
remote,
|
|
82
87
|
}))
|
|
83
88
|
await writeGenerated(directory, 'argtypes', argTypesCode(await loadDocgenArgTypes(index, features)))
|
|
@@ -103,6 +108,7 @@ export const getConfig: NitroBuilder['getConfig'] = async () => ({})
|
|
|
103
108
|
export const start: NitroBuilder['start'] = async ({ options, startTime, router, server: httpServer }) => {
|
|
104
109
|
const frameworkOptions = await getFrameworkOptions(options)
|
|
105
110
|
const root = projectRoot(options)
|
|
111
|
+
startedRoot = root
|
|
106
112
|
const port = await devServerPort(frameworkOptions)
|
|
107
113
|
const host = frameworkOptions.devServer?.host
|
|
108
114
|
const generator = await options.presets.apply<StoryIndexGenerator>('storyIndexGenerator')
|
|
@@ -138,10 +144,24 @@ export const start: NitroBuilder['start'] = async ({ options, startTime, router,
|
|
|
138
144
|
})
|
|
139
145
|
cleanup = () => removeServerState(root, storybookPort, process.pid)
|
|
140
146
|
|
|
141
|
-
|
|
147
|
+
const devServerURL = `http://${host === undefined ? 'localhost' : host === 'lan' ? lanAddress() ?? 'localhost' : host}:${port}`
|
|
148
|
+
// `nitro-storybook ios|android` launches the app itself; after a bare `storybook dev`, say how.
|
|
149
|
+
logger.info(process.env[ENV.launcher] === '1'
|
|
150
|
+
? `storybook-nitroui: NitroUI dev server on ${devServerURL}`
|
|
151
|
+
: `storybook-nitroui: NitroUI dev server on ${devServerURL}. Run \`nitro-storybook ios\` or \`nitro-storybook android\`, or launch the app with -NitroUIDevServerURL ${devServerURL}`)
|
|
142
152
|
return { bail, totalTime: process.hrtime(startTime) }
|
|
143
153
|
}
|
|
144
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Storybook's change detection. Called after `start()` with no options, and without a dev server
|
|
157
|
+
* (headless) with options.
|
|
158
|
+
*/
|
|
159
|
+
export const changeDetectionAdapter: NitroBuilder['changeDetectionAdapter'] = (options) => {
|
|
160
|
+
if (startedRoot !== undefined) return createChangeDetectionAdapter(startedRoot, { watch: true })
|
|
161
|
+
if (options !== undefined) return createChangeDetectionAdapter(projectRoot(options), { watch: false })
|
|
162
|
+
throw new Error('storybook-nitroui: changeDetectionAdapter() was called before start()')
|
|
163
|
+
}
|
|
164
|
+
|
|
145
165
|
/** The configured port, which must be free, or the first free one from 8082. */
|
|
146
166
|
async function devServerPort(frameworkOptions: FrameworkOptions) {
|
|
147
167
|
const configured = frameworkOptions.devServer?.port
|
|
@@ -167,6 +187,7 @@ export const build: NitroBuilder['build'] = async ({ options }) => {
|
|
|
167
187
|
}
|
|
168
188
|
|
|
169
189
|
export const bail: NitroBuilder['bail'] = async () => {
|
|
190
|
+
closeChangeDetection()
|
|
170
191
|
server?.kill()
|
|
171
192
|
server = undefined
|
|
172
193
|
cleanup?.()
|
package/src/cli.ts
CHANGED
|
@@ -21,7 +21,9 @@ Commands
|
|
|
21
21
|
devices List simulators, emulators and phones (--platform ios|android)
|
|
22
22
|
status Show this project's running Storybooks and their connected devices
|
|
23
23
|
stop Stop this project's running Storybooks (--port to pick one)
|
|
24
|
-
init Add .storybook/{main.ts,preview.tsx,index.tsx} and
|
|
24
|
+
init Add .storybook/{main.ts,preview.tsx,index.tsx}, package scripts and an example
|
|
25
|
+
story, and install the packages with the app's package manager (--skip-install
|
|
26
|
+
to only print the command)
|
|
25
27
|
test Render stories on a device as tests (nitro-storybook test --help)
|
|
26
28
|
serve [dir] Serve a \`storybook build\` output to devices (--port, default 8083)
|
|
27
29
|
|
|
@@ -96,6 +98,7 @@ async function main(): Promise<number> {
|
|
|
96
98
|
platform: { type: 'string' },
|
|
97
99
|
json: { type: 'boolean', default: false },
|
|
98
100
|
verbose: { type: 'boolean', short: 'v', default: false },
|
|
101
|
+
'skip-install': { type: 'boolean', default: false },
|
|
99
102
|
help: { type: 'boolean', short: 'h', default: false },
|
|
100
103
|
},
|
|
101
104
|
})
|
|
@@ -150,7 +153,7 @@ async function main(): Promise<number> {
|
|
|
150
153
|
return await stopCommand(root, number('port', values.port), out)
|
|
151
154
|
case 'init': {
|
|
152
155
|
const { initCommand } = await import('./launch/init.ts')
|
|
153
|
-
return initCommand(root, configDir, out)
|
|
156
|
+
return await initCommand(root, configDir, { skipInstall: values['skip-install'] }, out)
|
|
154
157
|
}
|
|
155
158
|
case 'serve': {
|
|
156
159
|
const { serveCommand } = await import('./launch/serve.ts')
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
} from 'storybook/internal/core-events'
|
|
14
14
|
import { combineParameters, inferControls } from 'storybook/internal/preview-api'
|
|
15
15
|
import type { Args, ArgTypes, Globals, GlobalTypes, Parameters, StoryId, StoryIndex, StrictArgTypes } from 'storybook/internal/types'
|
|
16
|
-
import { Store } from './Store
|
|
16
|
+
import { Store } from './Store'
|
|
17
17
|
|
|
18
18
|
/** What the manager knows about a story once the preview has prepared it. */
|
|
19
19
|
export type PreparedStoryState = {
|
package/src/device/NitroView.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { View } from 'storybook/internal/preview-api'
|
|
2
|
-
import { CanvasRoot } from '../renderer/CanvasRoot
|
|
3
|
-
import { Store } from './Store
|
|
2
|
+
import { CanvasRoot } from '../renderer/CanvasRoot'
|
|
3
|
+
import { Store } from './Store'
|
|
4
|
+
import { symbolicateStack } from './symbolicate'
|
|
4
5
|
|
|
5
6
|
export type CanvasStatus =
|
|
6
7
|
| { type: 'idle' }
|
|
8
|
+
/** Core found no story to show (web's "No Preview" page). */
|
|
9
|
+
| { type: 'noPreview' }
|
|
7
10
|
| { type: 'preparing' }
|
|
8
11
|
| { type: 'rendering' }
|
|
9
12
|
| { type: 'main' }
|
|
@@ -27,13 +30,26 @@ export class NitroView implements View<CanvasRoot> {
|
|
|
27
30
|
return this.root
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Web's error display: the message's first line is the heading. Below it goes the rest of a
|
|
35
|
+
* multi-line message, or else the stack (core's `renderError` passes a plain description there),
|
|
36
|
+
* never both. The stack's own copy of the message is dropped, and its frames are mapped to source
|
|
37
|
+
* once the dev server answers.
|
|
38
|
+
*/
|
|
39
|
+
showErrorDisplay({ message, stack }: { message?: string; stack?: string }) {
|
|
40
|
+
const [title = 'Error', ...rest] = (message || 'Unknown error').split('\n')
|
|
41
|
+
const detail = rest.length > 0 ? rest.join('\n').replace(/^\n/, '') : withoutMessage(stack ?? '', message ?? '')
|
|
42
|
+
const status: CanvasStatus = { type: 'error', title, description: detail.trim() === '' ? undefined : detail }
|
|
43
|
+
this.status.set(status)
|
|
44
|
+
if (rest.length > 0 || detail === '') return
|
|
45
|
+
void symbolicateStack(detail).then((frames) => {
|
|
46
|
+
// Unindented: the card is narrow on a phone, and every frame line would wrap.
|
|
47
|
+
if (frames !== undefined && this.status.get() === status) this.status.set({ ...status, description: frames.replace(/^\s+/gm, '') })
|
|
48
|
+
})
|
|
33
49
|
}
|
|
34
50
|
|
|
35
51
|
showNoPreview() {
|
|
36
|
-
this.status.set({ type: '
|
|
52
|
+
this.status.set({ type: 'noPreview' })
|
|
37
53
|
}
|
|
38
54
|
|
|
39
55
|
showPreparingStory() {
|
|
@@ -60,3 +76,11 @@ export class NitroView implements View<CanvasRoot> {
|
|
|
60
76
|
this.status.set({ type: 'main' })
|
|
61
77
|
}
|
|
62
78
|
}
|
|
79
|
+
|
|
80
|
+
/** A stack's frames without the `Error: message` lines engines put first. Other text is kept as it is. */
|
|
81
|
+
function withoutMessage(stack: string, message: string): string {
|
|
82
|
+
const lines = stack.split('\n')
|
|
83
|
+
const firstFrame = lines.findIndex((line) => /^\s*at /.test(line))
|
|
84
|
+
if (firstFrame > 0 && message !== '' && lines.slice(0, firstFrame).join('\n').includes(message)) return lines.slice(firstFrame).join('\n')
|
|
85
|
+
return stack
|
|
86
|
+
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { PreviewWithSelection } from 'storybook/internal/preview-api'
|
|
2
|
-
import type { ModuleImportFn, ProjectAnnotations, StoryIndex } from 'storybook/internal/types'
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
2
|
+
import type { ModuleImportFn, ProjectAnnotations, StoryId, StoryIndex } from 'storybook/internal/types'
|
|
3
|
+
import { reportStoryError } from '../renderer/storyErrors'
|
|
4
|
+
import type { NitroRenderer } from '../renderer/types'
|
|
5
|
+
import { MemorySelectionStore } from './MemorySelectionStore'
|
|
6
|
+
import type { NitroView } from './NitroView'
|
|
6
7
|
|
|
7
8
|
type RenderArg = Parameters<PreviewWithSelection<NitroRenderer>['renderSelection']>[0]
|
|
8
9
|
type TeardownRender = PreviewWithSelection<NitroRenderer>['teardownRender']
|
|
10
|
+
type StorySpecifier = Parameters<PreviewWithSelection<NitroRenderer>['renderStoryLoadingException']>[0]
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Core's `PreviewWithSelection`, with no DOM: an in-memory selection, a `NitroView`, and the story
|
|
@@ -17,10 +19,10 @@ export function createPreview(options: {
|
|
|
17
19
|
getProjectAnnotations: () => ProjectAnnotations<NitroRenderer> | Promise<ProjectAnnotations<NitroRenderer>>
|
|
18
20
|
selectionStore: MemorySelectionStore
|
|
19
21
|
view: NitroView
|
|
20
|
-
}) {
|
|
22
|
+
}): PreviewNitro {
|
|
21
23
|
let index = options.index
|
|
22
24
|
|
|
23
|
-
class
|
|
25
|
+
class NitroPreview extends PreviewWithSelection<NitroRenderer> {
|
|
24
26
|
private renderQueue: Promise<void> = Promise.resolve()
|
|
25
27
|
|
|
26
28
|
// `initialize()` runs from the constructor, so this must be a method, not a patched property.
|
|
@@ -41,6 +43,16 @@ export function createPreview(options: {
|
|
|
41
43
|
return run
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
// The canvas shows story errors, as on web, so core's logs of them stay out of NitroUI's dev
|
|
47
|
+
// error pill (see `storyErrors.ts`). `renderError` logs only its "Error rendering story" line.
|
|
48
|
+
renderException(storyId: StoryId, error: Error) {
|
|
49
|
+
reportStoryError(error, () => super.renderException(storyId, error))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
renderStoryLoadingException(storySpecifier: StorySpecifier, error: Error) {
|
|
53
|
+
reportStoryError(error, () => super.renderStoryLoadingException(storySpecifier, error))
|
|
54
|
+
}
|
|
55
|
+
|
|
44
56
|
// Core reloads the page when a render won't tear down. There is no page; the abort is enough.
|
|
45
57
|
teardownRender: TeardownRender = async (render, options) => {
|
|
46
58
|
this.storyRenders = this.storyRenders.filter((r) => r !== render)
|
|
@@ -48,7 +60,11 @@ export function createPreview(options: {
|
|
|
48
60
|
}
|
|
49
61
|
}
|
|
50
62
|
|
|
51
|
-
return new
|
|
63
|
+
return new NitroPreview(options.importFn, options.getProjectAnnotations, options.selectionStore, options.view)
|
|
52
64
|
}
|
|
53
65
|
|
|
54
|
-
|
|
66
|
+
/** The preview `createPreview` returns. Named, so the published declarations don't spell out the class. */
|
|
67
|
+
export type PreviewNitro = PreviewWithSelection<NitroRenderer> & {
|
|
68
|
+
/** Replaces the bundled index after an HMR update of the generated modules. */
|
|
69
|
+
setStoryIndex(next: StoryIndex): Promise<void>
|
|
70
|
+
}
|
package/src/device/hooks.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useCallback, useMemo } from 'react'
|
|
2
2
|
import type { Args, Globals, GlobalTypes, StoryId, StoryIndex, StrictArgTypes } from 'storybook/internal/types'
|
|
3
|
-
import type { ManagerState, PreparedStoryState } from './ManagerStore
|
|
4
|
-
import type { CanvasStatus } from './NitroView
|
|
5
|
-
import { getStorybook } from './runtime
|
|
6
|
-
import { useStore } from './Store
|
|
3
|
+
import type { ManagerState, PreparedStoryState } from './ManagerStore'
|
|
4
|
+
import type { CanvasStatus } from './NitroView'
|
|
5
|
+
import { getStorybook } from './runtime'
|
|
6
|
+
import { useStore } from './Store'
|
|
7
7
|
|
|
8
8
|
// Manager hooks with the same shapes as `storybook/manager-api`, so manager UI and future addon
|
|
9
9
|
// panels port easily. Each hook subscribes to one slice, so a panel re-renders only when it changes.
|