github.com/evanw/esbuild@v0.21.4/internal/graph/input.go (about)

     1  package graph
     2  
     3  // The code in this file mainly represents data that passes from the scan phase
     4  // to the compile phase of the bundler. There is currently one exception: the
     5  // "meta" member of the JavaScript file representation. That could have been
     6  // stored separately but is stored together for convenience and to avoid an
     7  // extra level of indirection. Instead it's kept in a separate type to keep
     8  // things organized.
     9  
    10  import (
    11  	"github.com/evanw/esbuild/internal/ast"
    12  	"github.com/evanw/esbuild/internal/config"
    13  	"github.com/evanw/esbuild/internal/css_ast"
    14  	"github.com/evanw/esbuild/internal/js_ast"
    15  	"github.com/evanw/esbuild/internal/logger"
    16  	"github.com/evanw/esbuild/internal/resolver"
    17  	"github.com/evanw/esbuild/internal/sourcemap"
    18  )
    19  
    20  type InputFile struct {
    21  	Repr           InputFileRepr
    22  	InputSourceMap *sourcemap.SourceMap
    23  
    24  	// If this file ends up being used in the bundle, these are additional files
    25  	// that must be written to the output directory. It's used by the "file" and
    26  	// "copy" loaders.
    27  	AdditionalFiles            []OutputFile
    28  	UniqueKeyForAdditionalFile string
    29  
    30  	SideEffects SideEffects
    31  	Source      logger.Source
    32  	Loader      config.Loader
    33  
    34  	OmitFromSourceMapsAndMetafile bool
    35  }
    36  
    37  type OutputFile struct {
    38  	// If "AbsMetadataFile" is present, this will be filled out with information
    39  	// about this file in JSON format. This is a partial JSON file that will be
    40  	// fully assembled later.
    41  	JSONMetadataChunk string
    42  
    43  	AbsPath      string
    44  	Contents     []byte
    45  	IsExecutable bool
    46  }
    47  
    48  type SideEffects struct {
    49  	// This is optional additional information for use in error messages
    50  	Data *resolver.SideEffectsData
    51  
    52  	Kind SideEffectsKind
    53  }
    54  
    55  type SideEffectsKind uint8
    56  
    57  const (
    58  	// The default value conservatively considers all files to have side effects.
    59  	HasSideEffects SideEffectsKind = iota
    60  
    61  	// This file was listed as not having side effects by a "package.json"
    62  	// file in one of our containing directories with a "sideEffects" field.
    63  	NoSideEffects_PackageJSON
    64  
    65  	// This file is considered to have no side effects because the AST was empty
    66  	// after parsing finished. This should be the case for ".d.ts" files.
    67  	NoSideEffects_EmptyAST
    68  
    69  	// This file was loaded using a data-oriented loader (e.g. "text") that is
    70  	// known to not have side effects.
    71  	NoSideEffects_PureData
    72  
    73  	// Same as above but it came from a plugin. We don't want to warn about
    74  	// unused imports to these files since running the plugin is a side effect.
    75  	// Removing the import would not call the plugin which is observable.
    76  	NoSideEffects_PureData_FromPlugin
    77  )
    78  
    79  type InputFileRepr interface {
    80  	ImportRecords() *[]ast.ImportRecord
    81  }
    82  
    83  type JSRepr struct {
    84  	Meta JSReprMeta
    85  	AST  js_ast.AST
    86  
    87  	// If present, this is the CSS file that this JavaScript stub corresponds to.
    88  	// A JavaScript stub is automatically generated for a CSS file when it's
    89  	// imported from a JavaScript file.
    90  	CSSSourceIndex ast.Index32
    91  }
    92  
    93  func (repr *JSRepr) ImportRecords() *[]ast.ImportRecord {
    94  	return &repr.AST.ImportRecords
    95  }
    96  
    97  func (repr *JSRepr) TopLevelSymbolToParts(ref ast.Ref) []uint32 {
    98  	// Overlay the mutable map from the linker
    99  	if parts, ok := repr.Meta.TopLevelSymbolToPartsOverlay[ref]; ok {
   100  		return parts
   101  	}
   102  
   103  	// Fall back to the immutable map from the parser
   104  	return repr.AST.TopLevelSymbolToPartsFromParser[ref]
   105  }
   106  
   107  type CSSRepr struct {
   108  	AST css_ast.AST
   109  
   110  	// If present, this is the JavaScript stub corresponding to this CSS file.
   111  	// A JavaScript stub is automatically generated for a CSS file when it's
   112  	// imported from a JavaScript file.
   113  	JSSourceIndex ast.Index32
   114  }
   115  
   116  func (repr *CSSRepr) ImportRecords() *[]ast.ImportRecord {
   117  	return &repr.AST.ImportRecords
   118  }
   119  
   120  type CopyRepr struct {
   121  	// The URL that replaces the contents of any import record paths for this file
   122  	URLForCode string
   123  }
   124  
   125  func (repr *CopyRepr) ImportRecords() *[]ast.ImportRecord {
   126  	return nil
   127  }