js_printer

package
v0.8.23 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendSourceMapChunk

func AppendSourceMapChunk(j *Joiner, prevEndState SourceMapState, startState SourceMapState, sourceMap []byte)

Source map chunks are computed in parallel for speed. Each chunk is relative to the zero state instead of being relative to the end state of the previous chunk, since it's impossible to know the end state of the previous chunk in a parallel computation.

After all chunks are computed, they are joined together in a second pass. This rewrites the first mapping in each chunk to be relative to the end state of the previous chunk.

func CanQuoteIdentifier added in v0.8.0

func CanQuoteIdentifier(name string, unsupportedJSFeatures compat.JSFeature, asciiOnly bool) bool

func QuoteForJSON

func QuoteForJSON(text string, asciiOnly bool) []byte

func QuoteIdentifier added in v0.8.0

func QuoteIdentifier(js []byte, name string, unsupportedFeatures compat.JSFeature) []byte

func RemovePrefixFromSourceMapChunk added in v0.7.18

func RemovePrefixFromSourceMapChunk(buffer []byte, offset int) []byte

Rewrite the source map to remove everything before "offset" and have the generated position start from (0, 0) at that point. This is used when erasing the variable declaration keyword from the start of a file.

Types

type Joiner

type Joiner struct {
	// contains filtered or unexported fields
}

This provides an efficient way to join lots of big string and byte slices together. It avoids the cost of repeatedly reallocating as the buffer grows by measuring exactly how big the buffer should be and then allocating once. This is a measurable speedup.

func (*Joiner) AddBytes

func (j *Joiner) AddBytes(data []byte)

func (*Joiner) AddString

func (j *Joiner) AddString(data string)

func (*Joiner) Done

func (j *Joiner) Done() []byte

func (*Joiner) LastByte

func (j *Joiner) LastByte() byte

func (*Joiner) Length

func (j *Joiner) Length() uint32

type Options added in v0.8.12

type Options struct {
	OutputFormat        config.Format
	RemoveWhitespace    bool
	MangleSyntax        bool
	ASCIIOnly           bool
	ExtractComments     bool
	Indent              int
	ToModuleRef         js_ast.Ref
	WrapperRefForSource func(uint32) js_ast.Ref
	UnsupportedFeatures compat.JSFeature

	// This contains the contents of the input file to map back to in the source
	// map. If it's nil that means we're not generating source maps.
	SourceForSourceMap *logger.Source

	// This will be present if the input file had a source map. In that case we
	// want to map all the way back to the original input file(s).
	InputSourceMap *sourcemap.SourceMap
}

type PrintResult

type PrintResult struct {
	JS []byte

	// This source map chunk just contains the VLQ-encoded offsets for the "JS"
	// field above. It's not a full source map. The bundler will be joining many
	// source map chunks together to form the final source map.
	SourceMapChunk SourceMapChunk

	ExtractedComments map[string]bool

	// These are used when stripping off the leading variable declaration
	FirstDeclByteOffset      uint32
	FirstDeclSourceMapOffset uint32
}

func Print

func Print(tree js_ast.AST, symbols js_ast.SymbolMap, r renamer.Renamer, options Options) PrintResult

func PrintExpr

func PrintExpr(expr js_ast.Expr, symbols js_ast.SymbolMap, r renamer.Renamer, options Options) PrintResult

type QuotedSource

type QuotedSource struct {
	// These are quoted ahead of time instead of during source map generation so
	// the quoting happens in parallel instead of in serial
	QuotedPath     []byte
	QuotedContents []byte
}

type SourceMapChunk

type SourceMapChunk struct {
	Buffer []byte

	// There may be more than one source for this chunk if the file being printed
	// has an associated source map. In that case the "source index" values in
	// the buffer are 0-based indices into this array. The source index of the
	// first mapping will be adjusted when the chunks are joined together. Since
	// the source indices are encoded using a delta from the previous source
	// index, none of the other source indices need to be modified while joining.
	QuotedSources []QuotedSource

	// This end state will be used to rewrite the start of the following source
	// map chunk so that the delta-encoded VLQ numbers are preserved.
	EndState SourceMapState

	// There probably isn't a source mapping at the end of the file (nor should
	// there be) but if we're appending another source map chunk after this one,
	// we'll need to know how many characters were in the last line we generated.
	FinalGeneratedColumn int

	// Like "FinalGeneratedColumn" but for the generated column of the last
	// semicolon for a "SLocal" statement.
	FinalLocalSemi int

	ShouldIgnore bool
}

type SourceMapState

type SourceMapState struct {
	// This isn't stored in the source map. It's only used by the bundler to join
	// source map chunks together correctly.
	GeneratedLine int

	// These are stored in the source map in VLQ format.
	GeneratedColumn int
	SourceIndex     int
	OriginalLine    int
	OriginalColumn  int
}

Coordinates in source maps are stored using relative offsets for size reasons. When joining together chunks of a source map that were emitted in parallel for different parts of a file, we need to fix up the first segment of each chunk to be relative to the end of the previous chunk.

Jump to

Keyboard shortcuts

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