1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/coffeescript/browser.js
AKFish 8407dd885a [CS2] Module should be require-able in non-Node environments like Webpack and Browserify (#4546)
* Add webpack support

* Move Node.js-only code from src/coffee-script.coffee to src/index.coffee
* Use lib/coffee-script/index.js as npm package's "main" script
* Export CoffeeScript from src/browser.coffee
* Set package.json's "browser" field to lib/coffee-script/browser.js (used by webpack as entry point)
* Use lib/coffee-script/browser.js as bower package's "main" script

* Use NOP moduleMain when generating parser with Jison

* Remove legacy debug code from browser.coffee

* Improve comments, style

* Fix path

* Remove stub that was only to avoid breaking browser tests; compensate for the lack of stub when running the browser tests in Node

* Update output

* Add test:webpack task to Cakefile

* Update output files

* Run browser tests against webpack build

* Fix newline at end of file

* Export webpack test bundle as CommonJS module

* Remove build:webpack task

* Save webpack build to tmpdir; suppress build output unless it fails
2017-05-13 21:20:36 -07:00

119 lines
3.3 KiB
JavaScript

// Generated by CoffeeScript 2.0.0-beta1
(function() {
var CoffeeScript, compile, runScripts,
indexOf = [].indexOf;
CoffeeScript = require('./coffeescript');
compile = CoffeeScript.compile;
CoffeeScript.eval = function(code, options = {}) {
if (options.bare == null) {
options.bare = true;
}
return eval(compile(code, options));
};
CoffeeScript.run = function(code, options = {}) {
options.bare = true;
options.shiftLine = true;
return Function(compile(code, options))();
};
module.exports = CoffeeScript;
if (typeof window === "undefined" || window === null) {
return;
}
if ((typeof btoa !== "undefined" && btoa !== null) && (typeof JSON !== "undefined" && JSON !== null)) {
compile = function(code, options = {}) {
options.inlineMap = true;
return CoffeeScript.compile(code, options);
};
}
CoffeeScript.load = function(url, callback, options = {}, hold = false) {
var xhr;
options.sourceFiles = [url];
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new window.XMLHttpRequest();
xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) {
xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() {
var param, ref;
if (xhr.readyState === 4) {
if ((ref = xhr.status) === 0 || ref === 200) {
param = [xhr.responseText, options];
if (!hold) {
CoffeeScript.run(...param);
}
} else {
throw new Error(`Could not load ${url}`);
}
if (callback) {
return callback(param);
}
}
};
return xhr.send(null);
};
runScripts = function() {
var coffees, coffeetypes, execute, fn, i, index, j, len, s, script, scripts;
scripts = window.document.getElementsByTagName('script');
coffeetypes = ['text/coffeescript', 'text/literate-coffeescript'];
coffees = (function() {
var j, len, ref, results;
results = [];
for (j = 0, len = scripts.length; j < len; j++) {
s = scripts[j];
if (ref = s.type, indexOf.call(coffeetypes, ref) >= 0) {
results.push(s);
}
}
return results;
})();
index = 0;
execute = function() {
var param;
param = coffees[index];
if (param instanceof Array) {
CoffeeScript.run(...param);
index++;
return execute();
}
};
fn = function(script, i) {
var options, source;
options = {
literate: script.type === coffeetypes[1]
};
source = script.src || script.getAttribute('data-src');
if (source) {
options.filename = source;
return CoffeeScript.load(source, function(param) {
coffees[i] = param;
return execute();
}, options, true);
} else {
options.filename = script.id && script.id !== '' ? script.id : `coffeescript${(i !== 0 ? i : '')}`;
options.sourceFiles = ['embedded'];
return coffees[i] = [script.innerHTML, options];
}
};
for (i = j = 0, len = coffees.length; j < len; i = ++j) {
script = coffees[i];
fn(script, i);
}
return execute();
};
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', runScripts, false);
} else {
window.attachEvent('onload', runScripts);
}
}).call(this);