-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement file extension resolution for CJS, same as ESM; add exhaust…
…ive tests for resolver (#1727) * WIP * restore .d.ts file * add notes and todos * strip down node-internal-modules-cjs-loader to match main branch; will build it back up from here * committing all local stuff * update * it kinda works! * fix CI? * fix? * fix? * fix * bump up to node 18 cuz why not * test matrix: replace node17 with node18 * fix node12 test failure * fix for prior hooks API * tweak * fix * teach node environment resetter to re-sort require.extensions * fix * fix * fix * windows fix? * Addressing TODOs * sync another raw file with upstream * Improve reuse / DI within node ESM stuff * cleanup node internalBinding stuff * Adding hint when ts-node is ignoring a file that you might want it to compile; addressing todos; adding a new one * Add tests for self-imports and empty package.json manifests importing a root index.* file * fix * fix node12
- Loading branch information
Showing
58 changed files
with
3,718 additions
and
911 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
/node_modules/ | ||
/tests/node_modules/ | ||
.nyc_output/ | ||
coverage/ | ||
/.nyc_output/ | ||
/coverage/ | ||
.DS_Store | ||
npm-debug.log | ||
/dist/ | ||
tsconfig.schema.json | ||
tsconfig.schemastore-schema.json | ||
.idea/ | ||
/.vscode/ | ||
/tsconfig.schema.json | ||
/tsconfig.schemastore-schema.json | ||
/.idea/ | ||
/.vscode/* | ||
!/.vscode/launch.json | ||
/website/static/api | ||
/tsconfig.tsbuildinfo | ||
/temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
*Delete this file before merging this PR* | ||
|
||
## 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. | ||
|
||
```typescript | ||
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: | ||
|
||
```typescript | ||
// 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) {...} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
*Delete this file before merging this PR* | ||
|
||
## TODOs | ||
|
||
Copy any relevant changes from `add-cjs-loader-resolve` | ||
|
||
I forgot exactly where I was in `add-cjs-loader-resolve` | ||
Re-do the renaming and moving that I did in that branch. | ||
Then diff to see that I did it correctly. | ||
Avoid introducing any accidental behavioral changes. | ||
|
||
Make list of changes from vanilla node in dist-raw/node-internal-modules-cjs-loader-old.js | ||
Apply those changes to dist-raw/node-internal-modules-cjs-loader.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copied from https://github.com/nodejs/node/blob/master/lib/internal/constants.js | ||
module.exports = { | ||
CHAR_FORWARD_SLASH: 47, /* / */ | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.