Skip to main content
Module

x/aleph/server/deps.ts>MagicString

The Full-stack Framework in Deno.
Very Popular
Go to Latest
class MagicString
import { MagicString } from "https://deno.land/x/aleph@1.0.0-beta.1/server/deps.ts";

Constructors

new
MagicString(str: string, options?: MagicStringOptions)

Properties

indentExclusionRanges: ExclusionRange | Array<ExclusionRange>
original: string

Methods

addSourcemapLocation(char: number): void

Adds the specified character index (with respect to the original string) to sourcemap mappings, if hires is false.

append(content: string): MagicString

Appends the specified content to the end of the string.

appendLeft(index: number, content: string): MagicString

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. See also s.prependLeft(...).

appendRight(index: number, content: string): MagicString

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. See also s.prependRight(...).

Does what you'd expect.

generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap

Generates a sourcemap 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.

generateMap(options?: SourceMapOptions): SourceMap

Generates a version 3 sourcemap.

getIndentString(): string
hasChanged(): boolean

Indicates if the string has been changed.

indent(options?: IndentOptions): MagicString

Prefixes each line of the string with prefix. If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.

indent(indentStr?: string, options?: IndentOptions): MagicString

Prefixes each line of the string with prefix. If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.

The options argument can have an exclude property, which is an array of [start, end] character ranges. These ranges will be excluded from the indentation - useful for (e.g.) multiline strings.

isEmpty(): boolean

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

lastChar(): string
lastLine(): string
length(): number
move(
start: number,
end: number,
index: number,
): MagicString

Moves the characters from start and endtoindex`.

overwrite(
start: number,
end: number,
content: string,
options?: boolean | OverwriteOptions,
): MagicString

Replaces the characters from start to end with content. The same restrictions as s.remove() apply.

The fourth argument is optional. It can have a storeName property — if true, the original name will be stored for later inclusion in a sourcemap's names array — and a contentOnly property which determines whether only the content is overwritten, or anything that was appended/prepended to the range as well.

prepend(content: string): MagicString

Prepends the string with the specified content.

prependLeft(index: number, content: string): MagicString

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

prependRight(index: number, content: string): MagicString

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

remove(start: number, end: number): MagicString

Removes the characters from start to end (of the original string, not the generated string). Removing the same content twice, or making removals that partially overlap, will cause an error.

replace(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): MagicString

String replacement with RegExp or string.

slice(start: number, end: number): string

Returns the content of the generated string that corresponds to the slice between start and end of the original string. Throws error if the indices are for characters that were already removed.

snip(start: number, end: number): MagicString

Returns a clone of s, with all content before the start and end characters of the original string removed.

toString(): string

Returns the generated string.

trim(charType?: string): MagicString

Trims content matching charType (defaults to \s, i.e. whitespace) from the start and end.

trimEnd(charType?: string): MagicString

Trims content matching charType (defaults to \s, i.e. whitespace) from the end.

Removes empty lines from the start and end.

trimStart(charType?: string): MagicString

Trims content matching charType (defaults to \s, i.e. whitespace) from the start.