forked from puppeteer/puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support ES modules (puppeteer#8306)
- Loading branch information
1 parent
be4c930
commit 6841bd6
Showing
20 changed files
with
211 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { dirname } from 'path'; | ||
|
||
let puppeteerDirname: string; | ||
|
||
try { | ||
// In some environments, like esbuild, this will throw an error. | ||
// We suppress the error since the bundled binary is not expected | ||
// to be used or installed in this case and, therefore, the | ||
// root directory does not have to be known. | ||
puppeteerDirname = dirname(require.resolve('./compat')); | ||
} catch (error) { | ||
// Fallback to __dirname. | ||
puppeteerDirname = __dirname; | ||
} | ||
|
||
export { puppeteerDirname }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"composite": true, | ||
"outDir": "../../lib/cjs/puppeteer", | ||
"module": "CommonJS" | ||
}, | ||
"references": [{ "path": "../../vendor/tsconfig.cjs.json" }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createRequire } from 'module'; | ||
import { dirname } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
let puppeteerDirname: string; | ||
|
||
try { | ||
// In some environments, like esbuild, this will throw an error. | ||
// We suppress the error since the bundled binary is not expected | ||
// to be used or installed in this case and, therefore, the | ||
// root directory does not have to be known. | ||
puppeteerDirname = dirname(require.resolve('./compat')); | ||
} catch (error) { | ||
puppeteerDirname = dirname(fileURLToPath(import.meta.url)); | ||
} | ||
|
||
export { puppeteerDirname }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"composite": true, | ||
"outDir": "../../lib/esm/puppeteer", | ||
"module": "esnext" | ||
}, | ||
"references": [{ "path": "../../vendor/tsconfig.esm.json" }] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,111 @@ | ||
#!/usr/bin/env sh | ||
set -e | ||
|
||
# All tests are headed by a echo 'Test'. | ||
# The general schema is: | ||
# 1. Check we can install from the tarball. | ||
# 2. The install script works and correctly exits without errors | ||
# 3. Requiring/importing Puppeteer from Node works. | ||
|
||
## Puppeter tests | ||
|
||
echo "Setting up Puppeteer tests..." | ||
ROOTDIR="$(pwd)" | ||
# Pack the module into a tarball | ||
npm pack | ||
tarball="$(realpath puppeteer-*.tgz)" | ||
|
||
echo "Testing... Chrome CommonJS" | ||
TMPDIR="$(mktemp -d)" | ||
cd $TMPDIR | ||
# Check we can install from the tarball. | ||
# This emulates installing from npm and ensures that: | ||
# 1. we publish the right files in the `files` list from package.json | ||
# 2. The install script works and correctly exits without errors | ||
# 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/ | ||
|
||
# Again for Firefox | ||
echo "Testing... Chrome ES Modules" | ||
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(); | ||
})(); | ||
" | ||
|
||
echo "Testing... Chrome Webpack ES Modules" | ||
TMPDIR="$(mktemp -d)" | ||
cd $TMPDIR | ||
echo '{"type": "module"}' >>$TMPDIR/package.json | ||
npm install --loglevel silent "${tarball}" | ||
npm install -D --loglevel silent webpack [email protected] | ||
echo 'export default { | ||
mode: "production", | ||
entry: "./index.js", | ||
target: "node", | ||
output: { | ||
filename: "bundle.cjs", | ||
}, | ||
};' >>$TMPDIR/webpack.config.js | ||
echo " | ||
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(); | ||
})(); | ||
" >>$TMPDIR/index.js | ||
npx webpack | ||
cp -r node_modules/puppeteer/.local-chromium . | ||
rm -rf node_modules | ||
node dist/bundle.cjs | ||
|
||
echo "Testing... Firefox CommonJS" | ||
TMPDIR="$(mktemp -d)" | ||
cd $TMPDIR | ||
PUPPETEER_PRODUCT=firefox npm install --loglevel silent "${tarball}" | ||
node --eval="require('puppeteer')" | ||
rm "${tarball}" | ||
node --eval="require('puppeteer/lib/cjs/puppeteer/revisions.js');" | ||
ls $TMPDIR/node_modules/puppeteer/.local-firefox/linux-*/firefox/firefox | ||
|
||
# Again for puppeteer-core | ||
echo "Testing... Firefox ES Modules" | ||
TMPDIR="$(mktemp -d)" | ||
cd $TMPDIR | ||
echo '{"type":"module"}' >>$TMPDIR/package.json | ||
PUPPETEER_PRODUCT=firefox 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';" | ||
ls $TMPDIR/node_modules/puppeteer/.local-firefox/linux-*/firefox/firefox | ||
|
||
## Puppeteer Core tests | ||
|
||
echo "Setting up Puppeteer Core tests..." | ||
cd $ROOTDIR | ||
rm "${tarball}" | ||
node ./utils/prepare_puppeteer_core.js | ||
npm pack | ||
tarball="$(realpath puppeteer-core-*.tgz)" | ||
|
||
echo "Testing... Puppeteer Core CommonJS" | ||
TMPDIR="$(mktemp -d)" | ||
cd $TMPDIR | ||
# Check we can install from the tarball. | ||
# This emulates installing from npm and ensures that: | ||
# 1. we publish the right files in the `files` list from package.json | ||
# 2. The install script works and correctly exits without errors | ||
# 3. Requiring Puppeteer Core from Node works. | ||
npm install --loglevel silent "${tarball}" | ||
node --eval="require('puppeteer-core')" | ||
node --eval="require('puppeteer-core/lib/cjs/puppeteer/revisions.js');" | ||
|
||
echo "Testing... Puppeteer Core ES Modules" | ||
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'" | ||
node --input-type="module" --eval="import 'puppeteer-core/lib/esm/puppeteer/revisions.js';" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare const puppeteerDirname: string; | ||
|
||
export { puppeteerDirname }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { dirname } from 'path'; | ||
import { puppeteerDirname } from './compat.js'; | ||
|
||
export const rootDirname = dirname(dirname(dirname(puppeteerDirname))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Generated Artifacts | ||
|
||
**Do not edit manually edit any TypeScript files in this folder** All TS files are generated from their respectively named template file (ext. `tmpl`) in the `templates` directory. Edit them there is needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const packageVersion = '13.7.0'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const packageVersion = 'PACKAGE_VERSION'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const { writeFileSync, readFileSync } = require('fs'); | ||
const { join } = require('path'); | ||
|
||
writeFileSync( | ||
join(__dirname, '../src/generated/version.ts'), | ||
readFileSync(join(__dirname, '../src/templates/version.ts.tmpl'), { | ||
encoding: 'utf-8', | ||
}).replace('PACKAGE_VERSION', require('../package.json').version) | ||
); |
Oops, something went wrong.