Skip to content

ts-node-esm binary which spawns a subprocess with --loader ts-node/esm #1258

Closed

Description

@cspotcode

I'd been avoiding this but it might be worth adding. So that someone can write scripts for native ESM with a shebang like this:

#!/usr/bin/env ts-node-esm

Activity

  1. added
    researchNeeds design work, investigation, or prototyping. Implementation uncertain.
    on Mar 1, 2021
  2. cspotcode commented on May 23, 2021

    @cspotcode
    CollaboratorAuthor

    Documentation:

    Should state that this feature exists solely to support esm use-cases. If spawning a child process is no longer necessary in the future because node adds an API for setting loaders, then this flag will stop spawning a child process.

    Implementation ideas:

    Parent process loads config, going only as far as is necessary to see that you specified "esm"

    • skip loading typescript & tsconfig.json if --esm already specified via CLI

    Then it spawns a child process, setting env var TS_NODE_CHILD_ARGS=<base64encodedJSON> and NODE_OPTIONS="--loader ts-node/esm"

    Child process detects TS_NODE_CHILD_ARGS, parses it, and immediately unsets it
    Child continues config loading where parent left off
    Child establishes both ESM and CJS hooks

    • Will entrypoint need to be resolved to absolute path before spawning child?
      • Is this resolution affected by config at all? hopefully not
      • Can we hack it since we're installed as a resolver? Replace it with ts-node-child:129365798 and detect that ID in our resolver?
  3. cspotcode commented on Jan 27, 2022

    @cspotcode
    CollaboratorAuthor

    Had an idea for suppressing the "experimental" warning from node. node's default warning logger is an event handler on the process 'warning' event.

    When we launch the subprocess, we can pass -r ./suppress-warnings.cjs

    It can do:

    // Not shown here: Additional logic to correctly interact with process's events, either using this direct manipulation, or via the API
    const warningEvent = process._events.warning;
    // TODO validate that this looks like node's built-in warning handler
    // TODO validate that this is not undefined, which might be caused by `--no-warnings`
    const messageMatch = /--(?:experimental-)?loader\b/;
    process._events.warning = function(warning, ...rest) {
      if(warning.name === 'ExperimentalWarning' && messageMatch.test(warning.message)) return;
      return warningEvent.call(this, warning, ...rest);
    });
  4. added this to the 10.6.0 or 10.5.1 milestone on Feb 22, 2022
  5. modified the milestones: 10.6.0, 10.7.0 on Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    researchNeeds design work, investigation, or prototyping. Implementation uncertain.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions