Skip to content

Commit

Permalink
feat: bundle types
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Jun 7, 2022
1 parent 7d66843 commit cc2446a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 141 deletions.
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
"description": "Replay is a library which provides an API to replay and stringify recordings created using Chrome DevTools Recorder](https://developer.chrome.com/docs/devtools/recorder/)",
"main": "lib/main.js",
"types": "lib/main.d.ts",
"bin": "./lib/cli.js",
"bin": "lib/cli.js",
"exports": {
".": {
"types": "./lib/main.d.ts",
"import": "./lib/main.js",
"require": "./lib/cjs/main.cjs"
}
},
"repository": "github:puppeteer/replay",
"directories": {
"doc": "docs"
Expand All @@ -18,9 +25,8 @@
"test:coverage": "c8 npm run test",
"eslint": "([ \"$CI\" = true ] && eslint --ext js --ext ts --quiet -f codeframe . || eslint --ext js --ext ts .)",
"eslint-fix": "eslint --ext js --ext ts --fix .",
"clean-lib": "rimraf lib",
"build": "npm run tsc",
"tsc": "npm run clean-lib && tsc --version && tsc -b tsconfig.json && tsc -b tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
"clean": "rimraf lib",
"build": "rollup --config rollup.config.ts --configPlugin typescript",
"lint": "npm run eslint",
"docs": "typedoc --readme none --gitRevision main --externalPattern --excludeExternals --excludeProtected --excludePrivate --plugin typedoc-plugin-markdown --out docs/api src/main.ts",
"release": "standard-version"
Expand All @@ -36,6 +42,7 @@
"license": "Apache-2.0",
"type": "module",
"devDependencies": {
"@rollup/plugin-typescript": "^8.3.2",
"@types/chai": "4.3.1",
"@types/mocha": "9.1.1",
"@types/node": "17.0.36",
Expand All @@ -53,17 +60,19 @@
"mime": "3.0.0",
"mocha": "10.0.0",
"prettier": "2.6.2",
"puppeteer": "14.2.1",
"puppeteer": "14.3.0",
"rimraf": "3.0.2",
"rollup": "2.75.6",
"rollup-plugin-dts": "4.2.2",
"snap-shot-it": "7.9.6",
"standard-version": "^9.5.0",
"standard-version": "9.5.0",
"ts-node": "10.8.0",
"typedoc": "0.22.15",
"typedoc-plugin-markdown": "3.12.1",
"typescript": "4.6.4"
},
"peerDependencies": {
"puppeteer": "^14.1.0"
"puppeteer": "^14.3.0"
},
"peerDependenciesMeta": {
"puppeteer": {
Expand Down
45 changes: 45 additions & 0 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import typescript from '@rollup/plugin-typescript';
import dts from 'rollup-plugin-dts';

export default [
{
input: 'src/main.ts',
output: {
file: 'lib/main.js',
format: 'es',
},
plugins: [typescript()],
},
{
input: 'src/main.ts',
output: {
file: 'lib/cjs/main.cjs',
format: 'cjs',
},
plugins: [typescript()],
},
{
input: 'src/cli.ts',
output: {
file: 'lib/cli.js',
format: 'es',
banner: '#!/usr/bin/env node',
},
external: ['./main.js'],
plugins: [typescript()],
},
{
input: 'src/main.ts',
output: {
file: 'lib/main.d.ts',
format: 'es',
},
external: [
'devtools-protocol',
'devtools-protocol/types/protocol-mapping.js',
],
plugins: [
dts({ respectExternal: true, compilerOptions: { declaration: true } }),
],
},
];
132 changes: 1 addition & 131 deletions src/PuppeteerRunnerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
limitations under the License.
*/

import { Browser, Page, Frame, ElementHandle } from 'puppeteer';
import { RunnerExtension } from './RunnerExtension.js';
import {
UserFlow,
Step,
WaitForElementStep,
Selector,
Key,
ChangeStep,
} from './Schema.js';
import {
Expand Down Expand Up @@ -638,133 +638,3 @@ async function waitForFunction(
}
throw new Error('Timed out');
}

// Partial description of Puppeteer API below to allow runtime dependencies.
type EvaluateFn<T = any> = string | ((arg1: T, ...args: any[]) => any);
type EvaluateFnReturnType<T extends EvaluateFn> = T extends (
...args: any[]
) => infer R
? R
: any;
type SerializableOrJSHandle = Serializable | JSHandle;
type JSONArray = readonly Serializable[];
interface JSONObject {
[key: string]: Serializable;
}
type Serializable =
| number
| string
| boolean
| null
| BigInt
| JSONArray
| JSONObject;
type UnwrapPromiseLike<T> = T extends PromiseLike<infer U> ? U : T;

interface Target {
url(): string;
page(): Promise<Page | null>;
}

export interface WaitForTargetOptions {
/**
* Maximum wait time in milliseconds. Pass `0` to disable the timeout.
* @defaultValue 30 seconds.
*/
timeout?: number;
}

interface Browser {
close(): Promise<void>;
waitForTarget(
predicate: (x: Target) => boolean,
options?: WaitForTargetOptions
): Promise<Target>;
}

interface JSHandle<HandleObjectType = unknown> {
// TODO: fix the type of evaluate.
evaluate<T extends EvaluateFn<HandleObjectType>>(
...args: any[]
): Promise<UnwrapPromiseLike<EvaluateFnReturnType<T>>>;
// TODO: fix the type of evaluateHandle.
evaluateHandle(...args: any[]): Promise<JSHandle<Element>>;
asElement(): ElementHandle<Element> | null;
}

interface ElementHandle<ElementType extends Element>
extends JSHandle<ElementType> {
isIntersectingViewport(opts: { threshold: number }): Promise<boolean>;
dispose(): Promise<void>;
click(opts: {
delay?: number;
button?: 'left' | 'right' | 'middle' | 'back' | 'forward';
clickCount?: number;
offset: {
x: number;
y: number;
};
}): Promise<void>;
type(input: string): Promise<void>;
hover(): Promise<void>;
focus(): Promise<void>;
$$<T extends Element = Element>(
selector: string
): Promise<Array<ElementHandle<T>>>;
waitForSelector(
selector: string,
options?: {
visible?: boolean;
hidden?: boolean;
timeout?: number;
}
): Promise<ElementHandle<Element> | null>;
asElement(): ElementHandle<ElementType> | null;
select(...args: string[]): Promise<unknown>;
}

interface CDPSession {
send(command: string, opts: { enabled: boolean }): Promise<unknown>;
}

interface Page {
setDefaultTimeout(timeout: number): void;
frames(): Frame[];
emulateNetworkConditions(conditions: any): void;
keyboard: {
type(value: string): Promise<void>;
down(key: Key): Promise<void>;
up(key: Key): Promise<void>;
};
waitForTimeout(timeout: number): Promise<void>;
close(): Promise<void>;
setViewport(viewport: any): Promise<void>;
mainFrame(): Frame;
waitForNavigation(opts: { timeout: number }): Promise<unknown>;
$$<T extends Element = Element>(
selector: string
): Promise<Array<ElementHandle<T>>>;
waitForFrame(url: string, opts: { timeout: number }): Promise<Frame>;
client(): CDPSession;
}

interface Frame {
waitForSelector(
part: string,
options: WaitForOptions
): Promise<ElementHandle<Element> | null>;
isOOPFrame(): boolean;
url(): string;
evaluate<T extends EvaluateFn>(
pageFunction: T,
...args: SerializableOrJSHandle[]
): Promise<UnwrapPromiseLike<EvaluateFnReturnType<T>>>;
goto(url: string): Promise<unknown>;
waitForFunction(expr: string, opts: { timeout: number }): Promise<unknown>;
childFrames(): Frame[];
waitForNavigation(opts: { timeout: number }): Promise<unknown>;
$$<T extends Element = Element>(
selector: string
): Promise<Array<ElementHandle<T>>>;
client(): CDPSession;
}
2 changes: 0 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

/**
Copyright 2022 Google LLC
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"strict": true,
"skipLibCheck": false,
"outDir": "lib",
"declaration": true,
"moduleResolution": "Node",
"stripInternal": true
},
Expand Down

0 comments on commit cc2446a

Please sign in to comment.