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.js
file has a call todefine
. So setting ashim
for it is useless, becauseshim
is 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.js
is not an AMD-module. So you do need theshim
for it. However, it depends onWkt
being present in the global space. The logic inwicket.js
is such that when it callsdefine
it does not setWkt
in 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 todefine
though and name the filewicket-glue.js
so that RequireJS takes the module name from the file name.)I've also added a
map
which essentially says "in all modules, when the modulewicket
is required, loadwicket-glue
instead, but inwicket-glue
whenwicket
is required, loadwicket
".The net effect is that whenever
wicket
is required,Wkt
will be leaked to the global space andwicket-glue
should work fine.