mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(puppeteer): export esm modules in package.json (#7964)
* feat(puppeteer): export esm modules in package.json Signed-off-by: Randolf Jung <jrandolf@chromium.org> Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com> Co-authored-by: Randolf Jung <jrandolf@chromium.org>
This commit is contained in:
16
compat/README.md
Normal file
16
compat/README.md
Normal file
@@ -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.
|
||||
3
compat/cjs/compat.ts
Normal file
3
compat/cjs/compat.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { dirname } from 'path';
|
||||
|
||||
export const puppeteerDirname = dirname(dirname(dirname(__dirname)));
|
||||
11
compat/cjs/tsconfig.json
Normal file
11
compat/cjs/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"outDir": "../../lib/cjs/puppeteer",
|
||||
"module": "CommonJS"
|
||||
},
|
||||
"references": [
|
||||
{ "path": "../../vendor/tsconfig.cjs.json"}
|
||||
]
|
||||
}
|
||||
6
compat/esm/compat.ts
Normal file
6
compat/esm/compat.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
export const puppeteerDirname = dirname(
|
||||
dirname(dirname(dirname(fileURLToPath(import.meta.url))))
|
||||
);
|
||||
9
compat/esm/tsconfig.json
Normal file
9
compat/esm/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"outDir": "../../lib/esm/puppeteer",
|
||||
"module": "esnext"
|
||||
},
|
||||
"references": [{ "path": "../../vendor/tsconfig.esm.json" }]
|
||||
}
|
||||
Reference in New Issue
Block a user