Skip to content

Commit

Permalink
feat: support ctx.baseDir based on tree.url
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 30, 2024
1 parent 89c8920 commit c425f66
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class HMR extends Service {
}

async stop() {
await this.watcher.close()
await this.watcher?.close()
}

async getLinked(filename: string) {
Expand Down
6 changes: 6 additions & 0 deletions packages/loader/src/config/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as yaml from 'js-yaml'
import { EntryOptions } from './entry.ts'
import { ImportTree } from './import.ts'
import { JsExpr } from './utils.ts'
import { dirname } from 'node:path'

export const schema = yaml.JSON_SCHEMA.extend(JsExpr)

Expand All @@ -22,6 +23,11 @@ export class LoaderFile {
this.trees.push(tree)
tree.url = pathToFileURL(this.name).href
tree.ctx.loader.files[tree.url] ??= this
// use defineProperty to prevent provide check
Object.defineProperty(tree.ctx, 'baseDir', {
value: dirname(this.name),
configurable: true,
})
}

unref(tree: ImportTree) {
Expand Down
1 change: 0 additions & 1 deletion packages/loader/src/config/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class ImportTree<C extends Context = Context> extends EntryTree<C> {
} else {
await this._init(baseDir, options)
}
this.ctx.provide('baseDir', baseDir, true)
}

private async _init(baseDir: string, options: Loader.Config) {
Expand Down
63 changes: 63 additions & 0 deletions packages/loader/tests/group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,66 @@ describe('group management: transfer', () => {
expect([...loader.entries()]).to.have.length(4)
})
})

describe('group management: intercept', () => {
const root = new Context()
const callback = mock.fn()

let loader!: MockLoader

before(async () => {
await root.plugin(MockLoader)
loader = root.loader as any
loader.mock('foo', (ctx: Context) => {
callback(ctx[Context.intercept])
})
await loader.start()
})

beforeEach(() => {
callback.mock.resetCalls()
})

let outer!: string
let inner!: string
let id!: string

it('initialize', async () => {
outer = await loader.create({
name: 'cordis/group',
group: true,
intercept: {
foo: {
a: 1,
},
},
config: [],
})

inner = await loader.create({
name: 'cordis/group',
group: true,
intercept: {
foo: {
b: 2,
},
},
config: [],
}, outer)

id = await loader.create({
name: 'foo',
intercept: {
foo: {
c: 3,
},
},
}, inner)

expect(callback.mock.calls).to.have.length(1)
const intercept = callback.mock.calls[0].arguments[0]
expect(intercept.foo).to.deep.equal({ c: 3 })
expect(Object.getPrototypeOf(intercept).foo).to.deep.equal({ b: 2 })
expect(Object.getPrototypeOf(Object.getPrototypeOf(intercept)).foo).to.deep.equal({ a: 1 })
})
})

0 comments on commit c425f66

Please sign in to comment.