diff --git a/packages/gatsby-operations/package.json b/packages/gatsby-operations/package.json new file mode 100644 index 000000000..0a1703970 --- /dev/null +++ b/packages/gatsby-operations/package.json @@ -0,0 +1,23 @@ +{ + "name": "@amazeelabs/gatsby-operations", + "version": "1.0.0", + "description": "", + "type": "module", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "@babel/types": "^7.23.6", + "@types/babel__core": "^7.20.5", + "babel-plugin-tester": "^11.0.4", + "typescript": "^5.3.3", + "vitest": "^1.1.0" + }, + "dependencies": { + "@babel/core": "^7.23.6" + } +} diff --git a/packages/gatsby-operations/src/plugin.test.ts b/packages/gatsby-operations/src/plugin.test.ts new file mode 100644 index 000000000..7c1363cfa --- /dev/null +++ b/packages/gatsby-operations/src/plugin.test.ts @@ -0,0 +1,22 @@ +import { pluginTester } from 'babel-plugin-tester'; + +import plugin from './plugin'; + +pluginTester({ + plugin, + pluginOptions: {}, + tests: [ + { + code: ` +import { MyOperation } from '@custom/schema'; +import { useStaticQuery } from '@amazeelabs/gatsby-operations'; +function useData() { return useStaticQuery(MyOperation); } +`, + output: ` +import { MyOperation } from '@custom/schema'; +import { graphql, useStaticQuery, } from 'gatsby'; +function useData() { return useStaticQuery(graphql\`{ field }\`); } +`, + }, + ], +}); diff --git a/packages/gatsby-operations/src/plugin.ts b/packages/gatsby-operations/src/plugin.ts new file mode 100644 index 000000000..58c0b3c1b --- /dev/null +++ b/packages/gatsby-operations/src/plugin.ts @@ -0,0 +1,23 @@ +import { PluginObj, PluginPass, types } from '@babel/core'; + +export default () => + ({ + visitor: { + ImportDeclaration(path, { opts }) { + if (path.node.source.value !== '@amazeelabs/gatsby-operations') { + path.replaceWith( + types.importDeclaration( + path.node.specifiers, + types.stringLiteral('gatsby'), + ), + ); + } + }, + }, + } as PluginObj< + PluginPass & { + opts: { + operations: Record; + }; + } + >); diff --git a/packages/gatsby-operations/tsconfig.json b/packages/gatsby-operations/tsconfig.json new file mode 100644 index 000000000..e7dd0311e --- /dev/null +++ b/packages/gatsby-operations/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "ESNext", + "allowJs": false, + "skipLibCheck": true, + "esModuleInterop": false, + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "module": "ESNext", + "moduleResolution": "Node", + "resolveJsonModule": true, + "isolatedModules": true, + "declaration": true, + "declarationDir": "build", + "outDir": "build" + }, + "include": ["src"] +} diff --git a/packages/gatsby-operations/vitest.config.ts b/packages/gatsby-operations/vitest.config.ts new file mode 100644 index 000000000..7382f40e7 --- /dev/null +++ b/packages/gatsby-operations/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + globals: true, + }, +});