Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(965)

Side by Side Diff: lib/server.js

Issue 6853044: Resource minification/aggregation css
Patch Set: Created 5 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« bin/merge-files ('K') | « lib/merge-files.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 'use strict'; 1 'use strict';
2 2
3 var express = require('express'), 3 var express = require('express'),
4 server = express(), 4 server = express(),
5 fs = require('fs'), 5 fs = require('fs'),
6 path = require('path'), 6 path = require('path'),
7 config = require('../config').config.server, 7 config = require('../config').config.server,
8 debugMode = (String(process.argv[2]).toLowerCase() === 'debug'), 8 debugMode = (String(process.argv[2]).toLowerCase() === 'debug'),
9 public_dir = config.public_dir, 9 public_dir = config.public_dir,
10 Templates = require('./templates.js'), 10 Templates = require('./templates.js'),
11 view = require('./view.js'); 11 view = require('./view.js');
12 12
13 13
14 /**
15 * It finds a file under the given path. The first match wins.
16 * @param {string} path starting point.
17 * @param {string} fileName name of the file to be found.
18 * @return {string} the file path.
19 */
20 function findFile(path, fileName) {
21 var file, files = fs.readdirSync(path);
22 for (var i = 0; i < files.length; i = i + 1) {
23 if (files[i] === fileName) {
24 return path + '/' + files[i];
25 }
26 file = fs.statSync(path + '/' + files[i]);
27 if (file.isDirectory()) {
28 file = findFile(path + '/' + files[i], fileName);
29 if (file) {
30 return file;
31 }
32 }
33 }
34 return null;
35 }
36
14 server.configure(function() { 37 server.configure(function() {
15 server.set('views', __dirname + './lib/views/'); 38 server.set('views', __dirname + './lib/views/');
16 server.set('view engine', 'handlebars'); 39 server.set('view engine', 'handlebars');
17 server.set('view options', {layout: false}); 40 server.set('view options', {layout: false});
18 server.engine('handlebars', view.handlebars); 41 server.engine('handlebars', view.handlebars);
19 42
20 server.use(express.logger('dev')); 43 server.use(express.logger('dev'));
21 // 'static' is a reserved word so dot notation is not used to 44 // 'static' is a reserved word so dot notation is not used to
22 // avoid annoying the linter. 45 // avoid annoying the linter.
23 server.use('/juju-ui', express['static'](public_dir)); 46 server.use('/juju-ui', express['static'](public_dir));
(...skipping 20 matching lines...) Expand all
44 res.json({ 67 res.json({
45 uptime: process.uptime(), 68 uptime: process.uptime(),
46 memory: process.memoryUsage() 69 memory: process.memoryUsage()
47 }); 70 });
48 }); 71 });
49 72
50 server.get('/assets/all-third.js', function(req, res) { 73 server.get('/assets/all-third.js', function(req, res) {
51 res.sendfile('app/assets/javascripts/generated/all-third.js'); 74 res.sendfile('app/assets/javascripts/generated/all-third.js');
52 }); 75 });
53 76
77 server.get('/assets/stylesheets/:file', function(req, res) {
frankban 2012/11/13 17:27:44 We discussed this on IRC: adding a 'juju-ui' prefi
thiago 2012/11/13 18:19:17 Done.
78 var file, fileName = req.params.file;
79 if (path.extname(fileName).toLowerCase() === '.css') {
80 res.sendfile('app/assets/stylesheets/' + fileName);
81 } else {
82 // We've merged all the yui and third party files into a single css file.
83 // YUI expects to load its images from the same path as its css files, so
84 // we need to mock this position. When the system tries to load a resource
85 // from "app/assets/stylesheets/", it tries to find this resource under the
86 // "./app/assets" directory. The first match wins. Another solution would
87 // be to manually copy all the images we need to the
88 // "./app/assets/stylesheets" directory.
matthew.scott 2012/11/13 17:08:24 I think this is a good way to do it, so long as th
thiago 2012/11/13 18:19:17 This is lightning fast.
89 // This is an example of image...
90 // ./yui/datatable-sort/assets/skins/sam/sort-arrow-sprite-ie.png
91 file = findFile('./app/assets', fileName);
92 res.sendfile(file);
93 }
94 });
95
54 server.get('/assets/modules.js', function(req, res) { 96 server.get('/assets/modules.js', function(req, res) {
55 if (debugMode) { 97 if (debugMode) {
56 res.sendfile('app/assets/javascripts/generated/all-app-debug.js'); 98 res.sendfile('app/assets/javascripts/generated/all-app-debug.js');
57 } else { 99 } else {
58 res.sendfile('app/assets/javascripts/generated/all-app.js'); 100 res.sendfile('app/assets/javascripts/generated/all-app.js');
59 } 101 }
60 }); 102 });
61 103
62 server.get('/assets/yui.js', function(req, res) { 104 server.get('/assets/yui.js', function(req, res) {
63 if (debugMode) { 105 if (debugMode) {
64 res.sendfile('app/assets/javascripts/yui/yui/yui-debug.js'); 106 res.sendfile('app/assets/javascripts/yui/yui/yui-debug.js');
65 } else { 107 } else {
66 res.sendfile('app/assets/javascripts/generated/all-yui.js'); 108 res.sendfile('app/assets/javascripts/generated/all-yui.js');
67 } 109 }
68 }); 110 });
69 111
70 server.get('*', function(req, res) { 112 server.get('*', function(req, res) {
71 res.sendfile('app/index.html'); 113 res.sendfile('app/index.html');
72 }); 114 });
73 115
74 exports.server = server; 116 exports.server = server;
OLDNEW
« bin/merge-files ('K') | « lib/merge-files.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 204d58d