Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export puppeteer methods #8493

Merged
merged 1 commit into from Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore
@@ -1,6 +1,5 @@
test/assets/modernizr.js
third_party/*
utils/browser/puppeteer-web.js
utils/doclint/check_public_api/test/
node6/*
node6-test/*
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -14,7 +14,6 @@ test-ts-types/**/dist/
package-lock.json
yarn.lock
/node6
/utils/browser/puppeteer-web.js
/lib
test/coverage.json
temp/
Expand Down
2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -121,8 +121,6 @@ lib
- vendor <== the output of compiling `vendor/tsconfig.esm.json`
```

The main entry point for the Node module Puppeteer is `cjs-entry.js`. This imports `lib/cjs/puppeteer/index.js` and exposes it to Node users.

### tsconfig for the tests

We also maintain `test/tsconfig.test.json`. This is **only used to compile the unit test `*.spec.ts` files**. When the tests are run, we first compile Puppeteer as normal before running the unit tests **against the compiled output**. Doing this lets the test run against the compiled code we ship to users so it gives us more confidence in our compiled output being correct.
Expand Down
29 changes: 0 additions & 29 deletions cjs-entry-core.js

This file was deleted.

29 changes: 0 additions & 29 deletions cjs-entry.js

This file was deleted.

17 changes: 5 additions & 12 deletions package.json
Expand Up @@ -9,12 +9,12 @@
"automation"
],
"type": "commonjs",
"main": "./cjs-entry.js",
"main": "./lib/cjs/puppeteer/puppeteer.js",
"exports": {
".": {
"types": "./lib/types.d.ts",
"import": "./lib/esm/puppeteer/node.js",
"require": "./cjs-entry.js"
"import": "./lib/esm/puppeteer/puppeteer.js",
"require": "./lib/cjs/puppeteer/puppeteer.js"
},
"./*": {
"import": "./*",
Expand Down Expand Up @@ -67,16 +67,9 @@
"build-docs-production": "cd website && npm install && npm run build"
},
"files": [
"lib/types.d.ts",
"lib/**/*.d.ts",
"lib/**/*.d.ts.map",
"lib/**/*.js",
"lib/**/*.js.map",
"lib/**/package.json",
"lib",
"install.js",
"typescript-if-required.js",
"cjs-entry.js",
"cjs-entry-core.js"
"typescript-if-required.js"
],
"author": "The Chromium Authors",
"license": "Apache-2.0",
Expand Down
18 changes: 18 additions & 0 deletions scripts/test-install.sh
Expand Up @@ -40,6 +40,24 @@ import puppeteer from 'puppeteer';
})();
"

echo "Testing... Chrome ES Modules Destructuring"
TMPDIR="$(mktemp -d)"
cd $TMPDIR
echo '{"type":"module"}' >>$TMPDIR/package.json
npm install --loglevel silent "${tarball}"
node --input-type="module" --eval="import puppeteer from 'puppeteer'"
node --input-type="module" --eval="import 'puppeteer/lib/esm/puppeteer/revisions.js';"
node --input-type="module" --eval="
import { launch } from 'puppeteer';
(async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto('http://example.com');
await page.screenshot({ path: 'example.png' });
await browser.close();
})();
"

echo "Testing... Chrome Webpack ES Modules"
TMPDIR="$(mktemp -d)"
cd $TMPDIR
Expand Down
19 changes: 0 additions & 19 deletions src/.eslintrc.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/common/LifecycleWatcher.ts
Expand Up @@ -117,10 +117,10 @@ export class LifecycleWatcher {
helper.addEventListener(
frameManager._client,
CDPSessionEmittedEvents.Disconnected,
() =>
this._terminate(
new Error('Navigation failed because browser has disconnected!')
)
this._terminate.bind(
this,
new Error('Navigation failed because browser has disconnected!')
)
),
helper.addEventListener(
this._frameManager,
Expand Down
8 changes: 8 additions & 0 deletions src/common/Puppeteer.ts
Expand Up @@ -66,6 +66,14 @@ export class Puppeteer {
*/
constructor(settings: CommonPuppeteerSettings) {
this._isPuppeteerCore = settings.isPuppeteerCore;

this.connect = this.connect.bind(this);
this.registerCustomQueryHandler =
this.registerCustomQueryHandler.bind(this);
this.unregisterCustomQueryHandler =
this.unregisterCustomQueryHandler.bind(this);
this.customQueryHandlerNames = this.customQueryHandlerNames.bind(this);
this.clearCustomQueryHandlers = this.clearCustomQueryHandlers.bind(this);
}

/**
Expand Down
24 changes: 0 additions & 24 deletions src/initialize-web.ts

This file was deleted.

20 changes: 10 additions & 10 deletions src/initialize-node.ts → src/initializePuppeteer.ts
Expand Up @@ -14,22 +14,22 @@
* limitations under the License.
*/

import { PuppeteerNode } from './node/Puppeteer.js';
import { PUPPETEER_REVISIONS } from './revisions.js';
import { sync } from 'pkg-dir';
import { Product } from './common/Product.js';
import { rootDirname } from './constants.js';
import { PuppeteerNode } from './node/Puppeteer.js';
import { PUPPETEER_REVISIONS } from './revisions.js';

export const initializePuppeteerNode = (packageName: string): PuppeteerNode => {
export const initializePuppeteer = (packageName: string): PuppeteerNode => {
const isPuppeteerCore = packageName === 'puppeteer-core';
const puppeteerRootDirectory = sync(rootDirname);
let preferredRevision = PUPPETEER_REVISIONS.chromium;
const isPuppeteerCore = packageName === 'puppeteer-core';
// puppeteer-core ignores environment variables
const productName = isPuppeteerCore
? undefined
: process.env['PUPPETEER_PRODUCT'] ||
process.env['npm_config_puppeteer_product'] ||
process.env['npm_package_config_puppeteer_product'];
const productName = !isPuppeteerCore
? ((process.env['PUPPETEER_PRODUCT'] ||
process.env['npm_config_puppeteer_product'] ||
process.env['npm_package_config_puppeteer_product']) as Product)
: undefined;

if (!isPuppeteerCore && productName === 'firefox')
preferredRevision = PUPPETEER_REVISIONS.firefox;
Expand All @@ -38,6 +38,6 @@ export const initializePuppeteerNode = (packageName: string): PuppeteerNode => {
projectRoot: puppeteerRootDirectory,
preferredRevision,
isPuppeteerCore,
productName: productName as Product,
productName,
});
};
6 changes: 6 additions & 0 deletions src/node/Puppeteer.ts
Expand Up @@ -101,6 +101,12 @@ export class PuppeteerNode extends Puppeteer {
this._projectRoot = projectRoot;
this.__productName = productName;
this._preferredRevision = preferredRevision;

this.connect = this.connect.bind(this);
this.launch = this.launch.bind(this);
this.executablePath = this.executablePath.bind(this);
this.defaultArgs = this.defaultArgs.bind(this);
this.createBrowserFetcher = this.createBrowserFetcher.bind(this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/node/install.ts
Expand Up @@ -17,7 +17,7 @@
import https, { RequestOptions } from 'https';
import ProgressBar from 'progress';
import URL from 'url';
import puppeteer from '../node.js';
import puppeteer from '../puppeteer.js';
import { PUPPETEER_REVISIONS } from '../revisions.js';
import { PuppeteerNode } from './Puppeteer.js';
import createHttpsProxyAgent, {
Expand Down
20 changes: 14 additions & 6 deletions src/node-puppeteer-core.ts → src/puppeteer-core.ts
Expand Up @@ -14,12 +14,20 @@
* limitations under the License.
*/

import { isNode } from './environment.js';
import { initializePuppeteerNode } from './initialize-node.js';
import { initializePuppeteer } from './initializePuppeteer.js';

if (!isNode) {
throw new Error('Cannot run puppeteer-core outside of Node.js');
}
const puppeteer = initializePuppeteer('puppeteer-core');

export const {
clearCustomQueryHandlers,
connect,
createBrowserFetcher,
customQueryHandlerNames,
defaultArgs,
executablePath,
launch,
registerCustomQueryHandler,
unregisterCustomQueryHandler,
} = puppeteer;

const puppeteer = initializePuppeteerNode('puppeteer-core');
export default puppeteer;
20 changes: 14 additions & 6 deletions src/node.ts → src/puppeteer.ts
Expand Up @@ -14,12 +14,20 @@
* limitations under the License.
*/

import { isNode } from './environment.js';
import { initializePuppeteerNode } from './initialize-node.js';
import { initializePuppeteer } from './initializePuppeteer.js';

if (!isNode) {
throw new Error('Trying to run Puppeteer-Node in a web environment.');
}
const puppeteer = initializePuppeteer('puppeteer');

export const {
clearCustomQueryHandlers,
connect,
createBrowserFetcher,
customQueryHandlerNames,
defaultArgs,
executablePath,
launch,
registerCustomQueryHandler,
unregisterCustomQueryHandler,
} = puppeteer;

const puppeteer = initializePuppeteerNode('puppeteer');
export default puppeteer;
25 changes: 0 additions & 25 deletions src/web.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test-browser/connection.spec.js
Expand Up @@ -15,7 +15,7 @@
*/
import { Connection } from '../lib/esm/puppeteer/common/Connection.js';
import { BrowserWebSocketTransport } from '../lib/esm/puppeteer/common/BrowserWebSocketTransport.js';
import puppeteer from '../lib/esm/puppeteer/web.js';
import puppeteer from '../lib/esm/puppeteer/puppeteer.js';
import expect from '../node_modules/expect/build-es5/index.js';
import { getWebSocketEndpoint } from './helper.js';

Expand Down
15 changes: 14 additions & 1 deletion test/assert-coverage-test.js
Expand Up @@ -2,14 +2,27 @@ const { describe, it } = require('mocha');
const { getCoverageResults } = require('./coverage-utils.js');
const expect = require('expect');

const EXCLUDED_METHODS = new Set([
'Puppeteer.registerCustomQueryHandler',
'Puppeteer.unregisterCustomQueryHandler',
'Puppeteer.customQueryHandlerNames',
'Puppeteer.clearCustomQueryHandlers',
'PuppeteerNode.connect',
'PuppeteerNode.launch',
'PuppeteerNode.executablePath',
'PuppeteerNode.defaultArgs',
'PuppeteerNode.createBrowserFetcher',
]);

describe('API coverage test', () => {
it('calls every method', () => {
if (!process.env.COVERAGE) return;

const coverageMap = getCoverageResults();
const missingMethods = [];
for (const method of coverageMap.keys()) {
if (!coverageMap.get(method)) missingMethods.push(method);
if (!coverageMap.get(method) && !EXCLUDED_METHODS.has(method))
missingMethods.push(method);
}
if (missingMethods.length) {
console.error(
Expand Down