@cappa/core 0.0.3 → 0.0.4
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 +30 -0
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +131 -0
- package/dist/index.d.ts +131 -0
- package/dist/index.js +221 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +194 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @cappa/core@0.0.3 build /home/tim/cappa/packages/core
|
|
4
|
+
> tsdown
|
|
5
|
+
|
|
6
|
+
[34mℹ[39m tsdown [2mv0.14.1[22m powered by rolldown [2mv1.0.0-beta.37[22m
|
|
7
|
+
[34mℹ[39m Using tsdown config: [4m/home/tim/cappa/packages/core/tsdown.config.ts[24m
|
|
8
|
+
(node:66866) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/tim/cappa/packages/core/tsdown.config.ts is not specified and it doesn't parse as CommonJS.
|
|
9
|
+
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
|
|
10
|
+
To eliminate this warning, add "type": "module" to /home/tim/cappa/packages/core/package.json.
|
|
11
|
+
(Use `node --trace-warnings ...` to show where the warning was created)
|
|
12
|
+
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
13
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
14
|
+
[34mℹ[39m Build start
|
|
15
|
+
[33mWarning: Invalid input options (1 issue found)
|
|
16
|
+
- For the "resolve.tsconfigFilename". It is deprecated. Please use the top-level `tsconfig` option instead. [0m
|
|
17
|
+
[33mWarning: Invalid input options (1 issue found)
|
|
18
|
+
- For the "resolve.tsconfigFilename". It is deprecated. Please use the top-level `tsconfig` option instead. [0m
|
|
19
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.js[22m [2m 6.91 kB[22m [2m│ gzip: 2.06 kB[22m
|
|
20
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.js.map [2m12.58 kB[22m [2m│ gzip: 3.32 kB[22m
|
|
21
|
+
[34mℹ[39m [33m[CJS][39m 2 files, total: 19.50 kB
|
|
22
|
+
[33mWarning: Invalid input options (1 issue found)
|
|
23
|
+
- For the "resolve.tsconfigFilename". It is deprecated. Please use the top-level `tsconfig` option instead. [0m
|
|
24
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m 5.73 kB[22m [2m│ gzip: 1.62 kB[22m
|
|
25
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [2m12.54 kB[22m [2m│ gzip: 3.31 kB[22m
|
|
26
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 3.73 kB[22m [2m│ gzip: 1.28 kB[22m
|
|
27
|
+
[34mℹ[39m [34m[ESM][39m 3 files, total: 21.99 kB
|
|
28
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.ts[22m[39m [2m3.73 kB[22m [2m│ gzip: 1.28 kB[22m
|
|
29
|
+
[34mℹ[39m [33m[CJS][39m 1 files, total: 3.73 kB
|
|
30
|
+
[32m✔[39m Build complete in [32m1378ms[39m
|
package/CHANGELOG.md
CHANGED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Browser, BrowserContext, Page } from "playwright";
|
|
2
|
+
|
|
3
|
+
//#region src/screenshot.d.ts
|
|
4
|
+
declare class ScreenshotTool {
|
|
5
|
+
browserType: string;
|
|
6
|
+
headless: boolean;
|
|
7
|
+
viewport: {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
};
|
|
11
|
+
outputDir: string;
|
|
12
|
+
quality: number;
|
|
13
|
+
fullPage: boolean;
|
|
14
|
+
browser: Browser | null;
|
|
15
|
+
context: BrowserContext | null;
|
|
16
|
+
constructor(options: {
|
|
17
|
+
browserType?: string;
|
|
18
|
+
headless?: boolean;
|
|
19
|
+
viewport?: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
outputDir?: string;
|
|
24
|
+
quality?: number;
|
|
25
|
+
fullPage?: boolean;
|
|
26
|
+
});
|
|
27
|
+
init(): Promise<void>;
|
|
28
|
+
close(): Promise<void>;
|
|
29
|
+
newPage(): Promise<Page>;
|
|
30
|
+
goTo(page: Page, url: string): Promise<void>;
|
|
31
|
+
getFilePath(filename: string): string;
|
|
32
|
+
takeScreenshot(page: Page, filename: string, options?: {
|
|
33
|
+
fullPage?: boolean;
|
|
34
|
+
type?: string;
|
|
35
|
+
quality?: number;
|
|
36
|
+
waitForSelector?: string;
|
|
37
|
+
waitForTimeout?: number;
|
|
38
|
+
}): Promise<string>;
|
|
39
|
+
takeElementScreenshot(page: Page, selector: string, options?: {
|
|
40
|
+
filename?: string;
|
|
41
|
+
fullPage?: boolean;
|
|
42
|
+
type?: string;
|
|
43
|
+
quality?: number;
|
|
44
|
+
}): Promise<string>;
|
|
45
|
+
batchScreenshots(urls: string[], options?: {}): Promise<{
|
|
46
|
+
url: string;
|
|
47
|
+
success: boolean;
|
|
48
|
+
filepath: string;
|
|
49
|
+
error?: string;
|
|
50
|
+
}[]>;
|
|
51
|
+
takeResponsiveScreenshots(url: string, viewports?: {
|
|
52
|
+
width: number;
|
|
53
|
+
height: number;
|
|
54
|
+
name: string;
|
|
55
|
+
}[], options?: {
|
|
56
|
+
fullPage?: boolean;
|
|
57
|
+
type?: string;
|
|
58
|
+
quality?: number;
|
|
59
|
+
}): Promise<{
|
|
60
|
+
viewport: string;
|
|
61
|
+
success: boolean;
|
|
62
|
+
filepath: string;
|
|
63
|
+
error?: string;
|
|
64
|
+
}[]>;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/plugin.d.ts
|
|
68
|
+
/**
|
|
69
|
+
* Plugin function type definition
|
|
70
|
+
*/
|
|
71
|
+
type PluginFunction = (screenshotTool: ScreenshotTool, options?: any) => Promise<any[]>;
|
|
72
|
+
/**
|
|
73
|
+
* Plugin definition interface
|
|
74
|
+
*/
|
|
75
|
+
type PluginDef = {
|
|
76
|
+
name: string;
|
|
77
|
+
description: string;
|
|
78
|
+
version: string;
|
|
79
|
+
execute: PluginFunction;
|
|
80
|
+
validateOptions?: (options: any) => boolean;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Plugin function type definition
|
|
84
|
+
*/
|
|
85
|
+
type Plugin<Config = any> = (config?: Config) => PluginDef;
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/utils/types.d.ts
|
|
88
|
+
type PossiblePromise<T> = Promise<T> | T;
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/types.d.ts
|
|
91
|
+
/**
|
|
92
|
+
* Config used in `cappa.config.ts`
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* import { defineConfig } from '@cappa/core'
|
|
96
|
+
* export default defineConfig({
|
|
97
|
+
* ...
|
|
98
|
+
* })
|
|
99
|
+
*/
|
|
100
|
+
type UserConfig = {
|
|
101
|
+
/**
|
|
102
|
+
* The project root directory, which can be either an absolute path or a path relative to the location of your `cappa.config.ts` file.
|
|
103
|
+
* @default process.cwd()
|
|
104
|
+
*/
|
|
105
|
+
root?: string;
|
|
106
|
+
/**
|
|
107
|
+
* An array of Cappa plugins used for generation. Each plugin may have additional configurable options (defined within the plugin itself). If a plugin relies on another plugin, an error will occur if the required dependency is missing. Refer to "pre" for more details.
|
|
108
|
+
*/
|
|
109
|
+
plugins?: Array<Plugin | PluginDef>;
|
|
110
|
+
};
|
|
111
|
+
type ScreenshotOptions = {
|
|
112
|
+
fullPage?: boolean;
|
|
113
|
+
type?: string;
|
|
114
|
+
quality?: number;
|
|
115
|
+
waitForSelector?: string;
|
|
116
|
+
waitForTimeout?: number;
|
|
117
|
+
};
|
|
118
|
+
interface Exposed {
|
|
119
|
+
emitCapture(opt: ScreenshotOptions): void;
|
|
120
|
+
getBaseScreenshotOptions(): ScreenshotOptions;
|
|
121
|
+
waitBrowserMetricsStable(): Promise<void>;
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/config.d.ts
|
|
125
|
+
/**
|
|
126
|
+
* Type helper to make it easier to use vite.config.ts accepts a direct UserConfig object, or a function that returns it. The function receives a ConfigEnv object.
|
|
127
|
+
*/
|
|
128
|
+
declare function defineConfig(options: PossiblePromise<UserConfig | Array<UserConfig>> | (() => PossiblePromise<UserConfig | Array<UserConfig>>)): typeof options;
|
|
129
|
+
//#endregion
|
|
130
|
+
export { type Exposed, type Plugin, type PluginDef, type PluginFunction, type ScreenshotOptions, ScreenshotTool, type UserConfig, defineConfig };
|
|
131
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Browser, BrowserContext, Page } from "playwright";
|
|
2
|
+
|
|
3
|
+
//#region src/screenshot.d.ts
|
|
4
|
+
declare class ScreenshotTool {
|
|
5
|
+
browserType: string;
|
|
6
|
+
headless: boolean;
|
|
7
|
+
viewport: {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
};
|
|
11
|
+
outputDir: string;
|
|
12
|
+
quality: number;
|
|
13
|
+
fullPage: boolean;
|
|
14
|
+
browser: Browser | null;
|
|
15
|
+
context: BrowserContext | null;
|
|
16
|
+
constructor(options: {
|
|
17
|
+
browserType?: string;
|
|
18
|
+
headless?: boolean;
|
|
19
|
+
viewport?: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
outputDir?: string;
|
|
24
|
+
quality?: number;
|
|
25
|
+
fullPage?: boolean;
|
|
26
|
+
});
|
|
27
|
+
init(): Promise<void>;
|
|
28
|
+
close(): Promise<void>;
|
|
29
|
+
newPage(): Promise<Page>;
|
|
30
|
+
goTo(page: Page, url: string): Promise<void>;
|
|
31
|
+
getFilePath(filename: string): string;
|
|
32
|
+
takeScreenshot(page: Page, filename: string, options?: {
|
|
33
|
+
fullPage?: boolean;
|
|
34
|
+
type?: string;
|
|
35
|
+
quality?: number;
|
|
36
|
+
waitForSelector?: string;
|
|
37
|
+
waitForTimeout?: number;
|
|
38
|
+
}): Promise<string>;
|
|
39
|
+
takeElementScreenshot(page: Page, selector: string, options?: {
|
|
40
|
+
filename?: string;
|
|
41
|
+
fullPage?: boolean;
|
|
42
|
+
type?: string;
|
|
43
|
+
quality?: number;
|
|
44
|
+
}): Promise<string>;
|
|
45
|
+
batchScreenshots(urls: string[], options?: {}): Promise<{
|
|
46
|
+
url: string;
|
|
47
|
+
success: boolean;
|
|
48
|
+
filepath: string;
|
|
49
|
+
error?: string;
|
|
50
|
+
}[]>;
|
|
51
|
+
takeResponsiveScreenshots(url: string, viewports?: {
|
|
52
|
+
width: number;
|
|
53
|
+
height: number;
|
|
54
|
+
name: string;
|
|
55
|
+
}[], options?: {
|
|
56
|
+
fullPage?: boolean;
|
|
57
|
+
type?: string;
|
|
58
|
+
quality?: number;
|
|
59
|
+
}): Promise<{
|
|
60
|
+
viewport: string;
|
|
61
|
+
success: boolean;
|
|
62
|
+
filepath: string;
|
|
63
|
+
error?: string;
|
|
64
|
+
}[]>;
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/plugin.d.ts
|
|
68
|
+
/**
|
|
69
|
+
* Plugin function type definition
|
|
70
|
+
*/
|
|
71
|
+
type PluginFunction = (screenshotTool: ScreenshotTool, options?: any) => Promise<any[]>;
|
|
72
|
+
/**
|
|
73
|
+
* Plugin definition interface
|
|
74
|
+
*/
|
|
75
|
+
type PluginDef = {
|
|
76
|
+
name: string;
|
|
77
|
+
description: string;
|
|
78
|
+
version: string;
|
|
79
|
+
execute: PluginFunction;
|
|
80
|
+
validateOptions?: (options: any) => boolean;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Plugin function type definition
|
|
84
|
+
*/
|
|
85
|
+
type Plugin<Config = any> = (config?: Config) => PluginDef;
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/utils/types.d.ts
|
|
88
|
+
type PossiblePromise<T> = Promise<T> | T;
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/types.d.ts
|
|
91
|
+
/**
|
|
92
|
+
* Config used in `cappa.config.ts`
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* import { defineConfig } from '@cappa/core'
|
|
96
|
+
* export default defineConfig({
|
|
97
|
+
* ...
|
|
98
|
+
* })
|
|
99
|
+
*/
|
|
100
|
+
type UserConfig = {
|
|
101
|
+
/**
|
|
102
|
+
* The project root directory, which can be either an absolute path or a path relative to the location of your `cappa.config.ts` file.
|
|
103
|
+
* @default process.cwd()
|
|
104
|
+
*/
|
|
105
|
+
root?: string;
|
|
106
|
+
/**
|
|
107
|
+
* An array of Cappa plugins used for generation. Each plugin may have additional configurable options (defined within the plugin itself). If a plugin relies on another plugin, an error will occur if the required dependency is missing. Refer to "pre" for more details.
|
|
108
|
+
*/
|
|
109
|
+
plugins?: Array<Plugin | PluginDef>;
|
|
110
|
+
};
|
|
111
|
+
type ScreenshotOptions = {
|
|
112
|
+
fullPage?: boolean;
|
|
113
|
+
type?: string;
|
|
114
|
+
quality?: number;
|
|
115
|
+
waitForSelector?: string;
|
|
116
|
+
waitForTimeout?: number;
|
|
117
|
+
};
|
|
118
|
+
interface Exposed {
|
|
119
|
+
emitCapture(opt: ScreenshotOptions): void;
|
|
120
|
+
getBaseScreenshotOptions(): ScreenshotOptions;
|
|
121
|
+
waitBrowserMetricsStable(): Promise<void>;
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/config.d.ts
|
|
125
|
+
/**
|
|
126
|
+
* Type helper to make it easier to use vite.config.ts accepts a direct UserConfig object, or a function that returns it. The function receives a ConfigEnv object.
|
|
127
|
+
*/
|
|
128
|
+
declare function defineConfig(options: PossiblePromise<UserConfig | Array<UserConfig>> | (() => PossiblePromise<UserConfig | Array<UserConfig>>)): typeof options;
|
|
129
|
+
//#endregion
|
|
130
|
+
export { type Exposed, type Plugin, type PluginDef, type PluginFunction, type ScreenshotOptions, ScreenshotTool, type UserConfig, defineConfig };
|
|
131
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let playwright = require("playwright");
|
|
25
|
+
playwright = __toESM(playwright);
|
|
26
|
+
let node_path = require("node:path");
|
|
27
|
+
node_path = __toESM(node_path);
|
|
28
|
+
let node_fs = require("node:fs");
|
|
29
|
+
node_fs = __toESM(node_fs);
|
|
30
|
+
|
|
31
|
+
//#region src/screenshot.ts
|
|
32
|
+
var ScreenshotTool = class {
|
|
33
|
+
browserType;
|
|
34
|
+
headless;
|
|
35
|
+
viewport;
|
|
36
|
+
outputDir;
|
|
37
|
+
quality;
|
|
38
|
+
fullPage;
|
|
39
|
+
browser = null;
|
|
40
|
+
context = null;
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.browserType = options.browserType || "chromium";
|
|
43
|
+
this.headless = options.headless !== false;
|
|
44
|
+
this.viewport = options.viewport || {
|
|
45
|
+
width: 1920,
|
|
46
|
+
height: 1080
|
|
47
|
+
};
|
|
48
|
+
this.outputDir = options.outputDir || "./screenshots";
|
|
49
|
+
this.quality = options.quality || 90;
|
|
50
|
+
this.fullPage = options.fullPage !== false;
|
|
51
|
+
}
|
|
52
|
+
async init() {
|
|
53
|
+
if (!node_fs.default.existsSync(this.outputDir)) node_fs.default.mkdirSync(this.outputDir, { recursive: true });
|
|
54
|
+
const browserClass = {
|
|
55
|
+
chromium: playwright.chromium,
|
|
56
|
+
firefox: playwright.firefox,
|
|
57
|
+
webkit: playwright.webkit
|
|
58
|
+
}[this.browserType];
|
|
59
|
+
if (!browserClass) throw new Error(`Unsupported browser type: ${this.browserType}`);
|
|
60
|
+
this.browser = await browserClass.launch({ headless: this.headless });
|
|
61
|
+
this.context = await this.browser.newContext({
|
|
62
|
+
reducedMotion: "reduce",
|
|
63
|
+
deviceScaleFactor: 1
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async close() {
|
|
67
|
+
if (this.browser) await this.browser.close();
|
|
68
|
+
}
|
|
69
|
+
async newPage() {
|
|
70
|
+
if (!this.context) throw new Error("Browser not initialized");
|
|
71
|
+
const page = await this.context?.newPage();
|
|
72
|
+
await page.setViewportSize(this.viewport);
|
|
73
|
+
return page;
|
|
74
|
+
}
|
|
75
|
+
async goTo(page, url) {
|
|
76
|
+
if (!this.context) throw new Error("Browser not initialized");
|
|
77
|
+
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
78
|
+
}
|
|
79
|
+
getFilePath(filename) {
|
|
80
|
+
return node_path.default.join(this.outputDir, filename);
|
|
81
|
+
}
|
|
82
|
+
async takeScreenshot(page, filename, options = {}) {
|
|
83
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
84
|
+
try {
|
|
85
|
+
if (options.waitForSelector) {
|
|
86
|
+
console.log(`Waiting for selector: ${options.waitForSelector}`);
|
|
87
|
+
await page.waitForSelector(options.waitForSelector, { timeout: 1e4 });
|
|
88
|
+
}
|
|
89
|
+
const waitTime = options.waitForTimeout !== void 0 ? options.waitForTimeout : 0;
|
|
90
|
+
if (waitTime > 0) {
|
|
91
|
+
console.log(`Waiting for ${waitTime}ms for dynamic content`);
|
|
92
|
+
await page.waitForTimeout(waitTime);
|
|
93
|
+
}
|
|
94
|
+
const filepath = this.getFilePath(filename);
|
|
95
|
+
const screenshotOptions = {
|
|
96
|
+
path: filepath,
|
|
97
|
+
fullPage: options.fullPage !== void 0 ? options.fullPage : this.fullPage,
|
|
98
|
+
type: options.type || "jpeg",
|
|
99
|
+
quality: options.quality || this.quality
|
|
100
|
+
};
|
|
101
|
+
await page.screenshot(screenshotOptions);
|
|
102
|
+
console.log(`Screenshot saved: ${filepath}`);
|
|
103
|
+
return filepath;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.error(`Error taking screenshot of ${page.url()}:`, error.message);
|
|
106
|
+
throw error;
|
|
107
|
+
} finally {
|
|
108
|
+
await page.close();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async takeElementScreenshot(page, selector, options = {}) {
|
|
112
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
113
|
+
try {
|
|
114
|
+
await page.waitForSelector(selector, { timeout: 1e4 });
|
|
115
|
+
const element = await page.$(selector);
|
|
116
|
+
if (!element) throw new Error(`Element not found: ${selector}`);
|
|
117
|
+
const filename = options.filename || `${selector.replace(/[^a-zA-Z0-9]/g, "_")}.png`;
|
|
118
|
+
const filepath = this.getFilePath(filename);
|
|
119
|
+
const elementScreenshotOptions = {
|
|
120
|
+
path: filepath,
|
|
121
|
+
type: options.type || "jpeg",
|
|
122
|
+
quality: options.quality || this.quality
|
|
123
|
+
};
|
|
124
|
+
await element.screenshot(elementScreenshotOptions);
|
|
125
|
+
console.log(`Element screenshot saved: ${filepath}`);
|
|
126
|
+
return filepath;
|
|
127
|
+
} catch (error) {
|
|
128
|
+
console.error(`Error taking element screenshot of ${page.url()}:`, error.message);
|
|
129
|
+
throw error;
|
|
130
|
+
} finally {
|
|
131
|
+
await page.close();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async batchScreenshots(urls, options = {}) {
|
|
135
|
+
const results = [];
|
|
136
|
+
for (const url of urls) try {
|
|
137
|
+
const page = await this.newPage();
|
|
138
|
+
await this.goTo(page, url);
|
|
139
|
+
const filepath = await this.takeScreenshot(page, url, options);
|
|
140
|
+
results.push({
|
|
141
|
+
url,
|
|
142
|
+
success: true,
|
|
143
|
+
filepath
|
|
144
|
+
});
|
|
145
|
+
} catch (error) {
|
|
146
|
+
results.push({
|
|
147
|
+
url,
|
|
148
|
+
success: false,
|
|
149
|
+
error: error.message,
|
|
150
|
+
filepath: ""
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return results;
|
|
154
|
+
}
|
|
155
|
+
async takeResponsiveScreenshots(url, viewports = [
|
|
156
|
+
{
|
|
157
|
+
width: 1920,
|
|
158
|
+
height: 1080,
|
|
159
|
+
name: "desktop"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
width: 768,
|
|
163
|
+
height: 1024,
|
|
164
|
+
name: "tablet"
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
width: 375,
|
|
168
|
+
height: 667,
|
|
169
|
+
name: "mobile"
|
|
170
|
+
}
|
|
171
|
+
], options = {}) {
|
|
172
|
+
if (!this.context) throw new Error("Browser not initialized");
|
|
173
|
+
const results = [];
|
|
174
|
+
for (const viewport of viewports) try {
|
|
175
|
+
const page = await this.context?.newPage();
|
|
176
|
+
await page.setViewportSize(viewport);
|
|
177
|
+
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
178
|
+
await page.waitForTimeout(2e3);
|
|
179
|
+
const filename = `${viewport.name}.png`;
|
|
180
|
+
const filepath = this.getFilePath(filename);
|
|
181
|
+
const responsiveScreenshotOptions = {
|
|
182
|
+
path: filepath,
|
|
183
|
+
fullPage: options.fullPage !== void 0 ? options.fullPage : this.fullPage,
|
|
184
|
+
type: options.type || "png",
|
|
185
|
+
quality: options.quality || this.quality
|
|
186
|
+
};
|
|
187
|
+
await page.screenshot(responsiveScreenshotOptions);
|
|
188
|
+
await page.close();
|
|
189
|
+
console.log(`Responsive screenshot saved: ${filepath}`);
|
|
190
|
+
results.push({
|
|
191
|
+
viewport: viewport.name,
|
|
192
|
+
success: true,
|
|
193
|
+
filepath
|
|
194
|
+
});
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error(`Error taking responsive screenshot for ${viewport.name}:`, error.message);
|
|
197
|
+
results.push({
|
|
198
|
+
viewport: viewport.name,
|
|
199
|
+
success: false,
|
|
200
|
+
error: error.message,
|
|
201
|
+
filepath: ""
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return results;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
var screenshot_default = ScreenshotTool;
|
|
208
|
+
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/config.ts
|
|
211
|
+
/**
|
|
212
|
+
* Type helper to make it easier to use vite.config.ts accepts a direct UserConfig object, or a function that returns it. The function receives a ConfigEnv object.
|
|
213
|
+
*/
|
|
214
|
+
function defineConfig(options) {
|
|
215
|
+
return options;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
//#endregion
|
|
219
|
+
exports.ScreenshotTool = screenshot_default;
|
|
220
|
+
exports.defineConfig = defineConfig;
|
|
221
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["fs","path","results: {\n url: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[]","results: {\n viewport: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[]"],"sources":["../src/screenshot.ts","../src/config.ts"],"sourcesContent":["import {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright\";\nimport path from \"node:path\";\nimport fs from \"node:fs\";\n\nclass ScreenshotTool {\n browserType: string;\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n quality: number;\n fullPage: boolean;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n constructor(options: {\n browserType?: string;\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n quality?: number;\n fullPage?: boolean;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.quality = options.quality || 90;\n this.fullPage = options.fullPage !== false; // Default to full page\n }\n\n async init() {\n // Create output directory if it doesn't exist\n if (!fs.existsSync(this.outputDir)) {\n fs.mkdirSync(this.outputDir, { recursive: true });\n }\n\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass =\n browserMap[this.browserType as keyof typeof browserMap];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n this.context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 1,\n });\n }\n\n async close() {\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n async newPage() {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n const page = await this.context?.newPage();\n await page.setViewportSize(this.viewport);\n return page;\n }\n\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n getFilePath(filename: string) {\n const filepath = path.join(this.outputDir, filename);\n return filepath;\n }\n\n async takeScreenshot(\n page: Page,\n filename: string,\n options: {\n fullPage?: boolean;\n type?: string;\n quality?: number;\n waitForSelector?: string;\n waitForTimeout?: number;\n } = {}\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Wait for selector if specified\n if (options.waitForSelector) {\n console.log(`Waiting for selector: ${options.waitForSelector}`);\n await page.waitForSelector(options.waitForSelector, { timeout: 10000 });\n }\n\n // Wait for custom timeout or default\n const waitTime =\n options.waitForTimeout !== undefined ? options.waitForTimeout : 0;\n if (waitTime > 0) {\n console.log(`Waiting for ${waitTime}ms for dynamic content`);\n await page.waitForTimeout(waitTime);\n }\n\n // Generate filename\n const filepath = this.getFilePath(filename);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage:\n (options.fullPage as boolean) !== undefined\n ? options.fullPage\n : this.fullPage,\n type: (options.type as \"jpeg\" | \"png\") || \"jpeg\",\n quality: (options.quality as number) || this.quality,\n } satisfies PageScreenshotOptions;\n\n await page.screenshot(screenshotOptions);\n\n console.log(`Screenshot saved: ${filepath}`);\n return filepath;\n } catch (error) {\n console.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message\n );\n throw error;\n } finally {\n await page.close();\n }\n }\n\n async takeElementScreenshot(\n page: Page,\n selector: string,\n options: {\n filename?: string;\n fullPage?: boolean;\n type?: string;\n quality?: number;\n } = {}\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Wait for element to be visible\n await page.waitForSelector(selector, { timeout: 10000 });\n\n const element = await page.$(selector);\n if (!element) {\n throw new Error(`Element not found: ${selector}`);\n }\n\n const filename =\n (options.filename as string | undefined) ||\n `${selector.replace(/[^a-zA-Z0-9]/g, \"_\")}.png`;\n const filepath = this.getFilePath(filename);\n\n const elementScreenshotOptions = {\n path: filepath,\n type: (options.type as \"jpeg\" | \"png\") || \"jpeg\",\n quality: (options.quality as number) || this.quality,\n } satisfies PageScreenshotOptions;\n\n await element.screenshot(elementScreenshotOptions);\n\n console.log(`Element screenshot saved: ${filepath}`);\n return filepath;\n } catch (error) {\n console.error(\n `Error taking element screenshot of ${page.url()}:`,\n (error as Error).message\n );\n throw error;\n } finally {\n await page.close();\n }\n }\n\n async batchScreenshots(urls: string[], options = {}) {\n const results: {\n url: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[] = [];\n\n for (const url of urls) {\n try {\n const page = await this.newPage();\n await this.goTo(page, url);\n const filepath = await this.takeScreenshot(page, url, options);\n results.push({ url, success: true, filepath });\n } catch (error) {\n results.push({\n url,\n success: false,\n error: (error as Error).message,\n filepath: \"\",\n });\n }\n }\n\n return results;\n }\n\n async takeResponsiveScreenshots(\n url: string,\n viewports: { width: number; height: number; name: string }[] = [\n { width: 1920, height: 1080, name: \"desktop\" },\n { width: 768, height: 1024, name: \"tablet\" },\n { width: 375, height: 667, name: \"mobile\" },\n ],\n options: {\n fullPage?: boolean;\n type?: string;\n quality?: number;\n } = {}\n ) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n\n const results: {\n viewport: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[] = [];\n\n for (const viewport of viewports) {\n try {\n const page = await this.context?.newPage();\n await page.setViewportSize(viewport);\n\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n await page.waitForTimeout(2000);\n\n const filename = `${viewport.name}.png`;\n const filepath = this.getFilePath(filename);\n\n const responsiveScreenshotOptions = {\n path: filepath,\n fullPage:\n (options.fullPage as boolean) !== undefined\n ? options.fullPage\n : this.fullPage,\n type: (options.type as \"jpeg\" | \"png\") || \"png\",\n quality: (options.quality as number) || this.quality,\n };\n\n await page.screenshot(responsiveScreenshotOptions);\n\n await page.close();\n\n console.log(`Responsive screenshot saved: ${filepath}`);\n results.push({ viewport: viewport.name, success: true, filepath });\n } catch (error) {\n console.error(\n `Error taking responsive screenshot for ${viewport.name}:`,\n (error as Error).message\n );\n results.push({\n viewport: viewport.name,\n success: false,\n error: (error as Error).message,\n filepath: \"\",\n });\n }\n }\n\n return results;\n }\n}\n\nexport default ScreenshotTool;\n","import type { PossiblePromise } from \"./utils/types\";\nimport type { UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use vite.config.ts accepts a direct UserConfig object, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig | Array<UserConfig>>)\n): typeof options {\n return options;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,IAAM,iBAAN,MAAqB;CACnB;CACA;CACA;CACA;CACA;CACA;CACA,UAA0B;CAC1B,UAAiC;CACjC,YAAY,SAOT;AACD,OAAK,cAAc,QAAQ,eAAe;AAC1C,OAAK,WAAW,QAAQ,aAAa;AACrC,OAAK,WAAW,QAAQ,YAAY;GAAE,OAAO;GAAM,QAAQ;GAAM;AACjE,OAAK,YAAY,QAAQ,aAAa;AACtC,OAAK,UAAU,QAAQ,WAAW;AAClC,OAAK,WAAW,QAAQ,aAAa;;CAGvC,MAAM,OAAO;AAEX,MAAI,CAACA,gBAAG,WAAW,KAAK,UAAU,CAChC,iBAAG,UAAU,KAAK,WAAW,EAAE,WAAW,MAAM,CAAC;EAUnD,MAAM,eANa;GACjB;GACA;GACA;GACD,CAGY,KAAK;AAClB,MAAI,CAAC,aACH,OAAM,IAAI,MAAM,6BAA6B,KAAK,cAAc;AAGlE,OAAK,UAAU,MAAM,aAAa,OAAO,EACvC,UAAU,KAAK,UAChB,CAAC;AAEF,OAAK,UAAU,MAAM,KAAK,QAAQ,WAAW;GAC3C,eAAe;GACf,mBAAmB;GACpB,CAAC;;CAGJ,MAAM,QAAQ;AACZ,MAAI,KAAK,QACP,OAAM,KAAK,QAAQ,OAAO;;CAI9B,MAAM,UAAU;AACd,M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;EAG5C,MAAMC,UAKA,EAAE;AAER,OAAK,MAAM,YAAY,UACrB,KAAI;GACF,MAAM,OAAO,MAAM,KAAK,SAAS,SAAS;AAC1C,SAAM,KAAK,gBAAgB,SAAS;AAEpC,SAAM,KAAK,KAAK,KAAK,EAAE,WAAW,oBAAoB,CAAC;AACvD,SAAM,KAAK,eAAe,IAAK;GAE/B,MAAM,WAAW,GAAG,SAAS,KAAK;GAClC,MAAM,WAAW,KAAK,YAAY,SAAS;GAE3C,MAAM,8BAA8B;IAClC,MAAM;IACN,UACG,QAAQ,aAAyB,SAC9B,QAAQ,WACR,KAAK;IACX,MAAO,QAAQ,QAA2B;IAC1C,SAAU,QAAQ,WAAsB,KAAK;IAC9C;AAED,SAAM,KAAK,WAAW,4BAA4B;AAElD,SAAM,KAAK,OAAO;AAElB,WAAQ,IAAI,gCAAgC,WAAW;AACvD,WAAQ,KAAK;IAAE,UAAU,SAAS;IAAM,SAAS;IAAM;IAAU,CAAC;WAC3D,OAAO;AACd,WAAQ,MACN,0CAA0C,SAAS,KAAK,IACvD,MAAgB,QAClB;AACD,WAAQ,KAAK;IACX,UAAU,SAAS;IACnB,SAAS;IACT,OAAQ,MAAgB;IACxB,UAAU;IACX,CAAC;;AAIN,SAAO;;;AAIX,yBAAe;;;;;;;ACnSf,SAAgB,aACd,SAGgB;AAChB,QAAO"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { chromium, firefox, webkit } from "playwright";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
|
|
5
|
+
//#region src/screenshot.ts
|
|
6
|
+
var ScreenshotTool = class {
|
|
7
|
+
browserType;
|
|
8
|
+
headless;
|
|
9
|
+
viewport;
|
|
10
|
+
outputDir;
|
|
11
|
+
quality;
|
|
12
|
+
fullPage;
|
|
13
|
+
browser = null;
|
|
14
|
+
context = null;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.browserType = options.browserType || "chromium";
|
|
17
|
+
this.headless = options.headless !== false;
|
|
18
|
+
this.viewport = options.viewport || {
|
|
19
|
+
width: 1920,
|
|
20
|
+
height: 1080
|
|
21
|
+
};
|
|
22
|
+
this.outputDir = options.outputDir || "./screenshots";
|
|
23
|
+
this.quality = options.quality || 90;
|
|
24
|
+
this.fullPage = options.fullPage !== false;
|
|
25
|
+
}
|
|
26
|
+
async init() {
|
|
27
|
+
if (!fs.existsSync(this.outputDir)) fs.mkdirSync(this.outputDir, { recursive: true });
|
|
28
|
+
const browserClass = {
|
|
29
|
+
chromium,
|
|
30
|
+
firefox,
|
|
31
|
+
webkit
|
|
32
|
+
}[this.browserType];
|
|
33
|
+
if (!browserClass) throw new Error(`Unsupported browser type: ${this.browserType}`);
|
|
34
|
+
this.browser = await browserClass.launch({ headless: this.headless });
|
|
35
|
+
this.context = await this.browser.newContext({
|
|
36
|
+
reducedMotion: "reduce",
|
|
37
|
+
deviceScaleFactor: 1
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async close() {
|
|
41
|
+
if (this.browser) await this.browser.close();
|
|
42
|
+
}
|
|
43
|
+
async newPage() {
|
|
44
|
+
if (!this.context) throw new Error("Browser not initialized");
|
|
45
|
+
const page = await this.context?.newPage();
|
|
46
|
+
await page.setViewportSize(this.viewport);
|
|
47
|
+
return page;
|
|
48
|
+
}
|
|
49
|
+
async goTo(page, url) {
|
|
50
|
+
if (!this.context) throw new Error("Browser not initialized");
|
|
51
|
+
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
52
|
+
}
|
|
53
|
+
getFilePath(filename) {
|
|
54
|
+
return path.join(this.outputDir, filename);
|
|
55
|
+
}
|
|
56
|
+
async takeScreenshot(page, filename, options = {}) {
|
|
57
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
58
|
+
try {
|
|
59
|
+
if (options.waitForSelector) {
|
|
60
|
+
console.log(`Waiting for selector: ${options.waitForSelector}`);
|
|
61
|
+
await page.waitForSelector(options.waitForSelector, { timeout: 1e4 });
|
|
62
|
+
}
|
|
63
|
+
const waitTime = options.waitForTimeout !== void 0 ? options.waitForTimeout : 0;
|
|
64
|
+
if (waitTime > 0) {
|
|
65
|
+
console.log(`Waiting for ${waitTime}ms for dynamic content`);
|
|
66
|
+
await page.waitForTimeout(waitTime);
|
|
67
|
+
}
|
|
68
|
+
const filepath = this.getFilePath(filename);
|
|
69
|
+
const screenshotOptions = {
|
|
70
|
+
path: filepath,
|
|
71
|
+
fullPage: options.fullPage !== void 0 ? options.fullPage : this.fullPage,
|
|
72
|
+
type: options.type || "jpeg",
|
|
73
|
+
quality: options.quality || this.quality
|
|
74
|
+
};
|
|
75
|
+
await page.screenshot(screenshotOptions);
|
|
76
|
+
console.log(`Screenshot saved: ${filepath}`);
|
|
77
|
+
return filepath;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error(`Error taking screenshot of ${page.url()}:`, error.message);
|
|
80
|
+
throw error;
|
|
81
|
+
} finally {
|
|
82
|
+
await page.close();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async takeElementScreenshot(page, selector, options = {}) {
|
|
86
|
+
if (!this.browser) throw new Error("Browser not initialized");
|
|
87
|
+
try {
|
|
88
|
+
await page.waitForSelector(selector, { timeout: 1e4 });
|
|
89
|
+
const element = await page.$(selector);
|
|
90
|
+
if (!element) throw new Error(`Element not found: ${selector}`);
|
|
91
|
+
const filename = options.filename || `${selector.replace(/[^a-zA-Z0-9]/g, "_")}.png`;
|
|
92
|
+
const filepath = this.getFilePath(filename);
|
|
93
|
+
const elementScreenshotOptions = {
|
|
94
|
+
path: filepath,
|
|
95
|
+
type: options.type || "jpeg",
|
|
96
|
+
quality: options.quality || this.quality
|
|
97
|
+
};
|
|
98
|
+
await element.screenshot(elementScreenshotOptions);
|
|
99
|
+
console.log(`Element screenshot saved: ${filepath}`);
|
|
100
|
+
return filepath;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error(`Error taking element screenshot of ${page.url()}:`, error.message);
|
|
103
|
+
throw error;
|
|
104
|
+
} finally {
|
|
105
|
+
await page.close();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async batchScreenshots(urls, options = {}) {
|
|
109
|
+
const results = [];
|
|
110
|
+
for (const url of urls) try {
|
|
111
|
+
const page = await this.newPage();
|
|
112
|
+
await this.goTo(page, url);
|
|
113
|
+
const filepath = await this.takeScreenshot(page, url, options);
|
|
114
|
+
results.push({
|
|
115
|
+
url,
|
|
116
|
+
success: true,
|
|
117
|
+
filepath
|
|
118
|
+
});
|
|
119
|
+
} catch (error) {
|
|
120
|
+
results.push({
|
|
121
|
+
url,
|
|
122
|
+
success: false,
|
|
123
|
+
error: error.message,
|
|
124
|
+
filepath: ""
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return results;
|
|
128
|
+
}
|
|
129
|
+
async takeResponsiveScreenshots(url, viewports = [
|
|
130
|
+
{
|
|
131
|
+
width: 1920,
|
|
132
|
+
height: 1080,
|
|
133
|
+
name: "desktop"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
width: 768,
|
|
137
|
+
height: 1024,
|
|
138
|
+
name: "tablet"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
width: 375,
|
|
142
|
+
height: 667,
|
|
143
|
+
name: "mobile"
|
|
144
|
+
}
|
|
145
|
+
], options = {}) {
|
|
146
|
+
if (!this.context) throw new Error("Browser not initialized");
|
|
147
|
+
const results = [];
|
|
148
|
+
for (const viewport of viewports) try {
|
|
149
|
+
const page = await this.context?.newPage();
|
|
150
|
+
await page.setViewportSize(viewport);
|
|
151
|
+
await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
152
|
+
await page.waitForTimeout(2e3);
|
|
153
|
+
const filename = `${viewport.name}.png`;
|
|
154
|
+
const filepath = this.getFilePath(filename);
|
|
155
|
+
const responsiveScreenshotOptions = {
|
|
156
|
+
path: filepath,
|
|
157
|
+
fullPage: options.fullPage !== void 0 ? options.fullPage : this.fullPage,
|
|
158
|
+
type: options.type || "png",
|
|
159
|
+
quality: options.quality || this.quality
|
|
160
|
+
};
|
|
161
|
+
await page.screenshot(responsiveScreenshotOptions);
|
|
162
|
+
await page.close();
|
|
163
|
+
console.log(`Responsive screenshot saved: ${filepath}`);
|
|
164
|
+
results.push({
|
|
165
|
+
viewport: viewport.name,
|
|
166
|
+
success: true,
|
|
167
|
+
filepath
|
|
168
|
+
});
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.error(`Error taking responsive screenshot for ${viewport.name}:`, error.message);
|
|
171
|
+
results.push({
|
|
172
|
+
viewport: viewport.name,
|
|
173
|
+
success: false,
|
|
174
|
+
error: error.message,
|
|
175
|
+
filepath: ""
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return results;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
var screenshot_default = ScreenshotTool;
|
|
182
|
+
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region src/config.ts
|
|
185
|
+
/**
|
|
186
|
+
* Type helper to make it easier to use vite.config.ts accepts a direct UserConfig object, or a function that returns it. The function receives a ConfigEnv object.
|
|
187
|
+
*/
|
|
188
|
+
function defineConfig(options) {
|
|
189
|
+
return options;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
//#endregion
|
|
193
|
+
export { screenshot_default as ScreenshotTool, defineConfig };
|
|
194
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["results: {\n url: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[]","results: {\n viewport: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[]"],"sources":["../src/screenshot.ts","../src/config.ts"],"sourcesContent":["import {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright\";\nimport path from \"node:path\";\nimport fs from \"node:fs\";\n\nclass ScreenshotTool {\n browserType: string;\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n quality: number;\n fullPage: boolean;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n constructor(options: {\n browserType?: string;\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n quality?: number;\n fullPage?: boolean;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.quality = options.quality || 90;\n this.fullPage = options.fullPage !== false; // Default to full page\n }\n\n async init() {\n // Create output directory if it doesn't exist\n if (!fs.existsSync(this.outputDir)) {\n fs.mkdirSync(this.outputDir, { recursive: true });\n }\n\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass =\n browserMap[this.browserType as keyof typeof browserMap];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n this.context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 1,\n });\n }\n\n async close() {\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n async newPage() {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n const page = await this.context?.newPage();\n await page.setViewportSize(this.viewport);\n return page;\n }\n\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n getFilePath(filename: string) {\n const filepath = path.join(this.outputDir, filename);\n return filepath;\n }\n\n async takeScreenshot(\n page: Page,\n filename: string,\n options: {\n fullPage?: boolean;\n type?: string;\n quality?: number;\n waitForSelector?: string;\n waitForTimeout?: number;\n } = {}\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Wait for selector if specified\n if (options.waitForSelector) {\n console.log(`Waiting for selector: ${options.waitForSelector}`);\n await page.waitForSelector(options.waitForSelector, { timeout: 10000 });\n }\n\n // Wait for custom timeout or default\n const waitTime =\n options.waitForTimeout !== undefined ? options.waitForTimeout : 0;\n if (waitTime > 0) {\n console.log(`Waiting for ${waitTime}ms for dynamic content`);\n await page.waitForTimeout(waitTime);\n }\n\n // Generate filename\n const filepath = this.getFilePath(filename);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage:\n (options.fullPage as boolean) !== undefined\n ? options.fullPage\n : this.fullPage,\n type: (options.type as \"jpeg\" | \"png\") || \"jpeg\",\n quality: (options.quality as number) || this.quality,\n } satisfies PageScreenshotOptions;\n\n await page.screenshot(screenshotOptions);\n\n console.log(`Screenshot saved: ${filepath}`);\n return filepath;\n } catch (error) {\n console.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message\n );\n throw error;\n } finally {\n await page.close();\n }\n }\n\n async takeElementScreenshot(\n page: Page,\n selector: string,\n options: {\n filename?: string;\n fullPage?: boolean;\n type?: string;\n quality?: number;\n } = {}\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Wait for element to be visible\n await page.waitForSelector(selector, { timeout: 10000 });\n\n const element = await page.$(selector);\n if (!element) {\n throw new Error(`Element not found: ${selector}`);\n }\n\n const filename =\n (options.filename as string | undefined) ||\n `${selector.replace(/[^a-zA-Z0-9]/g, \"_\")}.png`;\n const filepath = this.getFilePath(filename);\n\n const elementScreenshotOptions = {\n path: filepath,\n type: (options.type as \"jpeg\" | \"png\") || \"jpeg\",\n quality: (options.quality as number) || this.quality,\n } satisfies PageScreenshotOptions;\n\n await element.screenshot(elementScreenshotOptions);\n\n console.log(`Element screenshot saved: ${filepath}`);\n return filepath;\n } catch (error) {\n console.error(\n `Error taking element screenshot of ${page.url()}:`,\n (error as Error).message\n );\n throw error;\n } finally {\n await page.close();\n }\n }\n\n async batchScreenshots(urls: string[], options = {}) {\n const results: {\n url: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[] = [];\n\n for (const url of urls) {\n try {\n const page = await this.newPage();\n await this.goTo(page, url);\n const filepath = await this.takeScreenshot(page, url, options);\n results.push({ url, success: true, filepath });\n } catch (error) {\n results.push({\n url,\n success: false,\n error: (error as Error).message,\n filepath: \"\",\n });\n }\n }\n\n return results;\n }\n\n async takeResponsiveScreenshots(\n url: string,\n viewports: { width: number; height: number; name: string }[] = [\n { width: 1920, height: 1080, name: \"desktop\" },\n { width: 768, height: 1024, name: \"tablet\" },\n { width: 375, height: 667, name: \"mobile\" },\n ],\n options: {\n fullPage?: boolean;\n type?: string;\n quality?: number;\n } = {}\n ) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n\n const results: {\n viewport: string;\n success: boolean;\n filepath: string;\n error?: string;\n }[] = [];\n\n for (const viewport of viewports) {\n try {\n const page = await this.context?.newPage();\n await page.setViewportSize(viewport);\n\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n await page.waitForTimeout(2000);\n\n const filename = `${viewport.name}.png`;\n const filepath = this.getFilePath(filename);\n\n const responsiveScreenshotOptions = {\n path: filepath,\n fullPage:\n (options.fullPage as boolean) !== undefined\n ? options.fullPage\n : this.fullPage,\n type: (options.type as \"jpeg\" | \"png\") || \"png\",\n quality: (options.quality as number) || this.quality,\n };\n\n await page.screenshot(responsiveScreenshotOptions);\n\n await page.close();\n\n console.log(`Responsive screenshot saved: ${filepath}`);\n results.push({ viewport: viewport.name, success: true, filepath });\n } catch (error) {\n console.error(\n `Error taking responsive screenshot for ${viewport.name}:`,\n (error as Error).message\n );\n results.push({\n viewport: viewport.name,\n success: false,\n error: (error as Error).message,\n filepath: \"\",\n });\n }\n }\n\n return results;\n }\n}\n\nexport default ScreenshotTool;\n","import type { PossiblePromise } from \"./utils/types\";\nimport type { UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use vite.config.ts accepts a direct UserConfig object, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig |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