Skip to content

Commit ff3d324

Browse files
committed
fix(storybook): derive optimizer entries from component specs and workers
Read the modules component tests import by URL from the specs instead of copying them, and crawl *.worker.ts files, which the dependency scan does not follow.
1 parent 3dd6b6d commit ff3d324

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

  • packages/storybook/.storybook

‎packages/storybook/.storybook/main.ts‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineMain } from "storybook-solidjs-vite"
2+
import { existsSync, readdirSync, readFileSync } from "node:fs"
23
import path from "node:path"
34
import { fileURLToPath } from "node:url"
45
import tailwindcss from "@tailwindcss/vite"
@@ -11,6 +12,22 @@ const app = path.resolve(here, "../../app/src")
1112
const extensions = path.resolve(here, "../../gui-extensions/src")
1213
const mocks = path.resolve(here, "./mocks")
1314

15+
// Component tests import real modules by URL (`source("../src/…")`, `new URL("./x.fixture.tsx", import.meta.url)`),
16+
// past the mock aliases. Read those paths from the specs so the dependency optimizer crawls exactly what they load.
17+
const packages = path.resolve(here, "../..")
18+
const componentTestModules = readdirSync(packages)
19+
.map((name) => path.join(packages, name, "component-tests"))
20+
.filter((dir) => existsSync(dir))
21+
.flatMap((dir) =>
22+
readdirSync(dir, { recursive: true, encoding: "utf8" })
23+
.filter((file) => file.endsWith(".spec.ts"))
24+
.flatMap((file) =>
25+
Array.from(readFileSync(path.join(dir, file), "utf8").matchAll(/["'](\.{1,2}\/[^"']+\.tsx?)["']/g), (match) =>
26+
path.resolve(dir, path.dirname(file), match[1]!),
27+
),
28+
),
29+
)
30+
1431
export default defineMain({
1532
framework: {
1633
name: "storybook-solidjs-vite",
@@ -32,26 +49,17 @@ export default defineMain({
3249
],
3350
async viteFinal(config) {
3451
const { mergeConfig, searchForWorkspaceRoot } = await import("vite")
35-
const entry = (glob: string) =>
36-
path.relative(config.root ?? process.cwd(), path.resolve(here, glob)).replaceAll("\\", "/")
52+
const entry = (file: string) =>
53+
path.relative(config.root ?? process.cwd(), path.resolve(here, file)).replaceAll("\\", "/")
3754
const merged = mergeConfig(config, {
3855
plugins: [tailwindcss(), playgroundCss()],
3956
optimizeDeps: {
4057
// Without Storybook's optimizer plugin (removed below), Vite finds dependencies on first import and reloads
41-
// the page, aborting component tests mid-import. Crawl the stories and every module those tests import first.
58+
// the page, aborting component tests mid-import. Crawl the stories, the workers (the scan does not follow
59+
// `new Worker(new URL(…))`) and the modules component tests load.
4260
entries: [
43-
entry("../../ui/src/**/*.stories.tsx"),
44-
entry("../../session-ui/src/**/*.stories.tsx"),
45-
entry("../../app/src/**/*.stories.tsx"),
46-
entry("../../gui-extensions/src/**/*.stories.tsx"),
47-
entry("../../*/component-tests/*.fixture.tsx"),
48-
entry("../../gui-extensions/src/browser/panel.fixture.tsx"),
49-
entry("../../gui-extensions/src/{renderer,sdk/index,browser/index,file/index}.ts"),
50-
entry("../../gui-extensions/src/{browser/panel,browser/renderer,file/renderer}.tsx"),
51-
entry("../../app/src/runtime/extension/{embeds,host,panels,render}.tsx"),
52-
entry(
53-
"../../app/src/{runtime/i18n/language,runtime/platform/platform,settings/model,shell/commands/command}.tsx",
54-
),
61+
entry("../../{ui,session-ui,app,gui-extensions}/src/**/*.{stories.tsx,worker.ts}"),
62+
...new Set(componentTestModules.map(entry)),
5563
],
5664
},
5765
resolve: {

0 commit comments

Comments
 (0)