@cappa/plugin-storybook 0.0.12 → 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 +12 -12
- package/CHANGELOG.md +8 -0
- package/dist/index.js +16 -41
- package/dist/index.mjs +16 -41
- package/package.json +5 -3
- package/src/cappa/plugin.ts +22 -57
- package/src/cappa/util.ts +2 -4
- package/src/storybook/util.ts +1 -1
- package/src/storybook/screenshot.ts +0 -108
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @cappa/plugin-storybook@0.0.
|
|
3
|
+
> @cappa/plugin-storybook@0.0.12 build /home/tim/code/work/cappa/packages/plugins/plugin-storybook
|
|
4
4
|
> tsup
|
|
5
5
|
|
|
6
6
|
[34mCLI[39m Building entry: src/index.ts
|
|
7
7
|
[34mCLI[39m Using tsconfig: ../../../tsconfig.json
|
|
8
8
|
[34mCLI[39m tsup v8.5.0
|
|
9
|
-
[34mCLI[39m Using tsup config: /home/tim/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
9
|
+
[34mCLI[39m Using tsup config: /home/tim/code/work/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
10
10
|
[34mCLI[39m Building entry: src/storybook/addon/preview.ts
|
|
11
11
|
[34mCLI[39m Using tsconfig: ../../../tsconfig.json
|
|
12
12
|
[34mCLI[39m tsup v8.5.0
|
|
13
|
-
[34mCLI[39m Using tsup config: /home/tim/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
13
|
+
[34mCLI[39m Using tsup config: /home/tim/code/work/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
14
14
|
[34mCLI[39m Building entry: src/storybook/addon/browser.ts
|
|
15
15
|
[34mCLI[39m Using tsconfig: ../../../tsconfig.json
|
|
16
16
|
[34mCLI[39m tsup v8.5.0
|
|
17
|
-
[34mCLI[39m Using tsup config: /home/tim/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
17
|
+
[34mCLI[39m Using tsup config: /home/tim/code/work/cappa/packages/plugins/plugin-storybook/tsup.config.ts
|
|
18
18
|
[34mCLI[39m Target: esnext
|
|
19
19
|
[34mCLI[39m Target: node16
|
|
20
20
|
[34mCLI[39m Target: es2015
|
|
@@ -25,15 +25,15 @@
|
|
|
25
25
|
[34mESM[39m Build start
|
|
26
26
|
[34mCLI[39m Cleaning output folder
|
|
27
27
|
[34mESM[39m Build start
|
|
28
|
-
[32mESM[39m [1mdist/browser.mjs [22m[32m138.00 B[39m
|
|
29
|
-
[32mESM[39m ⚡️ Build success in 9ms
|
|
30
28
|
[32mESM[39m [1mdist/preview.mjs [22m[32m1.49 KB[39m
|
|
31
|
-
[32mESM[39m ⚡️ Build success in
|
|
32
|
-
[32mESM[39m [1mdist/index.mjs [22m[
|
|
33
|
-
[32mESM[39m ⚡️ Build success in
|
|
34
|
-
[32mCJS[39m [1mdist/index.js [22m[
|
|
35
|
-
[32mCJS[39m ⚡️ Build success in
|
|
29
|
+
[32mESM[39m ⚡️ Build success in 7ms
|
|
30
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m5.70 KB[39m
|
|
31
|
+
[32mESM[39m ⚡️ Build success in 9ms
|
|
32
|
+
[32mCJS[39m [1mdist/index.js [22m[32m6.99 KB[39m
|
|
33
|
+
[32mCJS[39m ⚡️ Build success in 9ms
|
|
34
|
+
[32mESM[39m [1mdist/browser.mjs [22m[32m138.00 B[39m
|
|
35
|
+
[32mESM[39m ⚡️ Build success in 7ms
|
|
36
36
|
DTS Build start
|
|
37
|
-
DTS ⚡️ Build success in
|
|
37
|
+
DTS ⚡️ Build success in 843ms
|
|
38
38
|
DTS dist/index.d.ts 593.00 B
|
|
39
39
|
DTS dist/index.d.mts 593.00 B
|
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -35,9 +35,6 @@ __export(index_exports, {
|
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(index_exports);
|
|
37
37
|
|
|
38
|
-
// src/cappa/plugin.ts
|
|
39
|
-
var import_core = require("@cappa/core");
|
|
40
|
-
|
|
41
38
|
// src/cappa/util.ts
|
|
42
39
|
var freezeUI = async (page) => {
|
|
43
40
|
await page.addStyleTag({
|
|
@@ -52,9 +49,7 @@ var waitForVisualIdle = async (page) => {
|
|
|
52
49
|
await page.waitForLoadState("networkidle");
|
|
53
50
|
await page.evaluate(async () => {
|
|
54
51
|
if (document.fonts?.ready) await document.fonts.ready;
|
|
55
|
-
await new Promise(
|
|
56
|
-
(r) => requestAnimationFrame(() => requestAnimationFrame(r))
|
|
57
|
-
);
|
|
52
|
+
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
|
58
53
|
});
|
|
59
54
|
};
|
|
60
55
|
|
|
@@ -89,40 +84,29 @@ var cappaPluginStorybook = (options) => {
|
|
|
89
84
|
try {
|
|
90
85
|
const response = await fetch(storiesUrl);
|
|
91
86
|
if (!response.ok) {
|
|
92
|
-
throw new Error(
|
|
93
|
-
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
94
|
-
);
|
|
87
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
95
88
|
}
|
|
96
89
|
const data = await response.json();
|
|
97
90
|
const stories = Object.values(data.entries);
|
|
98
91
|
let filteredStories = stories;
|
|
99
92
|
if (includeStories.length > 0) {
|
|
100
93
|
filteredStories = filteredStories.filter(
|
|
101
|
-
(story) => includeStories.some(
|
|
102
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
103
|
-
)
|
|
94
|
+
(story) => includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
104
95
|
);
|
|
105
96
|
}
|
|
106
97
|
if (excludeStories.length > 0) {
|
|
107
98
|
filteredStories = filteredStories.filter(
|
|
108
|
-
(story) => !excludeStories.some(
|
|
109
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
110
|
-
)
|
|
99
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
111
100
|
);
|
|
112
101
|
}
|
|
113
|
-
filteredStories = filteredStories.filter(
|
|
114
|
-
(story) => story.type === "story"
|
|
115
|
-
);
|
|
102
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
116
103
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
117
104
|
const results = [];
|
|
118
105
|
await screenshotTool.init();
|
|
119
106
|
let currentLatch = createLatch();
|
|
120
|
-
await screenshotTool.page?.exposeFunction(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
currentLatch.resolve(payload);
|
|
124
|
-
}
|
|
125
|
-
);
|
|
107
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload) => {
|
|
108
|
+
currentLatch.resolve(payload);
|
|
109
|
+
});
|
|
126
110
|
for (const story of filteredStories) {
|
|
127
111
|
try {
|
|
128
112
|
const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
|
|
@@ -159,16 +143,12 @@ var cappaPluginStorybook = (options) => {
|
|
|
159
143
|
if (options2.delay) {
|
|
160
144
|
await page.waitForTimeout(options2.delay);
|
|
161
145
|
}
|
|
162
|
-
const actualFilepath = await screenshotTool.takeScreenshot(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
169
|
-
waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
|
|
170
|
-
}
|
|
171
|
-
);
|
|
146
|
+
const actualFilepath = await screenshotTool.takeScreenshot(page, filename, {
|
|
147
|
+
fullPage: options2.fullPage ?? screenshotOptions.fullPage,
|
|
148
|
+
type: options2.type ?? screenshotOptions.type,
|
|
149
|
+
waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
150
|
+
waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
|
|
151
|
+
});
|
|
172
152
|
results.push({
|
|
173
153
|
storyId: story.id,
|
|
174
154
|
storyName: story.name,
|
|
@@ -177,10 +157,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
177
157
|
filepath: actualFilepath
|
|
178
158
|
});
|
|
179
159
|
} catch (error) {
|
|
180
|
-
console.error(
|
|
181
|
-
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
182
|
-
error.message
|
|
183
|
-
);
|
|
160
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, error.message);
|
|
184
161
|
results.push({
|
|
185
162
|
storyId: story.id,
|
|
186
163
|
storyName: story.name,
|
|
@@ -190,9 +167,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
190
167
|
});
|
|
191
168
|
}
|
|
192
169
|
}
|
|
193
|
-
console.log(
|
|
194
|
-
`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`
|
|
195
|
-
);
|
|
170
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
196
171
|
return results;
|
|
197
172
|
} catch (error) {
|
|
198
173
|
console.error("Error in Storybook plugin:", error.message);
|
package/dist/index.mjs
CHANGED
|
@@ -5,9 +5,6 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
5
5
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
6
|
});
|
|
7
7
|
|
|
8
|
-
// src/cappa/plugin.ts
|
|
9
|
-
import "@cappa/core";
|
|
10
|
-
|
|
11
8
|
// src/cappa/util.ts
|
|
12
9
|
var freezeUI = async (page) => {
|
|
13
10
|
await page.addStyleTag({
|
|
@@ -22,9 +19,7 @@ var waitForVisualIdle = async (page) => {
|
|
|
22
19
|
await page.waitForLoadState("networkidle");
|
|
23
20
|
await page.evaluate(async () => {
|
|
24
21
|
if (document.fonts?.ready) await document.fonts.ready;
|
|
25
|
-
await new Promise(
|
|
26
|
-
(r) => requestAnimationFrame(() => requestAnimationFrame(r))
|
|
27
|
-
);
|
|
22
|
+
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
|
28
23
|
});
|
|
29
24
|
};
|
|
30
25
|
|
|
@@ -59,40 +54,29 @@ var cappaPluginStorybook = (options) => {
|
|
|
59
54
|
try {
|
|
60
55
|
const response = await fetch(storiesUrl);
|
|
61
56
|
if (!response.ok) {
|
|
62
|
-
throw new Error(
|
|
63
|
-
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
64
|
-
);
|
|
57
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
65
58
|
}
|
|
66
59
|
const data = await response.json();
|
|
67
60
|
const stories = Object.values(data.entries);
|
|
68
61
|
let filteredStories = stories;
|
|
69
62
|
if (includeStories.length > 0) {
|
|
70
63
|
filteredStories = filteredStories.filter(
|
|
71
|
-
(story) => includeStories.some(
|
|
72
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
73
|
-
)
|
|
64
|
+
(story) => includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
74
65
|
);
|
|
75
66
|
}
|
|
76
67
|
if (excludeStories.length > 0) {
|
|
77
68
|
filteredStories = filteredStories.filter(
|
|
78
|
-
(story) => !excludeStories.some(
|
|
79
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
80
|
-
)
|
|
69
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
81
70
|
);
|
|
82
71
|
}
|
|
83
|
-
filteredStories = filteredStories.filter(
|
|
84
|
-
(story) => story.type === "story"
|
|
85
|
-
);
|
|
72
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
86
73
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
87
74
|
const results = [];
|
|
88
75
|
await screenshotTool.init();
|
|
89
76
|
let currentLatch = createLatch();
|
|
90
|
-
await screenshotTool.page?.exposeFunction(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
currentLatch.resolve(payload);
|
|
94
|
-
}
|
|
95
|
-
);
|
|
77
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload) => {
|
|
78
|
+
currentLatch.resolve(payload);
|
|
79
|
+
});
|
|
96
80
|
for (const story of filteredStories) {
|
|
97
81
|
try {
|
|
98
82
|
const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
|
|
@@ -129,16 +113,12 @@ var cappaPluginStorybook = (options) => {
|
|
|
129
113
|
if (options2.delay) {
|
|
130
114
|
await page.waitForTimeout(options2.delay);
|
|
131
115
|
}
|
|
132
|
-
const actualFilepath = await screenshotTool.takeScreenshot(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
139
|
-
waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
|
|
140
|
-
}
|
|
141
|
-
);
|
|
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
|
+
});
|
|
142
122
|
results.push({
|
|
143
123
|
storyId: story.id,
|
|
144
124
|
storyName: story.name,
|
|
@@ -147,10 +127,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
147
127
|
filepath: actualFilepath
|
|
148
128
|
});
|
|
149
129
|
} catch (error) {
|
|
150
|
-
console.error(
|
|
151
|
-
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
152
|
-
error.message
|
|
153
|
-
);
|
|
130
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, error.message);
|
|
154
131
|
results.push({
|
|
155
132
|
storyId: story.id,
|
|
156
133
|
storyName: story.name,
|
|
@@ -160,9 +137,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
160
137
|
});
|
|
161
138
|
}
|
|
162
139
|
}
|
|
163
|
-
console.log(
|
|
164
|
-
`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`
|
|
165
|
-
);
|
|
140
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
166
141
|
return results;
|
|
167
142
|
} catch (error) {
|
|
168
143
|
console.error("Error in Storybook plugin:", error.message);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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"
|
|
@@ -30,14 +30,16 @@
|
|
|
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.13"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
+
"playwright-core": "^1.55.0",
|
|
36
37
|
"tsup": "8.5.0",
|
|
37
38
|
"typescript": "5.7.3"
|
|
38
39
|
},
|
|
39
40
|
"scripts": {
|
|
40
41
|
"test": "vitest",
|
|
41
|
-
"build": "tsup"
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"tsc": "tsc"
|
|
42
44
|
}
|
|
43
45
|
}
|
package/src/cappa/plugin.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type Plugin,
|
|
3
|
-
type ScreenshotOptions,
|
|
4
|
-
ScreenshotTool,
|
|
5
|
-
} from "@cappa/core";
|
|
1
|
+
import type { Plugin, ScreenshotOptions, ScreenshotTool } from "@cappa/core";
|
|
6
2
|
import { freezeUI, waitForVisualIdle } from "./util";
|
|
7
3
|
|
|
8
4
|
interface StorybookStory {
|
|
@@ -34,6 +30,7 @@ interface StorybookPluginOptions {
|
|
|
34
30
|
*/
|
|
35
31
|
function createLatch<T>() {
|
|
36
32
|
let resolve!: (v: T) => void;
|
|
33
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: explicitly setting resolve
|
|
37
34
|
const p = new Promise<T>((r) => (resolve = r));
|
|
38
35
|
return { p, resolve };
|
|
39
36
|
}
|
|
@@ -42,13 +39,10 @@ function createLatch<T>() {
|
|
|
42
39
|
* Storybook Plugin for Cappa
|
|
43
40
|
* Fetches stories from a running Storybook server and takes screenshots of each story
|
|
44
41
|
*/
|
|
45
|
-
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
46
|
-
options
|
|
47
|
-
) => {
|
|
42
|
+
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) => {
|
|
48
43
|
return {
|
|
49
44
|
name: "StorybookPlugin",
|
|
50
|
-
description:
|
|
51
|
-
"Takes screenshots of Storybook stories with configurable options",
|
|
45
|
+
description: "Takes screenshots of Storybook stories with configurable options",
|
|
52
46
|
version: "1.0.0",
|
|
53
47
|
execute: async (screenshotTool: ScreenshotTool) => {
|
|
54
48
|
if (!options) {
|
|
@@ -74,12 +68,10 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
74
68
|
// Fetch stories from Storybook
|
|
75
69
|
const response = await fetch(storiesUrl);
|
|
76
70
|
if (!response.ok) {
|
|
77
|
-
throw new Error(
|
|
78
|
-
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
79
|
-
);
|
|
71
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
80
72
|
}
|
|
81
73
|
|
|
82
|
-
const data
|
|
74
|
+
const data = (await response.json()) as StorybookStoriesResponse;
|
|
83
75
|
const stories = Object.values(data.entries);
|
|
84
76
|
|
|
85
77
|
// Filter stories based on include/exclude patterns
|
|
@@ -87,27 +79,18 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
87
79
|
|
|
88
80
|
if (includeStories.length > 0) {
|
|
89
81
|
filteredStories = filteredStories.filter((story) =>
|
|
90
|
-
includeStories.some(
|
|
91
|
-
(pattern) =>
|
|
92
|
-
story.title.includes(pattern) || story.name.includes(pattern)
|
|
93
|
-
)
|
|
82
|
+
includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern)),
|
|
94
83
|
);
|
|
95
84
|
}
|
|
96
85
|
|
|
97
86
|
if (excludeStories.length > 0) {
|
|
98
87
|
filteredStories = filteredStories.filter(
|
|
99
|
-
(story) =>
|
|
100
|
-
!excludeStories.some(
|
|
101
|
-
(pattern) =>
|
|
102
|
-
story.title.includes(pattern) || story.name.includes(pattern)
|
|
103
|
-
)
|
|
88
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern)),
|
|
104
89
|
);
|
|
105
90
|
}
|
|
106
91
|
|
|
107
92
|
// Filter docs etc
|
|
108
|
-
filteredStories = filteredStories.filter(
|
|
109
|
-
(story) => story.type === "story"
|
|
110
|
-
);
|
|
93
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
111
94
|
|
|
112
95
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
113
96
|
|
|
@@ -124,12 +107,9 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
124
107
|
|
|
125
108
|
let currentLatch = createLatch<ScreenshotOptions>();
|
|
126
109
|
|
|
127
|
-
await screenshotTool.page?.exposeFunction(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
currentLatch.resolve(payload);
|
|
131
|
-
}
|
|
132
|
-
);
|
|
110
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload: ScreenshotOptions) => {
|
|
111
|
+
currentLatch.resolve(payload);
|
|
112
|
+
});
|
|
133
113
|
|
|
134
114
|
// Take screenshots of each story
|
|
135
115
|
for (const story of filteredStories) {
|
|
@@ -149,10 +129,8 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
149
129
|
// Take screenshot with story-specific filename
|
|
150
130
|
const filename = `${story.title.replace(
|
|
151
131
|
/[^a-zA-Z0-9]/g,
|
|
152
|
-
"_"
|
|
153
|
-
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${
|
|
154
|
-
screenshotOptions.type
|
|
155
|
-
}`;
|
|
132
|
+
"_",
|
|
133
|
+
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${screenshotOptions.type}`;
|
|
156
134
|
|
|
157
135
|
const page = screenshotTool.page;
|
|
158
136
|
if (!page) {
|
|
@@ -183,18 +161,12 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
183
161
|
}
|
|
184
162
|
|
|
185
163
|
// Actually take the screenshot
|
|
186
|
-
const actualFilepath = await screenshotTool.takeScreenshot(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
waitForSelector:
|
|
193
|
-
options.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
194
|
-
waitForTimeout:
|
|
195
|
-
options.waitForTimeout ?? screenshotOptions.waitForTimeout,
|
|
196
|
-
}
|
|
197
|
-
);
|
|
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
|
+
});
|
|
198
170
|
|
|
199
171
|
results.push({
|
|
200
172
|
storyId: story.id,
|
|
@@ -204,10 +176,7 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
204
176
|
filepath: actualFilepath,
|
|
205
177
|
});
|
|
206
178
|
} catch (error) {
|
|
207
|
-
console.error(
|
|
208
|
-
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
209
|
-
(error as Error).message
|
|
210
|
-
);
|
|
179
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, (error as Error).message);
|
|
211
180
|
results.push({
|
|
212
181
|
storyId: story.id,
|
|
213
182
|
storyName: story.name,
|
|
@@ -218,11 +187,7 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
218
187
|
}
|
|
219
188
|
}
|
|
220
189
|
|
|
221
|
-
console.log(
|
|
222
|
-
`Completed screenshots: ${results.filter((r) => r.success).length}/${
|
|
223
|
-
results.length
|
|
224
|
-
} successful`
|
|
225
|
-
);
|
|
190
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
226
191
|
return results;
|
|
227
192
|
} catch (error) {
|
|
228
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/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(() => {
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { Exposed, ScreenshotOptions } from "@cappa/core";
|
|
2
|
-
|
|
3
|
-
type Args<T> = T extends (...args: infer A) => any ? A : never;
|
|
4
|
-
type Return<T> = T extends (...args: any) => infer R ? R : never;
|
|
5
|
-
|
|
6
|
-
type ExposedFns = {
|
|
7
|
-
[P in keyof Exposed]: (
|
|
8
|
-
...args: Args<Exposed[P]>
|
|
9
|
-
) => Promise<Return<Exposed[P]>>;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
type CappaWindow = typeof window & {
|
|
13
|
-
requestIdleCallback(cb: Function, opt?: { timeout: number }): void;
|
|
14
|
-
optionStore?: { [storyKey: string]: ScreenshotOptions[] };
|
|
15
|
-
} & ExposedFns;
|
|
16
|
-
|
|
17
|
-
function withExpoesdWindow(cb: (win: CappaWindow) => any) {
|
|
18
|
-
if (typeof "window" === "undefined") return;
|
|
19
|
-
const win = window as CappaWindow;
|
|
20
|
-
if (!win.emitCapture) return;
|
|
21
|
-
return cb(win);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function pushOptions(
|
|
25
|
-
win: CappaWindow,
|
|
26
|
-
storyKey: string | undefined,
|
|
27
|
-
opt: Partial<ScreenshotOptions>
|
|
28
|
-
) {
|
|
29
|
-
if (!storyKey) return;
|
|
30
|
-
if (!win.optionStore) win.optionStore = {};
|
|
31
|
-
if (!win.optionStore[storyKey]) win.optionStore[storyKey] = [];
|
|
32
|
-
win.optionStore[storyKey].push(opt);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function consumeOptions(
|
|
36
|
-
win: CappaWindow,
|
|
37
|
-
storyKey: string
|
|
38
|
-
): ScreenshotOptions[] | null {
|
|
39
|
-
if (!win.optionStore) return null;
|
|
40
|
-
if (!win.optionStore[storyKey]) return null;
|
|
41
|
-
const result = win.optionStore[storyKey];
|
|
42
|
-
delete win.optionStore[storyKey];
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function url2StoryKey(url: string) {
|
|
47
|
-
const { searchParams } = new URL(url);
|
|
48
|
-
const id = searchParams.get("id");
|
|
49
|
-
const kind = searchParams.get("selectedKind");
|
|
50
|
-
const story = searchParams.get("selectedStory");
|
|
51
|
-
if (id) {
|
|
52
|
-
// Query string has `id` if Storybook v5
|
|
53
|
-
return id;
|
|
54
|
-
} else if (kind && story) {
|
|
55
|
-
// Query string has `selectedKind` and `selectedStory` if Storybook v4
|
|
56
|
-
return `${kind}/${story}`;
|
|
57
|
-
}
|
|
58
|
-
throw new Error();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function stock(opt: ScreenshotOptions = {}, context: any) {
|
|
62
|
-
let storyKey: string | undefined = undefined;
|
|
63
|
-
|
|
64
|
-
if (context && context.id) {
|
|
65
|
-
storyKey = context.id;
|
|
66
|
-
} else if (context && !!context.story && !!context.kind) {
|
|
67
|
-
storyKey = context.kind + "/" + context.story;
|
|
68
|
-
} else {
|
|
69
|
-
storyKey = url2StoryKey(location.href);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
withExpoesdWindow((win) => pushOptions(win, storyKey, opt));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function capture() {
|
|
76
|
-
withExpoesdWindow(async (win) => {
|
|
77
|
-
// First, wait until DOM calculation process is stable because UI frameworks can mutate DOM asynchronously.
|
|
78
|
-
// So we check the number of DOM elements and how many times the browser calculate CSS style.
|
|
79
|
-
// We assume that it's ready for screenshot if these values are stable.
|
|
80
|
-
// await win.waitBrowserMetricsStable();
|
|
81
|
-
|
|
82
|
-
// Fetch some properties from the Node.js main process
|
|
83
|
-
//const [baseScreenshotOptions] = await Promise.all([
|
|
84
|
-
// win.getBaseScreenshotOptions(), // Options set via CLI
|
|
85
|
-
//]);
|
|
86
|
-
|
|
87
|
-
// Finally, send options to the Node.js main process.
|
|
88
|
-
await win.emitCapture({});
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
*
|
|
94
|
-
* Emit given screenshot options to Node.js process.
|
|
95
|
-
*
|
|
96
|
-
* @param screenshotOptions - Options for screenshot
|
|
97
|
-
*
|
|
98
|
-
*/
|
|
99
|
-
export function triggerScreenshot(
|
|
100
|
-
screenshotOptions: ScreenshotOptions,
|
|
101
|
-
context: any
|
|
102
|
-
) {
|
|
103
|
-
// This function can be called twice or more.
|
|
104
|
-
// So we should stock all options for each calling and emit merged them to Node.js
|
|
105
|
-
// stock(screenshotOptions, context);
|
|
106
|
-
//Promise.resolve().then(capture);
|
|
107
|
-
capture();
|
|
108
|
-
}
|