Minor API changes.
This commit is contained in:
parent
96785872cd
commit
979e110a84
|
@ -46,21 +46,26 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.compile = compile = function(code, options) {
|
exports.compile = compile = function(code, options) {
|
||||||
var currentColumn, currentLine, fragment, fragments, header, js, merge, newLines, _j, _len1;
|
var answer, coffeeFile, currentColumn, currentLine, fragment, fragments, header, js, jsFile, merge, newLines, sourceMap, _j, _len1;
|
||||||
if (options == null) {
|
if (options == null) {
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
merge = exports.helpers.merge;
|
merge = exports.helpers.merge;
|
||||||
try {
|
try {
|
||||||
|
if (options.sourceMap) {
|
||||||
|
coffeeFile = path.basename(options.filename);
|
||||||
|
jsFile = baseFileName(options.filename) + ".js";
|
||||||
|
sourceMap = new sourcemap.SourceMap();
|
||||||
|
}
|
||||||
fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options);
|
fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options);
|
||||||
currentLine = 0;
|
currentLine = 0;
|
||||||
currentColumn = 0;
|
currentColumn = 0;
|
||||||
js = "";
|
js = "";
|
||||||
for (_j = 0, _len1 = fragments.length; _j < _len1; _j++) {
|
for (_j = 0, _len1 = fragments.length; _j < _len1; _j++) {
|
||||||
fragment = fragments[_j];
|
fragment = fragments[_j];
|
||||||
if (options.sourceMap) {
|
if (sourceMap) {
|
||||||
if (fragment.locationData) {
|
if (fragment.locationData) {
|
||||||
options.sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
|
sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
|
||||||
noReplace: true
|
noReplace: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -70,42 +75,27 @@
|
||||||
}
|
}
|
||||||
js += fragment.code;
|
js += fragment.code;
|
||||||
}
|
}
|
||||||
if (!options.header) {
|
|
||||||
return js;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (options.filename) {
|
if (options.filename) {
|
||||||
err.message = "In " + options.filename + ", " + err.message;
|
err.message = "In " + options.filename + ", " + err.message;
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
header = "Generated by CoffeeScript " + this.VERSION;
|
if (options.header) {
|
||||||
return "// " + header + "\n" + js;
|
header = "Generated by CoffeeScript " + this.VERSION;
|
||||||
};
|
js = "// " + header + "\n" + js;
|
||||||
|
|
||||||
exports.compileWithSourceMap = function(code, options) {
|
|
||||||
var coffeeFile, compiledJs, jsFile, merge, v3SourceMap;
|
|
||||||
if (options == null) {
|
|
||||||
options = {};
|
|
||||||
}
|
}
|
||||||
merge = exports.helpers.merge;
|
if (options.sourceMap || options.returnObject) {
|
||||||
try {
|
answer = {
|
||||||
options = helpers.extend({}, options);
|
js: js
|
||||||
options.sourceMap = new sourcemap.SourceMap();
|
|
||||||
coffeeFile = path.basename(options.filename);
|
|
||||||
jsFile = baseFileName(options.filename) + ".js";
|
|
||||||
compiledJs = exports.compile(code, options);
|
|
||||||
v3SourceMap = sourcemap.generateV3SourceMap(options.sourceMap, coffeeFile, jsFile);
|
|
||||||
return {
|
|
||||||
compiledJs: compiledJs,
|
|
||||||
v3SourceMap: v3SourceMap,
|
|
||||||
sourceMap: options.sourceMap
|
|
||||||
};
|
};
|
||||||
} catch (err) {
|
if (sourceMap) {
|
||||||
if (options.filename) {
|
answer.sourceMap = sourceMap;
|
||||||
err.message = "In " + options.filename + ", " + err.message;
|
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, coffeeFile, jsFile);
|
||||||
}
|
}
|
||||||
throw err;
|
return answer;
|
||||||
|
} else {
|
||||||
|
return js;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -168,13 +168,9 @@
|
||||||
sourceCode[sources.indexOf(t.file)] = t.input;
|
sourceCode[sources.indexOf(t.file)] = t.input;
|
||||||
return compileJoin();
|
return compileJoin();
|
||||||
} else {
|
} else {
|
||||||
if (o.maps) {
|
compiled = CoffeeScript.compile(t.input, t.options);
|
||||||
compiled = CoffeeScript.compileWithSourceMap(t.input, t.options);
|
t.output = compiled.js;
|
||||||
t.output = compiled.compiledJs;
|
t.sourceMap = compiled.v3SourceMap;
|
||||||
t.sourceMap = compiled.v3SourceMap;
|
|
||||||
} else {
|
|
||||||
t.output = CoffeeScript.compile(t.input, t.options);
|
|
||||||
}
|
|
||||||
CoffeeScript.emit('success', task);
|
CoffeeScript.emit('success', task);
|
||||||
if (o.print) {
|
if (o.print) {
|
||||||
return printLine(t.output.trim());
|
return printLine(t.output.trim());
|
||||||
|
@ -479,7 +475,9 @@
|
||||||
filename: filename,
|
filename: filename,
|
||||||
literate: helpers.isLiterate(filename),
|
literate: helpers.isLiterate(filename),
|
||||||
bare: opts.bare,
|
bare: opts.bare,
|
||||||
header: opts.compile
|
header: opts.compile,
|
||||||
|
sourceMap: opts.maps,
|
||||||
|
returnObject: true
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -34,11 +34,23 @@ baseFileName = (fileName) ->
|
||||||
extension = path.extname(fileName)
|
extension = path.extname(fileName)
|
||||||
return path.basename fileName, extension
|
return path.basename fileName, extension
|
||||||
|
|
||||||
# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
|
# Compile CoffeeScript code to JavaScript, using the Coffee/Jison compiler.
|
||||||
# compiler.
|
#
|
||||||
|
# If `options.sourceMap` is specified, then `options.filename` must also be specified.
|
||||||
|
#
|
||||||
|
# This returns a javascript string, unless `options.sourceMap` or `options.returnObject` are true,
|
||||||
|
# in which case this returns a `{js, v3SourceMap, sourceMap}
|
||||||
|
# object, where sourceMap is a sourcemap.coffee#SourceMap object, handy for doing programatic
|
||||||
|
# lookups.
|
||||||
exports.compile = compile = (code, options = {}) ->
|
exports.compile = compile = (code, options = {}) ->
|
||||||
{merge} = exports.helpers
|
{merge} = exports.helpers
|
||||||
try
|
try
|
||||||
|
|
||||||
|
if options.sourceMap
|
||||||
|
coffeeFile = path.basename options.filename
|
||||||
|
jsFile = baseFileName(options.filename) + ".js"
|
||||||
|
sourceMap = new sourcemap.SourceMap()
|
||||||
|
|
||||||
fragments = (parser.parse lexer.tokenize(code, options)).compileToFragments options
|
fragments = (parser.parse lexer.tokenize(code, options)).compileToFragments options
|
||||||
|
|
||||||
currentLine = 0
|
currentLine = 0
|
||||||
|
@ -46,9 +58,9 @@ exports.compile = compile = (code, options = {}) ->
|
||||||
js = ""
|
js = ""
|
||||||
for fragment in fragments
|
for fragment in fragments
|
||||||
# Update the sourcemap with data from each fragment
|
# Update the sourcemap with data from each fragment
|
||||||
if options.sourceMap
|
if sourceMap
|
||||||
if fragment.locationData
|
if fragment.locationData
|
||||||
options.sourceMap.addMapping(
|
sourceMap.addMapping(
|
||||||
[fragment.locationData.first_line, fragment.locationData.first_column],
|
[fragment.locationData.first_line, fragment.locationData.first_column],
|
||||||
[currentLine, currentColumn],
|
[currentLine, currentColumn],
|
||||||
{noReplace: true})
|
{noReplace: true})
|
||||||
|
@ -59,35 +71,22 @@ exports.compile = compile = (code, options = {}) ->
|
||||||
# Copy the code from each fragment into the final JavaScript.
|
# Copy the code from each fragment into the final JavaScript.
|
||||||
js += fragment.code
|
js += fragment.code
|
||||||
|
|
||||||
return js unless options.header
|
|
||||||
catch err
|
|
||||||
err.message = "In #{options.filename}, #{err.message}" if options.filename
|
|
||||||
throw err
|
|
||||||
header = "Generated by CoffeeScript #{@VERSION}"
|
|
||||||
"// #{header}\n#{js}"
|
|
||||||
|
|
||||||
# Generates a compiled code and a source map for a string of CoffeeScript code.
|
|
||||||
# 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 = helpers.extend {}, options
|
|
||||||
options.sourceMap = new sourcemap.SourceMap()
|
|
||||||
coffeeFile = path.basename options.filename
|
|
||||||
jsFile = baseFileName(options.filename) + ".js"
|
|
||||||
compiledJs = exports.compile code, options
|
|
||||||
v3SourceMap = sourcemap.generateV3SourceMap options.sourceMap, coffeeFile, jsFile
|
|
||||||
return {
|
|
||||||
compiledJs,
|
|
||||||
v3SourceMap,
|
|
||||||
sourceMap: options.sourceMap
|
|
||||||
}
|
|
||||||
catch err
|
catch err
|
||||||
err.message = "In #{options.filename}, #{err.message}" if options.filename
|
err.message = "In #{options.filename}, #{err.message}" if options.filename
|
||||||
throw err
|
throw err
|
||||||
|
|
||||||
|
if options.header
|
||||||
|
header = "Generated by CoffeeScript #{@VERSION}"
|
||||||
|
js = "// #{header}\n#{js}"
|
||||||
|
|
||||||
|
if options.sourceMap or options.returnObject
|
||||||
|
answer = {js}
|
||||||
|
if sourceMap
|
||||||
|
answer.sourceMap = sourceMap
|
||||||
|
answer.v3SourceMap = sourcemap.generateV3SourceMap sourceMap, coffeeFile, jsFile
|
||||||
|
answer
|
||||||
|
else
|
||||||
|
js
|
||||||
|
|
||||||
# Tokenize a string of CoffeeScript code, and return the array of tokens.
|
# Tokenize a string of CoffeeScript code, and return the array of tokens.
|
||||||
exports.tokens = (code, options) ->
|
exports.tokens = (code, options) ->
|
||||||
|
|
|
@ -125,12 +125,9 @@ compileScript = (file, input, base) ->
|
||||||
sourceCode[sources.indexOf(t.file)] = t.input
|
sourceCode[sources.indexOf(t.file)] = t.input
|
||||||
compileJoin()
|
compileJoin()
|
||||||
else
|
else
|
||||||
if o.maps
|
compiled = CoffeeScript.compile t.input, t.options
|
||||||
compiled = CoffeeScript.compileWithSourceMap t.input, t.options
|
t.output = compiled.js
|
||||||
t.output = compiled.compiledJs
|
t.sourceMap = compiled.v3SourceMap
|
||||||
t.sourceMap = compiled.v3SourceMap
|
|
||||||
else
|
|
||||||
t.output = CoffeeScript.compile t.input, t.options
|
|
||||||
|
|
||||||
CoffeeScript.emit 'success', task
|
CoffeeScript.emit 'success', task
|
||||||
if o.print then printLine t.output.trim()
|
if o.print then printLine t.output.trim()
|
||||||
|
@ -322,7 +319,14 @@ parseOptions = ->
|
||||||
|
|
||||||
# The compile-time options to pass to the CoffeeScript compiler.
|
# The compile-time options to pass to the CoffeeScript compiler.
|
||||||
compileOptions = (filename) ->
|
compileOptions = (filename) ->
|
||||||
{filename, literate: helpers.isLiterate(filename), bare: opts.bare, header: opts.compile}
|
{
|
||||||
|
filename
|
||||||
|
literate: helpers.isLiterate(filename)
|
||||||
|
bare: opts.bare
|
||||||
|
header: opts.compile
|
||||||
|
sourceMap: opts.maps
|
||||||
|
returnObject: yes
|
||||||
|
}
|
||||||
|
|
||||||
# Start up a new Node.js instance with the arguments in `--nodejs` passed to
|
# Start up a new Node.js instance with the arguments in `--nodejs` passed to
|
||||||
# the `node` binary, preserving the other options.
|
# the `node` binary, preserving the other options.
|
||||||
|
|
Loading…
Reference in New Issue