Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add note about virtual packages.

  • Loading branch information...
commit 95368d05a3b9d87f4a92c228d445ac4cae97a23c 1 parent 76d8848
@mixu authored
View
16 readme.md
@@ -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
1  test/sample/build.js
@@ -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
4 test/sample/generated.js
@@ -1,6 +1,8 @@
(function(){var global = this;function require(p, parent){ var path = require.resolve(p) , mod = require.modules[path]; if (!mod) throw new Error('failed to require "' + p + '" from ' + parent); if (!mod.exports) { mod.exports = {}; mod.call(mod.exports, mod, mod.exports, require.relative(path), global); } return mod.exports;}require.modules = {};require.resolve = function(path){ var orig = path , reg = path + '.js' , index = path + '/index.js'; return require.modules[reg] && reg || require.modules[index] && index || orig;};require.relative = function(parent) { return function(p){ if ('debug' == p) return debug; if ('.' != p.charAt(0)) return require(p); var path = parent.split('/') , segs = p.split('/'); path.pop(); for (var i = 0; i < segs.length; i++) { var seg = segs[i]; if ('..' == seg) path.pop(); else if ('.' != seg) path.push(seg); } return require(path.join('/'), parent); };};
-require.modules['index.js'] = function(module, exports, require, global){eval("module.exports = {\n a: require('./models/a.js'),\n b: require('./models/b.js')\n};\n\n//@ sourceURL=/myApp/index.js");};
+require.modules['model'] = function(module, exports, require, global){eval("module.exports = require(\"./models\");\n//@ sourceURL=/myApp/model");};
+require.modules['index.js'] = function(module, exports, require, global){eval("module.exports = {\n a: require('./models/a.js'),\n b: require('./models/b.js'),\n init: init()\n};\n\nfunction init() {\n console.log(require('model').a);\n console.log(require('model').b);\n}\n\n//@ sourceURL=/myApp/index.js");};
require.modules['models/a.js'] = function(module, exports, require, global){eval("module.exports = 'Module A';\n\n//@ sourceURL=/myApp/models/a.js");};
require.modules['models/b.js'] = function(module, exports, require, global){eval("module.exports = 'Module B';\n\n//@ sourceURL=/myApp/models/b.js");};
+require.modules['models/index.js'] = function(module, exports, require, global){eval("module.exports = {\n a: require('./a.js'),\n b: require('./b.js')\n};\n\n//@ sourceURL=/myApp/models/index.js");};
myApp = require('index.js');
})();
View
8 test/sample/input/index.js
@@ -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);
+}
View
4 test/sample/input/models/index.js
@@ -0,0 +1,4 @@
+module.exports = {
+ a: require('./a.js'),
+ b: require('./b.js')
+};
Please sign in to comment.
Something went wrong with that request. Please try again.