Skip to content

Commit

Permalink
JsChecker: support extensionless ES6 imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Figueiredo committed Sep 11, 2019
1 parent b2a6fb7 commit ce84d28
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions closure/compiler/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,27 @@ sh_test(
":exports_data",
],
)

# TEST: ES6 module can import another via a workspace-relative extensionless import path.

closure_js_library(
name = "es6module1",
srcs = ["es6module1.js"],
no_closure_library = True,
)

closure_js_library(
name = "es6module2",
srcs = ["es6module2.js"],
# suppress = ["moduleLoad"],
no_closure_library = True,
deps = [":es6module1"],
)

closure_js_binary(
name = "es6module_bin",
compilation_level = "ADVANCED",
entry_points = ["/closure/compiler/test/es6module2"],
language = "ECMASCRIPT5",
deps = [":es6module2"],
)
15 changes: 15 additions & 0 deletions closure/compiler/test/es6module1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2019 The Closure Rules Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export const name = "module1";
17 changes: 17 additions & 0 deletions closure/compiler/test/es6module2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2019 The Closure Rules Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { name } from "/closure/compiler/test/es6module1";

console.log(name);
6 changes: 6 additions & 0 deletions java/com/google/javascript/jscomp/CheckStrictDeps.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ private void visitEs6Import(NodeTraversal t, Node n) {
private void checkNamespaceIsProvided(NodeTraversal t, Node n, String namespace) {
if (namespace.startsWith("/") || namespace.startsWith(".")) {
// TODO(jart): Unify path resolution with ModuleLoader.
// NOTE(robfig): To enable usage of extensionless ES6 modules,
// copy these 3 lines from NodeModuleResolver.java.
if (!namespace.endsWith(".js")) {
namespace += ".js";
}

Webpath me = Webpath.get(t.getSourceName());
if (!me.isAbsolute()) {
me = Webpath.get("/").resolve(me);
Expand Down

0 comments on commit ce84d28

Please sign in to comment.