plasticine / inject-loader

💉📦 A Webpack loader for injecting code into modules via their dependencies.
https://www.npmjs.com/package/inject-loader
MIT License
482 stars 47 forks source link

Regression: When injecting a falsey export, it shouldn't fall back to real module #47

Closed Aaronius closed 6 years ago

Aaronius commented 6 years ago

Regression of #15.

In one of my tests, I wish to inject an falsey export. That is, I want to see what module A does when it requires module B and module B exports undefined, null, or false. Unfortunately, inject-loader outputs something like this:

...
      function __getInjection(dependency) {
        return __injections.hasOwnProperty(dependency) ? __injections[dependency] : null;
      }

      (function () {
        var mcidInstance = __getInjection("../sharedModules/mcidInstance") || __webpack_require__(4); /**
...

So even though __getInjection returns null, || __webpack_require__(4); overrides it anyway.

plasticine commented 6 years ago

@Aaronius thanks for following up on this, and apologies for the regression! :)

Aaronius commented 6 years ago

I started on this but I can't get tests to run on master:

aahardy@aahardy-osx ~/d/inject-loader> npm run test

> inject-loader@3.0.1 test /Users/aahardy/dev/inject-loader
> mocha tmp/testBundle.js --require source-map-support/register

/Users/aahardy/dev/inject-loader/tmp/webpack:/__tests__/tests.js:14
  const injectors = [
^
Error: Cannot find module "self!./modules/commonjs.js"
    at webpackMissingModule (/Users/aahardy/dev/inject-loader/tmp/webpack:/__tests__/tests.js:14:1)
    at Suite.<anonymous> (/Users/aahardy/dev/inject-loader/tmp/webpack:/__tests__/tests.js:14:1)
    at Object.create (/Users/aahardy/dev/inject-loader/node_modules/mocha/lib/interfaces/common.js:114:19)
    at context.describe.context.context (/Users/aahardy/dev/inject-loader/node_modules/mocha/lib/interfaces/bdd.js:44:27)
    at Object.<anonymous> (/Users/aahardy/dev/inject-loader/tmp/webpack:/__tests__/tests.js:13:1)
    at __webpack_require__ (/Users/aahardy/dev/inject-loader/tmp/webpack:/webpack/bootstrap 59bd648262060a4b5976:19:1)
    at Object.<anonymous> (/Users/aahardy/dev/inject-loader/tmp/testBundle.js:70:18)
    at __webpack_require__ (/Users/aahardy/dev/inject-loader/tmp/webpack:/webpack/bootstrap 59bd648262060a4b5976:19:1)
    at /Users/aahardy/dev/inject-loader/tmp/webpack:/webpack/bootstrap 59bd648262060a4b5976:62:1
    at Object.<anonymous> (/Users/aahardy/dev/inject-loader/tmp/testBundle.js:66:10)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at /Users/aahardy/dev/inject-loader/node_modules/mocha/lib/mocha.js:231:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/aahardy/dev/inject-loader/node_modules/mocha/lib/mocha.js:228:14)
    at Mocha.run (/Users/aahardy/dev/inject-loader/node_modules/mocha/lib/mocha.js:514:10)
    at Object.<anonymous> (/Users/aahardy/dev/inject-loader/node_modules/mocha/bin/_mocha:480:18)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

I haven't dug into it deeply, but if you know what's up, it would help me out.

Also, the strategy I was going to pursue was changing the output from this:

...
      function __getInjection(dependency) {
        return __injections.hasOwnProperty(dependency) ? __injections[dependency] : null;
      }

      (function () {
        var mcidInstance = __getInjection("../sharedModules/mcidInstance") || __webpack_require__(4); /**
...

To something like this:

...
      (function () {
        var mcidInstance = __injections.hasOwnProperty("../sharedModules/mcidInstance") ? __injections["../sharedModules/mcidInstance"] : __webpack_require__(4); /**
...

If you have feedback on that strategy, that would also help. Thanks!

Aaronius commented 6 years ago

Looks like I had to run npm run build-test before npm run test or something. Anyway, tests are running now. Thanks.