@cappa/plugin-storybook 0.0.21 → 0.0.23

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- > @cappa/plugin-storybook@0.0.20 build /home/tim/code/work/cappa/packages/plugins/plugin-storybook
3
+ > @cappa/plugin-storybook@0.0.22 build /home/tim/code/work/cappa/packages/plugins/plugin-storybook
4
4
  > tsup
5
5
 
6
6
  CLI Building entry: src/index.ts
@@ -26,14 +26,14 @@
26
26
  CLI Cleaning output folder
27
27
  ESM Build start
28
28
  ESM dist/browser.mjs 138.00 B
29
- ESM ⚡️ Build success in 6ms
30
- ESM dist/index.mjs 6.75 KB
31
- ESM ⚡️ Build success in 9ms
29
+ ESM ⚡️ Build success in 7ms
32
30
  ESM dist/preview.mjs 1.34 KB
33
- ESM ⚡️ Build success in 8ms
34
- CJS dist/index.js 8.11 KB
31
+ ESM ⚡️ Build success in 7ms
32
+ ESM dist/index.mjs 7.07 KB
33
+ ESM ⚡️ Build success in 9ms
34
+ CJS dist/index.js 8.43 KB
35
35
  CJS ⚡️ Build success in 10ms
36
36
  DTS Build start
37
- DTS ⚡️ Build success in 807ms
37
+ DTS ⚡️ Build success in 864ms
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.23
4
+
5
+ ### Patch Changes
6
+
7
+ - df7a7ec: Add more screenshot options
8
+ - Updated dependencies [df7a7ec]
9
+ - @cappa/core@0.0.23
10
+
11
+ ## 0.0.22
12
+
13
+ ### Patch Changes
14
+
15
+ - 27a570a: Always use png screenshots
16
+ - Updated dependencies [27a570a]
17
+ - @cappa/core@0.0.22
18
+
3
19
  ## 0.0.21
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -50,6 +50,7 @@ Cappa uses a storybook addon that allows you to pass options from storybook into
50
50
  | skip | Whether to skip the screenshot | false |
51
51
  | delay | The delay to wait before taking the screenshot | null (no delay) |
52
52
  | fullPage | Whether to take a full page screenshot | true |
53
+ | mask | An array of selectors to mask in the screenshot | [] |
53
54
 
54
55
  #### Example
55
56
 
@@ -60,6 +61,7 @@ export const Primary: Story = {
60
61
  skip: true,
61
62
  delay: 1000,
62
63
  fullPage: false,
64
+ mask: ["#my-element", ".my-class"],
63
65
  },
64
66
  },
65
67
  };
