Skip to content

Commit

Permalink
only resolve each unique path once per file
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 12, 2020
1 parent 1f3d1af commit dfc2721
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
72 changes: 42 additions & 30 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,40 +241,52 @@ func parseFile(args parseArgs) {
if result.ok && args.options.IsBundling {
result.resolveResults = make([]*resolver.ResolveResult, len(result.file.ast.ImportRecords))

// Resolve relative to the parent directory of the source file with the
// import path. Just use the current directory if the source file is virtual.
var sourceDir string
if source.KeyPath.IsAbsolute {
sourceDir = args.fs.Dir(source.KeyPath.Text)
} else if args.absResolveDir != "" {
sourceDir = args.absResolveDir
} else {
sourceDir = args.fs.Cwd()
}
if len(result.file.ast.ImportRecords) > 0 {
cache := make(map[string]*resolver.ResolveResult)

// Resolve relative to the parent directory of the source file with the
// import path. Just use the current directory if the source file is virtual.
var sourceDir string
if source.KeyPath.IsAbsolute {
sourceDir = args.fs.Dir(source.KeyPath.Text)
} else if args.absResolveDir != "" {
sourceDir = args.absResolveDir
} else {
sourceDir = args.fs.Cwd()
}

for _, part := range result.file.ast.Parts {
for _, importRecordIndex := range part.ImportRecordIndices {
// Don't try to resolve imports that are already resolved
record := &result.file.ast.ImportRecords[importRecordIndex]
if record.SourceIndex != nil {
continue
}
for _, part := range result.file.ast.Parts {
for _, importRecordIndex := range part.ImportRecordIndices {
// Don't try to resolve imports that are already resolved
record := &result.file.ast.ImportRecords[importRecordIndex]
if record.SourceIndex != nil {
continue
}

// Run the resolver and log an error if the path couldn't be resolved
resolveResult := args.res.Resolve(sourceDir, record.Path.Text)
if resolveResult == nil {
// Failed imports inside a try/catch are silently turned into
// external imports instead of causing errors. This matches a common
// code pattern for conditionally importing a module with a graceful
// fallback.
if !record.IsInsideTryBody {
r := source.RangeOfString(record.Loc)
args.log.AddRangeError(&source, r, fmt.Sprintf("Could not resolve %q", record.Path.Text))
// Cache the path in case it's imported multiple times in this file
if resolveResult, ok := cache[record.Path.Text]; ok {
result.resolveResults[importRecordIndex] = resolveResult
continue
}
continue
}

result.resolveResults[importRecordIndex] = resolveResult
// Run the resolver and log an error if the path couldn't be resolved
resolveResult := args.res.Resolve(sourceDir, record.Path.Text)
cache[record.Path.Text] = resolveResult

if resolveResult == nil {
// Failed imports inside a try/catch are silently turned into
// external imports instead of causing errors. This matches a common
// code pattern for conditionally importing a module with a graceful
// fallback.
if !record.IsInsideTryBody {
r := source.RangeOfString(record.Loc)
args.log.AddRangeError(&source, r, fmt.Sprintf("Could not resolve %q", record.Path.Text))
}
continue
}

result.resolveResults[importRecordIndex] = resolveResult
}
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions internal/bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2673,9 +2673,6 @@ func TestImportFSBrowser(t *testing.T) {
Platform: config.PlatformBrowser,
},
expectedScanLog: `/entry.js: error: Could not resolve "fs"
/entry.js: error: Could not resolve "fs"
/entry.js: error: Could not resolve "fs"
/entry.js: error: Could not resolve "fs"
`,
})
}
Expand Down Expand Up @@ -2755,7 +2752,6 @@ func TestExportFSBrowser(t *testing.T) {
Platform: config.PlatformBrowser,
},
expectedScanLog: `/entry.js: error: Could not resolve "fs"
/entry.js: error: Could not resolve "fs"
`,
})
}
Expand Down

0 comments on commit dfc2721

Please sign in to comment.