npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-transform-regex

v4.3.3

Published

Babel plugin for the regex package

Downloads

428

Readme

babel-plugin-transform-regex

npm version

This is a Babel plugin that transpiles tagged regex templates into native RegExp literals, enabling syntax for modern and more readable regex features (atomic groups, subroutines, insignificant whitespace, comments, etc.) without the need for calling regex at runtime. Although regex is already a lightweight and high-performance library, this takes things further by giving you its developer experience benefits without adding any runtime dependencies and without users paying any runtime cost.

Try the demo REPL

Example

Input:

const ipv4 = regex`
  ^ \g<byte> (\.\g<byte>){3} $

  (?(DEFINE)
    (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d)
  )
`;

Output:

const ipv4 = /^(?:2[0-4]\d|25[0-5]|1\d\d|[1-9]?\d)(?:\.(?:2[0-4]\d|25[0-5]|1\d\d|[1-9]?\d)){3}$/v;

Supported

The following call formats are all supported:

  • regex`<expression>`
  • regex()`<expression>`
  • regex('<flags>')`<expression>`
  • regex({<options>})`<expression>`

Interpolation into the expression is supported, so long as the interpolated values are:

  • Inline string, regexp, or number literals.
  • Inline regexes constructed via RegExp or new RegExp with string values.
  • Inline patterns, via pattern`…` as a template tag (without interpolation) or pattern(…) as a funcion call with a string or number literal as the value.

Additional details:

  • Wherever strings are allowed, '…', "…", `…`, and String.raw`…` can all be used, so long as they don't include interpolation.

Unsupported

  • Tagged regex templates that interpolate variables or other dynamic values are not transformed.
  • The specific regex options subclass, plugins, and unicodeSetsPlugin are unsupported. Regexes that use these options are not transformed.
  • Calling the regex tag as a function instead of with backticks is not transformed.

Babel plugin options

The following options are available when running the Babel plugin:

  • removeImport — If true, removes any import declarations with module name 'regex'.
  • disableUnicodeSets — If true, adds regex option disable: {v: true} to all regexes before transformation.
  • optimize (experimental) — If true, attempts to further optimize the regex source generated by regex.
    • Uses an external library (regexp-tree's optimizer) that doesn't support flag-v-only syntax and isn't fully context-aware, so you should check the output.
  • headerComment — If given a value, it will be added in a comment at the top of processed output/files.

Compatibility

By default, the regex tag implicitly adds flag v (unicodeSets, supported by Node.js 20 and 2023-era browsers) to generated regexes, but it automatically switches to flag u (while applying v's escaping rules) in environments without native v support. This creates an issue for the Babel plugin, because although it will typically be run in environments that support flag v, the transpiled results may need to run for users in old browsers without native v.

There are several ways to address this:

  • Option 1: Leave v enabled and transpile v with a separate Babel plugin.
  • Option 2: Disable v for all transpiled regexes.
    • To do this, set the Babel plugin option disableUnicodeSets: true (see details above).
    • This keeps things simple/clean and avoids a second regex transpilation step.
    • This doesn't support the use of v-only syntax.
  • Option 3: Disable v for individual regexes.
    • To do this, use the regex option regex({disable: {v: true}})`…` in your code.
    • This maintains 100% parity between code running with or without the Babel plugin.
    • This doesn't support the use of v-only syntax.

You can try all these options in the demo REPL.

Installation and usage

Add this plugin and a recent version of Babel (tested with v7.24) to your project:

npm install --save-dev @babel/core @babel/cli
npm install --save-dev babel-plugin-transform-regex

Run the following command to compile all of your code from the src directory to lib:

./node_modules/.bin/babel src --out-dir lib --plugins=babel-plugin-transform-regex

Optional setup steps

To make this easier to run, create a config file in the root of your project named babel.config.json, with this content:

{
  "plugins": ["babel-plugin-transform-regex"]
}

Or to set plugin options:

{
  "plugins": [["babel-plugin-transform-regex", {"removeImport": true}]]
}

Then add a script to your package.json to run the build:

"scripts": {
  "build": "babel src --out-dir lib"
}

After that, you can run it via npm run build.