Skip to content

Commit 523b487

Browse files
jrandolf-zzOrKoNjrandolf-2
authored
feat(puppeteer): export esm modules in package.json (#7964)
* feat(puppeteer): export esm modules in package.json Signed-off-by: Randolf Jung <[email protected]> Co-authored-by: Alex Rudenko <[email protected]> Co-authored-by: Randolf Jung <[email protected]>
1 parent a858cf7 commit 523b487

15 files changed

+108
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ yarn.lock
1919
test/coverage.json
2020
temp/
2121
new-docs/
22-
puppeteer.tgz
22+
puppeteer*.tgz
2323
docs-api-json/
2424
docs-dist/
2525
website/docs

compat/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Compatibility layer
2+
3+
This directory provides an additional compatibility layer between ES modules and CommonJS.
4+
5+
## Why?
6+
7+
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).
8+
9+
## Adding exports
10+
11+
In order to add exports, two things need to be done:
12+
13+
- The exports must be declared in `src/compat.ts`.
14+
- The exports must be realized in `./cjs/compat.ts` and `./esm/compat.ts`.
15+
16+
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.

compat/cjs/compat.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { dirname } from 'path';
2+
3+
export const puppeteerDirname = dirname(dirname(dirname(__dirname)));

compat/cjs/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"outDir": "../../lib/cjs/puppeteer",
6+
"module": "CommonJS"
7+
},
8+
"references": [
9+
{ "path": "../../vendor/tsconfig.cjs.json"}
10+
]
11+
}

compat/esm/compat.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { dirname } from 'path';
2+
import { fileURLToPath } from 'url';
3+
4+
export const puppeteerDirname = dirname(
5+
dirname(dirname(dirname(fileURLToPath(import.meta.url))))
6+
);

compat/esm/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"outDir": "../../lib/esm/puppeteer",
6+
"module": "esnext"
7+
},
8+
"references": [{ "path": "../../vendor/tsconfig.esm.json" }]
9+
}

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
"name": "puppeteer",
33
"version": "13.2.0-post",
44
"description": "A high-level API to control headless Chrome over the DevTools Protocol",
5+
"type": "commonjs",
56
"main": "./cjs-entry.js",
7+
"exports": {
8+
"import": "./lib/esm/puppeteer/node.js",
9+
"require": "./cjs-entry.js"
10+
},
611
"types": "lib/types.d.ts",
712
"repository": "github:puppeteer/puppeteer",
813
"engines": {
@@ -29,15 +34,18 @@
2934
"lint": "npm run eslint && npm run build && npm run doc && npm run markdownlint",
3035
"doc": "node utils/doclint/cli.js",
3136
"clean-lib": "rimraf lib",
32-
"build": "npm run tsc && npm run generate-d-ts",
33-
"tsc": "npm run clean-lib && tsc --version && npm run tsc-cjs && npm run tsc-esm",
37+
"build": "npm run tsc && npm run generate-d-ts && npm run generate-pkg-json",
38+
"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)",
3439
"tsc-cjs": "tsc -b src/tsconfig.cjs.json",
3540
"tsc-esm": "tsc -b src/tsconfig.esm.json",
41+
"tsc-compat-cjs": "tsc -b compat/cjs/tsconfig.json",
42+
"tsc-compat-esm": "tsc -b compat/esm/tsconfig.json",
3643
"apply-next-version": "node utils/apply_next_version.js",
3744
"test-install": "scripts/test-install.sh",
3845
"clean-docs": "rimraf website/docs && rimraf docs-api-json",
3946
"generate-d-ts": "npm run clean-docs && api-extractor run --local --verbose",
4047
"generate-docs": "npm run generate-d-ts && api-documenter markdown -i docs-api-json -o website/docs && node utils/remove-tag.js",
48+
"generate-pkg-json": "echo '{\"type\": \"module\"}' > lib/esm/package.json",
4149
"ensure-correct-devtools-protocol-revision": "ts-node -s scripts/ensure-correct-devtools-protocol-package",
4250
"ensure-pinned-deps": "ts-node -s scripts/ensure-pinned-deps",
4351
"test-types-file": "ts-node -s scripts/test-ts-definition-files.ts",
@@ -50,6 +58,7 @@
5058
"lib/**/*.d.ts.map",
5159
"lib/**/*.js",
5260
"lib/**/*.js.map",
61+
"lib/**/package.json",
5362
"install.js",
5463
"typescript-if-required.js",
5564
"cjs-entry.js",

scripts/test-install.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ npm install --loglevel silent "${tarball}"
1616
node --eval="require('puppeteer')"
1717
ls $TMPDIR/node_modules/puppeteer/.local-chromium/
1818

19+
# Testing ES module features
20+
TMPDIR="$(mktemp -d)"
21+
cd $TMPDIR
22+
echo '{"type":"module"}' >>$TMPDIR/package.json
23+
npm install --loglevel silent "${tarball}"
24+
node --input-type="module" --eval="import puppeteer from 'puppeteer'"
25+
ls $TMPDIR/node_modules/puppeteer/.local-chromium/
26+
27+
node --input-type="module" --eval="
28+
import puppeteer from 'puppeteer';
29+
30+
(async () => {
31+
const browser = await puppeteer.launch();
32+
const page = await browser.newPage();
33+
await page.goto('http://example.com');
34+
await page.screenshot({ path: 'example.png' });
35+
await browser.close();
36+
})();
37+
"
38+
1939
# Again for Firefox
2040
TMPDIR="$(mktemp -d)"
2141
cd $TMPDIR
@@ -39,3 +59,9 @@ cd $TMPDIR
3959
npm install --loglevel silent "${tarball}"
4060
node --eval="require('puppeteer-core')"
4161

62+
# Testing ES module features
63+
TMPDIR="$(mktemp -d)"
64+
cd $TMPDIR
65+
echo '{"type":"module"}' >>$TMPDIR/package.json
66+
npm install --loglevel silent "${tarball}"
67+
node --input-type="module" --eval="import puppeteer from 'puppeteer-core'"

src/common/Debug.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ import { isNode } from '../environment.js';
5555
export const debug = (prefix: string): ((...args: unknown[]) => void) => {
5656
if (isNode) {
5757
// eslint-disable-next-line @typescript-eslint/no-var-requires
58-
return require('debug')(prefix);
58+
return async (...logArgs: unknown[]) => {
59+
(await import('debug')).default(prefix)(logArgs);
60+
};
5961
}
6062

6163
return (...logArgs: unknown[]): void => {

src/compat.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const puppeteerDirname: string;
2+
3+
export { puppeteerDirname };

0 commit comments

Comments
 (0)