diff --git a/server/build_resolver.go b/server/build_resolver.go index 23c57808..c9686fea 100644 --- a/server/build_resolver.go +++ b/server/build_resolver.go @@ -615,10 +615,6 @@ func (ctx *BuildContext) resolveExternalModule(specifier string, kind api.Resolv return specifier, nil } - if ctx.externalAll { - return specifier, nil - } - defer func() { if err == nil && !withTypeJSON { resolvedPathFull := resolvedPath @@ -641,6 +637,12 @@ func (ctx *BuildContext) resolveExternalModule(specifier string, kind api.Resolv } }() + // check `?external` + if ctx.externalAll || ctx.args.external.Has(toPackageName(specifier)) { + resolvedPath = specifier + return + } + // if it's the main entry of current package if pkgJson := ctx.pkgJson; specifier == pkgJson.Name || specifier == pkgJson.PkgName { resolvedPath = ctx.getImportPath(EsmPath{ @@ -664,12 +666,6 @@ func (ctx *BuildContext) resolveExternalModule(specifier string, kind api.Resolv return } - // check `?external` - if ctx.externalAll || ctx.args.external.Has(toPackageName(specifier)) { - resolvedPath = specifier - return - } - // if it's a sub-module of current package if strings.HasPrefix(specifier, ctx.pkgJson.Name+"/") { subPath := strings.TrimPrefix(specifier, ctx.pkgJson.Name+"/") diff --git a/test/build-args/external.test.ts b/test/build-args/external.test.ts index 182417a8..dc3b927a 100644 --- a/test/build-args/external.test.ts +++ b/test/build-args/external.test.ts @@ -21,6 +21,49 @@ Deno.test("`?external` query", async () => { const res = await fetch("http://localhost:8080/*preact@10.23.2/denonext/hooks.mjs"); assertStringIncludes(await res.text(), 'from"preact"'); } + { + // ?external=react + const res = await fetch("http://localhost:8080/react-dom@19.0.0/X-ZXJlYWN0/es2022/client.mjs"); + const code = await res.text(); + assertStringIncludes(code, 'from"react"'); + assertStringIncludes(code, 'from"/react-dom@'); + assertStringIncludes(code, 'from"/scheduler@'); + } + { + // ?external=react,react-dom + const res = await fetch("http://localhost:8080/react-dom@19.0.0/X-ZXJlYWN0LHJlYWN0LWRvbQ/es2022/client.mjs"); + const code = await res.text(); + assertStringIncludes(code, 'from"react"'); + assertStringIncludes(code, 'from"react-dom"'); + assertStringIncludes(code, 'from"/scheduler@'); + } + { + // ?external=react,react-dom,scheduler + const res = await fetch("http://localhost:8080/react-dom@19.0.0/X-ZXJlYWN0LHJlYWN0LWRvbSxzY2hlZHVsZXI/es2022/client.mjs"); + const code = await res.text(); + assertStringIncludes(code, 'from"react"'); + assertStringIncludes(code, 'from"react-dom"'); + assertStringIncludes(code, 'from"scheduler"'); + } + { + const res = await fetch("http://localhost:8080/*react-dom@19.0.0/es2022/client.mjs"); + const code = await res.text(); + assertStringIncludes(code, 'from"react"'); + assertStringIncludes(code, 'from"react-dom"'); + assertStringIncludes(code, 'from"scheduler"'); + } + { + const res = await fetch("http://localhost:8080/react-dom@19.0.0/client?external=react,react-dom,scheduler"); + assertStringIncludes(await res.text(), '"/react-dom@19.0.0/X-ZXJlYWN0LHJlYWN0LWRvbSxzY2hlZHVsZXI/denonext/client.mjs"'); + } + { + const res = await fetch("http://localhost:8080/react-dom@19.0.0/client?external=react,scheduler,react-dom"); + assertStringIncludes(await res.text(), '"/react-dom@19.0.0/X-ZXJlYWN0LHJlYWN0LWRvbSxzY2hlZHVsZXI/denonext/client.mjs"'); + } + { + const res = await fetch("http://localhost:8080/react-dom@19.0.0/client?external=scheduler,react,react-dom"); + assertStringIncludes(await res.text(), '"/react-dom@19.0.0/X-ZXJlYWN0LHJlYWN0LWRvbSxzY2hlZHVsZXI/denonext/client.mjs"'); + } }); Deno.test("drop invalid `?external`", async () => {