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:
jrandolf
2022-02-09 08:47:27 +01:00
committed by GitHub
parent a858cf7021
commit 523b487e88
15 changed files with 108 additions and 14 deletions

16
compat/README.md Normal file
View 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
View File

@@ -0,0 +1,3 @@
import { dirname } from 'path';
export const puppeteerDirname = dirname(dirname(dirname(__dirname)));

11
compat/cjs/tsconfig.json Normal file
View 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
View 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
View File

@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outDir": "../../lib/esm/puppeteer",
"module": "esnext"
},
"references": [{ "path": "../../vendor/tsconfig.esm.json" }]
}