mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Merge pull request #2847 from hden/browser-source-map
Browser source map
This commit is contained in:
@@ -1,12 +1,32 @@
|
||||
// Generated by CoffeeScript 1.6.1
|
||||
(function() {
|
||||
var CoffeeScript, runScripts,
|
||||
var CoffeeScript, canGenerateDataUris, compile, runScripts,
|
||||
__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; };
|
||||
|
||||
CoffeeScript = require('./coffee-script');
|
||||
|
||||
CoffeeScript.require = require;
|
||||
|
||||
canGenerateDataUris = (typeof btoa !== "undefined" && btoa !== null) && (typeof JSON !== "undefined" && JSON !== null);
|
||||
|
||||
compile = function(code, options) {
|
||||
var base64SourceMap, js, res, v3SourceMap, _ref;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
res = void 0;
|
||||
if (canGenerateDataUris) {
|
||||
options.sourceMap = true;
|
||||
options.inline = true;
|
||||
_ref = CoffeeScript.compile(code, options), js = _ref.js, v3SourceMap = _ref.v3SourceMap;
|
||||
base64SourceMap = btoa(v3SourceMap);
|
||||
res = "" + js + "\n//@ sourceMappingURL=data:application/json;base64," + base64SourceMap + "\n//@ sourceURL=coffeescript";
|
||||
} else {
|
||||
res = CoffeeScript.compile(code, options);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
CoffeeScript["eval"] = function(code, options) {
|
||||
var _ref;
|
||||
if (options == null) {
|
||||
@@ -15,7 +35,7 @@
|
||||
if ((_ref = options.bare) == null) {
|
||||
options.bare = true;
|
||||
}
|
||||
return eval(CoffeeScript.compile(code, options));
|
||||
return eval(compile(code, options));
|
||||
};
|
||||
|
||||
CoffeeScript.run = function(code, options) {
|
||||
@@ -23,7 +43,7 @@
|
||||
options = {};
|
||||
}
|
||||
options.bare = true;
|
||||
return Function(CoffeeScript.compile(code, options))();
|
||||
return Function(compile(code, options))();
|
||||
};
|
||||
|
||||
if (typeof window === "undefined" || window === null) {
|
||||
@@ -35,6 +55,7 @@
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
options.sourceFiles = [url];
|
||||
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
|
||||
xhr.open('GET', url, true);
|
||||
if ('overrideMimeType' in xhr) {
|
||||
@@ -84,6 +105,7 @@
|
||||
if (script.src) {
|
||||
return CoffeeScript.load(script.src, execute, options);
|
||||
} else {
|
||||
options.sourceFiles = ['embedded'];
|
||||
CoffeeScript.run(script.innerHTML, options);
|
||||
return execute();
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@
|
||||
}
|
||||
fragments = (parser.parse(lexer.tokenize(code, options))).compileToFragments(options);
|
||||
currentLine = 0;
|
||||
if (options.header) {
|
||||
if (options.header || options.inline) {
|
||||
currentLine += 1;
|
||||
}
|
||||
currentColumn = 0;
|
||||
@@ -79,7 +79,7 @@
|
||||
};
|
||||
if (sourceMap) {
|
||||
answer.sourceMap = sourceMap;
|
||||
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options);
|
||||
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options, code);
|
||||
}
|
||||
return answer;
|
||||
} else {
|
||||
|
||||
@@ -113,7 +113,7 @@
|
||||
|
||||
})();
|
||||
|
||||
exports.generateV3SourceMap = function(sourceMap, options) {
|
||||
exports.generateV3SourceMap = function(sourceMap, options, code) {
|
||||
var answer, generatedFile, lastGeneratedColumnWritten, lastSourceColumnWritten, lastSourceLineWritten, mappings, needComma, sourceFiles, sourceRoot, writingGeneratedLine;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
@@ -155,6 +155,9 @@
|
||||
names: [],
|
||||
mappings: mappings
|
||||
};
|
||||
if (options.inline) {
|
||||
answer.sourcesContent = [code];
|
||||
}
|
||||
return JSON.stringify(answer, null, 2);
|
||||
};
|
||||
|
||||
|
||||
@@ -3,21 +3,36 @@
|
||||
CoffeeScript = require './coffee-script'
|
||||
CoffeeScript.require = require
|
||||
|
||||
canGenerateDataUris = btoa? and JSON?
|
||||
|
||||
compile = (code, options = {}) ->
|
||||
res = undefined
|
||||
if canGenerateDataUris
|
||||
options.sourceMap = true
|
||||
options.inline = true
|
||||
{js, v3SourceMap} = CoffeeScript.compile code, options
|
||||
base64SourceMap = btoa v3SourceMap
|
||||
res = "#{js}\n//@ sourceMappingURL=data:application/json;base64,#{base64SourceMap}\n//@ sourceURL=coffeescript"
|
||||
else
|
||||
res = CoffeeScript.compile code, options
|
||||
res
|
||||
|
||||
# Use standard JavaScript `eval` to eval code.
|
||||
CoffeeScript.eval = (code, options = {}) ->
|
||||
options.bare ?= on
|
||||
eval CoffeeScript.compile code, options
|
||||
eval compile code, options
|
||||
|
||||
# Running code does not provide access to this scope.
|
||||
CoffeeScript.run = (code, options = {}) ->
|
||||
options.bare = on
|
||||
Function(CoffeeScript.compile code, options)()
|
||||
Function(compile code, options)()
|
||||
|
||||
# If we're not in a browser environment, we're finished with the public API.
|
||||
return unless window?
|
||||
|
||||
# Load a remote script from the current domain via XHR.
|
||||
CoffeeScript.load = (url, callback, options = {}) ->
|
||||
options.sourceFiles = [url]
|
||||
xhr = if window.ActiveXObject
|
||||
new window.ActiveXObject('Microsoft.XMLHTTP')
|
||||
else
|
||||
@@ -50,6 +65,7 @@ runScripts = ->
|
||||
if script.src
|
||||
CoffeeScript.load script.src, execute, options
|
||||
else
|
||||
options.sourceFiles = ['embedded']
|
||||
CoffeeScript.run script.innerHTML, options
|
||||
execute()
|
||||
null
|
||||
|
||||
@@ -48,7 +48,7 @@ exports.compile = compile = (code, options = {}) ->
|
||||
fragments = (parser.parse lexer.tokenize(code, options)).compileToFragments options
|
||||
|
||||
currentLine = 0
|
||||
currentLine += 1 if options.header
|
||||
currentLine += 1 if options.header or options.inline
|
||||
currentColumn = 0
|
||||
js = ""
|
||||
for fragment in fragments
|
||||
@@ -74,7 +74,7 @@ exports.compile = compile = (code, options = {}) ->
|
||||
answer = {js}
|
||||
if sourceMap
|
||||
answer.sourceMap = sourceMap
|
||||
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options)
|
||||
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options, code)
|
||||
answer
|
||||
else
|
||||
js
|
||||
|
||||
@@ -96,7 +96,7 @@ class exports.SourceMap
|
||||
# `options.sourceFiles` and `options.generatedFile` may be passed to set "sources" and "file",
|
||||
# respectively. Note that `sourceFiles` must be an array.
|
||||
|
||||
exports.generateV3SourceMap = (sourceMap, options={}) ->
|
||||
exports.generateV3SourceMap = (sourceMap, options={}, code) ->
|
||||
sourceRoot = options.sourceRoot or ""
|
||||
sourceFiles = options.sourceFiles or [""]
|
||||
generatedFile = options.generatedFile or ""
|
||||
@@ -159,6 +159,7 @@ exports.generateV3SourceMap = (sourceMap, options={}) ->
|
||||
names: []
|
||||
mappings
|
||||
}
|
||||
answer.sourcesContent = [code] if options.inline
|
||||
|
||||
return JSON.stringify answer, null, 2
|
||||
|
||||
@@ -251,4 +252,5 @@ exports.vlqDecodeValue = (str, offset=0) ->
|
||||
|
||||
if signBit then value = -value
|
||||
|
||||
return [value, consumed]
|
||||
return [value, consumed]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user