Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(agnostic): ship CJS and ESM builds #6095

Merged
merged 5 commits into from Jun 25, 2020
Merged

Conversation

jackfranklin
Copy link
Collaborator

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.

@jackfranklin
Copy link
Collaborator Author

@TimvdLippe PTAL also - you have more TS config experience than me so I'm open to different ways of solving this!

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TSConfig looks fine. Haven't reviewed the other stuff.

@@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work in the browser as well as node?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This is on my list to figure out. The old solution wouldn't have worked either.

I think it only needs to use the package.json in order to know which browser revision to download which is not needed in the browser; the work here is to try to prise that code out into a node specific function.

@jackfranklin jackfranklin force-pushed the publish-esm-build branch 2 times, most recently from 98f9c8a to b606c24 Compare June 25, 2020 09:17
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants