I'm really struggling with this all new JS changes to Rails. I'm trying to implement stupidly simple slider script, Glide.js
This is my app/javascripts/packs/front.js
file:
// Internal
// require("@rails/ujs")
import Glide from '@glidejs/glide'
console.log(Glide);
import Rails from '@rails/ujs'
Rails.start()
require('jquery')
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
// Custom
require('./vendor_js/vendor_import.js')
Here is a file where I do call gilder (new_slider.js):
// require("./slider.js");
require("./new_slider.js");
require("./mBox.js");
require("./lightbox_script.js");
require("./map.js");
// import lightbox from "packs/custom/lightbox_script.js";
Here is the new_slider.js
file:
$(document).on('turbolinks:load', function(){
console.log("new_slider.js is loaded");
var slider = document.querySelector('.glide');
if (slider) {
var glide = new Glide(slider);
console.log("glide is loaded:");
console.log(glide);
glide.mount();
}
});
This is my haml view:
.glide
.glide__track{"data-glide-el" => "track"}
%ul.glide__slides
%li.glide__slide
%img{:src => asset_path('slider/winter_holidays/1.jpg'), :style => "width:100%"}/
%li.glide__slide
%img{:src => asset_path('slider/winter_holidays/2.jpg'), :style => "width:100%"}/
%li.glide__slide
%img{:src => asset_path('slider/winter_holidays/3.jpg'), :style => "width:100%"}/
%div{"data-glide-el" => "controls"}
%button{"data-glide-dir" => "<<"} start
%button{"data-glide-dir" => ">>"} end
And this is my console output:
ƒ Glide$$1() {
classCallCheck(this, Glide$$1);
return possibleConstructorReturn(this, (Glide$$1.__proto__ || Object.getPrototypeOf(Glide$$1)).apply(this, arguments));
}
new_slider.js:2 new_slider.js is loaded
new_slider.js:6 Uncaught ReferenceError: Glide is not defined
at HTMLDocument.<anonymous> (new_slider.js:6)
at HTMLDocument.dispatch (event.js:328)
at HTMLDocument.elemData.handle (event.js:148)
at Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch (turbolinks.js:75)
at r.notifyApplicationAfterPageLoad (turbolinks.js:994)
at r.pageLoaded (turbolinks.js:948)
at turbolinks.js:872
Gilde is obviously loaded since console.log(Glide) provides an output. new_slider.js
file is loaded after importing Gilde
, so there can't be any conflict. The only issue I found so far was this one, but unfortunately it doesn't help at all.
Could someone point me in the right direction?
In Webpack-land, you'll typically need to import a given module explicitly in every file where you want to use it. So, even though you imported Glide
in app/javascripts/packs/front.js
, if you want to reference it in new_slider.js
, you'll need to import it there as well:
// new_slider.js
import Glide from '@glidejs/glide';
// ...