graphql-code-generator
graphql-code-generator copied to clipboard
`gql-tag-operations-preset` `babelPlugin` doesn't work with `vite`
Describe the bug
Using babelPlugin from gql-tag-operations-preset inside vite project throws _babel_template.default is not a function
Your Example Website or App
https://stackblitz.com/edit/vitejs-vite-toyias
Steps to Reproduce the Bug or Issue
Use the link above or:
- Create new vite project with react
-
npm install graphql @graphql-codegen/gql-tag-operations-preset - Add babel plugin
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { babelPlugin } from '@graphql-codegen/gql-tag-operations-preset';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react({ babel: { plugins: [[babelPlugin, {}]] } })],
});
- Define a gql operation
// src/App.jsx
const Query = gql(`query AppQuery { hello }`);
Expected behavior
Works out of the box
Screenshots or Videos

Platform
-
graphqlversion: 16.5.0 -
@graphql-codegen/gql-tag-operations-presetversion: 1.5.4 -
viteversion: 3.0.4
Codegen Config File
No response
Additional context
Possible fix is to use template.smart instead of default export in gql-tag-operations/src/babel.ts#L56:
- const importDeclaration = template(`
+ const importDeclaration = template.smart(`
import { %%importName%% } from %%importPath%%
`);
Default export from @babel/template is smart.bind(undefined) so using tempalte.smart shouldn't be a breaking change.