prevent entry point add/remove

This commit is contained in:
Evan Wallace
2021-04-11 01:51:39 -07:00
parent 4072da4793
commit 0c331bf2d5
2 changed files with 28 additions and 19 deletions

View File

@@ -264,7 +264,7 @@ func (c *linkerContext) link() []graph.OutputFile {
c.markPartsReachableFromEntryPoints()
if c.options.Mode == config.ModePassThrough {
for _, entryPoint := range c.graph.EntryPoints {
for _, entryPoint := range c.graph.EntryPoints() {
c.preventExportsFromBeingRenamed(entryPoint.SourceIndex)
}
}
@@ -545,7 +545,7 @@ func (c *linkerContext) pathRelativeToOutbase(
absPath := file.InputFile.Source.KeyPath.Text
isCustomOutputPath := false
if outPath := c.graph.EntryPoints[entryPointBit].OutputPath; outPath != "" {
if outPath := c.graph.EntryPoints()[entryPointBit].OutputPath; outPath != "" {
// Use the configured output path if present
absPath = outPath
if !c.fs.IsAbs(absPath) {
@@ -2222,7 +2222,7 @@ func (c *linkerContext) advanceImportTracker(tracker importTracker) (importTrack
func (c *linkerContext) markPartsReachableFromEntryPoints() {
// Tree shaking: Each entry point marks all files reachable from itself
for _, entryPoint := range c.graph.EntryPoints {
for _, entryPoint := range c.graph.EntryPoints() {
c.markFileAsLive(entryPoint.SourceIndex)
}
@@ -2230,7 +2230,7 @@ func (c *linkerContext) markPartsReachableFromEntryPoints() {
// has to happen after tree shaking because there is an implicit dependency
// between live parts within the same file. All liveness has to be computed
// first before determining which entry points can reach which files.
for i, entryPoint := range c.graph.EntryPoints {
for i, entryPoint := range c.graph.EntryPoints() {
c.markFileAsReachable(entryPoint.SourceIndex, uint(i), 0)
}
}
@@ -2335,7 +2335,7 @@ func (c *linkerContext) markFileAsLive(sourceIndex uint32) {
if otherFile := &c.graph.Files[otherSourceIndex]; otherFile.InputFile.SideEffects.Kind != graph.HasSideEffects && !c.options.IgnoreDCEAnnotations {
// This is currently unsafe when code splitting is enabled, so
// disable it in that case
if len(c.graph.EntryPoints) < 2 {
if len(c.graph.EntryPoints()) < 2 {
continue
}
}
@@ -2586,12 +2586,12 @@ func (c *linkerContext) computeChunks() []chunkInfo {
cssChunks := make(map[string]chunkInfo)
// Create chunks for entry points
for i, entryPoint := range c.graph.EntryPoints {
for i, entryPoint := range c.graph.EntryPoints() {
file := &c.graph.Files[entryPoint.SourceIndex]
// Create a chunk for the entry point here to ensure that the chunk is
// always generated even if the resulting file is empty
entryBits := helpers.NewBitSet(uint(len(c.graph.EntryPoints)))
entryBits := helpers.NewBitSet(uint(len(c.graph.EntryPoints())))
entryBits.SetBit(uint(i))
info := chunkInfo{
entryBits: entryBits,
@@ -2743,7 +2743,7 @@ func (c *linkerContext) computeChunks() []chunkInfo {
var dir, base, ext string
var template []config.PathTemplate
if chunk.isEntryPoint {
if c.graph.Files[chunk.sourceIndex].EntryPointKind == graph.EntryPointUserSpecified {
if c.graph.Files[chunk.sourceIndex].IsUserSpecifiedEntryPoint() {
dir, base, ext = c.pathRelativeToOutbase(chunk.sourceIndex, chunk.entryPointBit, stdExt, false /* avoidIndex */)
template = c.options.EntryPathTemplate
} else {

View File

@@ -7,12 +7,12 @@ import (
"github.com/evanw/esbuild/internal/runtime"
)
type EntryPointKind uint8
type entryPointKind uint8
const (
EntryPointNone EntryPointKind = iota
EntryPointUserSpecified
EntryPointDynamicImport
entryPointNone entryPointKind = iota
entryPointUserSpecified
entryPointDynamicImport
)
type LinkerFile struct {
@@ -34,7 +34,7 @@ type LinkerFile struct {
// Note that dynamically-imported files are allowed to also be specified by
// the user as top-level entry points, so some dynamically-imported files
// may be "entryPointUserSpecified" instead of "entryPointDynamicImport".
EntryPointKind EntryPointKind
entryPointKind entryPointKind
// This is true if this file has been marked as live by the tree shaking
// algorithm.
@@ -42,7 +42,11 @@ type LinkerFile struct {
}
func (f *LinkerFile) IsEntryPoint() bool {
return f.EntryPointKind != EntryPointNone
return f.entryPointKind != entryPointNone
}
func (f *LinkerFile) IsUserSpecifiedEntryPoint() bool {
return f.entryPointKind == entryPointUserSpecified
}
type EntryPoint struct {
@@ -64,7 +68,7 @@ type EntryPoint struct {
type LinkerGraph struct {
Files []LinkerFile
EntryPoints []EntryPoint
entryPoints []EntryPoint
Symbols js_ast.SymbolMap
// We should avoid traversing all files in the bundle, because the linker
@@ -94,7 +98,7 @@ func MakeLinkerGraph(
// Mark all entry points so we don't add them again for import() expressions
for _, entryPoint := range entryPoints {
files[entryPoint.SourceIndex].EntryPointKind = EntryPointUserSpecified
files[entryPoint.SourceIndex].entryPointKind = entryPointUserSpecified
}
// Clone various things since we may mutate them later
@@ -135,9 +139,9 @@ func MakeLinkerGraph(
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 {
if otherFile := &files[record.SourceIndex.GetIndex()]; otherFile.entryPointKind == entryPointNone {
entryPoints = append(entryPoints, EntryPoint{SourceIndex: record.SourceIndex.GetIndex()})
otherFile.EntryPointKind = EntryPointDynamicImport
otherFile.entryPointKind = entryPointDynamicImport
}
}
}
@@ -208,13 +212,18 @@ func MakeLinkerGraph(
return LinkerGraph{
Symbols: symbols,
EntryPoints: entryPoints,
entryPoints: entryPoints,
Files: files,
ReachableFiles: reachableFiles,
StableSourceIndices: stableSourceIndices,
}
}
// Prevent packages that depend on us from adding or removing entry points
func (g *LinkerGraph) EntryPoints() []EntryPoint {
return g.entryPoints
}
func (g *LinkerGraph) AddPartToFile(sourceIndex uint32, part js_ast.Part) uint32 {
if part.SymbolUses == nil {
part.SymbolUses = make(map[js_ast.Ref]js_ast.SymbolUse)