or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

@babel/plugin-transform-unicode-regex

@babel/plugin-transform-unicode-regex is a Babel plugin that transforms ES2015 Unicode regular expressions (using the u flag) to ES5-compatible syntax. It enables modern Unicode regex features in environments that don't support ES2015, making it essential for maintaining backwards compatibility while using modern JavaScript regex patterns.

Package Information

  • Package Name: @babel/plugin-transform-unicode-regex
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-transform-unicode-regex

Core Imports

// Default import (the plugin function)
import plugin from "@babel/plugin-transform-unicode-regex";

For CommonJS:

const plugin = require("@babel/plugin-transform-unicode-regex");

Note: This plugin only exports a single default export - the plugin function. There are no named exports.

Basic Usage

Babel Configuration

Add the plugin to your Babel configuration:

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

Or in babel.config.js:

module.exports = {
  plugins: ["@babel/plugin-transform-unicode-regex"]
};

Transformation Example

Input (ES2015 with Unicode flag):

var string = "foo💩bar";
var match = string.match(/foo(.)bar/u);

Output (ES5-compatible):

var string = "foo💩bar";
var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))bar/);

Capabilities

Plugin Export

The main and only export of this package is the Babel plugin function.

/**
 * Default export: Babel plugin factory function created with declare()
 * The actual implementation uses declare() and createRegExpFeaturePlugin()
 */
declare const plugin: (
  api: PluginAPI,
  options?: any,
  dirname?: string
) => PluginObject;
export default plugin;

interface PluginAPI {
  assertVersion(range: number | string): void;
  version: string;
  // Additional properties available but not used by this specific plugin
}

interface PluginObject {
  name: string;
  manipulateOptions?: (opts: any, parserOpts: any) => void;
  pre?: (file: any) => void;
  post?: (file: any) => void;
  visitor?: {
    [key: string]: (path: any, state: any) => void;
  };
}

Plugin Configuration

The plugin is created using Babel's helper utilities and specifically targets the Unicode flag feature:

  • Plugin Name: "transform-unicode-regex"
  • Target Feature: "unicodeFlag"
  • Transformation Target: Regular expressions with Unicode flag (/pattern/u)
  • Output: ES5-compatible regex patterns
  • Babel Version Requirement: Requires Babel 7.0.0 or higher (enforced via api.assertVersion(REQUIRED_VERSION(7)))

Plugin Implementation

The source code shows the plugin is implemented as:

import { createRegExpFeaturePlugin } from "@babel/helper-create-regexp-features-plugin";
import { declare } from "@babel/helper-plugin-utils";

export default declare(api => {
  api.assertVersion(REQUIRED_VERSION(7));

  return createRegExpFeaturePlugin({
    name: "transform-unicode-regex",
    feature: "unicodeFlag",
  });
});

Architecture

The plugin is built using Babel's official helper libraries:

  • @babel/helper-create-regexp-features-plugin: Provides the core regex transformation functionality through createRegExpFeaturePlugin
  • @babel/helper-plugin-utils: Provides the declare function for plugin creation
  • Plugin Pattern: Uses the standard Babel plugin factory pattern with declare()

The plugin implementation is minimal - it uses createRegExpFeaturePlugin to handle all the complex Unicode-to-ES5 transformation logic. The plugin configuration specifies:

  • name: "transform-unicode-regex"
  • feature: "unicodeFlag" (targets the Unicode u flag specifically)

This leverages the shared regex transformation infrastructure that multiple Babel regex plugins use, ensuring consistent behavior and optimal performance.

Usage Context

This plugin is automatically included in Babel presets like @babel/preset-env when targeting environments that don't support ES2015 Unicode regex. It can also be used individually when you need specific Unicode regex transformation without other ES2015+ transformations.

Target Environments:

  • Older browsers without ES2015 Unicode regex support
  • JavaScript engines lacking Unicode flag support
  • Legacy Node.js versions
  • Any environment requiring ES5-compatible regex patterns

Use Cases:

  • Maintaining backwards compatibility for Unicode-heavy applications
  • Supporting older browsers while using modern regex features
  • Building libraries that need to work across diverse JavaScript environments
  • Ensuring regex patterns work consistently across all target platforms