Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/tmp
/require-in-the-middle-*.tgz
*.example.js
test/webpack/dist/
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const Module = require('module')
const debug = require('debug')('require-in-the-middle')
const moduleDetailsFromPath = require('module-details-from-path')

Comment thread
trentm marked this conversation as resolved.
/* global __non_webpack_require__ */
const nativeRequire = typeof __webpack_require__ === 'function' // eslint-disable-line camelcase
? __non_webpack_require__ // eslint-disable-line camelcase
: require

// Using the default export is discouraged, but kept for backward compatibility.
// Use this instead:
// const { Hook } = require('require-in-the-middle')
Expand Down Expand Up @@ -64,7 +69,7 @@ class ExportsCache {
if (this._localCache.has(filename)) {
return true
} else if (!isBuiltin) {
const mod = require.cache[filename]
const mod = nativeRequire.cache[filename]
return !!(mod && this._kRitmExports in mod)
} else {
return false
Expand All @@ -76,16 +81,16 @@ class ExportsCache {
if (cachedExports !== undefined) {
return cachedExports
} else if (!isBuiltin) {
const mod = require.cache[filename]
const mod = nativeRequire.cache[filename]
return (mod && mod[this._kRitmExports])
}
}

set (filename, exports, isBuiltin) {
if (isBuiltin) {
this._localCache.set(filename, exports)
} else if (filename in require.cache) {
require.cache[filename][this._kRitmExports] = exports
} else if (filename in nativeRequire.cache) {
nativeRequire.cache[filename][this._kRitmExports] = exports
} else {
debug('non-core module is unexpectedly not in require.cache: "%s"', filename)
this._localCache.set(filename, exports)
Expand Down Expand Up @@ -273,7 +278,7 @@ function Hook (modules, options, onrequire) {
// figure out if this is the main module file, or a file inside the module
let res
try {
res = require.resolve(moduleName, { paths: [basedir] })
res = nativeRequire.resolve(moduleName, { paths: [basedir] })
} catch (e) {
debug('could not resolve module: %s', moduleName)
self._cache.set(filename, exports, core)
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
"roundround": "^0.2.0",
"semver": "^6.3.0",
"standard": "^14.3.1",
"tape": "^4.11.0"
"tape": "^4.11.0",
"webpack": "^5.105.0",
"webpack-cli": "^5.1.0"
},
"scripts": {
"test": "npm run test:lint && npm run test:tape && npm run test:babel",
"test": "npm run test:lint && npm run test:tape && npm run test:babel && npm run test:webpack",
"test:lint": "standard",
"test:tape": "tape test/*.js",
"test:babel": "node test/babel/babel-register.js"
"test:babel": "node test/babel/babel-register.js",
"test:webpack": "webpack --config test/webpack/webpack.config.js && node test/webpack/dist/bundle.js"
},
"repository": {
"type": "git",
Expand Down
22 changes: 22 additions & 0 deletions test/webpack/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const assert = require('assert')

const { Hook } = require('../../')

const hooked = []

// Hook all modules as @opentelemetry/instrumentation does.
const hook = new Hook('*', function (exports, name, basedir) {
if (name === 'semver') {
hooked.push(name)
exports.patched = true
}
return exports
})

const semver = require('semver')
assert.strictEqual(semver.patched, true)
assert.deepStrictEqual(hooked, ['semver'])

hook.unhook()
16 changes: 16 additions & 0 deletions test/webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const path = require('path')

module.exports = {
target: 'node',
mode: 'production',
entry: path.join(__dirname, 'test.js'),
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
externals: {
semver: 'commonjs semver'
}
}
Loading