sourcemap

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: CC0-1.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sourcemap provides source map generation for the WGSL minifier.

It implements the Source Map v3 format as specified at: https://sourcemaps.info/spec.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeVLQ

func DecodeVLQ(input string) (int, int)

DecodeVLQ decodes a VLQ base64 string and returns the value and bytes consumed. Returns (0, 0) if the input is empty or invalid.

func DecodeVLQSequence

func DecodeVLQSequence(input string, n int) ([]int, error)

DecodeVLQSequence decodes a VLQ sequence expecting n values. Returns an error if not enough values can be decoded.

func EncodeVLQ

func EncodeVLQ(value int) string

EncodeVLQ encodes a signed integer as a VLQ base64 string. The encoding follows the source map v3 specification.

func EncodeVLQSequence

func EncodeVLQSequence(values []int) string

EncodeVLQSequence encodes multiple values as a VLQ sequence.

Types

type Generator

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

Generator builds a source map incrementally.

func NewGenerator

func NewGenerator(source string) *Generator

NewGenerator creates a new source map generator for the given original source.

func (*Generator) AddMapping

func (g *Generator) AddMapping(genLine, genCol, srcOffset int, name string)

AddMapping adds a mapping from generated position to source position. genLine and genCol are 0-indexed positions in the generated output. srcOffset is the byte offset in the original source. name is the original name (empty string if no name mapping needed).

func (*Generator) Generate

func (g *Generator) Generate() *SourceMap

Generate produces the final SourceMap.

func (*Generator) IncludeSourceContent

func (g *Generator) IncludeSourceContent(include bool)

IncludeSourceContent sets whether to include original source in sourcesContent.

func (*Generator) SetCoverLinesWithoutMappings

func (g *Generator) SetCoverLinesWithoutMappings(cover bool)

SetCoverLinesWithoutMappings enables or disables the line coverage workaround. When enabled (default), a mapping at column 0 is added for any line that would otherwise have no mappings. This works around a bug in Mozilla's source-map library that returns null for lines without a column 0 mapping.

func (*Generator) SetFile

func (g *Generator) SetFile(file string)

SetFile sets the generated file name.

func (*Generator) SetSourceName

func (g *Generator) SetSourceName(name string)

SetSourceName sets the original source file name.

type LineIndex

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

LineIndex provides efficient byte offset to line/column conversion. It pre-computes line start positions for O(log n) lookups.

func NewLineIndex

func NewLineIndex(source string) *LineIndex

NewLineIndex creates a LineIndex for the given source.

func (*LineIndex) ByteOffsetToLineColumn

func (idx *LineIndex) ByteOffsetToLineColumn(offset int) (line, col int)

ByteOffsetToLineColumn converts a byte offset to 0-indexed line and column. The column is in bytes (not UTF-16 code units).

func (*LineIndex) ByteOffsetToLineColumnUTF16

func (idx *LineIndex) ByteOffsetToLineColumnUTF16(offset int) (line, col int)

ByteOffsetToLineColumnUTF16 converts a byte offset to 0-indexed line and column. The column is in UTF-16 code units, as required by the source map spec.

func (*LineIndex) LineColumnToByteOffset

func (idx *LineIndex) LineColumnToByteOffset(line, col int) int

LineColumnToByteOffset converts a 0-indexed line and column to byte offset. The column is expected in bytes.

func (*LineIndex) LineCount

func (idx *LineIndex) LineCount() int

LineCount returns the number of lines in the source.

type Mapping

type Mapping struct {
	GenLine   int  // Generated line (0-indexed)
	GenCol    int  // Generated column (0-indexed)
	SrcIndex  int  // Source file index
	SrcLine   int  // Source line (0-indexed)
	SrcCol    int  // Source column (0-indexed)
	NameIndex int  // Name index (-1 if no name)
	HasName   bool // Whether this mapping has a name
}

Mapping represents a decoded source map mapping.

func DecodeMappings

func DecodeMappings(mappings string) ([]Mapping, error)

DecodeMappings decodes a VLQ-encoded mappings string.

type SourceMap

type SourceMap struct {
	Version        int      `json:"version"`
	File           string   `json:"file,omitempty"`
	SourceRoot     string   `json:"sourceRoot,omitempty"`
	Sources        []string `json:"sources"`
	SourcesContent []string `json:"sourcesContent,omitempty"`
	Names          []string `json:"names"`
	Mappings       string   `json:"mappings"`
}

SourceMap represents a Source Map v3. See https://sourcemaps.info/spec.html

func (*SourceMap) ToComment

func (sm *SourceMap) ToComment(inline bool) string

ToComment returns a source map comment for appending to generated code. For WGSL, we use JavaScript-style comments as WGSL doesn't have a standard.

func (*SourceMap) ToDataURI

func (sm *SourceMap) ToDataURI() string

ToDataURI returns the source map as a data URI for inline embedding.

func (*SourceMap) ToJSON

func (sm *SourceMap) ToJSON() string

ToJSON returns the source map as a JSON string.

Jump to

Keyboard shortcuts

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