Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-transform-define

FormidableLabs559kMIT2.1.4

Babel plugin that replaces member expressions and typeof statements with strings

babel-plugin, babel-transform, babel, define, DefinePlugin, webpack

readme

Babel Plugin Transform Define — Formidable, We build the modern web

npm version Maintenance Status

Compile time code replacement for babel similar to Webpack's DefinePlugin


Quick Start

$ npm install --save-dev babel-plugin-transform-define

.babelrc

{
  "plugins": [
    ["transform-define", {
      "process.env.NODE_ENV": "production",
      "typeof window": "object"
    }]
  ]
}

.babelrc.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
    }]
  ]
};

Reference Documentation

babel-plugin-transform-define can transform certain types of code as a babel transformation.

Identifiers

.babelrc

{
  "plugins": [
    ["transform-define", {
      "VERSION": "1.0.0",
    }]
  ]
}

Source Code

VERSION;

window.__MY_COMPANY__ = {
  version: VERSION
};

Output Code

"1.0.0";

window.__MY_COMPANY__ = {
  version: "1.0.0"
};

Member Expressions

.babelrc

{
  "plugins": [
    ["transform-define", {
      "process.env.NODE_ENV": "production"
    }]
  ]
}

Source Code

if (process.env.NODE_ENV === "production") {
  console.log(true);
}

Output Code

if (true) {
  console.log(true);
}

Unary Expressions

.babelrc

{
  "plugins": [
    ["transform-define", {
      "typeof window": "object"
    }]
  ]
}

Source Code

typeof window;
typeof window === "object";

Output Code

'object';
true;

License

MIT License

Maintenance Status

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.

changelog

Changelog

2.1.4

Patch Changes

  • follow-up to last release, also prevent object properties from being replaced (#88)

2.1.3

Patch Changes

  • Addresses #85, avoids replacing object keys. (#86)

2.1.2

Patch Changes

  • Adding GitHub release workflow (#83)

2.1.1

  • Avoid replacements for variable bindings via #82.

2.1.0

  • Add memoization to getSortedObjectPaths utility for better performance under certain circumstances.

2.0.1

2.0.0 (2019-10-23)

Breaking Changes

  • Change plugin options to only be a real JS object. Removes string configuration path option as now this is all possible with dynamic .babelrc.js or babel.config.js files.
  • Update to @babel/core / Babel 7+.
  • Update package.json:engines to minimum Node 8.

Internal

  • Lint all test code.

1.3.2 (2019-10-22)

  • Various infrastructure updates #54
    • Add CONTRIBUTING.md, yarn.lock, .npmignore, update .gitignore.
    • Switch to yarn for workflows and npm version for release workflow.
  • Bug: Fix sort comparator #48
  • Add test for CallExpression identifiers #35
  • Fixed Markdown headings #42

1.3.1 (2019-01-01)

  • Update lodash to fix vulnerabilities

1.3.0 (2017-05-15)

User Facing Changes

Internal

  • Update eslint config and packages
  • Update lodash version

1.2.0 (2016-08-25)

User Facing Changes

  • Add the ability define config as a deep object
  • Add a Code of Conduct

Internal

  • Remove release scripts
  • Rename ./modules to ./src
  • Add keywords and contributors to package.json
  • Remove author from package.json in favor of contributors
  • Expand test coverage

1.1.0 (2016-08-19)

User Facing Changes

  • Add the ability to get config from a file
  • Add support for Identifiers
  • Major improvements to the README.md

Internal

  • Add tests
  • Add lint
  • Add JSDoc
  • Add CI
  • Add explicit LICENSE file
  • Move dependencies into devDependencies
  • Major refactors to DRY the code

1.0.0 (2016-03-21)

User Facing Changes

  • Update README.md to reference Webpack's Define Plugin

1.0.0 (2016-03-21)

Initial Release