Skip to content

Modules naming expressions

Mikhail Davydov edited this page Dec 22, 2013 · 6 revisions

A simple way to declare module in LMD build is "name": "path" expression in "modules" section. It is ok if your app consists of few modules, but if it grows it is not convenient to declare each module.

Assume that your app tree is:

app/
├── main.js
├── collections
│   ├── items.js
│   └── users.js
├── helpers
│   ├── date.js
│   └── string.js
├── models
│   ├── item.js
│   └── user.js
├── routers
│   └── main.js
├── templates
│   ├── app.html
│   ├── item.html
│   ├── items.html
│   ├── user.html
│   └── users.html
└── views
    ├── app.js
    ├── item.js
    └── user.js

To define all modules like dirname/filename you can just write single expression in "modules" section:

{
    "modules": {
        "<%= dir[0] %>/<%= file %>": "*/*.{js,html}"
    }
}

LMD will find all modules matches glob pattern */*.{js,html} and declares each of them using underscore-style naming template "<%= dir[0] %>/<%= file %>".

And if you want to declare your modules in more expressive style like itemTemplate or mainRouter you can also use naming template:

{
    "modules": {
        "<%= file %><%= dir[0][0].toUpperCase() %><%= dir[0].slice(1, -1) %>": "*/*.{js,html}"
    }
}

Template variables

Assume that full path of our module is /Users/azproduction/glob/js/views/user.ie.js and build "root" is /Users/azproduction/glob. LMD will extract some parts of this file and pass them into naming template:

  • * - alias to <%= file %>
  • dir - Array of reversed sub-directories: ['views', 'js', 'glob', 'azproduction', 'Users']
  • ext - String the last file extension without .: js
  • file - String file name: user.ie
  • basename - String path.baseneme: /Users/azproduction/glob/js/view
  • subdir - Array|String a path releated to "root": ['views', 'js'] with toString() of "js/views/". If path is empty toString() will return an empty string.

See also:

Clone this wiki locally