Lodash not TreeShaking with Webpack with Webpack 4?

前端 未结 2 1325
你的背包
你的背包 2021-02-14 18:10

I want to tree shake lodash as well as my unused multiply function from the generated bundle from webpack

I have 2 main files

2条回答
  •  逝去的感伤
    2021-02-14 18:41

    Building off of @deadcoder0904's answer, here's how to do the same with babel-loader in webpack 4 (instead of using .babelrc):

    ...
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        plugins: ['lodash'],
                        presets: [['env', { modules: false }]]
                    }
                }
            },
    

    Note: I wasn't able to get this to work without explicitly importing from 'lodash-es' (even if I pointed lodash-es to lodash in my tsconfig (I'm using typescript). If someone can get this working without having to use the special import { map } from 'lodash-es'; and instead with import { map } from 'lodash'; it would be great to know how!

提交回复
热议问题