Treeshaking doesn’t take place – lodash

Do you want to request a feature or report a bug?
Bug?

What is the current behavior?
I am loading a single export from lodash, but get the complete lodash in output-bundle.js.

If the current behavior is a bug, please provide the steps to reproduce.

page1.ts

import {add} from "lodash"
alert(add(5, 5));

Webpack.config.ts

// import { Options, Configuration, optimize, RulesRule } from "webpack";
import * as webpack from "webpack";
const path = require('path');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const cleanPlugin = require("clean-webpack-plugin");

let config: webpack.Configuration = {
  mode: "production",
  devtool: false,
  entry: {
    page1: "./src/pages/page1",
    page2: "./src/pages/page2"
  },
  optimization: {
    sideEffects: false
  },
  output: {
    filename: '[name].js',
    chunkFilename: '[name].js',
    path: path.resolve(__dirname, 'dist')
  },
  resolve: {
    extensions: [".ts", ".js", ".txt", ".json", ".css", ".less", ".scss", ".saas"],
    alias: {
      "#comp": path.resolve("./src/components/")
    }
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: "ts-loader"
      },
      {
        test: /\.(less|css)$/,
        use: [{
          loader: 'css-loader',
          options: {
            sourceMap: true
          }
        }, {
          loader: 'less-loader',
          options: {
            sourceMap: true
          }
        }]
      }]
  },

  plugins: [new cleanPlugin("dist"),
  new UglifyJSPlugin({
    uglifyOptions: {
      warnings: true,
      mangle: false,
      output: {
        beautify: true,
        comments: true
      }
    }
  })
  ]
}

module.exports = config;

image

What is the expected behavior?
Treeshaked output.
If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.
Win10 x64
webpack 4.4.1
node 8.10

Author: Fantashit

3 thoughts on “Treeshaking doesn’t take place – lodash

  1. I know that I can use import {times} from 'lodash/times' but I thought webpack can treeshake it when doing import {times} from 'lodash'. Could be a future feature…

    I cannot use es imports here, have to switch to var add = require("lodash/add") ,
    because:

    import {add} from "lodash/add";

    @types/lodash/add”‘ resolves to a non-module entity and cannot be imported using this construct

  2. Sorry, I run the project on my cloud server, everything is ok, I will find the reason why my macbook doesn’t…

    Reason

    I find the reason.
    My mistake, I used other project .gitignore, it’s hide my .babelrc file.
    When I use babel-presets-env whithout options, the sideEffects doesn’t work.
    I change the config below will be ok.

    {
      "presets": [
        ["env", {
          "modules": false
        }]
      ]
    }

    so thanks.

Comments are closed.