Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use .mjs extenstion for sub-module build #917

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/ISSUE_TEMPLATE/failing-import-in-browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ After `onload` I got this:

## Additional info

- **esm.sh version**:
- **Browser version**:
- **Browser info**:
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/failing-import-in-deno.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ After running `deno run` I got this:

## Additional info

- **esm.sh version**:
- **Deno version**:
28 changes: 0 additions & 28 deletions .github/ISSUE_TEMPLATE/failing-import-with-reejs.md

This file was deleted.

27 changes: 15 additions & 12 deletions server/build_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ func (ctx *BuildContext) Path() string {
}

name := strings.TrimSuffix(path.Base(esmPath.PkgName), ".js")
extname := ".mjs"

if esmPath.SubBareName != "" {
name = esmPath.SubBareName
extname = ".js"
if esmPath.SubBareName == name {
// if the sub-module name is same as the package name
name = "__" + esmPath.SubBareName
} else {
name = esmPath.SubBareName
}
// workaround for es5-ext "../#/.." path
if esmPath.PkgName == "es5-ext" {
name = strings.ReplaceAll(name, "/#/", "/%23/")
Expand All @@ -117,22 +119,24 @@ func (ctx *BuildContext) Path() string {
name += ".nobundle"
}
ctx.path = fmt.Sprintf(
"/%s/%s%s/%s%s",
"/%s/%s%s/%s.mjs",
esmPath.PackageName(),
ctx.getBuildArgsPrefix(ctx.target == "types"),
ctx.target,
name,
extname,
)
return ctx.path
}

func (ctx *BuildContext) getImportPath(esmPath ESMPath, buildArgsPrefix string) string {
name := strings.TrimSuffix(path.Base(esmPath.PkgName), ".js")
extname := ".mjs"
if esmPath.SubBareName != "" {
name = esmPath.SubBareName
extname = ".js"
if esmPath.SubBareName == name {
// if the sub-module name is same as the package name
name = "__" + esmPath.SubBareName
} else {
name = esmPath.SubBareName
}
// workaround for es5-ext "../#/.." path
if esmPath.PkgName == "es5-ext" {
name = strings.ReplaceAll(name, "/#/", "/%23/")
Expand All @@ -142,12 +146,11 @@ func (ctx *BuildContext) getImportPath(esmPath ESMPath, buildArgsPrefix string)
name += ".development"
}
return fmt.Sprintf(
"/%s/%s%s/%s%s",
"/%s/%s%s/%s.mjs",
esmPath.PackageName(),
buildArgsPrefix,
ctx.target,
name,
extname,
)
}

Expand Down Expand Up @@ -653,7 +656,7 @@ func (ctx *BuildContext) resolveConditionExportEntry(conditions *OrderedMap, mTy
}
} else if ctx.isDenoTarget() {
var condition interface{}
for _, conditionName := range []string{"deno", "workerd", "worker", "node"} {
for _, conditionName := range []string{"deno", "node"} {
condition = conditions.Get(conditionName)
if condition != nil {
// entry.ibc = conditionName != "browser"
Expand Down
2 changes: 1 addition & 1 deletion server/embed/tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function tsx() {
async function initTsx() {
const pkg = "/@esm.sh/[email protected]";
const [m, w] = await Promise.all([
import(pkg + "/$TARGET/@esm.sh/tsx.mjs"),
import(pkg + "/$TARGET/tsx.mjs"),
fetch(urlFromCurrentModule(pkg + "/pkg/tsx_bg.wasm")),
]);
await m.default(w);
Expand Down
16 changes: 9 additions & 7 deletions server/esm_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ func esmRouter(debug bool) rex.Handle {
}

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

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

if !argsX {
if !xArgs {
externalRequire := query.Has("external-require")
// workaround: force "unocss/preset-icons" to external `require` calls
if !externalRequire && esm.PkgName == "@unocss/preset-icons" {
Expand Down Expand Up @@ -1384,7 +1384,7 @@ func esmRouter(debug bool) rex.Handle {
isDev = true
}
basename := strings.TrimSuffix(path.Base(esm.PkgName), ".js")
if strings.HasSuffix(submodule, ".css") && !strings.HasSuffix(esm.SubPath, ".js") {
if strings.HasSuffix(submodule, ".css") && !strings.HasSuffix(esm.SubPath, ".mjs") {
if submodule == basename+".css" {
esm.SubBareName = ""
target = maybeTarget
Expand All @@ -1393,9 +1393,11 @@ func esmRouter(debug bool) rex.Handle {
return rex.Redirect(url, http.StatusFound)
}
} else {
isMjs := strings.HasSuffix(esm.SubPath, ".mjs")
if isMjs && submodule == basename {
if submodule == basename {
submodule = ""
} else if submodule == "__"+basename {
// the sub-module name is same as the package name
submodule = basename
}
esm.SubBareName = submodule
target = maybeTarget
Expand Down
6 changes: 3 additions & 3 deletions test/build-args/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Deno.test("?bundle with ?external", async () => {
Deno.test("?bundle=false", async () => {
const res = await fetch("http://localhost:8080/@pyscript/[email protected]/dist/py-terminal-XWbSa71s?bundle=false&target=es2022");
res.body?.cancel();
assertEquals(res.headers.get("x-esm-path")!, "/@pyscript/[email protected]/es2022/dist/py-terminal-XWbSa71s.nobundle.js");
assertEquals(res.headers.get("x-esm-path")!, "/@pyscript/[email protected]/es2022/dist/py-terminal-XWbSa71s.nobundle.mjs");
const res2 = await fetch(new URL(res.headers.get("x-esm-path")!, "http://localhost:8080"));
const code = await res2.text();
assertStringIncludes(code, "./core.nobundle.js");
assertStringIncludes(code, "./error-96hMSEw8.nobundle.js");
assertStringIncludes(code, "./core.nobundle.mjs");
assertStringIncludes(code, "./error-96hMSEw8.nobundle.mjs");
});
4 changes: 2 additions & 2 deletions test/build-args/deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Deno.test("?deps", async () => {
const res = await fetch("http://localhost:8080/@mui/[email protected][email protected],[email protected],[email protected]&target=es2022");
const code = await res.text();
assertStringIncludes(code, 'import "/[email protected]/es2022/react-dom.mjs"');
assertStringIncludes(code, 'import "/[email protected]/es2022/jsx-runtime.js"');
assertStringIncludes(code, 'import "/[email protected]/es2022/jsx-runtime.mjs"');
assertStringIncludes(code, 'import "/[email protected]/es2022/react.mjs"');
assertStringIncludes(code, 'import "/react-transition-group@^[email protected],[email protected]&target=es2022"');
assertStringIncludes(code, 'export * from "/@mui/[email protected]/X-ZHJlYWN0LWRvbUAxOC4yLjAscmVhY3RAMTguMi4w/es2022/material.mjs"');
Expand All @@ -16,7 +16,7 @@ Deno.test("?deps", async () => {
const res = await fetch("http://localhost:8080/@mui/[email protected]/X-ZHJlYWN0LWRvbUAxOC4yLjAscmVhY3RAMTguMi4w/es2022/material.mjs");
const code = await res.text();
assertStringIncludes(code, 'from"/[email protected]/es2022/react-dom.mjs"');
assertStringIncludes(code, 'from"/[email protected]/es2022/jsx-runtime.js"');
assertStringIncludes(code, 'from"/[email protected]/es2022/jsx-runtime.mjs"');
assertStringIncludes(code, 'from"/[email protected]/es2022/react.mjs"');
assertStringIncludes(code, 'from"/react-transition-group@^[email protected],[email protected]&target=es2022"');
assertStringIncludes(code, 'from"/@mui/system@^5.16.7/[email protected]&target=es2022"');
Expand Down
4 changes: 2 additions & 2 deletions test/build-args/external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Deno.test("`?external` query", async () => {

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

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

Deno.test("drop invalid `?external`", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/common/jotail.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertExists } from "jsr:@std/assert";

Deno.test("jotail", async () => {
const utils = await import("http://localhost:8080/[email protected]/es2022/vanilla/utils.js");
const utils = await import("http://localhost:8080/[email protected]/es2022/vanilla/utils.mjs");
assertExists(utils.splitAtom);
});
2 changes: 1 addition & 1 deletion test/common/rc-util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertExists } from "jsr:@std/assert";

import * as dynamicCSS from "http://localhost:8080/[email protected]/es/Dom/dynamicCSS.js";
import * as dynamicCSS from "http://localhost:8080/[email protected]/es/Dom/dynamicCSS.mjs";

Deno.test("Export all members when the package is not a standard ES module", async () => {
assertExists(dynamicCSS.updateCSS);
Expand Down
6 changes: 3 additions & 3 deletions test/fix-url/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Deno.test("query as version suffix", async () => {
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
assert(!res.headers.get("vary")!.includes("User-Agent"));
assertStringIncludes(code, "/[email protected]/es2022/client.development.js");
assertStringIncludes(code, "/[email protected]/es2022/client.development.mjs");
});

Deno.test("`/jsx-runtime` in query", async () => {
Expand All @@ -17,7 +17,7 @@ Deno.test("`/jsx-runtime` in query", async () => {
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
assert(!res.headers.get("vary")!.includes("User-Agent"));
assertStringIncludes(code, "/[email protected]/es2022/jsx-runtime.development.js");
assertStringIncludes(code, "/[email protected]/es2022/jsx-runtime.development.mjs");
});

Deno.test("redirect semver versioning module for deno target", async () => {
Expand Down Expand Up @@ -112,7 +112,7 @@ Deno.test("support `/#/` in path", async () => {
assertEquals(res.status, 200);
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
assertStringIncludes(await res.text(), "/denonext/string/%23/contains.js");
assertStringIncludes(await res.text(), "/denonext/string/%23/contains.mjs");
});

Deno.test("dts-transformer: support `.d` extension", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/issue-581/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals } from "jsr:@std/assert";

import dayjs from "http://localhost:8080/[email protected]";
import relativeTime from "http://localhost:8080/[email protected]/plugin/relativeTime.js";
import relativeTime from "http://localhost:8080/[email protected]/plugin/relativeTime.mjs";

dayjs.extend(relativeTime);

Expand Down
4 changes: 2 additions & 2 deletions test/issue-671/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Deno.test("issue #671", async () => {
await res2.body?.cancel();
const esmPath2 = res2.headers.get("x-esm-path");
const code2 = await fetch("http://localhost:8080" + esmPath2).then((res) => res.text());
assertStringIncludes(code2, 'from"/[email protected]/es2020/compat/jsx-runtime.js"');
assertStringIncludes(code2, 'from"/[email protected]/es2020/compat.js"');
assertStringIncludes(code2, 'from"/[email protected]/es2020/compat/jsx-runtime.mjs"');
assertStringIncludes(code2, 'from"/[email protected]/es2020/compat.mjs"');
assertStringIncludes(code2, 'hi?alias=react:preact/compat&[email protected]&target=es2020"');
});
2 changes: 1 addition & 1 deletion test/issue-711/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert, assertEquals, assertStringIncludes } from "jsr:@std/assert";

Deno.test("issue #711", async () => {
const res = await fetch("http://localhost:8080/@pyscript/[email protected]/core.js", {
const res = await fetch("http://localhost:8080/@pyscript/[email protected]/core.mjs", {
headers: {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15",
},
Expand Down
2 changes: 1 addition & 1 deletion test/issue-728/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assertEquals } from "jsr:@std/assert";

Deno.test("issue #728", async () => {
const res2 = await fetch(
`http://localhost:8080/@wooorm/[email protected]/es2022/source.css.js`,
`http://localhost:8080/@wooorm/[email protected]/es2022/source.css.mjs`,
);
res2.body?.cancel();
assertEquals(
Expand Down
2 changes: 1 addition & 1 deletion test/issue-761/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "jsr:@std/assert";

import { streamParse } from "http://localhost:8080/gh/bablr-lang/bablr@279a549d0bf730e7d8ed008386ff157fc5b0fecd/lib/enhanceable.js";
import { streamParse } from "http://localhost:8080/gh/bablr-lang/bablr@279a549d0bf730e7d8ed008386ff157fc5b0fecd/lib/enhanceable.mjs";

Deno.test("issue #761", () => {
assertEquals(typeof streamParse, "function");
Expand Down
2 changes: 1 addition & 1 deletion test/vue/transfrom-sfc-on-fly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Deno.test("Transfrom SFC on fly", async () => {
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
const code = await res.text();
assertStringIncludes(code, "/[email protected]/");
assertStringIncludes(code, "/PhAirplay.vue.js");
assertStringIncludes(code, "/PhAirplay.vue.mjs");
}
});
6 changes: 3 additions & 3 deletions test/worker/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ Deno.test("esm-worker", { sanitizeOps: false, sanitizeResources: false }, async
assertEquals(res10.status, 200);
assertEquals(res10.headers.get("Cache-Control"), "public, max-age=31536000, immutable");
assertStringIncludes(js, `__Process$`);
assert(!js.includes("/node/process.js"));
assert(!js.includes("/node/process.mjs"));

const res11 = await fetch(`${workerOrigin}/[email protected]/es2022/typescript.mjs.map`);
assertEquals(res11.status, 200);
Expand All @@ -315,7 +315,7 @@ Deno.test("esm-worker", { sanitizeOps: false, sanitizeResources: false }, async
headers: { "User-Agent": "ES/2022" },
});
const modUrl = new URL(res.headers.get("x-esm-path")!, workerOrigin);
assert(modUrl.pathname.endsWith("/es2022/server.js"));
assert(modUrl.pathname.endsWith("/es2022/server.mjs"));
assertEquals(res.status, 200);
assertEquals(res.headers.get("Content-Type"), "application/javascript; charset=utf-8");
assertEquals(res.headers.get("Cache-Control"), "public, max-age=3600");
Expand Down Expand Up @@ -401,7 +401,7 @@ Deno.test("esm-worker", { sanitizeOps: false, sanitizeResources: false }, async
await t.step("jsr", async () => {
const res = await fetch(`${workerOrigin}/jsr/@std/encoding/base64`, { headers: { "User-Agent": "ES/2022" } });
const modUrl = new URL(res.headers.get("x-esm-path")!, workerOrigin);
assert(modUrl.pathname.endsWith("/es2022/base64.js"));
assert(modUrl.pathname.endsWith("/es2022/base64.mjs"));
assertEquals(res.status, 200);
assertEquals(res.headers.get("Content-Type"), "application/javascript; charset=utf-8");
assertEquals(res.headers.get("Cache-Control"), "public, max-age=3600");
Expand Down