-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BaseTranspiler.java
293 lines (263 loc) · 11.6 KB
/
BaseTranspiler.java
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2016 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.transpile;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.javascript.jscomp.CheckLevel;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.CompilerOptions.Es6ModuleTranspilation;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.DiagnosticGroup;
import com.google.javascript.jscomp.DiagnosticType;
import com.google.javascript.jscomp.Es6RewriteModulesToCommonJsModules;
import com.google.javascript.jscomp.PropertyRenamingPolicy;
import com.google.javascript.jscomp.Result;
import com.google.javascript.jscomp.SourceFile;
import com.google.javascript.jscomp.SourceMap;
import com.google.javascript.jscomp.VariableRenamingPolicy;
import com.google.javascript.jscomp.bundle.TranspilationException;
import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.jscomp.deps.ModuleLoader.PathEscaper;
import com.google.javascript.jscomp.deps.ModuleLoader.ResolutionMode;
import com.google.javascript.rhino.Node;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Basic Transpiler implementation for outputting ES5 code.
*/
public final class BaseTranspiler implements Transpiler {
private final CompilerSupplier compilerSupplier;
private final String runtimeLibraryName;
public BaseTranspiler(CompilerSupplier compilerSupplier, String runtimeLibraryName) {
this.compilerSupplier = checkNotNull(compilerSupplier);
this.runtimeLibraryName = checkNotNull(runtimeLibraryName);
}
@Override
public TranspileResult transpile(URI path, String code) {
CompileResult result = compilerSupplier.compile(path, code);
if (!result.transpiled) {
return new TranspileResult(path, code, code, "");
}
return new TranspileResult(path, code, result.source, result.sourceMap);
}
@Override
public String runtime() {
StringBuilder sb = new StringBuilder();
if (!Strings.isNullOrEmpty(runtimeLibraryName)) {
sb.append(compilerSupplier.runtime(runtimeLibraryName));
}
sb.append(compilerSupplier.runtime("modules"));
return sb.toString();
}
public static final BaseTranspiler ES5_TRANSPILER = new BaseTranspiler(
new CompilerSupplier(), "es6_runtime");
public static final BaseTranspiler ES_MODULE_TO_CJS_TRANSPILER =
new BaseTranspiler(new EsmToCjsCompilerSupplier(), "");
/**
* Wraps the Compiler into a more relevant interface, making it
* easy to test the Transpiler without depending on implementation
* details of the Compiler itself. Also works around the fact
* that the Compiler is not thread-safe (since we may do multiple
* transpiles concurrently), so we supply a fresh instance each
* time when we're in single-file mode.
*/
public static class CompilerSupplier {
protected final ResolutionMode moduleResolution;
protected final ImmutableList<String> moduleRoots;
protected final ImmutableMap<String, String> prefixReplacements;
public CompilerSupplier() {
// Use the default resolution mode
this(new CompilerOptions().getModuleResolutionMode(), ImmutableList.of(), ImmutableMap.of());
}
/**
* Accepts commonly overridden options for ES6 modules to avoid needed to subclass.
*
* @param moduleResolution module resolution for resolving import paths
* @param prefixReplacements prefix replacements for when moduleResolution is {@link
* ModuleLoader.ResolutionMode#BROWSER_WITH_TRANSFORMED_PREFIXES}
*/
public CompilerSupplier(
ModuleLoader.ResolutionMode moduleResolution,
ImmutableList<String> moduleRoots,
ImmutableMap<String, String> prefixReplacements) {
this.moduleResolution = moduleResolution;
this.moduleRoots = moduleRoots;
this.prefixReplacements = prefixReplacements;
}
public CompileResult compile(URI path, String code) {
Compiler compiler = compiler();
Result result =
compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
String source = compiler.toSource();
StringBuilder sourceMap = new StringBuilder();
if (result.sourceMap != null) {
try {
result.sourceMap.appendTo(sourceMap, path.toString());
} catch (IOException e) {
// impossible, and not a big deal even if it did happen.
}
}
boolean transpiled = !result.transpiledFiles.isEmpty();
if (result.errors.length > 0) {
throw new TranspilationException(compiler, result.errors, result.warnings);
}
return new CompileResult(
source,
transpiled,
transpiled ? sourceMap.toString() : "");
}
public String runtime(String library) {
Compiler compiler = compiler();
CompilerOptions options = options();
options.setForceLibraryInjection(ImmutableList.of(library));
compiler.compile(EXTERNS, EMPTY, options);
return compiler.toSource();
}
protected Compiler compiler() {
return new Compiler();
}
protected CompilerOptions options() {
CompilerOptions options = new CompilerOptions();
setOptions(options);
return options;
}
protected void setOptions(CompilerOptions options) {
options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT);
// TODO(sdh): It would be nice to allow people to output code in
// strict mode. But currently we swallow all the input language
// strictness checks, and there are various tests that are never
// compiled and so are broken when we output 'use strict'. We
// could consider adding some sort of logging/warning/error in
// cases where the input was not strict, though there could still
// be semantic differences even if syntax is strict. Possibly
// the first step would be to allow the option of outputting strict
// and then change the default and see what breaks. b/33005948
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
options.setQuoteKeywordProperties(true);
options.setSkipNonTranspilationPasses(true);
options.setVariableRenaming(VariableRenamingPolicy.OFF);
options.setPropertyRenaming(PropertyRenamingPolicy.OFF);
options.setWrapGoogModulesForWhitespaceOnly(false);
options.setPrettyPrint(true);
options.setWarningLevel(ES5_WARNINGS, CheckLevel.OFF);
options.setEs6ModuleTranspilation(Es6ModuleTranspilation.TO_COMMON_JS_LIKE_MODULES);
options.setModuleResolutionMode(moduleResolution);
options.setModuleRoots(moduleRoots);
options.setBrowserResolverPrefixReplacements(prefixReplacements);
// Don't escape module paths when bundling in the event paths are URLs.
options.setPathEscaper(PathEscaper.CANONICALIZE_ONLY);
options.setSourceMapOutputPath("/dev/null");
options.setSourceMapIncludeSourcesContent(true);
// Make sourcemaps use absolute paths, so that the path is not duplicated if a build tool adds
// a sourceurl. Exception: if the location has a scheme (like http:) then leave the path
// intact. This makes this usable from web servers.
options.setSourceMapLocationMappings(
ImmutableList.of((location) -> {
try {
if (new URI(location).getScheme() != null) {
return location;
}
} catch (URISyntaxException e) {
// Swallow, return the absolute version below.
}
return new SourceMap.PrefixLocationMapping("", "/").map(location);
}));
}
protected static final SourceFile EXTERNS =
SourceFile.fromCode("externs.js", "function Symbol() {}");
protected static final SourceFile EMPTY = SourceFile.fromCode("empty.js", "");
protected static final DiagnosticGroup ES5_WARNINGS = new DiagnosticGroup(
DiagnosticType.error("JSC_CANNOT_CONVERT", ""));
}
/**
* CompilerSupplier that only transforms EcmaScript Modules into a form that can be safely
* transformed on a file by file basis and concatenated.
*/
public static class EsmToCjsCompilerSupplier extends CompilerSupplier {
public EsmToCjsCompilerSupplier() {
super();
}
public EsmToCjsCompilerSupplier(
ModuleLoader.ResolutionMode moduleResolution,
ImmutableList<String> moduleRoots,
ImmutableMap<String, String> prefixReplacements) {
super(moduleResolution, moduleRoots, prefixReplacements);
}
@Override
public CompileResult compile(URI path, String code) {
CompilerOptions options = new CompilerOptions();
options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT);
options.setEmitUseStrict(false);
options.setSourceMapOutputPath("/dev/null");
options.setSourceMapIncludeSourcesContent(true);
options.setPrettyPrint(true);
options.setModuleResolutionMode(moduleResolution);
options.setModuleRoots(moduleRoots);
options.setBrowserResolverPrefixReplacements(prefixReplacements);
// Create a compiler and run specifically this one pass on it.
Compiler compiler = compiler();
compiler.init(
ImmutableList.of(),
ImmutableList.of(SourceFile.fromCode(path.toString(), code)),
options);
compiler.parseForCompilation();
boolean transpiled = false;
if (!compiler.hasErrors()
&& compiler.getRoot().getSecondChild().getFirstFirstChild().isModuleBody()
&& !compiler
.getRoot()
.getSecondChild()
.getFirstChild()
.getBooleanProp(Node.GOOG_MODULE)) {
new Es6RewriteModulesToCommonJsModules(compiler)
.process(null, compiler.getRoot().getSecondChild());
compiler.getRoot().getSecondChild().getFirstChild().putBooleanProp(Node.TRANSPILED, true);
transpiled = true;
}
Result result = compiler.getResult();
String source = compiler.toSource();
StringBuilder sourceMap = new StringBuilder();
if (result.sourceMap != null) {
try {
result.sourceMap.appendTo(sourceMap, path.toString());
} catch (IOException e) {
// impossible, and not a big deal even if it did happen.
}
}
if (result.errors.length > 0) {
throw new TranspilationException(compiler, result.errors, result.warnings);
}
return new CompileResult(source, transpiled, transpiled ? sourceMap.toString() : "");
}
}
/**
* The source together with the additional compilation results.
*/
public static class CompileResult {
public final String source;
public final boolean transpiled;
public final String sourceMap;
public CompileResult(String source, boolean transpiled, String sourceMap) {
this.source = checkNotNull(source);
this.transpiled = transpiled;
this.sourceMap = checkNotNull(sourceMap);
}
}
}