plasticine / inject-loader

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

support babel's import syntax #6

Closed tomatau closed 9 years ago

tomatau commented 9 years ago

Trying to work with a webpack bundle using babel to compile ES6 files and work with:

import DefaultExport from './File';

The inject loader doesn't work and ends up with:

TypeError: Cannot read property 'default' of undefined
    at inject (webpack:///./File.es6?./~/inject-loader:5:91)
    at Context.eval (webpack:///./test/File.es6?:19:17)
thomasklein commented 9 years ago

+1

kpdecker commented 9 years ago

What version of babel is this?

It's working fine for me under this version:

├─┬ babel-loader@4.3.0
│ ├─┬ babel-core@4.7.16
│ │ ├── acorn-babel@0.11.1-38
│ │ ├─┬ regenerator-babel@0.8.13-2

The code that is generated looks something like this:

var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

var EventEmitter = injections["events"].EventEmitter;
var Reflux = _interopRequire(injections["reflux"]);
var AuthActions = _interopRequire(injections["actions/authentication"]);
plasticine commented 9 years ago

I would like to actually add some test coverage around this and make sure it is working / ensure that it stay working.

tomatau commented 9 years ago

babel: ^5.0.0

The 5.0 introduces many big improvements but changes many of it's internals since 4.x around the module system.

kpdecker commented 9 years ago

This works for me under 5.0, but I am running with #7 merged (which appears to have been released under inject-loader 2.0.0).

    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
    var _reflux = (injections['reflux'] || __webpack_require__(116));
    var _reflux2 = _interopRequireDefault(_reflux);
...
        _reflux2['default']

My first guess, without seeing a repo case, is that the injection was missing, which causes the eventual .default reference to be undefined. I'm not seeing it locally as I have the failover from #7 in place. This is just a guess though.

@plasticine glad to try to PR some tests for this if I can make some time. It may be a few weeks, unfortunately.

kpdecker commented 9 years ago

I also had to do some hacks around the __esModule flag, which isn't really related to inject-loader persay, but we might be able to find a good utility or similar to help make that a bit more transparent.

nkbt commented 9 years ago

For me it works well, see example in https://github.com/in-flux/component-router/blob/master/test/Store-test.js and other tests in the repo.

Though there seems to be an issue when module exports more then one thing. Injector simply returns empty object in that case.

See example https://github.com/chenglou/react-motion/blob/f43b9365c3552d38d144358101b1496c7f4c716a/test/Spring-test.js

Not sure if it is helpful. I wanted to debug it later, maybe get some positive results

nkbt commented 9 years ago

Made a separate build:

/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {

module.exports = function inject(injections) {
var module = {exports: {}};
//import React, {PropTypes} from 'react';
'use strict';

exports.__esModule = true;
exports.updateCurrValue = updateCurrValue;
exports.updateCurrVelocity = updateCurrVelocity;

// ....

exports.TransitionSpring = TransitionSpring;
return module.exports;
}

So module.exports is never filled. Only exports. But injector returns module.exports.

If module exports default, then everything works fine:

/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {

module.exports = function inject(injections) {
var module = {exports: {}};
'use strict';

exports.__esModule = true;
exports['default'] = noVelocity;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _lodashIsplainobject = (injections['lodash.isplainobject'] || __webpack_require__(3));

var _lodashIsplainobject2 = _interopRequireDefault(_lodashIsplainobject);

function noVelocity(coll) {
  if (Array.isArray(coll)) {
    return coll.every(noVelocity);
  }
  if (_lodashIsplainobject2['default'](coll)) {
    return Object.keys(coll).every(function (key) {
      return key === 'config' ? true : noVelocity(coll[key]);
    });
  }
  return typeof coll === 'number' ? coll === 0 : true;
}

module.exports = exports['default'];
return module.exports;
}
rpominov commented 9 years ago

Same issue here. When a module exports default and only default it works fine. But if it also exports something else, the result of injector(injections) is an empty object.

When I was trying to find the issue, I saw the same generated code as @nkbt posted. At the top it creates module variable, and returns module.exports at the bottom, but in the middle exports variable is used, which is an argument of upper function function(module, exports, __webpack_require__) {.

Seems like in case of importing default it works just by a coincidence.

Perhaps the code at the top of injector should be:

var module = {exports: {}};
var exports = module.exports;
rpominov commented 9 years ago

Ah, I see it already fixed by #11. Cool, waiting for release and install from github in meantime :+1:

plasticine commented 9 years ago

@rpominov Hey there, yep—hopefully fixed in #11. I’m back at a computer as of tomorrow, so I’ll try and verify everything is A-OK and ship a release then. :)

rpominov commented 9 years ago

Great, thank you! Btw, I just installed from github and it works fine for us :ok_hand:

plasticine commented 9 years ago

OK, I’ve published 2.0.1, which should hopefully fix the issues with Babel generated code. Please have a look and open a new issue if there are dramas. :heart:

andyperlitch commented 7 years ago

FYI, this appears to be broken in 3.0.0-beta2. We downgraded to 2.0.1 and it worked.