@storybook-astro/framework 0.1.0-beta.8 → 1.0.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 +38 -0
- package/dist/base-IRZo3zgK.d.ts +23 -0
- package/dist/chunk-4SWPVM6R.js +96 -0
- package/dist/chunk-4SWPVM6R.js.map +1 -0
- package/dist/chunk-5EF25G5S.js +69 -0
- package/dist/chunk-5EF25G5S.js.map +1 -0
- package/dist/chunk-7GHEQUPV.js +439 -0
- package/dist/chunk-7GHEQUPV.js.map +1 -0
- package/dist/chunk-C5OH4VBR.js +492 -0
- package/dist/chunk-C5OH4VBR.js.map +1 -0
- package/dist/chunk-DNGQBPT7.js +15 -0
- package/dist/chunk-DNGQBPT7.js.map +1 -0
- package/dist/chunk-E4LB75JN.js +89 -0
- package/dist/chunk-E4LB75JN.js.map +1 -0
- package/dist/chunk-PJEDXZVN.js +240 -0
- package/dist/chunk-PJEDXZVN.js.map +1 -0
- package/dist/chunk-UK43WNEA.js +657 -0
- package/dist/chunk-UK43WNEA.js.map +1 -0
- package/dist/dist-HJOEPVRQ.js +15574 -0
- package/dist/dist-HJOEPVRQ.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +13 -64
- package/dist/index.js.map +1 -1
- package/dist/integrations/index.d.ts +138 -0
- package/dist/integrations/index.js +8 -196
- package/dist/integrations/index.js.map +1 -1
- package/dist/middleware.d.ts +26 -0
- package/dist/middleware.js +179 -0
- package/dist/middleware.js.map +1 -0
- package/dist/portable-stories-BvdaQigq.d.ts +83 -0
- package/dist/preset.d.ts +14 -0
- package/dist/preset.js +5 -1
- package/dist/testing.d.ts +27 -0
- package/dist/testing.js +324 -15539
- package/dist/testing.js.map +1 -1
- package/dist/types-CHTsRtA7.d.ts +42 -0
- package/dist/viteStorybookAstroMiddlewarePlugin-NP2E52IC.js +11 -0
- package/dist/viteStorybookAstroMiddlewarePlugin-NP2E52IC.js.map +1 -0
- package/dist/vitest/index.d.ts +19 -0
- package/dist/vitest/index.js +229 -0
- package/dist/vitest/index.js.map +1 -0
- package/package.json +31 -17
- package/src/importAstroConfig.ts +11 -0
- package/src/index.ts +20 -6
- package/src/integrations/alpine.ts +5 -2
- package/src/integrations/base.ts +2 -2
- package/src/integrations/moduleResolver.ts +43 -0
- package/src/integrations/preact.ts +5 -2
- package/src/integrations/react.ts +5 -2
- package/src/integrations/solid.ts +5 -2
- package/src/integrations/svelte.ts +5 -2
- package/src/integrations/vue.ts +5 -2
- package/src/lib/sanitization.test.ts +232 -0
- package/src/lib/sanitization.ts +338 -0
- package/src/lib/ssr-load-module-with-fs-fallback.ts +29 -0
- package/src/middleware.test.ts +48 -0
- package/src/middleware.ts +204 -96
- package/src/module-mocks.ts +16 -0
- package/src/msw-helpers.ts +1 -0
- package/src/msw.ts +58 -0
- package/src/preset.ts +38 -3
- package/src/rules-options.test.ts +71 -0
- package/src/rules-options.ts +87 -0
- package/src/rules.test.ts +183 -0
- package/src/rules.ts +314 -0
- package/src/testing/astro-runtime.ts +219 -0
- package/src/testing/component-utils.ts +32 -0
- package/src/testing/index.ts +2 -0
- package/src/testing/integration-config.ts +121 -0
- package/src/testing/project-root.ts +185 -0
- package/src/testing/renderer-daemon.ts +269 -0
- package/src/testing/story-composition.ts +33 -0
- package/src/testing/types.ts +14 -0
- package/src/testing/working-directory.ts +28 -0
- package/src/testing.ts +1 -254
- package/src/types.ts +16 -4
- package/src/virtual.d.ts +2 -1
- package/src/vite/createVirtualModulePlugin.test.ts +80 -0
- package/src/vite/createVirtualModulePlugin.ts +25 -0
- package/src/viteAstroContainerRenderersPlugin.ts +60 -26
- package/src/vitePluginAstro.ts +12 -5
- package/src/vitePluginAstroBuildPrerender.ts +665 -204
- package/src/vitePluginAstroRoutesFallback.ts +37 -0
- package/src/vitePluginAstroVueFallback.ts +47 -0
- package/src/viteStorybookAstroMiddlewarePlugin.ts +88 -12
- package/src/viteStorybookRendererFallbackPlugin.ts +13 -23
- package/src/vitest/config.ts +95 -0
- package/src/vitest/global-setup.ts +16 -0
- package/src/vitest/index.ts +2 -0
- package/src/vitest/vite-plugins.ts +187 -0
- package/dist/chunk-SAOPE6SA.js +0 -557
- package/dist/chunk-SAOPE6SA.js.map +0 -1
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyMswHandlers,
|
|
3
|
+
resolveSanitizationOptions,
|
|
4
|
+
resolveStoryModuleMock,
|
|
5
|
+
sanitizeRenderPayload,
|
|
6
|
+
selectStoryRules,
|
|
7
|
+
withStoryModuleMocks
|
|
8
|
+
} from "./chunk-C5OH4VBR.js";
|
|
9
|
+
import {
|
|
10
|
+
vitePluginAstroComponentMarker
|
|
11
|
+
} from "./chunk-E4LB75JN.js";
|
|
12
|
+
import {
|
|
13
|
+
createVirtualModulePlugin,
|
|
14
|
+
resolveRulesConfigFilePath,
|
|
15
|
+
ssrLoadModuleWithFsFallback,
|
|
16
|
+
vitePluginAstroFontsFallback,
|
|
17
|
+
vitePluginAstroRoutesFallback,
|
|
18
|
+
vitePluginAstroVueFallback,
|
|
19
|
+
vitePluginStorybookAstroMiddleware
|
|
20
|
+
} from "./chunk-7GHEQUPV.js";
|
|
21
|
+
import {
|
|
22
|
+
importAstroConfig
|
|
23
|
+
} from "./chunk-DNGQBPT7.js";
|
|
24
|
+
|
|
25
|
+
// src/viteStorybookRendererFallbackPlugin.ts
|
|
26
|
+
function viteStorybookRendererFallbackPlugin(integrations) {
|
|
27
|
+
const safeIntegrations = integrations ?? [];
|
|
28
|
+
return createVirtualModulePlugin({
|
|
29
|
+
pluginName: "storybook-renderer-fallback",
|
|
30
|
+
virtualModuleId: "virtual:storybook-renderer-fallback",
|
|
31
|
+
load() {
|
|
32
|
+
return safeIntegrations.filter((integration) => integration.storybookEntryPreview).map(
|
|
33
|
+
(integration) => `export * as ${integration.name} from '${integration.storybookEntryPreview}';`
|
|
34
|
+
).join("\n");
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/vitePluginAstroBuildPrerender.ts
|
|
40
|
+
import { createRequire } from "module";
|
|
41
|
+
import { mkdir, readFile, readdir, writeFile } from "fs/promises";
|
|
42
|
+
import { resolve } from "path";
|
|
43
|
+
import { experimental_AstroContainer as AstroContainer } from "astro/container";
|
|
44
|
+
import { createServer, mergeConfig } from "vite";
|
|
45
|
+
var PRERENDERED_STORIES_FILE = "astro-prerendered-stories.json";
|
|
46
|
+
function vitePluginAstroBuildPrerender(options) {
|
|
47
|
+
const integrations = options.integrations ?? [];
|
|
48
|
+
const resolveFrom = options.resolveFrom ?? process.cwd();
|
|
49
|
+
const storyRulesConfigFilePath = resolveRulesConfigFilePath(options.storyRules, resolveFrom);
|
|
50
|
+
const trackedSpecifiers = collectTrackedSpecifiers(integrations);
|
|
51
|
+
const staticEntrypointRefs = /* @__PURE__ */ new Map();
|
|
52
|
+
const componentEntrypointRefs = /* @__PURE__ */ new Map();
|
|
53
|
+
let outDir = resolve(resolveFrom, "storybook-static");
|
|
54
|
+
return {
|
|
55
|
+
name: "storybook-astro:build-prerender",
|
|
56
|
+
apply: "build",
|
|
57
|
+
enforce: "post",
|
|
58
|
+
configResolved(config) {
|
|
59
|
+
outDir = resolve(resolveFrom, config.build.outDir ?? "storybook-static");
|
|
60
|
+
},
|
|
61
|
+
resolveId(id) {
|
|
62
|
+
if (id.startsWith("virtual:astro-static-module/")) {
|
|
63
|
+
return `\0${id}`;
|
|
64
|
+
}
|
|
65
|
+
if (id.startsWith("virtual:astro-component-module/")) {
|
|
66
|
+
return `\0${id}`;
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
load(id) {
|
|
70
|
+
if (id.startsWith("\0virtual:astro-static-module/")) {
|
|
71
|
+
const encodedSpecifier = id.replace("\0virtual:astro-static-module/", "");
|
|
72
|
+
const specifier = decodeURIComponent(encodedSpecifier);
|
|
73
|
+
if (isClientEntrypoint(specifier)) {
|
|
74
|
+
return [`export { default } from '${specifier}';`, `export * from '${specifier}';`].join("\n");
|
|
75
|
+
}
|
|
76
|
+
return [`import '${specifier}';`, "export default undefined;"].join("\n");
|
|
77
|
+
}
|
|
78
|
+
if (id.startsWith("\0virtual:astro-component-module/")) {
|
|
79
|
+
const encodedSpecifier = id.replace("\0virtual:astro-component-module/", "");
|
|
80
|
+
const specifier = decodeURIComponent(encodedSpecifier);
|
|
81
|
+
return [`export { default } from '${specifier}';`, `export * from '${specifier}';`].join("\n");
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
async buildStart() {
|
|
85
|
+
integrations.forEach((integration) => {
|
|
86
|
+
const entrypoint = integration.renderer.client?.entrypoint;
|
|
87
|
+
if (entrypoint) {
|
|
88
|
+
this.addWatchFile(entrypoint);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
trackedSpecifiers.forEach((specifier) => {
|
|
92
|
+
const fileReferenceId = this.emitFile({
|
|
93
|
+
type: "chunk",
|
|
94
|
+
id: toStaticVirtualId(specifier)
|
|
95
|
+
});
|
|
96
|
+
staticEntrypointRefs.set(specifier, fileReferenceId);
|
|
97
|
+
});
|
|
98
|
+
const srcRoot = resolve(resolveFrom, "src/components");
|
|
99
|
+
const specifiers = await collectHydratableSourceModules(srcRoot);
|
|
100
|
+
specifiers.forEach((specifier) => {
|
|
101
|
+
const fileReferenceId = this.emitFile({
|
|
102
|
+
type: "chunk",
|
|
103
|
+
id: toComponentVirtualId(specifier)
|
|
104
|
+
});
|
|
105
|
+
componentEntrypointRefs.set(specifier, fileReferenceId);
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
async writeBundle() {
|
|
109
|
+
const staticModuleMap = buildStaticModuleMap(
|
|
110
|
+
this,
|
|
111
|
+
staticEntrypointRefs,
|
|
112
|
+
componentEntrypointRefs
|
|
113
|
+
);
|
|
114
|
+
const stories = await collectAstroStories(outDir);
|
|
115
|
+
if (stories.length === 0) {
|
|
116
|
+
await writePrerenderedStoriesFile(outDir, {});
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const prerenderedStories = await prerenderStories({
|
|
120
|
+
stories,
|
|
121
|
+
integrations,
|
|
122
|
+
sanitization: options.sanitization,
|
|
123
|
+
storyRulesConfigFilePath,
|
|
124
|
+
staticModuleMap,
|
|
125
|
+
trackedSpecifiers,
|
|
126
|
+
resolveFrom
|
|
127
|
+
});
|
|
128
|
+
await writePrerenderedStoriesFile(outDir, prerenderedStories);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async function writePrerenderedStoriesFile(outDir, payload) {
|
|
133
|
+
await mkdir(outDir, { recursive: true });
|
|
134
|
+
await writeFile(resolve(outDir, PRERENDERED_STORIES_FILE), JSON.stringify(payload), "utf-8");
|
|
135
|
+
}
|
|
136
|
+
async function prerenderStories(options) {
|
|
137
|
+
const sanitizationOptions = resolveSanitizationOptions(options.sanitization ?? void 0);
|
|
138
|
+
const resolveClientModule = createClientModuleResolver(
|
|
139
|
+
options.integrations,
|
|
140
|
+
options.staticModuleMap
|
|
141
|
+
);
|
|
142
|
+
const viteServer = await createStorySsrServer(
|
|
143
|
+
options.integrations,
|
|
144
|
+
options.trackedSpecifiers,
|
|
145
|
+
options.resolveFrom
|
|
146
|
+
);
|
|
147
|
+
const rulesConfigModule = await loadRulesConfigModule(viteServer, options.storyRulesConfigFilePath);
|
|
148
|
+
try {
|
|
149
|
+
const container = await AstroContainer.create({
|
|
150
|
+
resolve: async (specifier) => {
|
|
151
|
+
const mockedModule = resolveStoryModuleMock(specifier);
|
|
152
|
+
if (mockedModule) {
|
|
153
|
+
return mockedModule;
|
|
154
|
+
}
|
|
155
|
+
const resolution = resolveClientModule(specifier);
|
|
156
|
+
if (resolution) {
|
|
157
|
+
return resolution;
|
|
158
|
+
}
|
|
159
|
+
return specifier;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
await addContainerRenderers(container, options.integrations, resolveClientModule, viteServer);
|
|
163
|
+
const output = {};
|
|
164
|
+
for (const story of options.stories) {
|
|
165
|
+
const selectedRules = await selectStoryRules({
|
|
166
|
+
configModule: rulesConfigModule,
|
|
167
|
+
configFilePath: options.storyRulesConfigFilePath,
|
|
168
|
+
mode: "production",
|
|
169
|
+
story: {
|
|
170
|
+
id: story.id,
|
|
171
|
+
title: story.title,
|
|
172
|
+
name: story.name
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
await applyMswHandlers(selectedRules.mswHandlers);
|
|
176
|
+
if (selectedRules.moduleMocks.size > 0) {
|
|
177
|
+
viteServer.moduleGraph.invalidateAll();
|
|
178
|
+
}
|
|
179
|
+
const html = await withStoryModuleMocks(selectedRules.moduleMocks, async () => {
|
|
180
|
+
const modulePath = resolveImportPath(story.importPath, options.resolveFrom);
|
|
181
|
+
const storyModule = await viteServer.ssrLoadModule(modulePath);
|
|
182
|
+
const meta = isRecord(storyModule.default) ? storyModule.default : {};
|
|
183
|
+
const storyExport = isRecord(storyModule[story.exportName]) ? storyModule[story.exportName] : {};
|
|
184
|
+
if (typeof meta.component !== "function") {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`Unable to prerender story "${story.id}". Missing default export component in ${story.importPath}.`
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
if (storyExport.component && storyExport.component !== meta.component) {
|
|
190
|
+
return void 0;
|
|
191
|
+
}
|
|
192
|
+
const mergedArgs = mergeStoryArgs(toRecord(meta.args), toRecord(storyExport.args));
|
|
193
|
+
const { args, slots } = separateSlots(mergedArgs);
|
|
194
|
+
const processedArgs = await processImageMetadata(args);
|
|
195
|
+
const sanitizedPayload = sanitizeRenderPayload(
|
|
196
|
+
{
|
|
197
|
+
args: processedArgs,
|
|
198
|
+
slots
|
|
199
|
+
},
|
|
200
|
+
sanitizationOptions
|
|
201
|
+
);
|
|
202
|
+
return container.renderToString(patchCreateAstroCompat(meta.component), {
|
|
203
|
+
props: sanitizedPayload.args,
|
|
204
|
+
slots: sanitizedPayload.slots
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
if (html !== void 0) {
|
|
208
|
+
output[story.id] = html;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return output;
|
|
212
|
+
} finally {
|
|
213
|
+
await viteServer.close();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
async function createStorySsrServer(integrations, trackedSpecifiers, resolveFrom) {
|
|
217
|
+
const { getViteConfig } = await importAstroConfig(resolveFrom);
|
|
218
|
+
const astroConfig = await getViteConfig(
|
|
219
|
+
{ root: resolveFrom },
|
|
220
|
+
{
|
|
221
|
+
configFile: false,
|
|
222
|
+
integrations: await Promise.all(
|
|
223
|
+
integrations.map((integration) => integration.loadIntegration(resolveFrom))
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
)({
|
|
227
|
+
mode: "production",
|
|
228
|
+
command: "serve"
|
|
229
|
+
});
|
|
230
|
+
const config = mergeConfig(astroConfig, {
|
|
231
|
+
appType: "custom",
|
|
232
|
+
server: {
|
|
233
|
+
middlewareMode: true
|
|
234
|
+
},
|
|
235
|
+
plugins: [
|
|
236
|
+
createProjectAstroResolutionPlugin(resolveFrom),
|
|
237
|
+
vitePluginAstroFontsFallback(),
|
|
238
|
+
vitePluginAstroVueFallback(),
|
|
239
|
+
vitePluginAstroRoutesFallback(),
|
|
240
|
+
{
|
|
241
|
+
name: "storybook-astro:static-prerender-ssr-stubs",
|
|
242
|
+
resolveId(id) {
|
|
243
|
+
if (trackedSpecifiers.has(id)) {
|
|
244
|
+
return `\0storybook-astro-static-prerender-stub:${encodeURIComponent(id)}`;
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
load(id) {
|
|
248
|
+
if (id.startsWith("\0storybook-astro-static-prerender-stub:")) {
|
|
249
|
+
return "export default undefined;";
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
]
|
|
254
|
+
});
|
|
255
|
+
return createServer(config);
|
|
256
|
+
}
|
|
257
|
+
async function loadRulesConfigModule(viteServer, configFilePath) {
|
|
258
|
+
if (!configFilePath) {
|
|
259
|
+
return void 0;
|
|
260
|
+
}
|
|
261
|
+
try {
|
|
262
|
+
return await ssrLoadModuleWithFsFallback(viteServer, configFilePath, {
|
|
263
|
+
fixStacktrace: true
|
|
264
|
+
});
|
|
265
|
+
} catch (error) {
|
|
266
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
267
|
+
throw new Error(
|
|
268
|
+
`Unable to load framework.options.storyRules config module at ${configFilePath}: ${reason}`
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async function addContainerRenderers(container, integrations, resolveClientModule, viteServer) {
|
|
273
|
+
for (const integration of integrations) {
|
|
274
|
+
const serverRenderer = integration.renderer.server;
|
|
275
|
+
if (serverRenderer) {
|
|
276
|
+
const serverRendererModule = await viteServer.ssrLoadModule(serverRenderer.entrypoint);
|
|
277
|
+
const renderer = serverRendererModule.default ?? serverRendererModule;
|
|
278
|
+
if (integration.name === "solid" && isRecord(renderer)) {
|
|
279
|
+
container.addServerRenderer({
|
|
280
|
+
name: serverRenderer.name,
|
|
281
|
+
renderer: {
|
|
282
|
+
...renderer,
|
|
283
|
+
name: serverRenderer.name
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
} else {
|
|
287
|
+
container.addServerRenderer({
|
|
288
|
+
name: serverRenderer.name,
|
|
289
|
+
renderer
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const clientRenderer = integration.renderer.client;
|
|
294
|
+
if (clientRenderer) {
|
|
295
|
+
const resolvedEntrypoint = resolveClientModule(clientRenderer.entrypoint) ?? clientRenderer.entrypoint;
|
|
296
|
+
container.addClientRenderer({
|
|
297
|
+
name: clientRenderer.name,
|
|
298
|
+
entrypoint: resolvedEntrypoint
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
function createClientModuleResolver(integrations, staticModuleMap) {
|
|
304
|
+
return function resolveClientModule(specifier) {
|
|
305
|
+
if (Object.hasOwn(staticModuleMap, specifier)) {
|
|
306
|
+
return staticModuleMap[specifier];
|
|
307
|
+
}
|
|
308
|
+
const normalizedSpecifier = specifier.replace(/\\/g, "/").replace(/\?.*$/, "");
|
|
309
|
+
if (Object.hasOwn(staticModuleMap, normalizedSpecifier)) {
|
|
310
|
+
return staticModuleMap[normalizedSpecifier];
|
|
311
|
+
}
|
|
312
|
+
for (const integration of integrations) {
|
|
313
|
+
const resolution = integration.resolveClient(specifier);
|
|
314
|
+
if (resolution) {
|
|
315
|
+
return resolution;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
async function collectAstroStories(outDir) {
|
|
321
|
+
const indexFile = resolve(outDir, "index.json");
|
|
322
|
+
const indexRaw = await readFile(indexFile, "utf-8");
|
|
323
|
+
const indexJson = JSON.parse(indexRaw);
|
|
324
|
+
return Object.values(indexJson.entries ?? {}).filter((entry) => entry.type === "story" && entry.componentPath?.endsWith(".astro")).map((entry) => {
|
|
325
|
+
if (!entry.id || !entry.importPath || !entry.exportName) {
|
|
326
|
+
throw new Error(`Encountered an invalid Storybook index entry in ${indexFile}.`);
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
id: entry.id,
|
|
330
|
+
importPath: entry.importPath,
|
|
331
|
+
exportName: entry.exportName,
|
|
332
|
+
title: entry.title,
|
|
333
|
+
name: entry.name
|
|
334
|
+
};
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
function mergeStoryArgs(metaArgs, storyArgs) {
|
|
338
|
+
return {
|
|
339
|
+
...metaArgs ?? {},
|
|
340
|
+
...storyArgs ?? {}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function separateSlots(inputArgs) {
|
|
344
|
+
const args = { ...inputArgs };
|
|
345
|
+
const slotsCandidate = args.slots;
|
|
346
|
+
delete args.slots;
|
|
347
|
+
if (!isRecord(slotsCandidate)) {
|
|
348
|
+
return {
|
|
349
|
+
args,
|
|
350
|
+
slots: {}
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
args,
|
|
355
|
+
slots: slotsCandidate
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
function resolveImportPath(importPath, resolveFrom) {
|
|
359
|
+
if (importPath.startsWith("./")) {
|
|
360
|
+
return resolve(resolveFrom, importPath.slice(2));
|
|
361
|
+
}
|
|
362
|
+
return resolve(resolveFrom, importPath);
|
|
363
|
+
}
|
|
364
|
+
function isRecord(value) {
|
|
365
|
+
return typeof value === "object" && value !== null;
|
|
366
|
+
}
|
|
367
|
+
function toRecord(value) {
|
|
368
|
+
if (!isRecord(value)) {
|
|
369
|
+
return void 0;
|
|
370
|
+
}
|
|
371
|
+
return value;
|
|
372
|
+
}
|
|
373
|
+
function collectTrackedSpecifiers(integrations) {
|
|
374
|
+
const specifiers = /* @__PURE__ */ new Set(["astro:scripts/page.js", "astro:scripts/before-hydration.js"]);
|
|
375
|
+
integrations.forEach((integration) => {
|
|
376
|
+
const entrypoint = integration.renderer.client?.entrypoint;
|
|
377
|
+
if (entrypoint) {
|
|
378
|
+
specifiers.add(entrypoint);
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
return specifiers;
|
|
382
|
+
}
|
|
383
|
+
function buildStaticModuleMap(pluginContext, staticEntrypointRefs, componentEntrypointRefs) {
|
|
384
|
+
const map = {};
|
|
385
|
+
staticEntrypointRefs.forEach((fileReferenceId, specifier) => {
|
|
386
|
+
const fileName = pluginContext.getFileName(fileReferenceId);
|
|
387
|
+
if (fileName) {
|
|
388
|
+
map[specifier] = toPublicPath(fileName);
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
componentEntrypointRefs.forEach((fileReferenceId, specifier) => {
|
|
392
|
+
const fileName = pluginContext.getFileName(fileReferenceId);
|
|
393
|
+
if (fileName) {
|
|
394
|
+
map[specifier] = toPublicPath(fileName);
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
return map;
|
|
398
|
+
}
|
|
399
|
+
function toStaticVirtualId(specifier) {
|
|
400
|
+
return `virtual:astro-static-module/${encodeURIComponent(specifier)}`;
|
|
401
|
+
}
|
|
402
|
+
function toComponentVirtualId(specifier) {
|
|
403
|
+
return `virtual:astro-component-module/${encodeURIComponent(specifier)}`;
|
|
404
|
+
}
|
|
405
|
+
function isClientEntrypoint(specifier) {
|
|
406
|
+
return specifier.startsWith("@astrojs/") && specifier.endsWith("/client.js");
|
|
407
|
+
}
|
|
408
|
+
function toPublicPath(fileName) {
|
|
409
|
+
return `./${fileName}`;
|
|
410
|
+
}
|
|
411
|
+
async function collectHydratableSourceModules(srcRoot) {
|
|
412
|
+
const modules = [];
|
|
413
|
+
async function walk(directory) {
|
|
414
|
+
let entries;
|
|
415
|
+
try {
|
|
416
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
417
|
+
} catch {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
await Promise.all(
|
|
421
|
+
entries.map(async (entry) => {
|
|
422
|
+
const absolutePath = resolve(directory, entry.name);
|
|
423
|
+
if (entry.isDirectory()) {
|
|
424
|
+
await walk(absolutePath);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
if (!entry.isFile()) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
const normalizedPath = absolutePath.replace(/\\/g, "/");
|
|
431
|
+
if (!isHydratableSourceFile(normalizedPath)) {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (isNonHydratableSourceFile(normalizedPath)) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
modules.push(normalizedPath);
|
|
438
|
+
})
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
await walk(srcRoot);
|
|
442
|
+
return modules;
|
|
443
|
+
}
|
|
444
|
+
function isHydratableSourceFile(input) {
|
|
445
|
+
return /\.(jsx|tsx|vue|svelte|js|ts)$/.test(input);
|
|
446
|
+
}
|
|
447
|
+
function isNonHydratableSourceFile(input) {
|
|
448
|
+
return /\.stories\.[jt]sx?$|\.stories\.vue$|\.stories\.svelte$|\.(spec|test)\.[jt]sx?$/.test(
|
|
449
|
+
input
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
function patchCreateAstroCompat(component) {
|
|
453
|
+
if (typeof component !== "function") {
|
|
454
|
+
throw new Error("Expected Astro component factory to be a function.");
|
|
455
|
+
}
|
|
456
|
+
const originalComponent = component;
|
|
457
|
+
const wrapped = ((result, props, slots) => {
|
|
458
|
+
if (result && typeof result.createAstro === "function") {
|
|
459
|
+
const originalCreateAstro = result.createAstro;
|
|
460
|
+
const runtimeExpectsAstroGlobal = originalCreateAstro.length >= 3;
|
|
461
|
+
result.createAstro = (...args) => {
|
|
462
|
+
if (args.length === 3 && !runtimeExpectsAstroGlobal) {
|
|
463
|
+
return originalCreateAstro(args[1], args[2]);
|
|
464
|
+
}
|
|
465
|
+
return originalCreateAstro(...args);
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
return originalComponent(result, props, slots);
|
|
469
|
+
});
|
|
470
|
+
wrapped.isAstroComponentFactory = originalComponent.isAstroComponentFactory;
|
|
471
|
+
wrapped.moduleId = originalComponent.moduleId;
|
|
472
|
+
wrapped.propagation = originalComponent.propagation;
|
|
473
|
+
return wrapped;
|
|
474
|
+
}
|
|
475
|
+
async function processImageMetadata(args) {
|
|
476
|
+
const processed = {};
|
|
477
|
+
for (const [key, value] of Object.entries(args)) {
|
|
478
|
+
if (isImageMetadata(value)) {
|
|
479
|
+
processed[key] = convertImageMetadataToUrl(value);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (Array.isArray(value)) {
|
|
483
|
+
processed[key] = await Promise.all(
|
|
484
|
+
value.map(async (item) => {
|
|
485
|
+
if (isImageMetadata(item)) {
|
|
486
|
+
return convertImageMetadataToUrl(item);
|
|
487
|
+
}
|
|
488
|
+
if (isRecord(item)) {
|
|
489
|
+
return processImageMetadata(item);
|
|
490
|
+
}
|
|
491
|
+
return item;
|
|
492
|
+
})
|
|
493
|
+
);
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
if (isRecord(value)) {
|
|
497
|
+
processed[key] = await processImageMetadata(value);
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
processed[key] = value;
|
|
501
|
+
}
|
|
502
|
+
return processed;
|
|
503
|
+
}
|
|
504
|
+
function isImageMetadata(value) {
|
|
505
|
+
return isRecord(value) && typeof value.src === "string" && ("width" in value || "height" in value || "format" in value);
|
|
506
|
+
}
|
|
507
|
+
function convertImageMetadataToUrl(imageMetadata) {
|
|
508
|
+
const src = imageMetadata.src;
|
|
509
|
+
const fsPath = imageMetadata.fsPath;
|
|
510
|
+
if (typeof src === "string") {
|
|
511
|
+
return src;
|
|
512
|
+
}
|
|
513
|
+
if (typeof fsPath === "string") {
|
|
514
|
+
return fsPath;
|
|
515
|
+
}
|
|
516
|
+
return String(imageMetadata);
|
|
517
|
+
}
|
|
518
|
+
function createProjectAstroResolutionPlugin(resolveFrom) {
|
|
519
|
+
const require2 = createRequire(import.meta.url);
|
|
520
|
+
return {
|
|
521
|
+
name: "storybook-astro:resolve-project-astro-prerender",
|
|
522
|
+
enforce: "pre",
|
|
523
|
+
resolveId(id) {
|
|
524
|
+
if (id !== "astro" && !id.startsWith("astro/")) {
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
try {
|
|
528
|
+
return require2.resolve(id, {
|
|
529
|
+
paths: [resolveFrom]
|
|
530
|
+
});
|
|
531
|
+
} catch {
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// src/vitePluginAstro.ts
|
|
539
|
+
import { mergeConfig as mergeConfig2 } from "vite";
|
|
540
|
+
var ASTRO_PLUGINS_THAT_ARE_SUPPOSEDLY_NOT_NEEDED_IN_STORYBOOK = [
|
|
541
|
+
"@astro/plugin-actions",
|
|
542
|
+
"@astrojs/vite-plugin-astro-ssr-manifest",
|
|
543
|
+
"astro-content-virtual-mod-plugin",
|
|
544
|
+
"astro:actions",
|
|
545
|
+
"astro:build:normal",
|
|
546
|
+
"astro:container",
|
|
547
|
+
"astro:content-asset-propagation",
|
|
548
|
+
"astro:content-imports",
|
|
549
|
+
"astro:content-listen",
|
|
550
|
+
"astro:dev-toolbar",
|
|
551
|
+
"astro:head-metadata",
|
|
552
|
+
"astro:html",
|
|
553
|
+
"astro:i18n",
|
|
554
|
+
"astro:integration-container",
|
|
555
|
+
"astro:jsx",
|
|
556
|
+
"astro:markdown",
|
|
557
|
+
"astro:postprocess",
|
|
558
|
+
"astro:prefetch",
|
|
559
|
+
"astro:scanner",
|
|
560
|
+
"astro:scripts:page-ssr",
|
|
561
|
+
"astro:server",
|
|
562
|
+
"astro:vite-plugin-env",
|
|
563
|
+
"astro:vite-plugin-file-url"
|
|
564
|
+
];
|
|
565
|
+
async function mergeWithAstroConfig(config, integrations = [], resolveFrom = process.cwd(), mode = "development", command = "serve") {
|
|
566
|
+
const { getViteConfig } = await importAstroConfig(resolveFrom);
|
|
567
|
+
const safeIntegrations = integrations ?? [];
|
|
568
|
+
const astroConfig = await getViteConfig(
|
|
569
|
+
{},
|
|
570
|
+
{
|
|
571
|
+
configFile: false,
|
|
572
|
+
integrations: await Promise.all(
|
|
573
|
+
safeIntegrations.map((integration) => integration.loadIntegration(resolveFrom))
|
|
574
|
+
)
|
|
575
|
+
}
|
|
576
|
+
)({
|
|
577
|
+
mode,
|
|
578
|
+
command
|
|
579
|
+
});
|
|
580
|
+
const filteredPlugins = astroConfig.plugins.flat().filter(
|
|
581
|
+
(plugin) => plugin && "name" in plugin && !ASTRO_PLUGINS_THAT_ARE_SUPPOSEDLY_NOT_NEEDED_IN_STORYBOOK.includes(plugin.name)
|
|
582
|
+
);
|
|
583
|
+
return mergeConfig2(config, {
|
|
584
|
+
...astroConfig,
|
|
585
|
+
plugins: filteredPlugins
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// src/preset.ts
|
|
590
|
+
var core = {
|
|
591
|
+
builder: "@storybook/builder-vite",
|
|
592
|
+
renderer: "@storybook-astro/renderer"
|
|
593
|
+
};
|
|
594
|
+
var viteFinal = async (config, { configType, presets }) => {
|
|
595
|
+
const options = await presets.apply("frameworkOptions");
|
|
596
|
+
const { vitePlugin: storybookAstroMiddlewarePlugin, viteConfig } = await vitePluginStorybookAstroMiddleware(options);
|
|
597
|
+
if (!config.plugins) {
|
|
598
|
+
config.plugins = [];
|
|
599
|
+
}
|
|
600
|
+
const integrations = options.integrations ?? [];
|
|
601
|
+
const resolveFrom = options.resolveFrom ?? process.cwd();
|
|
602
|
+
const mode = configType === "DEVELOPMENT" ? "development" : "production";
|
|
603
|
+
const command = configType === "DEVELOPMENT" ? "serve" : "build";
|
|
604
|
+
resolveSanitizationOptions(options.sanitization);
|
|
605
|
+
config.plugins.push(
|
|
606
|
+
storybookAstroMiddlewarePlugin,
|
|
607
|
+
viteStorybookRendererFallbackPlugin(integrations),
|
|
608
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
609
|
+
vitePluginAstroComponentMarker(),
|
|
610
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
611
|
+
vitePluginAstroBuildPrerender(options),
|
|
612
|
+
vitePluginAstroVueFallback(),
|
|
613
|
+
...viteConfig.plugins
|
|
614
|
+
);
|
|
615
|
+
if (!config.resolve) {
|
|
616
|
+
config.resolve = {};
|
|
617
|
+
}
|
|
618
|
+
if (!config.resolve.alias) {
|
|
619
|
+
config.resolve.alias = {};
|
|
620
|
+
}
|
|
621
|
+
const aliases = config.resolve.alias;
|
|
622
|
+
if (!aliases["react"]) {
|
|
623
|
+
aliases["react"] = "react";
|
|
624
|
+
}
|
|
625
|
+
if (!aliases["react-dom"]) {
|
|
626
|
+
aliases["react-dom"] = "react-dom";
|
|
627
|
+
}
|
|
628
|
+
const finalConfig = await mergeWithAstroConfig(config, integrations, resolveFrom, mode, command);
|
|
629
|
+
if (!finalConfig.optimizeDeps) {
|
|
630
|
+
finalConfig.optimizeDeps = {};
|
|
631
|
+
}
|
|
632
|
+
if (!finalConfig.optimizeDeps.exclude) {
|
|
633
|
+
finalConfig.optimizeDeps.exclude = [];
|
|
634
|
+
}
|
|
635
|
+
if (!finalConfig.optimizeDeps.exclude.includes("@astrojs/vue")) {
|
|
636
|
+
finalConfig.optimizeDeps.exclude.push("@astrojs/vue");
|
|
637
|
+
}
|
|
638
|
+
if (!finalConfig.optimizeDeps.esbuildOptions) {
|
|
639
|
+
finalConfig.optimizeDeps.esbuildOptions = {};
|
|
640
|
+
}
|
|
641
|
+
if (!finalConfig.optimizeDeps.esbuildOptions.external) {
|
|
642
|
+
finalConfig.optimizeDeps.esbuildOptions.external = [];
|
|
643
|
+
}
|
|
644
|
+
const vueVirtualModules = ["virtual:@astrojs/vue/app", "virtual:astro:vue-app"];
|
|
645
|
+
for (const mod of vueVirtualModules) {
|
|
646
|
+
if (!finalConfig.optimizeDeps.esbuildOptions.external.includes(mod)) {
|
|
647
|
+
finalConfig.optimizeDeps.esbuildOptions.external.push(mod);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return finalConfig;
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
export {
|
|
654
|
+
core,
|
|
655
|
+
viteFinal
|
|
656
|
+
};
|
|
657
|
+
//# sourceMappingURL=chunk-UK43WNEA.js.map
|