Permalink
Browse files

Better support for coffeescript

  • Loading branch information...
1 parent cc1840b commit 974d4c745a21b674b745518effc9e30a6fbda7f0 @ryanwilliams committed Sep 16, 2012
Showing with 30 additions and 35 deletions.
  1. +5 −10 lib/jah/commands/server.js
  2. +20 −4 lib/jah/compiler.js
  3. +1 −0 lib/jah/mime.types
  4. +1 −19 lib/jah/server.js
  5. +3 −2 package.json
@@ -12,8 +12,8 @@ var options = [
description: 'URL to serve the JavaScript as. Default is output defined in the config file',
value: true },
- { short: 'j',
- long: 'jah-config',
+ { short: 'c',
+ long: 'config',
description: 'Project configuration file. Default is jah.json',
value: true },
@@ -25,21 +25,16 @@ var options = [
{ short: 'p',
long: 'port',
description: 'Port to listen on. Default is 4000',
- value: true },
-
- { short: 'c',
- long: 'coffee',
- description: 'Compile src with coffeescript automatically',
- value: false }
+ value: true }
];
exports.description = 'Run the Jah development web server';
exports.run = function () {
opts.parse(options, true);
var host = opts.get('host') || '127.0.0.1',
port = opts.get('port') || 4000,
- config = opts.get('jah-config') || 'jah.json',
+ config = opts.get('config') || 'jah.json',
server = new Server(config)
- server.start(host, port, opts.get('coffee'))
+ server.start(host, port)
};
View
@@ -8,12 +8,14 @@ var sys = require('util')
, mkdir = require('./copytree').mkdir
, T = require('./template').Template
, mimetypes = require('./mimetypes')
+ , coffee = require('coffee-script')
var DEFAULT_JAH_JSON = { mainModule: 'main'
, resourceURL: 'resources'
, sourcePath: 'src'
, output: '.'
, assetPath: 'assets'
+ , compileCoffee: true
, externalize: {}
}
@@ -202,6 +204,7 @@ Compiler.prototype.build = function () {
}
Compiler.prototype.copyPublicAssets = function (src, dst, callback) {
+ console.log(src)
var filenames = fs.readdirSync(src)
, current = 0
@@ -259,6 +262,7 @@ Compiler.prototype.copyAssets = function (dst, callback) {
return
}
+
var input = filenames[current]
, output = path.join(dst, assets[input])
@@ -395,8 +399,8 @@ Compiler.prototype.collectAssetFilenames = function (filename, mountRoot) {
mimetype = mimetypes.guessType(fullPath)
if (fs.statSync(fullPath).isFile()) {
- // If not a javascript file, then it's an asset
- if (mimetype != 'application/javascript') {
+ // If not a javascript or coffeescript file, then it's an asset
+ if (!(this.config.compileCoffee && mimetype == 'text/x-coffeescript') && mimetype != 'application/javascript') {
foundFiles[fullPath] = mount
}
} else {
@@ -507,15 +511,27 @@ Compiler.prototype.buildFile = function (filename, mount, tight, skipModules) {
}
}
- logger.info('Build ' + 'File'.underline, filename + ' => '.yellow + mount)
-
var mimetype = mimetypes.guessType(filename)
, data = ''
+
+ if (this.config.compileCoffee && mimetype == 'text/x-coffeescript') {
+ mount = mount.replace(/\.coffee$/, '.js')
+ }
+
+ logger.info('Build ' + 'File'.underline, filename + ' => '.yellow + mount)
+
+
switch (mimetype) {
case 'application/javascript':
data = this.wrapModule(fs.readFileSync(filename, 'utf8'), mount, tight)
break;
+ case 'text/x-coffeescript':
+ if (this.config.compileCoffee) {
+ data = this.wrapModule(coffee.compile(fs.readFileSync(filename, 'utf8'), {bare: true}), mount, tight)
+ break;
+ }
+ // Fallthrough to default if not compiling coffeescript
default:
data = this.wrapAsset(filename, mount, mimetype)
break;
View
@@ -1068,3 +1068,4 @@ video/x-ms-wvx wvx
video/x-msvideo avi
video/x-sgi-movie movie
x-conference/x-cooltalk ice
+text/x-coffeescript coffee
View
@@ -14,29 +14,11 @@ function Server (config) {
this.compiler = new Compiler(config)
}
-Server.prototype.watchForCoffeeScript = function (src, dest) {
- src = src || this.compiler.config.sourcePath
- dest = dest || 'build/js'
- this.compiler.config.sourcePath = dest
-
- var coffee = child_process.spawn('coffee', ['-w', '-c', '-o', dest, src])
- coffee.stderr.on('data', function (data) {
- process.stderr.write(data.toString())
- })
- coffee.stdout.on('data', function (data) {
- sys.print(data.toString())
- })
-}
-
-Server.prototype.start = function (host, port, coffeescript) {
+Server.prototype.start = function (host, port) {
host = host || '127.0.0.1'
port = port || 4000
- if (coffeescript) {
- this.watchForCoffeeScript();
- }
-
http.createServer(function (req, res) {
var uri = url.parse(req.url, true)
logger.group('Request', uri.pathname)
View
@@ -2,7 +2,7 @@
"name": "jah",
"author": "Ryan Williams <ryan@wigg.ly>",
"description": "Enable CommonJS in browser applications",
- "version": "0.3.0",
+ "version": "0.4.0",
"homepage": "https://github.com/ryanwilliams/jah",
"engines": {
@@ -26,7 +26,8 @@
}],
"dependencies": {
- "colors": ">= 0.5.1"
+ "colors": ">= 0.5.1",
+ "coffee-script": ">= 1.3.3"
},
"bunledDependencies": [

0 comments on commit 974d4c7

Please sign in to comment.