Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Better support for coffeescript

  • Loading branch information...
commit 974d4c745a21b674b745518effc9e30a6fbda7f0 1 parent cc1840b
Ryan Williams authored September 16, 2012
15  lib/jah/commands/server.js
@@ -12,8 +12,8 @@ var options = [
12 12
         description: 'URL to serve the JavaScript as. Default is output defined in the config file',
13 13
         value: true },
14 14
 
15  
-    {   short: 'j',
16  
-        long: 'jah-config',
  15
+    {   short: 'c',
  16
+        long: 'config',
17 17
         description: 'Project configuration file. Default is jah.json',
18 18
         value: true },
19 19
 
@@ -25,12 +25,7 @@ var options = [
25 25
     {   short: 'p',
26 26
         long: 'port',
27 27
         description: 'Port to listen on. Default is 4000',
28  
-        value: true },
29  
-
30  
-    {   short: 'c',
31  
-        long: 'coffee',
32  
-        description: 'Compile src with coffeescript automatically',
33  
-        value: false }
  28
+        value: true }
34 29
 ];
35 30
 
36 31
 exports.description = 'Run the Jah development web server';
@@ -38,8 +33,8 @@ exports.run = function () {
38 33
     opts.parse(options, true);
39 34
     var host     = opts.get('host')   || '127.0.0.1',
40 35
         port     = opts.get('port')   || 4000,
41  
-        config   = opts.get('jah-config') || 'jah.json',
  36
+        config   = opts.get('config') || 'jah.json',
42 37
         server   = new Server(config)
43 38
 
44  
-    server.start(host, port, opts.get('coffee'))
  39
+    server.start(host, port)
45 40
 };
24  lib/jah/compiler.js
@@ -8,12 +8,14 @@ var sys       = require('util')
8 8
   , mkdir     = require('./copytree').mkdir
9 9
   , T         = require('./template').Template
10 10
   , mimetypes = require('./mimetypes')
  11
+  , coffee    = require('coffee-script')
11 12
 
12 13
 var DEFAULT_JAH_JSON = { mainModule:  'main'
13 14
                        , resourceURL: 'resources'
14 15
                        , sourcePath:  'src'
15 16
                        , output:      '.'
16 17
                        , assetPath:   'assets'
  18
+                       , compileCoffee: true
17 19
                        , externalize: {}
18 20
                        }
19 21
 
@@ -202,6 +204,7 @@ Compiler.prototype.build = function () {
202 204
 }
203 205
 
204 206
 Compiler.prototype.copyPublicAssets = function (src, dst, callback) {
  207
+    console.log(src)
205 208
     var filenames = fs.readdirSync(src)
206 209
       , current = 0
207 210
 
@@ -259,6 +262,7 @@ Compiler.prototype.copyAssets = function (dst, callback) {
259 262
             return
260 263
         }
261 264
 
  265
+
262 266
         var input = filenames[current]
263 267
           , output = path.join(dst, assets[input])
264 268
 
@@ -395,8 +399,8 @@ Compiler.prototype.collectAssetFilenames = function (filename, mountRoot) {
395 399
         mimetype = mimetypes.guessType(fullPath)
396 400
 
397 401
         if (fs.statSync(fullPath).isFile()) {
398  
-            // If not a javascript file, then it's an asset
399  
-            if (mimetype != 'application/javascript') {
  402
+            // If not a javascript or coffeescript file, then it's an asset
  403
+            if (!(this.config.compileCoffee && mimetype == 'text/x-coffeescript') && mimetype != 'application/javascript') {
400 404
                 foundFiles[fullPath] = mount
401 405
             }
402 406
         } else {
@@ -507,15 +511,27 @@ Compiler.prototype.buildFile = function (filename, mount, tight, skipModules) {
507 511
         }
508 512
     }
509 513
 
510  
-    logger.info('Build ' + 'File'.underline, filename + ' => '.yellow + mount)
511  
-
512 514
     var mimetype = mimetypes.guessType(filename)
513 515
       , data = ''
514 516
 
  517
+
  518
+    if (this.config.compileCoffee && mimetype == 'text/x-coffeescript') {
  519
+        mount = mount.replace(/\.coffee$/, '.js')
  520
+    }
  521
+
  522
+    logger.info('Build ' + 'File'.underline, filename + ' => '.yellow + mount)
  523
+
  524
+
515 525
     switch (mimetype) {
516 526
     case 'application/javascript':
517 527
         data = this.wrapModule(fs.readFileSync(filename, 'utf8'), mount, tight)
518 528
         break;
  529
+    case 'text/x-coffeescript':
  530
+        if (this.config.compileCoffee) {
  531
+            data = this.wrapModule(coffee.compile(fs.readFileSync(filename, 'utf8'), {bare: true}), mount, tight)
  532
+            break;
  533
+        }
  534
+        // Fallthrough to default if not compiling coffeescript
519 535
     default:
520 536
         data = this.wrapAsset(filename, mount, mimetype)
521 537
         break;
1  lib/jah/mime.types
@@ -1068,3 +1068,4 @@ video/x-ms-wvx					wvx
1068 1068
 video/x-msvideo					avi
1069 1069
 video/x-sgi-movie				movie
1070 1070
 x-conference/x-cooltalk				ice
  1071
+text/x-coffeescript				coffee
20  lib/jah/server.js
@@ -14,29 +14,11 @@ function Server (config) {
14 14
     this.compiler = new Compiler(config)
15 15
 }
16 16
 
17  
-Server.prototype.watchForCoffeeScript = function (src, dest) {
18  
-    src  = src  || this.compiler.config.sourcePath
19  
-    dest = dest || 'build/js'
20  
-    this.compiler.config.sourcePath = dest
21  
-
22  
-    var coffee = child_process.spawn('coffee', ['-w', '-c', '-o', dest, src])
23  
-    coffee.stderr.on('data', function (data) {
24  
-      process.stderr.write(data.toString())
25  
-    })
26  
-    coffee.stdout.on('data', function (data) {
27  
-      sys.print(data.toString())
28  
-    })
29  
-}
30  
-
31 17
 
32  
-Server.prototype.start = function (host, port, coffeescript) {
  18
+Server.prototype.start = function (host, port) {
33 19
     host = host || '127.0.0.1'
34 20
     port = port || 4000
35 21
 
36  
-    if (coffeescript) {
37  
-        this.watchForCoffeeScript();
38  
-    }
39  
-
40 22
     http.createServer(function (req, res) {
41 23
         var uri = url.parse(req.url, true)
42 24
         logger.group('Request', uri.pathname)
5  package.json
@@ -2,7 +2,7 @@
2 2
     "name": "jah",
3 3
     "author": "Ryan Williams <ryan@wigg.ly>",
4 4
     "description": "Enable CommonJS in browser applications",
5  
-    "version": "0.3.0",
  5
+    "version": "0.4.0",
6 6
     "homepage": "https://github.com/ryanwilliams/jah",
7 7
 
8 8
     "engines": {
@@ -26,7 +26,8 @@
26 26
     }],
27 27
 
28 28
     "dependencies": {
29  
-        "colors": ">= 0.5.1"
  29
+        "colors": ">= 0.5.1",
  30
+        "coffee-script": ">= 1.3.3"
30 31
     },
31 32
 
32 33
     "bunledDependencies": [

0 notes on commit 974d4c7

Please sign in to comment.
Something went wrong with that request. Please try again.