Giter Club home page Giter Club logo

lodash-loader's Introduction

Lodash Loader

test node NPM

DEPRECATED: This project is not maintained anymore.

This Webpack loader cherry-picks Lodash functions and require them explicitly to reduce the webpack bundle size.

Installation

npm install lodash-loader

Usage

JavaScript source files

Add this to your webpack.config.js to apply the logic to your .js files.

const createLodashAliases = require('lodash-loader').createLodashAliases;

module.exports = {
  ...
  module: {
      rules: [
          { test: /\.js$/, loader: "babel-loader!lodash-loader" }
      ]
  },
  resolve: {
      alias: createLodashAliases()
  }
  ...
};

TypeScript source files

Add this to your webpack.config.js to apply the logic to your .ts files.

const createLodashAliases = require('lodash-loader').createLodashAliases;

module.exports = {
  ...
  module: {
      rules: [
          { test: /\.ts$/, loader: "ts-loader!lodash-loader" }
      ]
  },
  resolve: {
      alias: createLodashAliases()
  }
  ...
};

Note: This loader has to run before babel-loader or ts-loader.

Important notes

This loader changes your code to explicitly reference single lodash functions instead of import the whole lodash library.

Example:

import * as _ from "lodash";

export class Main {

    public myMethod() {
        _.each([], (e) => {
            console.log(e);
        });

        _.isArray({});

        _.filter([], { name: "joe" });
    }
}

This is modified to:

import * as _each from "lodash/each";
import * as _isArray from "lodash/isArray";
import * as _filter from "lodash/filter";

export class Main {

    public myMethod() {
        _each([], (e) => {
            console.log(e);
        });

        _isArray({});

        _filter([], { name: "joe" });
    }
}

This works only if you import the lodash library in your code. Do NOT use lodash from browsers window variable. The import has to be a valid ES2015 or TypeScript-Import:

import _ from "lodash";
import * as _ from "lodash";

Function chaining is NOT supported at the moment. The same applies to lodash/fp functions.

Configuration

You can configure a query parameter called importMode to decide the destination import format:

Parameter Mandatory Data type Possible values Default value
importMode False String legacy,es2015,es2015-default,commonjs legacy

Comparison

This are analysis of a webpack build from a medium-sized web-project. There were 11 different functions in use.

Analyse Library
underscore Underscore 1.8.3 (51,7k)
lodash-unoptimized Lodash 4.17.4 (full) (526,9k)
lodash-optimized Lodash 4.17.4 (optimized) (140,8k)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.