Skip to content

Commit

Permalink
resolver: move IsExternal into PathPair
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Oct 17, 2023
1 parent b0eddea commit 72b1e8b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
13 changes: 6 additions & 7 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func parseFile(args parseArgs) {
// All "require.resolve()" imports should be external because we don't
// want to waste effort traversing into them
if record.Kind == ast.ImportRequireResolve {
if resolveResult != nil && resolveResult.IsExternal {
if resolveResult != nil && resolveResult.PathPair.IsExternal {
// Allow path substitution as long as the result is external
result.resolveResults[importRecordIndex] = resolveResult
} else if !record.Flags.Has(ast.HandlesImportErrors) {
Expand Down Expand Up @@ -926,8 +926,7 @@ func RunOnResolvePlugins(
}

return &resolver.ResolveResult{
PathPair: resolver.PathPair{Primary: result.Path},
IsExternal: result.External,
PathPair: resolver.PathPair{Primary: result.Path, IsExternal: result.External},
PluginData: result.PluginData,
PrimarySideEffectsData: sideEffectsData,
}, false, resolver.DebugMeta{}
Expand Down Expand Up @@ -1580,7 +1579,7 @@ func (s *scanner) preprocessInjectedFiles() {
nil,
)
if resolveResult != nil {
if resolveResult.IsExternal {
if resolveResult.PathPair.IsExternal {
s.log.AddError(nil, logger.Range{}, fmt.Sprintf("The injected path %q cannot be marked as external", importPath))
} else {
injectResolveResults[i] = resolveResult
Expand Down Expand Up @@ -1759,7 +1758,7 @@ func (s *scanner) addEntryPoints(entryPoints []EntryPoint) []graph.EntryPoint {
nil,
)
if resolveResult != nil {
if resolveResult.IsExternal {
if resolveResult.PathPair.IsExternal {
s.log.AddError(nil, logger.Range{}, fmt.Sprintf("The entry point %q cannot be marked as external", entryPoint.InputPath))
} else {
entryPointInfos[i] = entryPointInfo{results: []resolver.ResolveResult{*resolveResult}}
Expand Down Expand Up @@ -1975,7 +1974,7 @@ func (s *scanner) scanAllDependencies() {
}

path := resolveResult.PathPair.Primary
if !resolveResult.IsExternal {
if !resolveResult.PathPair.IsExternal {
// Handle a path within the bundle
sourceIndex := s.maybeParseFile(*resolveResult, resolver.PrettyPath(s.fs, path),
&result.file.inputFile.Source, record.Range, inputKindNormal, nil)
Expand Down Expand Up @@ -2036,7 +2035,7 @@ func (s *scanner) generateResultForGlobResolve(
importRecordIndex := uint32(len(importRecords))
var sourceIndex ast.Index32

if !resolveResult.IsExternal {
if !resolveResult.PathPair.IsExternal {
sourceIndex = ast.MakeIndex32(s.maybeParseFile(
resolveResult,
resolver.PrettyPath(s.fs, resolveResult.PathPair.Primary),
Expand Down
28 changes: 11 additions & 17 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type PathPair struct {
// will be "main"
Primary logger.Path
Secondary logger.Path

IsExternal bool
}

func (pp *PathPair) iter() []*logger.Path {
Expand Down Expand Up @@ -114,8 +116,6 @@ type ResolveResult struct {

// This is the "type" field from "package.json"
ModuleTypeData js_ast.ModuleTypeData

IsExternal bool
}

type suggestionRange uint8
Expand Down Expand Up @@ -431,8 +431,7 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor

r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
IsExternal: true,
PathPair: PathPair{Primary: logger.Path{Text: importPath}, IsExternal: true},
}, debugMeta
}

Expand All @@ -444,8 +443,7 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor

r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
IsExternal: true,
PathPair: PathPair{Primary: logger.Path{Text: importPath}, IsExternal: true},
PrimarySideEffectsData: &SideEffectsData{}, // Mark this with "sideEffects: false"
}, debugMeta
}
Expand Down Expand Up @@ -491,8 +489,7 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor

r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
IsExternal: true,
PathPair: PathPair{Primary: logger.Path{Text: importPath}, IsExternal: true},
PrimarySideEffectsData: sideEffects,
}, debugMeta
}
Expand All @@ -516,8 +513,7 @@ func (res *Resolver) Resolve(sourceDir string, importPath string, kind ast.Impor
}
r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
IsExternal: true,
PathPair: PathPair{Primary: logger.Path{Text: importPath}, IsExternal: true},
}, debugMeta
}

Expand Down Expand Up @@ -730,8 +726,7 @@ func (res *Resolver) ResolveGlob(sourceDir string, importPathPattern []helpers.G
var result ResolveResult

if r.isExternal(r.options.ExternalSettings.PreResolve, relPath, kind) {
result.PathPair = PathPair{Primary: logger.Path{Text: relPath}}
result.IsExternal = true
result.PathPair = PathPair{Primary: logger.Path{Text: relPath}, IsExternal: true}

if r.debugLogs != nil {
r.debugLogs.addNote(fmt.Sprintf("The path %q was marked as external by the user", result.PathPair.Primary.Text))
Expand Down Expand Up @@ -847,11 +842,11 @@ func (r resolverQuery) flushDebugLogs(mode flushMode) {
}

func (r resolverQuery) finalizeResolve(result *ResolveResult) {
if !result.IsExternal && r.isExternal(r.options.ExternalSettings.PostResolve, result.PathPair.Primary.Text, r.kind) {
if !result.PathPair.IsExternal && r.isExternal(r.options.ExternalSettings.PostResolve, result.PathPair.Primary.Text, r.kind) {
if r.debugLogs != nil {
r.debugLogs.addNote(fmt.Sprintf("The path %q was marked as external by the user", result.PathPair.Primary.Text))
}
result.IsExternal = true
result.PathPair.IsExternal = true
} else {
for i, path := range result.PathPair.iter() {
if path.Namespace != "file" {
Expand Down Expand Up @@ -1024,8 +1019,7 @@ func (r resolverQuery) resolveWithoutSymlinks(sourceDir string, sourceDirInfo *d

r.flushDebugLogs(flushDueToSuccess)
return &ResolveResult{
PathPair: PathPair{Primary: logger.Path{Text: importPath}},
IsExternal: true,
PathPair: PathPair{Primary: logger.Path{Text: importPath}, IsExternal: true},
}
}

Expand All @@ -1037,7 +1031,7 @@ func (r resolverQuery) resolveWithoutSymlinks(sourceDir string, sourceDirInfo *d
if r.debugLogs != nil {
r.debugLogs.addNote(fmt.Sprintf("The path %q was marked as external by the user", absPath))
}
return &ResolveResult{PathPair: PathPair{Primary: logger.Path{Text: absPath, Namespace: "file"}}, IsExternal: true}
return &ResolveResult{PathPair: PathPair{Primary: logger.Path{Text: absPath, Namespace: "file"}, IsExternal: true}}
}

// Check the "browser" map
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ func loadPlugins(initialOptions *BuildOptions, fs fs.FS, log logger.Log, caches
result.Warnings = convertMessagesToPublic(logger.Warning, msgs)
if resolveResult != nil {
result.Path = resolveResult.PathPair.Primary.Text
result.External = resolveResult.IsExternal
result.External = resolveResult.PathPair.IsExternal
result.SideEffects = resolveResult.PrimarySideEffectsData == nil
result.Namespace = resolveResult.PathPair.Primary.Namespace
result.Suffix = resolveResult.PathPair.Primary.IgnoredSuffix
Expand Down

0 comments on commit 72b1e8b

Please sign in to comment.