graph

package
v0.23.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSSRepr

type CSSRepr struct {
	AST css_ast.AST

	// If present, this is the JavaScript stub corresponding to this CSS file.
	// A JavaScript stub is automatically generated for a CSS file when it's
	// imported from a JavaScript file.
	JSSourceIndex ast.Index32
}

func (*CSSRepr) ImportRecords

func (repr *CSSRepr) ImportRecords() *[]ast.ImportRecord

type CopyRepr added in v0.14.44

type CopyRepr struct {
	// The URL that replaces the contents of any import record paths for this file
	URLForCode string
}

func (*CopyRepr) ImportRecords added in v0.14.44

func (repr *CopyRepr) ImportRecords() *[]ast.ImportRecord

type EntryPoint

type EntryPoint struct {
	// This may be an absolute path or a relative path. If absolute, it will
	// eventually be turned into a relative path by computing the path relative
	// to the "outbase" directory. Then this relative path will be joined onto
	// the "outdir" directory to form the final output path for this entry point.
	OutputPath string

	// This is the source index of the entry point. This file must have a valid
	// entry point kind (i.e. not "none").
	SourceIndex uint32

	// Manually specified output paths are ignored when computing the default
	// "outbase" directory, which is computed as the lowest common ancestor of
	// all automatically generated output paths.
	OutputPathWasAutoGenerated bool
}

type ExportData

type ExportData struct {
	// Export star resolution happens first before import resolution. That means
	// it cannot yet determine if duplicate names from export star resolution are
	// ambiguous (point to different symbols) or not (point to the same symbol).
	// This issue can happen in the following scenario:
	//
	//   // entry.js
	//   export * from './a'
	//   export * from './b'
	//
	//   // a.js
	//   export * from './c'
	//
	//   // b.js
	//   export {x} from './c'
	//
	//   // c.js
	//   export let x = 1, y = 2
	//
	// In this case "entry.js" should have two exports "x" and "y", neither of
	// which are ambiguous. To handle this case, ambiguity resolution must be
	// deferred until import resolution time. That is done using this array.
	PotentiallyAmbiguousExportStarRefs []ImportData

	Ref ast.Ref

	// This is the file that the named export above came from. This will be
	// different from the file that contains this object if this is a re-export.
	NameLoc     logger.Loc // Optional, goes with sourceIndex, ignore if zero
	SourceIndex uint32
}

type ImportData

type ImportData struct {
	// This is an array of intermediate statements that re-exported this symbol
	// in a chain before getting to the final symbol. This can be done either with
	// "export * from" or "export {} from". If this is done with "export * from"
	// then this may not be the result of a single chain but may instead form
	// a diamond shape if this same symbol was re-exported multiple times from
	// different files.
	ReExports []js_ast.Dependency

	NameLoc     logger.Loc // Optional, goes with sourceIndex, ignore if zero
	Ref         ast.Ref
	SourceIndex uint32
}

type InputFile

type InputFile struct {
	Repr           InputFileRepr
	InputSourceMap *sourcemap.SourceMap

	// If this file ends up being used in the bundle, these are additional files
	// that must be written to the output directory. It's used by the "file" and
	// "copy" loaders.
	AdditionalFiles            []OutputFile
	UniqueKeyForAdditionalFile string

	SideEffects SideEffects
	Source      logger.Source
	Loader      config.Loader

	OmitFromSourceMapsAndMetafile bool
}

type InputFileRepr

type InputFileRepr interface {
	ImportRecords() *[]ast.ImportRecord
}

type JSRepr

type JSRepr struct {
	Meta JSReprMeta
	AST  js_ast.AST

	// If present, this is the CSS file that this JavaScript stub corresponds to.
	// A JavaScript stub is automatically generated for a CSS file when it's
	// imported from a JavaScript file.
	CSSSourceIndex ast.Index32
}

func (*JSRepr) ImportRecords

func (repr *JSRepr) ImportRecords() *[]ast.ImportRecord

func (*JSRepr) TopLevelSymbolToParts added in v0.11.23

func (repr *JSRepr) TopLevelSymbolToParts(ref ast.Ref) []uint32

type JSReprMeta

