webpack热重载毫无任何反应

发布于 2022-09-11 19:25:25 字数 4013 浏览 24 评论 0

问题分析:页面正常加载,npm run dev也无任何报错,控制台HMR完全没有,我记得websocket应该有个sockjs.node用来热更新的也没有。。。什么都没有

项目git地址 :git@github.com:liangyuqi/awesome-image.git

部分关键代码

  1. package.json
{
  "name": "awesome-image",
  "version": "1.0.0",
  "description": "init",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --config ./webpack-dev.config.js --content-base ./example --color  --environment NODE_ENV:development",
    "dev:liangyuqi": "webpack-dev-server --config ./webpack-dev.config.js --host 10.232.8.48 --port 8086 --progress",
    "build": "webpack --config ./webpack-build.config.js  --environment NODE_ENV:production"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/jest": "^23.3.13",
    "@types/node": "^10.12.20",
    "@types/tapable": "^1.0.4",
    "@types/webpack": "^4.4.24",
    "awesome-typescript-loader": "^5.2.1",
    "html-webpack-plugin": "^3.2.0",
    "immutable": "^4.0.0-rc.12",
    "ts-loader": "^5.3.3",
    "tslint": "^5.12.1",
    "typescript": "^3.2.4",
    "webpack": "^4.29.0",
    "webpack-cli": "^3.2.1",
    "webpack-dev-server": "^3.1.14"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/liangyuqi/awesome-image.git"
  },
  "bugs": {
    "url": "https://github.com/liangyuqi/awesome-image/issues"
  },
  "homepage": "https://github.com/liangyuqi/awesome-image#readme",
  "dependencies": {
    "@types/lodash": "^4.14.120"
  }
}
  1. webpack-dev.config.js
const path = require('path')
const join = path.join
const resolve = path.resolve
const {
  camelCase
} = require('lodash')
const webpack = require('webpack')
const {
  TsConfigPathsPlugin,
  CheckerPlugin
} = require('awesome-typescript-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const env = process && process.env && process.env.NODE_ENV
const serverPort = process.env.npm_package_config_devPort || 8084
const dev = !(env && env === 'production')
/**
 * Update this variable if you change your library name
 */
const libraryName = 'awesome-image'
const plugins = [
  new CheckerPlugin(),
  new TsConfigPathsPlugin(),
  new HtmlWebpackPlugin({
    inject: true,
    title: libraryName,
    filename: 'index.html',
    template: join(__dirname, 'example/index.html'),
    hash: true,
    chunks: ['common', 'index']
  })
]
let entry = [
  // 'react-hot-loader/patch',
  `webpack-dev-server/client?http://localhost:${serverPort}`,
  // 'webpack-hot-middleware/client?reload=true',
  // bundle the client for webpack-dev-servers and connect to the provided endpoint
  'webpack/hot/only-dev-server',
  // bundle the client for hot reloading
  // `./src/${libraryName}.ts`
  __dirname + `/example/index.ts`
]
if (dev === false) {

} else {
  plugins.push(new webpack.HotModuleReplacementPlugin())
}
// plugins.push(new TsConfigPathsPlugin({configFile: "example/tsconfig.json"}))
module.exports = {
  mode: 'development',
  entry: {
    index: entry
  },
  // Currently cheap-module-source-map is broken https://github.com/webpack/webpack/issues/4176
  devtool: 'source-map',
  output: {
    path: join(__dirname, 'dist'),
    libraryTarget: 'umd',
    library: camelCase(libraryName),
    filename: `${libraryName}.js`
  },
  resolve: {
    extensions: ['.ts', '.js']
  },
  externals: {
    "createjs": "createjs"
  },
  module: {
    rules: [{
      test: /\.ts$/,
      use: [{
        loader: 'ts-loader'
      }]
    },
    {
      test: /\.scss$/,
      use: [ 'style-loader', 'css-loader', 'sass-loader' ]
    }]
  },
  plugins: plugins,
  devServer: {
    hot: true,
    contentBase: resolve(__dirname, 'example'),
    port: serverPort,
    publicPath: '/'
  }
}
  1. 文件结构:

图片描述

  1. 控制台输出

图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十二 2022-09-18 19:25:25

同热重载不管用,过来问一哈有解决方案了吗

最佳男配角 2022-09-18 19:25:25

clipboard.png
你的example\index.html中的这段注释删除,或者放到后面去吧。
html-wabpack-plugininject的原理是通过正则匹配template中第一个</body>或者</html>作为插入位置。

clipboard.png

你的chunks被插入在这里了。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文