diff --git a/src/main/java/com/google/javascript/clutz/Options.java b/src/main/java/com/google/javascript/clutz/Options.java index 7903bef9..a69a7e0c 100644 --- a/src/main/java/com/google/javascript/clutz/Options.java +++ b/src/main/java/com/google/javascript/clutz/Options.java @@ -4,10 +4,12 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; import com.google.common.io.Files; import com.google.javascript.jscomp.*; import com.google.javascript.jscomp.CompilerOptions.LanguageMode; +import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.parsing.Config; import java.io.File; import java.io.IOException; @@ -163,6 +165,13 @@ public int parseArguments(Parameters params) throws CmdLineException { ) private CompilerOptions.TracerMode tracerMode = CompilerOptions.TracerMode.OFF; + @Option( + name = "--browserResolverStrippedPrefixes", + usage = "A list of prefixes for absolute ES6 module paths, that would be replaced by '/'", + handler = StringArrayOptionHandler.class + ) + List browserResolverStrippedPrefixes = new ArrayList<>(); + @Argument @Option(name = "--", handler = StopOptionHandler.class) List arguments = new ArrayList<>(); @@ -208,6 +217,14 @@ public CompilerOptions getCompilerOptions() { // Always parse and analyze the latest language features closure supports. options.setLanguage(LanguageMode.ECMASCRIPT_NEXT); options.setLanguageOut(LanguageMode.ECMASCRIPT5); + + options.setModuleResolutionMode(ModuleLoader.ResolutionMode.BROWSER_WITH_TRANSFORMED_PREFIXES); + ImmutableMap.Builder builder = ImmutableMap.builder(); + for (String str : browserResolverStrippedPrefixes) { + builder.put(str, "/"); + } + options.setBrowserResolverPrefixReplacements(builder.build()); + if (closureEnv != null) { options.setEnvironment(closureEnv); } diff --git a/src/test/java/com/google/javascript/clutz/ProgramSubject.java b/src/test/java/com/google/javascript/clutz/ProgramSubject.java index 981c685c..c88a3b4f 100644 --- a/src/test/java/com/google/javascript/clutz/ProgramSubject.java +++ b/src/test/java/com/google/javascript/clutz/ProgramSubject.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; @@ -124,6 +125,7 @@ private String[] parse() throws AssertionError { opts.partialInput = true; } opts.collidingProvides = ImmutableSet.of("colliding_provide.aliased"); + opts.browserResolverStrippedPrefixes = Arrays.asList("abs_strip_for_testing"); List sourceFiles = new ArrayList<>(); diff --git a/src/test/java/com/google/javascript/clutz/partial/es6_module.d.ts b/src/test/java/com/google/javascript/clutz/partial/es6_module.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/test/java/com/google/javascript/clutz/partial/es6_module.js b/src/test/java/com/google/javascript/clutz/partial/es6_module.js new file mode 100644 index 00000000..018b7641 --- /dev/null +++ b/src/test/java/com/google/javascript/clutz/partial/es6_module.js @@ -0,0 +1,10 @@ +import * as rel from './rel/module/name'; +import * as rel2 from '/abs/module/name'; +//!! abs_strip_for_testing is special cased in the test harness. +import * as abs from 'abs_strip_for_testing/module/name'; + +export let x = rel.x + abs.x + rel2.x; + +//!! The emit of this file is intentionally empty. +//!! So far clutz doesn't support es6 modules properly, but the test +//!! makes sure that it doesn't reject them either.