mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
use btoa as base64 encoder, inspired by @ashtuchkin
This commit is contained in:
parent
fd0b1ffc4d
commit
c24e957f17
6 changed files with 57 additions and 10 deletions
|
|
@ -1,12 +1,36 @@
|
|||
// Generated by CoffeeScript 1.6.1
|
||||
(function() {
|
||||
var CoffeeScript, runScripts,
|
||||
var CoffeeScript, compile, isModernBrowser, 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;
|
||||
|
||||
isModernBrowser = typeof window !== "undefined" && window !== null ? function() {
|
||||
if (btoa && JSON) {
|
||||
return true;
|
||||
}
|
||||
} : function() {};
|
||||
|
||||
compile = function(code, options) {
|
||||
var answer, js, res, v3SourceMap, _ref;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
res = void 0;
|
||||
if (isModernBrowser()) {
|
||||
options.sourceMap = true;
|
||||
options.inline = true;
|
||||
_ref = CoffeeScript.compile(code, options), js = _ref.js, v3SourceMap = _ref.v3SourceMap;
|
||||
answer = btoa(v3SourceMap);
|
||||
res = "" + js + "\n//@ sourceMappingURL=data:application/json;base64," + answer + "\n//@ sourceURL=coffeescript.coffee";
|
||||
} else {
|
||||
res = CoffeeScript.compile(code, options);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
CoffeeScript["eval"] = function(code, options) {
|
||||
var _ref;
|
||||
if (options == null) {
|
||||
|
|
@ -15,7 +39,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 +47,7 @@
|
|||
options = {};
|
||||
}
|
||||
options.bare = true;
|
||||
return Function(CoffeeScript.compile(code, options))();
|
||||
return Function(compile(code, options))();
|
||||
};
|
||||
|
||||
if (typeof window === "undefined" || window === null) {
|
||||
|
|
@ -35,6 +59,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 +109,7 @@
|
|||
if (script.src) {
|
||||
return CoffeeScript.load(script.src, execute, options);
|
||||
} else {
|
||||
options.sourceFiles = ['embedded'];
|
||||
CoffeeScript.run(script.innerHTML, options);
|
||||
return execute();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
isModernBrowser = if window? then -> true if btoa and JSON else ->
|
||||
|
||||
compile = (code, options = {}) ->
|
||||
res = undefined
|
||||
if isModernBrowser()
|
||||
options.sourceMap = true
|
||||
options.inline = true
|
||||
{js, v3SourceMap} = CoffeeScript.compile code, options
|
||||
answer = btoa v3SourceMap
|
||||
res = "#{js}\n//@ sourceMappingURL=data:application/json;base64,#{answer}\n//@ sourceURL=coffeescript.js"
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
@ -252,3 +253,4 @@ exports.vlqDecodeValue = (str, offset=0) ->
|
|||
if signBit then value = -value
|
||||
|
||||
return [value, consumed]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue