Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency cleanup #6896

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/nasty-peas-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphql-tools/graphql-file-loader': patch
'@graphql-tools/code-file-loader': patch
'@graphql-tools/json-file-loader': patch
'@graphql-tools/load-files': patch
'@graphql-tools/documents': patch
'@graphql-tools/load': patch
---

Replace globby with tinyglobby and remove lodash.sortby dependency
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"babel-jest": "29.7.0",
"bob-the-bundler": "7.0.1",
"bun": "^1.1.43",
"chalk": "5.4.1",
"concurrently": "9.1.2",
"cross-env": "7.0.3",
"eslint": "9.19.0",
Expand All @@ -69,14 +68,14 @@
"eslint-plugin-n": "17.15.1",
"eslint-plugin-promise": "7.2.1",
"eslint-plugin-standard": "5.0.0",
"globby": "11.1.0",
"graphql": "16.10.0",
"husky": "9.1.7",
"jest": "29.7.0",
"lint-staged": "15.4.3",
"patch-package": "8.0.0",
"prettier": "3.4.2",
"prettier-plugin-tailwindcss": "0.6.11",
"tinyglobby": "^0.2.10",
"ts-jest": "29.2.5",
"tsx": "4.19.2",
"typedoc": "0.25.13",
Expand Down
5 changes: 1 addition & 4 deletions packages/documents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
},
"dependencies": {
"lodash.sortby": "^4.7.0",
"tslib": "^2.4.0"
},
"devDependencies": {
"@types/lodash.sortby": "^4.7.7"
},
"devDependencies": {},
talentlessguy marked this conversation as resolved.
Show resolved Hide resolved
"publishConfig": {
"directory": "dist",
"access": "public"
Expand Down
50 changes: 32 additions & 18 deletions packages/documents/src/sort-executable-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
type SelectionNode,
type VariableDefinitionNode,
} from 'graphql';
import sortBy from 'lodash.sortby';
import { normalizeWhiteSpace } from './normalize-whitespace.js';

// Cache the sorted nodes to avoid sorting the same nodes multiple times
Expand Down Expand Up @@ -44,39 +43,54 @@ export function sortExecutableNodes(
}

if (isOfKindList<DirectiveNode>(nodes, Kind.DIRECTIVE)) {
return cacheResult(sortBy(nodes as any, 'name.value'));
return cacheResult(nodes.sort((a, b) => a.name.value.localeCompare(b.name.value)));
}

if (isOfKindList<VariableDefinitionNode>(nodes, Kind.VARIABLE_DEFINITION)) {
return cacheResult(sortBy(nodes as any, 'variable.name.value'));
return cacheResult(
nodes.sort((a, b) => a.variable.name.value.localeCompare(b.variable.name.value)),
);
}

if (isOfKindList<ArgumentNode>(nodes, Kind.ARGUMENT)) {
return cacheResult(sortBy(nodes as any, 'name.value'));
return cacheResult(nodes.sort((a, b) => a.name.value.localeCompare(b.name.value)));
}

if (
isOfKindList<SelectionNode>(nodes, [Kind.FIELD, Kind.FRAGMENT_SPREAD, Kind.INLINE_FRAGMENT])
) {
return cacheResult(
sortBy(nodes as any, node => {
if (node.kind === Kind.FIELD) {
return sortPrefixField + node.name.value;
} else if (node.kind === Kind.FRAGMENT_SPREAD) {
return sortPrefixFragmentSpread + node.name.value;
} else {
const typeCondition = node.typeCondition?.name.value ?? '';
// if you have a better idea, send a PR :)
const sortedNodes = buildInlineFragmentSelectionSetKey(
cacheResult(sortExecutableNodes(node.selectionSet.selections)),
);
return sortPrefixInlineFragmentNode + typeCondition + sortedNodes;
}
nodes.sort((a, b) => {
const getSortKey = (node: SelectionNode) => {
if (node.kind === Kind.FIELD) {
return sortPrefixField + node.name.value;
} else if (node.kind === Kind.FRAGMENT_SPREAD) {
return sortPrefixFragmentSpread + node.name.value;
} else {
const typeCondition = node.typeCondition?.name.value ?? '';
const sortedSelections = buildInlineFragmentSelectionSetKey(
cacheResult(sortExecutableNodes(node.selectionSet.selections)),
);
return sortPrefixInlineFragmentNode + typeCondition + sortedSelections;
}
};

return getSortKey(a).localeCompare(getSortKey(b));
}),
);
}
const _nodes = nodes as ASTNode[];
return cacheResult(
_nodes.sort((nodeA, nodeB) => {
const kindComparison = nodeA.kind.localeCompare(nodeB.kind);
if (kindComparison !== 0) return kindComparison;

const nameA = (nodeA as any).name?.value ?? '';
const nameB = (nodeB as any).name?.value ?? '';

return cacheResult(sortBy(nodes as any, 'kind', 'name.value'));
return nameA.localeCompare(nameB);
}),
);
}
Comment on lines +82 to 94
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do I need to add comments here or it's fine as it is?

}

