我想从生成的捆绑包中摇树lodash
以及我未使用的函数multiply
webpack
我有 2 个主要文件app.js
&math.js
它包含以下代码 -
应用程序.js
import map from "lodash/map";
import { sum } from "./math";
console.log("");
console.log(`2 + 3 = ${sum(2, 3)}`);
map([1, 2, 3], x => {
console.log(x);
});
数学.js
export const sum = (a, b) => a + b;
export const multiply = (m, n) => m * n;
webpack.config.js
const path = require("path");
const webpack = require("webpack");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const Jarvis = require("webpack-jarvis");
let plugins = [new Jarvis()];
if (process.env.NODE_ENV === "production") plugins.push(new UglifyJSPlugin());
const config = {
entry: "./app.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bundle.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: "babel-loader"
}
]
},
plugins
};
module.exports = config;
我的npm
脚本看起来像 -
"scripts": {
"dev": "webpack --optimize-minimize --mode development",
"dev:watch": "webpack --watch --optimize-minimize --mode development",
"prod": "webpack -p --optimize-minimize --mode production",
"prod:watch": "webpack -p --watch --optimize-minimize --mode production",
"start": "npm run prod",
"clean": "rm -rf dist"
}
完整的代码在https://github.com/deadcoder0904/webpack-treeshake
我试过使用 UglifyJSPlugin 但在生成的包中它仍然显示我未使用的multiply
函数math.js
。此外,从遗骸production
生成的捆绑包很多,我也看到了很多东西。npm run prod
20kB
lodash