Babel plugin that replaces member expressions and typeof statements with strings
npm install babel-plugin-transform-define*
``shell`
$ npm install --save-dev babel-plugin-transform-define
.babelrc
`json`
{
"plugins": [
["transform-define", {
"process.env.NODE_ENV": "production",
"typeof window": "object"
}]
]
}
.babelrc.js
`js
// E.g., any dynamic logic with JS, environment variables, etc.
const overrides = require("./another-path.js");
module.exports = {
plugins: [
["transform-define", {
"process.env.NODE_ENV": "production",
"typeof window": "object",
...overrides
}]
]
};
`
babel-plugin-transform-define can transform certain types of code as a babel transformation.
##### Identifiers
.babelrc
`json`
{
"plugins": [
["transform-define", {
"VERSION": "1.0.0",
}]
]
}
Source Code
`js
VERSION;
window.__MY_COMPANY__ = {
version: VERSION
};
`
Output Code
`js
"1.0.0";
window.__MY_COMPANY__ = {
version: "1.0.0"
};
`Member Expressions
*
#####
.babelrc
`json`
{
"plugins": [
["transform-define", {
"process.env.NODE_ENV": "production"
}]
]
}
Source Code
`js`
if (process.env.NODE_ENV === "production") {
console.log(true);
}
Output Code
`js`
if (true) {
console.log(true);
}Unary Expressions
*
#####
.babelrc
`json`
{
"plugins": [
["transform-define", {
"typeof window": "object"
}]
]
}
Source Code
`js`
typeof window;
typeof window === "object";
Output Code
`js``
'object';
true;
*
Stable: Formidable is not planning to develop any new features for this project. We are still responding to bug reports and security concerns. We are still welcoming PRs for this project, but PRs that include new features should be small and easy to integrate and should not include breaking changes.