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

Commit

Permalink
Make clutz support Closure's BROWSER_WITH_TRANSFORMED_PREFIXES resolu…
Browse files Browse the repository at this point in the history
…tion.

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.
  • Loading branch information
rkirov committed Jan 30, 2019
1 parent 95df710 commit 729641b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/google/javascript/clutz/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> browserResolverStrippedPrefixes = new ArrayList<>();

@Argument
@Option(name = "--", handler = StopOptionHandler.class)
List<String> arguments = new ArrayList<>();
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/google/javascript/clutz/ProgramSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SourceFile> sourceFiles = new ArrayList<>();

Expand Down
Empty file.
10 changes: 10 additions & 0 deletions src/test/java/com/google/javascript/clutz/partial/es6_module.js
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 729641b

Please sign in to comment.