-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] umd shrinkage #36
Conversation
Example before after for a simple module with no dependencies (strict umd): // Before
(function (global, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// export as AMD
define('myModule', [], factory);
} else if (typeof module !== 'undefined' && module.exports && typeof require === 'function') {
// node/browserify
factory();
} else {
// browser global
global.myModule = {};
factory();
}
}(typeof window !== 'undefined' ? window : this, function () {
'use strict';
console.log( 'hello' );
}));
// After
(function (factory) {
typeof define === 'function' && define.amd ?
define('myModule', [], factory) :
typeof exports === 'object' ?
factory() :
(myModule = {}, factory());
}(function () {
'use strict';
console.log( 'hello' );
})); |
Looks good - the current header is a bit on the long side. Occurs to me we could even take this further and differentiate between modules/bundles that have imports/exports and those that don't - for instance the example above could be more compact (at the same time, it would make sense to drop the (function (factory) {
typeof define === 'function' && define.amd ?
define('myModule', [], factory) :
factory();
}(function () {
'use strict';
console.log( 'hello' );
})); Possibly overkill considering how rare that situation is though. This does mean we're introducing a strict mode violation ( Btw, to save you some manual labour, once the output is as you expect, you can generate test results automatically with |
I would be a little concerned about loosening up the CommonJS environment check too much. Checking for just The ternary expression transformation is something that a good minifier should be able to do, though it doesn't hurt to help it along. Overall I like the direction of us, and have been thinking about a way to simply bundle the files together without any sort of environment checks by esperanto (essentially allow custom handling). |
I don't think so, but I think I will revert this. I realized later that this could create more work for minifiers and it's probably not worth the trouble.
Yes, I was also worried about this as well and went about searching for UMD wrappers. I based it on umdjs which has a lot of usage and hasn't seemed to take any heat for this. Also, immutable.js has a custom UMD wrapper which uses this check and I've gotten no complaints. I think most don't check for
I so agree. Unfortunately I haven't found one that will be this bold yet. |
This UMD definition is more terse. It's based on https://github.com/umdjs/umd/blob/master/returnExports.js but takes advantage of some expressions and implicit global scope for the fallback case.
Nice idea! I updated to include that case. Also updated all tests. If you guys are happy with this, it should be in a mergeable state. |
Just realised this should apply to one-to-one conversions too. Working on it |
Ah! Sorry I forgot about those. |
NP! I missed it too. Have pushed out a new release.
Definitely interested to hear your thoughts on custom handling. I keep thinking 'surely it's straightforward' but then discover some quirk of how AMD and CJS differ that makes it a pain to generalise, but I'm probably just not being imaginative enough. A plain bundle would be straightforward though, as long as there's no external dependencies (or you can safely assume they're on the page already) |
a "raw strict" mode might be nice. The top-level module exports via |
This shortened definition caused problems for us, because we had some other code defining |
This UMD definition is more terse. It's based on https://github.com/umdjs/umd/blob/master/returnExports.js but takes advantage of some expressions and implicit global scope for the fallback case.
( Need to update tests before this is pullable, wanted to get it up for comments first )