Skip to content

Commit

Permalink
Use jiti instead of tsx for importing TS and JS config files (#7261)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
enisdenjo and github-actions[bot] authored Jul 10, 2024
1 parent 1b5bcb5 commit a8e3f00
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_cli-7261-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/cli": patch
---
dependencies updates:
- Removed dependency [`tsx@^4.7.1` ↗︎](https://www.npmjs.com/package/tsx/v/4.7.1) (from `dependencies`)
6 changes: 6 additions & 0 deletions .changeset/@graphql-mesh_compose-cli-7261-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphql-mesh/compose-cli": patch
---
dependencies updates:
- Added dependency [`jiti@^1.21.6` ↗︎](https://www.npmjs.com/package/jiti/v/1.21.6) (to `dependencies`)
- Removed dependency [`tsx@^4.7.1` ↗︎](https://www.npmjs.com/package/tsx/v/4.7.1) (from `dependencies`)
6 changes: 6 additions & 0 deletions .changeset/@graphql-mesh_serve-cli-7261-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphql-mesh/serve-cli": patch
---
dependencies updates:
- Added dependency [`jiti@^1.21.6` ↗︎](https://www.npmjs.com/package/jiti/v/1.21.6) (to `dependencies`)
- Removed dependency [`tsx@^4.7.1` ↗︎](https://www.npmjs.com/package/tsx/v/4.7.1) (from `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_utils-7261-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/utils": patch
---
dependencies updates:
- Added dependency [`jiti@^1.21.6` ↗︎](https://www.npmjs.com/package/jiti/v/1.21.6) (to `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/itchy-students-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/utils': minor
---

Use jiti as the default import implementation
5 changes: 5 additions & 0 deletions .changeset/shiny-meals-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/cli': minor
---

Use jiti import from @graphql-mesh/utils instead of tsx
6 changes: 6 additions & 0 deletions .changeset/sweet-bobcats-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/compose-cli': minor
'@graphql-mesh/serve-cli': minor
---

Use jiti instead of tsx for importig config files
2 changes: 1 addition & 1 deletion e2e/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"dependencies": {
"@types/dockerode": "^3.3.28",
"dockerode": "^4.0.2",
"tsx": "^4.7.1"
"tsx": "^4.16.2"
}
}
3 changes: 1 addition & 2 deletions examples/v1-next/hive-example/gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"dependencies": {
"@graphql-mesh/serve-cli": "workspace:*",
"dotenv": "^16.4.5",
"graphql": "16.9.0",
"tsx": "4.16.2"
"graphql": "16.9.0"
}
}
3 changes: 1 addition & 2 deletions examples/v1-next/hive-example/my-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"dependencies": {
"@graphql-hive/cli": "0.38.5",
"graphql": "16.9.0",
"graphql-yoga": "5.6.0",
"tsx": "4.16.2"
"graphql-yoga": "5.6.0"
}
}
3 changes: 1 addition & 2 deletions examples/v1-next/hive-example/my-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@graphql-mesh/compose-cli": "workspace:*",
"@omnigraph/openapi": "workspace:*",
"fets": "0.8.1",
"graphql": "16.9.0",
"tsx": "4.16.2"
"graphql": "16.9.0"
}
}
2 changes: 1 addition & 1 deletion packages/compose-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@whatwg-node/fetch": "^0.9.14",
"commander": "^12.0.0",
"dotenv": "^16.3.1",
"tsx": "^4.7.1"
"jiti": "^1.21.6"
},
"publishConfig": {
"access": "public",
Expand Down
24 changes: 18 additions & 6 deletions packages/compose-cli/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'tsx/cjs'; // support importing typescript configs in CommonJS
import 'tsx/esm'; // support importing typescript configs in ESM
import 'dotenv/config'; // inject dotenv options to process.env

// eslint-disable-next-line import/no-nodejs-modules
import { promises as fsPromises } from 'fs';
// eslint-disable-next-line import/no-nodejs-modules
import { isAbsolute, join, resolve } from 'path';
import { parse } from 'graphql';
import createJITI from 'jiti';
import { Command, Option } from '@commander-js/extra-typings';
import type { Logger } from '@graphql-mesh/types';
import { DefaultLogger } from '@graphql-mesh/utils';
Expand Down Expand Up @@ -137,16 +136,29 @@ export async function run({
log.info('Done!');
}

const jiti = createJITI(
// import.meta.url is not available in CJS (and cant even be in the syntax) and __filename is not available in ESM
// instead, we dont care about the file path because we'll require config imports to have absolute paths
'',
);

async function importConfig(log: Logger, path: string): Promise<MeshComposeCLIConfig | null> {
if (!isAbsolute(path)) {
throw new Error('Configs can be imported using absolute paths only'); // see createJITI for explanation
}
try {
const importedConfigModule = await import(path);
const importedConfigModule = await jiti.import(path, {});
if (!importedConfigModule || typeof importedConfigModule !== 'object') {
throw new Error('Invalid imported config module!');
}
if ('default' in importedConfigModule) {
return importedConfigModule.default.composeConfig;
// eslint-disable-next-line dot-notation
return importedConfigModule.default['composeConfig'];
} else if ('composeConfig' in importedConfigModule) {
return importedConfigModule.composeConfig;
return importedConfigModule.composeConfig as MeshComposeCLIConfig;
}
} catch (err) {
if (err.code !== 'ERR_MODULE_NOT_FOUND') {
if (!String(err.code).includes('MODULE_NOT_FOUND')) {
log.error('Importing configuration failed!');
throw err;
}
Expand Down
1 change: 0 additions & 1 deletion packages/legacy/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"pascal-case": "^3.1.2",
"rimraf": "^6.0.0",
"tslib": "^2.4.0",
"tsx": "^4.7.1",
"typescript": "^5.4.2",
"ws": "^8.17.0",
"yargs": "^17.7.1"
Expand Down
3 changes: 0 additions & 3 deletions packages/legacy/cli/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/usr/bin/env node
import 'tsx/cjs'; // support importing typescript configs in CommonJS
import 'tsx/esm'; // support importing typescript configs in ESM

// eslint-disable-next-line import/no-extraneous-dependencies
import { DEFAULT_CLI_PARAMS, graphqlMesh, handleFatalError } from '@graphql-mesh/cli';
import { DefaultLogger } from '@graphql-mesh/utils';
Expand Down
1 change: 1 addition & 0 deletions packages/legacy/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@whatwg-node/fetch": "^0.9.13",
"disposablestack": "^1.1.6",
"dset": "^3.1.2",
"jiti": "^1.21.6",
"js-yaml": "^4.1.0",
"lodash.get": "^4.4.2",
"lodash.topath": "^4.5.2",
Expand Down
27 changes: 12 additions & 15 deletions packages/legacy/utils/src/defaultImportFn.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { path as pathModule } from '@graphql-mesh/cross-helpers';
import createJITI, { type JITI } from 'jiti';
import type { ImportFn } from '@graphql-mesh/types';

let jiti: JITI;
function getOrCreateImportFn(): ImportFn {
if (!jiti) {
// we instantiate on demand because sometimes jiti is not used
jiti = createJITI(__filename);
}
return module => jiti.import(module, {}) as Promise<any>;
}

async function defaultImportFn(path: string): Promise<any> {
let module = await import(/* @vite-ignore */ path)
.catch(e => {
if (e.code === 'ERR_REQUIRE_ESM') {
// eslint-disable-next-line no-new-func
return new Function(`return import(${JSON.stringify(path)})`)();
}
throw e;
})
.catch(e => {
if (pathModule.isAbsolute(path) && !path.endsWith('.js') && !path.endsWith('.ts')) {
return defaultImportFn(`${path}.ts`);
}
throw e;
});
let module: any = await getOrCreateImportFn()(path);
if (module.default != null) {
module = module.default;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/serve-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"@graphql-tools/utils": "^10.2.3",
"commander": "^12.0.0",
"dotenv": "^16.3.1",
"jiti": "^1.21.6",
"json-bigint-patch": "^0.0.8",
"spinnies": "^0.5.1",
"tsx": "^4.7.1"
"spinnies": "^0.5.1"
},
"optionalDependencies": {
"node-libcurl": "^4.0.0",
Expand Down
25 changes: 19 additions & 6 deletions packages/serve-cli/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'json-bigint-patch'; // JSON.parse/stringify with bigints support
import 'tsx/cjs'; // support importing typescript configs in CommonJS
import 'tsx/esm'; // support importing typescript configs in ESM
import 'dotenv/config'; // inject dotenv options to process.env

// eslint-disable-next-line import/no-nodejs-modules
Expand All @@ -9,6 +7,8 @@ import cluster from 'cluster';
import { availableParallelism, release } from 'os';
// eslint-disable-next-line import/no-nodejs-modules
import { dirname, isAbsolute, resolve } from 'path';
import createJITI from 'jiti';
// import { tsImport } from 'tsx/dist/esm/api/index.mjs';
import { Command, InvalidArgumentError, Option } from '@commander-js/extra-typings';
import type { UnifiedGraphConfig } from '@graphql-mesh/serve-runtime';
import { createServeRuntime } from '@graphql-mesh/serve-runtime';
Expand Down Expand Up @@ -253,16 +253,29 @@ export async function run({
terminateStack.use(server);
}

const jiti = createJITI(
// import.meta.url is not available in CJS (and cant even be in the syntax) and __filename is not available in ESM
// instead, we dont care about the file path because we'll require config imports to have absolute paths
'',
);

async function importConfig(log: Logger, path: string): Promise<MeshServeCLIConfig | null> {
if (!isAbsolute(path)) {
throw new Error('Configs can be imported using absolute paths only'); // see createJITI for explanation
}
try {
const importedConfigModule = await import(path);
const importedConfigModule = await jiti.import(path, {});
if (!importedConfigModule || typeof importedConfigModule !== 'object') {
throw new Error('Invalid imported config module!');
}
if ('default' in importedConfigModule) {
return importedConfigModule.default.serveConfig;
// eslint-disable-next-line dot-notation
return importedConfigModule.default['serveConfig'];
} else if ('serveConfig' in importedConfigModule) {
return importedConfigModule.serveConfig;
return importedConfigModule.serveConfig as MeshServeCLIConfig;
}
} catch (err) {
if (err.code !== 'ERR_MODULE_NOT_FOUND') {
if (!String(err.code).includes('MODULE_NOT_FOUND')) {
log.error('Importing configuration failed!');
throw err;
}
Expand Down
31 changes: 6 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3824,7 +3824,7 @@ __metadata:
dependencies:
"@types/dockerode": "npm:^3.3.28"
dockerode: "npm:^4.0.2"
tsx: "npm:^4.7.1"
tsx: "npm:^4.16.2"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -4291,7 +4291,6 @@ __metadata:
"@graphql-mesh/serve-cli": "workspace:*"
dotenv: "npm:^16.4.5"
graphql: "npm:16.9.0"
tsx: "npm:4.16.2"
languageName: unknown
linkType: soft

Expand All @@ -4302,7 +4301,6 @@ __metadata:
"@graphql-hive/cli": "npm:0.38.5"
graphql: "npm:16.9.0"
graphql-yoga: "npm:5.6.0"
tsx: "npm:4.16.2"
languageName: unknown
linkType: soft

Expand All @@ -4315,7 +4313,6 @@ __metadata:
"@omnigraph/openapi": "workspace:*"
fets: "npm:0.8.1"
graphql: "npm:16.9.0"
tsx: "npm:4.16.2"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -4821,7 +4818,6 @@ __metadata:
pascal-case: "npm:^3.1.2"
rimraf: "npm:^6.0.0"
tslib: "npm:^2.4.0"
tsx: "npm:^4.7.1"
typescript: "npm:^5.4.2"
uWebSockets.js: "uNetworking/uWebSockets.js#semver:^20"
ws: "npm:^8.17.0"
Expand Down Expand Up @@ -4855,7 +4851,7 @@ __metadata:
"@whatwg-node/fetch": "npm:^0.9.14"
commander: "npm:^12.0.0"
dotenv: "npm:^16.3.1"
tsx: "npm:^4.7.1"
jiti: "npm:^1.21.6"
peerDependencies:
"@graphql-mesh/types": ^0.98.10
graphql: "*"
Expand Down Expand Up @@ -5504,10 +5500,10 @@ __metadata:
"@types/spinnies": "npm:^0.5.3"
commander: "npm:^12.0.0"
dotenv: "npm:^16.3.1"
jiti: "npm:^1.21.6"
json-bigint-patch: "npm:^0.0.8"
node-libcurl: "npm:^4.0.0"
spinnies: "npm:^0.5.1"
tsx: "npm:^4.7.1"
uWebSockets.js: "uNetworking/uWebSockets.js#semver:^20"
peerDependencies:
"@parcel/watcher": ^2.1.0
Expand Down Expand Up @@ -6097,6 +6093,7 @@ __metadata:
"@whatwg-node/fetch": "npm:^0.9.13"
disposablestack: "npm:^1.1.6"
dset: "npm:^3.1.2"
jiti: "npm:^1.21.6"
js-yaml: "npm:^4.1.0"
lodash.get: "npm:^4.4.2"
lodash.topath: "npm:^4.5.2"
Expand Down Expand Up @@ -23034,7 +23031,7 @@ __metadata:
languageName: node
linkType: hard

"jiti@npm:^1.21.0":
"jiti@npm:^1.21.0, jiti@npm:^1.21.6":
version: 1.21.6
resolution: "jiti@npm:1.21.6"
bin:
Expand Down Expand Up @@ -34057,7 +34054,7 @@ __metadata:
languageName: node
linkType: hard

"tsx@npm:4.16.2":
"tsx@npm:^4.16.2":
version: 4.16.2
resolution: "tsx@npm:4.16.2"
dependencies:
Expand All @@ -34073,22 +34070,6 @@ __metadata:
languageName: node
linkType: hard

"tsx@npm:^4.7.1":
version: 4.15.8
resolution: "tsx@npm:4.15.8"
dependencies:
esbuild: "npm:~0.21.4"
fsevents: "npm:~2.3.3"
get-tsconfig: "npm:^4.7.5"
dependenciesMeta:
fsevents:
optional: true
bin:
tsx: dist/cli.mjs
checksum: 10c0/2e4e077e05e9ec324246f30be0b61e85c5a9c61e4dca160eec542eee410cc0fbcccf26f4d529a8d977d470dd0e13f6897cee5d5ebc3cbf6c2cdfbc5e4033b26c
languageName: node
linkType: hard

"tunnel-agent@npm:^0.6.0":
version: 0.6.0
resolution: "tunnel-agent@npm:0.6.0"
Expand Down

0 comments on commit a8e3f00

Please sign in to comment.