cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

file with one define() is causing "Multiple anonymous defines in url" error #166

Closed jcollum closed 11 years ago

jcollum commented 11 years ago

I'm trying to write my js to be more AMD/CommonJS compatible and be usable with Curl. Grabbed a chunk of code off of SO here: http://stackoverflow.com/questions/13673346/supporting-both-commonjs-and-amd

It looks like so: (mod.js)

(function (name, definition) {
  if (typeof module != 'undefined') {
    console.log("path A");
    module.exports = definition();
  }
  else if (typeof define == 'function' && typeof define.amd == 'object') {
    console.log("path B");
    console.log(definition);
    define(definition);
  }
  else {
    console.log("path C");
    this[name] = definition();
  }
}('mod', {
      sayHi:function (name) {
        console.log('Hi ' + name + '!');
      }
    }
));

I'm calling it from the console with: curl(['mod'], function(mod) {mod.sayHi('Hi unscriptable!'); });

It's failing. Looks like:

curl(['mod'], function(mod) {mod.sayHi('Hi unscriptable!'); }); 
curl f arguments: [x, undefined, Array[1]]  curl-debug.js:40 
curl f return: A {z: function, g: function, e: function, t: function, id: ""…}  curl-debug.js:42
curl h arguments: [A]  curl-debug.js:40
curl V arguments: ["mod", A]  curl-debug.js:40
curl m arguments:  ["mod", "", x]  curl-debug.js:40
curl m return: mod curl-debug.js:42 
curl k arguments: ["mod", x]  curl-debug.js:40
curl O arguments: ["mod", x]  curl-debug.js:40
curl O return: /js/mod curl-debug.js:42
curl k return: Object {b: x, url: "/js/mod"}  curl-debug.js:42
curl B arguments:  [x, "mod", undefined]  curl-debug.js:40
curl f arguments: [x, "mod", undefined, undefined]  curl-debug.js:40
curl f return: A {z: function, g: function, e: function, t: function, id: "mod"…}  curl-debug.js:42
curl B return: A {z: function, g: function, e: function, t: function, id: "mod"…}  curl-debug.js:42
curl A arguments: ["/js/mod", x]  curl-debug.js:40
curl A return: /js/mod.js curl-debug.js:42
curl W arguments:  [A]  curl-debug.js:40
curl I arguments:  [A]  curl-debug.js:40
curl I return: /js/mod.js curl-debug.js:42
curl J arguments: [A, function, function]  curl-debug.js:40
curl J return:  <script type=​"text/​javascript" charset=​"utf-8" async src=​"/​js/​mod.js">​</script>​  curl-debug.js:42
curl W return: A {z: function, g: function, e: function, t: function, id: "mod"…}  curl-debug.js:42
curl V return: A {z: function, g: function, e: function, t: function, id: "mod"…}  curl-debug.js:42
curl h return:  A {z: function, g: function, e: function, t: function, id: ""…}  curl-debug.js:42
da 
path B mod.js:7
Object {sayHi: function}  mod.js:8
curl X arguments: [Arguments[1]]  curl-debug.js:40
curl X return: Object {id: undefined, p: Array[0], v: function, o: undefined}  curl-debug.js:42
**Uncaught Error: Multiple anonymous defines in url curl.js:17**
curl: ********** modules waiting: 2 curl-debug.js:68
curl: ********** module waiting: mod.js curl-debug.js:71
curl: ********** module waiting: mod curl-debug.js:71
jcollum commented 11 years ago

Looks like changing the define call to define(name, [], definition); is getting me closer. It doesn't error but I don't get a mod object out of:

unscriptable commented 11 years ago

Hey @jcollum!

If you're just targeting AMD and CJSM, then you could try one of these. I recommend the top one in curl.js.

I can't explain why that's failing, atm. Hmmm...

-- John

unscriptable commented 11 years ago

The minification is making it hard to see what's going on.

unscriptable commented 11 years ago

Hey @jcollum, I copied your mod.js and tried to load it using a minified curl.js and it works fine for me. o_O -- J

jcollum commented 11 years ago

Yeah, I tried using the unminified curl.js but it failed in a big way so I don't think that'll work. As an end user it'd be helpful if there was a curl.js and curl-min.js in the dist folder.

I'll try your suggestions out tomorrow. Thanks for the help.

unscriptable commented 11 years ago

I believe this is resolved, too? If not, please reopen it and let us know. -- John

phated commented 11 years ago

This issue looks pretty close to something that is happening to me. On the Dev branch, when I include lodash and run a cram build on my project, it is exiting with this error. It seems that because lodash defines the anonymous AMD module inside its closure, it doesn't get assigned an identifier to be used by the built code, so curl doesn't know where to grab it. Everything works in a non-built version, and if I change

define(function() {
    return _;
});

to

define('lodash', function() {
    return _;
});

inside of lodash, the build works.

If this isn't related, I can open a new issue, just let me know.

Oh, and here is my config:

curl({
  baseUrl: './',
  pluginPath: 'curl/plugin',
  paths: {
    'jquery': 'deps/jquery',
    'lodash': 'deps/lodash',
    'tmpl': 'js/lodash-template'
  },
  packages: [
    {
      name: 'flight',
      location: 'deps/flight'
    },
    {
      name: 'curl',
      location: 'deps/curl/src/curl'
    },
    {
      name: 'app',
      location: 'js'
    }
  ],
  preloads: ['jquery']
}, ['app/Stickers']).then(function(Stickers){
  console.log('Loaded');

  Stickers.attachTo('.js-attach-stickers');

}, function(err){
  console.log('Error in loader:', err);
});
unscriptable commented 11 years ago

hey @phated, this is likely a problem in cram.js. I just published a new cram.js release (0.7.1) that fixed a problem that may be the cause of the lodash issue. try it. if it still fails, I'll set up a lodash test. - John

phated commented 11 years ago

Looks like it is still failing.