Skip to content

Latest commit

 

History

History
103 lines (72 loc) · 2 KB

README.md

File metadata and controls

103 lines (72 loc) · 2 KB

@tinkoff/babel-plugin-lodash

This plugin transforms lodash imports to include into bundle only imported functions.

Installation

npm install --save-dev @tinkoff/babel-plugin-lodash
yarn add --dev @tinkoff/babel-plugin-lodash

Example

In

import _ from "lodash";
import { add } from "lodash/fp";
import { slice } from "lodash-es";

const addOne = add(1);
_.map([1, 2, 3], addOne);
slice([1, 2, 3], 2);

Out

import _add from "lodash/fp/add";
import _map from "lodash/map";
import _slice from "lodash-es/slice";

const addOne = _add(1);
_map([1, 2, 3], addOne);
_slice([1, 2, 3], 2);

Usage

With a configuration file (Recommended)

.babelrc

{
  "plugins": ["@tinkoff/babel-plugin-lodash"]
}

Via CLI

$ babel --plugins @tinkoff/babel-plugin-lodash script.js

Via Node.js API

require("@babel/core").transformSync(code, {
  plugins: ["@tinkoff/babel-plugin-lodash"],
});

Options

ensureModuleExists

boolean, defaults to false.

When this option is enabled, plugin tries to resolve path to imported lodash module using require.resolve(). If the module can not be found, an error is thrown. Usually such check is done out of box by bundler (e.g. webpack) or typescript. Enabling this option may lead to decreasing performance.

In

import _ from "lodash";

_.slice([1, 2], 1);
_.unknownFunc();

Out

import _slice from "lodash/slice"; // OK
import _unknownFunc from "lodash/unknownFunc"; // ERROR

_slice([1, 2], 1);
_unknownFunc();

Limitations

  • Supported lodash packages: lodash, lodash/fp, lodash-es
  • You must use ES6 imports to load Lodash
  • Babel < 7 & Node.js < 16 aren’t supported
  • Chain sequences aren’t supported. See this blog post for alternatives.
  • Modularized method packages aren’t supported