Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
babel-plugin-transform-regex
Advanced tools
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.
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;
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:
RegExp
or new RegExp
with string values.pattern`…`
as a template tag (without interpolation) or pattern(…)
as a funcion call with a string or number literal as the value.Additional details:
'…'
, "…"
, `…`
, and String.raw`…`
can all be used, so long as they don't include interpolation.regex
templates that interpolate variables or other dynamic values are not transformed.regex
options subclass
, plugins
, and unicodeSetsPlugin
are unsupported. Regexes that use these options are not transformed.regex
tag as a function instead of with backticks is not transformed.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
.
headerComment
— If given a value, it will be added in a comment at the top of processed output/files.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:
disableUnicodeSets: true
(see details above).regex
option regex({disable: {v: true}})`…`
in your code.You can try all these options in the demo REPL.
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
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
.
FAQs
Babel plugin for the regex package
The npm package babel-plugin-transform-regex receives a total of 91 weekly downloads. As such, babel-plugin-transform-regex popularity was classified as not popular.
We found that babel-plugin-transform-regex demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.