@cappa/plugin-storybook 0.0.16 → 0.0.17
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 +8 -0
- package/package.json +2 -2
- package/src/cappa/plugin.ts +52 -18
- package/src/cappa/util.ts +3 -1
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappa/plugin-storybook",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
4
4
|
"description": "Storybook plugin for Cappa - takes screenshots of Storybook stories",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@swc/core": "^1.13.5",
|
|
32
32
|
"storybook": "^9.1.5",
|
|
33
|
-
"@cappa/core": "0.0.
|
|
33
|
+
"@cappa/core": "0.0.17"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"playwright-core": "^1.55.0",
|
package/src/cappa/plugin.ts
CHANGED
|
@@ -39,10 +39,13 @@ function createLatch<T>() {
|
|
|
39
39
|
* Storybook Plugin for Cappa
|
|
40
40
|
* Fetches stories from a running Storybook server and takes screenshots of each story
|
|
41
41
|
*/
|
|
42
|
-
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
42
|
+
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
43
|
+
options,
|
|
44
|
+
) => {
|
|
43
45
|
return {
|
|
44
46
|
name: "StorybookPlugin",
|
|
45
|
-
description:
|
|
47
|
+
description:
|
|
48
|
+
"Takes screenshots of Storybook stories with configurable options",
|
|
46
49
|
version: "1.0.0",
|
|
47
50
|
execute: async (screenshotTool: ScreenshotTool) => {
|
|
48
51
|
if (!options) {
|
|
@@ -68,7 +71,9 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) =>
|
|
|
68
71
|
// Fetch stories from Storybook
|
|
69
72
|
const response = await fetch(storiesUrl);
|
|
70
73
|
if (!response.ok) {
|
|
71
|
-
throw new Error(
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Failed to fetch stories: ${response.status} ${response.statusText}`,
|
|
76
|
+
);
|
|
72
77
|
}
|
|
73
78
|
|
|
74
79
|
const data = (await response.json()) as StorybookStoriesResponse;
|
|
@@ -79,18 +84,27 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) =>
|
|
|
79
84
|
|
|
80
85
|
if (includeStories.length > 0) {
|
|
81
86
|
filteredStories = filteredStories.filter((story) =>
|
|
82
|
-
includeStories.some(
|
|
87
|
+
includeStories.some(
|
|
88
|
+
(pattern) =>
|
|
89
|
+
story.title.includes(pattern) || story.name.includes(pattern),
|
|
90
|
+
),
|
|
83
91
|
);
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
if (excludeStories.length > 0) {
|
|
87
95
|
filteredStories = filteredStories.filter(
|
|
88
|
-
(story) =>
|
|
96
|
+
(story) =>
|
|
97
|
+
!excludeStories.some(
|
|
98
|
+
(pattern) =>
|
|
99
|
+
story.title.includes(pattern) || story.name.includes(pattern),
|
|
100
|
+
),
|
|
89
101
|
);
|
|
90
102
|
}
|
|
91
103
|
|
|
92
104
|
// Filter docs etc
|
|
93
|
-
filteredStories = filteredStories.filter(
|
|
105
|
+
filteredStories = filteredStories.filter(
|
|
106
|
+
(story) => story.type === "story",
|
|
107
|
+
);
|
|
94
108
|
|
|
95
109
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
96
110
|
|
|
@@ -106,9 +120,12 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) =>
|
|
|
106
120
|
|
|
107
121
|
let currentLatch = createLatch<ScreenshotOptions>();
|
|
108
122
|
|
|
109
|
-
await screenshotTool.page?.exposeFunction(
|
|
110
|
-
|
|
111
|
-
|
|
123
|
+
await screenshotTool.page?.exposeFunction(
|
|
124
|
+
"__cappa_parameters",
|
|
125
|
+
async (payload: ScreenshotOptions) => {
|
|
126
|
+
currentLatch.resolve(payload);
|
|
127
|
+
},
|
|
128
|
+
);
|
|
112
129
|
|
|
113
130
|
// Take screenshots of each story
|
|
114
131
|
for (const story of filteredStories) {
|
|
@@ -157,12 +174,18 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) =>
|
|
|
157
174
|
const filename = buildFilename(story);
|
|
158
175
|
|
|
159
176
|
// Actually take the screenshot
|
|
160
|
-
const actualFilepath = await screenshotTool.takeScreenshot(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
177
|
+
const actualFilepath = await screenshotTool.takeScreenshot(
|
|
178
|
+
page,
|
|
179
|
+
filename,
|
|
180
|
+
{
|
|
181
|
+
fullPage: options.fullPage ?? screenshotOptions.fullPage,
|
|
182
|
+
type: options.type ?? screenshotOptions.type,
|
|
183
|
+
waitForSelector:
|
|
184
|
+
options.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
185
|
+
waitForTimeout:
|
|
186
|
+
options.waitForTimeout ?? screenshotOptions.waitForTimeout,
|
|
187
|
+
},
|
|
188
|
+
);
|
|
166
189
|
|
|
167
190
|
results.push({
|
|
168
191
|
storyId: story.id,
|
|
@@ -171,7 +194,10 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) =>
|
|
|
171
194
|
filepath: actualFilepath,
|
|
172
195
|
});
|
|
173
196
|
} catch (error) {
|
|
174
|
-
console.error(
|
|
197
|
+
console.error(
|
|
198
|
+
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
199
|
+
(error as Error).message,
|
|
200
|
+
);
|
|
175
201
|
results.push({
|
|
176
202
|
storyId: story.id,
|
|
177
203
|
storyName: `${story.title} - ${story.name}`,
|
|
@@ -181,11 +207,19 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) =>
|
|
|
181
207
|
}
|
|
182
208
|
}
|
|
183
209
|
|
|
210
|
+
// Print error report if there are any errors
|
|
184
211
|
if (results.some((r) => !r.success)) {
|
|
185
|
-
console.log(
|
|
212
|
+
console.log(
|
|
213
|
+
`Error report: \n${results
|
|
214
|
+
.filter((r) => !r.success)
|
|
215
|
+
.map((r) => `${r.storyName}: ${r.error}`)
|
|
216
|
+
.join("\n")}`,
|
|
217
|
+
);
|
|
186
218
|
}
|
|
187
219
|
|
|
188
|
-
console.log(
|
|
220
|
+
console.log(
|
|
221
|
+
`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`,
|
|
222
|
+
);
|
|
189
223
|
return results;
|
|
190
224
|
} catch (error) {
|
|
191
225
|
console.error("Error in Storybook plugin:", (error as Error).message);
|
package/src/cappa/util.ts
CHANGED
|
@@ -28,7 +28,9 @@ export const waitForVisualIdle = async (page: Page) => {
|
|
|
28
28
|
if (document.fonts?.ready) await document.fonts.ready;
|
|
29
29
|
|
|
30
30
|
// Wait for a frame to be idle in the requestAnimationFrame queue
|
|
31
|
-
await new Promise((r) =>
|
|
31
|
+
await new Promise((r) =>
|
|
32
|
+
requestAnimationFrame(() => requestAnimationFrame(r)),
|
|
33
|
+
);
|
|
32
34
|
});
|
|
33
35
|
};
|
|
34
36
|
|