Skip to content

Commit

Permalink
fix(hmr): require --expose-internals for hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 25, 2024
1 parent ed598ee commit ead7215
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/hmr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cordisjs/plugin-hmr",
"description": "Hot Module Replacement Plugin for Cordis",
"version": "0.2.5",
"version": "0.2.6",
"type": "module",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
21 changes: 13 additions & 8 deletions packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Context, ForkScope, MainScope, Plugin, Schema, Service } from 'cordis'
import { Dict, makeArray } from 'cosmokit'
import { ModuleJob } from 'cordis/loader'
import { ModuleJob, ModuleLoader } from 'cordis/loader'
import { FSWatcher, watch, WatchOptions } from 'chokidar'
import { relative, resolve } from 'path'
import { handleError } from './error.js'
import { handleError } from './error.ts'
import {} from '@cordisjs/timer'
import { fileURLToPath, pathToFileURL } from 'url'
import enUS from './locales/en-US.yml'
Expand Down Expand Up @@ -40,6 +40,7 @@ class Watcher extends Service {
static inject = ['loader']

private base: string
private internal: ModuleLoader
private watcher!: FSWatcher

/**
Expand Down Expand Up @@ -70,6 +71,10 @@ class Watcher extends Service {

constructor(ctx: Context, public config: Watcher.Config) {
super(ctx, 'hmr')
if (!this.ctx.loader.internal) {
throw new Error('--expose-internals is required for HMR service')
}
this.internal = this.ctx.loader.internal
this.base = resolve(ctx.baseDir, config.base || '')
}

Expand Down Expand Up @@ -120,7 +125,7 @@ class Watcher extends Service {

async getLinked(filename: string) {
// The second parameter `type` should always be `javascript`.
const job = this.ctx.loader.internal!.loadCache.get(pathToFileURL(filename).toString())
const job = this.internal.loadCache.get(pathToFileURL(filename).toString())
if (!job) return []
const linked = await job.linked
return linked.map(job => fileURLToPath(job.url))
Expand Down Expand Up @@ -204,9 +209,9 @@ class Watcher extends Service {
for (const baseURL in nameMap) {
for (const name of nameMap[baseURL]) {
try {
const { url } = await this.ctx.loader.internal!.resolve(name, baseURL, {})
const { url } = await this.internal.resolve(name, baseURL, {})
if (this.declined.has(url)) continue
const job = this.ctx.loader.internal!.loadCache.get(url)
const job = this.internal.loadCache.get(url)
const plugin = this.ctx.loader.unwrapExports(job?.module?.getNamespace())
const runtime = this.ctx.registry.get(plugin)
if (!job || !plugin) continue
Expand Down Expand Up @@ -262,15 +267,15 @@ class Watcher extends Service {
// and delete cache before re-import
const backup: Dict = Object.create(null)
for (const filename of this.accepted) {
const job = Map.prototype.get.call(this.ctx.loader.internal!.loadCache, filename)
const job = Map.prototype.get.call(this.internal.loadCache, filename)
backup[filename] = job
Map.prototype.delete.call(this.ctx.loader.internal!.loadCache, filename)
Map.prototype.delete.call(this.internal.loadCache, filename)
}

/** rollback cache */
const rollback = () => {
for (const filename in backup) {
Map.prototype.set.call(this.ctx.loader.internal!.loadCache, filename, backup[filename])
Map.prototype.set.call(this.internal.loadCache, filename, backup[filename])
}
}

Expand Down

0 comments on commit ead7215

Please sign in to comment.