Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 729641b

Browse files
committed
Make clutz support Closure's BROWSER_WITH_TRANSFORMED_PREFIXES resolution.
Previously, clutz would throw when encountering an es6 file with absolute import. Closure has support for such paths along with an option to rewrite them to something rooted using the BROWSER_WITH_TRANSFORMED_PREFIXES module resolution strategy. This change makes clutz use that strategy and makes it so that one can configure which absolute prefixes will be replaced with '/'. Note, this is still not proper support for ES6, but rather just making sure clutz doesn't reject code that Closure could accept using the right flags.
1 parent 95df710 commit 729641b

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

src/main/java/com/google/javascript/clutz/Options.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import static java.nio.charset.StandardCharsets.UTF_8;
55

66
import com.google.common.collect.ImmutableList;
7+
import com.google.common.collect.ImmutableMap;
78
import com.google.common.collect.Sets;
89
import com.google.common.io.Files;
910
import com.google.javascript.jscomp.*;
1011
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
12+
import com.google.javascript.jscomp.deps.ModuleLoader;
1113
import com.google.javascript.jscomp.parsing.Config;
1214
import java.io.File;
1315
import java.io.IOException;
@@ -163,6 +165,13 @@ public int parseArguments(Parameters params) throws CmdLineException {
163165
)
164166
private CompilerOptions.TracerMode tracerMode = CompilerOptions.TracerMode.OFF;
165167

168+
@Option(
169+
name = "--browserResolverStrippedPrefixes",
170+
usage = "A list of prefixes for absolute ES6 module paths, that would be replaced by '/'",
171+
handler = StringArrayOptionHandler.class
172+
)
173+
List<String> browserResolverStrippedPrefixes = new ArrayList<>();
174+
166175
@Argument
167176
@Option(name = "--", handler = StopOptionHandler.class)
168177
List<String> arguments = new ArrayList<>();
@@ -208,6 +217,14 @@ public CompilerOptions getCompilerOptions() {
208217
// Always parse and analyze the latest language features closure supports.
209218
options.setLanguage(LanguageMode.ECMASCRIPT_NEXT);
210219
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
220+
221+
options.setModuleResolutionMode(ModuleLoader.ResolutionMode.BROWSER_WITH_TRANSFORMED_PREFIXES);
222+
ImmutableMap.Builder builder = ImmutableMap.builder();
223+
for (String str : browserResolverStrippedPrefixes) {
224+
builder.put(str, "/");
225+
}
226+
options.setBrowserResolverPrefixReplacements(builder.build());
227+
211228
if (closureEnv != null) {
212229
options.setEnvironment(closureEnv);
213230
}

src/test/java/com/google/javascript/clutz/ProgramSubject.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.PrintStream;
2121
import java.util.ArrayList;
22+
import java.util.Arrays;
2223
import java.util.Collections;
2324
import java.util.LinkedHashSet;
2425
import java.util.List;
@@ -124,6 +125,7 @@ private String[] parse() throws AssertionError {
124125
opts.partialInput = true;
125126
}
126127
opts.collidingProvides = ImmutableSet.of("colliding_provide.aliased");
128+
opts.browserResolverStrippedPrefixes = Arrays.asList("abs_strip_for_testing");
127129

128130
List<SourceFile> sourceFiles = new ArrayList<>();
129131

src/test/java/com/google/javascript/clutz/partial/es6_module.d.ts

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as rel from './rel/module/name';
2+
import * as rel2 from '/abs/module/name';
3+
//!! abs_strip_for_testing is special cased in the test harness.
4+
import * as abs from 'abs_strip_for_testing/module/name';
5+
6+
export let x = rel.x + abs.x + rel2.x;
7+
8+
//!! The emit of this file is intentionally empty.
9+
//!! So far clutz doesn't support es6 modules properly, but the test
10+
//!! makes sure that it doesn't reject them either.

0 commit comments

Comments
 (0)