I have several JavaScript files in my web application which were previously loaded through <script>
tags, in order of relative dependencies. Now I'm re-organizing them through require.js in order to accumulate and minify them for production.
For starters, I'd like to simply load all files into the global (window) context, without AMD encapsulation yet:
require([
'jquery'], function() {
require([
'jquery-ui'], function() {
require([
'jquery-ui.touch-punch',
// [...]
]);
});
});
The idea here is that 'jquery'
defines a global (window context) jQuery
variable, 'jquery-ui'
sets jQuery.ui
and 'jquery-ui.touch-punch'
alters jQuery.ui
(for better touch support).
This works well when running as is, i.e. without optimization. However, after compiling into one file and minifying using the RequireJS optimizer, the following error occurs:
Uncaught TypeError: Cannot read property 'mouse' of undefined
This happens on a line trying to access jQuery.ui.mouse
.
Inside the browser console, jQuery
is correctly set in the window context, but jQuery.ui
is undefined.
However, manually executing require(['jquery-ui'])
does set jQuery.ui
as expected.
It seems like jQuery UI behaves differently after optimization, but I can't see how exactly or why. What am I doing wrong?