Manage modules as folders - EugeneN/cafe GitHub Wiki

It may be tedious to specify each separate file in your application as a separate module, so cafe provides you a mechanism to specify some folder as a separate folder. Let's see how we can do this on a little example.

Initializing app, adding module

  1. First let's init our application - run cafe init
  2. cd to your app root and run cafe skel module [module name] (for example cafe skel module omg_module)
  3. Registering module in recipe:
abstract: 
    api_version: 5


modules:
    - omg_module:              # Step1 - register module
        path: omg_module


bundles:
    default_bundle:
        modules:
            - omg_module       # Step2 - include module in bundle
  1. Run cafe menu build, for now you will be able to do require('omg_module'). Folder modules are required by folder name.

Simply saying when cafe meets some folder with slug.json - it thinks about it as a folder module. (This idea is came from Spine apps).

Relative and global requires

Each module has it's own namespace, so there will be no name collisions between requires of filenames in different modules.

Let's see at on a little example:

  1. Create another one module in the same way cafe skel module omg_module2
  2. Register it in recipe in the same way as omg_module
  3. Add lib.coffee to omg_module and omg_module2 with such contents:

omg_module/lib.coffee

# omg_module/lib content
module.exports = "lib from omg_module"

omg_module2/lib.coffee

# omg_module2/lib content
module.exports = 'lib from omg_module2'
  1. Modify modules indexes, in the way that each of them will return it's own lib export:

omg_module/index.coffee

# omg_module/index.coffee content
module.exports = require 'lib'

omg_module2/index.coffee

# omg_module2/index.coffee content
module.exports = require 'lib'
  1. Run cafe menu build* and check in browser console the output of require('omg_module') and require('omg_module2'). They are different. I hope you've got the main idea. Relative imports such as '../', './' also works.