亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

webpack-dev-server 不重新編譯輸出文件

webpack-dev-server 不重新編譯輸出文件

肥皂起泡泡 2023-06-29 15:44:36
這是我的 webpack 文件,沒什么特別的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: '/scripts/'    }}但是,當我運行“npm run webpack-dev-server”時,我得到正常的 node.js 輸出,但在進行新更改時網站不會更新。我刪除了bundle.js 文件,當我再次運行它時,出現錯誤“找不到bundle.js”。我發現運行此命令時,bundle.js 根本沒有被重新編譯。如果這有什么區別的話,我在Windows上。任何幫助,將不勝感激。編輯:下面是我的文件夾結構。
查看完整描述

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


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號