package/dist/index.js CHANGED
@@ -42,20 +42,44 @@ var freezeUI = async (page) => {
42
42
  * { transition: none !important; animation: none !important }
43
43
  * { caret-color: transparent !important }
44
44
  ::-webkit-scrollbar { display: none }
45
+ html { scroll-behavior: auto !important }
45
46
  `
46
47
  });
47
48
  };
48
49
  var waitForVisualIdle = async (page) => {
49
50
  await page.waitForLoadState("networkidle");
50
51
  await page.evaluate(async () => {
51
- if (document.fonts?.ready) await document.fonts.ready;
52
- await new Promise(
53
- (r) => requestAnimationFrame(() => requestAnimationFrame(r))
54
- );
52
+ await document.fonts?.ready?.catch(() => {
53
+ });
54
+ const imgs = Array.from(document.images).filter((i) => !i.complete);
55
+ await Promise.all(imgs.map((i) => new Promise((res) => {
56
+ i.onload = i.onerror = () => res(null);
57
+ })));
55
58
  });
59
+ await page.evaluate(({ minIdleMs }) => new Promise((resolve) => {
60
+ let timer = null;
61
+ let last = performance.now();
62
+ const done = () => {
63
+ if (timer) cancelAnimationFrame(timer);
64
+ resolve();
65
+ };
66
+ const mo = new MutationObserver(() => {
67
+ last = performance.now();
68
+ });
69
+ mo.observe(document, { attributes: true, childList: true, subtree: true, characterData: true });
70
+ const tick = () => {
71
+ if (performance.now() - last >= minIdleMs) {
72
+ mo.disconnect();
73
+ done();
74
+ } else {
75
+ timer = requestAnimationFrame(tick);
76
+ }
77
+ };
78
+ requestAnimationFrame(() => requestAnimationFrame(tick));
79
+ }), { minIdleMs: 100 });
56
80
  };
57
81
  var buildFilename = (story) => {
58
- return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.jpeg`;
82
+ return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.png`;
59
83
  };
60
84
 
61
85
  // src/cappa/plugin.ts
@@ -80,10 +104,7 @@ var cappaPluginStorybook = (options) => {
80
104
  const {
81
105
  storybookUrl,
82
106
  includeStories = [],
83
- excludeStories = [],
84
- defaultViewport = { width: 1920, height: 1080 },
85
- waitForSelector,
86
- waitForTimeout
107
+ excludeStories = []
87
108
  } = options;
88
109
  const storiesUrl = `${storybookUrl}/index.json`;
89
110
  console.log(`Fetching stories from: ${storiesUrl}`);
@@ -127,14 +148,6 @@ var cappaPluginStorybook = (options) => {
127
148
  for (const story of filteredStories) {
128
149
  try {
129
150
  const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
130
- const screenshotOptions = {
131
- viewport: defaultViewport,
132
- waitForSelector,
133
- waitForTimeout,
134
- fullPage: true,
135
- quality: 90,
136
- type: "jpeg"
137
- };
138
151
  const page = screenshotTool.page;
139
152
  if (!page) {
140
153
  throw new Error("Page not initialized");
@@ -168,10 +181,9 @@ var cappaPluginStorybook = (options) => {
168
181
  page,
169
182
  filename,
170
183
  {
171
- fullPage: options2.fullPage ?? screenshotOptions.fullPage,
172
- type: options2.type ?? screenshotOptions.type,
173
- waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
174
- waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
184
+ fullPage: options2.fullPage,
185
+ mask: options2.mask?.map((selector) => page.locator(selector)),
186
+ omitBackground: options2.omitBackground
175
187
  }
176
188
  );
177
189
  results.push({
@@ -205,7 +217,7 @@ ${results.filter((r) => r.skipped).map((r) => `${r.storyName}`).join("\n")}`
205
217
  );
206
218
  }
