javascript - 启用 Webpack HMR 时注入的模块未正确加载
问题描述
我正在开发一个库,然后使用 Webpack 将其注入到 html 文档中。我需要在项目上启用 HMR。但是,当我启用它时,库将无法正确加载。这是我从控制台得到的:
Library.init()
不是函数。
并且库被加载为空object
,而通过关闭 HMR,它被正确加载。
这是我的 Webpack 配置:
{
...baseConfigs,
entry: './src/index.ts',
output: {
path: __dirname + '/dist',
filename: `lib.js`,
library: {
name: 'Library',
type: 'window',
},
},
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
publicPath: '/',
port: 8000,
open: true,
hot: true,
},
plugins: [
...baseConfig.plugins,
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'demo', 'index.html'),
inject: 'head',
}),
new webpack.HotModuleReplacementPlugin(),
],
}
而入口点————index.ts
是这样的:
import {main, subscribe} from './main' // Library's main functionality imported
export {
init,
subscribe,
}
if(module.hot){
module.hot.accept('./index.ts')
}
我错过了一块拼图吗?
解决方案
推荐阅读
- c# - NHibernate PropertyRef 流畅映射
- android - 我可以在 UI 线程中创建一个 android 视图吗?
- php - 密码字符和特殊字符会破坏代码并转换为布尔值,如下例所示
- javascript - 如何用 JavaScript 对应比较列表项
- java - 未生成 sonarqube cnes 报告(Windows)
- python-sphinx - 将其他 *.rst - 文件中的整个文本部分包含到此文件中吗?
- antd - 如何在 antd TimePicker 中限制持续时间?
- php - 仅在浏览器支持的情况下如何发送 SameSite
- smartsheet-api - 如何捕获 Smartsheet 共享邀请邮件中提供的链接
- c# - C# 使用 VLC.DOTNET.FORMS 包将 PC 摄像头图像流式传输到 Android