diff --git a/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java b/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java index 9c3fbf9dfa1..a8a41403e2d 100644 --- a/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java +++ b/src/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModules.java @@ -93,10 +93,22 @@ private static class LocalQName { *

Absolute import paths need to match the registered path exactly. Some {@link * ModuleLoader.ModulePath}s will have a leading slash and some won't. So in order to have * everything line up AND preserve schemes (if they exist) then just strip leading /. + * + *

Additionally if any path contains a protocol it will be stripped only the path part will + * remain. This is done heuristically as we cannot use {@link java.net.URL} or {@link + * java.nio.file.Path} due to GWT. As a result of stripping this cross-domain imports are not + * compatible with this pass. */ private static String normalizePath(String path) { - if (path.startsWith("/")) { - return path.substring(1); + int indexOfProtocol = path.indexOf("://"); + if (indexOfProtocol > -1) { + path = path.substring(indexOfProtocol + 3); + int indexOfSlash = path.indexOf('/'); + if (indexOfSlash > -1) { + path = path.substring(indexOfSlash + 1); + } + } else if (path.startsWith("/")) { + path = path.substring(1); } return path; } @@ -383,6 +395,12 @@ private void addExport(Node definePropertiesLit, String exportedName, LocalQName } private void visitImport(ModuleLoader.ModulePath path, Node importDecl) { + if (importDecl.getLastChild().getString().contains("://")) { + compiler.report( + JSError.make( + importDecl, Es6ToEs3Util.CANNOT_CONVERT, "Module requests with protocols.")); + } + // Normalize the import path according to the module resolution scheme so that bundles are // compatible with the compiler's module loader options. importRequests.add( diff --git a/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java b/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java index 3b1805aa46e..01fcd2dcf10 100644 --- a/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java +++ b/test/com/google/javascript/jscomp/Es6RewriteModulesToCommonJsModulesTest.java @@ -467,7 +467,7 @@ public void testExportWithArguments() { "}, 'testcode', []);")); } - public void testFileNameIsPreservedInRegisteredPathWhenNotEscaping() { + public void testProtocolAndDomainAreRemovedInRegisteredPathWhenNotEscaping() { pathEscaper = PathEscaper.CANONICALIZE_ONLY; test( srcs(SourceFile.fromCode("https://example.domain.google.com/test.js", "export var x;")), @@ -486,21 +486,11 @@ public void testFileNameIsPreservedInRegisteredPathWhenNotEscaping() { " },", " });", " var x;", - "}, 'https://example.domain.google.com/test.js', []);")))); + "}, 'test.js', []);")))); } - public void testFileNameIsPreservedInRequiredPathWhenNotEscaping() { - pathEscaper = PathEscaper.CANONICALIZE_ONLY; - test( - srcs(SourceFile.fromCode("test.js", "import 'file://imported.js';")), - expected( - SourceFile.fromCode( - "https://example.domain.google.com/test.js", - lines( - "$jscomp.registerAndLoadModule(function($$require, $$exports, $$module) {", - " 'test pragma';", - " var module$file_$$imported = $$require('file://imported.js');", - "}, 'test.js', ['file://imported.js']);")))); + public void testProtocolInImportPathIsError() { + testError("import * as foo from 'file://imported.js';", Es6ToEs3Util.CANNOT_CONVERT); } public void testRegisteredPathDoesNotIncludeModuleRoot() {