Skip to content

Commit 10f8088

Browse files
committed
init project
0 parents  commit 10f8088

File tree

8 files changed

+1142
-0
lines changed

8 files changed

+1142
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 592 additions & 0 deletions
Large diffs are not rendered by default.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 hamidyfine
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# jest-graphql-transformer
2+
3+
A Jest transformer that enables **GraphQL queries in separate files**, similar to how [`graphql-tag` loader for Webpack](http://dev.apollodata.com/react/webpack.html) works.
4+
5+
Since Jest **does not support Webpack loaders**, this package provides the same transformation inside Jest.
6+
7+
---
8+
9+
## 🚀 Installation
10+
11+
Install the package using **npm** or **yarn**:
12+
13+
```sh
14+
npm install --save-dev jest-graphql-transformer
15+
```
16+
17+
or
18+
19+
```sh
20+
yarn add --dev jest-graphql-transformer
21+
```
22+
23+
---
24+
25+
## 🔧 Usage
26+
27+
### ✅ Configure Jest to Use the Transformer
28+
29+
#### 📌 **In `package.json`**
30+
31+
Add the following to your Jest configuration:
32+
33+
```json
34+
"jest": {
35+
"transform": {
36+
"\\.(gql|graphql)$": "jest-graphql-transformer",
37+
".*": "babel-jest"
38+
}
39+
}
40+
```
41+
42+
#### 📌 **Or in `jest.config.js` / `jest.config.ts`**
43+
44+
```javascript
45+
export default {
46+
transform: {
47+
'\\.(gql|graphql)$': 'jest-graphql-transformer',
48+
},
49+
};
50+
```
51+
52+
### ⚠️ **Note:**
53+
54+
Once you define the `transform` property, **Jest's default transformations will be overridden**.
55+
If you need Babel for other files, make sure to include it:
56+
57+
```javascript
58+
export default {
59+
transform: {
60+
'\\.(gql|graphql)$': 'jest-graphql-transformer',
61+
'.*': 'babel-jest', // Keep Babel for other files
62+
},
63+
};
64+
```
65+
66+
---
67+
68+
## 📜 License
69+
70+
MIT License © 2024 [hamidyfine](https://github.com/hamidyfine)

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "jest-graphql-transformer",
3+
"version": "1.0.2",
4+
"description": "Jest transformer for .gql imports",
5+
"main": "dist/cjs/index.js",
6+
"module": "dist/esm/index.js",
7+
"exports": {
8+
"require": "./dist/cjs/index.js",
9+
"import": "./dist/esm/index.js"
10+
},
11+
"scripts": {
12+
"build": "rollup -c"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/hamidyfine/jest-graphql-transformer.git"
17+
},
18+
"keywords": [
19+
"jest",
20+
"graphql"
21+
],
22+
"author": {
23+
"name": "Hamid Yaftian",
24+
"email": "[email protected]",
25+
"url": "https://itshamid.me/",
26+
"github": "hamidyfine"
27+
},
28+
"license": "MIT",
29+
"bugs": {
30+
"url": "https://github.com/hamidyfine/jest-graphql-transformer/issues"
31+
},
32+
"homepage": "https://github.com/hamidyfine/jest-graphql-transformer#readme",
33+
"dependencies": {
34+
"graphql-tag": "^2.12.6"
35+
},
36+
"devDependencies": {
37+
"@rollup/plugin-commonjs": "^28.0.2",
38+
"@rollup/plugin-node-resolve": "^16.0.0",
39+
"@rollup/plugin-terser": "^0.4.4",
40+
"rollup": "^4.34.8"
41+
},
42+
"sideEffects": false
43+
}

rollup.config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const resolve = require("@rollup/plugin-node-resolve");
2+
const commonjs = require("@rollup/plugin-commonjs");
3+
const terser = require("@rollup/plugin-terser");
4+
5+
module.exports = [
6+
// CommonJS Build
7+
{
8+
input: "src/index.js",
9+
output: {
10+
file: "dist/cjs/index.js",
11+
format: "cjs",
12+
sourcemap: true,
13+
},
14+
plugins: [resolve(), commonjs()],
15+
},
16+
17+
// ES Module Build
18+
{
19+
input: "src/index.js",
20+
output: {
21+
file: "dist/esm/index.js",
22+
format: "esm",
23+
sourcemap: true,
24+
},
25+
plugins: [resolve(), commonjs(), terser()],
26+
},
27+
];

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const loader = require('graphql-tag/loader');
2+
3+
module.exports = {
4+
process: (src) => {
5+
const result = loader.call({ cacheable: () => {} }, src);
6+
return {
7+
code: result,
8+
};
9+
},
10+
};

0 commit comments

Comments
 (0)