TypeStrong/ts-node

ESM support: can't recognize '--experimental-specifier-resolution' flag passed via NODE_OPTIONS

koshic opened this issue · 2 comments

Code at

uses process.execArgv to get options and doesn't analyze NODE_OPTIONS anyhow.

I'm marking this as "help wanted" to indicate that we'll wait for a pull request. This seems like a straightforward contribution.

node-esm-resolve-implementation.js is largely copied from node's own source code.

// Copied from https://raw.githubusercontent.com/nodejs/node/v13.12.0/lib/internal/modules/esm/resolve.js
// Then modified to suite our needs.
// Formatting is intentionally bad to keep the diff as small as possible, to make it easier to merge
// upstream changes and understand our modifications.

To write an ESM loader, we need to mimic many built-in behaviors of node. Unfortunately node does not expose this functionality via an API. So our best option for maintainability is to copy-paste node's own implementation where appropriate, tweaking as needed.

See also: discussion in #1010, where @evg656e helped me implement our execArgv parsing.

Yep, I missed the need to parse the NODE_OPTIONS environment variable in the initial implementation.

Unfortunately, the arg parser does not allow getting options of the environment variables (like 'yargs` .env function does).

The workaround to use ParseNodeOptionsEnvVar from node source.

I rewrote getOptionValue code in my branch.

As you can see, the code becomes quite messy, so it may be worth putting it into a separate file to keep main module clean.