【发布时间】:2018-09-05 07:49:30
【问题描述】:
我创建了一个非常基本的应用程序,它只从 url 加载 json 然后显示它。 没有要拆分的块,事情被缩小了,但我仍然收到消息:
“资产大小限制警告:以下资产超出建议的大小限制 (244 KiB)。这可能会影响 Web 性能。资产:output_react.js (245 KiB)”。
模式生产开启(脚本被缩小,在开发过程中超过1mb),应用程序基本上没有做任何事情并且webpack已经报告它太大了?为什么?我该如何解决这个问题? (style.scss 也是空的,只有 150 个字符)。
我的代码: 应用.js
import React, {Component} from 'react';
require('../styles/style.scss');
class App extends Component {
constructor(){
super();
this.state = {
orders : []
}
this.getOrders();
}
getOrders (){
fetch('http://localhost/supplier/get_orders.php')
.then(results =>{
return results.json();
}).then(data =>{
let orders = data.orders.map((order) => {
return(
<div key={order.order_index} className={order.status}>
{order.sizes}
</div>
)
})
this.setState({orders: orders})
})
}
render() {
return <div className="orders_container">{this.state.orders}</div> ;
}
}
export default App;
我的 webpack.config.js:
const webpack = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, '/dist'),
filename: 'output_react.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: [/node_modules/, /git/],
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/,
exclude: /git/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
},
{
test: /\.css$/,
exclude: /git/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader" // translates CSS into CommonJS
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
],
watch: true
}
【问题讨论】:
-
使用此命令生成您的构建
webpack --profile --json > stats.json的配置文件。然后使用webpack-bundle-analyzer打开并查看配置文件(stats.json)。您也可以上传此文件并在此处共享,但您可能希望删除您不想共享的任何代码,因为此文件将包含代码。
标签: javascript reactjs webpack size bundle