微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何修复 webpack 上的热重载

如何解决我如何修复 webpack 上的热重载

所以,我让 webpack 与“npx webpack 服务器”一起工作,它可以编译任何更改……但是当我进行更改时,我仍然需要刷新浏览器(例如,在 options.mjs 中,我更改了 lala( ) 显示“你好”并保存,但在我按下按钮后浏览器中没有任何显示,直到我刷新并再次按下。

publish.js(连接到 webpack 的页面

import * as fnls_options from './options.mjs';

if (import.Meta.webpackHot) {
    import.Meta.webpackHot.accept('./options.mjs',() => {console.log('ok')});
};
//https://stackoverflow.com/questions/66085425/webpack-5-module-hot-is-undefined

options.mjs(连接到publish.js的模块)

export const divTop = document.getElementById('top');

export function createRevert(input,inputName){
    
    var butn=document.createElement("BUTTON");
    var br=document.createElement("BR"); //<--create a line break
    butn.setAttribute("id",input);
    butn.classList.add(`${inputName}`);
    butn.setAttribute('type','button');
    var butnType = document.createTextNode(input);
    butn.appendChild(butnType);
    divTop.appendChild(butn);
    divTop.appendChild(br);
}

export function lala(){
    console.log('hi')
}

webpack.config.cjs 文件

const path = require('path')
const webpack = require('webpack')

module.exports = {
    mode: 'development',//<---production will minify the file making it as small as possible for production
    entry: './homepage/static/js/publish.js',output: {
        filename: 'abda_bundle.js',path: path.resolve('/home/abd_p1/homepage','bunds')
            },//watch: true,<---dont need since theres a dev server

    devtool: 'source-map',devServer: {
        host: '0.0.0.0',port: 808,contentBase: '/home/abd_p1/homepage/static/js',hot:true,//enables hot module replacement
        inline: true,proxy: {
            '/api': {
                target: 'http://localhost:8001/cars/',pathRewrite: { '^/api': '' },secure: false,changeOrigin: true
          }
        }
      },plugins: [new webpack.HotModuleReplacementPlugin()]

}

package.json:

  },"devDependencies": {
    "webpack": "^5.46.0","webpack-cli": "^4.7.2","webpack-dev-middleware": "^5.0.0","webpack-dev-server": "^3.11.2","webpack-hot-middleware": "^2.25.0"
  }

'npx webpack server' 输出

(venv) root@abda:/home/abd_p1# npx webpack server
ℹ 「wds」: Project is running at http://0.0.0.0:808/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from /home/abd_p1/homepage/static/js
ℹ 「wdm」: asset abda_bundle.js 400 KiB [emitted] (name: main) 1 related asset
runtime modules 26.8 KiB 12 modules
modules by path ./node_modules/ 339 KiB
  modules by path ./node_modules/webpack-dev-server/ 21.2 KiB
    modules by path ./node_modules/webpack-dev-server/client/ 20.9 KiB 10 modules
    modules by path ./node_modules/webpack-dev-server/node_modules/ 296 bytes 2 modules
  modules by path ./node_modules/webpack/hot/ 4.46 KiB 5 modules
  modules by path ./node_modules/html-entities/lib/*.js 61 KiB 5 modules
  modules by path ./node_modules/url/ 37.4 KiB 3 modules
  modules by path ./node_modules/querystring/*.js 4.51 KiB 3 modules
  4 modules
modules by path ./homepage/static/js/ 5.7 KiB
  ./homepage/static/js/publish.js 5.06 KiB [built] [code generated]
  ./homepage/static/js/options.mjs 654 bytes [built] [code generated]
webpack 5.46.0 compiled successfully in 391 ms
ℹ 「wdm」: Compiled successfully.
ℹ 「wdm」: Compiling...
ℹ 「wdm」: asset abda_bundle.js 400 KiB [emitted] (name: main) 1 related asset
asset main.0fffa7602b5d577ef82c.hot-update.js 1.6 KiB [emitted] [immutable] [hmr] (name: main) 1 related asset
asset main.0fffa7602b5d577ef82c.hot-update.json 28 bytes [emitted] [immutable] [hmr]
Entrypoint main 401 KiB (452 KiB) = abda_bundle.js 400 KiB main.0fffa7602b5d577ef82c.hot-update.js 1.6 KiB 2 auxiliary assets
cached modules 344 KiB [cached] 33 modules
runtime modules 26.8 KiB 12 modules
./homepage/static/js/options.mjs 655 bytes [built] [code generated]
webpack 5.46.0 compiled successfully in 98 ms
ℹ 「wdm」: Compiled successfully.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。