webpack: 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;
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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 13
- Comments: 22 (10 by maintainers)
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.
so thanks.
@Legends with TS >= 2.7, if you enable
esModuleInterop
, then you can do:and it works.
I know that I can use
import {times} from 'lodash/times'
but I thought webpack can treeshake it when doingimport {times} from 'lodash'
. Could be a future feature…I cannot use
es imports
here, have to switch tovar add = require("lodash/add")
, because:import {add} from "lodash/add";