【发布时间】:2020-04-14 04:19:12
【问题描述】:
我创建了一个想要在某些项目中使用的 Js 库。它并不完全稳定,并且有一些我想要处理的错误。但是,当 HMR 不能处理依赖项时,开发一个库意味着什么? 用例:
- 创建一个新的 Vue 项目:
vue create fooproject,cd 进入该目录。 - 添加一个 local javascript 模块作为依赖项
- 如果你想在 ~/tmp 目录的某个地方下载my library;然后使用:
npm install --save ~/tmp/vue-extensions # or where your lib lives添加它。
- 如果你想在 ~/tmp 目录的某个地方下载my library;然后使用:
- 您的 node_modules 现在应该有一个指向该目录的符号链接。
- 运行
npm run serve- 它编译 Vue 项目并运行开发网络服务器。 让该终端保持打开状态。 - 现在更改本地
vue-extensions库中的一些代码 - 只需在任意位置添加空格并保存即可。 - 什么都没有发生 - 因为 lib 本身没有随更改一起编译。在 dist/ 目录中导入搜索。这是正常的 AFAIK。
- 现在,转到
vue-extensions目录并运行npm run build- 它应该构建 lib 并返回 qith 成功。 - 此时,第一个终端应该更改:Webpack 应该检测代码更改并触发 HMR - 它首先中断 - 我想是因为 vue-extensionpoints.common.js 被临时删除(在重新创建之前)而 webpack 没有再也找不到了:
Module build failed (from ./node_modules/eslint-loader/index.js):
Error: ENOENT: no such file or directory, open '/home/christian/vue-extensionpoints/dist/vue-extensionpoints.common.js'
@ ./src/main.js 7:0-50 10:8-23
@ multi (webpack)-dev-server/client?http://192.168.4.3:8080/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
这个没问题,重启构建:npm run serve
现在的问题是:它再次中断,出现许多错误:
error: Unexpected newline between function and ( of function call (no-unexpected-multiline) at ../../Projekte/vue-extensionpoints/dist/vue-extensionpoints.common.js:88:10:
86 | /******/ })
87 | /************************************************************************/
> 88 | /******/ ({
| ^
89 |
90 | /***/ "06cf":
91 | /***/ (function(module, exports, __webpack_require__) {
error: 'exports' is defined but never used (no-unused-vars) at ../../Projekte/vue-extensionpoints/dist/vue-extensionpoints.common.js:135:25:
133 |
134 | /***/ "1d80":
> 135 | /***/ (function(module, exports) {
| ^
136 |
137 | // `RequireObjectCoercible` abstract operation
138 | // https://tc39.github.io/ecma262/#sec-requireobjectcoercible
error: 'exports' is defined but never used (no-unused-vars) at ../../Projekte/vue-extensionpoints/dist/vue-extensionpoints.common.js:370:25:
368 |
369 | /***/ "5135":
> 370 | /***/ (function(module, exports) {
| ^
371 |
372 | var hasOwnProperty = {}.hasOwnProperty;
373 |
error: 'exports' is defined but never used (no-unused-vars) at ../../Projekte/vue-extensionpoints/dist/vue-extensionpoints.common.js:417:25:
415 |
416 | /***/ "5c6c":
> 417 | /***/ (function(module, exports) {
| ^
418 |
419 | module.exports = function (bitmap, value) {
420 | return {
error: 'exports' is defined but never used (no-unused-vars) at ../../Projekte/vue-extensionpoints/dist/vue-extensionpoints.common.js:603:25:
[...and so on...]
只需用 Ctrl+C 中断并运行npm run serve再次,它就可以工作了。
npm cache clear [--force] 没有帮助。
当我总是必须重建库并停止并重新启动主服务器两次时,我应该如何更改库并使用 HMR 重新加载它?这使得开发变得不可能,除非你有太多的时间和耐心去做这样的事情。
必须有一种(显然)更简单的方法来完成库开发,对吧?
提前致谢。
【问题讨论】:
标签: javascript npm webpack dependencies