Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Bug: UMD bundle template is too tricky for its own good. (CJS failure) #471

Closed
typhonrt opened this issue Jan 16, 2016 · 3 comments
Closed

Comments

@typhonrt
Copy link
Contributor

Greets Guy et al,

I'm definitely glad there is a UMD bundle option now. However, the template implementation is just being a bit too tricky for its own good. In particular it is the deps.map(require)) call which fails. In particular it fails for my backbone-parse-es6 library.

The solution is to not be tricky and essentially duplicate the AMD and CJS templates removing the deps.map(require)) statement.

I have tested the following and it works fine for the sfx-umd.js template:

(function(factory) {
  if (typeof define == 'function' && define.amd)
    define(${JSON.stringify(deps)}, factory);
  else if (typeof module == 'object' && module.exports && typeof require == 'function')
    module.exports = factory(${deps.map(function(dep) {
    return 'require("' + dep + '")';
  }).join(', ')});
  else
    ${ deps.length && !globalDeps.length
      ? 'throw new Error("Module must be loaded as AMD or CommonJS")'
      : (globalName ? globalName + ' = ' : '') + 'factory(' + (globalDeps.length ? globalDeps.join(', ') : '') + ')'};
});

In particular if you are interested though in the error here it is:

system.src.js:4837 Uncaught Error: Module underscore not declared as a dependency.(anonymous function) @ system.src.js:4837a @ system.src.js:4837(anonymous function) @ backbone-parse.js:10054(anonymous function) @ backbone-parse.js:2(anonymous function) @ backbone-parse.js:10048(anonymous function) @ backbone-parse.js:10058$ @ system.src.js:4837d.execute @ system.src.js:4837s @ system.src.js:4837n @ system.src.js:4837execute @ system.src.js:4837y @ system.src.js:4837x @ system.src.js:4837p @ system.src.js:4837h @ system.src.js:4837(anonymous function) @ system.src.js:4837
indexSrc.html:1 Uncaught (in promise) Uncaught Error: Module underscore not declared as a dependency.
    Evaluating http://localhost:63342/backbone-parse-es6-todos/backbone-parse.js
    Error loading http://localhost:63342/backbone-parse-es6-todos/main.js

The generated output which fails is this:

(function(factory) {
  var deps = ["underscore","jquery","parse","parse/lib/browser/encode"];

  if (typeof define == 'function' && define.amd)
    define(deps, factory);
  else if (typeof module == 'object' && module.exports && typeof require == 'function')
    module.exports = factory.apply(null, deps.map(require));
  else
  throw new Error("Module must be loaded as AMD or CommonJS");
});

While the UMD template I listed above which essentially duplicates the relevant part of the AMD / CJS templates produces this which works:

(function(factory) {
  if (typeof define == 'function' && define.amd)
    define(["underscore","parse","jquery","parse/lib/browser/encode"], factory);
  else if (typeof module == 'object' && module.exports && typeof require == 'function')
    module.exports = factory(require("underscore"), require("parse"), require("jquery"), require("parse/lib/browser/encode"));
  else
    throw new Error("Module must be loaded as AMD or CommonJS");
});

I can create a PR for this change after receiving your input. I don't see any problem / conflict with this change.

Regards...

@typhonrt typhonrt changed the title Bug: UMD bundle template is too tricky for it's own good. (CJS failure) Bug: UMD bundle template is too tricky for its own good. (CJS failure) Jan 16, 2016
@guybedford
Copy link
Member

Thanks so much! Yes this is exactly the right template to use, I'm including it in the next update.

@guybedford
Copy link
Member

Released in 0.15.4.

@typhonrt
Copy link
Contributor Author

👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants