/ src / coffee-script.js
coffee-script.js
 1  'use strict';
 2  
 3  var crypto = require('crypto');
 4  var path = require('path');
 5  var CoffeeScript = null;
 6  
 7  exports.shouldCompile = function() {
 8    return true;
 9  };
10  
11  exports.getCachePath = function(sourceCode) {
12    return path.join(
13      'coffee',
14      crypto
15        .createHash('sha1')
16        .update(sourceCode, 'utf8')
17        .digest('hex') + '.js'
18    );
19  };
20  
21  exports.compile = function(sourceCode, filePath) {
22    if (!CoffeeScript) {
23      var previousPrepareStackTrace = Error.prepareStackTrace;
24      CoffeeScript = require('coffee-script');
25  
26      // When it loads, coffee-script reassigns Error.prepareStackTrace. We have
27      // already reassigned it via the 'source-map-support' module, so we need
28      // to set it back.
29      Error.prepareStackTrace = previousPrepareStackTrace;
30    }
31  
32    if (process.platform === 'win32') {
33      filePath = 'file:///' + path.resolve(filePath).replace(/\\/g, '/');
34    }
35  
36    var output = CoffeeScript.compile(sourceCode, {
37      filename: filePath,
38      sourceFiles: [filePath],
39      inlineMap: true
40    });
41  
42    // Strip sourceURL from output so there wouldn't be duplicate entries
43    // in devtools.
44    return output.replace(/\/\/# sourceURL=[^'"\n]+\s*$/, '');
45  };