switchery icon indicating copy to clipboard operation
switchery copied to clipboard

Switchery error in webpack

Open weiyuq opened this issue 8 years ago • 18 comments

Hi, thanks for this awesome plugin. Switchery error in webpack, Initialization times wrong error: Cannot set property 'trackingClick' of undefined(…)

weiyuq avatar Dec 29 '16 07:12 weiyuq

Are you sure that's not something on your side? There's no such method in this library.

If you're certain it's a problem with Switchery though, please provide detailed steps of reproducing the issue and we'll take a look at it. Thanks!

abpetkov avatar Dec 29 '16 07:12 abpetkov

__xwv e pn2eku b e s6 n

 require("switchery/dist/switchery.css");
  var Switchery = require("switchery/dist/switchery");

var elem = document.querySelector(".js-switch");
      var switchery = new Switchery(elem,{size:"small"});

I use webpack import the Switchery and initialize a new Swithery object, the exception above throwed.

weiyuq avatar Dec 29 '16 07:12 weiyuq

Seeing the same issue. Somehow this is undefined in the FastClick class

thomask avatar Jan 15 '17 11:01 thomask

I had this issue too, I came up with this solution using the string-replace-loader:

module.exports = {
  // ...
  module: {
    loaders: [
      {
        test: /switchery\.js$/,
        loader: 'string-replace',
        query: {
          search: 'function FastClick(layer) {',
          replace: 'function FastClick(layer) {_fastClick.call(window,layer);};function _fastClick(layer){'
        }
      }
    ]
  }
}

This will make the FastClick function to have the window context (this = window).

An alternative solution would be to use the imports-loader to disable the module, exports and define variables within the Switchery module definition, this would set Switchery as a global and no need to use Switchery = require('switchery'); (maybe not what we want, hence the first solution).

{
          test: /switchery\.js$/,
          loader: "imports-loader?module=>false,exports=>false,define=>false,this=>window"
}

Note: I only tested the first solution.

kevinkl3 avatar Mar 14 '17 06:03 kevinkl3

webpack.config.js

module: {
    noParse: /switchery/
}

That works for me.

zhzz avatar Mar 16 '17 01:03 zhzz

thanks I'll try it

weiyuq avatar Mar 16 '17 01:03 weiyuq

Why was this closed? I'm getting this issue and it's still there. Was anything fixed in the project?

tmaly1980 avatar Jun 01 '17 14:06 tmaly1980

I am sorry, I thought it was resolved. and my project was later used in other ways

weiyuq avatar Jun 02 '17 02:06 weiyuq

also have this error, please fix it

shlensky avatar Aug 09 '17 20:08 shlensky

got same issue. Is there any good way to solve it ?

sss63232 avatar Aug 25 '17 09:08 sss63232

if someone want to use it with React - I made a version rewritten for React https://github.com/shlensky/sweetcherry

shlensky avatar Aug 25 '17 09:08 shlensky

@shlensky It looks nice!

But I'm not using React, although I use webpack to bundle.

Could you give me any direction about solving the "error: Cannot set property 'trackingClick' of undefined" ?

sss63232 avatar Aug 25 '17 09:08 sss63232

@sss63232 no 😞

shlensky avatar Aug 25 '17 10:08 shlensky

Same issue here. Will fallback full css. Shame, it was nice. One example among others using css: https://codepen.io/artyom-ivanov/pen/ggQaLV

dodubassman avatar Dec 05 '17 07:12 dodubassman

Using script-loader with Webpack 4 works too:

import "script-loader!switchery/dist/switchery.js";

bchavez avatar Apr 25 '18 01:04 bchavez

webpack.config.js

module: {
    noParse: /switchery/
}

That works for me.

Good job. that works for me :)

hatbilweb avatar May 03 '19 18:05 hatbilweb

webpack.config.js

module: {
    noParse: /switchery/
}

That works for me.

Thanks for the solution, but note that this won't work if you import Switchery CSS with webpack. You will have many errors: image

To prevent this issue, you can use the following configuration which will only ignore Switchery JS:

module: {
    noParse: /switchery\/dist\/switchery(min.)?\.js/
}

Kocal avatar Jan 06 '21 13:01 Kocal

That works for me("webpack": "^5.35.1", "string-replace-loader": "^3.0.1"):

module.exports = {
  module: {
    rules: [
      {
        test: /switchery\.js$/,
        loader: 'string-replace-loader',
        options: {
           search: 'function FastClick(layer) {',
           replace: 'function FastClick(layer) {_fastClick.call(window,layer);};function _fastClick(layer){',
        },
      },
    ]
  }

d-ganchar avatar Apr 26 '21 12:04 d-ganchar