switchery
switchery copied to clipboard
Switchery error in webpack
Hi, thanks for this awesome plugin. Switchery error in webpack, Initialization times wrong error: Cannot set property 'trackingClick' of undefined(…)
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!
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.
Seeing the same issue. Somehow this
is undefined in the FastClick class
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.
webpack.config.js
module: {
noParse: /switchery/
}
That works for me.
thanks I'll try it
Why was this closed? I'm getting this issue and it's still there. Was anything fixed in the project?
I am sorry, I thought it was resolved. and my project was later used in other ways
also have this error, please fix it
got same issue. Is there any good way to solve it ?
if someone want to use it with React - I made a version rewritten for React https://github.com/shlensky/sweetcherry
@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 no 😞
Same issue here. Will fallback full css. Shame, it was nice. One example among others using css: https://codepen.io/artyom-ivanov/pen/ggQaLV
webpack.config.js
module: { noParse: /switchery/ }
That works for me.
Good job. that works for me :)
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:
To prevent this issue, you can use the following configuration which will only ignore Switchery JS:
module: {
noParse: /switchery\/dist\/switchery(min.)?\.js/
}
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){',
},
},
]
}