Code Monkey home page Code Monkey logo

magic-string-extra's Introduction

DEPRECATED. It has been ported back to magic-string >= 0.26.0


magic-string-extra

NPM version

Extended Rich-Harris/magic-string with extra utilities.

Install

npm i magic-string-extra

magic-string-extra can be a drop-in replacement for magic-string:

- import MagicString from 'magic-string'
+ import MagicString from 'magic-string-extra'

Extra Utils

Check magic-string's documentation for the base utils.

.replace()

Shares the same signature as String.prototype.replace. Supports RegExp and replacer functions.

import MagicString from 'magic-string-extra'

const s = new MagicString(source)

s.replace(foo, 'bar')
s.replace(/foo/g, 'bar')
s.replace(/(\w)(\d+)/g, (_, $1, $2) => $1.toUpperCase() + $2)

The differences from String.replace:

  • It will always match against the original string
  • It mutates the magic string state (use .clone() to be immutable)

.hasChanged()

In some cases, when the string does not change you might be able to skip the sourcemap generation to improve the performance (e.g. Rollup and Vite's transform hook). This function allows you to check the state without tracking it externally.

import MagicString from 'magic-string-extra'

const s = new MagicString(source)

s.hasChanged() // false

s.prepend('foo')

s.hasChanged() // true

.toRollupResult()

It's common to use the magic string for code transformations in plugins. This function provides a shorthand to generate the result in Rollup's TransformResult format. When the string has not changed, null will be returned to skip the Rollup transformation.

import MagicString from 'magic-string-extra'

const s = new MagicString(source)

s.toRollupResult() // { code, map } | null

Included Upstream PRs

Sponsors

License

MIT License © 2022 Anthony Fu

magic-string-extra's People

Contributors

antfu avatar danielroe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.