@poveste/plugin-screenshot 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Guillaume Chau (original "histoire" project)
4
+ Copyright (c) 2026 Sorin Gitlan (poveste fork)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Poveste Screenshot visual regression testing
2
+
3
+ ```
4
+ pnpm add -D @poveste/plugin-screenshot
5
+ ```
6
+
7
+ Add the plugin in histoire config:
8
+
9
+ ```js
10
+ import { HstScreenshot } from '@poveste/plugin-screenshot'
11
+ import { defineConfig } from 'poveste'
12
+
13
+ export default defineConfig({
14
+ plugins: [
15
+ HstScreenshot({
16
+ // Options here
17
+ }),
18
+ ],
19
+ })
20
+ ```
21
+
22
+ ## Setting Up Chrome Linux Sandbox
23
+
24
+ If you get an error like No usable sandbox! or Running as root without --no-sandbox is not supported, you need to properly set up sandboxing on your Linux instance.
25
+ Alternatively, if you completely trust the content, you can disable sandboxing (strongly discouraged):
26
+
27
+ ref. https://github.com/sindresorhus/capture-website#faq
28
+
29
+ ```
30
+ import { defineConfig } from 'poveste'
31
+ import { HstScreenshot } from '@poveste/plugin-screenshot'
32
+
33
+ export default defineConfig({
34
+ plugins: [
35
+ HstScreenshot({
36
+ launchOptionsArgs: ['--no-sandbox', '--disable-setuid-sandbox'],
37
+ }),
38
+ ],
39
+ })
40
+ ```
@@ -0,0 +1,40 @@
1
+ import type { Plugin } from 'poveste';
2
+ interface ScreenshotPresets {
3
+ /**
4
+ * Screenshot width.
5
+ */
6
+ width?: number;
7
+ /**
8
+ * Screenshot height.
9
+ */
10
+ height?: number;
11
+ }
12
+ export interface ScreenshotPluginOptions {
13
+ /**
14
+ * Folder were screenshots will be saved.
15
+ */
16
+ saveFolder?: string;
17
+ /**
18
+ * Ignored stories.
19
+ */
20
+ ignored?: (payload: {
21
+ file: string;
22
+ story: {
23
+ title: string;
24
+ };
25
+ variant: {
26
+ id: string;
27
+ title: string;
28
+ };
29
+ }) => boolean;
30
+ /**
31
+ * Presets for each screenshot.
32
+ */
33
+ presets?: ScreenshotPresets[];
34
+ /**
35
+ * Args for puppeteer
36
+ */
37
+ launchOptionsArgs?: string[];
38
+ }
39
+ export declare function HstScreenshot(options?: ScreenshotPluginOptions): Plugin;
40
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ import { defu } from 'defu';
2
+ import fs from 'fs-extra';
3
+ import path from 'pathe';
4
+ const defaultOptions = {
5
+ saveFolder: '.histoire/screenshots',
6
+ presets: [],
7
+ };
8
+ export function HstScreenshot(options = {}) {
9
+ const finalOptions = defu(options, defaultOptions);
10
+ if (!finalOptions.presets.length) {
11
+ finalOptions.presets.push({
12
+ width: 1280,
13
+ height: 800,
14
+ });
15
+ }
16
+ return {
17
+ name: '@poveste/plugin-screenshot',
18
+ onBuild: async (api) => {
19
+ const { default: captureWebsite } = await import('capture-website');
20
+ await fs.ensureDir(finalOptions.saveFolder);
21
+ api.onPreviewStory(async ({ file, story, variant, url }) => {
22
+ if (finalOptions.ignored?.({
23
+ file,
24
+ story: {
25
+ title: story.title,
26
+ },
27
+ variant: {
28
+ id: variant.id,
29
+ title: variant.title,
30
+ },
31
+ })) {
32
+ return;
33
+ }
34
+ console.log('Rendering screenshot for', file, 'title:', story.title, 'variant:', variant.id, 'title:', variant.title);
35
+ for (const preset of finalOptions.presets) {
36
+ const launchOptions = finalOptions.launchOptionsArgs
37
+ ? {
38
+ args: finalOptions.launchOptionsArgs,
39
+ }
40
+ : {};
41
+ const captureWebsiteFileOptions = {
42
+ overwrite: true,
43
+ width: preset.width,
44
+ height: preset.height,
45
+ fullPage: true,
46
+ launchOptions,
47
+ };
48
+ await captureWebsite.file(url, path.join(finalOptions.saveFolder, `${story.id}-${variant.id}-${preset.width}x${preset.height}.png`), captureWebsiteFileOptions);
49
+ }
50
+ });
51
+ },
52
+ };
53
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@poveste/plugin-screenshot",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "description": "Poveste plugin to take screenshots for visual regression testing",
6
+ "author": {
7
+ "name": "Sorin Gitlan"
8
+ },
9
+ "license": "MIT",
10
+ "repository": {
11
+ "url": "https://github.com/poveste-dev/poveste.git",
12
+ "type": "git",
13
+ "directory": "packages/poveste-plugin-screenshot"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "exports": {
19
+ ".": "./dist/index.js",
20
+ "./*": "./*"
21
+ },
22
+ "main": "./dist/index.js",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "peerDependencies": {
26
+ "poveste": "^0.1.0"
27
+ },
28
+ "dependencies": {
29
+ "capture-website": "^4.2.0",
30
+ "defu": "^6.1.4",
31
+ "fs-extra": "^11.2.0",
32
+ "pathe": "^1.1.2"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^22.10.1",
36
+ "typescript": "5.6.3",
37
+ "poveste": "0.1.0"
38
+ },
39
+ "scripts": {
40
+ "build": "rimraf dist && tsc -d",
41
+ "watch": "tsc -d -w --sourceMap"
42
+ }
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,89 @@
1
+ import type { FileOptions } from 'capture-website'
2
+ import type { Plugin } from 'poveste'
3
+ import { defu } from 'defu'
4
+ import fs from 'fs-extra'
5
+ import path from 'pathe'
6
+
7
+ interface ScreenshotPresets {
8
+ /**
9
+ * Screenshot width.
10
+ */
11
+ width?: number
12
+ /**
13
+ * Screenshot height.
14
+ */
15
+ height?: number
16
+ }
17
+
18
+ export interface ScreenshotPluginOptions {
19
+ /**
20
+ * Folder were screenshots will be saved.
21
+ */
22
+ saveFolder?: string
23
+ /**
24
+ * Ignored stories.
25
+ */
26
+ ignored?: (payload: { file: string, story: { title: string }, variant: { id: string, title: string } }) => boolean
27
+ /**
28
+ * Presets for each screenshot.
29
+ */
30
+ presets?: ScreenshotPresets[]
31
+ /**
32
+ * Args for puppeteer
33
+ */
34
+ launchOptionsArgs?: string[]
35
+ }
36
+
37
+ const defaultOptions: ScreenshotPluginOptions = {
38
+ saveFolder: '.histoire/screenshots',
39
+ presets: [],
40
+ }
41
+
42
+ export function HstScreenshot(options: ScreenshotPluginOptions = {}): Plugin {
43
+ const finalOptions: ScreenshotPluginOptions = defu(options, defaultOptions)
44
+ if (!finalOptions.presets.length) {
45
+ finalOptions.presets.push({
46
+ width: 1280,
47
+ height: 800,
48
+ })
49
+ }
50
+ return {
51
+ name: '@poveste/plugin-screenshot',
52
+
53
+ onBuild: async (api) => {
54
+ const { default: captureWebsite } = await import('capture-website')
55
+ await fs.ensureDir(finalOptions.saveFolder)
56
+
57
+ api.onPreviewStory(async ({ file, story, variant, url }) => {
58
+ if (finalOptions.ignored?.({
59
+ file,
60
+ story: {
61
+ title: story.title,
62
+ },
63
+ variant: {
64
+ id: variant.id,
65
+ title: variant.title,
66
+ },
67
+ })) {
68
+ return
69
+ }
70
+ console.log('Rendering screenshot for', file, 'title:', story.title, 'variant:', variant.id, 'title:', variant.title)
71
+ for (const preset of finalOptions.presets) {
72
+ const launchOptions = finalOptions.launchOptionsArgs
73
+ ? {
74
+ args: finalOptions.launchOptionsArgs,
75
+ }
76
+ : {}
77
+ const captureWebsiteFileOptions: FileOptions = {
78
+ overwrite: true,
79
+ width: preset.width,
80
+ height: preset.height,
81
+ fullPage: true,
82
+ launchOptions,
83
+ }
84
+ await captureWebsite.file(url, path.join(finalOptions.saveFolder, `${story.id}-${variant.id}-${preset.width}x${preset.height}.png`), captureWebsiteFileOptions)
85
+ }
86
+ })
87
+ },
88
+ }
89
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ // Volar
5
+ "jsx": "preserve",
6
+ "lib": [
7
+ "ESNext",
8
+ "DOM"
9
+ ],
10
+ "rootDir": "src",
11
+ "module": "ESNext",
12
+ "moduleResolution": "node",
13
+ "resolveJsonModule": true,
14
+ "types": [
15
+ "node"
16
+ ],
17
+ "strictBindCallApply": true,
18
+ "strictFunctionTypes": true,
19
+ "alwaysStrict": true,
20
+ // Strict
21
+ "noImplicitAny": false,
22
+ "noImplicitThis": true,
23
+ "outDir": "dist",
24
+ "removeComments": false,
25
+ "sourceMap": false,
26
+ "allowSyntheticDefaultImports": true,
27
+ "esModuleInterop": true,
28
+ "skipLibCheck": true,
29
+ "preserveWatchOutput": true
30
+ },
31
+ "include": [
32
+ "src"
33
+ ],
34
+ "exclude": [
35
+ "node_modules",
36
+ "generated/**/*",
37
+ "dist/**/*",
38
+ "src/**/*.spec.ts"
39
+ ]
40
+ }