graph now finalizes entry points

This commit is contained in:
Evan Wallace
2021-04-11 01:46:24 -07:00
parent 3bf9883e75
commit d72deadd49
2 changed files with 28 additions and 22 deletions

View File

@@ -182,22 +182,22 @@ func newLinkerContext(
) linkerContext {
log = wrappedLog(log)
// Clone information about symbols and files so we don't mutate the input data
c := linkerContext{
options: options,
log: log,
fs: fs,
res: res,
graph: graph.MakeLinkerGraph(inputFiles, reachableFiles, entryPoints),
dataForSourceMaps: dataForSourceMaps,
graph: graph.MakeLinkerGraph(
inputFiles,
reachableFiles,
entryPoints,
options.CodeSplitting,
),
}
// Mark all entry points so we don't add them again for import() expressions
for _, entryPoint := range entryPoints {
file := &c.graph.Files[entryPoint.SourceIndex]
file.EntryPointKind = graph.EntryPointUserSpecified
if repr, ok := file.InputFile.Repr.(*graph.JSRepr); ok {
if repr, ok := c.graph.Files[entryPoint.SourceIndex].InputFile.Repr.(*graph.JSRepr); ok {
// Loaders default to CommonJS when they are the entry point and the output
// format is not ESM-compatible since that avoids generating the ESM-to-CJS
// machinery.
@@ -1041,15 +1041,7 @@ func (c *linkerContext) scanImportsAndExports() {
}
case ast.ImportDynamic:
if c.options.CodeSplitting {
// Files that are imported with import() must be entry points
if otherFile.EntryPointKind == graph.EntryPointNone {
c.graph.EntryPoints = append(c.graph.EntryPoints, graph.EntryPoint{
SourceIndex: record.SourceIndex.GetIndex(),
})
otherFile.EntryPointKind = graph.EntryPointDynamicImport
}
} else {
if !c.options.CodeSplitting {
// If we're not splitting, then import() is just a require() that
// returns a promise, so the imported file must be a CommonJS module
if otherRepr.AST.ExportsKind == js_ast.ExportsESM {

View File

@@ -86,16 +86,21 @@ func MakeLinkerGraph(
inputFiles []InputFile,
reachableFiles []uint32,
originalEntryPoints []EntryPoint,
codeSplitting bool,
) LinkerGraph {
entryPoints := append([]EntryPoint{}, originalEntryPoints...)
symbols := js_ast.NewSymbolMap(len(inputFiles))
files := make([]LinkerFile, len(inputFiles))
// Mark all entry points so we don't add them again for import() expressions
for _, entryPoint := range entryPoints {
files[entryPoint.SourceIndex].EntryPointKind = EntryPointUserSpecified
}
// Clone various things since we may mutate them later
for _, sourceIndex := range reachableFiles {
file := LinkerFile{
InputFile: inputFiles[sourceIndex],
}
file := &files[sourceIndex]
file.InputFile = inputFiles[sourceIndex]
switch repr := file.InputFile.Repr.(type) {
case *JSRepr:
@@ -126,6 +131,18 @@ func MakeLinkerGraph(
// Clone the import records
repr.AST.ImportRecords = append([]ast.ImportRecord{}, repr.AST.ImportRecords...)
// Add dynamic imports as additional entry points if code splitting is active
if codeSplitting {
for importRecordIndex := range repr.AST.ImportRecords {
if record := &repr.AST.ImportRecords[importRecordIndex]; record.SourceIndex.IsValid() && record.Kind == ast.ImportDynamic {
if otherFile := &files[record.SourceIndex.GetIndex()]; otherFile.EntryPointKind == EntryPointNone {
entryPoints = append(entryPoints, EntryPoint{SourceIndex: record.SourceIndex.GetIndex()})
otherFile.EntryPointKind = EntryPointDynamicImport
}
}
}
}
// Clone the import map
namedImports := make(map[js_ast.Ref]js_ast.NamedImport, len(repr.AST.NamedImports))
for k, v := range repr.AST.NamedImports {
@@ -177,9 +194,6 @@ func MakeLinkerGraph(
// All files start off as far as possible from an entry point
file.DistanceFromEntryPoint = ^uint32(0)
// Update the file in our copy of the file array
files[sourceIndex] = file
}
// Create a way to convert source indices to a stable ordering