Registry / devops / babel-plugin-define-patterns

babel-plugin-define-patterns

JSON →
library1.0.0jsnpmunverified

babel-plugin-define-patterns is a Babel plugin designed to perform build-time constant and expression replacements. It operates directly on the Abstract Syntax Tree (AST) during the Babel compilation process, allowing developers to define arbitrary JavaScript expressions or patterns that are replaced with static values. This functionality is analogous to Webpack's DefinePlugin but is integrated at the Babel transformation level, making it bundler-agnostic. Key use cases include injecting environment variables (e.g., `process.env.NODE_ENV`), build-time flags (e.g., `__DEV__`), or specific build metadata (e.g., `require('currentBuildNumber')`). The current stable version is 1.0.0. As a Babel plugin, its release cadence is typically tied to Babel's ecosystem, though specific information on its own cadence is not provided. Its primary differentiator is its deep integration into the Babel AST transformation, offering fine-grained control over code alteration before bundling.

npm install babel-plugin-define-patterns
INSTALL
IMPORT
SIG · BABEL-PLUGIN-DEFIN
B
babel-plugin-define-patterns
devopsjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

define-patterns
["define-patterns", { /* options */ }]
["babel-plugin-define-patterns", { /* options */ }]
The plugin is typically referenced by its short name ('define-patterns') in `babel.config.json` or similar configuration files.
pluginModule
const definePatterns = require('babel-plugin-define-patterns');
import definePatterns from 'babel-plugin-define-patterns';
When using a JavaScript configuration file (e.g., `babel.config.js`) or programmatically, the plugin module is loaded via CommonJS `require()`.
replacements
{ "plugins": [ ["define-patterns", { "replacements": { "process.env.NODE_ENV": "production" } }] ] }
The core configuration object where patterns are mapped to their replacement values.

This quickstart demonstrates how to configure `babel-plugin-define-patterns` in `babel.config.json` to replace various expressions with static values at build-time.

// Install the plugin as a dev dependency // npm install --save-dev babel-plugin-define-patterns // babel.config.json example { "plugins": [ ["define-patterns", { "replacements": { "process.env.NODE_ENV": "production", "typeof window": "object", "__DEV__": true, "require('currentBuildNumber')": 42 } }] ] } // Input file (e.g., src/app.js) // const env = process.env.NODE_ENV; // const isBrowser = typeof window === 'object'; // if (__DEV__) console.log('Development mode'); // const buildNum = require('currentBuildNumber'); // After Babel compilation with the above config, it would transform to: // const env = "production"; // const isBrowser = true; // if (true) console.log('Development mode'); // A minifier would remove the 'if' and just keep the console.log // const buildNum = 42;
Debug
Known issues
gotchaThe order of Babel plugins matters. If another plugin modifies an expression that `babel-plugin-define-patterns` is configured to replace, the pattern might not be matched, and the replacement will not occur. Ensure `define-patterns` runs before any transformations that might alter the target expressions.
fix
Adjust the order of plugins in your Babel configuration. `babel-plugin-define-patterns` should typically be placed early in the plugin array.
affects: >=1.0.0
gotchaPatterns are matched as exact string expressions within the AST. This means `process.env.NODE_ENV` will only match that exact string expression, not `process.env['NODE_ENV']` or `const myEnv = process.env; myEnv.NODE_ENV;`.
fix
Ensure the `replacements` keys exactly match the expressions as they appear in your source code's AST. For more complex scenarios, consider using `babel-plugin-macros` or a custom Babel plugin.
affects: >=1.0.0
gotchaBe mindful of replacing expressions with primitive JavaScript values (e.g., `true`, `false`, `42`) versus string literals (e.g., `'production'`, `'development'`). Replacing with `true` allows dead code elimination by minifiers (e.g., `if (true)`), while replacing with `'production'` will result in a string literal.
fix
Use boolean or numeric literals (`true`, `false`, `42`) if you intend for minifiers to perform dead-code elimination. Use string literals (`'production'`, `'development'`) if you want the string itself to be injected.
affects: >=1.0.0
gotchaOverwriting globally available identifiers like `process` or `window` via this plugin can have unintended side effects or break third-party libraries that rely on their original behavior, especially when targeting different environments.
fix
Only replace expressions that are safely isolated (e.g., specific environment variables or custom global flags). Thoroughly test your application if you are replacing core global object properties.
affects: >=1.0.0
Errors
Common errors & fixes
Error: .plugins[X] must be an array or a string
The plugin configuration is not correctly defined as an array containing the plugin name and its options.
fix
Ensure the plugin is configured as `['define-patterns', { 'replacements': { /* ... */ } }]` within your `plugins` array in `babel.config.json`.
ReferenceError: process is not defined
Attempting to use `process.env.NODE_ENV` in a browser environment without it being transformed by `babel-plugin-define-patterns` or another similar tool, or if the plugin configuration is incorrect.
fix
Verify that `babel-plugin-define-patterns` is correctly configured and running during your build process, and that `process.env.NODE_ENV` is included in its `replacements` options to be replaced with a static value.
Expression 'X' was not replaced as expected.
The string pattern defined in `replacements` does not exactly match the expression in the source code, or another Babel plugin has altered the expression before `define-patterns` could process it.
fix
Double-check the exact string match for your pattern in the `replacements` object. Review your Babel plugin order to ensure `define-patterns` runs before any plugins that might transform the target expression.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
babel-plugin-define-patterns — npm install babel-plugin-define-patterns · libregistry