Expose the default module resolver in a package
未关闭
还没有人认领这个 Issue。
esm
module
never-stale
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.4k
- 平均合并
- 4 天 3 小时
- 30 天内合并 PR
- 272
描述
Currently we can access the default resolver when in a --loader loader. However we can't access this loader from a regular program. I'd like to propose exposing this function as a regular function that can be imported at runtime for use with vm.SourceTextModule to create in-process loaders.
For example:
import vm from 'vm';
import process from 'process';
import url from 'url';
import { resolver } from 'module';
const cwd = url.pathToFileURL(process.cwd() + '/');
class TestLoader {
#cache = new Map();
#realm = vm.createContext({});
async #loadModule(url) {
return new vm.SourceTextModule(await fsp.readFile(resolvedUrl, 'utf8'), {
url: resolvedUrl,
context: this.#realm,
});
}
async #linker(module, specifier) {
// --- Use node's resolution algorithm ---
const resolvedUrl = resolver(module.url, specifier);
if (this.#cache.has(resolvedUrl)) {
return this.#cache.get(resolvedUrl);
}
const module = this.#loadModule(resolvedUrl);
this.#cache.set(resolvedUrl, module);
return module;
}
constructor() {
// Setup test environment
this.#realm.evaluate(`function deepEqual(a, b) { /* ... */ }`);
}
async runTest(file) {
const module = this.#loadModule(resolver(cwd, file));
await module.link((...args) => this.#linker(...args));
module.instantiate();
try {
await module.evaluate();
return { passed: true };
} catch (error) {
return { passed: false, error };
}
}
}
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先查看可用于 --loader loaders 的现有 resolver,以及示例中展示的 vm.SourceTextModule 链接流程。确定导入默认 resolver 所需的运行时 API 和模块入口点,然后验证它是否支持示例所示的进程内 loader 解析。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, node.js
- 领域
- backend
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100