Skip to content

Commit

Permalink
feat: Add Hook named export (#88)
Browse files Browse the repository at this point in the history
`import-in-the-middle` has some ESM interop issues.

From
[here](open-telemetry/opentelemetry-js#4546 (comment)):

> 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](open-telemetry/opentelemetry-js#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.
  • Loading branch information
timfish committed May 31, 2024
1 parent bef32f2 commit fb0e393
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ Hook.prototype.unhook = function () {
}

module.exports = Hook
module.exports.Hook = Hook
module.exports.addHook = addHook
module.exports.removeHook = removeHook
2 changes: 1 addition & 1 deletion test/hook/dynamic-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion test/hook/static-import-default.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit fb0e393

Please sign in to comment.