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's new node ESM support #1414

Closed
cspotcode opened this issue Jul 22, 2021 · 5 comments · Fixed by #1694
Closed

TS's new node ESM support #1414

cspotcode opened this issue Jul 22, 2021 · 5 comments · Fixed by #1694
Labels
you can do this Good candidate for a pull request.
Milestone

Comments

@cspotcode
Copy link
Collaborator

cspotcode commented Jul 22, 2021

Will need work on our side to integrate.

microsoft/TypeScript#44501
weswigham/TypeScript#67

Roughly

adds new module: node12 and module: nodeNext configs
In them:

  • existing extensions obey package.json "type" field
  • new extensions .mts, .mjs, .cts, .cjs force one or the other; ignore package.json "type" field

Questions I still have

Are any of the above assumed to be modules without needing to add export {};?

@alvis
Copy link

alvis commented Oct 17, 2021

With 4.5 beta announced 2 weeks ago, and the final on the horizon, time for us to do something.

@cspotcode
Copy link
Collaborator Author

@alvis are you referring to a pull request that you have submitted?

@cspotcode cspotcode added this to the next milestone Oct 18, 2021
@cspotcode
Copy link
Collaborator Author

I've assigned this to @alvis and added it to our next milestone. Let us know if you need any guidance to implement the necessary changes. I've added some notes below.

At a minimum, you'll need to add require.extensions hooks for mts, mjs, cts, and cjs extensions. You may also need to add them to the extensions passed to createResolve:

ts-node/src/esm.ts

Lines 50 to 54 in 3b40365

// Custom implementation that considers additional file extensions and automatically adds file extensions
const nodeResolveImplementation = createResolve({
...getExtensions(tsNodeService.config),
preferTsExts: tsNodeService.options.preferTsExts,
});

ts-node/src/index.ts

Lines 482 to 491 in 3b40365

export function getExtensions(config: _ts.ParsedCommandLine) {
const tsExtensions = ['.ts'];
const jsExtensions = [];
// Enable additional extensions when JSX or `allowJs` is enabled.
if (config.options.jsx) tsExtensions.push('.tsx');
if (config.options.allowJs) jsExtensions.push('.js');
if (config.options.jsx && config.options.allowJs) jsExtensions.push('.jsx');
return { tsExtensions, jsExtensions };
}

Should the new extensions be enabled conditionally, only when your TS version is >= 4.5?

I don't expect you'll need to implement any fancy resolving logic beyond that. TS already handles resolving within the typechecker, and node's existing resolution logic should continue to behave.

@PodaruDragos
Copy link

Will this mean that we can now potentially have import { foo } from './foo', basically omitting the extensions just like in commonJS ?

@cspotcode
Copy link
Collaborator Author

@PodaruDragos you're probably looking for node's --experimental-specifier-resolution=node which we already support. Just as node requires you to pass that flag, we also require you to pass that flag, often via the NODE_OPTIONS environment variable.

https://nodejs.org/dist/latest-v17.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm

D-Pow added a commit to D-Pow/react-app-boilerplate that referenced this issue Jan 12, 2022
This allows `ts-node` to be used to run TypeScript files without having to compile them with `tsc` first. It also adds the necessary configs, including `tsconfig-paths` for `paths` import-alias resolution. Unfortunately, this means we have to remove the `--experimental-module-resolution=node` since `ts-node` uses its own loader and thus form of resolving modules.

See:

Setup
* https://medium.com/@jimcraft123hd/setting-up-path-alias-in-typescript-and-tsc-build-without-error-9f1dbc0bccd2

Issues with `ts-node`, ESM, and aliases
* TypeStrong/ts-node#1007
    - TypeStrong/ts-node#476
    - dividab/tsconfig-paths#122 (comment)
    - TypeStrong/ts-node#1450 (comment)
* TypeStrong/ts-node#1414
* TypeStrong/ts-node#995
    - TypeStrong/ts-node#639

Node issues with ESM
* https://nodejs.org/api/packages.html#determining-module-system
* nodejs/node#37468
D-Pow added a commit to D-Pow/react-app-boilerplate that referenced this issue Jan 12, 2022
This allows `ts-node` to be used to run TypeScript files without having to compile them with `tsc` first. It also adds the necessary configs, including `tsconfig-paths` for `paths` import-alias resolution. Unfortunately, this means we have to remove the `--experimental-module-resolution=node` since `ts-node` uses its own loader and thus form of resolving modules.

