nodejs / nodejs/node

Expose the default module resolver in a package

オープン
#49,454 コメント 4 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

esm module never-stale
主要言語
JavaScript
スター
122k
フォーク
37.4k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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 };
    }
  }
}

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、--loader loaders で利用できる既存の resolver と、例に示されている vm.SourceTextModule のリンク処理を確認します。デフォルトの resolver をインポートするために必要なランタイム API とモジュールエントリーポイントを特定し、その後、示されているとおりインプロセスの loader 解決をサポートしていることを検証します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, node.js
領域
backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。