@histoire/plugin-screenshot 0.3.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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Guillaume Chau
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Histoire Screenshot visual regression testing
2
+
3
+ ```
4
+ pnpm add -D @histoire/plugin-screenshot
5
+ ```
6
+
7
+ Add the plugin in histoire config:
8
+
9
+ ```js
10
+ import { defineConfig } from 'histoire'
11
+ import { HstScreenshot } from '@histoire/plugin-screenshot'
12
+
13
+ export default defineConfig({
14
+ plugins: [
15
+ HstScreenshot({
16
+ // Options here
17
+ }),
18
+ ],
19
+ })
20
+ ```
@@ -0,0 +1,36 @@
1
+ import type { Plugin } from 'histoire';
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
+ export declare function HstScreenshot(options?: ScreenshotPluginOptions): Plugin;
36
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ import path from 'pathe';
2
+ import fs from 'fs-extra';
3
+ import { defu } from 'defu';
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: '@histoire/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
+ await captureWebsite.file(url, path.join(finalOptions.saveFolder, `${story.id}-${variant.id}.png`), {
37
+ overwrite: true,
38
+ width: preset.width,
39
+ height: preset.height,
40
+ fullPage: true,
41
+ });
42
+ }
43
+ });
44
+ },
45
+ };
46
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@histoire/plugin-screenshot",
3
+ "version": "0.3.0",
4
+ "description": "Histoire plugin to take screenshots for visual regression testing",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Guillaume Chau"
8
+ },
9
+ "repository": {
10
+ "url": "https://github.com/Akryum/histoire.git",
11
+ "type": "git",
12
+ "directory": "packages/histoire-plugin-screenshot"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "type": "module",
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
+ "dependencies": {
26
+ "capture-website": "^2.3.0",
27
+ "defu": "^6.0.0",
28
+ "fs-extra": "^10.1.0",
29
+ "pathe": "^0.2.0"
30
+ },
31
+ "devDependencies": {
32
+ "histoire": "0.3.0",
33
+ "typescript": "^4.6.3"
34
+ },
35
+ "peerDependencies": {
36
+ "histoire": "^0.3.0"
37
+ },
38
+ "scripts": {
39
+ "build": "rimraf dist && tsc -d",
40
+ "watch": "tsc -d -w --sourceMap"
41
+ },
42
+ "readme": "# Histoire Screenshot visual regression testing\n\n```\npnpm add -D @histoire/plugin-screenshot\n```\n\nAdd the plugin in histoire config:\n\n```js\nimport { defineConfig } from 'histoire'\nimport { HstScreenshot } from '@histoire/plugin-screenshot'\n\nexport default defineConfig({\n plugins: [\n HstScreenshot({\n // Options here\n }),\n ],\n})\n```\n"
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,77 @@
1
+ import path from 'pathe'
2
+ import fs from 'fs-extra'
3
+ import type { Plugin } from 'histoire'
4
+ import { defu } from 'defu'
5
+
6
+ interface ScreenshotPresets {
7
+ /**
8
+ * Screenshot width.
9
+ */
10
+ width?: number
11
+ /**
12
+ * Screenshot height.
13
+ */
14
+ height?: number
15
+ }
16
+
17
+ export interface ScreenshotPluginOptions {
18
+ /**
19
+ * Folder were screenshots will be saved.
20
+ */
21
+ saveFolder?: string
22
+ /**
23
+ * Ignored stories.
24
+ */
25
+ ignored?: (payload: { file: string, story: { title: string }, variant: { id: string, title: string } }) => boolean
26
+ /**
27
+ * Presets for each screenshot.
28
+ */
29
+ presets?: ScreenshotPresets[]
30
+ }
31
+
32
+ const defaultOptions: ScreenshotPluginOptions = {
33
+ saveFolder: '.histoire/screenshots',
34
+ presets: [],
35
+ }
36
+
37
+ export function HstScreenshot (options: ScreenshotPluginOptions = {}): Plugin {
38
+ const finalOptions: ScreenshotPluginOptions = defu(options, defaultOptions)
39
+ if (!finalOptions.presets.length) {
40
+ finalOptions.presets.push({
41
+ width: 1280,
42
+ height: 800,
43
+ })
44
+ }
45
+ return {
46
+ name: '@histoire/plugin-screenshot',
47
+
48
+ onBuild: async api => {
49
+ const { default: captureWebsite } = await import('capture-website')
50
+ await fs.ensureDir(finalOptions.saveFolder)
51
+
52
+ api.onPreviewStory(async ({ file, story, variant, url }) => {
53
+ if (finalOptions.ignored?.({
54
+ file,
55
+ story: {
56
+ title: story.title,
57
+ },
58
+ variant: {
59
+ id: variant.id,
60
+ title: variant.title,
61
+ },
62
+ })) {
63
+ return
64
+ }
65
+ console.log('Rendering screenshot for', file, 'title:', story.title, 'variant:', variant.id, 'title:', variant.title)
66
+ for (const preset of finalOptions.presets) {
67
+ await captureWebsite.file(url, path.join(finalOptions.saveFolder, `${story.id}-${variant.id}.png`), {
68
+ overwrite: true,
69
+ width: preset.width,
70
+ height: preset.height,
71
+ fullPage: true,
72
+ })
73
+ }
74
+ })
75
+ },
76
+ }
77
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node",
6
+ "outDir": "dist",
7
+ "rootDir": "src",
8
+ "allowSyntheticDefaultImports": true,
9
+ "esModuleInterop": true,
10
+ "removeComments": false,
11
+ "resolveJsonModule": true,
12
+ "skipLibCheck": true,
13
+ "types": [
14
+ "node",
15
+ "@peeky/test"
16
+ ],
17
+ "lib": [
18
+ "ESNext",
19
+ "DOM"
20
+ ],
21
+ "sourceMap": false,
22
+ "preserveWatchOutput": true,
23
+ "preserveSymlinks": true,
24
+ // Strict
25
+ "noImplicitAny": false,
26
+ "noImplicitThis": true,
27
+ "alwaysStrict": true,
28
+ "strictBindCallApply": true,
29
+ "strictFunctionTypes": true,
30
+ // Volar
31
+ "jsx": "preserve",
32
+ },
33
+ "include": [
34
+ "src"
35
+ ],
36
+ "exclude": [
37
+ "node_modules",
38
+ "generated/**/*",
39
+ "dist/**/*",
40
+ "src/**/*.spec.ts"
41
+ ]
42
+ }