Files
f76bdf0954 Support .mts, .cts, nodenext, and node16 (#1694)
* feat: support .mts .cts

* stuff

* WIP with TODOs

* WIP

* lint-fix

* WIP

* WIP

* WIP

* fixes

* flatten resolver tests by splitting into more  functions

* address extension-handling code

* lint-fix

* Fix; update resolver tests to test more things

* fix

* fix

* test against TS rc on nodes 14, 16, 18 (good sanity-checking with these new nodenext features)

* Teach ts.transpileModule to handle NodeNext correctly

* tweak tests

* fix test typos; update pluggable dep tests; update ignore() tests for new file extensions

* fix tests

* fix

* fix build against stable ts, no need for 4.7.0 just yet

* Gate nodenext/node16 tests behind a TS version check; fix TSCommon types

* Fix nyc require.extensions issues

* lint-fix

* tricky types

* skip nodenext tests on older TS

* fix tests

* fix bug and tests

* fix windows

* turn off another test on ancient TS versions

* fix windows

* fix allowing `moduleTypeOverrides` to override cts/cjs

* Update moduleTypes docs

* add mts and cts awareness to internal/external classifier; add .d.cts/.d.mts extensions to tests

* cleanup misc markdown files

* more markdown cleanup

Co-authored-by: bluelovers <[email protected]>
2022-05-18 00:16:56 -04:00

1.7 KiB

Yarn PnP interop

Asked about it here: https://discord.com/channels/226791405589233664/654372321225605128/957301175609344070

PnP API checks if import specifiers are for dependencies: non-relative, non-absolute libfoo @scope/libfoo

When they are, it does resolveToUnqualified to map to an unqualified path. This path points to the module's location on disk (in a zip, perhaps) but does not handle file extension resolution or stuff like that.

To interop with PnP, we need PnP to only resolveToUnqualified. We do everything else.

import { Module } from 'module';
import fs from 'fs';

const pathRegExp = /^(?![a-zA-Z]:[\\/]|\\\\|\.{0,2}(?:\/|$))((?:@[^/]+\/)?[^/]+)\/*(.*|)$/;

const originalModuleResolveFilename = Module._resolveFilename;
Module._resolveFilename = function (
    request: string,
    parent: typeof Module | null | undefined,
    isMain: boolean,
    options?: { [key: string]: any }
) {
    const dependencyNameMatch = request.match(pathRegExp);
    if (dependencyNameMatch !== null) {

      const [, dependencyName, subPath] = dependencyNameMatch;

      const unqualified = pnpapi.resolveToUnqualified(....);

      // Do your modified resolution on the unqualified path here

    } else {

      // Do your modified resolution here; no need for PnP

    }

};

PnP can be installed at runtime.

To conditionally check if PnP is available at the start of every resolution:

// Get the pnpapi of either the issuer or the specifier.
// The latter is required when the specifier is an absolute path to a
// zip file and the issuer doesn't belong to a pnpapi
const {findPnPApi} = Module;
const pnpapi = findPnPApi ? (findPnpApi(issuer) ?? (url ? findPnpApi(specifier) : null)) : null;
if (pnpapi) {...}