用的是nodemon的热更新开发,怎么加eslint语法检测呢? 就是每次保存代码eslint检测语法,没有问题nodemon继续运行。
VS Code 装 ESLint 会实时提醒,保存会自动 fix。
一般在开发阶段 lint 也就是提醒吧,就算强制要求通过也是在功能/阶段性开发完成之后检查。没理由每次保存都必须通过,多耽误事。
VSC配置项: "eslint.autoFixOnSave": true
我们项目里的规则: https://github.com/xiaozhongliu/node-api-seed/blob/master/.eslintrc.js
module.exports = {
extends: 'airbnb',
rules: {
'array-callback-return': 0,
'arrow-body-style': 0,
'arrow-parens': 0,
'comma-dangle': 0,
'consistent-return': 0,
'func-names': 0,
'global-require': 0,
'guard-for-in': 0,
'import/no-dynamic-require': 0,
'indent': ['error', 4],
'max-len': 0,
'no-await-in-loop': 0,
'no-bitwise': 0,
'no-console': 0,
'no-lonely-if': 0,
'no-new': 0,
'no-param-reassign': 0,
'no-plusplus': 0,
'no-restricted-syntax': 0,
'no-underscore-dangle': 0,
'no-unused-expressions': 0,
'no-use-before-define': 0,
'object-curly-newline': 0,
'prefer-const': ['error', { 'destructuring': 'all' }],
'semi': [2, 'never'],
}
}
@mengdu 卡的原因是什么, 机器再差也差不到哪里去. 就开发一个前端为什么卡. 你这都卡, 那java多开几个idea, .net多开几个visual studio还活不活了. 我i5 8g的机器开俩idea, vscode, compass, valentina, chrome多tab, 还有个onenote这个大户, 也没啥压力啊.
楼主可能需要自建一套简单的工具链 流程上就是检查代码是否更改 -> 走 eslint (甚至可以走 babel) -> 关掉原来的 node 进程 -> 调用 node 运行 前两步可以用 gulp 的 watch 模式解决,后两步可以用 nodemon 解决 不一样的地方在于,用 gulp 编译完的东西是放到 dist 文件夹的(假设源码在 src 文件夹),nodemon 运行 dist 文件夹里面的文件 这样如果前两步报错,dist 文件夹内容不会改变,nodemon 也就不会重启项目
这个方案有两个比较不完善的地方:
- 代码调试问题:代码毕竟最后被转移了位置,所以一定要有 sourceMap,否则调试的时候调试的就是 dist 里面最后生成的代码,而不是 src 里面你写的代码
- 要开两个终端窗口:虽然这个问题可以用 concurrently 这个库解决,但是日志输出可能很丑 23333333333