Permalink
Browse files

Added support for compiling less files on the server using express-co…

…mpiless (--compiless).

documentRoot => root
  • Loading branch information...
1 parent f69cf97 commit 3f397bd6b2d27f0882430817d10117984aae3663 @papandreou papandreou committed Oct 11, 2012
Showing with 49 additions and 31 deletions.
  1. +6 −0 README.md
  2. +17 −11 bin/livestyle
  3. +11 −6 lib/createLiveStyleApp.js
  4. +2 −2 lib/installLiveCssFileWatcherInServer.js
  5. +1 −0 package.json
  6. +3 −3 test/nonProxy-test.js
  7. +9 −9 test/proxy-test.js
View
6 README.md
@@ -64,6 +64,12 @@ switch: `--map /foo/=/bar/`
Multiple --map switches are allowed. When used in conjunction with
`--proxy`, the mappings are applied before proxying the request.
+#### --compiless
+
+Compile less assets on-the-fly using <a
+href="https://github.com/papandreou/express-compiless">express-compiless</a>. Also
+supports watching @import'ed less assets.
+
#### --debug|-d
Outputs a bunch of debugging information on both the server and the
View
28 bin/livestyle
@@ -8,12 +8,17 @@ var express = require('express'),
mappings = {},
optimist = require('optimist'),
commandLineOptions = optimist
- .usage('Usage: $0 [--proxy <remotehost> [-m <directoryMapping>]] [-d] [-h <hostname>] [-p <port>] [-r documentRoot]')
+ .usage('Usage: $0 [--proxy <remotehost> [-m <directoryMapping>]] [-d] [-h <hostname>] [-p <port>] [--root directory]')
.boolean('d')
.options('root', {
alias: 'r',
describe: 'The directory to serve static files from. Defaults to the current working directory unless --proxy is specified.'
})
+ .options('compiless', {
+ type: 'boolean',
+ describe: 'Whether to add the express-compiless middleware to the stack (compiles less files and serves it them as text/css).',
+ default: false
+ })
.options('debug', {
alias: 'd',
describe: 'Turn on debug mode (server logs events to stderr, client logs to console).'
@@ -23,7 +28,7 @@ var express = require('express'),
})
.options('map', {
alias: 'm',
- describe: 'Directory mappings of the form: remoteDir=localDir where localDir is relative to documentRoot unless prefixed with /'
+ describe: 'Directory mappings of the form: remoteDir=localDir where localDir is relative to the root directory unless prefixed with /'
})
.options('host', {
alias: 'h',
@@ -36,7 +41,7 @@ var express = require('express'),
describe: 'The local post number to listen on'
})
.options('watchfile', {
- describe: 'force using watchFile'
+ describe: 'force using fs.watchFile instead of fs.watch (works better on some OSes)'
})
.options('mtime', {
describe: 'suppress change events unless the mtime of a file has changed (experimental, fs.watch only)'
@@ -54,12 +59,12 @@ var express = require('express'),
}
})
.argv,
- documentRoot;
+ root;
if (commandLineOptions.root) {
- documentRoot = path.resolve(process.cwd(), commandLineOptions.root);
+ root = path.resolve(process.cwd(), commandLineOptions.root);
} else if (!commandLineOptions.proxy) {
- documentRoot = process.cwd();
+ root = process.cwd();
}
if (commandLineOptions.help) {
@@ -69,11 +74,11 @@ if (commandLineOptions.help) {
if (commandLineOptions.proxy) {
console.log('Proxying to ' + commandLineOptions.proxy);
- if (documentRoot) {
- console.log('Serving static CSS files from ' + documentRoot);
+ if (root) {
+ console.log('Serving static CSS files from ' + root);
}
-} else if (documentRoot) {
- console.log('Serving static files from ' + documentRoot);
+} else if (root) {
+ console.log('Serving static files from ' + root);
}
console.log('Listening to http://' + commandLineOptions.host + ':' + commandLineOptions.port + '/');
@@ -82,7 +87,8 @@ require('../lib/createLiveStyleApp')({
debug: commandLineOptions.debug,
mtime: commandLineOptions.mtime,
watchfile: commandLineOptions.watchfile,
- documentRoot: documentRoot,
+ root: root,
+ compiless: commandLineOptions.compiless,
mappings: mappings,
proxy: commandLineOptions.proxy || null
}).listen(commandLineOptions.port, commandLineOptions.host);
View
17 lib/createLiveStyleApp.js
@@ -1,6 +1,7 @@
var URL = require('url'),
express = require('express'),
- request = require('request');
+ request = require('request'),
+ compiless = require('express-compiless');
module.exports = function createLiveStyleApp(options) {
var app = express.createServer(),
@@ -34,8 +35,12 @@ module.exports = function createLiveStyleApp(options) {
});
}
- if (options.documentRoot) {
- var staticProvider = express['static'](options.documentRoot);
+ if (options.root) {
+ if (options.compiless) {
+ app.use(compiless({root: options.root}));
+ }
+
+ var staticProvider = express['static'](options.root);
app.use(function (req, res, next) {
if (!proxyUrl || req.accepts('css') || req.accepts('text/x-less')) {
staticProvider(req, res, next);
@@ -82,10 +87,10 @@ module.exports = function createLiveStyleApp(options) {
});
}
- if (options.documentRoot) {
- require('../lib/installLiveCssFileWatcherInServer')(app, {
+ if (options.root) {
+ require('./installLiveCssFileWatcherInServer')(app, {
debug: options.debug,
- documentRoot: options.documentRoot,
+ root: options.root,
mtime: options.mtime,
watchfile: options.watchfile,
mappings: options.mappings
View
4 lib/installLiveCssFileWatcherInServer.js
@@ -124,7 +124,7 @@ module.exports = function (app, options, sio) {
mappedUrl = mappedUrl[0] === '/' ? mappedUrl.substr(1) : mappedUrl;
var rootRelativePath = decodeURIComponent(mappedUrl),
- fileName = path.resolve(options.documentRoot, rootRelativePath);
+ fileName = path.resolve(options.root, rootRelativePath);
rootRelativeUrlsByFileName[fileName] = rootRelativeUrlsByFileName[fileName] || [];
if (rootRelativeUrlsByFileName[fileName].indexOf(rootRelativeUrl) === -1) {
@@ -188,7 +188,7 @@ module.exports = function (app, options, sio) {
}
}
} else {
- // TODO: Poll the upstream server if the file isn't found on disc (or no documentRoot is specified)
+ // TODO: Poll the upstream server if the file isn't found on disc (or no root is specified)
if (options.debug) {
console.warn('Starting to watch ' + fileName + ' using fs.watchFile');
}
View
1 package.json
@@ -22,6 +22,7 @@
},
"dependencies": {
"express": "=2.5.8",
+ "express-compiless": "=0.0.7",
"express-hijackresponse": "=0.0.6",
"optimist": "=0.3.0",
"request": "=2.9.3",
View
6 test/nonProxy-test.js
@@ -14,7 +14,7 @@ vows.describe('livestyle server in non-proxy mode').addBatch({
'create a livestyle server in non-proxy mode, then request an HTML file': {
topic: function () {
var callback = this.callback,
- appInfo = createLiveStyleTestServer({documentRoot: path.resolve(__dirname, 'nonProxy')});
+ appInfo = createLiveStyleTestServer({root: path.resolve(__dirname, 'nonProxy')});
// Wait a couple of seconds for the server to become available
setTimeout(function () {
@@ -31,7 +31,7 @@ vows.describe('livestyle server in non-proxy mode').addBatch({
'create a livestyle server in non-proxy mode, subscribe to changes in styles.css, then overwrite it': {
topic: function () {
var callback = this.callback,
- appInfo = createLiveStyleTestServer({documentRoot: path.resolve(__dirname, 'nonProxy')}),
+ appInfo = createLiveStyleTestServer({root: path.resolve(__dirname, 'nonProxy')}),
cssFileName = path.resolve(__dirname, 'nonProxy/style sheet.css'),
changedFileNames = [];
@@ -71,7 +71,7 @@ vows.describe('livestyle server in non-proxy mode').addBatch({
topic: function () {
var callback = this.callback,
appInfo = createLiveStyleTestServer({
- documentRoot: path.resolve(__dirname, 'nonProxy'),
+ root: path.resolve(__dirname, 'nonProxy'),
mappings: {
'/fo%20o/': '/ba%20r/'
}
View
18 test/proxy-test.js
@@ -15,9 +15,9 @@ vows.describe('proxy mode test').addBatch({
'create a livestyle server in pure proxy mode and an upstream server, then request an HTML file': {
topic: function () {
var callback = this.callback,
- documentRoot = path.resolve(__dirname, 'proxy'),
+ root = path.resolve(__dirname, 'proxy'),
upstreamServer = express.createServer()
- .use(express['static'](documentRoot));
+ .use(express['static'](root));
upstreamServer.listen(0);
@@ -39,9 +39,9 @@ vows.describe('proxy mode test').addBatch({
'create a livestyle server in pure proxy mode and an upstream server, then request an HTML file with no </head>': {
topic: function () {
var callback = this.callback,
- documentRoot = path.resolve(__dirname, 'proxy'),
+ root = path.resolve(__dirname, 'proxy'),
upstreamServer = express.createServer()
- .use(express['static'](documentRoot));
+ .use(express['static'](root));
upstreamServer.listen(0);
@@ -63,9 +63,9 @@ vows.describe('proxy mode test').addBatch({
'create a livestyle server in pure proxy mode and an upstream server, then request an HTML file with no </head> and no </html>': {
topic: function () {
var callback = this.callback,
- documentRoot = path.resolve(__dirname, 'proxy'),
+ root = path.resolve(__dirname, 'proxy'),
upstreamServer = express.createServer()
- .use(express['static'](documentRoot));
+ .use(express['static'](root));
upstreamServer.listen(0);
@@ -87,7 +87,7 @@ vows.describe('proxy mode test').addBatch({
'create a livestyle server in pure proxy mode and an upstream server that redirects /subdir to /subdir/, then request a directory': {
topic: function () {
var callback = this.callback,
- documentRoot = path.resolve(__dirname, 'proxy'),
+ root = path.resolve(__dirname, 'proxy'),
upstreamServerUrl,
upstreamServer = express.createServer()
.use(function (req, res, next) {
@@ -124,9 +124,9 @@ vows.describe('proxy mode test').addBatch({
'create a livestyle server with a mapping from /fo%20o/ to /ba%20r/ along with an upstream server, then request /fo%20o/hello.txt': {
topic: function () {
var callback = this.callback,
- documentRoot = path.resolve(__dirname, 'proxy'),
+ root = path.resolve(__dirname, 'proxy'),
upstreamServer = express.createServer()
- .use(express['static'](documentRoot));
+ .use(express['static'](root));
upstreamServer.listen(0);

0 comments on commit 3f397bd

Please sign in to comment.