Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ jobs:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm test
# Run the webpack tests for Node.js versions that webpack[-cli] support.
- run: npm run test:webpack
if: ${{ matrix.node != '8.10'
&& matrix.node != '10.0'
&& matrix.node != '10'
&& matrix.node != '12.0'
&& matrix.node != '12'
&& matrix.node != '14.0'
&& matrix.node != '14' }}
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/
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ 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.
// Webpack replaces `require` with its own fn that is insufficient for how
// require is used in this package. It is strongly hoped no other
// bundler-specific hacks are needed.
/* 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 +72,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 +84,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 +281,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
7 changes: 5 additions & 2 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: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 # requires node 16+"
},
"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