requirejs / almond

A minimal AMD API implementation for use after optimized builds
Other
2.42k stars 169 forks source link

AlmondJS + CDN module with UMD #112

Closed bjnsn closed 8 years ago

bjnsn commented 8 years ago

Recently ran into a collision between a built version of a project that uses AlmondJS but loads a CDN version of webfontloader. Webfontloader recently added a UMD wrapper - and that caused an error within the AlmondJS define method:

  Uncaught TypeError: Cannot read property 'splice' of undefined

The culprit was a call within the webfontloader UMD wrapper with this signature: define(function():*). This is valid within RequireJS, but isn't within AlmondJS.

Is this an issue with Webfontloader, with AlmondJS - or should I not be running AlmondJS alongside CDN versions of libraries that use UMD?

jrburke commented 8 years ago

Almond is not designed to load remote resources, it is strictly for use when combining a bunch of modules into one file for download. So this behavior is expected, out of scope for almond.

bjnsn commented 8 years ago

Hmm. I'm not using it to load an external resource; it is being referenced by an external resource that is already loaded via a script tag (from a CDN). Almond's define is being duck-typed by Webontloader's UMD wrapper to see if it is an AMD loader - once it is decided that it 'quacks like a duck' it proceeds to use it like RequireJS define.

I believe a noConflict option that would prevent AlmondJS from injecting define into the global scope would solve the issue. That way other files on the page which check for define wouldn't find one that doesn't behave as expected.

jrburke commented 8 years ago

You can get the effect of a noConflict by building with the r.js optimizer setting wrap: true.

bjnsn commented 8 years ago

Perfect - exactly what I was looking for!