Skip to content

Commit

Permalink
Fix *.js.js path (close #627)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed May 14, 2023
1 parent b404cbb commit 74cbdbc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
26 changes: 13 additions & 13 deletions server/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,31 +637,31 @@ func queryESMBuild(id string) (*ESMBuild, bool) {
return nil, false
}

var esmExts = []string{".mjs", ".js", ".jsx", ".mts", ".ts", ".tsx"}

func resovleESModule(wd string, packageName string, moduleSpecifier string) (resolvedName string, namedExports []string, err error) {
pkgDir := path.Join(wd, "node_modules", packageName)
switch path.Ext(moduleSpecifier) {
case ".mjs", ".js", ".jsx", ".mts", ".ts", ".tsx":
resolvedName = moduleSpecifier
default:
for _, ext := range []string{".mjs", ".js", ".jsx", ".mts", ".ts", ".tsx"} {
resolvedName = moduleSpecifier
if !fileExists(path.Join(pkgDir, resolvedName)) {
for _, ext := range esmExts {
name := moduleSpecifier + ext
if fileExists(path.Join(pkgDir, name)) {
resolvedName = name
break
}
}
if !fileExists(path.Join(pkgDir, resolvedName)) && dirExists(path.Join(pkgDir, moduleSpecifier)) {
for _, ext := range []string{".mjs", ".js", ".jsx", ".mts", ".ts", ".tsx"} {
name := path.Join(moduleSpecifier, "index"+ext)
if fileExists(path.Join(pkgDir, name)) {
resolvedName = name
break
}
}
if !fileExists(path.Join(pkgDir, resolvedName)) && dirExists(path.Join(pkgDir, moduleSpecifier)) {
for _, ext := range esmExts {
name := path.Join(moduleSpecifier, "index"+ext)
if fileExists(path.Join(pkgDir, name)) {
resolvedName = name
break
}
}
}
if !fileExists(path.Join(pkgDir, resolvedName)) {
for _, ext := range []string{".mjs", ".js", ".jsx", ".mts", ".ts", ".tsx"} {
for _, ext := range esmExts {
if strings.HasSuffix(resolvedName, "index/index"+ext) {
resolvedName = strings.TrimSuffix(resolvedName, "/index"+ext) + ext
break
Expand Down
9 changes: 7 additions & 2 deletions server/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,13 @@ func (a PkgSlice) String() string {
func toModuleName(path string) string {
if path != "" {
submodule := path
submodule = strings.TrimSuffix(submodule, ".js")
submodule = strings.TrimSuffix(submodule, ".mjs")
if strings.HasSuffix(submodule, ".mjs") {
submodule = strings.TrimSuffix(submodule, ".mjs")
} else if strings.HasSuffix(submodule, ".cjs") {
submodule = strings.TrimSuffix(submodule, ".cjs")
} else {
submodule = strings.TrimSuffix(submodule, ".js")
}
submodule = strings.TrimSuffix(submodule, "/index")
return submodule
}
Expand Down
9 changes: 9 additions & 0 deletions test/issue-627/issue-627.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import jsGrammar from "http://localhost:8080/@wooorm/[email protected]/lang/source.js.js";
import tsGrammar from "http://localhost:8080/@wooorm/[email protected]/lang/source.ts.js";

Deno.test("issue #627", async () => {
assertEquals(jsGrammar.scopeName, "source.js");
assertEquals(tsGrammar.scopeName, "source.ts");
});

0 comments on commit 74cbdbc

Please sign in to comment.