How to Import a Single Lodash Function?

后端 未结 7 1779
無奈伤痛
無奈伤痛 2020-11-30 19:42

Using webpack, I\'m trying to import isEqual since lodash seems to be importing everything. I\'ve tried doing the following with no success:

imp         


        
7条回答
  •  被撕碎了的回忆
    2020-11-30 20:03

    Lodash lists a couple of options in their README:

    • babel-plugin-lodash

      • Install lodash and the babel plugin:
      $ npm i --save lodash
      $ npm i --save-dev babel-plugin-lodash @babel/cli @babel/preset-env
      
      • Add this to your .babelrc
      {
        "plugins": ["lodash"],
        "presets": [["@babel/env", { "targets": { "node": 6 } }]]
      }
      
      • Transforms this
      import _ from 'lodash'
      import { add } from 'lodash/fp'
      
      const addOne = add(1)
      _.map([1, 2, 3], addOne)
      

      Roughly to this:

      import _add from 'lodash/fp/add'
      import _map from 'lodash/map'
      
      const addOne = _add(1)
      _map([1, 2, 3], addOne)
      
    • lodash-webpack-plugin

      • Install lodash and webpack plugin:
      $ npm i --save lodash
      $ npm i --save-dev lodash-webpack-plugin babel-core babel-loader babel-plugin-lodash babel-preset-env webpack
      
      • Configure your webpack.config.js:
      var LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
      var webpack = require('webpack');
      
      module.exports = {
        'module': {
          'rules': [{
            'use': 'babel-loader',
            'test': /\.js$/,
            'exclude': /node_modules/,
            'options': {
              'plugins': ['lodash'],
              'presets': [['env', { 'modules': false, 'targets': { 'node': 4 } }]]
            }
          }]
        },
        'plugins': [
          new LodashModuleReplacementPlugin,
          new webpack.optimize.UglifyJsPlugin
        ]
      };
      
    • lodash-es using the lodash cli

      • $ lodash modularize exports=es -o ./

提交回复
热议问题