Skip to content

Commit e51b5bd

Browse files
ChadKillingsworthbrad4d
authored andcommitted
Fix the interaction between absolute path imports and module roots
Closes #2343 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=152771274
1 parent 57cb431 commit e51b5bd

11 files changed

+658
-299
lines changed

src/com/google/javascript/jscomp/Compiler.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -1562,10 +1562,20 @@ Node parseInputs() {
15621562
options.moduleRoots,
15631563
inputs,
15641564
ModuleLoader.PathResolver.RELATIVE,
1565-
options.moduleResolutionMode);
1565+
options.moduleResolutionMode,
1566+
null);
15661567

15671568
if (options.moduleResolutionMode == ModuleLoader.ResolutionMode.NODE) {
1568-
this.moduleLoader.setPackageJsonMainEntries(processJsonInputs(inputs));
1569+
// processJsonInputs requires a module loader to already be defined
1570+
// so we redefine it afterwards with the package.json inputs
1571+
this.moduleLoader =
1572+
new ModuleLoader(
1573+
this,
1574+
options.moduleRoots,
1575+
inputs,
1576+
ModuleLoader.PathResolver.RELATIVE,
1577+
options.moduleResolutionMode,
1578+
processJsonInputs(inputs));
15691579
}
15701580

15711581
if (options.lowerFromEs6()) {
@@ -1582,10 +1592,9 @@ Node parseInputs() {
15821592
Map<String, CompilerInput> inputModuleIdentifiers = new HashMap<>();
15831593
for (CompilerInput input : inputs) {
15841594
if (input.getKnownProvides().isEmpty()) {
1585-
ModuleIdentifier modInfo =
1586-
ModuleIdentifier.forFile(input.getSourceFile().getOriginalPath());
1587-
1588-
inputModuleIdentifiers.put(modInfo.getClosureNamespace(), input);
1595+
ModuleLoader.ModulePath modPath =
1596+
moduleLoader.resolve(input.getSourceFile().getOriginalPath());
1597+
inputModuleIdentifiers.put(modPath.toModuleName(), input);
15891598
}
15901599
}
15911600

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2017 The Closure Compiler Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.javascript.jscomp.deps;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.common.collect.ImmutableSet;
21+
import com.google.javascript.jscomp.CheckLevel;
22+
import com.google.javascript.jscomp.ErrorHandler;
23+
import com.google.javascript.jscomp.JSError;
24+
import javax.annotation.Nullable;
25+
26+
/**
27+
* Resolution algorithm for Browsers.
28+
*
29+
* <p>Only unambiguous paths are supported. Paths must specify a file extension.
30+
*/
31+
public class BrowserModuleResolver extends ModuleResolver {
32+
33+
public BrowserModuleResolver(
34+
ImmutableSet<String> modulePaths,
35+
ImmutableList<String> moduleRootPaths,
36+
ErrorHandler errorHandler) {
37+
super(modulePaths, moduleRootPaths, errorHandler);
38+
}
39+
40+
@Override
41+
@Nullable
42+
public String resolveJsModule(
43+
String scriptAddress, String moduleAddress, String sourcename, int lineno, int colno) {
44+
45+
if (ModuleLoader.isAmbiguousIdentifier(moduleAddress)) {
46+
errorHandler.report(
47+
CheckLevel.WARNING,
48+
JSError.make(
49+
sourcename,
50+
lineno,
51+
colno,
52+
ModuleLoader.INVALID_MODULE_PATH,
53+
moduleAddress,
54+
"BROWSER"));
55+
return null;
56+
}
57+
58+
String loadAddress = locate(scriptAddress, moduleAddress);
59+
if (loadAddress == null) {
60+
errorHandler.report(
61+
CheckLevel.WARNING,
62+
JSError.make(sourcename, lineno, colno, ModuleLoader.LOAD_WARNING, moduleAddress));
63+
}
64+
return loadAddress;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2017 The Closure Compiler Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.javascript.jscomp.deps;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.common.collect.ImmutableSet;
21+
import com.google.javascript.jscomp.CheckLevel;
22+
import com.google.javascript.jscomp.ErrorHandler;
23+
import com.google.javascript.jscomp.JSError;
24+
import javax.annotation.Nullable;
25+
26+
/**
27+
* Resolution algorithm historically used by the compiler.
28+
*
29+
* <p>Ambiguous paths are assumed to be absolute.
30+
*/
31+
public class LegacyModuleResolver extends ModuleResolver {
32+
33+
public LegacyModuleResolver(
34+
ImmutableSet<String> modulePaths,
35+
ImmutableList<String> moduleRootPaths,
36+
ErrorHandler errorHandler) {
37+
super(modulePaths, moduleRootPaths, errorHandler);
38+
}
39+
40+
@Nullable
41+
public String resolveJsModule(
42+
String scriptAddress, String moduleAddress, String sourcename, int lineno, int colno) {
43+
// Allow module names with or without the ".js" extension.
44+
if (!moduleAddress.endsWith(".js")) {
45+
moduleAddress += ".js";
46+
}
47+
48+
String loadAddress = locate(scriptAddress, moduleAddress);
49+
50+
if (loadAddress == null) {
51+
errorHandler.report(
52+
CheckLevel.WARNING,
53+
JSError.make(sourcename, lineno, colno, ModuleLoader.LOAD_WARNING, moduleAddress));
54+
return canonicalizePath(scriptAddress, moduleAddress);
55+
}
56+
return loadAddress;
57+
}
58+
}

0 commit comments

Comments
 (0)