webpack 生产构建 bundle.js 文件大小为 10mb
webpack production build bundle.js file size is 10mb
我正在开发这个 React 应用程序,当我构建项目时,bundle.js
文件是 10mb,因此在部署之后加载内容需要时间。
代码如下:https://github.com/sef-global/scholarx-frontend
这是我的 webpack 配置文件:
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const webpack = require('webpack');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.tsx',
mode: 'development',
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
options: { presets: ['@babel/env'] },
},
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { modules: true } },
],
},
{
test: /\.less$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'less-loader',
options: {
lessOptions: { javascriptEnabled: true },
},
},
],
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [{ loader: 'file-loader' }],
},
],
},
resolve: { extensions: ['*', '.js', '.jsx', '.ts', '.tsx'] },
output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: '/dist/',
filename: 'bundle.js',
},
devServer: {
contentBase: path.join(__dirname, 'public/'),
port: 3000,
historyApiFallback: true,
open: true,
hotOnly: true,
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: 'public/index.html',
favicon: 'public/favicon.png',
}),
],
};
我假设您使用来自 packages.json
的“构建”命令进行生产构建,其中指出:
"build": "webpack",
这当然会触发 webpack
“构建”,但是在您的 webpack 配置中 mode
设置为 development
- 因此它将在开发模式下构建。
你要做的是:
"build": "webpack --mode production",
--mode
参数将覆盖您在 webpack.config.
中的内容
我正在开发这个 React 应用程序,当我构建项目时,bundle.js
文件是 10mb,因此在部署之后加载内容需要时间。
代码如下:https://github.com/sef-global/scholarx-frontend
这是我的 webpack 配置文件:
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const webpack = require('webpack');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.tsx',
mode: 'development',
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
options: { presets: ['@babel/env'] },
},
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { modules: true } },
],
},
{
test: /\.less$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'less-loader',
options: {
lessOptions: { javascriptEnabled: true },
},
},
],
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [{ loader: 'file-loader' }],
},
],
},
resolve: { extensions: ['*', '.js', '.jsx', '.ts', '.tsx'] },
output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: '/dist/',
filename: 'bundle.js',
},
devServer: {
contentBase: path.join(__dirname, 'public/'),
port: 3000,
historyApiFallback: true,
open: true,
hotOnly: true,
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
template: 'public/index.html',
favicon: 'public/favicon.png',
}),
],
};
我假设您使用来自 packages.json
的“构建”命令进行生产构建,其中指出:
"build": "webpack",
这当然会触发 webpack
“构建”,但是在您的 webpack 配置中 mode
设置为 development
- 因此它将在开发模式下构建。
你要做的是:
"build": "webpack --mode production",
--mode
参数将覆盖您在 webpack.config.