How to handle circular dependencies with RequireJS/AMD?

This is indeed a restriction in the AMD format. You could use exports, and that problem goes away. I find exports to be ugly, but it is how regular CommonJS modules solve the problem: define(“Employee”, [“exports”, “Company”], function(exports, Company) { function Employee(name) { this.name = name; this.company = new Company.Company(name + “‘s own company”); }; … Read more

How to make a jQuery plugin loadable with requirejs

There are some caveats with using shim configuration in RequireJS, pointed out on http://requirejs.org/docs/api.html#config-shim. Namely, “Do not mix CDN loading with shim config in a build” when you’re using the optimizer. I was looking for a way to use the same jQuery plugin code on sites both with and without RequireJS. I found this snippet … Read more

Webpack ProvidePlugin vs externals?

It’s both possible: You can include libraries with a <script> (i. e. to use a library from a CDN) or include them into the generated bundle. If you load it via <script> tag, you can use the externals option to allow to write require(…) in your modules. Example with library from CDN: <script src=”https://code.jquery.com/jquery-git2.min.js”></script> // … Read more

Requirejs why and when to use shim config

A primary use of shim is with libraries that don’t support AMD, but you need to manage their dependencies. For example, in the Backbone and Underscore example above: you know that Backbone requires Underscore, so suppose you wrote your code like this: require([‘underscore’, ‘backbone’] , function( Underscore, Backbone ) { // do something with Backbone … Read more

Requirejs domReady plugin vs Jquery $(document).ready()?

It seems like all the key points were already hit, but a few details fell through the cracks. Mainly: domReady It is both a plugin and a module. If you include it in the the requirements array w/ a trailing ! your module won’t execute until it’s “safe” to interact w/ the DOM: define([‘domReady!’], function … Read more

Best way to organize jQuery/JavaScript code (2013) [closed]

I’ll go over some simple things that may, or may not, help you. Some might be obvious, some might be extremely arcane. Step 1: Compartmentalize your code Separating your code into multiple, modular units is a very good first step. Round up what works “together” and put them in their own little encased unit. don’t … Read more

How to disable the warning ‘define’ is not defined using JSHint and RequireJS

Just to expand a bit, here’s a .jshintrc setup for Mocha: { …. “globals” : { /* MOCHA */ “describe” : false, “it” : false, “before” : false, “beforeEach” : false, “after” : false, “afterEach” : false } } From the JSHint Docs – the false (the default) means the variable is read-only. If you … Read more