Skip to content

Commit

Permalink
chore(agnostic): ship CJS and ESM builds
Browse files Browse the repository at this point in the history
For our work to enable Puppeteer in other environments (e.g. a browser)
we need to ship an ESM build. This commit changes our config to ship to
`lib/cjs` and `lib/esm` accordingly. The majority of our code stays the
same, with one small fix for the CJS build to ensure that we ship a
version that lets you `require('puppeteer')` rather than have to
`require('puppeteer').default`. We do this with the `cjs-entry.js` which
is what the `main` field in our `package.json` points to.

We also swap to `read-pkg-up` to find the `package.json` file. This is
because the folder structure of `lib/` does not match `src/` now we ship
to `cjs` and `esm`, so you cannot rely on exact paths. This module works
up from the file to find the nearest `package.json` so it will always
find Puppeteer's `package.json`.

Note that we *do not* point any users to the ESM build. We happen to
ship those files so people who know about them can get at them but it's
not expected (nor will we actively support) that people will rely on
them. The CommonJS build is considered our main build.

We may make breaking changes to the structure of the ESM build which we
will do without requiring new major versions. For example the ESM build
currently ships all files that the CJS build does, but given we are
working on the ESM build being able to run in the browser this may
change over time.

Long term once the Node versions catch up we can ditch CJS and ship
exclusively ESM but we are not there yet.
  • Loading branch information
jackfranklin committed Jun 25, 2020
1 parent f7f3f28 commit b606c24
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
29 changes: 29 additions & 0 deletions cjs-entry.js
@@ -0,0 +1,29 @@
/**
* Copyright 2020 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.
*/

/**
* We use `export default puppeteer` in `src/index.ts` to expose the library But
* TypeScript in CJS mode compiles that to `exports.default = `. This means that
* our CJS Node users would have to use `require('puppeteer').default` which
* isn't very nice.
*
* So instead we expose this file as our entry point. This requires the compiled
* Puppeteer output and re-exports the `default` export via `module.exports.`
* This means that we can publish to CJS and ESM whilst maintaining the expected
* import behaviour for CJS and ESM users.
*/
const puppeteerExport = require('./lib/cjs/index');
module.exports = puppeteerExport.default;
10 changes: 7 additions & 3 deletions package.json
Expand Up @@ -2,7 +2,7 @@
"name": "puppeteer",
"version": "4.0.0-post",
"description": "A high-level API to control headless Chrome over the DevTools Protocol",
"main": "lib/index.js",
"main": "./cjs-entry.js",
"repository": "github:puppeteer/puppeteer",
"engines": {
"node": ">=10.18.1"
Expand All @@ -28,7 +28,9 @@
"lint": "npm run eslint && npm run tsc && npm run doc",
"doc": "node utils/doclint/cli.js",
"clean-lib": "rm -rf lib",
"tsc": "npm run clean-lib && tsc --version && tsc -p . && cp src/protocol.d.ts lib/",
"tsc": "npm run clean-lib && tsc --version && npm run tsc-cjs && npm run tsc-esm",
"tsc-cjs": "tsc -p . && cp src/protocol.d.ts lib/cjs",
"tsc-esm": "tsc --build tsconfig-esm.json && cp src/protocol.d.ts lib/esm",
"apply-next-version": "node utils/apply_next_version.js",
"update-protocol-d-ts": "node utils/protocol-types-generator update",
"compare-protocol-d-ts": "node utils/protocol-types-generator compare",
Expand All @@ -41,7 +43,8 @@
"lib/",
"index.js",
"install.js",
"typescript-if-required.js"
"typescript-if-required.js",
"cjs-entry.js"
],
"author": "The Chromium Authors",
"license": "Apache-2.0",
Expand All @@ -53,6 +56,7 @@
"mitt": "^2.0.1",
"progress": "^2.0.1",
"proxy-from-env": "^1.0.0",
"read-pkg-up": "^7.0.1",
"rimraf": "^3.0.2",
"tar-fs": "^2.0.0",
"unbzip2-stream": "^1.3.3",
Expand Down
5 changes: 2 additions & 3 deletions src/common/Puppeteer.ts
Expand Up @@ -23,6 +23,7 @@ import { ProductLauncher } from '../node/Launcher';
import { BrowserFetcher, BrowserFetcherOptions } from '../node/BrowserFetcher';
import { puppeteerErrors, PuppeteerErrors } from './Errors';
import { ConnectionTransport } from './ConnectionTransport';
import readPkgUp from 'read-pkg-up';

import { devicesMap } from './DeviceDescriptors';
import { DevicesMap } from './DeviceDescriptors';
Expand Down Expand Up @@ -100,9 +101,7 @@ export class Puppeteer {
this._lazyLauncher.product !== this._productName ||
this._changedProduct
) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require('../../package.json');
const { packageJson } = readPkgUp.sync();
switch (this._productName) {
case 'firefox':
this._preferredRevision = packageJson.puppeteer.firefox_revision;
Expand Down
23 changes: 10 additions & 13 deletions src/index.ts
Expand Up @@ -14,21 +14,18 @@
* limitations under the License.
*/

import { initializePuppeteer } from './initialize';
import { initializePuppeteer, InitOptions } from './initialize';
import * as path from 'path';
import readPkgUp from 'read-pkg-up';

const packageJsonResult = readPkgUp.sync();
const packageJson = packageJsonResult.packageJson as unknown;

const rootDir = path.dirname(packageJsonResult.path);

const puppeteer = initializePuppeteer({
packageJson: require(path.join(__dirname, '..', 'package.json')),
rootDirectory: path.join(__dirname, '..'),
packageJson: packageJson as InitOptions['packageJson'],
rootDirectory: rootDir,
});

/*
* Has to be CJS here rather than ESM such that the output file ends with
* module.exports = puppeteer.
*
* If this was export default puppeteer the output would be:
* exports.default = puppeteer
* And therefore consuming via require('puppeteer') would break / require the user
* to access require('puppeteer').default;
*/
export = puppeteer;
export default puppeteer;
2 changes: 1 addition & 1 deletion src/initialize.ts
Expand Up @@ -22,7 +22,7 @@ const api = require('./api');
import { helper } from './common/helper';
import { Puppeteer } from './common/Puppeteer';

interface InitOptions {
export interface InitOptions {
packageJson: {
puppeteer: {
chromium_revision: string;
Expand Down
7 changes: 7 additions & 0 deletions tsconfig-esm.json
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./lib/esm",
"module": "ES2015",
},
}
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"outDir": "./lib",
"outDir": "./lib/cjs",
"target": "ESNext",
"moduleResolution": "node",
"module": "CommonJS",
Expand Down

0 comments on commit b606c24

Please sign in to comment.