Code coverage report for loader/lib/dev.js

Statements: 100% (67 / 67)      Branches: 100% (34 / 34)      Functions: 100% (11 / 11)      Lines: 100% (67 / 67)      Ignored: none     

All files » loader/lib/ » dev.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1171 1 1   1 1 1         1 1 6 1   5 5 1   4 4 2 2 2 1   1 1     2 2 1   1     1                 1 1 6 1   5 5 1   4 4 2 2 2 1   1 1     2 2 1   1     1                 1 1 6 1   5 5 1   4 4 2 2 2 1   1 1     2 2 2   1   1     1        
var fs = require('fs');
var url = require('url');
var path = require('path');
 
var less = require('less');
var stylus = require('stylus');
var coffee = require('coffee-script');
 
/**
 * 记得在static中间件之前使用,否则会被静态文件中间件处理
 */
exports.less = function (root) {
  return function (req, res, next) {
    if ('GET' !== req.method && 'HEAD' !== req.method) {
      return next();
    }
    var pathname = url.parse(req.originalUrl).pathname;
    if (!pathname.match(/\.less$/)) {
      return next();
    }
    fs.readFile(path.join(root, pathname), 'utf8', function (err, content) {
      if (err) {
        res.statusCode = 404;
        res.setHeader('Content-Type', 'text/html');
        if ('HEAD' === req.method) {
          return res.end();
        }
        res.end('Cannot find ' + req.originalUrl + '\n');
        return;
      }
      // 调用less将源文件内容翻译为CSS
      less.render(content, function (err, css) {
        if (err) {
          return next(err);
        }
        res.writeHead(200, {
          'Content-Type': 'text/css'
        });
        res.end(css);
      });
    });
  };
};
 
/**
 * 记得在static中间件之前使用,否则会被静态文件中间件处理
 */
exports.stylus = function (root) {
  return function (req, res, next) {
    if ('GET' !== req.method && 'HEAD' !== req.method) {
      return next();
    }
    var pathname = url.parse(req.originalUrl).pathname;
    if (!pathname.match(/\.styl$/)) {
      return next();
    }
    fs.readFile(path.join(root, pathname), 'utf8', function (err, content) {
      if (err) {
        res.statusCode = 404;
        res.setHeader('Content-Type', 'text/html');
        if ('HEAD' === req.method) {
          return res.end();
        }
        res.end('Cannot find ' + req.originalUrl + '\n');
        return;
      }
      // 调用stylus将源文件内容翻译为CSS
      stylus(content).render(function (err, css) {
        if (err) {
          return next(err);
        }
        res.writeHead(200, {
          'Content-Type': 'text/css'
        });
        res.end(css);
      });
    });
  };
};
 
/**
 * 记得在static中间件之前使用,否则会被静态文件中间件处理
 */
exports.coffee = function (root) {
  return function (req, res, next) {
    if ('GET' !== req.method && 'HEAD' !== req.method) {
      return next();
    }
    var pathname = url.parse(req.originalUrl).pathname;
    if (!pathname.match(/\.coffee$/)) {
      return next();
    }
    fs.readFile(path.join(root, pathname), 'utf8', function (err, content) {
      if (err) {
        res.statusCode = 404;
        res.setHeader('Content-Type', 'text/html');
        if ('HEAD' === req.method) {
          return res.end();
        }
        res.end('Cannot find ' + req.originalUrl + '\n');
        return;
      }
      // 调用coffee-script编译源文件
      var output;
      try {
        output = coffee.compile(content);
      } catch (ex) {
        return next(ex);
      }
      res.writeHead(200, {
        'Content-Type': 'text/javascript'
      });
      res.end(output);
    });
  };
};