@cappa/plugin-storybook 0.0.12 → 0.0.14
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 +16 -0
- package/dist/index.js +23 -45
- package/dist/index.mjs +23 -45
- package/dist/preview.mjs +1 -3
- package/package.json +9 -4
- package/src/cappa/plugin.ts +30 -64
- package/src/cappa/util.test.ts +47 -0
- package/src/cappa/util.ts +12 -4
- package/src/storybook/addon/decorator.ts +0 -1
- package/src/storybook/addon/preview.ts +1 -2
- package/src/storybook/util.ts +1 -1
- package/src/tsconfig.json +7 -0
- package/vitest.config.ts +8 -0
- 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.13 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/preview.mjs [22m[32m1.39 KB[39m
|
|
29
|
+
[32mESM[39m ⚡️ Build success in 7ms
|
|
30
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m5.81 KB[39m
|
|
31
|
+
[32mESM[39m ⚡️ Build success in 10ms
|
|
28
32
|
[32mESM[39m [1mdist/browser.mjs [22m[32m138.00 B[39m
|
|
29
|
-
[32mESM[39m ⚡️ Build success in
|
|
30
|
-
[
|
|
31
|
-
[
|
|
32
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m6.01 KB[39m
|
|
33
|
-
[32mESM[39m ⚡️ Build success in 13ms
|
|
34
|
-
[32mCJS[39m [1mdist/index.js [22m[32m7.32 KB[39m
|
|
35
|
-
[32mCJS[39m ⚡️ Build success in 15ms
|
|
33
|
+
[32mESM[39m ⚡️ Build success in 7ms
|
|
34
|
+
[32mCJS[39m [1mdist/index.js [22m[32m7.17 KB[39m
|
|
35
|
+
[32mCJS[39m ⚡️ Build success in 10ms
|
|
36
36
|
DTS Build start
|
|
37
|
-
DTS ⚡️ Build success in
|
|
37
|
+
DTS ⚡️ Build success in 795ms
|
|
38
38
|
DTS dist/index.d.ts 593.00 B
|
|
39
39
|
DTS dist/index.d.mts 593.00 B
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @cappa/plugin-storybook
|
|
2
2
|
|
|
3
|
+
## 0.0.14
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- b8af138: Screenshot Creation folders
|
|
8
|
+
- Updated dependencies [b8af138]
|
|
9
|
+
- @cappa/core@0.0.14
|
|
10
|
+
|
|
11
|
+
## 0.0.13
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- c538974: Replace playwright with playwright-core
|
|
16
|
+
- Updated dependencies [c538974]
|
|
17
|
+
- @cappa/core@0.0.13
|
|
18
|
+
|
|
3
19
|
## 0.0.12
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
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,13 +49,16 @@ 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
|
};
|
|
55
|
+
var buildFilename = (story) => {
|
|
56
|
+
return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.jpeg`;
|
|
57
|
+
};
|
|
60
58
|
|
|
61
59
|
// src/cappa/plugin.ts
|
|
60
|
+
var import_path = __toESM(require("path"));
|
|
61
|
+
var import_fs = __toESM(require("fs"));
|
|
62
62
|
function createLatch() {
|
|
63
63
|
let resolve;
|
|
64
64
|
const p = new Promise((r) => resolve = r);
|
|
@@ -89,40 +89,29 @@ var cappaPluginStorybook = (options) => {
|
|
|
89
89
|
try {
|
|
90
90
|
const response = await fetch(storiesUrl);
|
|
91
91
|
if (!response.ok) {
|
|
92
|
-
throw new Error(
|
|
93
|
-
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
94
|
-
);
|
|
92
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
95
93
|
}
|
|
96
94
|
const data = await response.json();
|
|
97
95
|
const stories = Object.values(data.entries);
|
|
98
96
|
let filteredStories = stories;
|
|
99
97
|
if (includeStories.length > 0) {
|
|
100
98
|
filteredStories = filteredStories.filter(
|
|
101
|
-
(story) => includeStories.some(
|
|
102
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
103
|
-
)
|
|
99
|
+
(story) => includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
104
100
|
);
|
|
105
101
|
}
|
|
106
102
|
if (excludeStories.length > 0) {
|
|
107
103
|
filteredStories = filteredStories.filter(
|
|
108
|
-
(story) => !excludeStories.some(
|
|
109
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
110
|
-
)
|
|
104
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
111
105
|
);
|
|
112
106
|
}
|
|
113
|
-
filteredStories = filteredStories.filter(
|
|
114
|
-
(story) => story.type === "story"
|
|
115
|
-
);
|
|
107
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
116
108
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
117
109
|
const results = [];
|
|
118
110
|
await screenshotTool.init();
|
|
119
111
|
let currentLatch = createLatch();
|
|
120
|
-
await screenshotTool.page?.exposeFunction(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
currentLatch.resolve(payload);
|
|
124
|
-
}
|
|
125
|
-
);
|
|
112
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload) => {
|
|
113
|
+
currentLatch.resolve(payload);
|
|
114
|
+
});
|
|
126
115
|
for (const story of filteredStories) {
|
|
127
116
|
try {
|
|
128
117
|
const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
|
|
@@ -134,10 +123,6 @@ var cappaPluginStorybook = (options) => {
|
|
|
134
123
|
quality: 90,
|
|
135
124
|
type: "jpeg"
|
|
136
125
|
};
|
|
137
|
-
const filename = `${story.title.replace(
|
|
138
|
-
/[^a-zA-Z0-9]/g,
|
|
139
|
-
"_"
|
|
140
|
-
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${screenshotOptions.type}`;
|
|
141
126
|
const page = screenshotTool.page;
|
|
142
127
|
if (!page) {
|
|
143
128
|
throw new Error("Page not initialized");
|
|
@@ -159,16 +144,14 @@ var cappaPluginStorybook = (options) => {
|
|
|
159
144
|
if (options2.delay) {
|
|
160
145
|
await page.waitForTimeout(options2.delay);
|
|
161
146
|
}
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
);
|
|
147
|
+
const filename = buildFilename(story);
|
|
148
|
+
import_fs.default.mkdirSync(import_path.default.dirname(filename), { recursive: true });
|
|
149
|
+
const actualFilepath = await screenshotTool.takeScreenshot(page, filename, {
|
|
150
|
+
fullPage: options2.fullPage ?? screenshotOptions.fullPage,
|
|
151
|
+
type: options2.type ?? screenshotOptions.type,
|
|
152
|
+
waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
153
|
+
waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
|
|
154
|
+
});
|
|
172
155
|
results.push({
|
|
173
156
|
storyId: story.id,
|
|
174
157
|
storyName: story.name,
|
|
@@ -177,10 +160,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
177
160
|
filepath: actualFilepath
|
|
178
161
|
});
|
|
179
162
|
} catch (error) {
|
|
180
|
-
console.error(
|
|
181
|
-
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
182
|
-
error.message
|
|
183
|
-
);
|
|
163
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, error.message);
|
|
184
164
|
results.push({
|
|
185
165
|
storyId: story.id,
|
|
186
166
|
storyName: story.name,
|
|
@@ -190,9 +170,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
190
170
|
});
|
|
191
171
|
}
|
|
192
172
|
}
|
|
193
|
-
console.log(
|
|
194
|
-
`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`
|
|
195
|
-
);
|
|
173
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
196
174
|
return results;
|
|
197
175
|
} catch (error) {
|
|
198
176
|
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,13 +19,16 @@ 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
|
};
|
|
25
|
+
var buildFilename = (story) => {
|
|
26
|
+
return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.jpeg`;
|
|
27
|
+
};
|
|
30
28
|
|
|
31
29
|
// src/cappa/plugin.ts
|
|
30
|
+
import path from "path";
|
|
31
|
+
import fs from "fs";
|
|
32
32
|
function createLatch() {
|
|
33
33
|
let resolve;
|
|
34
34
|
const p = new Promise((r) => resolve = r);
|
|
@@ -59,40 +59,29 @@ var cappaPluginStorybook = (options) => {
|
|
|
59
59
|
try {
|
|
60
60
|
const response = await fetch(storiesUrl);
|
|
61
61
|
if (!response.ok) {
|
|
62
|
-
throw new Error(
|
|
63
|
-
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
64
|
-
);
|
|
62
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
65
63
|
}
|
|
66
64
|
const data = await response.json();
|
|
67
65
|
const stories = Object.values(data.entries);
|
|
68
66
|
let filteredStories = stories;
|
|
69
67
|
if (includeStories.length > 0) {
|
|
70
68
|
filteredStories = filteredStories.filter(
|
|
71
|
-
(story) => includeStories.some(
|
|
72
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
73
|
-
)
|
|
69
|
+
(story) => includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
74
70
|
);
|
|
75
71
|
}
|
|
76
72
|
if (excludeStories.length > 0) {
|
|
77
73
|
filteredStories = filteredStories.filter(
|
|
78
|
-
(story) => !excludeStories.some(
|
|
79
|
-
(pattern) => story.title.includes(pattern) || story.name.includes(pattern)
|
|
80
|
-
)
|
|
74
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern))
|
|
81
75
|
);
|
|
82
76
|
}
|
|
83
|
-
filteredStories = filteredStories.filter(
|
|
84
|
-
(story) => story.type === "story"
|
|
85
|
-
);
|
|
77
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
86
78
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
87
79
|
const results = [];
|
|
88
80
|
await screenshotTool.init();
|
|
89
81
|
let currentLatch = createLatch();
|
|
90
|
-
await screenshotTool.page?.exposeFunction(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
currentLatch.resolve(payload);
|
|
94
|
-
}
|
|
95
|
-
);
|
|
82
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload) => {
|
|
83
|
+
currentLatch.resolve(payload);
|
|
84
|
+
});
|
|
96
85
|
for (const story of filteredStories) {
|
|
97
86
|
try {
|
|
98
87
|
const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
|
|
@@ -104,10 +93,6 @@ var cappaPluginStorybook = (options) => {
|
|
|
104
93
|
quality: 90,
|
|
105
94
|
type: "jpeg"
|
|
106
95
|
};
|
|
107
|
-
const filename = `${story.title.replace(
|
|
108
|
-
/[^a-zA-Z0-9]/g,
|
|
109
|
-
"_"
|
|
110
|
-
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${screenshotOptions.type}`;
|
|
111
96
|
const page = screenshotTool.page;
|
|
112
97
|
if (!page) {
|
|
113
98
|
throw new Error("Page not initialized");
|
|
@@ -129,16 +114,14 @@ var cappaPluginStorybook = (options) => {
|
|
|
129
114
|
if (options2.delay) {
|
|
130
115
|
await page.waitForTimeout(options2.delay);
|
|
131
116
|
}
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
);
|
|
117
|
+
const filename = buildFilename(story);
|
|
118
|
+
fs.mkdirSync(path.dirname(filename), { recursive: true });
|
|
119
|
+
const actualFilepath = await screenshotTool.takeScreenshot(page, filename, {
|
|
120
|
+
fullPage: options2.fullPage ?? screenshotOptions.fullPage,
|
|
121
|
+
type: options2.type ?? screenshotOptions.type,
|
|
122
|
+
waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
123
|
+
waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
|
|
124
|
+
});
|
|
142
125
|
results.push({
|
|
143
126
|
storyId: story.id,
|
|
144
127
|
storyName: story.name,
|
|
@@ -147,10 +130,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
147
130
|
filepath: actualFilepath
|
|
148
131
|
});
|
|
149
132
|
} catch (error) {
|
|
150
|
-
console.error(
|
|
151
|
-
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
152
|
-
error.message
|
|
153
|
-
);
|
|
133
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, error.message);
|
|
154
134
|
results.push({
|
|
155
135
|
storyId: story.id,
|
|
156
136
|
storyName: story.name,
|
|
@@ -160,9 +140,7 @@ var cappaPluginStorybook = (options) => {
|
|
|
160
140
|
});
|
|
161
141
|
}
|
|
162
142
|
}
|
|
163
|
-
console.log(
|
|
164
|
-
`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`
|
|
165
|
-
);
|
|
143
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
166
144
|
return results;
|
|
167
145
|
} catch (error) {
|
|
168
146
|
console.error("Error in Storybook plugin:", error.message);
|
package/dist/preview.mjs
CHANGED
|
@@ -28,7 +28,6 @@ var withCappaDecorator = makeDecorator({
|
|
|
28
28
|
const hasPlayFunction = context.playFunction != null;
|
|
29
29
|
queueMicrotask(async () => {
|
|
30
30
|
try {
|
|
31
|
-
console.log("hasPlayFunction", hasPlayFunction);
|
|
32
31
|
const cappaParameters = await waitForExposed("__cappa_parameters");
|
|
33
32
|
cappaParameters(parameters2 ?? {});
|
|
34
33
|
} catch (error) {
|
|
@@ -47,8 +46,7 @@ var parameters = {
|
|
|
47
46
|
enabled: true
|
|
48
47
|
}
|
|
49
48
|
};
|
|
50
|
-
var afterEach = (
|
|
51
|
-
console.log("afterEach", context);
|
|
49
|
+
var afterEach = () => {
|
|
52
50
|
window.playAfterEach = true;
|
|
53
51
|
};
|
|
54
52
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappa/plugin-storybook",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "Storybook plugin for Cappa - takes screenshots of Storybook stories",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -30,14 +30,19 @@
|
|
|
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.14"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
+
"playwright-core": "^1.55.0",
|
|
36
37
|
"tsup": "8.5.0",
|
|
37
|
-
"typescript": "5.7.3"
|
|
38
|
+
"typescript": "5.7.3",
|
|
39
|
+
"vitest": "3.2.4",
|
|
40
|
+
"@vitest/coverage-v8": "3.2.4",
|
|
41
|
+
"@vitest/ui": "3.2.4"
|
|
38
42
|
},
|
|
39
43
|
"scripts": {
|
|
40
44
|
"test": "vitest",
|
|
41
|
-
"build": "tsup"
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"tsc": "tsc"
|
|
42
47
|
}
|
|
43
48
|
}
|
package/src/cappa/plugin.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
interface StorybookStory {
|
|
1
|
+
import type { Plugin, ScreenshotOptions, ScreenshotTool } from "@cappa/core";
|
|
2
|
+
import { buildFilename, freezeUI, waitForVisualIdle } from "./util";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
|
|
6
|
+
export interface StorybookStory {
|
|
9
7
|
id: string;
|
|
10
8
|
name: string;
|
|
11
9
|
title: string;
|
|
@@ -34,6 +32,7 @@ interface StorybookPluginOptions {
|
|
|
34
32
|
*/
|
|
35
33
|
function createLatch<T>() {
|
|
36
34
|
let resolve!: (v: T) => void;
|
|
35
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: explicitly setting resolve
|
|
37
36
|
const p = new Promise<T>((r) => (resolve = r));
|
|
38
37
|
return { p, resolve };
|
|
39
38
|
}
|
|
@@ -42,13 +41,10 @@ function createLatch<T>() {
|
|
|
42
41
|
* Storybook Plugin for Cappa
|
|
43
42
|
* Fetches stories from a running Storybook server and takes screenshots of each story
|
|
44
43
|
*/
|
|
45
|
-
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
46
|
-
options
|
|
47
|
-
) => {
|
|
44
|
+
export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (options) => {
|
|
48
45
|
return {
|
|
49
46
|
name: "StorybookPlugin",
|
|
50
|
-
description:
|
|
51
|
-
"Takes screenshots of Storybook stories with configurable options",
|
|
47
|
+
description: "Takes screenshots of Storybook stories with configurable options",
|
|
52
48
|
version: "1.0.0",
|
|
53
49
|
execute: async (screenshotTool: ScreenshotTool) => {
|
|
54
50
|
if (!options) {
|
|
@@ -74,12 +70,10 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
74
70
|
// Fetch stories from Storybook
|
|
75
71
|
const response = await fetch(storiesUrl);
|
|
76
72
|
if (!response.ok) {
|
|
77
|
-
throw new Error(
|
|
78
|
-
`Failed to fetch stories: ${response.status} ${response.statusText}`
|
|
79
|
-
);
|
|
73
|
+
throw new Error(`Failed to fetch stories: ${response.status} ${response.statusText}`);
|
|
80
74
|
}
|
|
81
75
|
|
|
82
|
-
const data
|
|
76
|
+
const data = (await response.json()) as StorybookStoriesResponse;
|
|
83
77
|
const stories = Object.values(data.entries);
|
|
84
78
|
|
|
85
79
|
// Filter stories based on include/exclude patterns
|
|
@@ -87,27 +81,18 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
87
81
|
|
|
88
82
|
if (includeStories.length > 0) {
|
|
89
83
|
filteredStories = filteredStories.filter((story) =>
|
|
90
|
-
includeStories.some(
|
|
91
|
-
(pattern) =>
|
|
92
|
-
story.title.includes(pattern) || story.name.includes(pattern)
|
|
93
|
-
)
|
|
84
|
+
includeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern)),
|
|
94
85
|
);
|
|
95
86
|
}
|
|
96
87
|
|
|
97
88
|
if (excludeStories.length > 0) {
|
|
98
89
|
filteredStories = filteredStories.filter(
|
|
99
|
-
(story) =>
|
|
100
|
-
!excludeStories.some(
|
|
101
|
-
(pattern) =>
|
|
102
|
-
story.title.includes(pattern) || story.name.includes(pattern)
|
|
103
|
-
)
|
|
90
|
+
(story) => !excludeStories.some((pattern) => story.title.includes(pattern) || story.name.includes(pattern)),
|
|
104
91
|
);
|
|
105
92
|
}
|
|
106
93
|
|
|
107
94
|
// Filter docs etc
|
|
108
|
-
filteredStories = filteredStories.filter(
|
|
109
|
-
(story) => story.type === "story"
|
|
110
|
-
);
|
|
95
|
+
filteredStories = filteredStories.filter((story) => story.type === "story");
|
|
111
96
|
|
|
112
97
|
console.log(`Found ${filteredStories.length} stories to screenshot`);
|
|
113
98
|
|
|
@@ -124,12 +109,9 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
124
109
|
|
|
125
110
|
let currentLatch = createLatch<ScreenshotOptions>();
|
|
126
111
|
|
|
127
|
-
await screenshotTool.page?.exposeFunction(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
currentLatch.resolve(payload);
|
|
131
|
-
}
|
|
132
|
-
);
|
|
112
|
+
await screenshotTool.page?.exposeFunction("__cappa_parameters", async (payload: ScreenshotOptions) => {
|
|
113
|
+
currentLatch.resolve(payload);
|
|
114
|
+
});
|
|
133
115
|
|
|
134
116
|
// Take screenshots of each story
|
|
135
117
|
for (const story of filteredStories) {
|
|
@@ -146,14 +128,6 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
146
128
|
type: "jpeg" as const,
|
|
147
129
|
};
|
|
148
130
|
|
|
149
|
-
// Take screenshot with story-specific filename
|
|
150
|
-
const filename = `${story.title.replace(
|
|
151
|
-
/[^a-zA-Z0-9]/g,
|
|
152
|
-
"_"
|
|
153
|
-
)}_${story.name.replace(/[^a-zA-Z0-9]/g, "_")}.${
|
|
154
|
-
screenshotOptions.type
|
|
155
|
-
}`;
|
|
156
|
-
|
|
157
131
|
const page = screenshotTool.page;
|
|
158
132
|
if (!page) {
|
|
159
133
|
throw new Error("Page not initialized");
|
|
@@ -182,19 +156,18 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
182
156
|
await page.waitForTimeout(options.delay);
|
|
183
157
|
}
|
|
184
158
|
|
|
159
|
+
// Take screenshot with story-specific filename
|
|
160
|
+
const filename = buildFilename(story);
|
|
161
|
+
|
|
162
|
+
fs.mkdirSync(path.dirname(filename), { recursive: true });
|
|
163
|
+
|
|
185
164
|
// 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
|
-
);
|
|
165
|
+
const actualFilepath = await screenshotTool.takeScreenshot(page, filename, {
|
|
166
|
+
fullPage: options.fullPage ?? screenshotOptions.fullPage,
|
|
167
|
+
type: options.type ?? screenshotOptions.type,
|
|
168
|
+
waitForSelector: options.waitForSelector ?? screenshotOptions.waitForSelector,
|
|
169
|
+
waitForTimeout: options.waitForTimeout ?? screenshotOptions.waitForTimeout,
|
|
170
|
+
});
|
|
198
171
|
|
|
199
172
|
results.push({
|
|
200
173
|
storyId: story.id,
|
|
@@ -204,10 +177,7 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
204
177
|
filepath: actualFilepath,
|
|
205
178
|
});
|
|
206
179
|
} catch (error) {
|
|
207
|
-
console.error(
|
|
208
|
-
`Error taking screenshot of story ${story.title} - ${story.name}:`,
|
|
209
|
-
(error as Error).message
|
|
210
|
-
);
|
|
180
|
+
console.error(`Error taking screenshot of story ${story.title} - ${story.name}:`, (error as Error).message);
|
|
211
181
|
results.push({
|
|
212
182
|
storyId: story.id,
|
|
213
183
|
storyName: story.name,
|
|
@@ -218,11 +188,7 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
|
|
|
218
188
|
}
|
|
219
189
|
}
|
|
220
190
|
|
|
221
|
-
console.log(
|
|
222
|
-
`Completed screenshots: ${results.filter((r) => r.success).length}/${
|
|
223
|
-
results.length
|
|
224
|
-
} successful`
|
|
225
|
-
);
|
|
191
|
+
console.log(`Completed screenshots: ${results.filter((r) => r.success).length}/${results.length} successful`);
|
|
226
192
|
return results;
|
|
227
193
|
} catch (error) {
|
|
228
194
|
console.error("Error in Storybook plugin:", (error as Error).message);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { buildFilename } from "./util";
|
|
3
|
+
import type { StorybookStory } from "./plugin";
|
|
4
|
+
|
|
5
|
+
describe("buildFilename", () => {
|
|
6
|
+
it("should build a filename for a story", () => {
|
|
7
|
+
const story: StorybookStory = {
|
|
8
|
+
id: "1",
|
|
9
|
+
name: "Primary",
|
|
10
|
+
title: "Story",
|
|
11
|
+
kind: "Kind",
|
|
12
|
+
story: "Story",
|
|
13
|
+
type: "story",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const filename = buildFilename(story);
|
|
17
|
+
expect(filename).toBe("Story/Primary.jpeg");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should build a filename for a story with a folder", () => {
|
|
21
|
+
const story: StorybookStory = {
|
|
22
|
+
id: "1",
|
|
23
|
+
name: "Primary",
|
|
24
|
+
title: "Folder/Story",
|
|
25
|
+
kind: "Kind",
|
|
26
|
+
story: "Story",
|
|
27
|
+
type: "story",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const filename = buildFilename(story);
|
|
31
|
+
expect(filename).toBe("Folder/Story/Primary.jpeg");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should build a filename for a story with special characters", () => {
|
|
35
|
+
const story: StorybookStory = {
|
|
36
|
+
id: "1",
|
|
37
|
+
name: "Primary123!@#$%^&*()",
|
|
38
|
+
title: "Story123!@#$%^&*()",
|
|
39
|
+
kind: "Kind",
|
|
40
|
+
story: "Story",
|
|
41
|
+
type: "story",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const filename = buildFilename(story);
|
|
45
|
+
expect(filename).toBe("Story123/Primary123.jpeg");
|
|
46
|
+
});
|
|
47
|
+
});
|
package/src/cappa/util.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Page } from "playwright";
|
|
1
|
+
import type { Page } from "playwright-core";
|
|
2
|
+
import type { StorybookStory } from "./plugin";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Disable CSS transitions, animations, and scrollbars
|
|
@@ -27,8 +28,15 @@ export const waitForVisualIdle = async (page: Page) => {
|
|
|
27
28
|
if (document.fonts?.ready) await document.fonts.ready;
|
|
28
29
|
|
|
29
30
|
// Wait for a frame to be idle in the requestAnimationFrame queue
|
|
30
|
-
await new Promise((r) =>
|
|
31
|
-
requestAnimationFrame(() => requestAnimationFrame(r))
|
|
32
|
-
);
|
|
31
|
+
await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
|
|
33
32
|
});
|
|
34
33
|
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Build a filename for a story.
|
|
37
|
+
* @param story - The story to build a filename for
|
|
38
|
+
* @returns The filename for the story. Slashes represent folders that should be handled by seperate file system calls.
|
|
39
|
+
*/
|
|
40
|
+
export const buildFilename = (story: StorybookStory) => {
|
|
41
|
+
return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.jpeg`;
|
|
42
|
+
};
|
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(() => {
|
package/vitest.config.ts
ADDED
|
@@ -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
|
-
}
|