Skip to content

TS's new node ESM support #1414

Closed
Closed

Description

@cspotcode

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

Activity

  1. alvis commented on Oct 17, 2021

    @alvis

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

  2. cspotcode commented on Oct 17, 2021

    @cspotcode
    CollaboratorAuthor

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

  3. added this to the next milestone on Oct 18, 2021
  4. cspotcode commented on Oct 18, 2021

    @cspotcode
    CollaboratorAuthor

    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.

  5. PodaruDragos commented on Dec 11, 2021

    @PodaruDragos

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

  6. cspotcode commented on Dec 11, 2021

    @cspotcode
    CollaboratorAuthor

    @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

  7. modified the milestones: 10.5.0, next on Jan 31, 2022
  8. modified the milestones: next, 10.8.0 on May 17, 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

    you can do thisGood candidate for a pull request.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions