Permalink
Browse files

support RequireJS

  • Loading branch information...
1 parent 7f450c4 commit f66c58339c08767664a58ed688c1ab8e572b50d4 @hisayan hisayan committed Jan 10, 2012
Showing with 26 additions and 11 deletions.
  1. +26 −11 lib/express-expose.js
View
@@ -96,7 +96,7 @@ HTTPSServer.prototype.expose = function(obj, namespace, name){
// buffer module object
} else if (this._require) {
obj = 'module.exports = ' + string(obj);
- this.expose(renderRegister(namespace, obj), name);
+ this.expose(renderRegister(namespace, obj, this._rjs), name);
// buffer object
} else {
this.expose(renderNamespace(namespace), name);
@@ -110,17 +110,26 @@ HTTPSServer.prototype.expose = function(obj, namespace, name){
/**
* Expose the common-js module system to the client-side.
*
+ * @param {Boolean} rjs
* @return {HTTPServer} for chaining
* @api public
*/
res.exposeRequire =
HTTPServer.prototype.exposeRequire =
-HTTPSServer.prototype.exposeRequire = function(){
+HTTPSServer.prototype.exposeRequire = function(rjs){
if (this._require) return this;
this._require = true;
- var js = fs.readFileSync(__dirname + '/require.js', 'ascii');
- this.expose(js);
+
+ if (!rjs) {
+ var js = fs.readFileSync(__dirname + '/require.js', 'ascii');
+ this.expose(js);
+
+ this._rjs = false;
+ }else{
+ this._rjs = true;
+ }
+
return this;
};
@@ -144,7 +153,7 @@ HTTPSServer.prototype.exposeModule = function(path, namespace, name){
, namespace = namespace || basename(path, extname(path));
if (this._require) {
- this.expose(renderRegister(namespace, js), name);
+ this.expose(renderRegister(namespace, js, this._rjs), name);
} else {
js = namespace
+ ' = (function(exports){\n'
@@ -181,17 +190,23 @@ HTTPSServer.prototype.exposed = function(name){
*
* @param {String} mod
* @param {String} js
+ * @param {Boolean} rjs
* @return {String}
* @api private
*/
-function renderRegister(mod, js) {
- return 'require.register("'
- + mod + '", function(module, exports, require){\n'
- + js + '\n});';
+function renderRegister(mod, js, rjs) {
+ if (rjs) {
+ return 'define("'
+ + mod + '", function(require, exports, module){\n'
+ + js + '\n});';
+ }else{
+ return 'require.register("'
+ + mod + '", function(module, exports, require){\n'
+ }
}
-/**
+/*
* Render a namespace from the given `str`.
*
* Examples:
@@ -257,4 +272,4 @@ function string(obj) {
} else {
return JSON.stringify(obj);
}
-}
+}

0 comments on commit f66c583

Please sign in to comment.