Skip to content

Commit

Permalink
Allow module customization options for the ES5 transpiler - it also t…
Browse files Browse the repository at this point in the history
…ranspiles ES6 modules.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196752374
  • Loading branch information
johnplaisted authored and blickly committed May 17, 2018
1 parent 1873d4a commit 9ec0218
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/com/google/javascript/jscomp/transpile/BaseTranspiler.java
Expand Up @@ -88,6 +88,31 @@ public String runtime() {
* 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 =
Expand Down Expand Up @@ -149,6 +174,9 @@ protected void setOptions(CompilerOptions options) {
options.setPrettyPrint(true);
options.setWarningLevel(ES5_WARNINGS, CheckLevel.OFF);
options.setTranspileEs6ModulesToCjsModules(true);
options.setModuleResolutionMode(moduleResolution);
options.setModuleRoots(moduleRoots);
options.setBrowserResolverPrefixReplacements(prefixReplacements);

options.setSourceMapOutputPath("/dev/null");
options.setSourceMapIncludeSourcesContent(true);
Expand Down Expand Up @@ -181,31 +209,18 @@ protected void setOptions(CompilerOptions options) {
*/
public static class EsmToCjsCompilerSupplier extends CompilerSupplier {

private final ResolutionMode moduleResolution;
private final ImmutableList<String> moduleRoots;
private final ImmutableMap<String, String> prefixReplacements;

public EsmToCjsCompilerSupplier() {
// Use the default resolution mode
this(new CompilerOptions().getModuleResolutionMode(), ImmutableList.of(), ImmutableMap.of());
super();
}

/**
* 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 EsmToCjsCompilerSupplier(
ModuleLoader.ResolutionMode moduleResolution,
ImmutableList<String> moduleRoots,
ImmutableMap<String, String> prefixReplacements) {
this.moduleResolution = moduleResolution;
this.moduleRoots = moduleRoots;
this.prefixReplacements = prefixReplacements;
super(moduleResolution, moduleRoots, prefixReplacements);
}


@Override
public CompileResult compile(URI path, String code) {
CompilerOptions options = new CompilerOptions();
Expand Down

0 comments on commit 9ec0218

Please sign in to comment.