From 23a1b7782ef1fa9f62f0e3a8e40aca8626317da2 Mon Sep 17 00:00:00 2001 From: X Date: Sun, 1 Sep 2024 11:14:19 +0800 Subject: [PATCH] Fix `encodeBuildArgsPrefix` function --- server/build_args.go | 9 ++++++--- test/external-all/preact.test.tsx | 5 ++++- test/external/preact.test.tsx | 12 +++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/server/build_args.go b/server/build_args.go index 7beb355eb..7092390bb 100644 --- a/server/build_args.go +++ b/server/build_args.go @@ -94,12 +94,15 @@ func encodeBuildArgsPrefix(args BuildArgs, pkg Pkg, isDts bool) string { for name := range info.PeerDependencies { pkgDeps.Add(name) } + if pkg.SubPath != "" { + pkgDeps.Add(pkg.Name) + } } } if len(args.alias) > 0 && pkgDeps.Len() > 0 { var ss sort.StringSlice for from, to := range args.alias { - if from != pkg.Name { + if from != pkg.Name || pkg.SubPath != "" { ss = append(ss, fmt.Sprintf("%s:%s", from, to)) } } @@ -111,7 +114,7 @@ func encodeBuildArgsPrefix(args BuildArgs, pkg Pkg, isDts bool) string { if len(args.deps) > 0 && pkgDeps.Len() > 0 { var ss sort.StringSlice for _, p := range args.deps { - if p.Name != pkg.Name { + if p.Name != pkg.Name || pkg.SubPath != "" { ss = append(ss, fmt.Sprintf("%s@%s", p.Name, p.Version)) } } @@ -123,7 +126,7 @@ func encodeBuildArgsPrefix(args BuildArgs, pkg Pkg, isDts bool) string { if args.external.Len() > 0 && (args.external.Has("*") || pkgDeps.Len() > 0) { var ss sort.StringSlice for _, name := range args.external.Values() { - if name != pkg.Name { + if name != pkg.Name || pkg.SubPath != "" { ss = append(ss, name) } } diff --git a/test/external-all/preact.test.tsx b/test/external-all/preact.test.tsx index 5ebf425e0..98a2bb2eb 100644 --- a/test/external-all/preact.test.tsx +++ b/test/external-all/preact.test.tsx @@ -30,5 +30,8 @@ Deno.test("external all", () => { Deno.test("external all 2", async () => { const res = await fetch("http://localhost:8080/*preact@10.23.2/jsx-runtime"); const code = await res.text(); - assertStringIncludes(code, "preact@10.23.2/X-ZS8q/"); + assertStringIncludes(code, "preact@10.23.2/X-ZS8q/denonext/"); + const res2 = await fetch("http://localhost:8080/*preact@10.23.2"); + const code2 = await res2.text(); + assertStringIncludes(code2, "preact@10.23.2/denonext/preact.mjs"); }); diff --git a/test/external/preact.test.tsx b/test/external/preact.test.tsx index 948d6e1cb..682af0f22 100644 --- a/test/external/preact.test.tsx +++ b/test/external/preact.test.tsx @@ -1,4 +1,4 @@ -import { assert } from "https://deno.land/std@0.180.0/testing/asserts.ts"; +import { assert, assertStringIncludes } from "https://deno.land/std@0.180.0/testing/asserts.ts"; import { h } from "preact"; import render from "preact-render-to-string"; @@ -26,3 +26,13 @@ Deno.test("external", () => { const html = render(); assert(html == "

just now

"); }); + +Deno.test("external 2", async () => { + const res = await fetch("http://localhost:8080/preact@10.23.2/hooks?external=preact"); + const code = await res.text(); + assertStringIncludes(code, "preact@10.23.2/X-ZS9wcmVhY3Q/"); + + const res2 = await fetch("http://localhost:8080/preact@10.23.2?external=preact&target=es2022"); + const code2 = await res2.text(); + assertStringIncludes(code2, "preact@10.23.2/es2022/preact.mjs"); +});