├── .gitignore ├── tsconfig.json ├── src └── index.ts ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node12/tsconfig.json", 3 | "include": [ 4 | "src" 5 | ], 6 | "compilerOptions": { 7 | "outDir": "dist", 8 | "declaration": true, 9 | "removeComments": false, 10 | }, 11 | } -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | function getCode(schema: string) { 2 | return ` 3 | import gql from 'graphql-tag'; 4 | 5 | export default gql(${schema}) 6 | `; 7 | } 8 | 9 | function transform(src: string, id: string) { 10 | if (id.endsWith('.graphql') || id.endsWith('.gql')) { 11 | const schema = JSON.stringify(src); 12 | 13 | return { 14 | code: getCode(schema), 15 | map: null 16 | }; 17 | } 18 | } 19 | 20 | export default function VitePluginGQL() { 21 | return { 22 | name: 'vite-plugin-gql', 23 | transform 24 | }; 25 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vite-plugin-simple-gql", 3 | "version": "0.5.0", 4 | "description": "Easily import gql and graphql files", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "build": "tsc" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/alansikora/vite-plugin-gql.git" 12 | }, 13 | "keywords": [ 14 | "Vite", 15 | "Javascript", 16 | "GraphQL" 17 | ], 18 | "author": "Alan Sikora (alan@allskar.com)", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/alansikora/vite-plugin-gql/issues" 22 | }, 23 | "homepage": "https://github.com/alansikora/vite-plugin-gql#readme", 24 | "peerDependencies": { 25 | "graphql-tag": "^2.12.6" 26 | }, 27 | "devDependencies": { 28 | "@tsconfig/node12": "^1.0.9", 29 | "typescript": "^4.5.2" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ⚡ vite-plugin-simple-gql 2 | 3 | [![npm](https://img.shields.io/npm/v/vite-plugin-simple-gql.svg)](https://www.npmjs.com/package/vite-plugin-simple-gql) 4 | [![npm](https://img.shields.io/npm/dt/vite-plugin-simple-gql)](https://www.npmjs.com/package/vite-plugin-simple-gql) 5 | 6 | Easily import [`.graphql`] and [`.gql`] files using [`Vite`]. 7 | 8 | [`vite`]: https://github.com/vitejs/vite 9 | 10 | ## Install 11 | 12 | ```bash 13 | npm i vite-plugin-simple-gql -D 14 | # yarn add vite-plugin-simple-gql -D 15 | ``` 16 | 17 | ## Usage 18 | 19 | add this plugin to `vite.config.js` 20 | 21 | ```js 22 | import { defineConfig } from 'vite'; 23 | import gql from 'vite-plugin-simple-gql'; 24 | export default defineConfig({ 25 | plugins: [gql()] 26 | }); 27 | ``` 28 | 29 | import your `graphql` files 30 | 31 | ```js 32 | import MyQuery from './queries/MyQuery.graphql' 33 | ``` 34 | 35 | ## 36 | 37 | ## License 38 | 39 | MIT --------------------------------------------------------------------------------