Skip to content

Commit

Permalink
feat(build): prevent submodules bundling their local dependencies (#354)
Browse files Browse the repository at this point in the history
Co-authored-by: zhouhongxuan <[email protected]>
  • Loading branch information
alienzhou and zhouhongxuan authored Jun 29, 2022
1 parent 16c8d2a commit 7afd9db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ esmd
node_modules/
target/
pkg/
.vscode/launch.json
30 changes: 28 additions & 2 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -308,11 +310,35 @@ func (task *BuildTask) build(tracing *stringSet) (esm *ModuleMeta, err error) {
}
}

// bundles undefined relative imports or the package/module it self
if isLocalImport(specifier) || specifier == task.Pkg.ImportPath() {
// bundle the package/module it self and the entrypoint
if specifier == task.Pkg.ImportPath() || specifier == entryPoint {
return api.OnResolveResult{}, nil
}

// for local modules
if isLocalImport(specifier) {
// bundle if the entry pkg is not a submodule
if task.Pkg.Submodule == "" {
return api.OnResolveResult{}, nil
}

// bundle if this pkg has 'exports' definitions but the local module is not in 'exports'
if npm.DefinedExports != nil && !reflect.ValueOf(npm.DefinedExports).IsNil() {
return api.OnResolveResult{}, nil
}

// otherwise do not bundle its local dependencies
var dirpath = args.ResolveDir
if strings.HasPrefix(dirpath, "/private/var/") {
dirpath = strings.TrimPrefix(dirpath, "/private")
}
fullFilepath := filepath.Join(dirpath, specifier)
// convert: full filepath --> package name + submodule path
specifier = strings.TrimPrefix(fullFilepath, filepath.Join(task.wd, "node_modules")+"/")
external.Add(specifier)
return api.OnResolveResult{Path: "__ESM_SH_EXTERNAL:" + specifier, External: true}, nil
}

// ignore `require()` of esm package
if task.NoRequire && (args.Kind == api.ResolveJSRequireCall || args.Kind == api.ResolveJSRequireResolve) {
return api.OnResolveResult{Path: specifier, External: true}, nil
Expand Down

0 comments on commit 7afd9db

Please sign in to comment.