Permalink
Browse files

Add note about virtual packages.

  • Loading branch information...
1 parent 76d8848 commit 95368d05a3b9d87f4a92c228d445ac4cae97a23c @mixu committed Jul 16, 2012
Showing with 28 additions and 5 deletions.
  1. +13 −3 readme.md
  2. +1 −0 test/sample/build.js
  3. +3 −1 test/sample/generated.js
  4. +7 −1 test/sample/input/index.js
  5. +4 −0 test/sample/input/models/index.js
View
@@ -11,6 +11,8 @@ A CommonJS-based build system with a chainable API
- watch() the build and rebuild automatically when files change
- Bind variables under window.* to require() statements using replace()
- Compile templating language files to JS via a custom handler
+- Source url support
+- Virtual packages
- Concatenate multiple packages into one file
## Usage example
@@ -155,6 +157,16 @@ The callback params:
- wrap: a function(filename, content) which wraps the content string inside a anonymous function, just like normal JS files.
- second param (done): a callback(string) which should be called with the return value - this allows for async calls inside the handler.
+## Virtual packages
+
+Let's assume that your app consists of several internal packages. For example, within your app code you want to refer to the Foo model as require('model').Foo rather than require('../../model/foo.js').
+
+One way to do this would be to simply have multiple packages, App and Models - where models exports window.model. If you prefer to avoid that extra global variable, do this instead:
+
+ .define('model', 'require("./model")');
+
+Don't use require('model') in files inside the ./model directory, since that may introduce a circular dependency (e.g. model/a -> model/index -> model/a).
+
## Generating modules from JS
```define(module, code)```: Meant for writing a full module. The difference here is that while replace() code is not wrapped in a closure while define() code is.
@@ -167,7 +179,7 @@ The callback params:
The example above generates a index.js file depending on hasBrowser and hasLocalStorage.
-## Concatenating multiple builds
+## Concatenating multiple packages into one file
Glue.concat([ package, package ], function(err, txt)). For example:
@@ -186,6 +198,4 @@ Glue.concat([ package, package ], function(err, txt)). For example:
## TODO
-Virtual packages
-
.npm(file.json): includes a package.json
View
@@ -5,6 +5,7 @@ new Glue()
.include('./input')
.basepath('./input')
.export('myApp')
+ .define('model', 'module.exports = require("./models");')
.set('debug', true)
.render(function (err, txt) {
fs.writeFileSync('./generated.js', txt);
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -1,4 +1,10 @@
module.exports = {
a: require('./models/a.js'),
- b: require('./models/b.js')
+ b: require('./models/b.js'),
+ init: init()
};
+
+function init() {
+ console.log(require('model').a);
+ console.log(require('model').b);
+}
@@ -0,0 +1,4 @@
+module.exports = {
+ a: require('./a.js'),
+ b: require('./b.js')
+};

0 comments on commit 95368d0

Please sign in to comment.