Skip to content

Commit 20f7614

Browse files
authored
Use .mjs extenstion for sub-module build (#917)
1 parent f494547 commit 20f7614

File tree

19 files changed

+48
-73
lines changed

19 files changed

+48
-73
lines changed

.github/ISSUE_TEMPLATE/failing-import-in-browser.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ After `onload` I got this:
2424

2525
## Additional info
2626

27-
- **esm.sh version**:
28-
- **Browser version**:
27+
- **Browser info**:

.github/ISSUE_TEMPLATE/failing-import-in-deno.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ After running `deno run` I got this:
2424

2525
## Additional info
2626

27-
- **esm.sh version**:
2827
- **Deno version**:

.github/ISSUE_TEMPLATE/failing-import-with-reejs.md

Lines changed: 0 additions & 28 deletions
This file was deleted.

server/build_resolver.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ func (ctx *BuildContext) Path() string {
9797
}
9898

9999
name := strings.TrimSuffix(path.Base(esmPath.PkgName), ".js")
100-
extname := ".mjs"
101-
102100
if esmPath.SubBareName != "" {
103-
name = esmPath.SubBareName
104-
extname = ".js"
101+
if esmPath.SubBareName == name {
102+
// if the sub-module name is same as the package name
103+
name = "__" + esmPath.SubBareName
104+
} else {
105+
name = esmPath.SubBareName
106+
}
105107
// workaround for es5-ext "../#/.." path
106108
if esmPath.PkgName == "es5-ext" {
107109
name = strings.ReplaceAll(name, "/#/", "/%23/")
@@ -117,22 +119,24 @@ func (ctx *BuildContext) Path() string {
117119
name += ".nobundle"
118120
}
119121
ctx.path = fmt.Sprintf(
120-
"/%s/%s%s/%s%s",
122+
"/%s/%s%s/%s.mjs",
121123
esmPath.PackageName(),
122124
ctx.getBuildArgsPrefix(ctx.target == "types"),
123125
ctx.target,
124126
name,
125-
extname,
126127
)
127128
return ctx.path
128129
}
129130

130131
func (ctx *BuildContext) getImportPath(esmPath ESMPath, buildArgsPrefix string) string {
131132
name := strings.TrimSuffix(path.Base(esmPath.PkgName), ".js")
132-
extname := ".mjs"
133133
if esmPath.SubBareName != "" {
134-
name = esmPath.SubBareName
135-
extname = ".js"
134+
if esmPath.SubBareName == name {
135+
// if the sub-module name is same as the package name
136+
name = "__" + esmPath.SubBareName
137+
} else {
138+
name = esmPath.SubBareName
139+
}
136140
// workaround for es5-ext "../#/.." path
137141
if esmPath.PkgName == "es5-ext" {
138142
name = strings.ReplaceAll(name, "/#/", "/%23/")
@@ -142,12 +146,11 @@ func (ctx *BuildContext) getImportPath(esmPath ESMPath, buildArgsPrefix string)
142146
name += ".development"
143147
}
144148
return fmt.Sprintf(
145-
"/%s/%s%s/%s%s",
149+
"/%s/%s%s/%s.mjs",
146150
esmPath.PackageName(),
147151
buildArgsPrefix,
148152
ctx.target,
149153
name,
150-
extname,
151154
)
152155
}
153156

@@ -653,7 +656,7 @@ func (ctx *BuildContext) resolveConditionExportEntry(conditions *OrderedMap, mTy
653656
}
654657
} else if ctx.isDenoTarget() {
655658
var condition interface{}
656-
for _, conditionName := range []string{"deno", "workerd", "worker", "node"} {
659+
for _, conditionName := range []string{"deno", "node"} {
657660
condition = conditions.Get(conditionName)
658661
if condition != nil {
659662
// entry.ibc = conditionName != "browser"

server/embed/tsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function tsx() {
9595
async function initTsx() {
9696
const pkg = "/@esm.sh/[email protected]";
9797
const [m, w] = await Promise.all([
98-
import(pkg + "/$TARGET/@esm.sh/tsx.mjs"),
98+
import(pkg + "/$TARGET/tsx.mjs"),
9999
fetch(urlFromCurrentModule(pkg + "/pkg/tsx_bg.wasm")),
100100
]);
101101
await m.default(w);

server/esm_router.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ func esmRouter(debug bool) rex.Handle {
12631263
}
12641264

12651265
// match path `PKG@VERSION/X-${args}/esnext/SUBPATH`
1266-
argsX := false
1266+
xArgs := false
12671267
if resType == ESMBuild || resType == ESMDts {
12681268
a := strings.Split(esm.SubBareName, "/")
12691269
if len(a) > 1 && strings.HasPrefix(a[0], "X-") {
@@ -1274,12 +1274,12 @@ func esmRouter(debug bool) rex.Handle {
12741274
esm.SubPath = strings.Join(strings.Split(esm.SubPath, "/")[1:], "/")
12751275
esm.SubBareName = toModuleBareName(esm.SubPath, true)
12761276
buildArgs = args
1277-
argsX = true
1277+
xArgs = true
12781278
}
12791279
}
12801280

12811281
// fix the build args that are from the query
1282-
if !argsX {
1282+
if !xArgs {
12831283
err := normalizeBuildArgs(npmrc, path.Join(npmrc.StoreDir(), esm.PackageName()), &buildArgs, esm)
12841284
if err != nil {
12851285
return rex.Status(500, err.Error())
@@ -1338,7 +1338,7 @@ func esmRouter(debug bool) rex.Handle {
13381338
return bytes.ReplaceAll(buffer, []byte("{ESM_CDN_ORIGIN}"), []byte(cdnOrigin))
13391339
}
13401340

1341-
if !argsX {
1341+
if !xArgs {
13421342
externalRequire := query.Has("external-require")
13431343
// workaround: force "unocss/preset-icons" to external `require` calls
13441344
if !externalRequire && esm.PkgName == "@unocss/preset-icons" {
@@ -1384,7 +1384,7 @@ func esmRouter(debug bool) rex.Handle {
13841384
isDev = true
13851385
}
13861386
basename := strings.TrimSuffix(path.Base(esm.PkgName), ".js")
1387-
if strings.HasSuffix(submodule, ".css") && !strings.HasSuffix(esm.SubPath, ".js") {
1387+
if strings.HasSuffix(submodule, ".css") && !strings.HasSuffix(esm.SubPath, ".mjs") {
13881388
if submodule == basename+".css" {
13891389
esm.SubBareName = ""
13901390
target = maybeTarget
@@ -1393,9 +1393,11 @@ func esmRouter(debug bool) rex.Handle {
13931393
return rex.Redirect(url, http.StatusFound)
13941394
}
13951395
} else {
1396-
isMjs := strings.HasSuffix(esm.SubPath, ".mjs")
1397-
if isMjs && submodule == basename {
1396+
if submodule == basename {
13981397
submodule = ""
1398+
} else if submodule == "__"+basename {
1399+
// the sub-module name is same as the package name
1400+
submodule = basename
13991401
}
14001402
esm.SubBareName = submodule
14011403
target = maybeTarget

test/build-args/bundle.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Deno.test("?bundle with ?external", async () => {
2020
Deno.test("?bundle=false", async () => {
2121
const res = await fetch("http://localhost:8080/@pyscript/[email protected]/dist/py-terminal-XWbSa71s?bundle=false&target=es2022");
2222
res.body?.cancel();
23-
assertEquals(res.headers.get("x-esm-path")!, "/@pyscript/[email protected]/es2022/dist/py-terminal-XWbSa71s.nobundle.js");
23+
assertEquals(res.headers.get("x-esm-path")!, "/@pyscript/[email protected]/es2022/dist/py-terminal-XWbSa71s.nobundle.mjs");
2424
const res2 = await fetch(new URL(res.headers.get("x-esm-path")!, "http://localhost:8080"));
2525
const code = await res2.text();
26-
assertStringIncludes(code, "./core.nobundle.js");
27-
assertStringIncludes(code, "./error-96hMSEw8.nobundle.js");
26+
assertStringIncludes(code, "./core.nobundle.mjs");
27+
assertStringIncludes(code, "./error-96hMSEw8.nobundle.mjs");
2828
});

test/build-args/deps.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Deno.test("?deps", async () => {
55
const res = await fetch("http://localhost:8080/@mui/[email protected][email protected],[email protected],[email protected]&target=es2022");
66
const code = await res.text();
77
assertStringIncludes(code, 'import "/[email protected]/es2022/react-dom.mjs"');
8-
assertStringIncludes(code, 'import "/[email protected]/es2022/jsx-runtime.js"');
8+
assertStringIncludes(code, 'import "/[email protected]/es2022/jsx-runtime.mjs"');
99
assertStringIncludes(code, 'import "/[email protected]/es2022/react.mjs"');
1010
assertStringIncludes(code, 'import "/react-transition-group@^[email protected],[email protected]&target=es2022"');
1111
assertStringIncludes(code, 'export * from "/@mui/[email protected]/X-ZHJlYWN0LWRvbUAxOC4yLjAscmVhY3RAMTguMi4w/es2022/material.mjs"');
@@ -16,7 +16,7 @@ Deno.test("?deps", async () => {
1616
const res = await fetch("http://localhost:8080/@mui/[email protected]/X-ZHJlYWN0LWRvbUAxOC4yLjAscmVhY3RAMTguMi4w/es2022/material.mjs");
1717
const code = await res.text();
1818
assertStringIncludes(code, 'from"/[email protected]/es2022/react-dom.mjs"');
19-
assertStringIncludes(code, 'from"/[email protected]/es2022/jsx-runtime.js"');
19+
assertStringIncludes(code, 'from"/[email protected]/es2022/jsx-runtime.mjs"');
2020
assertStringIncludes(code, 'from"/[email protected]/es2022/react.mjs"');
2121
assertStringIncludes(code, 'from"/react-transition-group@^[email protected],[email protected]&target=es2022"');
2222
assertStringIncludes(code, 'from"/@mui/system@^5.16.7/[email protected]&target=es2022"');

test/build-args/external.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ Deno.test("`?external` query", async () => {
77

88
const res2 = await fetch("http://localhost:8080/*[email protected]/jsx-runtime");
99
const code2 = await res2.text();
10-
assertStringIncludes(code2, '"/[email protected]/X-Kg/denonext/jsx-runtime.js"');
10+
assertStringIncludes(code2, '"/[email protected]/X-Kg/denonext/jsx-runtime.mjs"');
1111

1212
const res3 = await fetch("http://localhost:8080/[email protected]/hooks?external=preact");
1313
const code3 = await res3.text();
14-
assertStringIncludes(code3, '"/[email protected]/X-ZXByZWFjdA/denonext/hooks.js"');
14+
assertStringIncludes(code3, '"/[email protected]/X-ZXByZWFjdA/denonext/hooks.mjs"');
1515
});
1616

1717
Deno.test("drop invalid `?external`", async () => {

test/common/jotail.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertExists } from "jsr:@std/assert";
22

33
Deno.test("jotail", async () => {
4-
const utils = await import("http://localhost:8080/[email protected]/es2022/vanilla/utils.js");
4+
const utils = await import("http://localhost:8080/[email protected]/es2022/vanilla/utils.mjs");
55
assertExists(utils.splitAtom);
66
});

0 commit comments

Comments
 (0)