1 |
|
2 |
|
3 | "use strict";
|
4 |
|
5 | require("./node");
|
6 | var transform = module.exports = require("../transformation");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | transform.options = require("../transformation/file/options");
|
13 | transform.version = require("../../package").version;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | transform.transform = transform;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | transform.run = function (code) {
|
26 | var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
27 |
|
28 | opts.sourceMaps = "inline";
|
29 | return new Function(transform(code, opts).code)();
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | transform.load = function (url, callback, opts, hold) {
|
37 | if (opts === undefined) opts = {};
|
38 |
|
39 | opts.filename = opts.filename || url;
|
40 |
|
41 | var xhr = global.ActiveXObject ? new global.ActiveXObject("Microsoft.XMLHTTP") : new global.XMLHttpRequest();
|
42 | xhr.open("GET", url, true);
|
43 | if ("overrideMimeType" in xhr) xhr.overrideMimeType("text/plain");
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | xhr.onreadystatechange = function () {
|
50 | if (xhr.readyState !== 4) return;
|
51 |
|
52 | var status = xhr.status;
|
53 | if (status === 0 || status === 200) {
|
54 | var param = [xhr.responseText, opts];
|
55 | if (!hold) transform.run.apply(transform, param);
|
56 | if (callback) callback(param);
|
57 | } else {
|
58 | throw new Error("Could not load " + url);
|
59 | }
|
60 | };
|
61 |
|
62 | xhr.send(null);
|
63 | };
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | var runScripts = function runScripts() {
|
73 | var scripts = [];
|
74 | var types = ["text/ecmascript-6", "text/6to5", "text/babel", "module"];
|
75 | var index = 0;
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 | var exec = function exec() {
|
82 | var param = scripts[index];
|
83 | if (param instanceof Array) {
|
84 | transform.run.apply(transform, param);
|
85 | index++;
|
86 | exec();
|
87 | }
|
88 | };
|
89 |
|
90 | |
91 |
|
92 |
|
93 |
|
94 | var run = function run(script, i) {
|
95 | var opts = {};
|
96 |
|
97 | if (script.src) {
|
98 | transform.load(script.src, function (param) {
|
99 | scripts[i] = param;
|
100 | exec();
|
101 | }, opts, true);
|
102 | } else {
|
103 | opts.filename = "embedded";
|
104 | scripts[i] = [script.innerHTML, opts];
|
105 | }
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 | var _scripts = global.document.getElementsByTagName("script");
|
111 |
|
112 | for (var i = 0; i < _scripts.length; ++i) {
|
113 | var _script = _scripts[i];
|
114 | if (types.indexOf(_script.type) >= 0) scripts.push(_script);
|
115 | }
|
116 |
|
117 | for (i in scripts) {
|
118 | run(scripts[i], i);
|
119 | }
|
120 |
|
121 | exec();
|
122 | };
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | if (global.addEventListener) {
|
129 | global.addEventListener("DOMContentLoaded", runScripts, false);
|
130 | } else if (global.attachEvent) {
|
131 | global.attachEvent("onload", runScripts);
|
132 | } |
\ | No newline at end of file |