Skip to content

Commit

Permalink
Use browser for package main
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed May 14, 2023
1 parent 300daeb commit b404cbb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 49 deletions.
86 changes: 44 additions & 42 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,54 +775,56 @@ rebuild:
depPkg.Subpath = strings.Join(a[1:], "/")
}
depPkg.Submodule = toModuleName(depPkg.Subpath)
if err == nil {
task := &BuildTask{
BuildArgs: task.BuildArgs,
Pkg: depPkg,
Target: task.Target,
Dev: task.Dev,
wd: task.getRealWD(),
}
depESM, depNpm, _, e := task.analyze()
if e != nil {
log.Warnf("analyze dep(%s) failed: %s", depPkg, e.Error())
}
if e == nil {
// support edge case like `require('htmlparser').Parser`
if bytes.HasPrefix(p, []byte{'.'}) {
// right shift to strip the object `key`
shift := 0
for i, l := 1, len(p); i < l; i++ {
if !isJSIdentChar(p[i]) {
break
}
shift++
}
importName := string(p[1 : shift+1])
if importName != "default" && includes(depESM.NamedExports, importName) {
cjsImportNames.Add(importName)
marked = true
p = p[1:]
} else {
cjsImportNames.Add("default")
marked = true
var np NpmPackage
if utils.ParseJSONFile(path.Join(task.getRealWD(), "node_modules", depPkg.Name, "package.json"), &np) == nil {
depPkg.Version = np.Version
}
task := &BuildTask{
BuildArgs: task.BuildArgs,
Pkg: depPkg,
Target: task.Target,
Dev: task.Dev,
wd: task.getRealWD(),
}
depESM, depNpm, _, e := task.analyze()
if e != nil {
log.Warnf("analyze dep(%s) failed: %s", depPkg, e.Error())
}
if e == nil {
// support edge case like `require('htmlparser').Parser`
if bytes.HasPrefix(p, []byte{'.'}) {
// right shift to strip the object `key`
shift := 0
for i, l := 1, len(p); i < l; i++ {
if !isJSIdentChar(p[i]) {
break
}
shift++
}
// if the dep is an es6 module
if !marked && depNpm.Module != "" {
if depESM.HasExportDefault && len(depESM.NamedExports) == 1 {
cjsImportNames.Add("default")
} else if bytes.Contains(outputContent, []byte("__esModule")) {
cjsImportNames.Add("*?")
} else {
cjsImportNames.Add("*")
}
importName := string(p[1 : shift+1])
if importName != "default" && includes(depESM.NamedExports, importName) {
cjsImportNames.Add(importName)
marked = true
p = p[1:]
} else {
cjsImportNames.Add("default")
marked = true
}
if !marked && includes(depESM.NamedExports, "__esModule") && depESM.HasExportDefault {
}
// if the dep is an es6 module
if !marked && depNpm.Module != "" {
if depESM.HasExportDefault && len(depESM.NamedExports) == 1 {
cjsImportNames.Add("default")
} else if bytes.Contains(outputContent, []byte("__esModule")) {
cjsImportNames.Add("*?")
} else {
cjsImportNames.Add("*")
marked = true
}
marked = true
}
if !marked && includes(depESM.NamedExports, "__esModule") && depESM.HasExportDefault {
cjsImportNames.Add("*")
marked = true
}
}
}
Expand Down
43 changes: 37 additions & 6 deletions server/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,46 @@ func (task *BuildTask) fixNpmPackage(p NpmPackage) NpmPackage {
}
}

browserMain := p.Browser["."]
if browserMain != "" && fileExists(path.Join(nmDir, p.Name, browserMain)) {
isEsm, _, _ := validateJS(path.Join(nmDir, p.Name, browserMain))
if isEsm {
log.Infof("%s@%s: use `browser` field as module: %s", p.Name, p.Version, browserMain)
p.Module = browserMain
if p.Module != "" && !strings.HasPrefix(p.Module, "./") {
p.Module = "." + utils.CleanPath(p.Module)
}
if p.Main != "" && !strings.HasPrefix(p.Main, "./") {
p.Main = "." + utils.CleanPath(p.Main)
}

if !task.isServerTarget() {
var browserModule string
var browserMain string
if p.Module != "" {
m, ok := p.Browser[p.Module]
if ok {
browserModule = m
}
} else if p.Main != "" {
m, ok := p.Browser[p.Main]
if ok {
browserMain = m
}
}
if browserModule == "" && browserMain == "" {
if m := p.Browser["."]; m != "" && fileExists(path.Join(nmDir, p.Name, m)) {
isEsm, _, _ := validateJS(path.Join(nmDir, p.Name, m))
if isEsm {
browserModule = m
} else {
browserMain = m
}
}
}
if browserModule != "" {
p.Module = browserModule
} else if browserMain != "" {
p.Main = browserMain
}
}

// log.Debug("[main]", task.Pkg, p.Main, p.Module)

if p.Types == "" && p.Main != "" {
if strings.HasSuffix(p.Main, ".d.ts") {
p.Types = p.Main
Expand Down
2 changes: 1 addition & 1 deletion test/webtorrent/webtorrent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import webtorrent from "http://localhost:8080/[email protected]?target=es2022";
import webtorrent from "http://localhost:8080/[email protected]?target=es2022&dev";

Deno.test("webtorrent", async () => {
assertEquals(typeof webtorrent, "function");
Expand Down

0 comments on commit b404cbb

Please sign in to comment.