diff --git a/.gitignore b/.gitignore index 0e7ca0bb92f41..7db3ffd3274d1 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,7 @@ yarn.lock test/coverage.json temp/ new-docs/ -puppeteer*.tgz +puppeteer.tgz docs-api-json/ docs-dist/ website/docs diff --git a/compat/README.md b/compat/README.md deleted file mode 100644 index a72ecab4e8a3c..0000000000000 --- a/compat/README.md +++ /dev/null @@ -1,16 +0,0 @@ -# Compatibility layer - -This directory provides an additional compatibility layer between ES modules and CommonJS. - -## Why? - -Both `./cjs/compat.ts` and `./esm/compat.ts` are written as ES modules, but `./cjs/compat.ts` can additionally use NodeJS CommonJS globals such as `__dirname` and `require` while these are disabled in ES module mode. For more information, see [Differences between ES modules and CommonJS](https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs). - -## Adding exports - -In order to add exports, two things need to be done: - -- The exports must be declared in `src/compat.ts`. -- The exports must be realized in `./cjs/compat.ts` and `./esm/compat.ts`. - -In the event `compat.ts` becomes too large, you can place declarations in another file. Just make sure `./cjs`, `./esm`, and `src` have the same structure. diff --git a/compat/cjs/compat.ts b/compat/cjs/compat.ts deleted file mode 100644 index 8bd871b20b859..0000000000000 --- a/compat/cjs/compat.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2022 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { dirname } from 'path'; - -export const puppeteerDirname = dirname(dirname(dirname(__dirname))); diff --git a/compat/cjs/tsconfig.json b/compat/cjs/tsconfig.json deleted file mode 100644 index 0de31b76c4440..0000000000000 --- a/compat/cjs/tsconfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "composite": true, - "outDir": "../../lib/cjs/puppeteer", - "module": "CommonJS" - }, - "references": [ - { "path": "../../vendor/tsconfig.cjs.json"} - ] -} diff --git a/compat/esm/compat.ts b/compat/esm/compat.ts deleted file mode 100644 index 1decaefb280b6..0000000000000 --- a/compat/esm/compat.ts +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Copyright 2022 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { dirname } from 'path'; -import { fileURLToPath } from 'url'; - -export const puppeteerDirname = dirname( - dirname(dirname(dirname(fileURLToPath(import.meta.url)))) -); diff --git a/compat/esm/tsconfig.json b/compat/esm/tsconfig.json deleted file mode 100644 index 42b320fcd3f28..0000000000000 --- a/compat/esm/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "composite": true, - "outDir": "../../lib/esm/puppeteer", - "module": "esnext" - }, - "references": [{ "path": "../../vendor/tsconfig.esm.json" }] -} diff --git a/package.json b/package.json index 3699a17afe6d2..bb42b78827d3c 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,7 @@ "name": "puppeteer", "version": "13.3.0-post", "description": "A high-level API to control headless Chrome over the DevTools Protocol", - "type": "commonjs", "main": "./cjs-entry.js", - "exports": { - ".": { - "import": "./lib/esm/puppeteer/node.js", - "require": "./cjs-entry.js" - }, - "./*": { - "import": "./*", - "require": "./*" - } - }, "types": "lib/types.d.ts", "repository": "github:puppeteer/puppeteer", "engines": { @@ -40,18 +29,15 @@ "lint": "npm run eslint && npm run build && npm run doc && npm run markdownlint", "doc": "node utils/doclint/cli.js", "clean-lib": "rimraf lib", - "build": "npm run tsc && npm run generate-d-ts && npm run generate-pkg-json", - "tsc": "npm run clean-lib && tsc --version && (npm run tsc-cjs & npm run tsc-esm) && (npm run tsc-compat-cjs & npm run tsc-compat-esm)", + "build": "npm run tsc && npm run generate-d-ts", + "tsc": "npm run clean-lib && tsc --version && npm run tsc-cjs && npm run tsc-esm", "tsc-cjs": "tsc -b src/tsconfig.cjs.json", "tsc-esm": "tsc -b src/tsconfig.esm.json", - "tsc-compat-cjs": "tsc -b compat/cjs/tsconfig.json", - "tsc-compat-esm": "tsc -b compat/esm/tsconfig.json", "apply-next-version": "node utils/apply_next_version.js", "test-install": "scripts/test-install.sh", "clean-docs": "rimraf website/docs && rimraf docs-api-json", "generate-d-ts": "npm run clean-docs && api-extractor run --local --verbose", "generate-docs": "npm run generate-d-ts && api-documenter markdown -i docs-api-json -o website/docs && node utils/remove-tag.js", - "generate-pkg-json": "echo '{\"type\": \"module\"}' > lib/esm/package.json", "ensure-correct-devtools-protocol-revision": "ts-node -s scripts/ensure-correct-devtools-protocol-package", "ensure-pinned-deps": "ts-node -s scripts/ensure-pinned-deps", "test-types-file": "ts-node -s scripts/test-ts-definition-files.ts", @@ -64,7 +50,6 @@ "lib/**/*.d.ts.map", "lib/**/*.js", "lib/**/*.js.map", - "lib/**/package.json", "install.js", "typescript-if-required.js", "cjs-entry.js", diff --git a/scripts/test-install.sh b/scripts/test-install.sh index 25ab3fef7c098..e59aa39d00be1 100755 --- a/scripts/test-install.sh +++ b/scripts/test-install.sh @@ -14,31 +14,6 @@ cd $TMPDIR # 3. Requiring Puppeteer from Node works. npm install --loglevel silent "${tarball}" node --eval="require('puppeteer')" -node --eval=" -require('puppeteer/lib/cjs/puppeteer/revisions.js'); -" -ls $TMPDIR/node_modules/puppeteer/.local-chromium/ - -# Testing ES module features -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 puppeteer from 'puppeteer'; - -(async () => { - const browser = await puppeteer.launch(); - const page = await browser.newPage(); - await page.goto('http://example.com'); - await page.screenshot({ path: 'example.png' }); - await browser.close(); -})(); -" ls $TMPDIR/node_modules/puppeteer/.local-chromium/ # Again for Firefox @@ -64,9 +39,3 @@ cd $TMPDIR npm install --loglevel silent "${tarball}" node --eval="require('puppeteer-core')" -# Testing ES module features -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-core'" diff --git a/src/common/Debug.ts b/src/common/Debug.ts index ce01a7d7dd208..8ff124b094b3b 100644 --- a/src/common/Debug.ts +++ b/src/common/Debug.ts @@ -55,9 +55,7 @@ import { isNode } from '../environment.js'; export const debug = (prefix: string): ((...args: unknown[]) => void) => { if (isNode) { // eslint-disable-next-line @typescript-eslint/no-var-requires - return async (...logArgs: unknown[]) => { - (await import('debug')).default(prefix)(logArgs); - }; + return require('debug')(prefix); } return (...logArgs: unknown[]): void => { diff --git a/src/compat.ts b/src/compat.ts deleted file mode 100644 index cc48163eb6707..0000000000000 --- a/src/compat.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright 2022 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -declare const puppeteerDirname: string; - -export { puppeteerDirname }; diff --git a/src/initialize-node.ts b/src/initialize-node.ts index 1e8bd75543019..673125ac4670f 100644 --- a/src/initialize-node.ts +++ b/src/initialize-node.ts @@ -18,10 +18,9 @@ import { PuppeteerNode } from './node/Puppeteer.js'; import { PUPPETEER_REVISIONS } from './revisions.js'; import { sync } from 'pkg-dir'; import { Product } from './common/Product.js'; -import { puppeteerDirname } from './compat.js'; export const initializePuppeteerNode = (packageName: string): PuppeteerNode => { - const puppeteerRootDirectory = sync(puppeteerDirname); + const puppeteerRootDirectory = sync(__dirname); let preferredRevision = PUPPETEER_REVISIONS.chromium; const isPuppeteerCore = packageName === 'puppeteer-core'; diff --git a/src/node/BrowserFetcher.ts b/src/node/BrowserFetcher.ts index 646602e7b6046..01140df4c487b 100644 --- a/src/node/BrowserFetcher.ts +++ b/src/node/BrowserFetcher.ts @@ -34,8 +34,6 @@ import createHttpsProxyAgent, { } from 'https-proxy-agent'; import { getProxyForUrl } from 'proxy-from-env'; import { assert } from '../common/assert.js'; -import tar from 'tar-fs'; -import bzip from 'unbzip2-stream'; const debugFetcher = debug('puppeteer:fetcher'); @@ -501,6 +499,10 @@ function install(archivePath: string, folderPath: string): Promise { * @internal */ function extractTar(tarPath: string, folderPath: string): Promise { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const tar = require('tar-fs'); + // eslint-disable-next-line @typescript-eslint/no-var-requires + const bzip = require('unbzip2-stream'); return new Promise((fulfill, reject) => { const tarStream = tar.extract(folderPath); tarStream.on('error', reject); diff --git a/src/node/NodeWebSocketTransport.ts b/src/node/NodeWebSocketTransport.ts index 1e679b6e6d2f6..393763239fef3 100644 --- a/src/node/NodeWebSocketTransport.ts +++ b/src/node/NodeWebSocketTransport.ts @@ -15,26 +15,18 @@ */ import { ConnectionTransport } from '../common/ConnectionTransport.js'; import NodeWebSocket from 'ws'; -import { readFileSync } from 'fs'; -import { join } from 'path'; -import { puppeteerDirname } from '../compat.js'; - -// We parse rather than import for ES module compatibility. -const version = JSON.parse( - readFileSync(join(puppeteerDirname, 'package.json'), { - encoding: 'utf8', - }) -).version; export class NodeWebSocketTransport implements ConnectionTransport { static create(url: string): Promise { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pkg = require('../../../../package.json'); return new Promise((resolve, reject) => { const ws = new NodeWebSocket(url, [], { followRedirects: true, perMessageDeflate: false, maxPayload: 256 * 1024 * 1024, // 256Mb headers: { - 'User-Agent': `Puppeteer ${version}`, + 'User-Agent': `Puppeteer ${pkg.version}`, }, }); diff --git a/tsconfig.json b/tsconfig.json index 0abb2d09decad..0fe7e06ad83c7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ */ "module": "esnext" }, - "include": ["src"], + "include": ["src"] } diff --git a/utils/prepare_puppeteer_core.js b/utils/prepare_puppeteer_core.js index be4644eea1efc..e1e9a64f2f4ed 100755 --- a/utils/prepare_puppeteer_core.js +++ b/utils/prepare_puppeteer_core.js @@ -21,10 +21,7 @@ const path = require('path'); const packagePath = path.join(__dirname, '..', 'package.json'); const json = require(packagePath); -delete json.scripts.install; - json.name = 'puppeteer-core'; +delete json.scripts.install; json.main = './cjs-entry-core.js'; -json.exports['.'].imports = './lib/esm/puppeteer/node-puppeteer-core.js'; -json.exports['.'].require = './cjs-entry-core.js'; fs.writeFileSync(packagePath, JSON.stringify(json, null, ' '));