- 35
- 0
问题描述
antd打包jsBundle过大
问题出现的环境背景及自己尝试过哪些方法
打包出来antd有600多KB,太大了,webpack要求小于244KB
相关代码
package.json文件
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --progress --config webpack.config.js",
"start": "webpack-dev-server --config webpack.config.js --progress"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.4",
"babel-minify-webpack-plugin": "^0.3.1",
"babel-plugin-import": "^1.11.0",
"clean-webpack-plugin": "^1.0.0",
"css-loader": "^2.1.0",
"file-loader": "^3.0.1",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.11.0",
"prop-types": "^15.6.2",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2",
"webpack": "^4.28.2",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.14"
},
"dependencies": {
"antd": "^3.12.3",
"axios": "^0.18.0",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-router-dom": "^4.3.1"
}
}
webpack配置文件
"use strict";
const path = require('path');
const HtmlPlugin = require('html-webpack-plugin');
const CleanPlugin = require('clean-webpack-plugin');
const MinifyJSPlugin = require("babel-minify-webpack-plugin");
//修改配置后需要重启
module.exports = {
//入口文件
entry: {
main: './src/index.js'
},
//出口文件
output: {
path: path.resolve(__dirname, './dist/'),
filename: "[name].js?hash=[hash]",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
include: path.resolve(__dirname, './src'),
exclude: path.resolve(__dirname, './node_modules'),
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/env', '@babel/react'],
plugins: [
"@babel/plugin-syntax-dynamic-import",
["import", {
//按需引入antd
libraryName: "antd",
style: "css" // `style: true` 会加载 less 文件
}]
]
}
}
},
//SCSS支持模块化加载,本地开发时使用,antd不可以使用模块化css
{
test: /\.(scss|css)$/,
exclude: /node_modules|antd\.css/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: true
}
},
{
loader: "sass-loader"
}
]
},
//针对antd不启用模块化css
{
test: /\.css$/,
include: /node_modules|antd\.css/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader"
}
]
},
//html文件压缩
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
minimize: true,
removeComments: true,
collapseWhitespace: true
}
}
],
},
//图片资源支持模块化加载
{
test: /\.(png|jpg|jpeg|gif)$/,
use: [
{
loader: "url-loader",
options: {
limit: 10000
}
},
{
loader: "image-webpack-loader",
options: {
//压缩jpeg
mozjpeg: {
progressive: true,
quality: 65
},
//压缩png图片
optipng: {
enabled: false,
},
pngquant: {
quality: '65-90',
speed: 4
},
//压缩gif图片
gifsicle: {
interlaced: false,
},
// //将JPG和PNG转换为webp
// webp: {
// quality: 75
// }
}
}
]
},
//字体文件加载
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [
{
loader: "url-loader",
options: {
limit: 10000
}
}
]
}
]
},
//打包模式
mode: "production",
//开发服务器
devServer: {
contentBase: './src',
port: 4000,
open: true,
proxy: {}
},
//插件
plugins: [
new HtmlPlugin({
template: './src/index.html'
}),
new CleanPlugin('./dist'),
new MinifyJSPlugin({
removeConsole: true,
removeDebugger: true
}, {
comments: false
})
],
//别名
resolve: {
//以下后缀名文件import时不需要添加后缀名, js必写
extensions: ['.jsx', '.json', '.js'],
},
//分包处理
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/](react|react-dom|react-router-dom|axios)/,
name: 'vendors',
chunks: 'all'
},
}
}
}
};
你期待的结果是什么?实际看到的错误信息又是什么?
打包出来的antd包小于244KB
- 共 0 条
- 全部回答
-
回不去的曾经 普通会员 1楼
antd是一个React库,使用Webpack进行打包。以下是一个基本的步骤:
- 首先,你需要在你的项目根目录下创建一个名为
webpack.config.js的文件,然后在该文件中添加以下配置:
javascript module.exports = { // 设置编译器 entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist') }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-react'] } } } ] } };在这个配置中,我们首先设置了入口文件,然后设置输出文件的路径。然后,我们配置了模块规则,即只编译JavaScript文件。
-
然后,你需要在你的
package.json文件中添加"dependencies": { "antd": "^3.21.0" }这一行,以便Ant Design库能够通过npm来安装。 -
最后,你需要在你的项目根目录下运行以下命令来打包:
bash npm run build这将会在
dist目录下生成一个名为bundle.js的文件,这个文件包含了你的React应用的所有代码。注意,这个过程只是一个基本的示例,你可能需要根据你的具体需求来调整配置。例如,你可能需要添加额外的配置来处理CSS、图片或其他资源,或者你可能需要添加错误处理和加载图片的代码。
- 首先,你需要在你的项目根目录下创建一个名为
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
