1
0
Fork 0
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:
Hao-kang Den 2013-03-16 23:22:05 +08:00
parent fd0b1ffc4d
commit c24e957f17
6 changed files with 57 additions and 10 deletions

View file

@ -1,12 +1,36 @@
// Generated by CoffeeScript 1.6.1 // Generated by CoffeeScript 1.6.1
(function() { (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; }; __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('./coffee-script');
CoffeeScript.require = require; 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) { CoffeeScript["eval"] = function(code, options) {
var _ref; var _ref;
if (options == null) { if (options == null) {
@ -15,7 +39,7 @@
if ((_ref = options.bare) == null) { if ((_ref = options.bare) == null) {
options.bare = true; options.bare = true;
} }
return eval(CoffeeScript.compile(code, options)); return eval(compile(code, options));
}; };
CoffeeScript.run = function(code, options) { CoffeeScript.run = function(code, options) {
@ -23,7 +47,7 @@
options = {}; options = {};
} }
options.bare = true; options.bare = true;
return Function(CoffeeScript.compile(code, options))(); return Function(compile(code, options))();
}; };
if (typeof window === "undefined" || window === null) { if (typeof window === "undefined" || window === null) {
@ -35,6 +59,7 @@
if (options == null) { if (options == null) {
options = {}; options = {};
} }
options.sourceFiles = [url];
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(); xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
xhr.open('GET', url, true); xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) { if ('overrideMimeType' in xhr) {
@ -84,6 +109,7 @@
if (script.src) { if (script.src) {
return CoffeeScript.load(script.src, execute, options); return CoffeeScript.load(script.src, execute, options);
} else { } else {
options.sourceFiles = ['embedded'];
CoffeeScript.run(script.innerHTML, options); CoffeeScript.run(script.innerHTML, options);
return execute(); return execute();
} }

View file

@ -79,7 +79,7 @@
}; };
if (sourceMap) { if (sourceMap) {
answer.sourceMap = sourceMap; answer.sourceMap = sourceMap;
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options); answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options, code);
} }
return answer; return answer;
} else { } else {

View file

@ -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; var answer, generatedFile, lastGeneratedColumnWritten, lastSourceColumnWritten, lastSourceLineWritten, mappings, needComma, sourceFiles, sourceRoot, writingGeneratedLine;
if (options == null) { if (options == null) {
options = {}; options = {};
@ -155,6 +155,9 @@
names: [], names: [],
mappings: mappings mappings: mappings
}; };
if (options.inline) {
answer.sourcesContent = [code];
}
return JSON.stringify(answer, null, 2); return JSON.stringify(answer, null, 2);
}; };

View file

@ -3,21 +3,36 @@
CoffeeScript = require './coffee-script' CoffeeScript = require './coffee-script'
CoffeeScript.require = require 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. # Use standard JavaScript `eval` to eval code.
CoffeeScript.eval = (code, options = {}) -> CoffeeScript.eval = (code, options = {}) ->
options.bare ?= on options.bare ?= on
eval CoffeeScript.compile code, options eval compile code, options
# Running code does not provide access to this scope. # Running code does not provide access to this scope.
CoffeeScript.run = (code, options = {}) -> CoffeeScript.run = (code, options = {}) ->
options.bare = on 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. # If we're not in a browser environment, we're finished with the public API.
return unless window? return unless window?
# Load a remote script from the current domain via XHR. # Load a remote script from the current domain via XHR.
CoffeeScript.load = (url, callback, options = {}) -> CoffeeScript.load = (url, callback, options = {}) ->
options.sourceFiles = [url]
xhr = if window.ActiveXObject xhr = if window.ActiveXObject
new window.ActiveXObject('Microsoft.XMLHTTP') new window.ActiveXObject('Microsoft.XMLHTTP')
else else
@ -50,6 +65,7 @@ runScripts = ->
if script.src if script.src
CoffeeScript.load script.src, execute, options CoffeeScript.load script.src, execute, options
else else
options.sourceFiles = ['embedded']
CoffeeScript.run script.innerHTML, options CoffeeScript.run script.innerHTML, options
execute() execute()
null null

View file

@ -74,7 +74,7 @@ exports.compile = compile = (code, options = {}) ->
answer = {js} answer = {js}
if sourceMap if sourceMap
answer.sourceMap = sourceMap answer.sourceMap = sourceMap
answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options) answer.v3SourceMap = sourcemap.generateV3SourceMap(sourceMap, options, code)
answer answer
else else
js js

View file

@ -96,7 +96,7 @@ class exports.SourceMap
# `options.sourceFiles` and `options.generatedFile` may be passed to set "sources" and "file", # `options.sourceFiles` and `options.generatedFile` may be passed to set "sources" and "file",
# respectively. Note that `sourceFiles` must be an array. # respectively. Note that `sourceFiles` must be an array.
exports.generateV3SourceMap = (sourceMap, options={}) -> exports.generateV3SourceMap = (sourceMap, options={}, code) ->
sourceRoot = options.sourceRoot or "" sourceRoot = options.sourceRoot or ""
sourceFiles = options.sourceFiles or [""] sourceFiles = options.sourceFiles or [""]
generatedFile = options.generatedFile or "" generatedFile = options.generatedFile or ""
@ -159,6 +159,7 @@ exports.generateV3SourceMap = (sourceMap, options={}) ->
names: [] names: []
mappings mappings
} }
answer.sourcesContent = [code] if options.inline
return JSON.stringify answer, null, 2 return JSON.stringify answer, null, 2
@ -251,4 +252,5 @@ exports.vlqDecodeValue = (str, offset=0) ->
if signBit then value = -value if signBit then value = -value
return [value, consumed] return [value, consumed]