Skip to content

Commit

Permalink
refa: use unwrapExports
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 23, 2022
1 parent 4da89fb commit 7dbc220
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
18 changes: 10 additions & 8 deletions packages/cli/src/addons/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,20 @@ class Watcher {
}

start() {
const { loader } = this.ctx
const { root = '', ignored = [] } = this.config
this.root = resolve(this.ctx.loader.dirname, root)
this.root = resolve(loader.dirname, root)
this.watcher = watch(this.root, {
...this.config,
ignored: ['**/node_modules/**', '**/.git/**', '**/logs/**', ...ignored],
})

// files independent from any plugins will trigger a full reload
this.externals = loadDependencies(__filename, new Set(Object.keys(this.ctx.loader.cache)))
this.externals = loadDependencies(__filename, new Set(Object.values(loader.cache)))
const triggerLocalReload = debounce(this.config.debounce, () => this.triggerLocalReload())

this.watcher.on('change', (path) => {
const isEntry = path === this.ctx.loader.filename
const isEntry = path === loader.filename
if (this.suspend && isEntry) {
this.suspend = false
return
Expand Down Expand Up @@ -120,9 +121,10 @@ class Watcher {

private triggerEntryReload() {
// use original config
const oldConfig = this.ctx.loader.config
this.ctx.loader.loadConfig()
const newConfig = this.ctx.loader.config
const { loader } = this.ctx
const oldConfig = loader.config
loader.loadConfig()
const newConfig = loader.config

// check non-plugin changes
const merged = { ...oldConfig, ...newConfig }
Expand All @@ -138,9 +140,9 @@ class Watcher {
if (name.startsWith('~')) continue
if (deepEqual(oldPlugins[name], newPlugins[name])) continue
if (name in newPlugins) {
this.ctx.loader.reloadPlugin(name)
loader.reloadPlugin(name)
} else {
this.ctx.loader.unloadPlugin(name)
loader.unloadPlugin(name)
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions plugins/frontend/manager/src/packages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, App, Context, Dict, omit, pick, Plugin, remove, Schema } from 'koishi'
import { Adapter, App, Context, Dict, omit, pick, Plugin, remove, Schema, unwrapExports } from 'koishi'
import { DataSource } from '@koishijs/plugin-console'
import { promises as fsp } from 'fs'
import { dirname } from 'path'
Expand All @@ -7,10 +7,6 @@ import {} from '@koishijs/cli'

const { readdir, readFile } = fsp

function unwrap(module: any) {
return module.default || module
}

/** require without affecting the dependency tree */
function getExports(id: string) {
const path = require.resolve(id)
Expand All @@ -21,7 +17,7 @@ function getExports(id: string) {
remove(module.children, result)
delete require.cache[path]
}
return unwrap(result.exports)
return unwrapExports(result.exports)
}

class PackageProvider extends DataSource<Dict<PackageProvider.Data>> {
Expand All @@ -47,7 +43,7 @@ class PackageProvider extends DataSource<Dict<PackageProvider.Data>> {

private async updatePackage(plugin: Plugin, id: string) {
const entry = Object.keys(require.cache).find((key) => {
return unwrap(require.cache[key].exports) === plugin
return unwrapExports(require.cache[key].exports) === plugin
})
if (!this.cache[entry]) return
const local = await this.cache[entry]
Expand Down

0 comments on commit 7dbc220

Please sign in to comment.