progressively icon indicating copy to clipboard operation
progressively copied to clipboard

error when using with webpack

Open dev-seahouse opened this issue 8 years ago • 9 comments

i got the following error in dev tool and yet it is still working.

app.js?id=06c12ce…:561 Uncaught TypeError: e.removeEventListener is not a function
    at Object.u.drop (app.js?id=06c12ce…:561)
    at Object.u.check (app.js?id=06c12ce…:561)
    at Object.u.render (app.js?id=06c12ce…:561)
    at Object.u.init (app.js?id=06c12ce…:561)
    at Facilis er...:261

What i did:

window.progressively= require(progressively);
progressively.init();

dev-seahouse avatar Jun 22 '17 10:06 dev-seahouse

;
(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(function () {
     root = window; // <========= this fixed it, the problem seem to be 'root' is not window object for some reason
      return factory(root)
    })
  } else if (typeof exports === 'object') {
    module.exports = factory
  } else {
    root.progressively = factory(root)
  }
})(this, function (root) {
  'use strict'

dev-seahouse avatar Jun 22 '17 12:06 dev-seahouse

This seems to be happening because root is not defined when the factory function is exported. I'll find a workaround soon and post it in this thread. Till then, you could use webpack's external configuration. Hope that will solve your problem.

thinker3197 avatar Jun 22 '17 17:06 thinker3197

@thinker3197 Did you find any solution? I kind of running in the same issue.

the94air avatar Oct 21 '17 12:10 the94air

I tried out a few other plugins and this is a common issue when using js/jquery libraries with webpack. This happens, as I stated above because webpack wraps the package in it's own object while bundling thus loosing the access to window object. You can use the above mentioned solution if that helps!

thinker3197 avatar Oct 22 '17 17:10 thinker3197

Then.. I guess I will use the CDN version for now. Thanks.

the94air avatar Oct 22 '17 17:10 the94air

@dev-seahouse

In my environments, this would work. (webpack 3)

in webpack.config.js:

module: {
  rules: [
      ...
      {
        test: /progressively.*\.js$/,
        loader: 'imports-loader?this=>window',
      },
   ],
  },

in js:

import progressively from 'progressively';
progressively.init();

koishimasato avatar Jan 30 '18 07:01 koishimasato

Thanks @koishimasato for sharing the solution. I'll try it out later.

the94air avatar Jan 30 '18 17:01 the94air

@koishimasato solution doesn't work with webpack 4. Any ideas?

cgpro avatar Nov 18 '18 04:11 cgpro

;
(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(function () {
     root = window; // <========= this fixed it, the problem seem to be 'root' is not window object for some reason
      return factory(root)
    })
  } else if (typeof exports === 'object') {
    module.exports = factory
  } else {
    root.progressively = factory(root)
  }
})(this, function (root) {
  'use strict'

Yep , this works 👍

import progressively from 'progressively/src/progressively';

LockeAG avatar Dec 25 '18 20:12 LockeAG