webpack - Webpack 工作,但 webpack-dev-server 失败
问题描述
我正在使用 webpack 打包一些前端代码。当我使用 webpack 打包时,一切正常,但是当我使用 webpack-dev-server 发布到本地服务器时,我得到了一些错误。
这是执行 webpack 的结果。 在此处输入图像描述
这是执行 webpack-dev-server 的结果。 在此处输入图像描述
webpack.config.js 的配置如下:
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
//
var WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev';
// 获取html-webpack-plugin参数的方法
var getHtmlConfig = function(name){
return {
template: './src/view/' + name + '.html',
filename: 'view/' + name + '.html',
inject: true,
hash: true,
chunks: ['common', name]
}
};
// webpack config
var config = {
// mode: 'dev' === WEBPACK_ENV ? 'development' : 'production',
mode: 'development',
entry: {
'common' : ['./src/page/common/index.js'],
'index' : ['./src/page/index/index.js'],
'login' : ['./src/page/login/index.js'],
},
devServer: {
contentBase: './dist/view'
},
output: {
// path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
filename: 'js/[name].js'
},
externals: {
'jquery' : 'window.jQuery'
},
module: {
rules: [
// css文件的处理
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
// 图片的配置
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 2048,
name: 'resource/[name].[ext]',
}
}
]
},
// 字体图标的配置
{
test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
name: 'resource/[name].[ext]'
}
}
]
}
]
},
resolve: {
alias: {
util : path.resolve(__dirname + '/src/util'),
page : path.resolve(__dirname + '/src/page'),
service : path.resolve(__dirname + '/src/service'),
image : path.resolve(__dirname + '/src/image'),
}
},
optimization: {
runtimeChunk: false,
splitChunks: {
cacheGroups: {
common: {
name: "common",
chunks: "all",
minChunks: 2
}
}
}
},
plugins: [
// new CleanWebpackPlugin(['dist']),
// 把CSS单独打包到文件里
new MiniCssExtractPlugin({
filename: "css/[name].css",
}),
// HTML模版的处理
new HtmlWebpackPlugin(getHtmlConfig('index')),
new HtmlWebpackPlugin(getHtmlConfig('login')),
],
devServer: {
port: 8088,
inline: true,
proxy: {
'**/*.do' : {
target: 'http://localhost:8088/',
changeOrigin: true
}
}
}
};
/*if('dev' === WEBPACK_ENV){
config.entry.common.push('webpack-dev-server/client?http://localhost:8088/');
}*/
module.exports = config;
webpack
的版本是 4.29.5 webpack-cli
的版本是 3.2.3 webpack-dev-server 的版本是 3.2.1
我不知道为什么会这样。有人能帮我吗?
解决方案
我有类似的问题并在这里找到了解决方案:github问题
文档说:“最好避免使用入口点作为缓存组的名称”,所以基本上你应该改变
optimization: {
runtimeChunk: false,
splitChunks: {
cacheGroups: {
common: {
name: "common",
chunks: "all",
minChunks: 2
}
}
}
},
至:
optimization: {
runtimeChunk: false,
splitChunks: {
cacheGroups: {
commonStyle: { // changed this name of group
name: "common", // to be different from word used here
chunks: "all",
minChunks: 2
}
}
}
},
希望这可以帮助
推荐阅读
- python - 无法在配置文件模板上显示登录用户配置文件详细信息?
- eclipse - 无法强制 gcc 链接器删除未使用的功能
- google-apps-script - 如何更改代码以应用于命名工作表选项卡,而不是 Google 电子表格中的第一个工作表选项卡
- spring - Spring Batch - 理解阅读器如何工作的问题
- python - 我怎样才能在python中实现这个?
- c# - Unity中如何知道电话是否在通话?
- java - 处理一个 JFrame
- python - 有没有更自动化的方法来发现子字符串?
- v8 - SetAccessor 和 SetAccessorProperty,有什么区别?
- python - 在这种情况下如何打印 valueError