Expand Down
2 changes: 1 addition & 1 deletion packages/load-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
},
"dependencies": {
"globby": "11.1.0",
"tinyglobby": "^0.2.10",
"tslib": "^2.4.0",
"unixify": "^1.0.0"
},
Expand Down
28 changes: 18 additions & 10 deletions packages/load-files/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import { promises as fsPromises, readFileSync, statSync } from 'fs';
import { createRequire } from 'module';
import { extname, join } from 'path';
import { cwd } from 'process';
import globby, { GlobbyOptions, sync as globbySync } from 'globby';
import { DocumentNode, parse } from 'graphql';
import { glob, GlobOptions, globSync } from 'tinyglobby';
import unixify from 'unixify';

const { readFile, stat } = fsPromises;

function unixifyWithDriveLetter(path: string): string {
if (path.match(/^[A-Z]:\\/)) {
const driveLetter = path[0];
return `${driveLetter}:${unixify(path)}`;
}
return unixify(path);
}

talentlessguy marked this conversation as resolved.
Show resolved Hide resolved
const DEFAULT_IGNORED_EXTENSIONS = ['spec', 'test', 'd', 'map'];
const DEFAULT_EXTENSIONS = ['gql', 'graphql', 'graphqls', 'ts', 'js'];
const DEFAULT_EXPORT_NAMES = ['schema', 'typeDef', 'typeDefs', 'resolver', 'resolvers'];
Expand Down Expand Up @@ -63,8 +71,8 @@ async function isDirectory(path: string) {
}
}

