@cappa/plugin-storybook 0.0.11 → 0.0.13
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 +36 -28
- package/CHANGELOG.md +16 -0
- package/README.md +96 -0
- package/dist/browser.mjs +7 -0
- package/dist/index.d.mts +14 -17
- package/dist/index.d.ts +14 -17
- package/dist/index.js +165 -182
- package/dist/index.mjs +143 -166
- package/dist/preview.mjs +58 -0
- package/package.json +14 -7
- package/src/cappa/plugin.ts +22 -53
- package/src/cappa/util.ts +2 -4
- package/src/index.ts +2 -1
- package/src/storybook/addon/browser.ts +6 -0
- package/src/storybook/{decorator.ts → addon/decorator.ts} +6 -1
- package/src/storybook/addon/preview.ts +15 -0
- package/src/storybook/util.ts +1 -1
- package/tsup.config.ts +29 -0
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/storybook/screenshot.ts +0 -108
- package/tsdown.config.ts +0 -10
package/dist/index.mjs
CHANGED
|
@@ -1,178 +1,155 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
3
7
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*/
|
|
9
|
-
const freezeUI = async (page) => {
|
|
10
|
-
await page.addStyleTag({ content: `
|
|
8
|
+
// src/cappa/util.ts
|
|
9
|
+
var freezeUI = async (page) => {
|
|
10
|
+
await page.addStyleTag({
|
|
11
|
+
content: `
|
|
11
12
|
* { transition: none !important; animation: none !important }
|
|
12
13
|
* { caret-color: transparent !important }
|
|
13
14
|
::-webkit-scrollbar { display: none }
|
|
14
|
-
`
|
|
15
|
+
`
|
|
16
|
+
});
|
|
15
17
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
await page.evaluate(async () => {
|
|
23
|
-
if (document.fonts?.ready) await document.fonts.ready;
|
|
24
|
-
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
|
25
|
-
});
|
|
18
|
+
var waitForVisualIdle = async (page) => {
|
|
19
|
+
await page.waitForLoadState("networkidle");
|
|
20
|
+
await page.evaluate(async () => {
|
|
21
|
+
if (document.fonts?.ready) await document.fonts.ready;
|
|
22
|
+
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
|
23
|
+
});
|
|
26
24
|
};
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
//#region src/cappa/plugin.ts
|
|
30
|
-
/**
|
|
31
|
-
* Create a latch for a Promise. A latch is a Promise that can be resolved from outside.
|
|
32
|
-
* @param T - The type of the Promise
|
|
33
|
-
* @returns A latch for a Promise
|
|
34
|
-
*/
|
|
26
|
+
// src/cappa/plugin.ts
|
|
35
27
|
function createLatch() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
resolve
|
|
40
|
-
};
|
|
28
|
+
let resolve;
|
|
29
|
+
const p = new Promise((r) => resolve = r);
|
|
30
|
+
return { p, resolve };
|
|
41
31
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
32
|
+
var cappaPluginStorybook = (options) => {
|
|
33
|
+
return {
|
|
34
|
+
name: "StorybookPlugin",
|
|
35
|
+
description: "Takes screenshots of Storybook stories with configurable options",
|
|
36
|
+
version: "1.0.0",
|
|
37
|
+
execute: async (screenshotTool) => {
|
|
38
|
+
if (!options) {
|
|
39
|
+
throw new Error("Storybook plugin options are required");
|
|
40
|
+
}
|
|
41
|
+
if (!options.storybookUrl) {
|
|
42
|
+
throw new Error("storybookUrl is required");
|
|
43
|
+
}
|
|
44
|
+
const {
|
|
45
|
+
storybookUrl,
|
|
46
|
+
includeStories = [],
|
|
47
|
+
excludeStories = [],
|
|
48
|
+
defaultViewport = { width: 1920, height: 1080 },
|
|
49
|
+
waitForSelector,
|
|
50
|
+
waitForTimeout
|
|
51
|
+
} = options;
|
|
52
|
+
const storiesUrl = `${storybookUrl}/index.json`;
|
|
53
|
+
console.log(`Fetching stories from: ${storiesUrl}`);
|
|
54
|
+
try {
|
|
55
|
+
const response = await fetch(storiesUrl);
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
58
|
+
}
|
|
59
|
+
const data = await response.json();
|
|
60
|
+
const stories = Object.values(data.entries);
|
|
61
|
+
let filteredStories = stories;
|
|
62
|
+
if (includeStories.length > 0) {
|
|
63
|
+
filteredStories = filteredStories.filter(
|
|
64
|
+
(story) => includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (excludeStories.length > 0) {
|
|
68
|
+
filteredStories = filteredStories.filter(
|
|
69
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
73
|
+
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
74
|
+
const results = [];
|
|
75
|
+
await screenshotTool.init();
|
|
76
|
+
let currentLatch = createLatch();
|
|
77
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload) => {
|
|
78
|
+
currentLatch.resolve(payload);
|
|
79
|
+
});
|
|
80
|
+
for (const story of filteredStories) {
|
|
81
|
+
try {
|
|
82
|
+
const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
|
|
83
|
+
const screenshotOptions = {
|
|
84
|
+
viewport: defaultViewport,
|
|
85
|
+
waitForSelector,
|
|
86
|
+
waitForTimeout,
|
|
87
|
+
fullPage: true,
|
|
88
|
+
quality: 90,
|
|
89
|
+
type: "jpeg"
|
|
90
|
+
};
|
|
91
|
+
const filename = `${story.title.replace(
|
|
92
|
+
/[^a-zA-Z0-9]/g,
|
|
93
|
+
"_"
|
|
94
|
+
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${screenshotOptions.type}`;
|
|
95
|
+
const page = screenshotTool.page;
|
|
96
|
+
if (!page) {
|
|
97
|
+
throw new Error("Page not initialized");
|
|
98
|
+
}
|
|
99
|
+
page.on("console", (message) => {
|
|
100
|
+
console.log("console", message.text());
|
|
101
|
+
});
|
|
102
|
+
page.on("pageerror", (error) => {
|
|
103
|
+
console.log("pageerror", error);
|
|
104
|
+
});
|
|
105
|
+
await page.goto(storyUrl);
|
|
106
|
+
const options2 = await currentLatch.p;
|
|
107
|
+
currentLatch = createLatch();
|
|
108
|
+
if (options2.skip) {
|
|
109
|
+
console.log(`Skipping story ${story.title} - ${story.name}`);
|
|
110
|
+
}
|
|
111
|
+
await freezeUI(page);
|
|
112
|
+
await waitForVisualIdle(page);
|
|
113
|
+
if (options2.delay) {
|
|
114
|
+
await page.waitForTimeout(options2.delay);
|
|
115
|
+
}
|
|
116
|
+
const actualFilepath = await screenshotTool.takeScreenshot(page, filename, {
|
|
117
|
+
fullPage: options2.fullPage ?? screenshotOptions.fullPage,
|
|
118
|
+
type: options2.type ?? screenshotOptions.type,
|
|
119
|
+
waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
120
|
+
waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
|
|
121
|
+
});
|
|
122
|
+
results.push({
|
|
123
|
+
storyId: story.id,
|
|
124
|
+
storyName: story.name,
|
|
125
|
+
storyTitle: story.title,
|
|
126
|
+
success: true,
|
|
127
|
+
filepath: actualFilepath
|
|
128
|
+
});
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, error.message);
|
|
131
|
+
results.push({
|
|
132
|
+
storyId: story.id,
|
|
133
|
+
storyName: story.name,
|
|
134
|
+
storyTitle: story.title,
|
|
135
|
+
success: false,
|
|
136
|
+
error: error.message
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
141
|
+
return results;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.error("Error in Storybook plugin:", error.message);
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
132
148
|
};
|
|
133
149
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
* @param timeoutMs - The timeout in milliseconds
|
|
140
|
-
* @returns The function or rejects if the function is not exposed within the timeout
|
|
141
|
-
*/
|
|
142
|
-
const waitForExposed = (name, timeoutMs = 5e3) => {
|
|
143
|
-
return new Promise((resolve, reject) => {
|
|
144
|
-
const start = performance.now();
|
|
145
|
-
const t = setInterval(() => {
|
|
146
|
-
const fn = window[name];
|
|
147
|
-
if (typeof fn === "function") {
|
|
148
|
-
clearInterval(t);
|
|
149
|
-
resolve(fn);
|
|
150
|
-
} else if (performance.now() - start > timeoutMs) {
|
|
151
|
-
clearInterval(t);
|
|
152
|
-
reject(/* @__PURE__ */ new Error(`Timed out waiting for ${name}`));
|
|
153
|
-
}
|
|
154
|
-
}, 16);
|
|
155
|
-
});
|
|
150
|
+
// src/index.ts
|
|
151
|
+
var previewAnnotations = [__require.resolve("./preview.mjs")];
|
|
152
|
+
export {
|
|
153
|
+
cappaPluginStorybook,
|
|
154
|
+
previewAnnotations
|
|
156
155
|
};
|
|
157
|
-
|
|
158
|
-
//#endregion
|
|
159
|
-
//#region src/storybook/decorator.ts
|
|
160
|
-
const withCappaDecorator = makeDecorator({
|
|
161
|
-
name: "withCappa",
|
|
162
|
-
parameterName: "cappa",
|
|
163
|
-
skipIfNoParametersOrOptions: false,
|
|
164
|
-
wrapper: (getStory, context, { parameters }) => {
|
|
165
|
-
queueMicrotask(async () => {
|
|
166
|
-
try {
|
|
167
|
-
(await waitForExposed("__cappa_parameters"))(parameters ?? {});
|
|
168
|
-
} catch (error) {
|
|
169
|
-
console.error(error);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
return getStory(context);
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
//#endregion
|
|
177
|
-
export { cappaPluginStorybook, withCappaDecorator };
|
|
178
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/preview.mjs
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/storybook/addon/decorator.ts
|
|
2
|
+
import { makeDecorator } from "storybook/preview-api";
|
|
3
|
+
|
|
4
|
+
// src/storybook/util.ts
|
|
5
|
+
var waitForExposed = (name, timeoutMs = 5e3) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
const start = performance.now();
|
|
8
|
+
const t = setInterval(() => {
|
|
9
|
+
const fn = window[name];
|
|
10
|
+
if (typeof fn === "function") {
|
|
11
|
+
clearInterval(t);
|
|
12
|
+
resolve(fn);
|
|
13
|
+
} else if (performance.now() - start > timeoutMs) {
|
|
14
|
+
clearInterval(t);
|
|
15
|
+
reject(new Error(`Timed out waiting for ${name}`));
|
|
16
|
+
}
|
|
17
|
+
}, 16);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/storybook/addon/decorator.ts
|
|
22
|
+
var withCappaDecorator = makeDecorator({
|
|
23
|
+
name: "withCappa",
|
|
24
|
+
parameterName: "cappa",
|
|
25
|
+
skipIfNoParametersOrOptions: false,
|
|
26
|
+
wrapper: (getStory, context, { parameters: parameters2 }) => {
|
|
27
|
+
window.__cappa_screenshot = true;
|
|
28
|
+
const hasPlayFunction = context.playFunction != null;
|
|
29
|
+
queueMicrotask(async () => {
|
|
30
|
+
try {
|
|
31
|
+
console.log("hasPlayFunction", hasPlayFunction);
|
|
32
|
+
const cappaParameters = await waitForExposed("__cappa_parameters");
|
|
33
|
+
cappaParameters(parameters2 ?? {});
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(error);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return getStory(context);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// src/storybook/addon/preview.ts
|
|
43
|
+
var decorators = [withCappaDecorator];
|
|
44
|
+
var parameters = {
|
|
45
|
+
layout: "full",
|
|
46
|
+
cappa: {
|
|
47
|
+
enabled: true
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var afterEach = (context) => {
|
|
51
|
+
console.log("afterEach", context);
|
|
52
|
+
window.playAfterEach = true;
|
|
53
|
+
};
|
|
54
|
+
export {
|
|
55
|
+
afterEach,
|
|
56
|
+
decorators,
|
|
57
|
+
parameters
|
|
58
|
+
};
|
package/package.json
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappa/plugin-storybook",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "Storybook plugin for Cappa - takes screenshots of Storybook stories",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
8
|
"main": "./dist/index.js",
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
|
+
"module": "CommonJS",
|
|
10
11
|
"exports": {
|
|
11
12
|
".": {
|
|
12
|
-
"import": "./dist/index.
|
|
13
|
+
"import": "./dist/index.js",
|
|
13
14
|
"require": "./dist/index.js"
|
|
14
15
|
},
|
|
16
|
+
"./browser": {
|
|
17
|
+
"import": "./dist/browser.mjs",
|
|
18
|
+
"require": "./dist/browser.mjs"
|
|
19
|
+
},
|
|
15
20
|
"./package.json": "./package.json"
|
|
16
21
|
},
|
|
17
22
|
"keywords": [
|
|
@@ -23,16 +28,18 @@
|
|
|
23
28
|
"author": "Tim Richter",
|
|
24
29
|
"license": "MIT",
|
|
25
30
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
31
|
+
"@swc/core": "^1.13.5",
|
|
32
|
+
"storybook": "^9.1.5",
|
|
33
|
+
"@cappa/core": "0.0.13"
|
|
28
34
|
},
|
|
29
35
|
"devDependencies": {
|
|
30
|
-
"
|
|
36
|
+
"playwright-core": "^1.55.0",
|
|
37
|
+
"tsup": "8.5.0",
|
|
31
38
|
"typescript": "5.7.3"
|
|
32
39
|
},
|
|
33
|
-
"module": "./dist/index.mjs",
|
|
34
40
|
"scripts": {
|
|
35
41
|
"test": "vitest",
|
|
36
|
-
"build": "
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"tsc": "tsc"
|
|
37
44
|
}
|
|
38
45
|
}
|
package/src/cappa/plugin.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Plugin, ScreenshotOptions, ScreenshotTool } from "@cappa/core";
|
|
1
|
+
import type { Plugin, ScreenshotOptions, ScreenshotTool } from "@cappa/core";
|
|
2
2
|
import { freezeUI, waitForVisualIdle } from "./util";
|
|
3
3
|
|
|
4
4
|
interface StorybookStory {
|
|
@@ -30,6 +30,7 @@ interface StorybookPluginOptions {
|
|
|
30
30
|
*/
|
|
31
31
|
function createLatch<T>() {
|
|
32
32
|
let resolve!: (v: T) => void;
|
|
33
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: explicitly setting resolve
|
|
33
34
|
const p = new Promise<T>((r) => (resolve = r));
|
|
34
35
|
return { p, resolve };
|
|
35
36
|
}
|
|
@@ -38,13 +39,10 @@ function createLatch<T>() {
|
|
|
38
39
|
* Storybook Plugin for Cappa
|
|
39
40
|
* Fetches stories from a running Storybook server and takes screenshots of each story
|
|
40
41
|
*/
|
|
41
|
-
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
42
|
-
options
|
|
43
|
-
) => {
|
|
42
|
+
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) => {
|
|
44
43
|
return {
|
|
45
44
|
name: "StorybookPlugin",
|
|
46
|
-
description:
|
|
47
|
-
"Takes screenshots of Storybook stories with configurable options",
|
|
45
|
+
description: "Takes screenshots of Storybook stories with configurable options",
|
|
48
46
|
version: "1.0.0",
|
|
49
47
|
execute: async (screenshotTool: ScreenshotTool) => {
|
|
50
48
|
if (!options) {
|
|
@@ -70,12 +68,10 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
70
68
|
// Fetch stories from Storybook
|
|
71
69
|
const response = await fetch(storiesUrl);
|
|
72
70
|
if (!response.ok) {
|
|
73
|
-
throw new Error(
|
|
74
|
-
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
75
|
-
);
|
|
71
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
76
72
|
}
|
|
77
73
|
|
|
78
|
-
const data
|
|
74
|
+
const data = (await response.json()) as StorybookStoriesResponse;
|
|
79
75
|
const stories = Object.values(data.entries);
|
|
80
76
|
|
|
81
77
|
// Filter stories based on include/exclude patterns
|
|
@@ -83,27 +79,18 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
83
79
|
|
|
84
80
|
if (includeStories.length > 0) {
|
|
85
81
|
filteredStories = filteredStories.filter((story) =>
|
|
86
|
-
includeStories.some(
|
|
87
|
-
(pattern) =>
|
|
88
|
-
story.title.includes(pattern) || story.name.includes(pattern)
|
|
89
|
-
)
|
|
82
|
+
includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern)),
|
|
90
83
|
);
|
|
91
84
|
}
|
|
92
85
|
|
|
93
86
|
if (excludeStories.length > 0) {
|
|
94
87
|
filteredStories = filteredStories.filter(
|
|
95
|
-
(story) =>
|
|
96
|
-
!excludeStories.some(
|
|
97
|
-
(pattern) =>
|
|
98
|
-
story.title.includes(pattern) || story.name.includes(pattern)
|
|
99
|
-
)
|
|
88
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern)),
|
|
100
89
|
);
|
|
101
90
|
}
|
|
102
91
|
|
|
103
92
|
// Filter docs etc
|
|
104
|
-
filteredStories = filteredStories.filter(
|
|
105
|
-
(story) => story.type === "story"
|
|
106
|
-
);
|
|
93
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
107
94
|
|
|
108
95
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
109
96
|
|
|
@@ -120,12 +107,9 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
120
107
|
|
|
121
108
|
let currentLatch = createLatch<ScreenshotOptions>();
|
|
122
109
|
|
|
123
|
-
await screenshotTool.page?.exposeFunction(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
currentLatch.resolve(payload);
|
|
127
|
-
}
|
|
128
|
-
);
|
|
110
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload: ScreenshotOptions) => {
|
|
111
|
+
currentLatch.resolve(payload);
|
|
112
|
+
});
|
|
129
113
|
|
|
130
114
|
// Take screenshots of each story
|
|
131
115
|
for (const story of filteredStories) {
|
|
@@ -145,10 +129,8 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
145
129
|
// Take screenshot with story-specific filename
|
|
146
130
|
const filename = `${story.title.replace(
|
|
147
131
|
/[^a-zA-Z0-9]/g,
|
|
148
|
-
"_"
|
|
149
|
-
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${
|
|
150
|
-
screenshotOptions.type
|
|
151
|
-
}`;
|
|
132
|
+
"_",
|
|
133
|
+
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${screenshotOptions.type}`;
|
|
152
134
|
|
|
153
135
|
const page = screenshotTool.page;
|
|
154
136
|
if (!page) {
|
|
@@ -179,18 +161,12 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
179
161
|
}
|
|
180
162
|
|
|
181
163
|
// Actually take the screenshot
|
|
182
|
-
const actualFilepath = await screenshotTool.takeScreenshot(
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
waitForSelector:
|
|
189
|
-
options.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
190
|
-
waitForTimeout:
|
|
191
|
-
options.waitForTimeout ?? screenshotOptions.waitForTimeout,
|
|
192
|
-
}
|
|
193
|
-
);
|
|
164
|
+
const actualFilepath = await screenshotTool.takeScreenshot(page, filename, {
|
|
165
|
+
fullPage: options.fullPage ?? screenshotOptions.fullPage,
|
|
166
|
+
type: options.type ?? screenshotOptions.type,
|
|
167
|
+
waitForSelector: options.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
168
|
+
waitForTimeout: options.waitForTimeout ?? screenshotOptions.waitForTimeout,
|
|
169
|
+
});
|
|
194
170
|
|
|
195
171
|
results.push({
|
|
196
172
|
storyId: story.id,
|
|
@@ -200,10 +176,7 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
200
176
|
filepath: actualFilepath,
|
|
201
177
|
});
|
|
202
178
|
} catch (error) {
|
|
203
|
-
console.error(
|
|
204
|
-
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
205
|
-
(error as Error).message
|
|
206
|
-
);
|
|
179
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, (error as Error).message);
|
|
207
180
|
results.push({
|
|
208
181
|
storyId: story.id,
|
|
209
182
|
storyName: story.name,
|
|
@@ -214,11 +187,7 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
214
187
|
}
|
|
215
188
|
}
|
|
216
189
|
|
|
217
|
-
console.log(
|
|
218
|
-
`Completed screenshots: ${results.filter((r) => r.success).length}/${
|
|
219
|
-
results.length
|
|
220
|
-
} successful`
|
|
221
|
-
);
|
|
190
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
222
191
|
return results;
|
|
223
192
|
} catch (error) {
|
|
224
193
|
console.error("Error in Storybook plugin:", (error as Error).message);
|
package/src/cappa/util.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Page } from "playwright";
|
|
1
|
+
import type { Page } from "playwright-core";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Disable CSS transitions, animations, and scrollbars
|
|
@@ -27,8 +27,6 @@ export const waitForVisualIdle = async (page: Page) => {
|
|
|
27
27
|
if (document.fonts?.ready) await document.fonts.ready;
|
|
28
28
|
|
|
29
29
|
// Wait for a frame to be idle in the requestAnimationFrame queue
|
|
30
|
-
await new Promise((r) =>
|
|
31
|
-
requestAnimationFrame(() => requestAnimationFrame(r))
|
|
32
|
-
);
|
|
30
|
+
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
|
33
31
|
});
|
|
34
32
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { makeDecorator } from "storybook/preview-api";
|
|
2
|
-
import { waitForExposed } from "
|
|
2
|
+
import { waitForExposed } from "../util";
|
|
3
3
|
|
|
4
4
|
export const withCappaDecorator = makeDecorator({
|
|
5
5
|
name: "withCappa",
|
|
6
6
|
parameterName: "cappa",
|
|
7
7
|
skipIfNoParametersOrOptions: false,
|
|
8
8
|
wrapper: (getStory, context, { parameters }) => {
|
|
9
|
+
(window as any).__cappa_screenshot = true;
|
|
10
|
+
|
|
11
|
+
const hasPlayFunction = context.playFunction != null;
|
|
12
|
+
|
|
9
13
|
queueMicrotask(async () => {
|
|
10
14
|
try {
|
|
15
|
+
console.log("hasPlayFunction", hasPlayFunction);
|
|
11
16
|
const cappaParameters = await waitForExposed("__cappa_parameters");
|
|
12
17
|
|
|
13
18
|
cappaParameters(parameters ?? {});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { withCappaDecorator } from "./decorator";
|
|
2
|
+
|
|
3
|
+
export const decorators = [withCappaDecorator];
|
|
4
|
+
|
|
5
|
+
export const parameters = {
|
|
6
|
+
layout: "full",
|
|
7
|
+
cappa: {
|
|
8
|
+
enabled: true,
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const afterEach = (context) => {
|
|
13
|
+
console.log("afterEach", context);
|
|
14
|
+
(window as any).playAfterEach = true;
|
|
15
|
+
};
|
package/src/storybook/util.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @returns The function or rejects if the function is not exposed within the timeout
|
|
6
6
|
*/
|
|
7
7
|
export const waitForExposed = (name: string, timeoutMs = 5000) => {
|
|
8
|
-
return new Promise
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
9
|
const start = performance.now();
|
|
10
10
|
|
|
11
11
|
const t = setInterval(() => {
|