I'm developing a module for Magento2 and I'm using requirejs to load custom javascript that depend on jquery. I'm using the shim option in requirejs-config.js to set this dependency between the custom scripts and jquery. The issue is that this dependency is not (always) set. Sometimes jQuery does load before the script and all is fine but sometimes it loads after the scripts resulting in a script error:
Uncaught ReferenceError: jQuery is not defined(anonymous function) @ jquery.easing.1.3.js:39
Uncaught ReferenceError: jQuery is not defined(anonymous function) @ jquery.flexslider-min.js:5
Uncaught TypeError: $(...).flexslider is not a function
See below an example of my requirejs-config.js:
var config = {
map: {
'*': {
'flexslider': 'Vendor_Modulejs/jquery.flexslider-min',
'picturefill': 'Vendor_Modulejs/picturefill.min',
'easing': 'Vendor_Modulejs/jquery.easing.1.3',
'hoverintent': 'Vendor_Modulejs/jquery.hoverIntent',
'fitvids': 'Vendor_Modulejs/jquery.fitvids',
'vimeo': 'Vendor_Modulejs/jquery.vimeo.api.min'
}
},
shim: {
'flexslider': ['jquery'],
'picturefill': ['jquery'],
'easing': ['jquery'],
'hoverintent': ['jquery'],
'fitvids': ['jquery'],
'vimeo': ['jquery']
}
};
This is the javascript in my phtml file:
require(['jquery', 'domReady!', 'picturefill', 'flexslider', 'easing', 'hoverintent', 'fitvids', 'vimeo'], function($) {
"use strict";
// javascript here
});
What am I doing wrong here, why isnt the shim option honored and is jQuery not always loaded before the other scripts.