From fb0e393529832a9e3cad123d5048c9add4b477d0 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 31 May 2024 15:07:04 +0200 Subject: [PATCH] feat: Add `Hook` named export (#88) `import-in-the-middle` has some ESM interop issues. From [here](https://github.com/open-telemetry/opentelemetry-js/pull/4546#issue-2186767832): > Specifically, namespace imports are required by the ESM spec to be objects. IITM's top-level export is not an object, but a callable function This causes [issues](https://github.com/open-telemetry/opentelemetry-js/issues/4717) with some bundlers. Obviously bundled imports will not go through `import-in-the-middle` but using it should not inhibit bundlers as iitm can still hook build-in modules. This adds a `Hook` named export and changes one cjs and esm test to use that new export. --- README.md | 2 +- index.js | 1 + test/hook/dynamic-import.js | 2 +- test/hook/static-import-default.mjs | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 14eae8f..50851f7 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ imported in ESM files, regardless of whether they're imported statically or dynamically. ```js -import Hook from 'import-in-the-middle' +import { Hook } from 'import-in-the-middle' import { foo } from 'package-i-want-to-modify' console.log(foo) // whatever that module exported diff --git a/index.js b/index.js index 50a8665..50aa04f 100644 --- a/index.js +++ b/index.js @@ -89,5 +89,6 @@ Hook.prototype.unhook = function () { } module.exports = Hook +module.exports.Hook = Hook module.exports.addHook = addHook module.exports.removeHook = removeHook diff --git a/test/hook/dynamic-import.js b/test/hook/dynamic-import.js index 1d012f1..a466a1d 100644 --- a/test/hook/dynamic-import.js +++ b/test/hook/dynamic-import.js @@ -2,7 +2,7 @@ // // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. -const Hook = require('../../index.js') +const { Hook } = require('../../index.js') const { strictEqual } = require('assert') Hook((exports, name) => { diff --git a/test/hook/static-import-default.mjs b/test/hook/static-import-default.mjs index 304224b..2c6982a 100644 --- a/test/hook/static-import-default.mjs +++ b/test/hook/static-import-default.mjs @@ -2,7 +2,7 @@ // // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. -import Hook from '../../index.js' +import { Hook } from '../../index.js' import barMjs from '../fixtures/something.mjs' import barJs from '../fixtures/something.js' import { strictEqual } from 'assert'