Search code examples
javascriptdojorequirejsamd

Indent requireJS files so that it's easy to add and delete included modules


I am trying to figure out the best way to indent my files. I am writing something using Dojo, and have some files with a lot of dependencies.

The hope:

  • Each line is the same (especially the first one, and the last one)
  • Easy to add/delete elements

At the moment, I am doing this:

define([
  "dojo/_base/declare",
  "dojo/topic",

  "dijit/Dialog",
  "dijit/_OnDijitClickMixin",
  "dijit/layout/BorderContainer",
  "dijit/layout/TabContainer",
  "dijit/layout/ContentPane",

  "hotplate/hotDojoStores/stores",
  "hotplate/hotDojoWidgets/_OverlayMixin",
  "hotplate/hotDojoGlobals/globals",
  "hotplate/hotDojoAuth/panels/Resume",
  "hotplate/hotDojoJade/DestroyableJadeTemplatedContainer",
  "hotplate/hotDojoStoreConfig/ConfigVars",
  "hotplate/hotDojoWidgets/util",
  "hotplate/hotDojoComet/_TabRegisterMixin",
  "hotplate/hotDojoAuth/_ReLoginMixin",

  "hotplate/bd/WorkspacesUsersConfig",
  "hotplate/bd/WorkspacesConfig",
  "hotplate/bd/UsersConfig",
  "hotplate/bd/Dashboard",
  "hotplate/bd/Contacts",

  ], function(

  declare
  , topic

  , Dialog
  , _OnDijitClickMixin
  , BorderContainer
  , TabContainer
  , ContentPane

  , stores
  , _OverlayMixin
  , globals
  , Resume
  , DestroyableJadeTemplatedContainer
  , ConfigVars
  , util
  , _TabRegisterMixin
  , _ReLoginMixin

  , WorkspacesUsersConfig
  , WorkspacesConfig
  , UsersConfig
  , Dashboard
  , Contacts

){
  var counter = 0;
});

Problems:

  • There is an extra comma at the end of the last parameter. All modern browsers are OK with it, but IE9 still seems to choke on it. Yes of course I can delete it, but then it would break the first requirement, where each line is equal

  • The first function parameter is different to the others (if course, missing comma)

What's the least painful way, or established standard, to indent requireJS files with a lot of modules?


Solution

  • Here's the style that helps me avoid the most errors.

    define([
        'dijit/form/DateTextBox'
        , 'dijit/form/FilteringSelect'
        , 'dijit/layout/ContentPane'        
        , 'dojo/_base/declare'
    ], function(
        DateTextBox
        , FilteringSelect
        , ContentPane
        , declare
    ){
        return declare(ContentPane, {
            postCreate: function() {
                // do stuff
            }
        });
    });
    
    • Leading commas.
    • Alphabetically sorted args in the define function.