【发布时间】:2021-07-20 22:16:12
【问题描述】:
我正在开发这个 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 中没有看到可以将其覆盖为生产的命令
-
会是这个问题吗?
-
当然,让我发布一个答案
-
非常感谢!
标签: javascript reactjs typescript webpack production