Skip to content

Commit

Permalink
feat: support gts and gjs files
Browse files Browse the repository at this point in the history
  • Loading branch information
DeclanBoller committed Sep 16, 2024
1 parent aeba914 commit d499109
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/graphql-tag-pluck/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export default function generateConfig(
case '.astro':
plugins.push('typescript', 'jsx');
break;
case '.gts':
plugins.push('typescript');
break;
case '.gjs':
plugins.push('jsx');
break;
default:
plugins.push('jsx', ...dynamicFlowPlugins);
break;
Expand Down
39 changes: 38 additions & 1 deletion packages/graphql-tag-pluck/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ const supportedExtensions = [
'.vue',
'.svelte',
'.astro',
'.gts',
'.gjs',
];

// tslint:disable-next-line: no-implicit-dependencies
Expand Down Expand Up @@ -196,6 +198,12 @@ function parseWithAstroSync(
return fileInTsx.code;
}

function parseWithGlimmer(glimmerSyntax: typeof import('@glimmer/syntax'), fileData: string) {
const ast = glimmerSyntax.preprocess(fileData);
// You may want to traverse the AST or extract specific elements
return glimmerSyntax.print(ast);
}

/**
* Asynchronously plucks GraphQL template literals from a single file.
*
Expand Down Expand Up @@ -238,7 +246,7 @@ export const gqlPluckFromCodeString = async (
/**
* Synchronously plucks GraphQL template literals from a single file
*
* Supported file extensions include: `.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.mjs`, `.cjs`, `.tsx`, `.flow`, `.flow.js`, `.flow.jsx`, `.vue`, `.svelte`, `.astro`
* Supported file extensions include: `.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.mjs`, `.cjs`, `.tsx`, `.flow`, `.flow.js`, `.flow.jsx`, `.vue`, `.svelte`, `.astro`, `.gts`, `.gjs`
*
* @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 @@ -263,6 +271,8 @@ export const gqlPluckFromCodeStringSync = (
code = pluckSvelteFileScriptSync(code);
} else if (fileExt === '.astro') {
code = pluckAstroFileScriptSync(code);
} else if (fileExt === '.gts' || fileExt === '.gjs') {
code = pluckGlimmerFileScriptSync(code);
}

const sources = parseCode({ code, filePath, options }).map(
Expand Down Expand Up @@ -359,6 +369,21 @@ const MissingAstroCompilerError = new Error(
`),
);

const MissingGlimmerCompilerError = new Error(
freeText(`
GraphQL template literals cannot be plucked from a Glimmer template code without having the "@glimmer/syntax" package installed.
Please install it and try again.
Via NPM:
$ npm install @glimmer/syntax
Via Yarn:
$ yarn add @glimmer/syntax
`),
);

async function loadVueCompilerAsync() {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -445,3 +470,15 @@ function pluckAstroFileScriptSync(fileData: string) {

return parseWithAstroSync(astroCompiler, fileData);
}

function pluckGlimmerFileScriptSync(fileData: string) {
let glimmerSyntax: typeof import('@glimmer/syntax');
try {
// eslint-disable-next-line import/no-extraneous-dependencies
glimmerSyntax = require('@glimmer/syntax');
} catch {
throw MissingGlimmerCompilerError;
}

return parseWithGlimmer(glimmerSyntax, fileData);
}
4 changes: 3 additions & 1 deletion packages/loaders/code-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const FILE_EXTENSIONS = [
'.vue',
'.svelte',
'.astro',
'.gts',
'.gjs',
];

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

0 comments on commit d499109

Please sign in to comment.