function scanForFilesSync(globStr: string | string[], globOptions: GlobbyOptions = {}): string[] {
return globbySync(globStr, { absolute: true, ...globOptions });
function scanForFilesSync(globStr: string | string[], globOptions: GlobOptions = {}): string[] {
return globSync(globStr, { absolute: true, ...globOptions });
}

function formatExtension(extension: string): string {
Expand Down Expand Up @@ -99,7 +107,7 @@ export interface LoadFilesOptions {
// An alternative to `require` to use if `require` would be used to load a file
requireMethod?: any;
// Additional options to pass to globby
globOptions?: GlobbyOptions;
globOptions?: GlobOptions;
// Named exports to extract from each file. Defaults to ['typeDefs', 'schema']
exportNames?: string[];
// Load files from nested directories. Set to `false` to only search the top-level directory.
Expand Down Expand Up @@ -138,12 +146,12 @@ export function loadFilesSync<T = any>(
asArray(pattern).map(path =>
isDirectorySync(path)
? buildGlob(
unixify(path),
unixifyWithDriveLetter(path),
execOptions.extensions,
execOptions.ignoredExtensions,
execOptions.recursive,
)
: unixify(path),
: unixifyWithDriveLetter(path),
),
options.globOptions,
);
Expand Down Expand Up @@ -183,9 +191,9 @@ export function loadFilesSync<T = any>(

async function scanForFiles(
globStr: string | string[],
globOptions: GlobbyOptions = {},
globOptions: GlobOptions = {},
): Promise<string[]> {
return globby(globStr, { absolute: true, ...globOptions });
return glob(globStr, { absolute: true, ...globOptions });
}

const checkExtension = (
Expand Down Expand Up @@ -237,12 +245,12 @@ export async function loadFiles(
asArray(pattern).map(async path =>
(await isDirectory(path))
? buildGlob(
unixify(path),
unixifyWithDriveLetter(path),
execOptions.extensions,
execOptions.ignoredExtensions,
execOptions.recursive,
)
: unixify(path),
: unixifyWithDriveLetter(path),
),
),
options.globOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { loadDocuments, loadDocumentsSync } from '@graphql-tools/load';
import { runTests } from '../../../../testing/utils.js';
import '../../../../testing/to-be-similar-string';
import { readFileSync } from 'fs';
import globby from 'globby';
import { globSync } from 'tinyglobby';
import { removeLoc } from '@graphql-tools/optimize';

describe('documentsFromGlob', () => {
Expand All @@ -20,7 +20,7 @@ describe('documentsFromGlob', () => {
loaders: [new GraphQLFileLoader()],
});
expect(result).toHaveLength(1);
const expectedFiles = globby.sync(glob);
const expectedFiles = globSync(glob);
for (const expectedFileName of expectedFiles) {
const fileNameResult = result?.find(({ location }) => location === expectedFileName);
if (fileNameResult) {
Expand All @@ -38,7 +38,7 @@ describe('documentsFromGlob', () => {
loaders: [new GraphQLFileLoader()],
});
expect(result).toHaveLength(2);
const expectedFiles = globby.sync(glob);
const expectedFiles = globSync(glob);
for (const expectedFileName of expectedFiles) {
const fileNameResult = result?.find(({ location }) => location === expectedFileName);
if (fileNameResult) {
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/code-file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"dependencies": {
"@graphql-tools/graphql-tag-pluck": "8.3.12",
"@graphql-tools/utils": "^10.7.2",
"globby": "^11.0.3",
"tinyglobby": "^0.2.10",
"tslib": "^2.4.0",
"unixify": "^1.0.0"
},
Expand Down
26 changes: 18 additions & 8 deletions packages/loaders/code-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { existsSync, promises as fsPromises, readFileSync } from 'fs';
import { createRequire } from 'module';
import { isAbsolute, resolve } from 'path';
import { cwd, env } from 'process';
import type { GlobbyOptions } from 'globby';
import globby from 'globby';
import { DocumentNode, GraphQLSchema, isSchema, parse } from 'graphql';
import { glob, globSync, type GlobOptions } from 'tinyglobby';
import unixify from 'unixify';
import {
gqlPluckFromCodeString,
Expand All @@ -24,6 +23,14 @@ import { tryToLoadFromExport, tryToLoadFromExportSync } from './load-from-module

const { readFile, access } = fsPromises;

function unixifyWithDriveLetter(path: string): string {
if (path.match(/^[A-Z]:\\/)) {
const driveLetter = path[0];
return `${driveLetter}:${unixify(path)}`;
}
return unixify(path);
}

talentlessguy marked this conversation as resolved.
Show resolved Hide resolved
export type CodeFileLoaderConfig = {
pluckConfig?: GraphQLTagPluckOptions;
noPluck?: boolean;
Expand Down Expand Up @@ -59,7 +66,7 @@ const FILE_EXTENSIONS = [
'.gjs',
];

function createGlobbyOptions(options: CodeFileLoaderOptions): GlobbyOptions {
function createGlobbyOptions(options: CodeFileLoaderOptions): GlobOptions {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure the options are the same? Otherwise it is a breaking change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some options are different, so I think I better rename it to TinyGlobbyOptions

return { absolute: true, ...options, ignore: [] };
}

Expand Down Expand Up @@ -131,20 +138,23 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {

private _buildGlobs(glob: string, options: CodeFileLoaderOptions) {
const ignores = asArray(options.ignore || []);
const globs = [unixify(glob), ...ignores.map(v => buildIgnoreGlob(unixify(v)))];
const globs = [
unixifyWithDriveLetter(glob),
...ignores.map(v => buildIgnoreGlob(unixifyWithDriveLetter(v))),
];
return globs;
}

async resolveGlobs(glob: string, options: CodeFileLoaderOptions) {
async resolveGlobs(path: string, options: CodeFileLoaderOptions) {
options = this.getMergedOptions(options);
const globs = this._buildGlobs(glob, options);
return globby(globs, createGlobbyOptions(options));
const globs = this._buildGlobs(path, options);
return glob(globs, createGlobbyOptions(options));
}

resolveGlobsSync(glob: string, options: CodeFileLoaderOptions) {
options = this.getMergedOptions(options);
const globs = this._buildGlobs(glob, options);
return globby.sync(globs, createGlobbyOptions(options));
return globSync(globs, createGlobbyOptions(options));
}

async load(pointer: string, options: CodeFileLoaderOptions): Promise<Source[]> {
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/graphql-file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"dependencies": {
"@graphql-tools/import": "7.0.11",
"@graphql-tools/utils": "^10.7.2",
"globby": "^11.0.3",
"tinyglobby": "^0.2.10",
"tslib": "^2.4.0",
"unixify": "^1.0.0"
},
Expand Down
Loading
Loading