From 5d89f8eb73a97d50fbd81b162b7f09feac3d2946 Mon Sep 17 00:00:00 2001 From: wpessers Date: Mon, 17 Mar 2025 22:13:12 +0100 Subject: [PATCH 1/7] feat: add failing webpack test --- .gitignore | 1 + package.json | 7 +++++-- test/webpack/test.js | 22 ++++++++++++++++++++++ test/webpack/webpack.config.js | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 test/webpack/test.js create mode 100644 test/webpack/webpack.config.js diff --git a/.gitignore b/.gitignore index ffff996..16e5df5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /tmp /require-in-the-middle-*.tgz *.example.js +test/webpack/dist/ diff --git a/package.json b/package.json index fe7cf14..ee34d2d 100644 --- a/package.json +++ b/package.json @@ -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" }, "repository": { "type": "git", diff --git a/test/webpack/test.js b/test/webpack/test.js new file mode 100644 index 0000000..7899250 --- /dev/null +++ b/test/webpack/test.js @@ -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() diff --git a/test/webpack/webpack.config.js b/test/webpack/webpack.config.js new file mode 100644 index 0000000..7c67eed --- /dev/null +++ b/test/webpack/webpack.config.js @@ -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' + } +} From de99223752f0caf468ea685e640f046022ca67c5 Mon Sep 17 00:00:00 2001 From: wpessers Date: Thu, 19 Mar 2026 23:43:13 +0100 Subject: [PATCH 2/7] feat: explicitly use native require when bundled with webpack --- index.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index d3d49f9..bcc834a 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,11 @@ const Module = require('module') const debug = require('debug')('require-in-the-middle') const moduleDetailsFromPath = require('module-details-from-path') +/* 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') @@ -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 @@ -76,7 +81,7 @@ 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]) } } @@ -84,8 +89,8 @@ class ExportsCache { 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) @@ -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) From 87ae6da4042743ca6d46db1a264c90bf9c02c54a Mon Sep 17 00:00:00 2001 From: wpessers Date: Thu, 19 Mar 2026 23:45:37 +0100 Subject: [PATCH 3/7] chore: add test:webpack script run command to main test script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ee34d2d..61cc176 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "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", From d149a0b36db9b6e8a3f90c7f506e5809601a08cf Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 31 Mar 2026 15:56:16 -0700 Subject: [PATCH 4/7] grumpy comment; only run test:webpack in CI on recent-enough Node.js versions The npm is node 12.0 is not sufficient to install a working webpack-cli. --- .github/workflows/test.yml | 3 +++ index.js | 3 +++ package.json | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c8e3209..c3c4eb7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,3 +20,6 @@ jobs: node-version: ${{ matrix.node }} - run: npm install - run: npm test + # Run the webpack tests for Node.js versions that suppport the webpack version being used. + - run: npm run test:webpack + if: ${{ matrix.node != '8.10' && matrix.node != '10.0' && matrix.node != '12.0' }} diff --git a/index.js b/index.js index bcc834a..13c3e19 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,9 @@ const Module = require('module') const debug = require('debug')('require-in-the-middle') const moduleDetailsFromPath = require('module-details-from-path') +// 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 diff --git a/package.json b/package.json index 61cc176..72c7027 100644 --- a/package.json +++ b/package.json @@ -23,11 +23,11 @@ "webpack-cli": "^5.1.0" }, "scripts": { - "test": "npm run test:lint && npm run test:tape && npm run test:babel && npm run test:webpack", + "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:webpack": "webpack --config test/webpack/webpack.config.js && node test/webpack/dist/bundle.js" + "test:webpack": "webpack --config test/webpack/webpack.config.js && node test/webpack/dist/bundle.js # requires Node.js >=10.8, >=12.?" }, "repository": { "type": "git", From 4b8c6f208eb59ec0028e5877cda4b1b6d54fe5a4 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 31 Mar 2026 15:58:10 -0700 Subject: [PATCH 5/7] node 14.0 also cannot install a working webpack-cli Error (from CI run) is: > webpack --config test/webpack/webpack.config.js && node test/webpack/dist/bundle.js # requires Node.js >=10.8, >=12.? [webpack-cli] Error: Cannot find module 'ajv/dist/compile/codegen' Require stack: - /home/runner/work/require-in-the-middle/require-in-the-middle/node_modules/ajv-keywords/dist/definitions/typeof.js - /home/runner/work/require-in-the-middle/require-in-the-middle/node_modules/ajv-keywords/dist/keywords/typeof.js ... --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c3c4eb7..30691bc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,4 +22,4 @@ jobs: - run: npm test # Run the webpack tests for Node.js versions that suppport the webpack version being used. - run: npm run test:webpack - if: ${{ matrix.node != '8.10' && matrix.node != '10.0' && matrix.node != '12.0' }} + if: ${{ matrix.node != '8.10' && matrix.node != '10.0' && matrix.node != '12.0' && matrix.node != '14.0' }} From 6ab04a9817b13353377b2b5115837245fa79878f Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 31 Mar 2026 16:17:52 -0700 Subject: [PATCH 6/7] my mistake, this is about the webpack-cli min node, which is >=14.15.0 --- .github/workflows/test.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 30691bc..21d8ffd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,6 +20,11 @@ jobs: node-version: ${{ matrix.node }} - run: npm install - run: npm test - # Run the webpack tests for Node.js versions that suppport the webpack version being used. + # 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 != '12.0' && matrix.node != '14.0' }} + if: ${{ matrix.node != '8.10' + && matrix.node != '10.0' + && matrix.node != '10' + && matrix.node != '12.0' + && matrix.node != '12' + && matrix.node != '14.0' }} From 78001cdae4a6795dc193a3a2a7c12ddba51828df Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 31 Mar 2026 16:25:51 -0700 Subject: [PATCH 7/7] skip node 14 as well, 4th time's the charm --- .github/workflows/test.yml | 3 ++- package.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 21d8ffd..c55a025 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,4 +27,5 @@ jobs: && matrix.node != '10' && matrix.node != '12.0' && matrix.node != '12' - && matrix.node != '14.0' }} + && matrix.node != '14.0' + && matrix.node != '14' }} diff --git a/package.json b/package.json index 72c7027..c0569a9 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "test:lint": "standard", "test:tape": "tape test/*.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.js >=10.8, >=12.?" + "test:webpack": "webpack --config test/webpack/webpack.config.js && node test/webpack/dist/bundle.js # requires node 16+" }, "repository": { "type": "git",