From 240c8bbadb40a4eb20479f4f630ab61c485d47aa Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Fri, 15 Dec 2023 17:34:02 +0100 Subject: [PATCH 1/2] Migrate to @httptoolkit/esm --- package-lock.json | 18 +++++++++--------- package.json | 2 +- src/httptoolkit-esm-types.d.ts | 4 ++++ src/import.ts | 3 ++- src/types.ts | 2 +- 5 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 src/httptoolkit-esm-types.d.ts diff --git a/package-lock.json b/package-lock.json index f3082db..e361f8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "2.1.2", "license": "MIT", "dependencies": { - "esm": "^3.2.25" + "@httptoolkit/esm": "^3.3.0" }, "devDependencies": { "@types/esm": "^3.2.2", @@ -775,6 +775,14 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, + "node_modules/@httptoolkit/esm": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/@httptoolkit/esm/-/esm-3.3.0.tgz", + "integrity": "sha512-gS+TH14750cveYP5p2q/oLyjVV5+qRFZMOpsPzK1KrgacqKc6RLDmt3ioGaMjsn1aianQbROkl4QXDJmO5swPA==", + "engines": { + "node": ">=6" + } + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.13", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.13.tgz", @@ -2564,14 +2572,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/esm": { - "version": "3.2.25", - "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", - "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==", - "engines": { - "node": ">=6" - } - }, "node_modules/espree": { "version": "9.6.1", "resolved": "https://registry.npmjs.org/espree/-/espree-9.6.1.tgz", diff --git a/package.json b/package.json index ab18fd0..3ab4b84 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,6 @@ "typescript": "^5.3.3" }, "dependencies": { - "esm": "^3.2.25" + "@httptoolkit/esm": "^3.3.0" } } diff --git a/src/httptoolkit-esm-types.d.ts b/src/httptoolkit-esm-types.d.ts new file mode 100644 index 0000000..50b07ff --- /dev/null +++ b/src/httptoolkit-esm-types.d.ts @@ -0,0 +1,4 @@ +declare module '@httptoolkit/esm' { + import * as Esm from 'esm'; + export = Esm; +} diff --git a/src/import.ts b/src/import.ts index c709c9a..58ae958 100644 --- a/src/import.ts +++ b/src/import.ts @@ -1,5 +1,6 @@ -import esm from 'esm'; +import esm from '@httptoolkit/esm'; import fs from 'fs'; + import { ESMOptions, Options } from './types'; import { findModuleFile, getCallerDirname } from './files'; diff --git a/src/types.ts b/src/types.ts index 99c8398..1de298e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import esm from 'esm'; +import esm from '@httptoolkit/esm'; export type ESMOptions = Parameters[1]; From 1ee105d75ea3ed33fc51767657f7324c60bb940d Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Fri, 15 Dec 2023 17:40:01 +0100 Subject: [PATCH 2/2] Remove fixes now included directly within @httptoolkit/esm --- src/import.ts | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/import.ts b/src/import.ts index 58ae958..373a5fe 100644 --- a/src/import.ts +++ b/src/import.ts @@ -1,5 +1,4 @@ import esm from '@httptoolkit/esm'; -import fs from 'fs'; import { ESMOptions, Options } from './types'; import { findModuleFile, getCallerDirname } from './files'; @@ -11,27 +10,7 @@ import { findModuleFile, getCallerDirname } from './files'; */ /* istanbul ignore next */ const createEsmRequire = (esmOptions: ESMOptions) => { - /** - * Removes the error for "[ERR_REQUIRE_ESM]: require() of ES Module", as per - * - https://github.com/standard-things/esm/issues/855#issuecomment-657982788 - */ - require('module').Module._extensions['.js'] = (m: any, filename: string) => { - m._compile(fs.readFileSync(filename, 'utf-8'), filename); - }; - const loader = esm(module, esmOptions); - - const newModule = module.constructor.length > 1 ? module.constructor : loader('module'); - const oldResolveFilename = newModule._resolveFilename; - /** - * Referencing - * - https://github.com/kenotron/esm-jest/commit/624b9524ee698f5cbd16ee2481dc4cd0dec52e42 - * - https://github.com/standard-things/esm/issues/331#issuecomment-377056717 - */ - newModule._resolveFilename = function(request: string, parent: any, isMain: boolean) { - const newRequest = request.startsWith('node:') ? request.substring(5) : request; - return oldResolveFilename.call(this, newRequest, parent, isMain); - }; - return loader; + return esm(module, esmOptions); }; /**