Skip to content

Commit

Permalink
fix: enable cts and mts file extensions for graphql-tag-pluck (#5030)
Browse files Browse the repository at this point in the history
* fix: enable cts and mts file extensions for graphql-tag-pluck

* chore: add a changelog
  • Loading branch information
mscharley committed Feb 7, 2023
1 parent ab4cf86 commit bb2d257
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-ravens-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-tools/graphql-tag-pluck': patch
'@graphql-tools/code-file-loader': patch
---

Added the ability to parse cjs/mjs files for graphql documents
4 changes: 4 additions & 0 deletions packages/graphql-tag-pluck/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export default function generateConfig(
const fileExt = getExtNameFromFilePath(filePath);
switch (fileExt) {
case '.ts':
case '.cts':
case '.mts':
plugins.push('typescript');
plugins.push('importAssertions');
break;
Expand All @@ -52,6 +54,8 @@ export default function generateConfig(
// (unlike .tsx) and because people are seem to use it with regular file extensions
// (e.g. .js) see https://github.com/dotansimha/graphql-code-generator/issues/1967
case '.js':
case '.mjs':
case '.cjs':
plugins.push('jsx', ...dynamicFlowPlugins);
break;
case '.jsx':
Expand Down
20 changes: 17 additions & 3 deletions packages/graphql-tag-pluck/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,21 @@ export interface GraphQLTagPluckOptions {
skipIndent?: boolean;
}

const supportedExtensions = ['.js', '.jsx', '.ts', '.tsx', '.flow', '.flow.js', '.flow.jsx', '.vue', '.svelte'];
const supportedExtensions = [
'.js',
'.mjs',
'.cjs',
'.jsx',
'.ts',
'.mts',
'.cts',
'.tsx',
'.flow',
'.flow.js',
'.flow.jsx',
'.vue',
'.svelte',
];

// tslint:disable-next-line: no-implicit-dependencies
function parseWithVue(vueTemplateCompiler: typeof import('@vue/compiler-sfc'), fileData: string) {
Expand All @@ -131,7 +145,7 @@ function parseWithSvelte(svelte2tsx: typeof import('svelte2tsx'), fileData: stri
/**
* Asynchronously plucks GraphQL template literals from a single file.
*
* Supported file extensions include: `.js`, `.jsx`, `.ts`, `.tsx`, `.flow`, `.flow.js`, `.flow.jsx`, `.vue`, `.svelte`
* Supported file extensions include: `.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.mts`, `.cts`, `.tsx`, `.flow`, `.flow.js`, `.flow.jsx`, `.vue`, `.svelte`
*
* @param filePath Path to the file containing the code. Required to detect the file type
* @param code The contents of the file being parsed.
Expand All @@ -157,7 +171,7 @@ export const gqlPluckFromCodeString = async (
/**
* Synchronously plucks GraphQL template literals from a single file
*
* Supported file extensions include: `.js`, `.jsx`, `.ts`, `.tsx`, `.flow`, `.flow.js`, `.flow.jsx`, `.vue`, `.svelte`
* Supported file extensions include: `.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.mjs`, `.cjs`, `.tsx`, `.flow`, `.flow.js`, `.flow.jsx`, `.vue`, `.svelte`
*
* @param filePath Path to the file containing the code. Required to detect the file type
* @param code The contents of the file being parsed.
Expand Down
5 changes: 3 additions & 2 deletions packages/loaders/code-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type CodeFileLoaderOptions = {
} & CodeFileLoaderConfig &
BaseLoaderOptions;

const FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.vue', '.svelte'];
const FILE_EXTENSIONS = ['.ts', '.mts', '.cts', '.tsx', '.js', '.mjs', 'cjs', '.jsx', '.vue', '.svelte'];

function createGlobbyOptions(options: CodeFileLoaderOptions): GlobbyOptions {
return { absolute: true, ...options, ignore: [] };
Expand All @@ -65,7 +65,8 @@ const buildIgnoreGlob = (path: string) => `!${path}`;
* });
* ```
*
* Supported extensions include: `.ts`, `.tsx`, `.js`, `.jsx`, `.vue`, `.svelte`
* Supported extensions include: `.ts`, `.mts`, `.cts`, `.tsx`, `.js`, `.mjs`,
* `.cjs`, `.jsx`, `.vue`, `.svelte`
*/
export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
private config: CodeFileLoaderConfig;
Expand Down

0 comments on commit bb2d257

Please sign in to comment.