Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switchery error in webpack #120

Open
weiyuq opened this issue Dec 29, 2016 · 18 comments
Open

Switchery error in webpack #120

weiyuq opened this issue Dec 29, 2016 · 18 comments

Comments

@weiyuq
Copy link

weiyuq commented Dec 29, 2016

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

@abpetkov
Copy link
Owner

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!

@weiyuq
Copy link
Author

weiyuq commented Dec 29, 2016

__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.

@thomask
Copy link

thomask commented Jan 15, 2017

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

@kevinkl3
Copy link

kevinkl3 commented Mar 14, 2017

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.

@zhzz
Copy link

zhzz commented Mar 16, 2017

webpack.config.js

module: {
    noParse: /switchery/
}

That works for me.

@weiyuq
Copy link
Author

weiyuq commented Mar 16, 2017

thanks I'll try it

@weiyuq weiyuq closed this as completed Mar 16, 2017
@tmaly1980
Copy link

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

@weiyuq
Copy link
Author

weiyuq commented Jun 2, 2017

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

@weiyuq weiyuq reopened this Jun 2, 2017
@shlensky
Copy link

shlensky commented Aug 9, 2017

also have this error, please fix it

@sss63232
Copy link

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

@shlensky
Copy link

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

@sss63232
Copy link

sss63232 commented Aug 25, 2017

@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" ?

@shlensky
Copy link

@sss63232 no 😞

@dodubassman
Copy link

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

@bchavez
Copy link

bchavez commented Apr 25, 2018

Using script-loader with Webpack 4 works too:

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

@hatbilweb
Copy link

webpack.config.js

module: {
    noParse: /switchery/
}

That works for me.

Good job. that works for me :)

@Kocal
Copy link

Kocal commented Jan 6, 2021

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/
}

@d-ganchar
Copy link

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){',
        },
      },
    ]
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests