Skip to content

Interface: RolldownMagicString ​

Extends ​

  • BindingMagicString

Properties ​

isRolldownMagicString ​

  • Type: true
  • Experimental

A property that indicates whether this is a RolldownMagicString instance.


filename ​

Get Signature ​

  • Type: () => string | null
  • Experimental
Returns ​

string | null

Inherited from ​

NativeBindingMagicString.filename


original ​

Get Signature ​

  • Type: () => string
  • Experimental
Returns ​

string

Inherited from ​

NativeBindingMagicString.original

Methods ​

addSourcemapLocation() ​

  • Type: (index: number) => void
  • Experimental

Registers a UTF-16 index in the original source for maps with hires: false. If the index falls within a non-newline character in an unedited piece, the map includes a segment for that index. The offset option does not change this index.

Parameters ​

index ​

number

Returns ​

void

Inherited from ​

NativeBindingMagicString.addSourcemapLocation


append() ​

  • Type: (content: string) => this
  • Experimental

Appends the specified content to the end of the string.

Parameters ​

content ​

string

Returns ​

this

Inherited from ​

NativeBindingMagicString.append


appendLeft() ​

  • Type: (index: number, content: string) => this
  • Experimental

Appends the specified content at the index in the original string.

If a range ending with index is subsequently moved, the insert will be moved with it.

Parameters ​

index ​

number

content ​

string

Returns ​

this

Inherited from ​

NativeBindingMagicString.appendLeft


appendRight() ​

  • Type: (index: number, content: string) => this
  • Experimental

Appends the specified content at the index in the original string.

If a range starting with index is subsequently moved, the insert will be moved with it.

Parameters ​

index ​

number

content ​

string

Returns ​

this

Inherited from ​

NativeBindingMagicString.appendRight


clone() ​

  • Type: () => BindingMagicString
  • Experimental

Returns an independent copy of the MagicString instance, including edits, intro/outro, sourcemap locations, stored names, indent exclusion ranges, offset, filename and ignoreList.

Later edits to either string do not affect the other.

Returns ​

BindingMagicString

Inherited from ​

NativeBindingMagicString.clone


generateDecodedMap() ​

  • Type: (options?: { file?: string; hires?: string | boolean; includeContent?: boolean; source?: string; } | null) => BindingDecodedMap
  • Experimental

Generates a decoded source map for the transformations applied to this MagicString. Returns a BindingDecodedMap object with raw mappings in array form, rather than encoded as a string.

Useful if you need to manipulate the sourcemap further, but most of the time you will use generateMap instead.

Parameters ​

options? ​

{ file?: string; hires?: string | boolean; includeContent?: boolean; source?: string; } | null

Type Literal ​

{ file?: string; hires?: string | boolean; includeContent?: boolean; source?: string; }

file? ​

string

The filename for the generated file (goes into map.file)

hires? ​

string | boolean

Accepts boolean or string: true, false, "boundary"

  • true: high-resolution sourcemaps (character-level)
  • false: low-resolution sourcemaps (line-level) - default
  • "boundary": high-resolution only at word boundaries
includeContent? ​

boolean

Whether to include the original content in map.sourcesContent array

source? ​

string

The filename of the original source (goes into map.sources)


null

Returns ​

BindingDecodedMap

Inherited from ​

NativeBindingMagicString.generateDecodedMap


generateMap() ​

  • Type: (options?: { file?: string; hires?: string | boolean; includeContent?: boolean; source?: string; } | null) => BindingSourceMap
  • Experimental

Generates a source map for the transformations applied to this MagicString. Returns a BindingSourceMap object with version, file, sources, sourcesContent, names, mappings.

Parameters ​

options? ​

{ file?: string; hires?: string | boolean; includeContent?: boolean; source?: string; } | null

Type Literal ​

{ file?: string; hires?: string | boolean; includeContent?: boolean; source?: string; }

file? ​

string

The filename for the generated file (goes into map.file)

hires? ​

string | boolean

Accepts boolean or string: true, false, "boundary"

  • true: high-resolution sourcemaps (character-level)
  • false: low-resolution sourcemaps (line-level) - default
  • "boundary": high-resolution only at word boundaries
includeContent? ​

boolean

Whether to include the original content in map.sourcesContent array

source? ​

string

The filename of the original source (goes into map.sources)


null

Returns ​

BindingSourceMap

Inherited from ​

NativeBindingMagicString.generateMap


getIndentString() ​

  • Type: () => string
  • Experimental

Returns the guessed indentation string, or \t if none is found.

This is the value s.indent() uses when no prefix is supplied.

Returns ​

string

Inherited from ​

NativeBindingMagicString.getIndentString


hasChanged() ​

  • Type: () => boolean
  • Experimental

Indicates if the string has been changed.

Returns ​

boolean

Inherited from ​

NativeBindingMagicString.hasChanged


indent() ​

  • Type: (indentor?: string | null, options?: { exclude?: number[] | number[][]; } | null) => this
  • Experimental

Prefixes each line of the string with prefix.

If prefix is not supplied, the value of s.getIndentString() is used.

Parameters ​

indentor? ​

string | null

options? ​

{ exclude?: number[] | number[][]; } | null

Type Literal ​

{ exclude?: number[] | number[][]; }

exclude? ​

number[] | number[][]

An array of [start, end] character ranges to be excluded from the indentation.

This is useful for (e.g.) multiline strings.


null

Returns ​

this

Inherited from ​

NativeBindingMagicString.indent


insert() ​

  • Type: (index: number, content: string) => void
  • Experimental

Deprecated method that throws an error directing users to use prependRight or appendLeft. This matches the original magic-string API which deprecated this method.

Parameters ​

index ​

number

content ​

string

Returns ​

void

Inherited from ​

NativeBindingMagicString.insert


isEmpty() ​

  • Type: () => boolean
  • Experimental

Returns true if the resulting source is empty (disregarding white space).

Returns ​

boolean

Inherited from ​

NativeBindingMagicString.isEmpty


lastChar() ​

  • Type: () => string
  • Experimental

Returns the last character of the generated string, or an empty string if empty.

Returns ​

string

Inherited from ​

NativeBindingMagicString.lastChar


lastLine() ​

  • Type: () => string
  • Experimental

Returns the content after the last newline in the generated string.

This returns the whole string if there is no newline.

Returns ​

string

Inherited from ​

NativeBindingMagicString.lastLine


length() ​

  • Type: () => number
  • Experimental

Returns the length of the generated string, counting only content attached to the original characters.

Content that sits outside the string body is not counted, so s.length() and s.toString().length can disagree. s.prepend() and s.append() always land outside the body, as do inserts made before the first character or after the last one.

Returns ​

number

Inherited from ​

NativeBindingMagicString.length


move() ​

  • Type: (start: number, end: number, index: number) => this
  • Experimental

Moves the characters from start to end to index.

Parameters ​

start ​

number

end ​

number

index ​

number

Returns ​

this

Inherited from ​

NativeBindingMagicString.move


overwrite() ​

  • Type: (start: number, end: number, content: string, options?: { contentOnly?: boolean; storeName?: boolean; } | null) => this
  • Experimental

Replaces the characters from start to end with content, along with the appended/prepended content in that range.

The same restrictions as s.remove() apply.

It may be preferred to use s.update(...) instead if you wish to avoid overwriting the appended/prepended content.

Parameters ​

start ​

number

end ​

number

content ​

string

options? ​

{ contentOnly?: boolean; storeName?: boolean; } | null

Type Literal ​

{ contentOnly?: boolean; storeName?: boolean; }

contentOnly? ​

boolean

Whether only the content is overwritten, or anything that was appended/prepended to the range as well.

storeName? ​

boolean

Stores the replaced content in the generated sourcemap's names field.


null

Returns ​

this

Inherited from ​

NativeBindingMagicString.overwrite


prepend() ​

  • Type: (content: string) => this
  • Experimental

Prepends the string with the specified content.

Parameters ​

content ​

string

Returns ​

this

Inherited from ​

NativeBindingMagicString.prepend


prependLeft() ​

  • Type: (index: number, content: string) => this
  • Experimental

Same as s.appendLeft(...), except that the inserted content will go before any previous appends or prepends at index

Parameters ​

index ​

number

content ​

string

Returns ​

this

Inherited from ​

NativeBindingMagicString.prependLeft


prependRight() ​

  • Type: (index: number, content: string) => this
  • Experimental

Same as s.appendRight(...), except that the inserted content will go before any previous appends or prepends at index