type JSReprMeta struct {
	// This is only for TypeScript files. If an import symbol is in this map, it
	// means the import couldn't be found and doesn't actually exist. This is not
	// an error in TypeScript because the import is probably just a type.
	//
	// Normally we remove all unused imports for TypeScript files during parsing,
	// which automatically removes type-only imports. But there are certain re-
	// export situations where it's impossible to tell if an import is a type or
	// not:
	//
	//   import {typeOrNotTypeWhoKnows} from 'path';
	//   export {typeOrNotTypeWhoKnows};
	//
	// Really people should be using the TypeScript "isolatedModules" flag with
	// bundlers like this one that compile TypeScript files independently without
	// type checking. That causes the TypeScript type checker to emit the error
	// "Re-exporting a type when the '--isolatedModules' flag is provided requires
	// using 'export type'." But we try to be robust to such code anyway.
	IsProbablyTypeScriptType map[ast.Ref]bool

	// Imports are matched with exports in a separate pass from when the matched
	// exports are actually bound to the imports. Here "binding" means adding non-
	// local dependencies on the parts in the exporting file that declare the
	// exported symbol to all parts in the importing file that use the imported
	// symbol.
	//
	// This must be a separate pass because of the "probably TypeScript type"
	// check above. We can't generate the part for the export namespace until
	// we've matched imports with exports because the generated code must omit
	// type-only imports in the export namespace code. And we can't bind exports
	// to imports until the part for the export namespace is generated since that
	// part needs to participate in the binding.
	//
	// This array holds the deferred imports to bind so the pass can be split
	// into two separate passes.
	ImportsToBind map[ast.Ref]ImportData

	// This includes both named exports and re-exports.
	//
	// Named exports come from explicit export statements in the original file,
	// and are copied from the "NamedExports" field in the AST.
	//
	// Re-exports come from other files and are the result of resolving export
	// star statements (i.e. "export * from 'foo'").
	ResolvedExports     map[string]ExportData
	ResolvedExportStar  *ExportData
	ResolvedExportTypos *helpers.TypoDetector

	// Never iterate over "resolvedExports" directly. Instead, iterate over this
	// array. Some exports in that map aren't meant to end up in generated code.
	// This array excludes these exports and is also sorted, which avoids non-
	// determinism due to random map iteration order.
	SortedAndFilteredExportAliases []string

	// This is merged on top of the corresponding map from the parser in the AST.
	// You should call "TopLevelSymbolToParts" to access this instead of accessing
	// it directly.
	TopLevelSymbolToPartsOverlay map[ast.Ref][]uint32

	// If this is an entry point, this array holds a reference to one free
	// temporary symbol for each entry in "sortedAndFilteredExportAliases".
	// These may be needed to store copies of CommonJS re-exports in ESM.
	CJSExportCopies []ast.Ref

	// The index of the automatically-generated part used to represent the
	// CommonJS or ESM wrapper. This part is empty and is only useful for tree
	// shaking and code splitting. The wrapper can't be inserted into the part
	// because the wrapper contains other parts, which can't be represented by
	// the current part system. Only wrapped files have one of these.
	WrapperPartIndex ast.Index32

	// The index of the automatically-generated part used to handle entry point
	// specific stuff. If a certain part is needed by the entry point, it's added
	// as a dependency of this part. This is important for parts that are marked
	// as removable when unused and that are not used by anything else. Only
	// entry point files have one of these.
	EntryPointPartIndex ast.Index32

	// This is true if this file is affected by top-level await, either by having
	// a top-level await inside this file or by having an import/export statement
	// that transitively imports such a file. It is forbidden to call "require()"
	// on these files since they are evaluated asynchronously.
	IsAsyncOrHasAsyncDependency bool

	Wrap WrapKind

	// If true, we need to insert "var exports = {};". This is the case for ESM
	// files when the import namespace is captured via "import * as" and also
	// when they are the target of a "require()" call.
	NeedsExportsVariable bool

	// If true, the "__export(exports, { ... })" call will be force-included even
	// if there are no parts that reference "exports". Otherwise this call will
	// be removed due to the tree shaking pass. This is used when for entry point
	// files when code related to the current output format needs to reference
	// the "exports" variable.
	ForceIncludeExportsForEntryPoint bool

	// This is set when we need to pull in the "__export" symbol in to the part
	// at "nsExportPartIndex". This can't be done in "createExportsForFile"
	// because of concurrent map hazards. Instead, it must be done later.
	NeedsExportSymbolFromRuntime bool

	// Wrapped files must also ensure that their dependencies are wrapped. This
	// flag is used during the traversal that enforces this invariant, and is used
	// to detect when the fixed point has been reached.
	DidWrapDependencies bool
}

This contains linker-specific metadata corresponding to a "file" struct from the initial scan phase of the bundler. It's separated out because it's conceptually only used for a single linking operation and because multiple linking operations may be happening in parallel with different metadata for the same file.

type LinkerFile

type LinkerFile struct {
	// This holds all entry points that can reach this file. It will be used to
	// assign the parts in this file to a chunk.
	EntryBits helpers.BitSet

	InputFile InputFile

	// The minimum number of links in the module graph to get from an entry point
	// to this file
	DistanceFromEntryPoint uint32

	// If "entryPointKind" is not "entryPointNone", this is the index of the
	// corresponding entry point chunk.
	EntryPointChunkIndex uint32

	// This is true if this file has been marked as live by the tree shaking
	// algorithm.
	IsLive bool
	// contains filtered or unexported fields
}

func (*LinkerFile) IsEntryPoint

func (f *LinkerFile) IsEntryPoint() bool

