1 回答

TA貢獻1818條經驗 獲得超3個贊
您需要為 devServer 使用 watchContentBase 選項:
watchContentBase:true
還建議為模塊替換設置 hot:true 和 open:true - 這樣當您運行開發服務器時,它會自動在默認瀏覽器中打開您的站點。
編輯
經過長時間的聊天,結果如下:
仍然“實時重新加載”您應該使用的頁面
watchContentBase
但在這種情況下還有其他問題 - devServer 中的 publicPath 和 outputPath 不一樣,然后index.html應該引用/public/scripts下的bundle.js
新的 webpack.config.js:
const path = require('path')
? ??
? ? module.exports = {
? ? ? ? entry: './src/index.js',
? ? ? ? output: {
? ? ? ? ? ? path: path.resolve(__dirname, '/public/scripts'),
? ? ? ? ? ? publicPath: '/public/scripts',
? ? ? ? ? ? filename: 'bundle.js'
? ? ? ? },
? ? ? ? module: {
? ? ? ? ? ? rules: [{
? ? ? ? ? ? ? ? test: /\.js$/,
? ? ? ? ? ? ? ? exclude: /node_modules/,
? ? ? ? ? ? ? ? use: {
? ? ? ? ? ? ? ? ? ? loader: 'babel-loader',
? ? ? ? ? ? ? ? ? ? options: {
? ? ? ? ? ? ? ? ? ? ? ? presets: ['env']
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }]
? ? ? ? },
? ? ? ? devServer: {
? ? ? ? ? ? contentBase: path.resolve(__dirname, 'public'),
? ? ? ? ? ? watchContentBase : true,?
? ? ? ? ? ? publicPath: '/public/scripts'
? ? ? ? }
? ? }
Index.html 中捆綁包的新 src: /public/scripts/bundle.js
添加回答
舉報