-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom module resolution with our own hooks (#7596)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0a3e52c
commit b7f6ebf
Showing
33 changed files
with
614 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@graphql-mesh/cache-redis": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@whatwg-node/disposablestack@^0.0.5` ↗︎](https://www.npmjs.com/package/@whatwg-node/disposablestack/v/0.0.5) (from `^0.0.4`, in `dependencies`) |
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,5 @@ | ||
--- | ||
"@graphql-mesh/fusion-runtime": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@whatwg-node/disposablestack@^0.0.5` ↗︎](https://www.npmjs.com/package/@whatwg-node/disposablestack/v/0.0.5) (from `^0.0.4`, in `dependencies`) |
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,5 @@ | ||
--- | ||
"@graphql-mesh/include": patch | ||
--- | ||
dependencies updates: | ||
- Added dependency [`sucrase@^3.35.0` ↗︎](https://www.npmjs.com/package/sucrase/v/3.35.0) (to `dependencies`) |
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,5 @@ | ||
--- | ||
"@graphql-mesh/serve-runtime": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@whatwg-node/disposablestack@^0.0.5` ↗︎](https://www.npmjs.com/package/@whatwg-node/disposablestack/v/0.0.5) (from `^0.0.4`, in `dependencies`) |
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,5 @@ | ||
--- | ||
"@graphql-mesh/utils": patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@whatwg-node/disposablestack@^0.0.5` ↗︎](https://www.npmjs.com/package/@whatwg-node/disposablestack/v/0.0.5) (from `^0.0.4`, in `dependencies`) |
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,5 @@ | ||
--- | ||
'@graphql-mesh/include': minor | ||
--- | ||
|
||
Module hooks using sucrase transpiling only TS with tsconfig paths support |
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,5 @@ | ||
--- | ||
'@graphql-mesh/include': minor | ||
--- | ||
|
||
No more @graphql-mesh/include/register-tsconfig-paths |
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,6 @@ | ||
--- | ||
'@graphql-mesh/compose-cli': minor | ||
'@graphql-mesh/serve-cli': minor | ||
--- | ||
|
||
Register @graphql-mesh/include/hooks instead of using jiti and include |
File renamed without changes.
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 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 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 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 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 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,124 @@ | ||
/* eslint-disable import/no-nodejs-modules */ | ||
// ONLY FOR NODE. register with `node --import @graphql-mesh/include/hooks <your script>` | ||
|
||
import fs from 'node:fs/promises'; | ||
import module from 'node:module'; | ||
import path from 'node:path'; | ||
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'; | ||
import { transform } from 'sucrase'; | ||
|
||
const isDebug = ['1', 'y', 'yes', 't', 'true'].includes(String(process.env.DEBUG)); | ||
|
||
function debug(msg: string) { | ||
if (isDebug) { | ||
process.stderr.write(`[${new Date().toISOString()}] HOOKS ${msg}\n`); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line dot-notation | ||
const resolveFilename: (path: string) => string = module['_resolveFilename']; | ||
|
||
let pathsMatcher: ((specifier: string) => string[]) | undefined; | ||
|
||
let packedDepsPath = ''; | ||
|
||
export interface InitializeData { | ||
/** | ||
* Packed deps will be checked first, and enforced if present, during module resolution. | ||
* This allows us to consistently use the same module instance even if multiple are installed by the user. | ||
*/ | ||
packedDepsPath?: string; | ||
/** | ||
* tsconfig search path for registering tsconfig paths. | ||
* | ||
* @default process.env.MESH_INCLUDE_TSCONFIG_SEARCH_PATH || 'tsconfig.json' | ||
*/ | ||
tsconfigSearchPath?: string; | ||
} | ||
|
||
export const initialize: module.InitializeHook<InitializeData> = (data = {}) => { | ||
if (data.packedDepsPath) { | ||
packedDepsPath = data.packedDepsPath; | ||
debug(`Packed dependencies available at "${packedDepsPath}"`); | ||
} | ||
const tsconfig = getTsconfig( | ||
undefined, | ||
data.tsconfigSearchPath || process.env.MESH_INCLUDE_TSCONFIG_SEARCH_PATH || 'tsconfig.json', | ||
); | ||
if (tsconfig) { | ||
debug(`tsconfig found at "${tsconfig.path}"`); | ||
pathsMatcher = createPathsMatcher(tsconfig); | ||
} | ||
}; | ||
|
||
export const resolve: module.ResolveHook = async (specifier, context, nextResolve) => { | ||
if (packedDepsPath) { | ||
try { | ||
const resolved = await nextResolve( | ||
resolveFilename(path.join(packedDepsPath, specifier)), | ||
context, | ||
); | ||
debug(`Using packed dependency "${specifier}" from "${packedDepsPath}"`); | ||
return resolved; | ||
} catch { | ||
// noop | ||
} | ||
} | ||
|
||
try { | ||
return await nextResolve(specifier, context); | ||
} catch (e) { | ||
// default resolve failed, try alternatives | ||
try { | ||
return await nextResolve(resolveFilename(specifier), context); | ||
} catch { | ||
try { | ||
// usual filenames tried, could be a .ts file? | ||
return await nextResolve( | ||
resolveFilename( | ||
specifier + '.ts', // TODO: .mts or .cts? | ||
), | ||
context, | ||
); | ||
} catch { | ||
// not a .ts file, try the tsconfig paths if available | ||
if (pathsMatcher) { | ||
for (const possiblePath of pathsMatcher(specifier)) { | ||
try { | ||
return await nextResolve(resolveFilename(possiblePath), context); | ||
} catch { | ||
try { | ||
// the tsconfig path might point to a .ts file, try it too | ||
return await nextResolve( | ||
resolveFilename( | ||
possiblePath + '.ts', // TODO: .mts or .cts? | ||
), | ||
context, | ||
); | ||
} catch { | ||
// noop | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// none of the alternatives worked, fail with original error | ||
throw e; | ||
} | ||
}; | ||
|
||
export const load: module.LoadHook = async (url, context, nextLoad) => { | ||
if (/\.(m|c)?ts$/.test(url)) { | ||
debug(`Transpiling TypeScript file at "${url}"`); | ||
const source = await fs.readFile(new URL(url), 'utf8'); | ||
const { code } = transform(source, { transforms: ['typescript'] }); | ||
return { | ||
format: /\.cts$/.test(url) ? 'commonjs' : 'module', // TODO: ".ts" files _might_ not always be esm | ||
source: code, | ||
shortCircuit: true, | ||
}; | ||
} | ||
return nextLoad(url, context); | ||
}; |
Oops, something went wrong.