@cappa/plugin-storybook 0.1.3 → 0.2.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/.turbo/turbo-build.log +13 -13
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +1 -91
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +192 -146
- package/dist/index.mjs +192 -146
- package/dist/preview.mjs +1 -1
- package/package.json +7 -7
- package/src/cappa/plugin.ts +213 -184
- package/src/cappa/storybook-url.test.ts +43 -0
- package/src/cappa/storybook-url.ts +55 -0
- package/src/storybook/addon/decorator.ts +1 -1
- package/src/storybook/util.ts +1 -1
- package/src/types.ts +29 -4
- package/tsconfig.json +13 -0
- package/src/tsconfig.json +0 -7
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,42 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
|
|
8
8
|
// src/cappa/plugin.ts
|
|
9
9
|
import { getLogger } from "@cappa/logger";
|
|
10
|
-
|
|
10
|
+
|
|
11
|
+
// src/cappa/storybook-url.ts
|
|
12
|
+
function encodePairs(obj) {
|
|
13
|
+
if (!obj || !Object.keys(obj).length) return "";
|
|
14
|
+
return Object.entries(obj).filter(([, v]) => v !== void 0).map(([k, v]) => {
|
|
15
|
+
const val = typeof v === "string" ? encodeURIComponent(v) : typeof v === "number" || typeof v === "boolean" ? String(v) : encodeURIComponent(JSON.stringify(v));
|
|
16
|
+
return `${encodeURIComponent(k)}:${val}`;
|
|
17
|
+
}).join(";");
|
|
18
|
+
}
|
|
19
|
+
function buildStorybookIframeUrl({
|
|
20
|
+
baseUrl,
|
|
21
|
+
storyId,
|
|
22
|
+
viewMode = "story",
|
|
23
|
+
args,
|
|
24
|
+
globals,
|
|
25
|
+
query,
|
|
26
|
+
fullscreen = true,
|
|
27
|
+
singleStory = true
|
|
28
|
+
}) {
|
|
29
|
+
const u = new URL("/iframe.html", baseUrl);
|
|
30
|
+
u.searchParams.set("id", storyId);
|
|
31
|
+
u.searchParams.set("viewMode", viewMode);
|
|
32
|
+
const argPairs = encodePairs(args);
|
|
33
|
+
if (argPairs) u.searchParams.set("args", argPairs);
|
|
34
|
+
const globalPairs = encodePairs(globals);
|
|
35
|
+
if (globalPairs) u.searchParams.set("globals", globalPairs);
|
|
36
|
+
if (fullscreen) u.searchParams.set("full", "1");
|
|
37
|
+
if (singleStory) u.searchParams.set("singleStory", "true");
|
|
38
|
+
if (query) {
|
|
39
|
+
for (const [k, v] of Object.entries(query)) {
|
|
40
|
+
if (v === void 0) continue;
|
|
41
|
+
u.searchParams.set(k, String(v));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return u.toString();
|
|
45
|
+
}
|
|
11
46
|
|
|
12
47
|
// src/cappa/util.ts
|
|
13
48
|
var freezeUI = async (page) => {
|
|
@@ -93,8 +128,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
93
128
|
return {
|
|
94
129
|
name: "StorybookPlugin",
|
|
95
130
|
description: "Takes screenshots of Storybook stories with configurable options",
|
|
96
|
-
|
|
97
|
-
execute: async (screenshotTool) => {
|
|
131
|
+
discover: async () => {
|
|
98
132
|
if (!options) {
|
|
99
133
|
throw new Error("Storybook plugin options are required");
|
|
100
134
|
}
|
|
@@ -105,7 +139,8 @@ var cappaPluginStorybook = (options) => {
|
|
|
105
139
|
const {
|
|
106
140
|
storybookUrl,
|
|
107
141
|
includeStories = [],
|
|
108
|
-
excludeStories = []
|
|
142
|
+
excludeStories = [],
|
|
143
|
+
storybook: defaultStorybookOptions
|
|
109
144
|
} = options;
|
|
110
145
|
const storiesUrl = `${storybookUrl}/index.json`;
|
|
111
146
|
logger.debug(`Fetching stories from: ${storiesUrl}`);
|
|
@@ -115,159 +150,170 @@ var cappaPluginStorybook = (options) => {
|
|
|
115
150
|
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
116
151
|
);
|
|
117
152
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (includeStories.length > 0) {
|
|
123
|
-
filteredStories = filteredStories.filter(
|
|
124
|
-
(story) => includeStories.some(
|
|
125
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
126
|
-
)
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
if (excludeStories.length > 0) {
|
|
130
|
-
filteredStories = filteredStories.filter(
|
|
131
|
-
(story) => !excludeStories.some(
|
|
132
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
133
|
-
)
|
|
134
|
-
);
|
|
135
|
-
}
|
|
153
|
+
const data = await response.json();
|
|
154
|
+
const stories = Object.values(data.entries);
|
|
155
|
+
let filteredStories = stories;
|
|
156
|
+
if (includeStories.length > 0) {
|
|
136
157
|
filteredStories = filteredStories.filter(
|
|
137
|
-
(story) =>
|
|
158
|
+
(story) => includeStories.some(
|
|
159
|
+
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
160
|
+
)
|
|
138
161
|
);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
await screenshotTool.page?.exposeFunction(
|
|
146
|
-
"__cappa_parameters",
|
|
147
|
-
async (payload) => {
|
|
148
|
-
currentLatch.resolve(payload);
|
|
149
|
-
}
|
|
162
|
+
}
|
|
163
|
+
if (excludeStories.length > 0) {
|
|
164
|
+
filteredStories = filteredStories.filter(
|
|
165
|
+
(story) => !excludeStories.some(
|
|
166
|
+
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
167
|
+
)
|
|
150
168
|
);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
fullPage: options2.fullPage,
|
|
194
|
-
mask: options2.mask?.map(
|
|
195
|
-
(selector) => page.locator(selector)
|
|
196
|
-
),
|
|
197
|
-
omitBackground: options2.omitBackground,
|
|
198
|
-
saveDiffImage: true
|
|
199
|
-
}
|
|
200
|
-
);
|
|
201
|
-
actualFilepath = screenshotPath;
|
|
202
|
-
results.push({
|
|
203
|
-
storyId: story.id,
|
|
204
|
-
storyName: `${story.title} - ${story.name}`,
|
|
205
|
-
filepath: actualFilepath,
|
|
206
|
-
success: comparisonResult.passed
|
|
207
|
-
});
|
|
208
|
-
} else {
|
|
209
|
-
logger.info(
|
|
210
|
-
`Expected image not found for story ${story.title} - ${story.name}, taking screenshot`
|
|
211
|
-
);
|
|
212
|
-
actualFilepath = await screenshotTool.takeScreenshot(
|
|
213
|
-
page,
|
|
214
|
-
filename,
|
|
215
|
-
{
|
|
216
|
-
fullPage: options2.fullPage,
|
|
217
|
-
mask: options2.mask?.map((selector) => page.locator(selector)),
|
|
218
|
-
omitBackground: options2.omitBackground
|
|
219
|
-
}
|
|
220
|
-
);
|
|
221
|
-
results.push({
|
|
222
|
-
storyId: story.id,
|
|
223
|
-
storyName: `${story.title} - ${story.name}`,
|
|
224
|
-
filepath: actualFilepath,
|
|
225
|
-
success: true
|
|
226
|
-
});
|
|
169
|
+
}
|
|
170
|
+
filteredStories = filteredStories.filter(
|
|
171
|
+
(story) => story.type === "story"
|
|
172
|
+
);
|
|
173
|
+
logger.start(`Found ${filteredStories.length} stories to screenshot...`);
|
|
174
|
+
return filteredStories.map((story) => ({
|
|
175
|
+
id: story.id,
|
|
176
|
+
url: buildStorybookIframeUrl({
|
|
177
|
+
baseUrl: storybookUrl,
|
|
178
|
+
storyId: story.id,
|
|
179
|
+
viewMode: defaultStorybookOptions?.viewMode,
|
|
180
|
+
args: defaultStorybookOptions?.args,
|
|
181
|
+
globals: defaultStorybookOptions?.globals,
|
|
182
|
+
query: defaultStorybookOptions?.query,
|
|
183
|
+
fullscreen: defaultStorybookOptions?.fullscreen,
|
|
184
|
+
singleStory: defaultStorybookOptions?.singleStory
|
|
185
|
+
}),
|
|
186
|
+
data: {
|
|
187
|
+
story
|
|
188
|
+
}
|
|
189
|
+
}));
|
|
190
|
+
},
|
|
191
|
+
initPage: async () => {
|
|
192
|
+
return {
|
|
193
|
+
latchMap: /* @__PURE__ */ new Map()
|
|
194
|
+
};
|
|
195
|
+
},
|
|
196
|
+
// Phase 2: Execute single story
|
|
197
|
+
execute: async (task, page, screenshotTool, context) => {
|
|
198
|
+
const { url, data } = task;
|
|
199
|
+
const { story } = data;
|
|
200
|
+
const logger = getLogger();
|
|
201
|
+
try {
|
|
202
|
+
const currentLatch = createLatch();
|
|
203
|
+
context.latchMap.set(story.id, currentLatch);
|
|
204
|
+
if (await page.evaluate(() => {
|
|
205
|
+
return window.__cappa_parameters === void 0;
|
|
206
|
+
})) {
|
|
207
|
+
await page.exposeFunction(
|
|
208
|
+
"__cappa_parameters",
|
|
209
|
+
async (id, payload) => {
|
|
210
|
+
context.latchMap.get(id)?.resolve?.(payload);
|
|
227
211
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
page.on("console", (message) => {
|
|
215
|
+
logger.debug("console", message.text());
|
|
216
|
+
});
|
|
217
|
+
page.on("pageerror", (error) => {
|
|
218
|
+
logger.debug("pageerror", error);
|
|
219
|
+
});
|
|
220
|
+
await page.goto(url);
|
|
221
|
+
const storyParameters = await context.latchMap.get(story.id)?.p;
|
|
222
|
+
context.latchMap.delete(story.id);
|
|
223
|
+
const variantParameters = storyParameters?.variants ?? [];
|
|
224
|
+
if (storyParameters?.skip) {
|
|
225
|
+
logger.warn(`Skipping story ${story.title} - ${story.name}`);
|
|
226
|
+
return {
|
|
227
|
+
storyId: story.id,
|
|
228
|
+
storyName: `${story.title} - ${story.name}`,
|
|
229
|
+
skipped: true
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
if (storyParameters && Object.keys(storyParameters).length > 0) {
|
|
233
|
+
logger.debug("Taking screenshot with options", storyParameters);
|
|
239
234
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
235
|
+
const resolvedViewport = storyParameters?.viewport ?? screenshotTool.viewport;
|
|
236
|
+
await page.setViewportSize(resolvedViewport);
|
|
237
|
+
await freezeUI(page);
|
|
238
|
+
await waitForVisualIdle(page);
|
|
239
|
+
const toLocatorMask = (mask) => mask?.map((selector) => page.locator(selector));
|
|
240
|
+
const screenshotVariants = variantParameters.map(
|
|
241
|
+
(variant) => ({
|
|
242
|
+
id: variant.id,
|
|
243
|
+
label: variant.label,
|
|
244
|
+
filename: variant.filename,
|
|
245
|
+
options: variant.options ? {
|
|
246
|
+
...variant.options,
|
|
247
|
+
mask: toLocatorMask(variant.options.mask)
|
|
248
|
+
} : void 0
|
|
249
|
+
})
|
|
250
|
+
);
|
|
251
|
+
const screenshotOptions = {
|
|
252
|
+
fullPage: storyParameters?.fullPage,
|
|
253
|
+
delay: storyParameters?.delay,
|
|
254
|
+
omitBackground: storyParameters?.omitBackground,
|
|
255
|
+
skip: storyParameters?.skip,
|
|
256
|
+
viewport: storyParameters?.viewport ?? screenshotTool.viewport,
|
|
257
|
+
mask: toLocatorMask(storyParameters?.mask),
|
|
258
|
+
variants: screenshotVariants.length ? screenshotVariants : void 0
|
|
259
|
+
};
|
|
260
|
+
const filename = buildFilename(story);
|
|
261
|
+
const expectedExists = screenshotTool.filesystem.hasExpectedFile(filename);
|
|
262
|
+
if (!expectedExists) {
|
|
263
|
+
logger.info(
|
|
264
|
+
`Expected image not found for story ${story.title} - ${story.name}, taking screenshot`
|
|
244
265
|
);
|
|
245
266
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
267
|
+
const variantFilenameMap = /* @__PURE__ */ new Map();
|
|
268
|
+
for (const variant of screenshotVariants) {
|
|
269
|
+
const variantFilename = screenshotTool.getVariantFilename(
|
|
270
|
+
filename,
|
|
271
|
+
variant
|
|
250
272
|
);
|
|
273
|
+
variantFilenameMap.set(variant.id, variantFilename);
|
|
274
|
+
if (!screenshotTool.filesystem.hasExpectedFile(variantFilename)) {
|
|
275
|
+
logger.info(
|
|
276
|
+
`Expected image not found for variant ${story.title} - ${story.name} [${variant.label ?? variant.id}], taking screenshot`
|
|
277
|
+
);
|
|
278
|
+
}
|
|
251
279
|
}
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
280
|
+
const variantExtrasEntries = screenshotVariants.map(
|
|
281
|
+
(variant) => [
|
|
282
|
+
variant.id,
|
|
283
|
+
{
|
|
284
|
+
saveDiffImage: true,
|
|
285
|
+
diffImageFilename: variantFilenameMap.get(variant.id) ?? filename
|
|
286
|
+
}
|
|
287
|
+
]
|
|
255
288
|
);
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
return
|
|
289
|
+
const captureExtras = {
|
|
290
|
+
saveDiffImage: true,
|
|
291
|
+
diffImageFilename: filename,
|
|
292
|
+
variants: variantExtrasEntries.length ? Object.fromEntries(variantExtrasEntries) : void 0
|
|
293
|
+
};
|
|
294
|
+
const captureResult = await screenshotTool.capture(
|
|
295
|
+
page,
|
|
296
|
+
filename,
|
|
297
|
+
screenshotOptions,
|
|
298
|
+
captureExtras
|
|
299
|
+
);
|
|
300
|
+
return {
|
|
301
|
+
storyId: story.id,
|
|
302
|
+
storyName: `${story.title} - ${story.name}`,
|
|
303
|
+
filepath: captureResult.base.filepath,
|
|
304
|
+
success: captureResult.base.skipped ? void 0 : captureResult.base.comparisonResult ? captureResult.base.comparisonResult.passed : Boolean(captureResult.base.filepath),
|
|
305
|
+
skipped: captureResult.base.skipped
|
|
306
|
+
};
|
|
268
307
|
} catch (error) {
|
|
269
|
-
logger.error(
|
|
270
|
-
|
|
308
|
+
logger.error(
|
|
309
|
+
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
310
|
+
error.message
|
|
311
|
+
);
|
|
312
|
+
return {
|
|
313
|
+
storyId: story.id,
|
|
314
|
+
storyName: `${story.title} - ${story.name}`,
|
|
315
|
+
error: error?.message ?? "Unknown error"
|
|
316
|
+
};
|
|
271
317
|
}
|
|
272
318
|
}
|
|
273
319
|
};
|
package/dist/preview.mjs
CHANGED
|
@@ -29,7 +29,7 @@ var withCappaDecorator = makeDecorator({
|
|
|
29
29
|
queueMicrotask(async () => {
|
|
30
30
|
try {
|
|
31
31
|
const cappaParameters = await waitForExposed("__cappa_parameters");
|
|
32
|
-
cappaParameters(parameters2 ?? {});
|
|
32
|
+
cappaParameters(context.id, parameters2 ?? {});
|
|
33
33
|
} catch (error) {
|
|
34
34
|
console.error(error);
|
|
35
35
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappa/plugin-storybook",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Storybook plugin for Cappa - takes screenshots of Storybook stories",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
"main": "./dist/index.js",
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
10
|
"module": "CommonJS",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Tim Richter",
|
|
11
13
|
"exports": {
|
|
12
14
|
".": {
|
|
13
15
|
"import": "./dist/index.js",
|
|
@@ -25,21 +27,19 @@
|
|
|
25
27
|
"storybook",
|
|
26
28
|
"visual-testing"
|
|
27
29
|
],
|
|
28
|
-
"author": "Tim Richter",
|
|
29
|
-
"license": "MIT",
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@swc/core": "^1.13.5",
|
|
32
32
|
"chalk": "^5.6.2",
|
|
33
|
-
"storybook": "^9.1.
|
|
34
|
-
"@cappa/core": "0.
|
|
33
|
+
"storybook": "^9.1.12",
|
|
34
|
+
"@cappa/core": "0.2.0",
|
|
35
35
|
"@cappa/logger": "0.0.3"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@vitest/coverage-v8": "3.2.4",
|
|
39
39
|
"@vitest/ui": "3.2.4",
|
|
40
|
-
"playwright-core": "^1.
|
|
40
|
+
"playwright-core": "^1.56.0",
|
|
41
41
|
"tsup": "8.5.0",
|
|
42
|
-
"typescript": "5.
|
|
42
|
+
"typescript": "5.9.3",
|
|
43
43
|
"vitest": "3.2.4"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|