Parameters ​

index ​

number

content ​

string

Returns ​

this

Inherited from ​

NativeBindingMagicString.prependRight


remove() ​

  • Type: (start: number, end: number) => this
  • Experimental

Removes the characters from start to end (of the original string, not the generated string).

Content appended or prepended at positions strictly inside the range is removed along with it, while content attached at start or end is preserved. Use s.overwrite( start, end, '' ) to remove the range including its edge inserts. Repeated removals and removals with partially overlapping ranges are allowed.

Parameters ​

start ​

number

end ​

number

Returns ​

this

Inherited from ​

NativeBindingMagicString.remove


replace() ​

Accepts a string or RegExp pattern with a string replacement ($&, $$, and $N substitutions are supported), or a replacer function invoked like String.prototype.replace.

Parameters ​

from ​

string | RegExp

to ​

string | ReplacementFunction

Returns ​

this

Overrides ​

NativeBindingMagicString.replace


replaceAll() ​

Accepts a string or RegExp pattern (RegExp must have the global (g) flag) with a string replacement, or a replacer function.

Parameters ​

from ​

string | RegExp

to ​

string | ReplacementFunction

Returns ​

this

Overrides ​

NativeBindingMagicString.replaceAll


reset() ​

  • Type: (start: number, end: number) => this
  • Experimental

Resets the portion of the string from start to end to its original content.

This undoes any modifications made to that range. Supports negative indices (counting from the end).

Parameters ​

start ​

number

end ​

number

Returns ​

this

Inherited from ​

NativeBindingMagicString.reset


slice() ​

  • Type: (start?: number | null, end?: number | null) => string
  • Experimental

Returns the content between the specified UTF-16 code unit positions (JS string indices).

Supports negative indices (counting from the end).

Parameters ​

start? ​

number | null

end? ​

number | null

Returns ​

string

Inherited from ​

NativeBindingMagicString.slice


snip() ​

  • Type: (start: number, end: number) => BindingMagicString
  • Experimental

Returns a clone with content outside the specified range removed.

Parameters ​

start ​

number

end ​

number

Returns ​

BindingMagicString

Inherited from ​

NativeBindingMagicString.snip


toString() ​

  • Type: () => string
  • Experimental

Returns the generated string.

Returns ​

string

Inherited from ​

NativeBindingMagicString.toString


trim() ​

  • Type: (charType?: string | null) => this
  • Experimental

Trims whitespace or specified characters from the start and end.

Parameters ​

charType? ​

string | null

defaults to \s

Returns ​

this

Inherited from ​

NativeBindingMagicString.trim


trimEnd() ​

  • Type: (charType?: string | null) => this
  • Experimental

Trims whitespace or specified characters from the end.

Parameters ​

charType? ​

string | null

defaults to \s

Returns ​

this

Inherited from ​

NativeBindingMagicString.trimEnd


trimLines() ​

  • Type: () => this
  • Experimental

Trims newlines from the start and end.

Returns ​

this

Inherited from ​

NativeBindingMagicString.trimLines


trimStart() ​

  • Type: (charType?: string | null) => this
  • Experimental

Trims whitespace or specified characters from the start.

Parameters ​

charType? ​

string | null

defaults to \s

Returns ​

this

Inherited from ​

NativeBindingMagicString.trimStart


update() ​

  • Type: (start: number, end: number, content: string, options?: { overwrite?: boolean; storeName?: boolean; } | null) => this
  • Experimental

Replaces the characters from start to end with content.

The same restrictions as s.remove() apply.

Without overwrite, all of that content is kept, no matter how earlier edits have split the range: content attached at start stays in front of the new content, and the rest follows it in its original order.

s.update(start, end, content) is equivalent to s.overwrite(start, end, content, { contentOnly: true }).

Parameters ​

start ​

number

end ​

number

content ​

string

options? ​

{ overwrite?: boolean; storeName?: boolean; } | null

Type Literal ​

{ overwrite?: boolean; storeName?: boolean; }

overwrite? ​

boolean

Whether anything that was appended/prepended to the range will be overwritten along with the original content.

storeName? ​

boolean

Stores the replaced content in the generated sourcemap's names field.


null

Returns ​

this

Inherited from ​

NativeBindingMagicString.update

Was this page helpful?