实现一个简单的Nodemon

效果展示

Nodemon 源码分析(核心部分)

第一步

  • 描述:使用 chokidar 进行文件监听,当文件更改后会触发filterAndRestart函数。
  • 文件路径:lib/monitor/watch.js

第二步

  • 描述:filterAndRestart函数,会通过事件总线来发送重启Node.js进程的指令。
  • 文件路径:lib/monitor/watch.js

第三步

  • 描述:事件总线接收到重启指令后,会触发run.kill,终止子进程。
  • 文件路径:lib/monitor/run.js

第四步

  • 描述:子进程退出时,会触发重启函数,从而实现热更新。
  • 文件路径:lib/monitor/run.js

第五步

  • 描述:最后看看nodemon一般会通过spawn运行子进程,与主进程进行管道连接。
  • 文件路径:lib/monitor/run.js

实现思路

  • 使用chokidar来监听JS文件的修改。
  • 通过event模块来实现事件总线,用来跨模块通信。
  • 文件修改后,终止当前子进程,然后重新创建一个子进程,与主进程进行管道连接,最终达到热更新效果。

代码实现

第一步

  • 先搞一个事件总线,用来提供通信。
  • 文件路径:lib/myNodemon/bus.js
js 复制代码
const EventEmitter = require('events')

class Bus extends EventEmitter {}

const bus = new Bus();

module.exports = {
  bus
}

第二步

  • 实现监听函数。
  • 文件路径:lib/myNodemon/watch.js
js 复制代码
const chokidar = require("chokidar");
const path = require("path");
const { bus } = require("./bus.js");

function watch() {
  console.log("开始监听");
  const dirs = path.resolve(__dirname, "../../src");
  chokidar.watch(dirs).on('add', (path) => {
    bus.emit('restart')
  })
  chokidar.watch(dirs).on("change", (path) => {
    bus.emit('restart')
  });
}

module.exports = {
  watch,
};

第三步

  • 在入口文件执行监听、绑定重启函数。
  • 文件路径:lib/myNodemon/main.js
js 复制代码
const { spawn, exec } = require("child_process");
const { bus } = require("./bus.js");
const { watch } = require("./watch.js");

watch();

let child = undefined;

bus.on("restart", restartChild);

function restartChild() {
  console.clear();
  console.log(`[${new Date().toLocaleString()}] 已清理命令行内容:`)
  if (child) {
    child.kill("SIGINT");
    if (process.stdin && child.stdin) {
      process.stdin.unpipe(child.stdin);
    }
    if (process.stdout && child.stdout) {
      process.stdout.unpipe(child.stdout);
    }
  }
  const binPath = process.cwd() + "/node_modules/.bin" + "/node";
  child = spawn("sh", [binPath, process.argv.at(-1)], { stdio: "inherit" });
  if (process.stdin && child.stdin) {
    process.stdin.pipe(child.stdin);
  }
  if (process.stdout && child.stdout) {
    process.stdout.pipe(child.stdout);
  }
}

第四步

  • 最后,在package.json中配置脚本
json 复制代码
{
  "scripts": {
    "dev": "node lib/myNodemon/main.js src/main.js"
  }
}

结语

本文记录了作者对nodemon工作原理的学习和了解,文中若有不足的部分欢迎各位大佬进行指导。

😝 打个广告:如果您目前有合并表格的行/列需求,可以尝试一下 @jinming6/merge-helper 插件。

相关推荐
海盐泡泡龟20 分钟前
Javascript本地存储的方式有哪些?区别及应用场景?(含Deep Seek讲解)
开发语言·javascript·ecmascript
Elastic 中国社区官方博客3 小时前
JavaScript 中使用 Elasticsearch 的正确方式,第一部分
大数据·开发语言·javascript·数据库·elasticsearch·搜索引擎·全文检索
万物得其道者成3 小时前
从零开始创建一个 Next.js 项目并实现一个 TodoList 示例
开发语言·javascript·ecmascript
sunbyte4 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Expanding Cards (展开式卡片)
javascript·vue.js·ecmascript
肠胃炎4 小时前
React Contxt详解
javascript·react.js·ecmascript
xx24064 小时前
React Native简介
javascript·react native·react.js
重生之后端学习5 小时前
02-前端Web开发(JS+Vue+Ajax)
java·开发语言·前端·javascript·vue.js
布鲁斯的快乐小屋5 小时前
axios的基本使用
javascript·ajax
来自星星的坤8 小时前
【Vue 3 + Vue Router 4】如何正确重置路由实例(resetRouter)——避免“VueRouter is not defined”错误
前端·javascript·vue.js
香蕉可乐荷包蛋12 小时前
浅入ES5、ES6(ES2015)、ES2023(ES14)版本对比,及使用建议---ES6就够用(个人觉得)
前端·javascript·es6