我正在努力提高我们网站的加载速度。 所以我一直在把手放在我们的webpack.config中。
经过几次修改后,我现在将模块分开并正确散列。 我还使用HtmlWebpackPlugin自动生成索引.html从out模板。
但是我也出现了其他几个问题。
- 供应商.js不会自动添加到模板中。只有 chunk-manifest.json 和索引。[哈希].js是自动添加的。如果我尝试像那样直接加载,我会得到一个
未捕获的引用错误:未定义 webpackJsonp
- 添加供应商后.js手动出现以下错误:
未捕获的引用错误:再生器运行时未定义
这是 webpack.config:
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');
const WebpackChunkHash = require('webpack-chunk-hash');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
/* Shared Dev & Production */
const config = {
context: path.resolve(__dirname, 'src'),
entry: {
index: './index.js',
vendor: ['react', 'react-dom', 'react-router', 'react-redux', 'history', 'react-router-dom', 'redux', 'react-router-redux', 'redux-form', 'lodash'],
},
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /.(ico|jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(?.*)?$/,
exclude: //favicon.ico$/,
use: [
{
loader: 'file-loader',
query: {
name: '[path][name][hash].[ext]',
publicPath: '/'
}
}
]
},
{
test: /.css$/,
include: path.join(__dirname, 'src/style'),
use: [
'style-loader',
'css-loader'
]
},
{
test: /.(ico)(?.*)?$/,
exclude: /node_modules/,
use: {
loader: 'file-loader',
options: { name: '[name].[ext]' },
},
},
{
test: /.xml/,
use: {
loader: 'file-loader',
options: { name: '[name].[ext]' },
},
},
],
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js',
publicPath: '/',
},
resolve: {
extensions: ['.js'],
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
plugins: [
// New moment.js optimization
new webpack.ContextReplacementPlugin(/moment[/\]locale$/, /en/),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor'],
filename: '[name].js',
minChunks: Infinity
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new HtmlWebpackPlugin({
appMountId: 'app-root',
inlineManifestWebpackName: 'webpackManifest',
template: 'templateIndex.html',
title: 'Our Site',
}),
],
devServer: {
historyApiFallback: true,
},
};
//if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
config.plugins = [
...config.plugins,
new BundleAnalyzerPlugin({analyzerMode: 'static'}),
];
//}
//if (process.env.NODE_ENV === 'production') {
config.output.filename = '[name].[chunkhash].js';
config.plugins = [
...config.plugins,
new webpack.HashedModuleIdsPlugin(),
new WebpackChunkHash(),
new ChunkManifestPlugin({
filename: 'chunk-manifest.json',
manifestVariable: 'webpackManifest',
inlineManifest: true,
}),
];
//}
module.exports = config;
在这一点上,我不确定 webpack.config 出了什么问题。
如何自动添加供应商.js?
这是模板:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title itemprop="name">Our Site</title>
<base href="/">
<link rel="apple-touch-icon" sizes="57x57" href="/images/favicon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/images/favicon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/images/favicon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/images/favicon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/images/favicon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/images/favicon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/images/favicon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/images/favicon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/images/favicon-180x180.png">
<link rel="icon" type="image/png" href="/images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
<link rel="icon" type="image/png" href="/images/favicon-160x160.png" sizes="160x160">
<link rel="icon" type="image/png" href="/images/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/images/favicon-16x16.png" sizes="16x16">
<link type="image/png" href="/images/favicon.png" rel="icon">
<link rel="icon" href="/images/favicon.ico" />
<link rel="shortcut icon" href="/images/favicon.png" />
<link rel="manifest" href="/manifest.webmanifest">
<noscript>Your browser does not support JavaScript!</noscript>
</head>
<body>
<div id="app"></div>
</body>
</html>
这是 .babelrc(我认为再生运行时与它有关,只需要弄清楚如何)
{
"presets": [
[
"env",
{ "modules": false }
],
"react",
"stage-0",
"stage-2"
],
"plugins": [ "syntax-dynamic-import", "transform-react-jsx", "transform-decorators-legacy" ]
}
从我在网上读到的内容来看,再生器错误是由于包装冲突造成的。但我看不出这是从哪里来的。
根据我的经验:
Uncaught ReferenceError: webpackJsonp is not defined
通常是缺少由CommonsChunkPlugin
提取的 Webpack 代码的结果。您可能需要验证此文件是否也正确注入。
溶液
我能够通过删除ChunkManifestPlugin
来使其工作。它和CommonsChunkPlugin
之间似乎有冲突.我自己过去没有经历过的事情,但看看我在下面做了什么,这可以解决问题:
注意:您还需要包含babel-polyfill
以使再生运行时在浏览器中工作。
第一:
通天塔 6
npm install --save babel-polyfill
通天塔 7
npm install --save @babel/polyfill
然后:
差异:
8a9,11
> // To handle the regeneratorRuntime exception
> require('babel-polyfill'); // or @babel/polyfill if Babel 7
>
15c18,22
< index: './index.js',
---
> index: [
> // To handle the regeneratorRuntime exception
> 'babel-polyfill', // or @babel/polyfill if Babel 7
> './index.js'
> ],
82d88
< filename: '[name].js',
114c120
< new ChunkManifestPlugin({
---
> /*new ChunkManifestPlugin({
118c124
< }),
---
> }),*/
完整的 webpack.config.js
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');
const WebpackChunkHash = require('webpack-chunk-hash');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// To handle the regeneratorRuntime exception
require('babel-polyfill'); // or @babel/polyfill if Babel 7
/* Shared Dev & Production */
const config = {
context: path.resolve(__dirname, 'src'),
entry: {
index: [
// To handle the regeneratorRuntime exception
'babel-polyfill', // or @babel/polyfill if Babel 7
'./index.js'
],
vendor: ['react', 'react-dom', 'react-router', 'react-redux', 'history', 'react-router-dom', 'redux', 'react-router-redux', 'redux-form', 'lodash'],
},
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /.(ico|jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(?.*)?$/,
exclude: //favicon.ico$/,
use: [
{
loader: 'file-loader',
query: {
name: '[path][name][hash].[ext]',
publicPath: '/'
}
}
]
},
{
test: /.css$/,
include: path.join(__dirname, 'src/style'),
use: [
'style-loader',
'css-loader'
]
},
{
test: /.(ico)(?.*)?$/,
exclude: /node_modules/,
use: {
loader: 'file-loader',
options: {name: '[name].[ext]'},
},
},
{
test: /.xml/,
use: {
loader: 'file-loader',
options: {name: '[name].[ext]'},
},
},
],
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js',
publicPath: '/',
},
resolve: {
extensions: ['.js'],
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
plugins: [
// New moment.js optimization
new webpack.ContextReplacementPlugin(/moment[/\]locale$/, /en/),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor'],
minChunks: Infinity
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new HtmlWebpackPlugin({
appMountId: 'app-root',
inlineManifestWebpackName: 'webpackManifest',
template: 'templateIndex.html',
title: 'Our Site',
}),
],
devServer: {
historyApiFallback: true,
},
};
//if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
config.plugins = [
...config.plugins,
new BundleAnalyzerPlugin({analyzerMode: 'static'}),
];
//}
//if (process.env.NODE_ENV === 'production') {
config.output.filename = '[name].[chunkhash].js';
config.plugins = [
...config.plugins,
new webpack.HashedModuleIdsPlugin(),
new WebpackChunkHash(),
/*new ChunkManifestPlugin({
filename: 'chunk-manifest.json',
manifestVariable: 'webpackManifest',
inlineManifest: true,
}),*/
];
//}
module.exports = config;