From 689028fea03639142f0757132474f58c3f243cc1 Mon Sep 17 00:00:00 2001 From: Khiet Tam Nguyen Date: Fri, 20 Oct 2023 20:33:46 +1100 Subject: [PATCH] 2.0.3 - exposing ESMOptions, refactor to use newModule._extensions --- package-lock.json | 4 ++-- package.json | 2 +- src/import.ts | 16 ++++++++-------- src/index.ts | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index f26c90a..6222d5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "import-sync", - "version": "2.0.2", + "version": "2.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "import-sync", - "version": "2.0.2", + "version": "2.0.3", "license": "MIT", "dependencies": { "esm": "^3.2.25" diff --git a/package.json b/package.json index f221384..d30a2f9 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type": "git", "url": "https://github.com/nktnet1/import-sync" }, - "version": "2.0.2", + "version": "2.0.3", "files": [ "dist" ], diff --git a/src/import.ts b/src/import.ts index 89880cf..e7ffe0c 100644 --- a/src/import.ts +++ b/src/import.ts @@ -11,15 +11,7 @@ import { getCallerFilePath } from './files'; */ /* istanbul ignore next */ const createEs6Require = (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; /** @@ -31,6 +23,14 @@ const createEs6Require = (esmOptions: ESMOptions) => { const newRequest = request.startsWith('node:') ? request.substring(5) : request; return oldResolveFilename.call(this, newRequest, parent, isMain); }; + /** + * Removes the error for "[ERR_REQUIRE_ESM]: require() of ES Module", as per + * - https://github.com/standard-things/esm/issues/855#issuecomment-657982788 + */ + newModule._extensions['.js'] = (m: any, filename: string) => { + m._compile(fs.readFileSync(filename, 'utf-8'), filename); + }; + return loader; }; diff --git a/src/index.ts b/src/index.ts index 8529ff4..a37079e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import importSync from './import'; export default importSync; -export type { Options } from './types'; +export type { Options, ESMOptions } from './types'; module.exports = importSync; module.exports.default = importSync;