Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,12 @@ public CommandLineConfig setJsonStreamMode(JsonStreamMode mode) {
return this;
}

public CommandLineConfig setModuleResolutionMode(ModuleLoader.ResolutionMode mode) {
this.moduleResolutionMode = mode;
return this;
}

private ModuleLoader.ResolutionMode moduleResolutionMode = ModuleLoader.ResolutionMode.LEGACY;
}

/**
Expand Down
30 changes: 24 additions & 6 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,23 @@ private static class Flags {
usage = "Rewrite ES6 library calls to use polyfills provided by the compiler's runtime.")
private boolean rewritePolyfills = true;

@Option(name = "--print_source_after_each_pass",
hidden = true,
usage = "Whether to iteratively print resulting JS source per pass.")
private boolean printSourceAfterEachPass = false;
@Option(
name = "--print_source_after_each_pass",
hidden = true,
usage = "Whether to iteratively print resulting JS source per pass."
)
private boolean printSourceAfterEachPass = false;

@Option(
name = "--module_resolution",
hidden = false,
usage =
"Specifies how the compiler locates modules. BROWSER requires all module imports "
+ "to begin with a '.' or '/' and have a file extension. NODE uses the node module "
+ "rules. LEGACY prepends a '/' to any import not already beginning with a "
+ "'.' or '/'."
)
private ModuleLoader.ResolutionMode moduleResolutionMode = ModuleLoader.ResolutionMode.NODE;

@Argument
private List<String> arguments = new ArrayList<>();
Expand Down Expand Up @@ -781,7 +794,10 @@ private void parse(List<String> args) throws CmdLineException {
.putAll(
"JS Modules",
ImmutableList.of(
"js_module_root", "process_common_js_modules", "transform_amd_modules"))
"js_module_root",
"module_resolution",
"process_common_js_modules",
"transform_amd_modules"))
.putAll(
"Library and Framework Specific",
ImmutableList.of(
Expand Down Expand Up @@ -1507,7 +1523,8 @@ private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err
.setTracerMode(flags.tracerMode)
.setInstrumentationTemplateFile(flags.instrumentationFile)
.setNewTypeInference(flags.useNewTypeInference)
.setJsonStreamMode(flags.jsonStreamMode);
.setJsonStreamMode(flags.jsonStreamMode)
.setModuleResolutionMode(flags.moduleResolutionMode);
}
errorStream = null;
}
Expand Down Expand Up @@ -1682,6 +1699,7 @@ protected CompilerOptions createOptions() {
options.setPrintSourceAfterEachPass(flags.printSourceAfterEachPass);
options.setStrictModeInput(flags.strictModeInput);
options.setEmitUseStrict(flags.emitUseStrict);
options.setModuleResolutionMode(flags.moduleResolutionMode);

return options;
}
Expand Down
12 changes: 9 additions & 3 deletions src/com/google/javascript/jscomp/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1481,9 +1481,15 @@ Node parseInputs() {
|| options.transformAMDToCJSModules
|| options.processCommonJSModules) {

this.moduleLoader = new ModuleLoader(this, options.moduleRoots, inputs);

if (options.processCommonJSModules) {
this.moduleLoader =
new ModuleLoader(
this,
options.moduleRoots,
inputs,
ModuleLoader.PathResolver.RELATIVE,
options.moduleResolutionMode);

if (options.moduleResolutionMode == ModuleLoader.ResolutionMode.NODE) {
this.moduleLoader.setPackageJsonMainEntries(processJsonInputs(inputs));
}

Expand Down
16 changes: 15 additions & 1 deletion src/com/google/javascript/jscomp/CompilerOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -1086,10 +1086,13 @@ public void setWrapGoogModulesForWhitespaceOnly(boolean enable) {
*/
private boolean isStrictModeInput = true;

/** Which algorithm to use for locating ES6 and CommonJS modules */
ModuleLoader.ResolutionMode moduleResolutionMode;

