Thanks to visit codestin.com
Credit goes to pkg.go.dev

sourcemapx

package
v1.20.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package sourcemapx contains utilities for passing source map information around, intended to work with a [neelance sourcemap].

Mapping Go source coude

GopherJS code generator outputs hints about correspondence between the generated code and original sources inline. Such hints are marked by the special `\b` (0x08) magic byte, followed by a variable-length sequence of bytes, which can be extracted from the byte slice using ReadHint() function.

'\b' was chosen as a magic symbol because it would never occur unescaped in the generated code, other than when explicitly inserted by the source mapping hint. See Hint type documentation for the details of the encoded format.

The hinting mechanism is designed to be extensible, the Hint type able to wrap different types containing different information:

  • go/token.Pos indicates position in the original source the current location in the generated code corresponds to.
  • Identifier maps a JS identifier to the original Go identifier it represents.

More types may be added in future if necessary.

Filter type is used to extract the hints from the written code stream and pass them into source map generator. It also ensures that the encoded inline hints don't make it into the final output, since they are not valid JS.

Mapping JS source code

The filter also provides a WriteJS methods that can be used to write pure JS code (without hints) through the filter. While it is passing through the filter, it will produce a source map for the JS code and pass that source map information to a [neelance sourcemap]. This uses [esbuild]

[neelance sourcemap]:github.com/neelance/sourcemap [esbuild]: https://esbuild.github.io/

Index

Constants

View Source
const HintMagic byte = '\b'

A magic byte in the generated code output that indicates a beginning of a source map hint. The character has been chosen because it should never show up in valid generated code unescaped, other than for source map hint purposes.

Variables

This section is empty.

Functions

func FindHint

func FindHint(b []byte) int

FindHint returns the lowest index in the byte slice where a source map Hint is embedded or -1 if it isn't found. Invariant: if FindHint(b) != -1 then b[FindHint(b)] == '\b'.

Types

type Filter

type Filter struct {
	Writer  io.Writer
	FileSet *token.FileSet
	// contains filtered or unexported fields
}

Filter implements io.Writer which extracts source map hints from the written stream and passed them to the MappingCallback if it's not nil. Encoded hints are always filtered out of the output stream.

func (*Filter) EnableMapping

func (f *Filter) EnableMapping(jsFileName, goroot, gopath string, localMap bool)

func (*Filter) IsMapping

func (f *Filter) IsMapping() bool

func (*Filter) Write

func (f *Filter) Write(p []byte) (n int, err error)

func (*Filter) WriteJS

func (f *Filter) WriteJS(jsSource, jsFilePath string, minify bool) (n int, err error)

func (*Filter) WriteMappingTo

func (f *Filter) WriteMappingTo(w io.Writer) error

type Hint

type Hint struct {
	Payload []byte
}

Hint is a container for a sourcemap hint that can be embedded into the generated code stream. Payload size and semantics depend on the nature of the hint.

Within the stream, the hint is encoded in the following binary format:

  • magic: 0x08 - ASCII backspace, magic symbol indicating the beginning of the hint;
  • size: 16 bit, big endian unsigned int - the size of the payload.
  • payload: [size]byte - the payload of the hint.

func ReadHint

func ReadHint(b []byte) (h Hint, length int)

ReadHint reads the Hint from the beginning of the byte slice and returns the hint and the number of bytes in the slice it occupies. The caller is expected to find the location of the hint using FindHint prior to calling this function.

Returned hint payload does not share backing array with b.

Function panics if:

  • b[0] != '\b'
  • len(b) < size + 3

func (*Hint) Pack

func (h *Hint) Pack(value any) error

Pack the given value into hint's payload.

Supported types: go/token.Pos.

The first byte of the payload will indicate the encoded type, and the rest is an opaque, type-dependent binary representation of the type.

func (*Hint) Unpack

func (h *Hint) Unpack() (any, error)

Unpack and return hint's payload, previously packed by Pack().

func (*Hint) WriteTo

func (h *Hint) WriteTo(w io.Writer) (int64, error)

WriteTo the encoded hint into the output stream. Panics if payload is longer than 0xFFFF bytes.

type Identifier

type Identifier struct {
	Name         string    // Identifier to use in the generated code.
	OriginalName string    // Original identifier name.
	OriginalPos  token.Pos // Original identifier position.
}

Identifier represents a generated code identifier with a the associated original identifier information, which can be used to produce a source map.

This allows us to map a JS function or variable name back to the original Go symbol name.

func (Identifier) EncodeHint

func (i Identifier) EncodeHint() string

EncodeHint returns a string with an encoded source map hint. The hint can be inserted into the generated code to be later extracted by the SourceMapFilter to produce a source map.

func (Identifier) String

func (i Identifier) String() string

String returns generated code identifier name.

Jump to

Keyboard shortcuts

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