Skip to content

Commit 5ff698e

Browse files
authored
chore: move index.js into src and TS-ify (#6049)
It was causing some infra issues when trying to migrate tests to TypeScript (that's WIP in another branch that I'll have up soon). It's unusual to have the entire src in TS except for the main file, which then reaches into the compiled `lib` directory for the files it needs. Much better is to move the entry point into TypeScript itself and update the `main` entry in our `package.json` to point to the compiled output. This also has the advantange of hooking up all the TS type defs that we are shipping and will make that process easier too, along with making it easier to port our tests to TypeScript.
1 parent 940a570 commit 5ff698e

File tree

5 files changed

+91
-81
lines changed

5 files changed

+91
-81
lines changed

index.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

install.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function download() {
4141
process.env.PUPPETEER_DOWNLOAD_HOST ||
4242
process.env.npm_config_puppeteer_download_host ||
4343
process.env.npm_package_config_puppeteer_download_host;
44-
const puppeteer = require('./index');
44+
const puppeteer = require('.');
4545
const product =
4646
process.env.PUPPETEER_PRODUCT ||
4747
process.env.npm_config_puppeteer_product ||

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "puppeteer",
33
"version": "4.0.0-post",
44
"description": "A high-level API to control headless Chrome over the DevTools Protocol",
5-
"main": "index.js",
5+
"main": "lib/index.js",
66
"repository": "github:puppeteer/puppeteer",
77
"engines": {
88
"node": ">=10.18.1"

src/index.ts

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2020 Google Inc. All rights reserved.
2+
* Copyright 2017 Google Inc. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,59 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
// api.ts has to use module.exports as it's also consumed by DocLint which runs
18-
// on Node.
19-
// eslint-disable-next-line @typescript-eslint/no-var-requires
20-
const api = require('./api');
17+
import { initializePuppeteer } from './initialize';
18+
import * as path from 'path';
2119

22-
import { helper } from './common/helper';
23-
import { Page } from './common/Page';
24-
import { Puppeteer } from './common/Puppeteer';
20+
const puppeteer = initializePuppeteer({
21+
packageJson: require(path.join(__dirname, '..', 'package.json')),
22+
rootDirectory: path.join(__dirname, '..'),
23+
});
2524

26-
interface InitOptions {
27-
packageJson: {
28-
puppeteer: {
29-
chromium_revision: string;
30-
firefox_revision: string;
31-
};
32-
name: string;
33-
};
34-
rootDirectory: string;
35-
}
36-
37-
export const initializePuppeteer = (options: InitOptions): Puppeteer => {
38-
const { packageJson, rootDirectory } = options;
39-
40-
for (const className in api) {
41-
if (typeof api[className] === 'function')
42-
helper.installAsyncStackHooks(api[className]);
43-
}
44-
45-
// Expose alias for deprecated method.
46-
// @ts-expect-error emulateMedia does not exist error
47-
Page.prototype.emulateMedia = Page.prototype.emulateMediaType;
48-
49-
let preferredRevision = packageJson.puppeteer.chromium_revision;
50-
const isPuppeteerCore = packageJson.name === 'puppeteer-core';
51-
// puppeteer-core ignores environment variables
52-
const product = isPuppeteerCore
53-
? undefined
54-
: process.env.PUPPETEER_PRODUCT ||
55-
process.env.npm_config_puppeteer_product ||
56-
process.env.npm_package_config_puppeteer_product;
57-
if (!isPuppeteerCore && product === 'firefox')
58-
preferredRevision = packageJson.puppeteer.firefox_revision;
59-
60-
const puppeteer = new Puppeteer(
61-
rootDirectory,
62-
preferredRevision,
63-
isPuppeteerCore,
64-
product
65-
);
66-
67-
// The introspection in `Helper.installAsyncStackHooks` references `Puppeteer._launcher`
68-
// before the Puppeteer ctor is called, such that an invalid Launcher is selected at import,
69-
// so we reset it.
70-
puppeteer._lazyLauncher = undefined;
71-
return puppeteer;
72-
};
25+
/*
26+
* Has to be CJS here rather than ESM such that the output file ends with
27+
* module.exports = puppeteer.
28+
*
29+
* If this was export default puppeteer the output would be:
30+
* exports.default = puppeteer
31+
* And therefore consuming via require('puppeteer') would break / require the user
32+
* to access require('puppeteer').default;
33+
*/
34+
module.exports = puppeteer;

src/initialize.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2020 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// api.ts has to use module.exports as it's also consumed by DocLint which runs
18+
// on Node.
19+
// eslint-disable-next-line @typescript-eslint/no-var-requires
20+
const api = require('./api');
21+
22+
import { helper } from './common/helper';
23+
import { Page } from './common/Page';
24+
import { Puppeteer } from './common/Puppeteer';
25+
26+
interface InitOptions {
27+
packageJson: {
28+
puppeteer: {
29+
chromium_revision: string;
30+
firefox_revision: string;
31+
};
32+
name: string;
33+
};
34+
rootDirectory: string;
35+
}
36+
37+
export const initializePuppeteer = (options: InitOptions): Puppeteer => {
38+
const { packageJson, rootDirectory } = options;
39+
40+
for (const className in api) {
41+
if (typeof api[className] === 'function')
42+
helper.installAsyncStackHooks(api[className]);
43+
}
44+
45+
// Expose alias for deprecated method.
46+
// @ts-expect-error emulateMedia does not exist error
47+
Page.prototype.emulateMedia = Page.prototype.emulateMediaType;
48+
49+
let preferredRevision = packageJson.puppeteer.chromium_revision;
50+
const isPuppeteerCore = packageJson.name === 'puppeteer-core';
51+
// puppeteer-core ignores environment variables
52+
const product = isPuppeteerCore
53+
? undefined
54+
: process.env.PUPPETEER_PRODUCT ||
55+
process.env.npm_config_puppeteer_product ||
56+
process.env.npm_package_config_puppeteer_product;
57+
if (!isPuppeteerCore && product === 'firefox')
58+
preferredRevision = packageJson.puppeteer.firefox_revision;
59+
60+
const puppeteer = new Puppeteer(
61+
rootDirectory,
62+
preferredRevision,
63+
isPuppeteerCore,
64+
product
65+
);
66+
67+
// The introspection in `Helper.installAsyncStackHooks` references `Puppeteer._launcher`
68+
// before the Puppeteer ctor is called, such that an invalid Launcher is selected at import,
69+
// so we reset it.
70+
puppeteer._lazyLauncher = undefined;
71+
return puppeteer;
72+
};

0 commit comments

Comments
 (0)