Skip to content

Commit

Permalink
feat: export puppeteer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Jun 9, 2022
1 parent b30f3f4 commit 75edbda
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 167 deletions.
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
19 changes: 0 additions & 19 deletions src/.eslintrc.js

This file was deleted.

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/mocha-utils.ts
Expand Up @@ -19,7 +19,7 @@ import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import sinon from 'sinon';
import puppeteer from '../lib/cjs/puppeteer/node.js';
import puppeteer from '../lib/cjs/puppeteer/puppeteer.js';
import {
Browser,
BrowserContext,
Expand Down
6 changes: 3 additions & 3 deletions utils/prepare_puppeteer_core.js
Expand Up @@ -24,8 +24,8 @@ const json = require(packagePath);
delete json.scripts.install;

json.name = 'puppeteer-core';
json.main = './cjs-entry-core.js';
json.exports['.'].import = './lib/esm/puppeteer/node-puppeteer-core.js';
json.exports['.'].require = './cjs-entry-core.js';
json.main = './lib/cjs/puppeteer/puppeteer-core.js';
json.exports['.'].import = './lib/esm/puppeteer/puppeteer-core.js';
json.exports['.'].require = './lib/cjs/puppeteer/puppeteer-core.js';

fs.writeFileSync(packagePath, JSON.stringify(json, null, ' '));

0 comments on commit 75edbda

Please sign in to comment.