@onlook/storybook-plugin 0.4.0-beta.7 → 0.4.0-beta.9

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.
@@ -1,25 +1,88 @@
1
1
  import { createRequire } from 'node:module';
2
+ import { execFile } from 'child_process';
3
+ import { createRequire as createRequire$1 } from 'module';
4
+ import { promisify } from 'util';
2
5
  import { chromium } from 'playwright';
3
6
 
4
7
  globalThis.require = createRequire(import.meta.url);
5
- var browser = null;
8
+ var execFileAsync = promisify(execFile);
9
+ var require2 = createRequire$1(import.meta.url);
10
+ var browserPromise = null;
11
+ var installPromise = null;
12
+ var isMissingExecutableError = (err) => {
13
+ const msg = err instanceof Error ? err.message : String(err);
14
+ return msg.includes("Executable doesn't exist") || msg.includes("Please run the following command to download new browsers") || // Older playwright revisions phrase the version-mismatch case differently.
15
+ /chromium-\d+\/chrome-linux\/chrome/i.test(msg);
16
+ };
17
+ async function installChromium() {
18
+ if (!installPromise) {
19
+ installPromise = (async () => {
20
+ const cliPath = require2.resolve("playwright-core/cli.js");
21
+ console.log(
22
+ `[STORYBOOK_PLUGIN] chromium binary missing \u2014 installing via ${cliPath}`
23
+ );
24
+ try {
25
+ const { stdout, stderr } = await execFileAsync(
26
+ process.execPath,
27
+ [cliPath, "install", "--force", "chromium"],
28
+ { timeout: 5 * 60 * 1e3, maxBuffer: 32 * 1024 * 1024 }
29
+ );
30
+ if (stdout)
31
+ console.log(`[STORYBOOK_PLUGIN] playwright install: ${stdout.slice(-1e3)}`);
32
+ if (stderr)
33
+ console.log(
34
+ `[STORYBOOK_PLUGIN] playwright install stderr: ${stderr.slice(-1e3)}`
35
+ );
36
+ console.log("[STORYBOOK_PLUGIN] chromium installed");
37
+ } catch (err) {
38
+ const detail = err instanceof Error ? err.message : String(err);
39
+ console.error(`[STORYBOOK_PLUGIN] chromium install FAILED: ${detail}`);
40
+ throw new Error(`Playwright chromium install failed: ${detail}`);
41
+ } finally {
42
+ installPromise = null;
43
+ }
44
+ })();
45
+ }
46
+ return installPromise;
47
+ }
48
+ async function launchWithSelfHeal() {
49
+ try {
50
+ return await chromium.launch({ headless: true });
51
+ } catch (err) {
52
+ if (!isMissingExecutableError(err)) throw err;
53
+ console.log("[STORYBOOK_PLUGIN] launch failed, attempting self-heal install");
54
+ await installChromium();
55
+ try {
56
+ return await chromium.launch({ headless: true });
57
+ } catch (postInstallErr) {
58
+ const detail = postInstallErr instanceof Error ? postInstallErr.message : String(postInstallErr);
59
+ console.error(
60
+ `[STORYBOOK_PLUGIN] launch still failing after install \u2014 likely path or libs issue: ${detail}`
61
+ );
62
+ throw new Error(
63
+ `Playwright chromium is unavailable in this sandbox (install ran but launch still failed): ${detail}`
64
+ );
65
+ }
66
+ }
67
+ }
6
68
  async function getBrowser() {
7
- if (!browser) {
8
- browser = await chromium.launch({
9
- headless: true
69
+ if (!browserPromise) {
70
+ browserPromise = launchWithSelfHeal().catch((err) => {
71
+ browserPromise = null;
72
+ throw err;
10
73
  });
11
74
  }
12
- return browser;
75
+ return browserPromise;
13
76
  }
14
77
  async function closeBrowser() {
15
- if (browser) {
16
- try {
17
- await browser.close();
18
- } catch (error) {
19
- console.error("Error closing browser:", error);
20
- } finally {
21
- browser = null;
22
- }
78
+ const pending = browserPromise;
79
+ if (!pending) return;
80
+ browserPromise = null;
81
+ try {
82
+ const b = await pending;
83
+ await b.close();
84
+ } catch (error) {
85
+ console.error("Error closing browser:", error);
23
86
  }
24
87
  }
25
88
 
@@ -0,0 +1,61 @@
1
+ import { Plugin, PluginOption } from 'vite';
2
+
3
+ /** One module the engine asked us to substitute with a deferred-throw stand-in. */
4
+ type EnvContainmentSubstitution = {
5
+ /**
6
+ * Path of the throwing module relative to the project root (the directory
7
+ * `storybook dev` runs from), posix separators, extension included —
8
+ * e.g. `src/env.ts`. Alias (`@/env`), relative (`../env`), and absolute
9
+ * imports that resolve to this file are all intercepted.
10
+ */
11
+ module: string;
12
+ /**
13
+ * Best-effort named exports of the real module. The stand-in re-exports
14
+ * each (plus a default export, always) so static ESM bindings keep
15
+ * resolving. Names that aren't valid identifiers are skipped.
16
+ */
17
+ exports?: string[];
18
+ /** Env keys the module validates — carried into the containment marker. */
19
+ envKeys?: string[];
20
+ /** Recognized schema library (`t3` | `zod` | `valibot`) — diagnostics only. */
21
+ schemaKind?: string;
22
+ };
23
+ /**
24
+ * The `envContainment` plugin-options surface. Written by the preflight
25
+ * engine into `.storybook/main.*` (`storybookOnlookPlugin({ envContainment })`).
26
+ * Mirrored structurally as `EnvContainmentValue` in
27
+ * `packages/preflight/src/checks/storybook/patch.ts` — keep the shapes in sync.
28
+ */
29
+ type EnvContainmentOptions = {
30
+ /** Modules to replace with deferred-throw stand-ins. */
31
+ substitute?: EnvContainmentSubstitution[];
32
+ /**
33
+ * Env key → placeholder value. Each key is injected via Vite `define` as
34
+ * both `process.env.<KEY>` and `import.meta.env.<KEY>`.
35
+ */
36
+ define?: Record<string, string>;
37
+ };
38
+ /**
39
+ * Build the env-containment Vite plugin. Returns `null` when the options
40
+ * carry no work — callers spread it conditionally so a config without
41
+ * `envContainment` behaves exactly as before this plugin existed.
42
+ */
43
+ declare function envContainmentPlugin(options: EnvContainmentOptions | undefined): Plugin | null;
44
+
45
+ type OnlookPluginOptions = {
46
+ /** Storybook port (default: 6006) */
47
+ port?: number;
48
+ /** Additional allowed origins for CORS (merged with defaults) */
49
+ allowedOrigins?: string[];
50
+ hmr?: {
51
+ protocol?: 'ws' | 'wss';
52
+ clientPort?: number;
53
+ };
54
+ storyGlobs?: string[];
55
+ tailwindEntryCss?: string | false;
56
+ envContainment?: EnvContainmentOptions;
57
+ moduleLoadCatchAll?: boolean;
58
+ };
59
+ declare function storybookOnlookPlugin(options?: OnlookPluginOptions): PluginOption[];
60
+
61
+ export { type EnvContainmentOptions as E, type OnlookPluginOptions as O, type EnvContainmentSubstitution as a, envContainmentPlugin as e, storybookOnlookPlugin as s };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onlook/storybook-plugin",
3
- "version": "0.4.0-beta.7",
3
+ "version": "0.4.0-beta.9",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "onlook-storybook": "./dist/cli/index.js"
@@ -10,6 +10,14 @@
10
10
  "types": "./dist/index.d.ts",
11
11
  "default": "./dist/index.js"
12
12
  },
13
+ "./preset": {
14
+ "types": "./dist/preset/index.d.ts",
15
+ "default": "./dist/preset/index.js"
16
+ },
17
+ "./preview": {
18
+ "types": "./dist/preview/index.d.ts",
19
+ "default": "./dist/preview/index.js"
20
+ },
13
21
  "./screenshot-service": {
14
22
  "types": "./dist/screenshot-service/index.d.ts",
15
23
  "default": "./dist/screenshot-service/index.js"
@@ -24,7 +32,7 @@
24
32
  "README.md"
25
33
  ],
26
34
  "scripts": {
27
- "build": "tsup",
35
+ "build": "rm -rf dist && tsup",
28
36
  "clean": "git clean -xdf .cache .turbo dist node_modules",
29
37
  "typecheck": "tsc --noEmit",
30
38
  "prepublishOnly": "bun run build && bun scripts/prepublish.ts",
@@ -34,11 +42,13 @@
34
42
  },
35
43
  "dependencies": {
36
44
  "@babel/generator": "^7.26.9",
37
- "@babel/parser": "^7.26.9",
45
+ "@babel/parser": "^7.29.3",
38
46
  "@babel/traverse": "^7.26.9",
39
- "@babel/types": "^7.26.9",
47
+ "@babel/types": "^7.29.0",
40
48
  "@drizzle-team/brocli": "^0.11.0",
41
- "playwright": "^1.52.0"
49
+ "playwright": "^1.52.0",
50
+ "playwright-core": "^1.52.0",
51
+ "storybook": "^10.1.11"
42
52
  },
43
53
  "devDependencies": {
44
54
  "@onbook/tsconfig": "0.1.0",
@@ -54,6 +64,7 @@
54
64
  "access": "public"
55
65
  },
56
66
  "peerDependencies": {
67
+ "react": "^18.0.0 || ^19.0.0",
57
68
  "vite": "^5.0.0 || ^6.0.0"
58
69
  }
59
70
  }