Skip to content

Commit

Permalink
Update requireModeAllowList (close #520)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Feb 21, 2023
1 parent d13852c commit 13d8074
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
30 changes: 4 additions & 26 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,7 @@ func (task *BuildTask) Build() (esm *ESM, err error) {

task.stage = "install"
for i := 0; i < 3; i++ {
err = yarnAdd(task.wd, fmt.Sprintf("%s@%s", task.Pkg.Name, task.Pkg.Version))
if err == nil && !fileExists(path.Join(task.wd, "node_modules", task.Pkg.Name, "package.json")) {
defer yarnCacheClean(task.Pkg.Name)
err = fmt.Errorf("yarnAdd(%s): package.json not found", task.Pkg)
}
if err == nil {
break
}
if i < 2 {
time.Sleep(100 * time.Millisecond)
}
err = yarnAdd(task.wd, task.Pkg)
}
if err != nil {
return
Expand Down Expand Up @@ -197,8 +187,6 @@ func (task *BuildTask) build(marker *stringSet) (esm *ESM, err error) {
buf := bytes.NewBuffer(nil)
importPath := task.Pkg.ImportPath()
fmt.Fprintf(buf, `import * as __module from "%s";`, importPath)
// Default reexport all members from original module to prevent missing named exports members
fmt.Fprintf(buf, `export * from "%s";`, importPath)
if len(esm.Exports) > 0 {
var exports []string
for _, k := range esm.Exports {
Expand All @@ -214,6 +202,8 @@ func (task *BuildTask) build(marker *stringSet) (esm *ESM, err error) {
}
fmt.Fprintf(buf, "const { default: __default, ...__rest } = __module;")
fmt.Fprintf(buf, "export default (__default !== undefined ? __default : __rest);")
// Default reexport all members from original module to prevent missing named exports members
fmt.Fprintf(buf, `export * from "%s";`, importPath)
input = &api.StdinOptions{
Contents: buf.String(),
ResolveDir: task.wd,
Expand Down Expand Up @@ -775,19 +765,7 @@ esbuild:
if _, ok := builtInNodeModules[name]; !ok {
pkg, _, err := validatePkgPath(name)
if err == nil && !fileExists(path.Join(task.wd, "node_modules", pkg.Name, "package.json")) {
for i := 0; i < 3; i++ {
err = yarnAdd(task.wd, fmt.Sprintf("%s@%s", pkg.Name, pkg.Version))
if err == nil && !fileExists(path.Join(task.wd, "node_modules", pkg.Name, "package.json")) {
defer yarnCacheClean(pkg.Name)
err = fmt.Errorf("yarnAdd(%s): package.json not found", pkg)
}
if err == nil {
break
}
if i < 2 {
time.Sleep(100 * time.Millisecond)
}
}
err = yarnAdd(task.wd, pkg)
}
if err == nil {
dep, depNpm, err := initModule(task.wd, pkg, task.Target, task.DevMode)
Expand Down
1 change: 1 addition & 0 deletions server/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var requireModeAllowList = []string{
"lz-string",
"maplibre-gl",
"postcss-selector-parser",
"react-draggable",
"resolve",
"safe-buffer",
"seedrandom",
Expand Down
13 changes: 13 additions & 0 deletions server/esm.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *ESM, npm Np
}

if npm.Main != "" {
// install peer dependencies when using `requireMode`
if includes(requireModeAllowList, pkg.Name) && len(npm.PeerDependencies) > 0 {
pkgs := make([]string, len(npm.PeerDependencies))
i := 0
for n, v := range npm.PeerDependencies {
pkgs[i] = n + "@" + v
i++
}
err = runYarnAdd(wd, pkgs...)
if err != nil {
return
}
}
var ret cjsExportsResult
ret, err = parseCJSModuleExports(wd, pkg.ImportPath(), nodeEnv)
if err == nil && ret.Error != "" {
Expand Down
31 changes: 22 additions & 9 deletions server/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ func fetchPackageInfo(name string, version string) (info NpmPackage, err error)
time.Sleep(10 * time.Millisecond)
}

lock.Store(id, struct{}{})
defer lock.Delete(id)

// check cache firstly
if cache != nil {
var data []byte
Expand All @@ -363,9 +366,6 @@ func fetchPackageInfo(name string, version string) (info NpmPackage, err error)
}
}

lock.Store(id, struct{}{})
defer lock.Delete(id)

start := time.Now()
req, err := http.NewRequest("GET", strings.TrimRight(cfg.NpmRegistry, "/")+"/"+name, nil)
if err != nil {
Expand Down Expand Up @@ -576,7 +576,24 @@ func installNodejs(dir string, version string) (err error) {
return
}

func yarnAdd(wd string, packages ...string) (err error) {
func yarnAdd(wd string, pkg Pkg) (err error) {
for i := 0; i < 3; i++ {
err = runYarnAdd(wd, fmt.Sprintf("%s@%s", pkg.Name, pkg.Version))
if err == nil && !fileExists(path.Join(wd, "node_modules", pkg.Name, "package.json")) {
yarnCacheClean(pkg.Name)
err = fmt.Errorf("yarnAdd(%s): package.json not found", pkg)
}
if err == nil {
break
}
if i < 2 {
time.Sleep(100 * time.Millisecond)
}
}
return
}

func runYarnAdd(wd string, packages ...string) (err error) {
if len(packages) > 0 {
start := time.Now()
args := []string{
Expand All @@ -602,13 +619,11 @@ func yarnAdd(wd string, packages ...string) (err error) {
if yarnMutex != "" {
args = append(args, "--mutex", yarnMutex)
}

cmd := exec.Command("yarn", append(args, packages...)...)
cmd.Dir = wd
if cfg.NpmToken != "" {
cmd.Env = append(os.Environ(), "ESM_NPM_TOKEN="+cfg.NpmToken)
}

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("yarn add %s: %s", strings.Join(packages, ","), string(output))
Expand All @@ -630,11 +645,9 @@ func yarnCacheClean(packages ...string) {
args = append(args, "--mutex", yarnMutex)
}
cmd := exec.Command("yarn", append(args, packages...)...)
output, err := cmd.CombinedOutput()
_, err := cmd.CombinedOutput()
if err != nil {
log.Warnf("yarn cache clean %s: %s", strings.Join(packages, ","), err)
} else {
log.Debugf("yarn cache clean %s: %s", strings.Join(packages, ","), string(output))
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/issue-520/issue-520.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import Draggable from "http://localhost:8080/react-draggable";

Deno.test("issue #520", () => {
assertEquals(typeof Draggable, "function");
});

0 comments on commit 13d8074

Please sign in to comment.