207
219
  logger.box({
208
- title: "StorybookReport",
220
+ title: "Storybook Report",
209
221
  message: `${import_chalk.default.yellow("Skipped stories:")} ${results.filter((r) => r.skipped).length}
210
222
  ${import_chalk.default.red("Failed stories:")} ${results.filter((r) => r.error).length}
211
223
  ${import_chalk.default.green("Successful screenshots:")} ${results.length}`
package/dist/index.mjs CHANGED
@@ -12,20 +12,44 @@ var freezeUI = async (page) => {
12
12
  * { transition: none !important; animation: none !important }
13
13
  * { caret-color: transparent !important }
14
14
  ::-webkit-scrollbar { display: none }
15
+ html { scroll-behavior: auto !important }
15
16
  `
16
17
  });
17
18
  };
18
19
  var waitForVisualIdle = async (page) => {
19
20
  await page.waitForLoadState("networkidle");
20
21
  await page.evaluate(async () => {
21
- if (document.fonts?.ready) await document.fonts.ready;
22
- await new Promise(
23
- (r) => requestAnimationFrame(() => requestAnimationFrame(r))
24
- );
22
+ await document.fonts?.ready?.catch(() => {
23
+ });
24
+ const imgs = Array.from(document.images).filter((i) => !i.complete);
25
+ await Promise.all(imgs.map((i) => new Promise((res) => {
26
+ i.onload = i.onerror = () => res(null);
27
+ })));
25
28
  });
29
+ await page.evaluate(({ minIdleMs }) => new Promise((resolve) => {
30
+ let timer = null;
31
+ let last = performance.now();
32
+ const done = () => {
33
+ if (timer) cancelAnimationFrame(timer);
34
+ resolve();
35
+ };
36
+ const mo = new MutationObserver(() => {
37
+ last = performance.now();
38
+ });
39
+ mo.observe(document, { attributes: true, childList: true, subtree: true, characterData: true });
40
+ const tick = () => {
41
+ if (performance.now() - last >= minIdleMs) {
42
+ mo.disconnect();
43
+ done();
44
+ } else {
45
+ timer = requestAnimationFrame(tick);
46
+ }
47
+ };
48
+ requestAnimationFrame(() => requestAnimationFrame(tick));
49
+ }), { minIdleMs: 100 });
26
50
  };
27
51
  var buildFilename = (story) => {
28
- return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.jpeg`;
52
+ return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.png`;
29
53
  };
30
54
 
31
55
  // src/cappa/plugin.ts
@@ -50,10 +74,7 @@ var cappaPluginStorybook = (options) => {
50
74
  const {
51
75
  storybookUrl,
52
76
  includeStories = [],
53
- excludeStories = [],
54
- defaultViewport = { width: 1920, height: 1080 },
55
- waitForSelector,
56
- waitForTimeout
77
+ excludeStories = []
57
78
  } = options;
58
79
  const storiesUrl = `${storybookUrl}/index.json`;
59
80
  console.log(`Fetching stories from: ${storiesUrl}`);
@@ -97,14 +118,6 @@ var cappaPluginStorybook = (options) => {
97
118
  for (const story of filteredStories) {
98
119
  try {
99
120
  const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
100
- const screenshotOptions = {
101
- viewport: defaultViewport,
102
- waitForSelector,
103
- waitForTimeout,
104
- fullPage: true,
105
- quality: 90,
106
- type: "jpeg"
107
- };
108
121
  const page = screenshotTool.page;
109
122
  if (!page) {
110
123
  throw new Error("Page not initialized");
@@ -138,10 +151,9 @@ var cappaPluginStorybook = (options) => {
138
151
  page,
139
152
  filename,
140
153
  {
141
- fullPage: options2.fullPage ?? screenshotOptions.fullPage,
142
- type: options2.type ?? screenshotOptions.type,
143
- waitForSelector: options2.waitForSelector ?? screenshotOptions.waitForSelector,
144
- waitForTimeout: options2.waitForTimeout ?? screenshotOptions.waitForTimeout
154
+ fullPage: options2.fullPage,
155
+ mask: options2.mask?.map((selector) => page.locator(selector)),
156
+ omitBackground: options2.omitBackground
145
157
  }
146
158
  );
147
159
  results.push({
@@ -175,7 +187,7 @@ ${results.filter((r) => r.skipped).map((r) => `${r.storyName}`).join("\n")}`
175
187
  );
176
188
  }
177
189
  logger.box({
178
- title: "StorybookReport",
190
+ title: "Storybook Report",
179
191
  message: `${chalk.yellow("Skipped stories:")} ${results.filter((r) => r.skipped).length}
180
192
  ${chalk.red("Failed stories:")} ${results.filter((r) => r.error).length}
181
193
  ${chalk.green("Successful screenshots:")} ${results.length}`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cappa/plugin-storybook",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "Storybook plugin for Cappa - takes screenshots of Storybook stories",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -31,7 +31,7 @@
31
31
  "@swc/core": "^1.13.5",
32
32
  "chalk": "^5.6.2",
33
33
  "storybook": "^9.1.5",
34
- "@cappa/core": "0.0.21"
34
+ "@cappa/core": "0.0.23"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@vitest/coverage-v8": "3.2.4",
@@ -60,9 +60,6 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
60
60
  storybookUrl,
61
61
  includeStories = [],
62
62
  excludeStories = [],
63
- defaultViewport = { width: 1920, height: 1080 },
64
- waitForSelector,
65
- waitForTimeout,
66
63
  } = options;
67
64
 
68
65
  const storiesUrl = `${storybookUrl}/index.json`;
@@ -134,16 +131,6 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
134
131
  try {
135
132
  const storyUrl = `${storybookUrl}/iframe.html?id=${story.id}&viewMode=story`;
136
133
 
137
- // Merge with default options
138
- const screenshotOptions = {
139
- viewport: defaultViewport,
140
- waitForSelector: waitForSelector,
141
- waitForTimeout: waitForTimeout,
142
- fullPage: true,
143
- quality: 90,
144
- type: "jpeg" as const,
145
- };
146
-
147
134
  const page = screenshotTool.page;
148
135
  if (!page) {
149
136
  throw new Error("Page not initialized");
@@ -188,12 +175,9 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
188
175
  page,
189
176
  filename,
190
177
  {
191
- fullPage: options.fullPage ?? screenshotOptions.fullPage,
192
- type: options.type ?? screenshotOptions.type,
193
- waitForSelector:
194
- options.waitForSelector ?? screenshotOptions.waitForSelector,
195
- waitForTimeout:
196
- options.waitForTimeout ?? screenshotOptions.waitForTimeout,
178
+ fullPage: options.fullPage,
179
+ mask: options.mask?.map((selector) => page.locator(selector)),
180
+ omitBackground: options.omitBackground,
197
181
  },
198
182
  );
199
183
 
@@ -237,7 +221,7 @@ export const cappaPluginStorybook: Plugin<StorybookPluginOptions> = (
237
221
  }
238
222
 
239
223
  logger.box({
240
- title: "StorybookReport",
224
+ title: "Storybook Report",
241
225
  message: `${chalk.yellow("Skipped stories:")} ${results.filter((r) => r.skipped).length}\n${chalk.red("Failed stories:")} ${results.filter((r) => r.error).length}\n${chalk.green("Successful screenshots:")} ${results.length}`,
242
226
  });
243
227
 
@@ -14,7 +14,7 @@ describe("buildFilename", () => {
14
14
  };
15
15
 
16
16
  const filename = buildFilename(story);
17
- expect(filename).toBe("Story/Primary.jpeg");
17
+ expect(filename).toBe("Story/Primary.png");
18
18
  });
19
19
 
20
20
  it("should build a filename for a story with a folder", () => {
@@ -28,7 +28,7 @@ describe("buildFilename", () => {
28
28
  };
29
29
 
30
30
  const filename = buildFilename(story);
31
- expect(filename).toBe("Folder/Story/Primary.jpeg");
31
+ expect(filename).toBe("Folder/Story/Primary.png");
32
32
  });
33
33
 
34
34
  it("should build a filename for a story with special characters", () => {
@@ -42,6 +42,6 @@ describe("buildFilename", () => {
42
42
  };
43
43
 
44
44
  const filename = buildFilename(story);
45
- expect(filename).toBe("Story123/Primary123.jpeg");
45
+ expect(filename).toBe("Story123/Primary123.png");
46
46
  });
47
47
  });
package/src/cappa/util.ts CHANGED
@@ -11,6 +11,7 @@ export const freezeUI = async (page: Page) => {
11
11
  * { transition: none !important; animation: none !important }
12
12
  * { caret-color: transparent !important }
13
13
  ::-webkit-scrollbar { display: none }
14
+ html { scroll-behavior: auto !important }
14
15
  `,
15
16
  });
16
17
  };
@@ -23,15 +24,37 @@ export const waitForVisualIdle = async (page: Page) => {
23
24
  // Wait for the page to be fully loaded
24
25
  await page.waitForLoadState("networkidle");
25
26
 
26
- await page.evaluate(async () => {
27
- // Wait for the fonts to be loaded
28
- if (document.fonts?.ready) await document.fonts.ready;
27
+ // fonts + images
28
+ await page.evaluate(async () => {
29
+ await (document as any).fonts?.ready?.catch(() => {})
30
+ const imgs = Array.from(document.images).filter(i => !i.complete)
31
+ await Promise.all(imgs.map(i => new Promise(res => { i.onload = i.onerror = () => res(null) })))
32
+ })
29
33
 
30
- // Wait for a frame to be idle in the requestAnimationFrame queue
31
- await new Promise((r) =>
32
- requestAnimationFrame(() => requestAnimationFrame(r)),
33
- );
34
- });
34
+ // 2 animation frames + idle period with no DOM mutations
35
+ await page.evaluate(({ minIdleMs }) => new Promise<void>(resolve => {
36
+ let timer: number | null = null
37
+ let last = performance.now()
38
+
39
+ const done = () => {
40
+ if (timer) cancelAnimationFrame(timer)
41
+ resolve()
42
+ }
43
+
44
+ const mo = new MutationObserver(() => { last = performance.now() })
45
+ mo.observe(document, { attributes: true, childList: true, subtree: true, characterData: true })
46
+
47
+ const tick = () => {
48
+ if (performance.now() - last >= minIdleMs) {
49
+ mo.disconnect()
50
+ done()
51
+ } else {
52
+ timer = requestAnimationFrame(tick)
53
+ }
54
+ }
55
+
56
+ requestAnimationFrame(() => requestAnimationFrame(tick))
57
+ }), { minIdleMs: 100 })
35
58
  };
36
59
 
37
60
  /**
@@ -40,5 +63,5 @@ export const waitForVisualIdle = async (page: Page) => {
40
63
  * @returns The filename for the story. Slashes represent folders that should be handled by seperate file system calls.
41
64
  */
42
65
  export const buildFilename = (story: StorybookStory) => {
43
- return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.jpeg`;
66
+ return `${story.title.replace(/[^a-zA-Z0-9/]/g, "")}/${story.name.replace(/[^a-zA-Z0-9]/g, "")}.png`;
44
67
  };