我一直在遵循学习wepback及其捆绑功能的指南,并想找到一种方法,当对代码进行更改后发现自动更新浏览器,并且发现--watch是这样做的一种方法。 / p>
在webpack.config.json文件或package.json文件中没有任何监视的情况下,站点已正确编译并显示“正在侦听端口8080”
当我在package.json构建脚本中添加--watch并在webpack.config.json文件中添加watch / watchOptions时,出现了问题,它只是挂起并且不打印“在8080端口上监听”
这是我的代码:
webpack.config.json
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const outputDirectory = 'dist';
console.log(__dirname);
module.exports = {
entry: {
'bundle.js': [
path.resolve(__dirname, 'src/client/index.js'),
]
},
output: {
filename: '[name]',
path: path.resolve(__dirname, 'dist'),
},
// entry: ['babel-polyfill', './src/client/index.js'],
// output: {
// path: path.join(__dirname, outputDirectory),
// filename: 'bundle.js'
// },
module: {
rules: [{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000'
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
devServer: {
port: 3000,
open: true,
inline: true,
proxy: {
'/api': 'http://localhost:8080'
}
},
watch: true,
watchOptions: {
poll: true,
ignored: /node_modules/
},
plugins: [
new CleanWebpackPlugin([outputDirectory]),
new HtmlWebpackPlugin({
template: './public/index.html',
favicon: './public/favicon.ico'
})
]
};
package.json
{
"name": "simple-react-full-stack",
"version": "1.0.0",
"description": "Boilerplate to build a full stack web application using React, Node.js, Express and Webpack.",
"main": "src/server/index.js",
"scripts": {
"build": "webpack --mode production --watch --colors",
"start": "npm run build && node src/server/index.js",
"client": "webpack-dev-server --mode development --devtool inline-source-map --hot",
"server": "nodemon src/server/index.js",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"author": "Sandeep Raveesh",
"license": "ISC",
"dependencies": {
"babel-polyfill": "^6.26.0",
"express": "^4.16.4",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/preset-env": "^7.4.3",
"@babel/preset-react": "^7.0.0",
"babel-eslint": "^10.0.0",
"babel-loader": "^8.0.5",
"clean-webpack-plugin": "^1.0.1",
"concurrently": "^4.1.0",
"css-loader": "^2.1.1",
"eslint": "^5.0.0",
"eslint-config-airbnb": "^17.0.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"file-loader": "^3.0.0",
"html-webpack-plugin": "^3.2.0",
"nodemon": "^1.17.3",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2",
"webpack": "^4.30.0",
"webpack-cli": "^3.3.1",
"webpack-dev-server": "^3.1.3"
}
这是执行npm start后命令行显示给我的图像 git command prompt
非常感谢您的帮助,这是我唯一遇到的困难,之后我对Node.js和React.js十分满意:)
答案 0 :(得分:0)
"webpack-cli": "3.3.1",
"webpack-dev-server": "3.1.3"
尝试删除上限符号并进行npm安装
您需要使用--hot标志运行webpack-dev-server:
webpack-dev-server --content-base ./ --port 9966 --hot
然后您可以访问热加载版本localhost:9966 / webpack-dev-server /
您也不需要进行监视。
更新: 您的webpack配置中的此项必须更改:
条目:['./js/main.js'],->条目:['webpack / hot / dev-server','./js/main.js']
更改您的publicPath条目:
publicPath:'/ assets /'
答案 1 :(得分:0)