
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
babel-plugin-define-variables
Advanced tools
A Babel plugin that works like webpack.DefinePlugin for defining global variables and constants at compile time.
babel-plugin-define-variables is a powerful Babel plugin that allows you to define global variables and constants at compile time, similar to webpack's DefinePlugin. This plugin is particularly useful for:
npm install --save-dev babel-plugin-define-variables
# or
yarn add -D babel-plugin-define-variables
// babel.config.js
module.exports = {
presets: [
'@babel/preset-env'
],
plugins: [
[
'babel-plugin-define-variables',
{
defines: {
'process.env.BUILD_ENV': process.env.BUILD_ENV,
'process.env.NODE_ENV': process.env.NODE_ENV,
'VERSION': '1.0.0',
'IS_PRODUCTION': process.env.NODE_ENV === 'production'
}
// builtIns not specified - all variables enabled by default
}
]
]
};
// babel.config.js
module.exports = {
presets: [
'@babel/preset-env'
],
plugins: [
[
'babel-plugin-define-variables',
{
defines: {
'process.env.BUILD_ENV': process.env.BUILD_ENV,
'process.env.NODE_ENV': process.env.NODE_ENV,
'VERSION': '1.0.0',
'IS_PRODUCTION': process.env.NODE_ENV === 'production'
},
builtIns: {
filename: true, // Enable __filename__ (default)
filehash: true, // Enable __filehash__ (default)
dirname: true, // Enable __dirname__ (default)
now: true, // Enable __now__ (default)
timestamp: true, // Enable __timestamp__ (default)
packagename: true, // Enable __packagename__ (default)
packageversion: true // Enable __packageversion__ (default)
}
}
]
]
};
defines ObjectUsed to define custom global variables, supports the following value types:
builtIns ObjectControls the enable/disable state of built-in variables. All built-in variables are enabled by default. If you want to disable any of them, you need to explicitly set them to false.
Important Notes:
builtIns if you want all variables enabled (default behavior)false| Option | Default | Description |
|---|---|---|
filename | true | Whether to enable __filename__ variable |
filehash | true | Whether to enable __filehash__ variable |
dirname | true | Whether to enable __dirname__ variable |
now | true | Whether to enable __now__ variable |
timestamp | true | Whether to enable __timestamp__ variable |
packagename | true | Whether to enable __packagename__ variable |
packageversion | true | Whether to enable __packageversion__ variable |
Example of disabling specific built-ins:
{
builtIns: {
filename: false, // Disable __filename__
filehash: false, // Disable __filehash__
now: false // Disable __now__
// Other variables remain enabled by default
}
}
__filename__The file path of the current code file relative to the project root directory (where package.json is located).
Example:
console.log(__filename__); // Output: "/src/components/Button.js"
__dirname__The directory path of the current code file relative to the project root directory.
Example:
console.log(__dirname__); // Output: "/src/components"
__filehash__The hash value of the current code file, generated based on the filename.
Example:
console.log(__filehash__); // Output: "d7bfcc4a"
__now__The time at build moment, formatted as 'yyyy-MM-dd hh:mm:ss'.
Example:
console.log(__now__); // Output: "2024-01-15 14:30:25"
__timestamp__The timestamp at build moment (milliseconds).
Example:
console.log(__timestamp__); // Output: 1705312225000
__packagename__The package name of the current project.
Example:
console.log(__packagename__); // Output: "babel-plugin-define-variables"
__packageversion__The package version of the current project, or the version of a specified package.
Usage:
// Get current project version
console.log(__packageversion__); // Output: "0.0.4"
// Get version of specified package
console.log(__packageversion__('react')); // Output: "18.2.0"
console.log(__packageversion__('@babel/core')); // Output: "7.5.4"
// Configuration
{
defines: {
'process.env.API_URL': process.env.API_URL || 'http://localhost:3000',
'process.env.DEBUG': process.env.DEBUG || false
}
}
// Usage
if (process.env.DEBUG) {
console.log('API URL:', process.env.API_URL);
}
// Configuration
{
defines: {
'BUILD_TIME': new Date().toISOString(),
'GIT_COMMIT': process.env.GIT_COMMIT || 'unknown'
}
}
// Usage
console.log('Build time:', BUILD_TIME);
console.log('Git commit:', GIT_COMMIT);
// Using built-in variables
const configPath = __dirname__ + '/config.json';
const fileHash = __filehash__;
// Check version
if (__packageversion__('react').startsWith('18.')) {
console.log('Using React 18');
}
// Display build information
console.log(`Building ${__packagename__} v${__packageversion__} at ${__now__}`);
function test() {
console.log('__filename__', __filename__);
console.log('__filehash__', __filehash__);
console.log('__dirname__', __dirname__);
console.log('__now__', __now__);
console.log('__timestamp__', __timestamp__);
console.log('__packagename__', __packagename__);
console.log('__packageversion__', __packageversion__);
console.log('__packageversion__', __packageversion__(''));
console.log('__packageversion__', __packageversion__('@babel/cli'));
console.log('process.env.BUILD_ENV', process.env.BUILD_ENV);
__packageversion__.split('.');
}
export default test;
function test() {
console.log('__filename__', "/demo/src/test1.js");
console.log('__filehash__', "d7bfcc4a");
console.log('__dirname__', "/demo/src");
console.log('__now__', "2020-12-03 18:43:46");
console.log('__timestamp__', 1606992227198);
console.log('__packagename__', "babel-plugin-define-variables");
console.log('__packageversion__', "0.0.2");
console.log('__packageversion__', "");
console.log('__packageversion__', "7.6.4");
console.log('process.env.BUILD_ENV', "test");
"0.0.2".split('.');
}
package.json is located)__packageversion__('packageName') will try to resolve the version of the specified package, returning an empty string if the package doesn't exist__now__ and __timestamp__ are generated at each build, not calculated at runtimeIssues and Pull Requests are welcome!
MIT License
FAQs
一个类似 webpack.DefinePlugin 的 Babel 插件,用于在编译时定义全局变量和常量
The npm package babel-plugin-define-variables receives a total of 106 weekly downloads. As such, babel-plugin-define-variables popularity was classified as not popular.
We found that babel-plugin-define-variables demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.