See:

Setup
* https://medium.com/@jimcraft123hd/setting-up-path-alias-in-typescript-and-tsc-build-without-error-9f1dbc0bccd2

Issues with `ts-node`, ESM, and aliases
* TypeStrong/ts-node#1007
    - TypeStrong/ts-node#476
    - dividab/tsconfig-paths#122 (comment)
    - TypeStrong/ts-node#1450 (comment)
* TypeStrong/ts-node#1414
* TypeStrong/ts-node#995
    - TypeStrong/ts-node#639

Node issues with ESM
* https://nodejs.org/api/packages.html#determining-module-system
* nodejs/node#37468
D-Pow added a commit to D-Pow/react-app-boilerplate that referenced this issue Jan 12, 2022
This allows `ts-node` to be used to run TypeScript files without having to compile them with `tsc` first. It also adds the necessary configs, including `tsconfig-paths` for `paths` import-alias resolution. Unfortunately, this means we have to remove the `--experimental-module-resolution=node` since `ts-node` uses its own loader and thus form of resolving modules.

See:

Setup
* https://medium.com/@jimcraft123hd/setting-up-path-alias-in-typescript-and-tsc-build-without-error-9f1dbc0bccd2

Issues with `ts-node`, ESM, and aliases
* TypeStrong/ts-node#1007
    - TypeStrong/ts-node#476
    - dividab/tsconfig-paths#122 (comment)
    - TypeStrong/ts-node#1450 (comment)
* TypeStrong/ts-node#1414
* TypeStrong/ts-node#995
    - TypeStrong/ts-node#639

