webpack 开发服务器:收到 "Hot Module Replacement enabled" 但我的页面没有改变
webpack dev server: received "Hot Module Replacement enabled" but my page does not change
我正在尝试使用 webpack-dev-server 来 运行 一个简单的网页。我可以在 http://localhost:8080/ 中看到我的页面,但无论我做什么设置都无法正确地 运行 热重载,当我单击 ctrl s
时,我的 console.log 会立即显示如下内容:
[WDS] Live Reloading enabled.
我的命令显示:
「wdm」: Compiled successfully.
「wdm」: Compiling...
但是在这些之后,我的页面仍然没有变化。我需要 运行 npm run build
看看我的代码是如何工作的。
我的项目结构是这样的:
folder
-index.html
-package.json
-webpack.config.js
-src
---router
-----router.js
---views
-----header.vue
-----footer.vue
-----cat1.vue
这是我的代码。
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue.js v2</title>
</head>
<body>
<div id="app">
<isheader></isheader>
<router-view></router-view>
<isfooter></isfooter>
</div>
<script src="http://localhost:8080/webpack-dev-server.js"></script>
</body>
</html>
package.json
{
"name": "webpack0409",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"serve": "webpack-dev-server --devtool eval --progress --colors --hot",
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^8.1.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^3.5.1",
"file-loader": "^6.0.0",
"style-loader": "^1.1.3",
"url-loader": "^4.1.0",
"vue": "^2.6.11",
"vue-loader": "^15.9.1",
"vue-router": "^3.1.6",
"vue-template-compiler": "^2.6.11",
"webpack": "^4.42.1"
},
"devDependencies": {
"html-webpack-plugin": "^4.0.4",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
}
}
webpack.config.js
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const htmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
var path = require('path')
var config = {
mode: "development",
entry: [
'webpack/hot/dev-server',
path.join(__dirname, 'src/router', 'router')
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: './'
},
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
hot: true,
historyApiFallback: true
},
module: {
rules: [
{ test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.vue$/, loader: 'vue-loader' },
{ test: /\.css$/, loader: ["style-loader", "css-loader"] },
{
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'file-loader',
options: { name: 'image/[name].[ext]' }
},
],
},
resolve: {
extensions: ['.js', '.vue'],
alias: {
vue: 'vue/dist/vue.js',
node_modules: __dirname + '/node_modules'
}
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new VueLoaderPlugin(),
new htmlWebpackPlugin({
template: './index.html'
}),
]
}
module.exports = config
header
<template>
<div>
<div style="background: #44bb55;">
<router-link to="/">Home</router-link>
<p>header</p>
</div>
</div>
</template>
header、footer、cat1,这3个文件的代码很相似,只是css风格不同。
router-link 可以正常工作,只是热重载不行。
我做的有什么问题吗?
提前致谢。
感谢阅读。我发现有人有类似的问题:
以为我不太了解它是如何工作的,我在 webpack.config.js
中删除了 publicPath
现在看起来像这样:
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
现在我的页面可以正常刷新了。
我正在尝试使用 webpack-dev-server 来 运行 一个简单的网页。我可以在 http://localhost:8080/ 中看到我的页面,但无论我做什么设置都无法正确地 运行 热重载,当我单击 ctrl s
时,我的 console.log 会立即显示如下内容:
[WDS] Live Reloading enabled.
我的命令显示:
「wdm」: Compiled successfully.
「wdm」: Compiling...
但是在这些之后,我的页面仍然没有变化。我需要 运行 npm run build
看看我的代码是如何工作的。
我的项目结构是这样的:
folder
-index.html
-package.json
-webpack.config.js
-src
---router
-----router.js
---views
-----header.vue
-----footer.vue
-----cat1.vue
这是我的代码。
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue.js v2</title>
</head>
<body>
<div id="app">
<isheader></isheader>
<router-view></router-view>
<isfooter></isfooter>
</div>
<script src="http://localhost:8080/webpack-dev-server.js"></script>
</body>
</html>
package.json
{
"name": "webpack0409",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack",
"serve": "webpack-dev-server --devtool eval --progress --colors --hot",
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^8.1.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^3.5.1",
"file-loader": "^6.0.0",
"style-loader": "^1.1.3",
"url-loader": "^4.1.0",
"vue": "^2.6.11",
"vue-loader": "^15.9.1",
"vue-router": "^3.1.6",
"vue-template-compiler": "^2.6.11",
"webpack": "^4.42.1"
},
"devDependencies": {
"html-webpack-plugin": "^4.0.4",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
}
}
webpack.config.js
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const htmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
var path = require('path')
var config = {
mode: "development",
entry: [
'webpack/hot/dev-server',
path.join(__dirname, 'src/router', 'router')
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: './'
},
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
hot: true,
historyApiFallback: true
},
module: {
rules: [
{ test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.vue$/, loader: 'vue-loader' },
{ test: /\.css$/, loader: ["style-loader", "css-loader"] },
{
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'file-loader',
options: { name: 'image/[name].[ext]' }
},
],
},
resolve: {
extensions: ['.js', '.vue'],
alias: {
vue: 'vue/dist/vue.js',
node_modules: __dirname + '/node_modules'
}
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new VueLoaderPlugin(),
new htmlWebpackPlugin({
template: './index.html'
}),
]
}
module.exports = config
header
<template>
<div>
<div style="background: #44bb55;">
<router-link to="/">Home</router-link>
<p>header</p>
</div>
</div>
</template>
header、footer、cat1,这3个文件的代码很相似,只是css风格不同。 router-link 可以正常工作,只是热重载不行。
我做的有什么问题吗? 提前致谢。
感谢阅读。我发现有人有类似的问题:
以为我不太了解它是如何工作的,我在 webpack.config.js
中删除了 publicPath现在看起来像这样:
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
现在我的页面可以正常刷新了。