Skip to content

Commit

Permalink
Merge branch 'main' into mapped-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm committed Mar 20, 2024
2 parents 86e6bcf + a8dd63d commit e6fb004
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# require-in-the-middle changelog

## v7.2.1

- Fix a limitation so that a single `Hook` can hook a module and a sub-module,
e.g. `new Hook(['example', 'example/some-sub-module'], ...)`.
(https://github.com/elastic/require-in-the-middle/pull/84)


## v7.2.0

- Improve performance (possibly significantly) when using the Hook without
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SHELL=/bin/bash

# Some targets depend on https://github/trentm/json being on the PATH.
JSON ?= json
Expand Down Expand Up @@ -33,7 +34,7 @@ cutarelease: check
@which $(JSON) 2>/dev/null 1>/dev/null && \
ver=$(shell $(JSON) -f package.json version) && \
name=$(shell $(JSON) -f package.json name) && \
publishedVer=$(shell npm view -j $(shell $(JSON) -f package.json name)@$(shell $(JSON) -f package.json version) version 2>/dev/null) && \
publishedVer=$(shell npm view -j $(shell $(JSON) -f package.json name) time 2>/dev/null | $(JSON) -D/ $(shell $(JSON) -f package.json version)) && \
if [[ -n "$$publishedVer" ]]; then \
echo "error: $$name@$$ver is already published to npm"; \
exit 1; \
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "require-in-the-middle",
"version": "7.2.0",
"version": "7.2.1",
"description": "Module to hook into the Node.js require function",
"main": "index.js",
"types": "types/index.d.ts",
Expand Down
23 changes: 23 additions & 0 deletions test/sub-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,26 @@ test('require(\'sub-module/conflict/index.js\') => sub-module/conflict/index.js'
t.equal(require('./node_modules/sub-module'), 'sub-module/index.js')
t.equal(require('./node_modules/sub-module/conflict/index.js'), 'sub-module/conflict/index.js')
})

test('require(\'sub-module/foo\') with \'sub-module\'', function (t) {
t.plan(5)

const names = []
const hook = new Hook(['sub-module/foo', 'sub-module'], function (exports, name, basedir) {
names.push(name)
if (name === 'sub-module') {
t.equal(exports, 'sub-module/index.js')
} else {
t.equal(exports, 'sub-module/foo.js')
}
return name
})

t.on('end', function () {
hook.unhook()
})

t.equal(require('./node_modules/sub-module'), 'sub-module')
t.equal(require('./node_modules/sub-module/foo'), 'sub-module/foo')
t.same(names, ['sub-module', 'sub-module/foo'])
})

0 comments on commit e6fb004

Please sign in to comment.