Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): prevent submodules bundling their local dependencies #354

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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