Skip to content

Commit

Permalink
fix(legacy/utils,legacy/cli): no jiti in utils, but use it as default…
Browse files Browse the repository at this point in the history
… JS importer in cli (#7360)

* no jiti utils

* chore(dependencies): updated changesets for modified dependencies

* changewset

* jiti in cli as defaultImportFn

* changeset

* chore(dependencies): updated changesets for modified dependencies

* use .js

* cli defaultImportFn gets default

* create jiti on-demand

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
enisdenjo and github-actions[bot] authored Jul 24, 2024
1 parent 6bbe249 commit 5e5dec5
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_cli-7360-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/cli": 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/@graphql-mesh_utils-7360-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/utils": patch
---
dependencies updates:
- Removed dependency [`jiti@^1.21.6` ↗︎](https://www.npmjs.com/package/jiti/v/1.21.6) (from `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/two-geese-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/utils': patch
---

No jiti, keep the utils JS env agnostic
5 changes: 5 additions & 0 deletions .changeset/young-tips-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/cli': patch
---

Use jiti as default JS importer
1 change: 1 addition & 0 deletions packages/legacy/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"dotenv": "^16.0.3",
"graphql-import-node": "^0.0.5",
"graphql-ws": "^5.12.1",
"jiti": "^1.21.6",
"json-bigint-patch": "^0.0.8",
"json5": "^2.2.3",
"mkdirp": "^3.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/legacy/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { processConfig } from '@graphql-mesh/config';
import { path, process } from '@graphql-mesh/cross-helpers';
import type { YamlConfig } from '@graphql-mesh/types';
import { jsonSchema } from '@graphql-mesh/types';
import { defaultImportFn, DefaultLogger, loadYaml } from '@graphql-mesh/utils';
import { DefaultLogger, loadYaml } from '@graphql-mesh/utils';
import { defaultImportFn } from './defaultImportFn.js';

export function validateConfig(
config: any,
Expand Down
29 changes: 29 additions & 0 deletions packages/legacy/cli/src/defaultImportFn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 id => jiti.import(id, {}) as Promise<any>;
}

export const defaultImportFn: ImportFn = async id => {
let module: any = await getOrCreateImportFn()(id);
if (module.default != null) {
module = module.default;
}
if (typeof module === 'object' && module != null) {
const prototypeOfObject = Object.getPrototypeOf(module);
if (prototypeOfObject == null || prototypeOfObject === Object.prototype) {
const normalizedVal: Record<string, any> = {};
for (const key in module) {
normalizedVal[key] = module[key];
}
return normalizedVal;
}
}
return module;
};
2 changes: 1 addition & 1 deletion packages/legacy/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { getMesh } from '@graphql-mesh/runtime';
import { FsStoreStorageAdapter, MeshStore } from '@graphql-mesh/store';
import type { Logger, YamlConfig } from '@graphql-mesh/types';
import {
defaultImportFn,
DefaultLogger,
pathExists,
registerTerminateHandler,
Expand All @@ -18,6 +17,7 @@ import { printSchemaWithDirectives } from '@graphql-tools/utils';
import { serveMesh } from './commands/serve/serve.js';
import { generateTsArtifacts } from './commands/ts-artifacts.js';
import { findAndParseConfig } from './config.js';
import { defaultImportFn } from './defaultImportFn.js';
import { handleFatalError } from './handleFatalError.js';

export { generateTsArtifacts, serveMesh, findAndParseConfig, handleFatalError };
Expand Down
1 change: 0 additions & 1 deletion packages/legacy/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@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: 15 additions & 12 deletions packages/legacy/utils/src/defaultImportFn.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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>;
}
import { path as pathModule } from '@graphql-mesh/cross-helpers';

async function defaultImportFn(path: string): Promise<any> {
let module: any = await getOrCreateImportFn()(path);
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;
});
if (module.default != null) {
module = module.default;
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4828,6 +4828,7 @@ __metadata:
dotenv: "npm:^16.0.3"
graphql-import-node: "npm:^0.0.5"
graphql-ws: "npm:^5.12.1"
jiti: "npm:^1.21.6"
json-bigint-patch: "npm:^0.0.8"
json5: "npm:^2.2.3"
mkdirp: "npm:^3.0.0"
Expand Down Expand Up @@ -6145,7 +6146,6 @@ __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

0 comments on commit 5e5dec5

Please sign in to comment.