# rich-harris/magic-string

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/rich-harris-magic-string).**

2,668 stars · 116 forks · JavaScript · mit

## Links

- GitHub: https://github.com/Rich-Harris/magic-string
- awesome-repositories: https://awesome-repositories.com/repository/rich-harris-magic-string.md

## Description

magic-string is a low-level JavaScript library designed for compilers and bundlers to perform source code transformations. It serves as a string transformation utility and source map manipulation library that allows for modifying text while automatically tracking character offsets.

The library enables the modification of source code strings through insertions, replacements, and removals without losing the relationship to original source positions. It specifically provides mechanisms to generate mapping files that link transformed output text back to original source file coordinates.

Capabilities include adjusting text indentation while excluding specific character ranges and concatenating multiple modified strings into a single bundle with a shared mapping file. The tool also supports replacing text patterns and analyzing string state.

## Tags

### Development Tools & Productivity

- [Transformation Libraries](https://awesome-repositories.com/f/development-tools-productivity/javascript-and-typescript-build-systems/transformation-libraries.md) — Serves as a fundamental library for compilers and bundlers to perform source code transformations and track mapping data.
- [Source Map Generators](https://awesome-repositories.com/f/development-tools-productivity/source-map-generators.md) — Creates mapping files with configurable resolution to link generated output back to original source files. ([source](https://github.com/Rich-Harris/magic-string#readme))
- [Source-Aware Transformations](https://awesome-repositories.com/f/development-tools-productivity/string-utilities/source-aware-transformations.md) — Provides a utility for inserting, replacing, and indenting text without losing the relationship to original source positions.
- [Range-Aware](https://awesome-repositories.com/f/development-tools-productivity/code-quality-analysis/static-analysis-engines/static-analysis-tools/code-formatting-tools/indentation-settings/range-aware.md) — Adds or adjusts whitespace prefixes in text files while excluding specific ranges from the modification.
- [Range-Aware Indentation Logic](https://awesome-repositories.com/f/development-tools-productivity/indentation-rules/range-aware-indentation-logic.md) — Adds indentation to lines of text while excluding specific character ranges from the process. ([source](https://github.com/Rich-Harris/magic-string/blob/master/CHANGELOG.md))
- [Range-Excluding Source Indentation](https://awesome-repositories.com/f/development-tools-productivity/indentation-rules/range-excluding-source-indentation.md) — Prefixes lines with specific strings or guessed indentation while excluding defined character ranges. ([source](https://github.com/Rich-Harris/magic-string/blob/master/README.md))
- [Range-Excluding Indentations](https://awesome-repositories.com/f/development-tools-productivity/indentation-rules/snippet-indentation-overrides/range-excluding-indentations.md) — Allows prefixing lines with indentation strings while ignoring specified character ranges to maintain layout. ([source](https://github.com/Rich-Harris/magic-string#readme))

### Software Engineering & Architecture

- [Offset Tracking](https://awesome-repositories.com/f/software-engineering-architecture/string-validation-and-normalization/speech-to-text-normalizers/custom-text-normalizers/offset-tracking.md) — Tracks character offsets during text modifications to maintain precise alignment between original and transformed source code.
- [Immutable Range Awareness](https://awesome-repositories.com/f/software-engineering-architecture/indentation-engines/immutable-range-awareness.md) — Calculates indentation shifts while ignoring specified character ranges marked as immutable to preserve layout.

### Part of an Awesome List

- [Sourcemap Management](https://awesome-repositories.com/f/awesome-lists/devtools/javascript-minifiers/sourcemap-management.md) — Generates standardized sourcemaps that link transformed output text back to original source file coordinates.

### Data & Databases

- [String Edit Queues](https://awesome-repositories.com/f/data-databases/data-joins/deferred-materialization/string-edit-queues.md) — Implements deferred string materialization to optimize performance by queuing edits before generating the final output.

### DevOps & Infrastructure

- [Offset-Tracking Text Transformations](https://awesome-repositories.com/f/devops-infrastructure/cicd-pipeline-automation/cicd-pipeline-management/build-pipelines/plugin-based-transformation-pipelines/source-text-transformation-streams/offset-tracking-text-transformations.md) — Changes content by inserting, removing, or replacing text while tracking original character offsets for consistency. ([source](https://github.com/Rich-Harris/magic-string/blob/master/CHANGELOG.md))

### Programming Languages & Runtimes

- [Offset-Aware String Modifications](https://awesome-repositories.com/f/programming-languages-runtimes/offset-aware-string-modifications.md) — Updates text within strings while tracking original character offsets to ensure accurate source mapping. ([source](https://github.com/Rich-Harris/magic-string#readme))
- [Offset-Preserving Text Insertions](https://awesome-repositories.com/f/programming-languages-runtimes/offset-preserving-text-insertions.md) — Appends or prepends content at specific character positions to keep insertions associated with the correct original range. ([source](https://github.com/Rich-Harris/magic-string#readme))
- [Source Code Transformation Engines](https://awesome-repositories.com/f/programming-languages-runtimes/source-code-compilers/source-code-templates/source-code-transformation-engines.md) — Modifies JavaScript source code strings by inserting or replacing text while preserving accurate character offset tracking.
- [Mapped String Concatenations](https://awesome-repositories.com/f/programming-languages-runtimes/mapped-string-concatenations.md) — Merges multiple modified strings into a single bundle while preserving individual mapping data for each segment. ([source](https://github.com/Rich-Harris/magic-string/blob/master/CHANGELOG.md))
- [Source Bundle Concatenations](https://awesome-repositories.com/f/programming-languages-runtimes/source-bundle-concatenations.md) — Combines several modified strings into a single bundle using unified indentation and a shared mapping file. ([source](https://github.com/Rich-Harris/magic-string#readme))
- [Sourcemap Offset Concatenations](https://awesome-repositories.com/f/programming-languages-runtimes/sourcemap-offset-concatenations.md) — Provides additive mapping concatenation to merge multiple offset maps into a single bundle while shifting coordinates.
- [String Pattern Replacements](https://awesome-repositories.com/f/programming-languages-runtimes/string-pattern-replacements.md) — Executes string or regular expression replacements based on the content of the original source text. ([source](https://github.com/Rich-Harris/magic-string#readme))

### Web Development

- [Build Tooling](https://awesome-repositories.com/f/web-development/build-tooling.md) — Provides a low-level utility for compilers and bundlers to merge modified strings into a single output file.
