UNPKG

2.67 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.10.0
2var browserify, express, expressCjs, fs, jade, koutoSwiss, path, stylus;
3
4express = require('express');
5
6browserify = require('browserify-middleware');
7
8jade = require('jade');
9
10stylus = require('stylus');
11
12koutoSwiss = require('kouto-swiss');
13
14path = require('path');
15
16fs = require('fs');
17
18expressCjs = function(arg) {
19 var browserifyMiddleware, cjs, cssCache, debug, ref, root, staticServe;
20 ref = arg != null ? arg : {}, root = ref.root, debug = ref.debug, staticServe = ref.staticServe;
21 cjs = express.Router();
22 if (root == null) {
23 root = process.cwd();
24 }
25 if (debug == null) {
26 debug = process.env.NODE_ENV !== 'production';
27 }
28 if (staticServe == null) {
29 staticServe = true;
30 }
31 cjs.get('/', function(req, res) {
32 var content, filename, options;
33 filename = root + path.sep + 'index.jade';
34 options = {
35 pretty: debug,
36 cache: !debug
37 };
38 content = jade.renderFile(filename, options);
39 res.set('Content-type', 'text/html');
40 res.set('Content-length', Buffer.byteLength(content, 'utf8'));
41 return res.end(content);
42 });
43 browserify.settings('transform', [
44 'coffeeify', [
45 'browserify-ngannotate', {
46 ext: '.coffee'
47 }
48 ], 'browserify-plain-jade'
49 ]);
50 browserifyMiddleware = browserify(root + path.sep + 'index.coffee', {
51 extensions: ['.coffee'],
52 bundleExternal: false
53 });
54 cjs.get('/index.js', browserifyMiddleware);
55 cssCache = null;
56 cjs.get('/index.css', function(req, res) {
57 var styl, stylData;
58 if (cssCache != null) {
59 res.set('Content-type', 'text/css');
60 res.set('Content-length', Buffer.byteLength(cssCache, 'utf8'));
61 res.end(cssCache);
62 return;
63 }
64 styl = root + path.sep + 'index.styl';
65 stylData = fs.readFileSync(styl, 'utf8');
66 return stylus(stylData).set('filename', styl).set('sourcemap', {
67 inline: true
68 }).set('compress', !debug).use(koutoSwiss())["import"]('kouto-swiss').render(function(error, css) {
69 if (error != null) {
70 throw error;
71 }
72 if (!debug) {
73 if (cssCache == null) {
74 cssCache = css;
75 }
76 }
77 res.set('Content-type', 'text/css');
78 res.set('Content-length', Buffer.byteLength(css, 'utf8'));
79 return res.end(css);
80 });
81 });
82 if (staticServe) {
83 cjs.use(function(req, res, next) {
84 if (req.url.match(/.gz$/)) {
85 res.set('Content-Encoding', 'gzip');
86 }
87 if (req.url.match(/.js.gz$/)) {
88 res.set('Content-Type', 'text/javascript');
89 }
90 return next();
91 });
92 cjs.use(express["static"](root));
93 }
94 return cjs;
95};
96
97module.exports = expressCjs;