Expose the default module resolver in a package
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- JavaScript
- Star
- 122k
- Fork
- 37.3k
- Merge trung bình
- 4 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 283
Mô tả
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 };
}
}
}
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách xem xét resolver hiện có được cung cấp cho --loader loaders và quy trình liên kết vm.SourceTextModule được minh họa trong ví dụ. Xác định runtime API và điểm vào module cần thiết để import resolver mặc định, sau đó xác minh rằng nó hỗ trợ việc phân giải loader trong cùng tiến trình như được minh họa.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, node.js
- Lĩnh vực
- backend
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100