func (*LinkerFile) IsUserSpecifiedEntryPoint

func (f *LinkerFile) IsUserSpecifiedEntryPoint() bool

func (*LinkerFile) LineColumnTracker added in v0.11.23

func (f *LinkerFile) LineColumnTracker() *logger.LineColumnTracker

Note: This is not guarded by a mutex. Make sure this isn't called from a parallel part of the code.

type LinkerGraph

type LinkerGraph struct {
	Files []LinkerFile

	Symbols ast.SymbolMap

	// This is for cross-module inlining of TypeScript enum constants
	TSEnums map[ast.Ref]map[string]js_ast.TSEnumValue

	// This is for cross-module inlining of detected inlinable constants
	ConstValues map[ast.Ref]js_ast.ConstValue

	// We should avoid traversing all files in the bundle, because the linker
	// should be able to run a linking operation on a large bundle where only
	// a few files are needed (e.g. an incremental compilation scenario). This
	// holds all files that could possibly be reached through the entry points.
	// If you need to iterate over all files in the linking operation, iterate
	// over this array. This array is also sorted in a deterministic ordering
	// to help ensure deterministic builds (source indices are random).
	ReachableFiles []uint32

	// This maps from unstable source index to stable reachable file index. This
	// is useful as a deterministic key for sorting if you need to sort something
	// containing a source index (such as "ast.Ref" symbol references).
	StableSourceIndices []uint32
	// contains filtered or unexported fields
}

func CloneLinkerGraph added in v0.11.13

func CloneLinkerGraph(
	inputFiles []InputFile,
	reachableFiles []uint32,
	originalEntryPoints []EntryPoint,
	codeSplitting bool,
) LinkerGraph

func (*LinkerGraph) AddPartToFile

func (g *LinkerGraph) AddPartToFile(sourceIndex uint32, part js_ast.Part) uint32

func (*LinkerGraph) EntryPoints

func (g *LinkerGraph) EntryPoints() []EntryPoint

Prevent packages that depend on us from adding or removing entry points

func (*LinkerGraph) GenerateNewSymbol

func (g *LinkerGraph) GenerateNewSymbol(sourceIndex uint32, kind ast.SymbolKind, originalName string) ast.Ref

func (*LinkerGraph) GenerateRuntimeSymbolImportAndUse

func (g *LinkerGraph) GenerateRuntimeSymbolImportAndUse(
	sourceIndex uint32,
	partIndex uint32,
	name string,
	useCount uint32,
)

func (*LinkerGraph) GenerateSymbolImportAndUse

func (g *LinkerGraph) GenerateSymbolImportAndUse(
	sourceIndex uint32,
	partIndex uint32,
	ref ast.Ref,
	useCount uint32,
	sourceIndexToImportFrom uint32,
)

type OutputFile

type OutputFile struct {
	// If "AbsMetadataFile" is present, this will be filled out with information
	// about this file in JSON format. This is a partial JSON file that will be
	// fully assembled later.
	JSONMetadataChunk string

	AbsPath      string
	Contents     []byte
	IsExecutable bool
}

type SideEffects

type SideEffects struct {
	// This is optional additional information for use in error messages
	Data *resolver.SideEffectsData

	Kind SideEffectsKind
}

type SideEffectsKind

type SideEffectsKind uint8
const (
	// The default value conservatively considers all files to have side effects.
	HasSideEffects SideEffectsKind = iota

	// This file was listed as not having side effects by a "package.json"
	// file in one of our containing directories with a "sideEffects" field.
	NoSideEffects_PackageJSON

	// This file is considered to have no side effects because the AST was empty
	// after parsing finished. This should be the case for ".d.ts" files.
	NoSideEffects_EmptyAST

	// This file was loaded using a data-oriented loader (e.g. "text") that is
	// known to not have side effects.
	NoSideEffects_PureData

	// Same as above but it came from a plugin. We don't want to warn about
	// unused imports to these files since running the plugin is a side effect.
	// Removing the import would not call the plugin which is observable.
	NoSideEffects_PureData_FromPlugin
)

type WrapKind

type WrapKind uint8
const (
	WrapNone WrapKind = iota

	// The module will be bundled CommonJS-style like this:
	//
	//   // foo.ts
	//   let require_foo = __commonJS((exports, module) => {
	//     exports.foo = 123;
	//   });
	//
	//   // bar.ts
	//   let foo = flag ? require_foo() : null;
	//
	WrapCJS

	// The module will be bundled ESM-style like this:
	//
	//   // foo.ts
	//   var foo, foo_exports = {};
	//   __export(foo_exports, {
	//     foo: () => foo
	//   });
	//   let init_foo = __esm(() => {
	//     foo = 123;
	//   });
	//
	//   // bar.ts
	//   let foo = flag ? (init_foo(), __toCommonJS(foo_exports)) : null;
	//
	WrapESM
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL