Skip to content

Commit

Permalink
Fix ?deps query in deep (#867)
Browse files Browse the repository at this point in the history
* Fix `?deps` query in deep

* Move testings

* Move testings
  • Loading branch information
ije authored Aug 17, 2024
1 parent a3ca5f2 commit 3e71c5a
Show file tree
Hide file tree
Showing 49 changed files with 377 additions and 460 deletions.
50 changes: 25 additions & 25 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ func (ctx *BuildContext) Build() (ret BuildResult, err error) {
}

// check if the package is deprecated
if ctx.isDeprecated == "" && !ctx.module.FromGithub && !strings.HasPrefix(ctx.module.Name, "@jsr/") {
if ctx.isDeprecated == "" && !ctx.module.FromGithub && !strings.HasPrefix(ctx.module.PkgName, "@jsr/") {
var info PackageJSON
info, err = ctx.npmrc.fetchPackageInfo(ctx.module.Name, ctx.module.Version)
info, err = ctx.npmrc.fetchPackageInfo(ctx.module.PkgName, ctx.module.PkgVersion)
if err != nil {
return
}
Expand Down Expand Up @@ -183,8 +183,8 @@ func (ctx *BuildContext) install() (err error) {
return
}
ctx.packageJson = ctx.normalizePackageJSON(pkgJson)
ctx.wd = path.Join(ctx.npmrc.NpmDir(), ctx.module.Fullname())
ctx.pkgDir = path.Join(ctx.wd, "node_modules", ctx.module.Name)
ctx.wd = path.Join(ctx.npmrc.NpmDir(), ctx.module.PackageName())
ctx.pkgDir = path.Join(ctx.wd, "node_modules", ctx.module.PkgName)
if rp, e := os.Readlink(ctx.pkgDir); e == nil {
ctx.pnpmPkgDir = path.Join(path.Dir(ctx.pkgDir), rp)
} else {
Expand All @@ -198,7 +198,7 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
// build json
if strings.HasSuffix(ctx.module.SubModule, ".json") {
nmDir := path.Join(ctx.wd, "node_modules")
jsonPath := path.Join(nmDir, ctx.module.Name, ctx.module.SubModule)
jsonPath := path.Join(nmDir, ctx.module.PkgName, ctx.module.SubModule)
if existsFile(jsonPath) {
var jsonData []byte
jsonData, err = os.ReadFile(jsonPath)
Expand Down Expand Up @@ -228,7 +228,7 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
typesOnly := strings.HasPrefix(ctx.packageJson.Name, "@types/") || (entry.esm == "" && entry.cjs == "" && entry.dts != "")
if typesOnly {
result.TypesOnly = true
result.Dts = "/" + ctx.module.Fullname() + entry.dts[1:]
result.Dts = "/" + ctx.module.PackageName() + entry.dts[1:]
ctx.transformDTS(entry.dts)
return
}
Expand All @@ -240,29 +240,29 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {

// cjs reexport
if reexport != "" {
pkg, _, e := ctx.lookupDep(reexport, false)
mod, _, e := ctx.lookupDep(reexport, false)
if e != nil {
err = e
return
}
// create a new build context to check if the reexported module has default export
b := NewBuildContext(ctx.zoneId, ctx.npmrc, pkg, ctx.args, ctx.target, BundleFalse, ctx.dev, false)
b := NewBuildContext(ctx.zoneId, ctx.npmrc, mod, ctx.args, ctx.target, BundleFalse, ctx.dev, false)
err = b.install()
if err != nil {
return
}
var r BuildResult
entry := b.resolveEntry(pkg)
entry := b.resolveEntry(mod)
r, _, err = b.lexer(&entry, false)
if err != nil {
return
}
importUrl := ctx.getImportPath(mod, ctx.getBuildArgsPrefix(mod, false))
buf := bytes.NewBuffer(nil)
importPath := ctx.getImportPath(pkg, ctx.getBuildArgsPrefix(pkg, false))
fmt.Fprintf(buf, `export * from "%s";`, importPath)
fmt.Fprintf(buf, `export * from "%s";`, importUrl)
if r.HasDefaultExport {
fmt.Fprintf(buf, "\n")
fmt.Fprintf(buf, `export { default } from "%s";`, importPath)
fmt.Fprintf(buf, `export { default } from "%s";`, importUrl)
}
_, err = fs.WriteFile(ctx.getSavepath(), buf)
if err != nil {
Expand All @@ -275,7 +275,7 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
var entryPoint string
var input *api.StdinOptions

entryModuleSpecifier := ctx.module.Name
entryModuleSpecifier := ctx.module.PkgName
if ctx.module.SubModule != "" {
entryModuleSpecifier += "/" + ctx.module.SubModule
}
Expand Down Expand Up @@ -461,8 +461,8 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
if forceNpmSpecifiers[specifier] && ctx.target == "denonext" {
version := ""
pkgName, _, subPath, _ := splitPkgPath(specifier)
if pkgName == ctx.module.Name {
version = ctx.module.Version
if pkgName == ctx.module.PkgName {
version = ctx.module.PkgVersion
} else if v, ok := ctx.packageJson.Dependencies[pkgName]; ok && regexpFullVersion.MatchString(v) {
version = v
} else if v, ok := ctx.packageJson.PeerDependencies[pkgName]; ok && regexpFullVersion.MatchString(v) {
Expand Down Expand Up @@ -513,8 +513,8 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {

// externalize the _parent_ module
// e.g. "react/jsx-runtime" imports "react"
if ctx.module.SubModule != "" && specifier == ctx.module.Name && ctx.bundleMode != BundleAll {
externalPath, err := ctx.resolveExternalModule(ctx.module.Name, args.Kind)
if ctx.module.SubModule != "" && specifier == ctx.module.PkgName && ctx.bundleMode != BundleAll {
externalPath, err := ctx.resolveExternalModule(ctx.module.PkgName, args.Kind)
if err != nil {
return api.OnResolveResult{}, err
}
Expand Down Expand Up @@ -578,10 +578,10 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
if path.Ext(fullFilepath) == "" || !existsFile(fullFilepath) {
subPath := utils.CleanPath(moduleSpecifier)[1:]
entry := ctx.resolveEntry(Module{
Name: ctx.module.Name,
Version: ctx.module.Version,
SubModule: toModuleBareName(subPath, true),
SubPath: subPath,
PkgName: ctx.module.PkgName,
PkgVersion: ctx.module.PkgVersion,
SubModule: toModuleBareName(subPath, true),
SubPath: subPath,
})
if args.Kind == api.ResolveJSImportStatement || args.Kind == api.ResolveJSDynamicImport {
if entry.esm != "" {
Expand Down Expand Up @@ -951,8 +951,8 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
if !ctx.isDenoTarget() {
options.JSX = api.JSXAutomatic
if ctx.args.jsxRuntime != nil {
if ctx.args.external.Has(ctx.args.jsxRuntime.Name) || ctx.args.externalAll {
options.JSXImportSource = ctx.args.jsxRuntime.Name
if ctx.args.external.Has(ctx.args.jsxRuntime.PkgName) || ctx.args.externalAll {
options.JSXImportSource = ctx.args.jsxRuntime.PkgName
} else {
options.JSXImportSource = "/" + ctx.args.jsxRuntime.String()
}
Expand Down Expand Up @@ -1197,7 +1197,7 @@ rebuild:
finalContent.Write(jsContent)

if ctx.isDeprecated != "" {
fmt.Fprintf(finalContent, `console.warn("%%c[esm.sh]%%c %%cdeprecated%%c %s@%s: %s", "color:grey", "", "color:red", "");%s`, ctx.module.Name, ctx.module.Version, strings.ReplaceAll(ctx.isDeprecated, "\"", "\\\""), "\n")
fmt.Fprintf(finalContent, `console.warn("%%c[esm.sh]%%c %%cdeprecated%%c %s@%s: %s", "color:grey", "", "color:red", "");%s`, ctx.module.PkgName, ctx.module.PkgVersion, strings.ReplaceAll(ctx.isDeprecated, "\"", "\\\""), "\n")
}

// add sourcemap Url
Expand Down Expand Up @@ -1284,7 +1284,7 @@ func (ctx *BuildContext) buildTypes() (ret BuildResult, err error) {
ctx.stage = "build"
err = ctx.transformDTS(dts)
if err == nil {
ret.Dts = "/" + ctx.module.Fullname() + dts[1:]
ret.Dts = "/" + ctx.module.PackageName() + dts[1:]
}
return
}
Expand Down
33 changes: 20 additions & 13 deletions server/build_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func decodeBuildArgs(npmrc *NpmRC, argsString string) (args BuildArgs, err error
} else if strings.HasPrefix(p, "c") {
args.conditions = append(args.conditions, strings.Split(p[1:], ",")...)
} else if strings.HasPrefix(p, "x") {
p, _, _, _, e := validateESMPath(npmrc, p[1:])
p, _, _, _, e := validateModulePath(npmrc, p[1:])
if e == nil {
args.jsxRuntime = &p
}
Expand All @@ -85,7 +85,7 @@ func encodeBuildArgs(args BuildArgs, pkg Module, isDts bool) string {
if len(args.alias) > 0 {
var ss sort.StringSlice
for from, to := range args.alias {
if from != pkg.Name {
if from != pkg.PkgName {
ss = append(ss, fmt.Sprintf("%s:%s", from, to))
}
}
Expand All @@ -97,7 +97,7 @@ func encodeBuildArgs(args BuildArgs, pkg Module, isDts bool) string {
if len(args.deps) > 0 {
var ss sort.StringSlice
for name, version := range args.deps {
if name != pkg.Name {
if name != pkg.PkgName {
ss = append(ss, fmt.Sprintf("%s@%s", name, version))
}
}
Expand All @@ -112,7 +112,7 @@ func encodeBuildArgs(args BuildArgs, pkg Module, isDts bool) string {
if args.external.Len() > 0 {
var ss sort.StringSlice
for _, name := range args.external.Values() {
if name != pkg.Name {
if name != pkg.PkgName {
ss = append(ss, name)
}
}
Expand Down Expand Up @@ -164,10 +164,10 @@ func encodeBuildArgs(args BuildArgs, pkg Module, isDts bool) string {
}

// fixBuildArgs removes invalid alias, deps, external from the build args
func fixBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, pkg Module) error {
func fixBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, module Module) error {
if len(args.alias) > 0 || len(args.deps) > 0 || args.external.Len() > 0 {
depsSet := NewStringSet()
err := walkDeps(npmrc, installDir, pkg, depsSet)
err := walkDeps(npmrc, installDir, module, depsSet)
if err != nil {
return err
}
Expand All @@ -180,7 +180,7 @@ func fixBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, pkg Module)
}
for from, to := range alias {
pkgName, _, _, _ := splitPkgPath(to)
if pkgName == pkg.Name {
if pkgName == module.PkgName {
delete(alias, from)
} else {
depsSet.Add(pkgName)
Expand All @@ -191,6 +191,10 @@ func fixBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, pkg Module)
if len(args.deps) > 0 {
newDeps := map[string]string{}
for name, version := range args.deps {
if module.PkgName == "react-dom" && name == "react" {
// react version should be the same as react-dom
continue
}
if depsSet.Has(name) {
newDeps[name] = version
}
Expand All @@ -210,20 +214,23 @@ func fixBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, pkg Module)
return nil
}

func walkDeps(npmrc *NpmRC, installDir string, pkg Module, mark *StringSet) (err error) {
if mark.Has(pkg.Name) {
func walkDeps(npmrc *NpmRC, installDir string, module Module, mark *StringSet) (err error) {
if mark.Has(module.PkgName) {
return
}
mark.Add(pkg.Name)
mark.Add(module.PkgName)
var p PackageJSON
pkgJsonPath := path.Join(installDir, "node_modules", ".pnpm", "node_modules", pkg.Name, "package.json")
pkgJsonPath := path.Join(installDir, "node_modules", ".pnpm", "node_modules", module.PkgName, "package.json")
if !existsFile(pkgJsonPath) {
pkgJsonPath = path.Join(installDir, "node_modules", pkg.Name, "package.json")
pkgJsonPath = path.Join(installDir, "node_modules", module.PkgName, "package.json")
}
if existsFile(pkgJsonPath) {
err = utils.ParseJSONFile(pkgJsonPath, &p)
} else if regexpFullVersion.MatchString(module.PkgVersion) || module.FromGithub {
p, err = npmrc.installPackage(module)
} else {
return nil
// p, err = npmrc.getPackageInfo(module.PkgName, module.PkgVersion)
}
if err != nil {
return
Expand All @@ -239,7 +246,7 @@ func walkDeps(npmrc *NpmRC, installDir string, pkg Module, mark *StringSet) (err
if strings.HasPrefix(name, "@types/") || strings.HasPrefix(name, "@babel/") || strings.HasPrefix(name, "is-") {
continue
}
err := walkDeps(npmrc, installDir, Module{Name: name, Version: version}, mark)
err := walkDeps(npmrc, installDir, Module{PkgName: name, PkgVersion: version}, mark)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions server/build_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TestEncodeBuildArgs(t *testing.T) {
external: external,
exports: exports,
conditions: conditions,
jsxRuntime: &Module{Version: "18.2.0", Name: "react"},
jsxRuntime: &Module{PkgVersion: "18.2.0", PkgName: "react"},
externalRequire: true,
keepNames: true,
ignoreAnnotations: true,
},
Module{Name: "foo"},
Module{PkgName: "foo"},
false,
)
args, err := decodeBuildArgs(nil, buildArgsString)
Expand Down
Loading

0 comments on commit 3e71c5a

Please sign in to comment.