tfoxy / chrome-promise

Promises for chrome JavaScript APIs used in extensions and apps.
MIT License
150 stars 14 forks source link

this remapped to undefined when using babel and es6 modules #15

Closed WaqasIbrahim closed 6 years ago

WaqasIbrahim commented 7 years ago

Hi, I am using babel + webpack and I used import to include chrome promise.

This is what the transpiled code looks like:

(function (root, factory) {
  if (true) {
    // AMD. Register as an anonymous module.
    !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory.bind(null, ( false ? 'undefined' : (0, _typeof3.default)(exports)) === 'object' ? this : root)),
                __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
                (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
                __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
  } else if ((typeof exports === 'undefined' ? 'undefined' : (0, _typeof3.default)(exports)) === 'object') {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like environments that support module.exports,
    // like Node.
    module.exports = factory(this);
  } else {
    // Browser globals (root is window)
    root.ChromePromise = factory(root);
  }
})(undefined, function (root) {

https://babeljs.io/faq/#why-is-this-being-remapped-to-undefined-

Is there any way to fix this without disabling strict mode?

tfoxy commented 7 years ago

I think it has something to do with the integration between webpack and amd modules. It happened to me with other packages.

You can disable amd support in webpack config and it will probably fix the problem. But it may break another dependency. So I will check if putting the commonjs definition first fixes the problem.

If only es2015 modules work, then I will create a separate esm file.

WaqasIbrahim commented 7 years ago

Hi, I just tested and it worked after disabling amd.

tfoxy commented 6 years ago

Fixed in 797b0af2e4c4094b11175180de29685dba2971bb