Skip to content
This repository was archived by the owner on Feb 17, 2021. It is now read-only.

add support for AMD and CommonJS #151

Merged
merged 3 commits into from
Apr 21, 2015
Merged

add support for AMD and CommonJS #151

merged 3 commits into from
Apr 21, 2015

Conversation

stebunovd
Copy link
Contributor

no jQuery dependency for AMD or CommonJS, but jQuery will be used if available in global scope. All tests passing. Solves #150

@@ -2387,4 +2405,8 @@
// @@include('../../tmp/js/hopscotch_templates.js') //
}());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we could get around swapping out window.hopscotch with a couple of changes...

  • Use (function(){...}).call(winHopscotch);
  • Update the namespace for the jst task in Gruntfile.js to use templates instead of hopscotch.templates.

@timlindvall
Copy link
Contributor

Hi @stebunovd, thanks for the pull request! Would love to see Hopscotch work well in whichever context its used within. A contemplation about how to best handle Hopscotch in multiple environments...

Originally, I believe this was something we were planning to address as part of refactoring Hopscotch in 0.3.0. When originally thinking through the problem, the solution I was leaning towards was to use common environment-agnostic JS code that was then included (using the include-replace grunt task) in wrappers for their particular environment. This would result in multiple files (Hopscotch for AMD, CommonJS, window global) and the developer would import the file that works best for their environment (instead of having Hopscotch interpret what to use depending on window globals). Hopefully this allows the developer to best reason about how Hopscotch is invoked in their app and cuts down on code that's isn't required for every environment.

Do we believe this sort of build target separation be useful, or would this just convolute things?

@stebunovd
Copy link
Contributor Author

Hi @zimmi88,

thank you for pointing out how to avoid that hack for templates.

the pattern I used here for AMD/CommonJS support is called UMD, see https://github.com/umdjs/umd. This is a well-tested and widely used solution, which can be found in many JS libraries. Size overhead doesn't seem to be large, and having one universal file looks simpler and clearer from my perspective. In most cases users want all libraries in their environment to support the same format, either AMD, or CommonJS, or browser globals, so auto-detect works fine. Even if I want to do something different, say export library object from CommonJS environment to browser global scope, this is pretty easy to do either by single line of code window.hopscotch = require('hopscotch'), or using built-in tool of my favorite JS bundler. For example, see exports loader for Webpack http://webpack.github.io/docs/list-of-loaders.html

}
context[namespace] = factory(context.jQuery, context.YAHOO);
}
}(this, (function(jQuery, YAHOO) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove jQuery and YAHOO from factory params, since they are optional library dependencies. Somebody quickly looking at the source code might assume they are hard dependencies. You should still be able to do hasJquery = (typeof jQuery !== undefinedStr), inside factory method, since it will assume window scope anyway.

@kate2753
Copy link
Contributor

Thank you for the pull request! Overall these changes look good to me. My only concern is jQuery and YAHOO params to factory function. It would be nice to remove them.

@stebunovd
Copy link
Contributor Author

A matter of habit. I write all my JS code with 'use strict';, so I always try to avoid implicit calls to global scope. However hopscotch.js doesn't use strict mode, so you're right, these params are redundant.

@kate2753
Copy link
Contributor

Looks good to me! I'll pull it in tomorrow if there are no other comments.

@timlindvall timlindvall modified the milestone: 0.2.4 Apr 21, 2015
kate2753 added a commit that referenced this pull request Apr 21, 2015
Add support for AMD and CommonJS
@kate2753 kate2753 merged commit 22beebc into LinkedInAttic:master Apr 21, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants