Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cspotcode opened this issue Mar 1, 2021 · 2 comments · Fixed by #1655
Closed

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

cspotcode opened this issue Mar 1, 2021 · 2 comments · Fixed by #1655
Labels
research Needs design work, investigation, or prototyping. Implementation uncertain.
Milestone

Comments

@cspotcode
Copy link
Collaborator

cspotcode commented Mar 1, 2021

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
@cspotcode cspotcode added the research Needs design work, investigation, or prototyping. Implementation uncertain. label Mar 1, 2021
@cspotcode
Copy link
Collaborator Author

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?

@cspotcode
Copy link
Collaborator Author

cspotcode commented Jan 27, 2022

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

@cspotcode cspotcode added this to the 10.6.0 or 10.5.1 milestone Feb 22, 2022
@cspotcode cspotcode modified the milestones: 10.6.0, 10.7.0 Mar 1, 2022
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this issue Mar 8, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-node](https://typestrong.org/ts-node) ([source](https://github.com/TypeStrong/ts-node)) | devDependencies | minor | [`10.6.0` -> `10.7.0`](https://renovatebot.com/diffs/npm/ts-node/10.6.0/10.7.0) |

---

### Release Notes

<details>
<summary>TypeStrong/ts-node</summary>

### [`v10.7.0`](https://github.com/TypeStrong/ts-node/releases/v10.7.0)

[Compare Source](TypeStrong/ts-node@v10.6.0...v10.7.0)

Questions about this release? Ask in the official discussion thread: [#&#8203;1680](TypeStrong/ts-node#1680)

**Added**

-   Adds `--esm` flag, option, and `ts-node-esm` binary ([#&#8203;1258](TypeStrong/ts-node#1258), [#&#8203;1655](TypeStrong/ts-node#1655))
    -   Enables full `esm` support; no need for `--loader` nor `NODE_OPTIONS`
    -   Use shebang `#!/usr/bin/env ts-node-esm`, run `ts-node --esm`, or add to your tsconfig.json: `"ts-node": {"esm": true}`

**Changed**

-   Unflag ESM json modules on node >=17.5.0 ([#&#8203;1661](TypeStrong/ts-node#1661), [#&#8203;1665](TypeStrong/ts-node#1665)) [@&#8203;Jamesernator](https://github.com/Jamesernator)
    -   no longer requires `--experimental-json-modules`
-   Lazy-load dependencies to improve startup responsiveness. ([#&#8203;1676](TypeStrong/ts-node#1676))

**Fixed**

-   Fixed bug where "compiler", "transpiler", and swc backend would not resolve relative to the tsconfig.json that declared them ([#&#8203;1662](TypeStrong/ts-node#1662), [#&#8203;1655](TypeStrong/ts-node#1655))
    -   Enables reusable tsconfig.json shared via node module to include necessary dependencies

https://github.com/TypeStrong/ts-node/milestone/11

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1200
Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
research Needs design work, investigation, or prototyping. Implementation uncertain.
Projects
None yet
1 participant