1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Change compileWithSourceMap() so it returns an object instead of an Array, and return the SourceMap object.

This commit is contained in:
Jason Walton 2013-03-01 11:34:39 -05:00
parent d626e70287
commit ad0306b00c
4 changed files with 40 additions and 25 deletions

View file

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.5.0
(function() {
var Lexer, baseFileName, compile, count, ext, extensions, fs, lexer, loadFile, parser, path, sourcemap, vm, _i, _len,
var Lexer, baseFileName, compile, count, ext, extend, extensions, fs, lexer, loadFile, parser, path, sourcemap, vm, _i, _len, _ref,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
__hasProp = {}.hasOwnProperty;
@ -16,7 +16,7 @@
vm = require('vm');
count = require('./helpers').count;
_ref = require('./helpers'), count = _ref.count, extend = _ref.extend;
extensions = ['.coffee', '.litcoffee'];
@ -85,18 +85,23 @@
};
exports.compileWithSourceMap = function(code, options) {
var coffeeFile, js, jsFile, map, merge;
var coffeeFile, compiledJs, jsFile, merge, v3SourceMap;
if (options == null) {
options = {};
}
merge = exports.helpers.merge;
try {
options = extend({}, options);
options.sourceMap = new sourcemap.SourceMap();
coffeeFile = path.basename(options.filename);
jsFile = baseFileName(options.filename) + ".js";
js = exports.compile(code, options);
map = sourcemap.generateV3SourceMap(options.sourceMap, coffeeFile, jsFile);
return [js, map];
compiledJs = exports.compile(code, options);
v3SourceMap = sourcemap.generateV3SourceMap(options.sourceMap, coffeeFile, jsFile);
return {
compiledJs: compiledJs,
v3SourceMap: v3SourceMap,
sourceMap: options.sourceMap
};
} catch (err) {
if (options.filename) {
err.message = "In " + options.filename + ", " + err.message;
@ -118,7 +123,7 @@
};
exports.run = function(code, options) {
var mainModule, _ref;
var mainModule, _ref1;
if (options == null) {
options = {};
}
@ -126,7 +131,7 @@
mainModule.filename = process.argv[1] = options.filename ? fs.realpathSync(options.filename) : '.';
mainModule.moduleCache && (mainModule.moduleCache = {});
mainModule.paths = require('module')._nodeModulePaths(path.dirname(fs.realpathSync(options.filename)));
if ((_ref = path.extname(mainModule.filename), __indexOf.call(extensions, _ref) < 0) || require.extensions) {
if ((_ref1 = path.extname(mainModule.filename), __indexOf.call(extensions, _ref1) < 0) || require.extensions) {
return mainModule._compile(compile(code, options), mainModule.filename);
} else {
return mainModule._compile(code, mainModule.filename);
@ -134,7 +139,7 @@
};
exports["eval"] = function(code, options) {
var Module, Script, js, k, o, r, sandbox, v, _j, _len1, _module, _ref, _ref1, _require;
var Module, Script, js, k, o, r, sandbox, v, _j, _len1, _module, _ref1, _ref2, _require;
if (options == null) {
options = {};
}
@ -148,10 +153,10 @@
sandbox = options.sandbox;
} else {
sandbox = Script.createContext();
_ref = options.sandbox;
for (k in _ref) {
if (!__hasProp.call(_ref, k)) continue;
v = _ref[k];
_ref1 = options.sandbox;
for (k in _ref1) {
if (!__hasProp.call(_ref1, k)) continue;
v = _ref1[k];
sandbox[k] = v;
}
}
@ -168,9 +173,9 @@
return Module._load(path, _module, true);
};
_module.filename = sandbox.__filename;
_ref1 = Object.getOwnPropertyNames(require);
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
r = _ref1[_j];
_ref2 = Object.getOwnPropertyNames(require);
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
r = _ref2[_j];
if (r !== 'paths') {
_require[r] = require[r];
}

View file

@ -152,7 +152,7 @@
};
compileScript = function(file, input, base) {
var o, options, t, task, _ref1;
var compiled, o, options, t, task;
o = opts;
options = compileOptions(file);
try {
@ -173,7 +173,9 @@
return compileJoin();
} else {
if (o.maps) {
_ref1 = CoffeeScript.compileWithSourceMap(t.input, t.options), t.output = _ref1[0], t.sourceMap = _ref1[1];
compiled = CoffeeScript.compileWithSourceMap(t.input, t.options);
t.output = compiled.compiledJs;
t.sourceMap = compiled.v3SourceMap;
} else {
t.output = CoffeeScript.compile(t.input, t.options);
}

View file

@ -12,7 +12,7 @@ path = require 'path'
{parser} = require './parser'
sourcemap = require './sourcemap'
vm = require 'vm'
{count} = require './helpers'
{count, extend} = require './helpers'
# The file extensions that are considered to be CoffeeScript.
extensions = ['.coffee', '.litcoffee']
@ -70,17 +70,23 @@ exports.compile = compile = (code, options = {}) ->
"// #{header}\n#{js}"
# Generates a compiled code and a source map for a string of CoffeeScript code.
# Callers should specifiy `options.filename`. Returns a `[sourcemap, compiledCode]` pair, where
# sourcemap is a v3 source map.
# Callers should specifiy `options.filename`. Returns a `{compiledJs, v3SourceMap, sourceMap}
# object, where sourceMap is a sourcemap.coffee#SourceMap object, handy for doing programatic
# lookups.
exports.compileWithSourceMap = (code, options={}) ->
{merge} = exports.helpers
try
options = extend {}, options
options.sourceMap = new sourcemap.SourceMap()
coffeeFile = path.basename options.filename
jsFile = baseFileName(options.filename) + ".js"
js = exports.compile code, options
map = sourcemap.generateV3SourceMap options.sourceMap, coffeeFile, jsFile
return [js, map]
compiledJs = exports.compile code, options
v3SourceMap = sourcemap.generateV3SourceMap options.sourceMap, coffeeFile, jsFile
return {
compiledJs,
v3SourceMap,
sourceMap: options.sourceMap
}
catch err
err.message = "In #{options.filename}, #{err.message}" if options.filename
throw err

View file

@ -127,7 +127,9 @@ compileScript = (file, input, base) ->
compileJoin()
else
if o.maps
[t.output, t.sourceMap] = CoffeeScript.compileWithSourceMap t.input, t.options
compiled = CoffeeScript.compileWithSourceMap t.input, t.options
t.output = compiled.compiledJs
t.sourceMap = compiled.v3SourceMap
else
t.output = CoffeeScript.compile t.input, t.options