I am using the library Wicket for map stuff. If I reference it as the script tag as normal, It works fine. Working link as below.
The problem comes when our project using RequireJS for module loader.
This is the following code that I tried.
require.config({
waitSeconds: 200,
paths: {
'wicket': '/Vendor/Wicket/wicket',
'wicketGmap3': '/Vendor/Wicket/wicket-gmap3'
},
shim: {
wicket: {
exports: 'Wkt'
},
wicketGmap3: {
deps: ['wicket']
}
},
});
require(['wicket', 'wicketGmap3'],(Wkt) => {
$(() => {
angular.bootstrap(document, ['app']);
});
});
The error is still as below.
Uncaught ReferenceError: Wkt is not defined at wicket-gmap3.js:744
Has anyone experienced the same?
The
wicket.jsfile has a call todefine. So setting ashimfor it is useless, becauseshimis meaningful only for non-AMD "modules" (i.e. files that are not really modules but which we want to behave as if they were). AMD modules calldefine.On the other hand
wicket-gmap3.jsis not an AMD-module. So you do need theshimfor it. However, it depends onWktbeing present in the global space. The logic inwicket.jsis such that when it callsdefineit does not setWktin the global space. (Which is the correct behavior for well-behaved AMD modules.) You need to perform the leakage yourself.Change your config to:
I've added a new module named
wicket-glue. I often place such modules together with the configuration so that they don't require an additional data fetch. You could just as well put it in a separate file if you want. (If I did that, I'd remove the first argument todefinethough and name the filewicket-glue.jsso that RequireJS takes the module name from the file name.)I've also added a
mapwhich essentially says "in all modules, when the modulewicketis required, loadwicket-glueinstead, but inwicket-gluewhenwicketis required, loadwicket".The net effect is that whenever
wicketis required,Wktwill be leaked to the global space andwicket-glueshould work fine.