babel-plugin-minify-constant-folding

Example

In

  1. "a" + "b"
  2. 2 * 3;
  3. 4 | 3;
  4. "b" + a + "c" + "d" + g + z + "f" + "h" + "i"
  5. [a, b, c].concat([d, e], f, g, [h]);
  6. ["a", "b", "c"].join();
  7. ["a", "b", "c"].join('@');
  8. [1, 2, 3].length;
  9. [1, 2, 3][1];
  10. [1, 2, 3].shift();
  11. [1, 2, 3].slice(0, 2);
  12. [a, b, c].pop();
  13. [a, b, c].reverse();
  14. "a,b,c".split(",");
  15. "abc"[0];
  16. "abc".charAt();
  17. "abc".charAt(1);
  18. "abc".length;

Out

  1. "ab";
  2. 6;
  3. 7;
  4. "b" + a + "cd" + g + z + "fhi";
  5. [a, b, c, d, e, f, g, h];
  6. "a,b,c";
  7. "a@b@c";
  8. 3;
  9. 2;
  10. 2;
  11. [1, 2];
  12. c;
  13. [c, b, a];
  14. ["a", "b", "c"];
  15. "a";
  16. "a";
  17. "a";
  18. "b";
  19. 3;

Installation

  1. npm install babel-plugin-minify-constant-folding --save-dev

Usage

.babelrc

  1. {
  2. "plugins": ["minify-constant-folding"]
  3. }

Via CLI

  1. babel --plugins minify-constant-folding script.js

Via Node API

  1. require("@babel/core").transform("code", {
  2. plugins: ["minify-constant-folding"]
  3. });

Options

  • tdz - Account for TDZ (Temporal Dead Zone)

You can read more about configuring plugin options here