{"id":23529939,"url":"https://github.com/patreeceeo/hot_mod","last_synced_at":"2025-04-22T17:24:44.586Z","repository":{"id":265671741,"uuid":"612804587","full_name":"patreeceeo/hot_mod","owner":"patreeceeo","description":"HMR for ES Modules","archived":false,"fork":false,"pushed_at":"2023-03-19T23:22:12.000Z","size":50,"stargazers_count":7,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-22T17:24:36.407Z","etag":null,"topics":["deno","developer-tools","esm","esmodules","hmr","hot-module-replacement","javascript","rapid-development","rapid-prototyping","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patreeceeo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-12T02:42:43.000Z","updated_at":"2023-03-14T00:50:16.000Z","dependencies_parsed_at":"2024-11-30T12:45:22.142Z","dependency_job_id":"29abbcdb-bef6-4a6c-8e77-41398bdab5f1","html_url":"https://github.com/patreeceeo/hot_mod","commit_stats":null,"previous_names":["patreeceeo/hot_mod"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patreeceeo%2Fhot_mod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patreeceeo%2Fhot_mod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patreeceeo%2Fhot_mod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patreeceeo%2Fhot_mod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patreeceeo","download_url":"https://codeload.github.com/patreeceeo/hot_mod/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250285962,"owners_count":21405340,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deno","developer-tools","esm","esmodules","hmr","hot-module-replacement","javascript","rapid-development","rapid-prototyping","typescript"],"created_at":"2024-12-25T21:14:18.791Z","updated_at":"2025-04-22T17:24:44.564Z","avatar_url":"https://github.com/patreeceeo.png","language":"TypeScript","readme":"# hot_mod\n\n\nhttps://user-images.githubusercontent.com/578371/224897357-0119a373-5f50-4b46-b3de-5f158d7c8591.mp4\n\n\n\n\nAn ESM-HMR client and server, allowing for hot-reloading client-side (and maybe server-side? untested) ESModules, as described by https://github.com/FredKSchott/esm-hmr.\n\n## Usage with Deno\n\nThese modules are published at https://deno.land/x/hot_mod\n\nIn each client-side module:\n\n```javascript\nimport { useClient } from \"hot_mod/dist/client/mod.js\";\n\nuseClient(import.meta);\n\n// The following is just my best idea so far of how to write hot-reloadable modules :)\nexport const hotExports = {\n  // Add more identifiers here\n  drawPlayers // example\n}\nif (import.meta.hot) {\n  import.meta.hot.accept([], ({ module }) =\u003e {\n    for(const key of Object.keys(hotExports) as Array\u003ckeyof typeof hotExports\u003e) {\n      hotExports[key] = module.hotExports[key]\n    }\n  });\n}\n// In app code, write hotExports.drawPlayers() instead of drawPlayers()\n```\n\n`import.meta.hot` will available in development (well, as long as serving from localhost. I mean to support some kind of configuration or environment variables for deciding when HMR should be enabled.)\n\nThen, in the dev server, import the HMR engine and wire it up:\n\n```typescript\nimport { serve } from \"http\";\nimport { relative } from \"path\";\nimport { debounce } from \"async\";\nimport { EsmHmrEngine } from \"hot_mod/src/server/mod.ts\";\n\ninterface ModuleEventHandler {\n  (paths: IterableIterator\u003cstring\u003e): void;\n}\n\nlet listenerCount = 0;\nconst modifiedModuleUrls = new Set\u003cstring\u003e();\nasync function addModuleEventHandler(\n  handler: ModuleEventHandler,\n  absPaths: Array\u003cstring\u003e,\n) {\n  const watcher = Deno.watchFs(absPaths, { recursive: true });\n\n  const debouncedListener = debounce(() =\u003e {\n    const copy = new Set(modifiedModuleUrls);\n    handler(copy.values());\n    modifiedModuleUrls.clear();\n  }, 200);\n\n  listenerCount++;\n  console.log(\n    `Module event handler #${listenerCount} for ${absPaths.join(\", \")}`,\n  );\n\n  for await (const event of watcher) {\n    if (event.kind === \"modify\") {\n      for (const path of event.paths) {\n        // These strings must correspond to those created on the client:\n        // The pathname to the full URL to the module subject to hot reloading,\n        // e.g. new URL(import.meta.url).pathname;\n        const moduleId = \"/\" + relative(Deno.cwd(), path);\n        modifiedModuleUrls.add(moduleId);\n      }\n    }\n    if (modifiedModuleUrls.size \u003e 0) {\n      debouncedListener();\n    }\n  }\n}\n\nconst engine = new EsmHmrEngine((emitModuleModifiedEvent) =\u003e {\n  addModuleEventHandler((paths) =\u003e {\n    for (const path of paths) {\n      emitModuleModifiedEvent(path);\n    }\n  }, [Deno.cwd() + \"/public\"]);\n});\nserve((request) =\u003e {\n  const { socket, response } = Deno.upgradeWebSocket(request);\n  engine.addClient(socket);\n  return response;\n}, { port: 12321 });\n```\n\nAnd run it with:\n\n```sh\ndeno run --allow-net --allow-read --watch \"path/to/dev_server.ts\"\n```\n\n## API\n\nSee https://github.com/FredKSchott/esm-hmr\n\n## Contributing\n\nGitHub issues and pull requests welcome!\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatreeceeo%2Fhot_mod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatreeceeo%2Fhot_mod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatreeceeo%2Fhot_mod/lists"}