Node issues with ESM
* https://nodejs.org/api/packages.html#determining-module-system
* nodejs/node#37468
@cspotcode cspotcode modified the milestones: 10.5.0, next Jan 31, 2022
@cspotcode cspotcode modified the milestones: 10.6.0 or 10.5.1, next Feb 21, 2022
@cspotcode cspotcode modified the milestones: next, 10.8.0 May 17, 2022
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this issue May 30, 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.7.0` -> `10.8.0`](https://renovatebot.com/diffs/npm/ts-node/10.7.0/10.8.0) |

---

### Release Notes

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

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

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

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

**Added**

-   Added support for `module=NodeNext`, `module=Node16`, `.mts`, `.cts`, `.mjs`, and `.cjs` file extensions ([#&#8203;1414](TypeStrong/ts-node#1414), [#&#8203;1694](TypeStrong/ts-node#1694), [#&#8203;1744](TypeStrong/ts-node#1744), [#&#8203;1745](TypeStrong/ts-node#1745), [#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1717](TypeStrong/ts-node#1717), [#&#8203;1753](TypeStrong/ts-node#1753), [#&#8203;1757](TypeStrong/ts-node#1757)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   For best results, enable `experimentalResolver` ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver))
    -   See TypeScript's official documentation: https://www.typescriptlang.org/docs/handbook/esm-node.html
    -   enables mixed-mode projects with both ESM and CommonJS
    -   enables all supported file extensions in TypeScript 4.7
    -   Obeys package.json "type"
-   Added ability to include file extensions in CommonJS imports ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Enables consistency with ESM, where file extensions are often mandatory
-   Resolves from emitted to source file extensions ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Must enable `experimentalResolver`, will be enabled by default in a future version ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver))
    -   Typechecker requires importing the *emitted* file extension; ts-node resolves correctly to the *source* file.  E.g. `import "./foo.js"` will execute `foo.ts` See also: [TypeScript issue #&#8203;37582](microsoft/TypeScript#37582)
    -   If typechecking is disabled, you can also use *source* file extensions.  E.g. `import "./foo.ts"`
-   Added `experimentalSpecifierResolution` ([#&#8203;1727](TypeStrong/ts-node#1727), [#&#8203;1753](TypeStrong/ts-node#1753)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   the same as Node's `--experimental-specifier-resolution` ([Node docs](https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm))
    -   can also be specified in `tsconfig.json` for convenience, to avoid the CLI flag
    -   allows omitting file extensions in ESM imports, plus a few other CommonJS-style conveniences
-   Adds `diagnostics` property to `TSError`, with array of TypeScript diagnostic objects from the compiler ([API docs](https://typestrong.org/ts-node/api/classes/TSError.html)) ([#&#8203;1705](TypeStrong/ts-node#1705), [#&#8203;1706](TypeStrong/ts-node#1706)) [@&#8203;paulbrimicombe](https://github.com/paulbrimicombe)

**Changed**

-   Renames option `experimentalResolverFeatures` to `experimentalResolver` ([docs](https://typestrong.org/ts-node/docs/options#experimentalresolver)) ([#&#8203;1727](TypeStrong/ts-node#1727)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Internal change to ESM loader for compatibility with forthcoming node versions: returns `shortCircuit: true` ([#&#8203;1714](TypeStrong/ts-node#1714), [#&#8203;1715](TypeStrong/ts-node#1715)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Performance: Optimize filesystem stat calls in ESM loader and new CommonJS resolver ([#&#8203;1758](TypeStrong/ts-node#1758), [#&#8203;1759](TypeStrong/ts-node#1759)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Performance, maintenance: Upgrade source-mapper dependency "[@&#8203;cspotcode/source-map-support](https://github.com/cspotcode/source-map-support)"
    -   Switches to "trace-mapping" for underlying source-map parsing ([#&#8203;1729](TypeStrong/ts-node#1729)) [@&#8203;cspotcode](https://github.com/cspotcode)

**Fixed**

-   Fixed bug where REPL `.type` command was not showing any type information when using TypeScript nightly builds ([#&#8203;1761](TypeStrong/ts-node#1761), [#&#8203;1762](TypeStrong/ts-node#1762)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Correctly suppress "Custom ESM Loaders" warning on newer node versions where the warning's prose changed ([#&#8203;1701](TypeStrong/ts-node#1701)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed REPL bug where function signatures could not be entered across multiple lines ([#&#8203;1667](TypeStrong/ts-node#1667), [#&#8203;1677](TypeStrong/ts-node#1677)) [@&#8203;d9k](https://github.com/d9k)
-   REPL treats unparenthesized object literals as objects, instead of as block scopes ([#&#8203;1697](TypeStrong/ts-node#1697), [#&#8203;1699](TypeStrong/ts-node#1699)) [@&#8203;jhmaster2000](https://github.com/jhmaster2000)
-   Fixed bug where `preferTsExts` combined with third-party transpiler hooks could disrupt `nyc` code coverage ([#&#8203;1755](TypeStrong/ts-node#1755)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed bug where `file://` URLs in stack traces did not always use percent-encoding ([#&#8203;1738](TypeStrong/ts-node#1738), [#&#8203;1726](TypeStrong/ts-node#1726), [#&#8203;1729](TypeStrong/ts-node#1729)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed bug where v8-compile-cache-lib did not correctly unhook itself ([#&#8203;1717](TypeStrong/ts-node#1717), [#&#8203;1718](TypeStrong/ts-node#1718), [#&#8203;1719](TypeStrong/ts-node#1719)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   This internal dependency is used to speed up loading the TypeScript compiler

**Docs**

-   Many docs improvements ([#&#8203;1682](TypeStrong/ts-node#1682)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Options page: each option its own linkable header w/usage example ([#&#8203;1606](TypeStrong/ts-node#1606)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Categorize APIs in typedoc, make entrypoints more prominent ([#&#8203;1456](TypeStrong/ts-node#1456)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Clarify that the shorthand for `--project` is `-P`, not `-p` ([#&#8203;1731](TypeStrong/ts-node#1731), [#&#8203;1734](TypeStrong/ts-node#1734)) [@&#8203;lobsterkatie](https://github.com/lobsterkatie)
-   Add common ESM errors to Troubleshooting page ([#&#8203;1607](TypeStrong/ts-node#1607)) [@&#8203;cspotcode](https://github.com/cspotcode)

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

</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/1367
Reviewed-by: crapStone <crapstone@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
you can do this Good candidate for a pull request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants