我正在自学Webpack,并尝试使用webpack-dev-server
实现当我更改应用程序文件中的.js时,自动更新浏览器并显示更改内容。假设我有以下package.json
{
"name": "webpack-babel",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"serve": "webpack-dev-server --hot",
"start": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-preset-es2015": "^6.22.0",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
}
}
这是我的webpack.config.json文件。
const path = require('path');
const config = {
entry: './app/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
],
}
};
module.exports = config;
我在./app/index.js中有以下代码(没有什么太复杂的):
let message = 'I love socks and snacks !';
console.log(message);
当我运行
npm run serve
命令后,将app/index.js
中的信息更改为'我爱可乐和零食!!!'
并保存,终端会编译但浏览器中没有更新。打包文件仍然包含旧信息并未生成。我做错了什么?我只是几个小时前开始尝试使用webpack,因此对它还不太熟悉。我已在IDE中关闭了“安全写入”,我的文件结构如下: 这是我的index.html...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="dist/bundle.js"></script>
</body>
* 更新 * 在 output
对象中添加 'dist/
' 作为 publicPath
似乎可以解决我的问题... 但我不确定这是否是正确的解决方案。