Skip to content

Commit

Permalink
Fix subpath of ?alias (close #671)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Jun 30, 2023
1 parent a0ccd9b commit 8e10bbc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 33 deletions.
73 changes: 49 additions & 24 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ rebuild:
if len(task.Args.alias) > 0 {
if name, ok := task.Args.alias[specifier]; ok {
specifier = name
} else {
pkgName, subpath := splitPkgPath(specifier)
if subpath != "" {
if name, ok := task.Args.alias[pkgName]; ok {
specifier = name + "/" + subpath
}
}
}
}

Expand Down Expand Up @@ -857,11 +864,11 @@ rebuild:

func (task *BuildTask) resolveExternal(specifier string, kind api.ResolveKind) string {
var importPath string
// remote imports
// check `?external`
if task.Args.external.Has(specifier) || task.Args.external.Has("*") {
importPath = specifier
}
// sub module
// is sub-module of current package
if importPath == "" && strings.HasPrefix(specifier, task.Pkg.Name+"/") {
subPath := strings.TrimPrefix(specifier, task.Pkg.Name+"/")
subPkg := Pkg{
Expand Down Expand Up @@ -951,30 +958,48 @@ func (task *BuildTask) resolveExternal(specifier string, kind api.ResolveKind) s
}
// common npm dependency
if importPath == "" {
version := "latest"
pkgName, subpath := splitPkgPath(specifier)
if pkgName == task.Pkg.Name {
version = task.Pkg.Version
} else if v, ok := task.npm.Dependencies[pkgName]; ok {
version = v
} else if v, ok := task.npm.PeerDependencies[pkgName]; ok {
version = v
}
pkg := Pkg{
Name: pkgName,
Version: version,
Subpath: subpath,
Submodule: toModuleName(subpath),
}
args := BuildArgs{
alias: map[string]string{},
deps: task.Args.deps,
external: task.Args.external,
treeShaking: newStringSet(), // remove `?exports` args
conditions: newStringSet(), // remove `?conditions` args
denoStdVersion: task.Args.denoStdVersion,
if task.Args.external.Has(pkgName) {
importPath = specifier
} else {
version := "latest"
if pkgName == task.Pkg.Name {
version = task.Pkg.Version
} else if pkg, ok := task.Args.deps.Get(pkgName); ok {
version = pkg.Version
} else if v, ok := task.npm.Dependencies[pkgName]; ok {
version = v
} else if v, ok := task.npm.PeerDependencies[pkgName]; ok {
version = v
}
pkg := Pkg{
Name: pkgName,
Version: version,
Subpath: subpath,
Submodule: toModuleName(subpath),
}
args := BuildArgs{
alias: task.Args.alias,
deps: task.Args.deps,
external: task.Args.external,
denoStdVersion: task.Args.denoStdVersion,
ignoreAnnotations: task.Args.ignoreAnnotations,
ignoreRequire: task.Args.ignoreAnnotations,
keepNames: task.Args.ignoreAnnotations,
treeShaking: newStringSet(), // remove `?exports` args
conditions: newStringSet(), // remove `?conditions` args
}
if stableBuild[pkgName] {
args.alias = map[string]string{}
args.deps = []Pkg{}
args.external = newStringSet()
args.denoStdVersion = ""
args.ignoreAnnotations = false
args.ignoreRequire = false
args.keepNames = false
}
importPath = task.getImportPath(pkg, encodeBuildArgsPrefix(args, pkg, false))
}
importPath = task.getImportPath(pkg, encodeBuildArgsPrefix(args, pkg, false))
}
if importPath == "" {
importPath = specifier
Expand Down
17 changes: 10 additions & 7 deletions server/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,17 @@ func (task *BuildTask) getSavepath() string {

func (task *BuildTask) getPackageInfo(name string) (pkg Pkg, p NpmPackage, fromPackageJSON bool, err error) {
pkgName, subpath := splitPkgPath(name)
v, ok := task.npm.Dependencies[pkgName]
if !ok {
v, ok = task.npm.PeerDependencies[pkgName]
}
if !ok {
v = "latest"
var version string
if pkg, ok := task.Args.deps.Get(pkgName); ok {
version = pkg.Version
} else if v, ok := task.npm.Dependencies[pkgName]; ok {
version = v
} else if v, ok = task.npm.PeerDependencies[pkgName]; ok {
version = v
} else {
version = "latest"
}
p, fromPackageJSON, err = getPackageInfo(task.installDir, pkgName, v)
p, fromPackageJSON, err = getPackageInfo(task.installDir, pkgName, version)
if err == nil {
pkg = Pkg{
Name: p.Name,
Expand Down
5 changes: 3 additions & 2 deletions server/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func esmHandler() rex.Handle {
isPined := ctx.Form.Has("pin") || hasBuildVerPrefix || stableBuild[reqPkg.Name]
isWorker := ctx.Form.Has("worker")
noCheck := ctx.Form.Has("no-check") || ctx.Form.Has("no-dts")
ignoreRequire := ctx.Form.Has("ignore-require") || ctx.Form.Has("no-require") || reqPkg.Name == "@unocss/preset-icons"
ignoreRequire := ctx.Form.Has("ignore-require") || reqPkg.Name == "@unocss/preset-icons"
keepNames := ctx.Form.Has("keep-names")
ignoreAnnotations := ctx.Form.Has("ignore-annotations")

Expand Down Expand Up @@ -1222,14 +1222,15 @@ func esmHandler() rex.Handle {
} else {
if len(esm.Deps) > 0 {
// TODO: lookup deps of deps
ctx.AddHeader("X-Esm-Deps", strings.Join(sliceMap(esm.Deps, func(dep string) string {
ctx.SetHeader("X-Esm-Deps", strings.Join(sliceMap(esm.Deps, func(dep string) string {
if strings.HasPrefix(dep, "/") {
dep = fmt.Sprintf("%s%s%s", cdnOrigin, cfg.BasePath, dep)
}
fmt.Fprintf(buf, `import "%s";%s`, dep, EOL)
return dep
}), ", "))
}
ctx.SetHeader("X-Esm-Id", taskID)
fmt.Fprintf(buf, `export * from "%s%s/%s";%s`, cdnOrigin, cfg.BasePath, taskID, EOL)
if (esm.FromCJS || esm.HasExportDefault) && (treeShaking.Len() == 0 || treeShaking.Has("default")) {
fmt.Fprintf(buf, `export { default } from "%s%s/%s";%s`, cdnOrigin, cfg.BasePath, taskID, EOL)
Expand Down
13 changes: 13 additions & 0 deletions test/issue-671/issue-671.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { assertStringIncludes } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("issue #671", async () => {
const res = await fetch(
"http://localhost:8080/[email protected]?alias=react:preact/compat,react-dom:preact/compat",
);
await res.body?.cancel();
const id = res.headers.get("x-esm-id");
const code = await fetch(
`http://localhost:8080/${id}`,
).then((res) => res.text());
assertStringIncludes(code, "compat/jsx-runtime.js");
});

0 comments on commit 8e10bbc

Please sign in to comment.