diff options
Diffstat (limited to '')
-rw-r--r-- | remote/test/puppeteer/compat/README.md | 16 | ||||
-rw-r--r-- | remote/test/puppeteer/compat/cjs/compat.ts | 19 | ||||
-rw-r--r-- | remote/test/puppeteer/compat/cjs/tsconfig.json | 8 | ||||
-rw-r--r-- | remote/test/puppeteer/compat/esm/compat.ts | 22 | ||||
-rw-r--r-- | remote/test/puppeteer/compat/esm/tsconfig.json | 8 |
5 files changed, 73 insertions, 0 deletions
diff --git a/remote/test/puppeteer/compat/README.md b/remote/test/puppeteer/compat/README.md new file mode 100644 index 0000000000..a72ecab4e8 --- /dev/null +++ b/remote/test/puppeteer/compat/README.md @@ -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. diff --git a/remote/test/puppeteer/compat/cjs/compat.ts b/remote/test/puppeteer/compat/cjs/compat.ts new file mode 100644 index 0000000000..782727e00e --- /dev/null +++ b/remote/test/puppeteer/compat/cjs/compat.ts @@ -0,0 +1,19 @@ +import {dirname} from 'path'; + +/** + * @internal + */ +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}; diff --git a/remote/test/puppeteer/compat/cjs/tsconfig.json b/remote/test/puppeteer/compat/cjs/tsconfig.json new file mode 100644 index 0000000000..5429268843 --- /dev/null +++ b/remote/test/puppeteer/compat/cjs/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "composite": true, + "outDir": "../../lib/cjs/puppeteer", + "module": "CommonJS" + } +} diff --git a/remote/test/puppeteer/compat/esm/compat.ts b/remote/test/puppeteer/compat/esm/compat.ts new file mode 100644 index 0000000000..8d74765870 --- /dev/null +++ b/remote/test/puppeteer/compat/esm/compat.ts @@ -0,0 +1,22 @@ +import {createRequire} from 'module'; +import {dirname} from 'path'; +import {fileURLToPath} from 'url'; + +const require = createRequire(import.meta.url); + +/** + * @internal + */ +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}; diff --git a/remote/test/puppeteer/compat/esm/tsconfig.json b/remote/test/puppeteer/compat/esm/tsconfig.json new file mode 100644 index 0000000000..5afe459a05 --- /dev/null +++ b/remote/test/puppeteer/compat/esm/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "composite": true, + "outDir": "../../lib/esm/puppeteer", + "module": "esnext" + } +} |