UNPKG

3 kBJavaScriptView Raw
1/* eslint no-new-func: 0 */
2
3"use strict";
4
5require("./node");
6var transform = module.exports = require("../transformation");
7
8/**
9 * Add `options` and `version` to `babel` global.
10 */
11
12transform.options = require("../transformation/file/options");
13transform.version = require("../../package").version;
14
15/**
16 * Add `transform` api to `babel` global.
17 */
18
19transform.transform = transform;
20
21/**
22 * Tranform and execute script, adding in inline sourcemaps.
23 */
24
25transform.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 * Load scripts via xhr, and `transform` when complete (optional).
34 */
35
36transform.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 * When successfully loaded, transform (optional), and call `callback`.
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 * Load and transform all scripts of `types`.
67 *
68 * @example
69 * <script type="module"></script>
70 */
71
72var runScripts = function runScripts() {
73 var scripts = [];
74 var types = ["text/ecmascript-6", "text/6to5", "text/babel", "module"];
75 var index = 0;
76
77 /**
78 * Transform and execute script. Ensures correct load order.
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 * Load, transform, and execute all scripts.
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 // Collect scripts with Babel `types`.
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 * Register load event to transform and execute scripts.
126 */
127
128if (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