Permalink
Browse files

added middleware for npm

  • Loading branch information...
agate committed Jun 19, 2012
1 parent f020829 commit 4a0c88de5899e63a9aea50ab98e6a9df6b2f8a1b
View
@@ -1,11 +1,15 @@
+### 0.5.7
+
+ * added middleware for express/connect
+
### 0.5.6
* merged 0.5.4
-
+
### 0.5.5
- * compile support for connect
-
+ * compile support for connect
+
### 0.5.4
* added haml filter support
@@ -15,7 +19,7 @@
* fixed self problem in bootstrap
### 0.5.2
-
+
* fixed #3
### 0.5.0
View
@@ -1 +1 @@
-0.5.6
+0.5.7
@@ -1,3 +1,3 @@
module Mochiscript
- VERSION = "0.5.6"
+ VERSION = "0.5.7"
end
@@ -1,4 +1,5 @@
-var $m = {
+var fs = require('fs');
+var $m = {
ROOT: root,
ADAPTER: {
out: function () { print.call(null, arguments); },
@@ -1141,8 +1142,39 @@ exports.mochi = $m;
exports.compile = function(text, opts) {
return $m.parse(text);
};
+exports.middleware = function (options) {
+ options = options || {};
+
+ var src = options.src;
+ var dest = options.dest;
+ var prefix = options.prefix || '';
+
+ if (!src) { throw new Error('mochiscript.middleware() requires "src" directory'); }
+
+ return function (req, res, next) {
+ if (req.method == 'GET' && req.url.match(/\.js$/)) {
+ var prefixRegExp = new RegExp('^/' + prefix + '/?');
+ var filePath = req.url.replace(prefixRegExp, '/');
+ var msPath = src + filePath.replace(/js$/, 'ms');
+ var jsPath = dest + filePath;
+
+ fs.readFile(msPath, "utf8", function(err, data) {
+ if (err) {
+ next();
+ } else {
+ data = $m.parse(data);
+ if (dest) { fs.writeFileSync(jsPath, data); }
+
+ res.header("Content-type", "text/javascript");
+ res.send(data);
+ }
+ });
+ } else {
+ next();
+ }
+ }
+};
-var fs = require('fs');
var requireScript = "var $m = require('mochiscript').mochi; $m.PUSH_ROOT(root);";
var endScript = "$m.POP_ROOT();";
if (require.extensions) {
@@ -3,7 +3,7 @@
"description": "Javascript Dessert",
"keywords": [ "javascript", "language", "mochiscript", "compiler" ],
"author": "Jeff Su",
- "version": "0.5.6",
+ "version": "0.5.7",
"licenses": [ { "type": "MIT" }],
"engines": { "node": ">=0.4.0" },
"directories": { "lib": "./lib/mochiscript" },
@@ -1,4 +1,5 @@
-var $m = {
+var fs = require('fs');
+var $m = {
ROOT: root,
ADAPTER: {
out: function () { print.call(null, arguments); },
@@ -21,8 +22,39 @@ exports.mochi = $m;
exports.compile = function(text, opts) {
return $m.parse(text);
};
+exports.middleware = function (options) {
+ options = options || {};
+
+ var src = options.src;
+ var dest = options.dest;
+ var prefix = options.prefix || '';
+
+ if (!src) { throw new Error('mochiscript.middleware() requires "src" directory'); }
+
+ return function (req, res, next) {
+ if (req.method == 'GET' && req.url.match(/\.js$/)) {
+ var prefixRegExp = new RegExp('^/' + prefix + '/?');
+ var filePath = req.url.replace(prefixRegExp, '/');
+ var msPath = src + filePath.replace(/js$/, 'ms');
+ var jsPath = dest + filePath;
+
+ fs.readFile(msPath, "utf8", function(err, data) {
+ if (err) {
+ next();
+ } else {
+ data = $m.parse(data);
+ if (dest) { fs.writeFileSync(jsPath, data); }
+
+ res.header("Content-type", "text/javascript");
+ res.send(data);
+ }
+ });
+ } else {
+ next();
+ }
+ }
+};
-var fs = require('fs');
var requireScript = "var $m = require('mochiscript').mochi; $m.PUSH_ROOT(root);";
var endScript = "$m.POP_ROOT();";
if (require.extensions) {

0 comments on commit 4a0c88d

Please sign in to comment.