@codegrowers/storybook-jaspr-vite 1.0.0 → 1.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.1
4
+
5
+ - Keep the parts dart2js emits for deferred imports. Development serves them
6
+ next to the preview, and static builds emit them with the preview into one
7
+ content-hashed directory, so `loadLibrary()` works in Storybook.
8
+
3
9
  ## 1.0.0
4
10
 
5
11
  - Initial MIT-licensed release of the Jaspr Storybook integration.
package/README.md CHANGED
@@ -6,7 +6,7 @@ Controls, record Actions and run TypeScript play functions against the DOM.
6
6
 
7
7
  MIT licensed. Requires the companion
8
8
  [`jaspr_storybook`](https://pub.dev/packages/jaspr_storybook) Dart runtime.
9
- Both packages are released at `1.0.0`.
9
+ This framework is released at `1.0.1` and works with the `1.x` runtime.
10
10
 
11
11
  ```sh
12
12
  bun add --dev @codegrowers/storybook-jaspr-vite@^1.0.0 storybook@10.6.5 vite@8.3.5 @storybook/addon-docs@10.6.5
package/STORYBOOK.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Storybook for Jaspr
2
2
 
3
- Two MIT-licensed packages provide the integration, both at version `1.0.0`:
3
+ Two MIT-licensed packages provide the integration. The npm framework is at
4
+ `1.0.1` and works with the `1.x` Dart runtime:
4
5
 
5
6
  | Registry | Package | Source |
6
7
  | --- | --- | --- |
@@ -88,6 +89,9 @@ No remote Dart or Jaspr server is required to view a built Storybook.
88
89
  Builder errors reject rendering so Storybook can show its error UI.
89
90
  - Play functions run after the Dart mount/update promise completes. They are
90
91
  authored in TypeScript against the preview's regular DOM.
92
+ - Deferred imports work. The parts dart2js emits are served next to the
93
+ preview during development and emitted with it into one content-hashed
94
+ asset directory in static builds.
91
95
  - Changes to Dart files under the entrypoint directory or `watchPaths` trigger
92
96
  recompilation and a full preview reload. Dart state resets on source changes;
93
97
  this is not Jaspr hot reload. Compilation failures show the Vite error overlay.
@@ -138,8 +142,8 @@ explicit source override, the code panel indicates that Dart source is unconfigu
138
142
  ## Examples and development
139
143
 
140
144
  Each repository includes a small `example/` with a button, a controlled switch
141
- and stateful counters. No backend or application-specific assets are needed.
142
- The example configuration lives in `example/storybook/` so pub.dev includes it;
145
+ and stateful counters. The npm example also loads a deferred library. No
146
+ backend or application-specific assets are needed. The example configuration lives in `example/storybook/` so pub.dev includes it;
143
147
  consumer applications can use the conventional `.storybook/` directory.
144
148
 
145
149
  In the Dart runtime repository:
@@ -1,4 +1,13 @@
1
1
  import type { Plugin } from "vite";
2
2
  import type { FrameworkOptions } from "./types.js";
3
- export declare function compilePreview(entrypoint: string, dartExecutable: string, production: boolean): Promise<Buffer>;
3
+ export type CompiledPreview = ReadonlyMap<string, Buffer>;
4
+ export interface PreviewAssets {
5
+ entry: string;
6
+ files: {
7
+ fileName: string;
8
+ source: Buffer;
9
+ }[];
10
+ }
11
+ export declare function compilePreview(entrypoint: string, dartExecutable: string, production: boolean): Promise<CompiledPreview>;
12
+ export declare function previewAssets(preview: CompiledPreview, assetsDir: string): PreviewAssets;
4
13
  export declare function dartPreviewPlugin(options: FrameworkOptions, configDir: string): Plugin;
@@ -1,13 +1,17 @@
1
1
  import { execFile } from "node:child_process";
2
- import { access, mkdtemp, readFile, rm } from "node:fs/promises";
2
+ import { createHash } from "node:crypto";
3
+ import { access, mkdtemp, readdir, readFile, rm } from "node:fs/promises";
3
4
  import { tmpdir } from "node:os";
4
- import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
+ import { dirname, isAbsolute, join, posix, relative, resolve, sep, } from "node:path";
5
6
  import { promisify } from "node:util";
6
7
  import { readSourceRegions } from "./sources.js";
7
8
  const exec = promisify(execFile);
8
9
  const virtualId = "virtual:jaspr-storybook-runtime";
9
10
  const resolvedId = `\0${virtualId}`;
10
- const devUrl = "/@jaspr-storybook/preview.js";
11
+ const previewName = "preview.js";
12
+ const previewOutput = /^preview\.js(?:_\d+\.part\.js)?$/;
13
+ const devPrefix = "/@jaspr-storybook";
14
+ const devUrl = `${devPrefix}/${previewName}`;
11
15
  async function packageConfig(entrypoint) {
12
16
  let directory = dirname(entrypoint);
13
17
  while (true) {
@@ -28,7 +32,7 @@ export async function compilePreview(entrypoint, dartExecutable, production) {
28
32
  const packages = await packageConfig(entrypoint);
29
33
  const scratch = await mkdtemp(join(tmpdir(), "jaspr-storybook-"));
30
34
  try {
31
- const output = join(scratch, "preview.js");
35
+ const output = join(scratch, previewName);
32
36
  await exec(dartExecutable, [
33
37
  "compile",
34
38
  "js",
@@ -39,7 +43,10 @@ export async function compilePreview(entrypoint, dartExecutable, production) {
39
43
  output,
40
44
  entrypoint,
41
45
  ], { cwd: dirname(entrypoint), maxBuffer: 8 * 1024 * 1024 });
42
- return await readFile(output);
46
+ const names = (await readdir(scratch))
47
+ .filter((name) => previewOutput.test(name))
48
+ .sort();
49
+ return new Map(await Promise.all(names.map(async (name) => [name, await readFile(join(scratch, name))])));
43
50
  }
44
51
  catch (error) {
45
52
  const output = error;
@@ -49,6 +56,19 @@ export async function compilePreview(entrypoint, dartExecutable, production) {
49
56
  await rm(scratch, { recursive: true, force: true });
50
57
  }
51
58
  }
59
+ export function previewAssets(preview, assetsDir) {
60
+ const hash = createHash("sha256");
61
+ for (const [name, source] of preview)
62
+ hash.update(`${name}\0${source.length}\0`).update(source);
63
+ const directory = posix.join(assetsDir, `jaspr-preview-${hash.digest("hex").slice(0, 12)}`);
64
+ return {
65
+ entry: posix.join(directory, previewName),
66
+ files: [...preview].map(([name, source]) => ({
67
+ fileName: posix.join(directory, name),
68
+ source,
69
+ })),
70
+ };
71
+ }
52
72
  export function dartPreviewPlugin(options, configDir) {
53
73
  const entrypoint = resolve(configDir, options.entrypoint);
54
74
  const sourceFiles = [
@@ -61,7 +81,8 @@ export function dartPreviewPlugin(options, configDir) {
61
81
  ...(options.watchPaths ?? []).map((path) => resolve(configDir, path)),
62
82
  ];
63
83
  let production = false;
64
- let bundle;
84
+ let assetsDir = "assets";
85
+ let bundle = new Map();
65
86
  let sources = {};
66
87
  let queued = Promise.resolve();
67
88
  let timer;
@@ -89,6 +110,7 @@ export function dartPreviewPlugin(options, configDir) {
89
110
  name: "jaspr-storybook",
90
111
  configResolved(config) {
91
112
  production = config.command === "build";
113
+ assetsDir = config.build.assetsDir;
92
114
  },
93
115
  async buildStart() {
94
116
  sources = await readSourceRegions(sourceFiles);
@@ -104,19 +126,25 @@ export function dartPreviewPlugin(options, configDir) {
104
126
  const sourceExport = `export const dartSources = ${JSON.stringify(sources)};`;
105
127
  if (!production)
106
128
  return `${sourceExport} export const bundleUrl = ${JSON.stringify(devUrl)};`;
107
- const reference = this.emitFile({
108
- type: "asset",
109
- name: "jaspr-preview.js",
110
- source: bundle,
111
- });
129
+ const assets = previewAssets(bundle, assetsDir);
130
+ let reference = "";
131
+ for (const file of assets.files) {
132
+ const emitted = this.emitFile({ type: "asset", ...file });
133
+ if (file.fileName === assets.entry)
134
+ reference = emitted;
135
+ }
112
136
  return `${sourceExport} export const bundleUrl = import.meta.ROLLUP_FILE_URL_${reference};`;
113
137
  },
114
138
  configureServer(devServer) {
115
139
  server = devServer;
116
- server.middlewares.use(devUrl, (_request, response) => {
140
+ server.middlewares.use(devPrefix, (request, response, next) => {
141
+ const name = new URL(request.url ?? "/", "http://localhost").pathname;
142
+ const source = bundle.get(name.slice(1));
143
+ if (!source)
144
+ return next();
117
145
  response.setHeader("Content-Type", "application/javascript; charset=utf-8");
118
146
  response.setHeader("Cache-Control", "no-store");
119
- response.end(bundle);
147
+ response.end(source);
120
148
  });
121
149
  server.watcher.add(watchPaths);
122
150
  const onChange = (path) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codegrowers/storybook-jaspr-vite",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A native Storybook renderer and Vite framework for Jaspr components written in Dart.",
5
5
  "type": "module",
6
6
  "license": "MIT",