-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ClosureBundlerTest.java
226 lines (204 loc) · 8.8 KB
/
ClosureBundlerTest.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
/*
* Copyright 2014 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.deps;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.RETURNS_SMART_NULLS;
import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableMap;
import com.google.javascript.jscomp.bundle.TranspilationException;
import com.google.javascript.jscomp.transpile.BaseTranspiler;
import com.google.javascript.jscomp.transpile.TranspileResult;
import com.google.javascript.jscomp.transpile.Transpiler;
import java.io.IOException;
import java.net.URI;
import junit.framework.TestCase;
import org.mockito.Mockito;
/**
* Tests for ClosureBundler
*/
public final class ClosureBundlerTest extends TestCase {
private static final DependencyInfo MODULE =
SimpleDependencyInfo.builder("", "").setGoogModule(true).build();
private static final DependencyInfo TRADITIONAL =
SimpleDependencyInfo.builder("", "").build();
public void testGoogModule() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler().appendTo(sb, MODULE, "\"a string\"");
assertThat(sb.toString())
.isEqualTo("goog.loadModule(function(exports) {'use strict';"
+ "\"a string\"\n"
+ ";return exports;});\n");
}
public void testGoogModuleWithSourceURL() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler()
.useEval(true)
.withSourceUrl("URL")
.appendTo(sb, MODULE, "\"a string\"");
assertThat(sb.toString())
.isEqualTo("goog.loadModule(\"\\x22a string\\x22\\n//# sourceURL\\x3dURL\\n\");\n");
}
public void testGoogModuleWithEval() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler()
.useEval(true)
.appendTo(sb, MODULE, "\"a string\"");
assertThat(sb.toString()).isEqualTo("goog.loadModule(\"\\x22a string\\x22\");\n");
}
public void testGoogModuleWithEvalWithURL() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler()
.useEval(true)
.withSourceUrl("URL")
.appendTo(sb, MODULE, "\"a string\"");
assertThat(sb.toString())
.isEqualTo("goog.loadModule(\"\\x22a string\\x22\\n//# sourceURL\\x3dURL\\n\");\n");
}
public void testTraditional() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler().appendTo(sb, TRADITIONAL, "\"a string\"");
assertThat(sb.toString()).isEqualTo("\"a string\"");
}
public void testTraditionalWithSourceURL() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler()
.withSourceUrl("URL")
.appendTo(sb, TRADITIONAL, "\"a string\"");
assertThat(sb.toString())
.isEqualTo("\"a string\"\n"
+ "//# sourceURL=URL\n");
}
public void testTraditionalWithEval() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler()
.useEval(true)
.appendTo(sb, TRADITIONAL, "\"a string\"");
assertThat(sb.toString()).isEqualTo("eval(\"\\x22a string\\x22\");\n");
}
public void testTraditionalWithEvalWithSourceUrl() throws IOException {
StringBuilder sb = new StringBuilder();
new ClosureBundler()
.useEval(true)
.withSourceUrl("URL")
.appendTo(sb, TRADITIONAL, "\"a string\"");
assertThat(sb.toString())
.isEqualTo("eval(\"\\x22a string\\x22\\n//# sourceURL\\x3dURL\\n\");\n");
}
public void testTranspilation() throws Exception {
String input = "goog.module('Foo');\nclass Foo {}";
URI uri = new URI("foo.js");
Transpiler transpiler = Mockito.mock(Transpiler.class, RETURNS_SMART_NULLS);
when(transpiler.runtime()).thenReturn("RUNTIME;");
when(transpiler.transpile(uri, input))
.thenReturn(new TranspileResult(uri, input, "TRANSPILED;", ""));
ClosureBundler bundler = new ClosureBundler(transpiler).withPath("foo.js");
StringBuilder sb = new StringBuilder();
bundler.appendRuntimeTo(sb);
bundler.appendTo(sb, MODULE, input);
assertThat(sb.toString()).startsWith("RUNTIME;");
// Call endsWith because the ES6 module runtime is also injected.
assertThat(sb.toString())
.endsWith("goog.loadModule(function(exports) {'use strict';TRANSPILED;\n"
+ ";return exports;});\n");
// Without calling appendRuntimeTo(), the runtime is not included anymore.
sb = new StringBuilder();
bundler.appendTo(sb, MODULE, input);
assertThat(sb.toString())
.isEqualTo("goog.loadModule(function(exports) {'use strict';TRANSPILED;\n"
+ ";return exports;});\n");
}
public void testEs6Module() throws IOException {
String input =
"import {x} from './other.js';\n"
+ "export {x as y};"
+ "let local;\n"
+ "export function foo() { return local; }\n";
ClosureBundler bundler =
new ClosureBundler(BaseTranspiler.LATEST_TRANSPILER).withPath("nested/path/foo.js");
StringBuilder sb = new StringBuilder();
bundler.appendRuntimeTo(sb);
bundler.appendTo(
sb,
SimpleDependencyInfo.builder("", "").setLoadFlags(ImmutableMap.of("module", "es6")).build(),
input);
String result = sb.toString();
// ES6 module runtime should be injected.
assertThat(result).contains("$jscomp.require = createRequire();");
assertThat(result).startsWith("var $jscomp");
assertThat(result)
.endsWith(
"$jscomp.registerAndLoadModule(function($$require, $$exports, $$module) {\n"
+ " \"use strict\";\n"
+ " Object.defineProperties($$exports, {foo:{enumerable:true, get:function() {\n"
+ " return foo;\n"
+ " }}, y:{enumerable:true, get:function() {\n"
+ " return module$nested$path$other.x;\n"
+ " }}});\n"
+ " var module$nested$path$other = $$require(\"nested/path/other.js\");\n"
+ " let local;\n"
+ " function foo() {\n"
+ " return local;\n"
+ " }\n"
+ "}, \"nested/path/foo.js\", [\"nested/path/other.js\"]);\n");
}
public void testPassThroughIfNoTranspilationNeeded() throws Exception {
String input = "/** Hello Comments! */ const s = 0;\n let intended;";
ClosureBundler bundler = new ClosureBundler(BaseTranspiler.LATEST_TRANSPILER);
StringBuilder sb = new StringBuilder();
bundler.appendTo(
sb,
SimpleDependencyInfo.builder("", "").build(),
input);
assertThat(sb.toString()).isEqualTo(input);
}
public void testCommentsAndFormattingRemovedWithTranspilation() throws Exception {
String input = "/** Hello Comments! */ const s = 0;\n let intended;";
ClosureBundler bundler = new ClosureBundler(BaseTranspiler.ES5_TRANSPILER);
StringBuilder sb = new StringBuilder();
bundler.appendTo(
sb,
SimpleDependencyInfo.builder("", "").build(),
input);
assertThat(sb.toString()).isEqualTo("var s = 0;\nvar intended;\n");
}
public void testFullWidthLowLineWithDefaultTranspilerIsOkay() throws Exception {
// The last character is something the compiler doesn't handle correctly
String input = "var aesthetic_";
ClosureBundler bundler = new ClosureBundler();
StringBuilder sb = new StringBuilder();
bundler.appendTo(
sb,
SimpleDependencyInfo.builder("", "").build(),
input);
assertThat(sb.toString()).isEqualTo(input);
}
// TODO(johnplaisted): If / when the compiler can parse full width low line in identifiers
// this should be okay to be transpiled.
public void testFullWidthLowLineInTranspiledCodeIsError() throws Exception {
// The last character is something the compiler doesn't handle correctly
String input = "let aesthetic_";
ClosureBundler bundler = new ClosureBundler(BaseTranspiler.ES5_TRANSPILER);
StringBuilder sb = new StringBuilder();
try {
bundler.appendTo(sb, SimpleDependencyInfo.builder("", "").build(), input);
fail("Expected an exception");
} catch (TranspilationException e) {
assertThat(e)
.hasMessageThat()
.contains("Parse error. Character '_' (U+FF3F) is not a valid identifier start char");
}
}
}