/**
* Should the compiler print its configuration options to stderr when they are initialized?
*
* <p> Default {@code false}.
* <p>Default {@code false}.
*/
public void setPrintConfig(boolean printConfig) {
this.printConfig = printConfig;
Expand All @@ -1109,6 +1112,9 @@ public CompilerOptions() {
// Which environment to use
environment = Environment.BROWSER;

// Modules
moduleResolutionMode = ModuleLoader.ResolutionMode.LEGACY;

// Checks
skipNonTranspilationPasses = false;
devMode = DevMode.OFF;
Expand Down Expand Up @@ -2648,6 +2654,14 @@ public CompilerOptions setEmitUseStrict(boolean emitUseStrict) {
return this;
}

public ModuleLoader.ResolutionMode getModuleResolutionMode() {
return this.moduleResolutionMode;
}

public void setModuleResolutionMode(ModuleLoader.ResolutionMode mode) {
this.moduleResolutionMode = mode;
}

@Override
public String toString() {
String strValue =
Expand Down
4 changes: 3 additions & 1 deletion src/com/google/javascript/jscomp/DiagnosticGroups.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ public DiagnosticGroup forName(String name) {

public static final DiagnosticGroup COMMON_JS_MODULE_LOAD =
DiagnosticGroups.registerGroup(
"commonJsModuleLoad", ProcessCommonJSModules.COMMON_JS_MODULE_LOAD_ERROR);
"commonJsModuleLoad",
ProcessCommonJSModules.SUSPICIOUS_EXPORTS_ASSIGNMENT,
ProcessCommonJSModules.UNKNOWN_REQUIRE_ENSURE);

public static final DiagnosticGroup GLOBAL_THIS =
DiagnosticGroups.registerGroup("globalThis",
Expand Down
1 change: 0 additions & 1 deletion src/com/google/javascript/jscomp/ExportTestFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.javascript.jscomp.parsing.parser.util.format.SimpleFormat;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;

import java.util.regex.Pattern;

/**
Expand Down
43 changes: 32 additions & 11 deletions src/com/google/javascript/jscomp/ProcessCommonJSModules.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public final class ProcessCommonJSModules implements CompilerPass {
private static final String EXPORTS = "exports";
private static final String MODULE = "module";

public static final DiagnosticType COMMON_JS_MODULE_LOAD_ERROR = DiagnosticType.error(
"JSC_COMMONJS_MODULE_LOAD_ERROR",
"Failed to load module \"{0}\"");

public static final DiagnosticType UNKNOWN_REQUIRE_ENSURE =
DiagnosticType.warning(
"JSC_COMMONJS_UNKNOWN_REQUIRE_ENSURE_ERROR", "Unrecognized require.ensure call: {0}");
Expand Down Expand Up @@ -356,9 +352,16 @@ public void visit(NodeTraversal t, Node n, Node parent) {
/** Visit require calls. Emit corresponding goog.require call. */
private void visitRequireCall(NodeTraversal t, Node require, Node parent) {
String requireName = require.getSecondChild().getString();
ModulePath modulePath = t.getInput().getPath().resolveCommonJsModule(requireName);
ModulePath modulePath =
t.getInput()
.getPath()
.resolveJsModule(
requireName,
require.getSourceFileName(),
require.getLineno(),
require.getCharno());
if (modulePath == null) {
compiler.report(t.makeError(require, COMMON_JS_MODULE_LOAD_ERROR, requireName));
// The module loader will issue an error
return;
}

Expand Down Expand Up @@ -700,9 +703,16 @@ public void visit(NodeTraversal t, Node n, Node parent) {
*/
private void visitRequireCall(NodeTraversal t, Node require, Node parent) {
String requireName = require.getSecondChild().getString();
ModulePath modulePath = t.getInput().getPath().resolveCommonJsModule(requireName);
ModulePath modulePath =
t.getInput()
.getPath()
.resolveJsModule(
requireName,
require.getSourceFileName(),
require.getLineno(),
require.getCharno());
if (modulePath == null) {
compiler.report(t.makeError(require, COMMON_JS_MODULE_LOAD_ERROR, requireName));
// The module loader will issue an error
return;
}

Expand Down Expand Up @@ -1192,7 +1202,11 @@ private String getModuleImportName(NodeTraversal t, Node n) {
&& rValue.getSecondChild().isString()
&& t.getScope().getVar(rValue.getFirstChild().getQualifiedName()) == null) {
String requireName = rValue.getSecondChild().getString();
ModulePath modulePath = t.getInput().getPath().resolveCommonJsModule(requireName);
ModulePath modulePath =
t.getInput()
.getPath()
.resolveJsModule(
requireName, n.getSourceFileName(), n.getLineno(), n.getCharno());
if (modulePath == null) {
return null;
}
Expand Down Expand Up @@ -1229,9 +1243,16 @@ private void fixTypeNode(NodeTraversal t, Node typeNode) {
}

String moduleName = name.substring(0, endIndex);
ModulePath modulePath = t.getInput().getPath().resolveCommonJsModule(moduleName);
ModulePath modulePath =
t.getInput()
.getPath()
.resolveJsModule(
moduleName,
typeNode.getSourceFileName(),
typeNode.getLineno(),
typeNode.getCharno());
if (modulePath == null) {
t.makeError(typeNode, COMMON_JS_MODULE_LOAD_ERROR, moduleName);
// The module loader will issue an error
return;
}

Expand Down
46 changes: 41 additions & 5 deletions src/com/google/javascript/jscomp/ProcessEs6Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,21 @@ private void visitImport(NodeTraversal t, Node importDecl, Node parent) {
// These are rewritten to plain namespace object accesses.
moduleName = importName.substring("goog:".length());
} else {
moduleName = t.getInput().getPath().resolveEs6Module(importName).toModuleName();
ModuleLoader.ModulePath modulePath =
t.getInput()
.getPath()
.resolveJsModule(
importName,
importDecl.getSourceFileName(),
importDecl.getLineno(),
importDecl.getCharno());
if (modulePath == null) {
// The module loader issues an error
// Fall back to assuming the module is a file path
modulePath = t.getInput().getPath().resolveModuleAsPath(importName);
}

moduleName = modulePath.toModuleName();
}

for (Node child : importDecl.children()) {
Expand Down Expand Up @@ -270,8 +284,18 @@ private void visitExport(NodeTraversal t, Node export, Node parent) {
parent.addChildBefore(importNode, export);
visit(t, importNode, parent);

String moduleName =
t.getInput().getPath().resolveEs6Module(moduleIdentifier.getString()).toModuleName();
ModuleLoader.ModulePath path =
t.getInput()
.getPath()
.resolveJsModule(
moduleIdentifier.getString(),
export.getSourceFileName(),
export.getLineno(),
export.getCharno());
if (path == null) {
path = t.getInput().getPath().resolveModuleAsPath(moduleIdentifier.getString());
}
String moduleName = path.toModuleName();

for (Node exportSpec : export.getFirstChild().children()) {
String nameFromOtherModule = exportSpec.getFirstChild().getString();
Expand Down Expand Up @@ -539,8 +563,20 @@ private void fixTypeNode(NodeTraversal t, Node typeNode) {
}

String moduleName = name.substring(0, endIndex);
String globalModuleName =
t.getInput().getPath().resolveEs6Module(moduleName).toModuleName();
ModuleLoader.ModulePath path =
t.getInput()
.getPath()
.resolveJsModule(
moduleName,
typeNode.getSourceFileName(),
typeNode.getLineno(),
typeNode.getCharno());

if (path == null) {
path = t.getInput().getPath().resolveModuleAsPath(moduleName);
}
String globalModuleName = path.toModuleName();

typeNode.setString(
localTypeName == null ? globalModuleName : globalModuleName + localTypeName);
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/com/google/javascript/jscomp/deps/JsFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ protected boolean parseLine(String line) throws ParseException {
if (arg.startsWith("goog:")) {
requires.add(arg.substring(5)); // cut off the "goog:" prefix
} else {
requires.add(file.resolveEs6Module(arg).toModuleName());
ModuleLoader.ModulePath path = file.resolveJsModule(arg);
if (path == null) {
path = file.resolveModuleAsPath(arg);
}
requires.add(path.toModuleName());
}
}
}
Expand Down
Loading