Skip to content

Commit

Permalink
feat(loader): support internal based module loader
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 12, 2024
1 parent 79c564a commit 0fb99bd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
8 changes: 3 additions & 5 deletions packages/cordis/src/worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { createRequire } from 'node:module'
import Loader from '@cordisjs/loader'
import * as daemon from './daemon.js'
import * as logger from './logger.js'
import { ModuleLoader } from './internal.js'
import { Context } from '../index.ts'

export type * from './internal.js'

declare module '@cordisjs/loader' {
interface Loader {
internal?: ModuleLoader
Expand All @@ -22,8 +20,8 @@ export async function start(options: Options) {
ctx.plugin(Loader, options)
await ctx.loader.init(process.env.CORDIS_LOADER_ENTRY)
if (process.execArgv.includes('--expose-internals')) {
const { internal } = await import('./internal.js')
ctx.loader.internal = internal
const require = createRequire(import.meta.url)
ctx.loader.internal = require('internal/process/esm_loader').esmLoader
}
if (options.logger) ctx.plugin(logger, options.logger)
if (options.daemon) ctx.plugin(daemon, options.daemon)
Expand Down
2 changes: 1 addition & 1 deletion packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, ForkScope, MainScope, Plugin, Schema, Service } from 'cordis'
import { Dict, makeArray } from 'cosmokit'
import { ModuleJob } from 'cordis/worker'
import { ModuleJob } from '@cordisjs/loader'
import { FSWatcher, watch, WatchOptions } from 'chokidar'
import { relative, resolve } from 'path'
import { handleError } from './error.js'
Expand Down
9 changes: 1 addition & 8 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { promises as fs } from 'fs'
import * as dotenv from 'dotenv'
import * as path from 'path'

export * from './internal.ts'
export * from './entry.ts'
export * from './shared.ts'

Expand Down Expand Up @@ -45,14 +46,6 @@ class NodeLoader extends Loader<NodeLoader.Options> {
return await super.readConfig()
}

async import(name: string) {
try {
return await import(name)
} catch (err: any) {
this.app.emit('internal/error', new Error(`Cannot find package "${name}"`))
}
}

exit(code = NodeLoader.exitCode) {
const body = JSON.stringify(this.envData)
process.send?.({ type: 'shared', body }, (err: any) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createRequire, LoadHookContext } from 'module'
import { LoadHookContext } from 'module'
import { Dict } from 'cosmokit'

const require = createRequire(import.meta.url)

type ModuleFormat = 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm'
type ModuleSource = string | ArrayBuffer

Expand Down Expand Up @@ -48,5 +46,3 @@ export interface ModuleLoader {
load(specifier: string, context: Pick<LoadHookContext, 'format' | 'importAttributes'>): Promise<LoadResult>
loadSync(specifier: string, context: Pick<LoadHookContext, 'format' | 'importAttributes'>): LoadResult
}

export const internal: ModuleLoader = require('internal/process/esm_loader').esmLoader
21 changes: 17 additions & 4 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Context, EffectScope, Service } from '@cordisjs/core'
import { defineProperty, Dict, isNullable, valueMap } from 'cosmokit'
import { constants, promises as fs } from 'fs'
import { constants, promises as fs } from 'node:fs'
import { pathToFileURL } from 'node:url'
import { ModuleLoader } from './internal.ts'
import { interpolate } from './utils.ts'
import { Entry } from './entry.ts'
import * as yaml from 'js-yaml'
Expand Down Expand Up @@ -72,12 +74,12 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
public realms: Dict<Dict<symbol>> = Object.create(null)
public delims: Dict<symbol> = Object.create(null)

public internal?: ModuleLoader

private tasks = new Set<Promise<any>>()
private _writeTask?: Promise<void>
private _writeSlient = true

abstract import(name: string): Promise<any>

constructor(public app: Context, public options: T) {
super(app, 'loader', true)
this.root = new Entry(this)
Expand Down Expand Up @@ -216,8 +218,19 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> extends
}
}

async import(name: string) {
if (this.internal) {
return this.internal.import(name, pathToFileURL(this.filename).href, {})
} else {
return import(name)
}
}

async resolve(name: string) {
const task = this.import(name)
const task = this.import(name).catch((error) => {
this.app.emit('internal/error', new Error(`Cannot find package "${name}"`))
this.app.emit('internal/error', error)
})
this.tasks.add(task)
task.finally(() => this.tasks.delete(task))
return this.unwrapExports(await task)
Expand Down

0 comments on commit 0fb99bd

Please sign in to comment.