forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
transformScriptTags.ts
259 lines (235 loc) 路 6.53 KB
/
transformScriptTags.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of the React source tree.
*/
const scriptTypes = ["text/jsx", "text/babel"];
import type { transform } from "./index.ts";
import type { InputOptions } from "@babel/core";
let headEl: HTMLHeadElement;
let inlineScriptCount = 0;
type CompilationResult = {
async: boolean;
type: string;
error: boolean;
loaded: boolean;
content: string | null;
executed: boolean;
// nonce is undefined in browsers that don't support the nonce global attribute
nonce: string | undefined;
// todo: refine plugins/presets
plugins: InputOptions["plugins"];
presets: InputOptions["presets"];
url: string | null;
};
/**
* Actually transform the code.
*/
function transformCode(
transformFn: typeof transform,
script: CompilationResult,
) {
let source;
if (script.url != null) {
source = script.url;
} else {
source = "Inline Babel script";
inlineScriptCount++;
if (inlineScriptCount > 1) {
source += " (" + inlineScriptCount + ")";
}
}
return transformFn(script.content, buildBabelOptions(script, source)).code;
}
/**
* Builds the Babel options for transforming the specified script, using some
* sensible default presets and plugins if none were explicitly provided.
*/
function buildBabelOptions(script: CompilationResult, filename: string) {
let presets = script.presets;
if (!presets) {
if (script.type === "module") {
presets = [
"react",
[
"env",
{
targets: {
esmodules: true,
},
modules: false,
},
],
];
} else {
presets = ["react", "env"];
}
}
return {
filename,
presets,
plugins: script.plugins || [
"transform-class-properties",
"transform-object-rest-spread",
"transform-flow-strip-types",
],
sourceMaps: "inline" as const,
sourceFileName: filename,
};
}
/**
* Appends a script element at the end of the <head> with the content of code,
* after transforming it.
*/
function run(transformFn: typeof transform, script: CompilationResult) {
const scriptEl = document.createElement("script");
if (script.type) {
scriptEl.setAttribute("type", script.type);
}
if (script.nonce) {
scriptEl.nonce = script.nonce;
}
scriptEl.text = transformCode(transformFn, script);
headEl.appendChild(scriptEl);
}
/**
* Load script from the provided url and pass the content to the callback.
*/
function load(
url: string,
successCallback: (content: string) => void,
errorCallback: () => void,
) {
const xhr = new XMLHttpRequest();
// async, however scripts will be executed in the order they are in the
// DOM to mirror normal script loading.
xhr.open("GET", url, true);
if ("overrideMimeType" in xhr) {
xhr.overrideMimeType("text/plain");
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 0 || xhr.status === 200) {
successCallback(xhr.responseText);
} else {
errorCallback();
throw new Error("Could not load " + url);
}
}
};
xhr.send(null);
}
/**
* Converts a comma-separated data attribute string into an array of values. If
* the string is empty, returns an empty array. If the string is not defined,
* returns null.
*/
function getPluginsOrPresetsFromScript(
script: HTMLScriptElement,
attributeName: string,
) {
const rawValue = script.getAttribute(attributeName);
if (rawValue === "") {
// Empty string means to not load ANY presets or plugins
return [];
}
if (!rawValue) {
// Any other falsy value (null, undefined) means we're not overriding this
// setting, and should use the default.
return null;
}
return rawValue.split(",").map(item => item.trim());
}
/**
* Loop over provided script tags and get the content, via innerHTML if an
* inline script, or by using XHR. Transforms are applied if needed. The scripts
* are executed in the order they are found on the page.
*/
function loadScripts(
transformFn: typeof transform,
scripts: HTMLScriptElement[],
) {
const results: CompilationResult[] = [];
const count = scripts.length;
function check() {
for (let i = 0; i < count; i++) {
const result = results[i];
if (result.loaded && !result.executed) {
result.executed = true;
run(transformFn, result);
} else if (!result.loaded && !result.error && !result.async) {
break;
}
}
}
for (let i = 0; i < count; i++) {
const script = scripts[i];
const result: CompilationResult = {
// script.async is always true for non-JavaScript script tags
async: script.hasAttribute("async"),
type: script.getAttribute("data-type"),
nonce: script.nonce,
error: false,
executed: false,
plugins: getPluginsOrPresetsFromScript(script, "data-plugins"),
presets: getPluginsOrPresetsFromScript(script, "data-presets"),
loaded: false,
url: null,
content: null,
};
results.push(result);
if (script.src) {
result.url = script.src;
load(
script.src,
content => {
result.loaded = true;
result.content = content;
check();
},
() => {
result.error = true;
check();
},
);
} else {
result.url = script.getAttribute("data-module") || null;
result.loaded = true;
result.content = script.innerHTML;
}
}
check();
}
/**
* Run script tags with type="text/jsx".
* @param {Array} scriptTags specify script tags to run, run all in the <head> if not given
*/
export function runScripts(
transformFn: typeof transform,
scripts?: HTMLCollectionOf<HTMLScriptElement>,
) {
headEl = document.getElementsByTagName("head")[0];
if (!scripts) {
scripts = document.getElementsByTagName("script");
}
// Array.prototype.slice cannot be used on NodeList on IE8
const jsxScripts = [];
for (let i = 0; i < scripts.length; i++) {
const script = scripts.item(i);
// Support the old type="text/jsx;harmony=true"
const type = script.type.split(";")[0];
if (scriptTypes.indexOf(type) !== -1) {
jsxScripts.push(script);
}
}
if (jsxScripts.length === 0) {
return;
}
console.warn(
"You are using the in-browser Babel transformer. Be sure to precompile " +
"your scripts for production - https://babeljs.io/docs/setup/",
);
loadScripts(transformFn, jsxScripts);
}