Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magic-string

Package Overview
Dependencies
Maintainers
4
Versions
116
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-string

Modify strings, generate sourcemaps

  • 0.26.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.9M
decreased by-80.02%
Maintainers
4
Weekly downloads
 
Created

What is magic-string?

The magic-string package is a utility library designed for use in compilers and other tools that manipulate strings, particularly for source code transformations. It allows for efficient editing of string content, such as adding, replacing, and removing sections, while keeping track of original and modified positions. This is particularly useful for tasks like source map generation, code rewriting, and more.

What are magic-string's main functionalities?

Generate a source map

This feature demonstrates how to prepend and append content to a string and generate a source map for the transformation. It's useful for tracking changes between the original and transformed code.

const MagicString = require('magic-string');
let s = new MagicString('export var answer = 42;');
s.prepend('/* hello */\n').append('\n/* world */');
console.log(s.toString());
console.log(s.generateMap({ hires: true }));

Replace content

This feature shows how to replace a specific part of the string ('answer' with 'question'). It's particularly useful for code modifications where precise control over the text is needed.

const MagicString = require('magic-string');
let s = new MagicString('export var answer = 42;');
s.overwrite(12, 17, 'question');
console.log(s.toString());

Remove content

This feature illustrates how to remove a section of the string (the 'export ' part). It's useful for cleaning up or simplifying code by removing unnecessary parts.

const MagicString = require('magic-string');
let s = new MagicString('export var answer = 42;');
s.remove(0, 7);
console.log(s.toString());

Other packages similar to magic-string

Keywords

FAQs

Package last updated on 03 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc