Skip to content

Commit

Permalink
Fix build with flag external=* (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Jan 17, 2025
1 parent 52b46e2 commit ced51f6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
16 changes: 6 additions & 10 deletions server/build_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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+"/")
Expand Down
43 changes: 43 additions & 0 deletions test/build-args/external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,49 @@ Deno.test("`?external` query", async () => {
const res = await fetch("http://localhost:8080/*[email protected]/denonext/hooks.mjs");
assertStringIncludes(await res.text(), 'from"preact"');
}
{
// ?external=react
const res = await fetch("http://localhost:8080/[email protected]/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/[email protected]/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/[email protected]/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/*[email protected]/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/[email protected]/client?external=react,react-dom,scheduler");
assertStringIncludes(await res.text(), '"/[email protected]/X-ZXJlYWN0LHJlYWN0LWRvbSxzY2hlZHVsZXI/denonext/client.mjs"');
}
{
const res = await fetch("http://localhost:8080/[email protected]/client?external=react,scheduler,react-dom");
assertStringIncludes(await res.text(), '"/[email protected]/X-ZXJlYWN0LHJlYWN0LWRvbSxzY2hlZHVsZXI/denonext/client.mjs"');
}
{
const res = await fetch("http://localhost:8080/[email protected]/client?external=scheduler,react,react-dom");
assertStringIncludes(await res.text(), '"/[email protected]/X-ZXJlYWN0LHJlYWN0LWRvbSxzY2hlZHVsZXI/denonext/client.mjs"');
}
});

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

0 comments on commit ced51f6

Please sign in to comment.