@soda-gql/babel-plugin 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +240 -0
- package/dist/index.cjs +664 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +29 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +633 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shota Hatada
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# @soda-gql/babel-plugin
|
|
2
|
+
|
|
3
|
+
> **Note**: This package is not yet published to npm. It is under active development and will be available in a future release.
|
|
4
|
+
|
|
5
|
+
Babel plugin for soda-gql zero-runtime GraphQL transformations.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Transforms `gql.default()` calls to runtime registrations at build time
|
|
10
|
+
- Removes GraphQL system imports and injects runtime imports
|
|
11
|
+
- Supports both ESM and CommonJS module formats
|
|
12
|
+
- Full TypeScript support
|
|
13
|
+
- HMR-ready for development workflows
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bun add -D @soda-gql/babel-plugin @soda-gql/cli
|
|
19
|
+
bun add @soda-gql/runtime
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### Babel Configuration
|
|
25
|
+
|
|
26
|
+
Add the plugin to your Babel configuration:
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
// babel.config.js
|
|
30
|
+
module.exports = {
|
|
31
|
+
plugins: [
|
|
32
|
+
[
|
|
33
|
+
"@soda-gql/babel-plugin",
|
|
34
|
+
{
|
|
35
|
+
configPath: "./soda-gql.config.ts",
|
|
36
|
+
artifact: {
|
|
37
|
+
useBuilder: true,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Project Setup
|
|
46
|
+
|
|
47
|
+
1. Generate your GraphQL system:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
bun run soda-gql codegen
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
2. Write GraphQL operations:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { gql } from "@/graphql-system";
|
|
57
|
+
|
|
58
|
+
export const userQuery = gql.default(({ query }, { $var }) =>
|
|
59
|
+
query.composed(
|
|
60
|
+
{
|
|
61
|
+
operationName: "GetUser",
|
|
62
|
+
variables: [$var("id").scalar("ID:!")],
|
|
63
|
+
},
|
|
64
|
+
({ f, $ }) => ({
|
|
65
|
+
user: f.user({ id: $.id })(({ f }) => [f.id(), f.name()]),
|
|
66
|
+
}),
|
|
67
|
+
),
|
|
68
|
+
);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
3. The plugin transforms this to runtime calls:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { gqlRuntime } from "@soda-gql/runtime";
|
|
75
|
+
|
|
76
|
+
export const userQuery = gqlRuntime.getComposedOperation("canonicalId");
|
|
77
|
+
gqlRuntime.composedOperation("canonicalId", { /* ... */ });
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Configuration Options
|
|
81
|
+
|
|
82
|
+
### `PluginOptions`
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
interface PluginOptions {
|
|
86
|
+
/** Path to soda-gql.config.ts */
|
|
87
|
+
configPath?: string;
|
|
88
|
+
|
|
89
|
+
/** Artifact configuration */
|
|
90
|
+
artifact?: {
|
|
91
|
+
/** Use builder to generate artifacts (default: true) */
|
|
92
|
+
useBuilder?: boolean;
|
|
93
|
+
/** Path to pre-built artifact.json */
|
|
94
|
+
path?: string;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** Development mode options */
|
|
98
|
+
dev?: {
|
|
99
|
+
/** Enable HMR support (default: false) */
|
|
100
|
+
hmr?: boolean;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Configuration Examples
|
|
106
|
+
|
|
107
|
+
#### Production Build
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
{
|
|
111
|
+
plugins: [
|
|
112
|
+
[
|
|
113
|
+
"@soda-gql/babel-plugin",
|
|
114
|
+
{
|
|
115
|
+
configPath: "./soda-gql.config.ts",
|
|
116
|
+
artifact: {
|
|
117
|
+
useBuilder: true,
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
],
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Development with HMR
|
|
126
|
+
|
|
127
|
+
```javascript
|
|
128
|
+
{
|
|
129
|
+
plugins: [
|
|
130
|
+
[
|
|
131
|
+
"@soda-gql/babel-plugin",
|
|
132
|
+
{
|
|
133
|
+
configPath: "./soda-gql.config.ts",
|
|
134
|
+
artifact: {
|
|
135
|
+
useBuilder: true,
|
|
136
|
+
},
|
|
137
|
+
dev: {
|
|
138
|
+
hmr: true,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
],
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### Using Pre-built Artifact
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
{
|
|
150
|
+
plugins: [
|
|
151
|
+
[
|
|
152
|
+
"@soda-gql/babel-plugin",
|
|
153
|
+
{
|
|
154
|
+
artifact: {
|
|
155
|
+
useBuilder: false,
|
|
156
|
+
path: "./dist/artifact.json",
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
],
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Module Format Support
|
|
165
|
+
|
|
166
|
+
The plugin automatically handles both ESM and CommonJS:
|
|
167
|
+
|
|
168
|
+
**Input (ESM)**:
|
|
169
|
+
```typescript
|
|
170
|
+
import { gql } from "@/graphql-system";
|
|
171
|
+
export const model = gql.default(/* ... */);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
**Output (ESM)**:
|
|
175
|
+
```typescript
|
|
176
|
+
import { gqlRuntime } from "@soda-gql/runtime";
|
|
177
|
+
export const model = gqlRuntime.model("canonicalId", /* ... */);
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**Output (CommonJS)** - when using `@babel/plugin-transform-modules-commonjs`:
|
|
181
|
+
```javascript
|
|
182
|
+
const { gqlRuntime } = require("@soda-gql/runtime");
|
|
183
|
+
module.exports.model = gqlRuntime.model("canonicalId", /* ... */);
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Architecture
|
|
187
|
+
|
|
188
|
+
### Transformation Pipeline
|
|
189
|
+
|
|
190
|
+
1. **Metadata Collection**: Analyzes AST to identify `gql.default()` calls
|
|
191
|
+
2. **Canonical ID Resolution**: Maps each call to a unique identifier (file path + AST path)
|
|
192
|
+
3. **Artifact Lookup**: Retrieves build artifacts for each GraphQL element
|
|
193
|
+
4. **AST Transformation**: Replaces builder calls with runtime calls
|
|
194
|
+
5. **Import Management**: Removes GraphQL system imports, adds runtime imports
|
|
195
|
+
|
|
196
|
+
### Supported GraphQL Elements
|
|
197
|
+
|
|
198
|
+
- **Models**: Fragment definitions with data normalization
|
|
199
|
+
- **Slices**: Reusable query/mutation/subscription fragments
|
|
200
|
+
- **Operations**: Composed operations from multiple slices
|
|
201
|
+
- **Inline Operations**: Self-contained operations
|
|
202
|
+
|
|
203
|
+
## Comparison with Other Plugins
|
|
204
|
+
|
|
205
|
+
| Feature | babel-plugin | plugin-swc | tsc-plugin |
|
|
206
|
+
|---------|--------------|------------|------------|
|
|
207
|
+
| Production Ready | ✅ | 🚧 In Development | ✅ |
|
|
208
|
+
| ESM Support | ✅ | ✅ | ✅ |
|
|
209
|
+
| CJS Support | ✅ | ✅ | ✅ |
|
|
210
|
+
| HMR Support | ✅ | ⚠️ Planned | ✅ |
|
|
211
|
+
| Build Speed | Good | Excellent | Fair |
|
|
212
|
+
| Setup Complexity | Low | Low | Medium |
|
|
213
|
+
|
|
214
|
+
## Troubleshooting
|
|
215
|
+
|
|
216
|
+
### Plugin Not Transforming Code
|
|
217
|
+
|
|
218
|
+
- Verify `configPath` points to a valid config file
|
|
219
|
+
- Ensure GraphQL system is generated (`bun run soda-gql codegen`)
|
|
220
|
+
- Check Babel is processing your source files
|
|
221
|
+
|
|
222
|
+
### Type Errors After Transformation
|
|
223
|
+
|
|
224
|
+
- Ensure `@soda-gql/runtime` is installed
|
|
225
|
+
- Verify GraphQL system types are up to date
|
|
226
|
+
- Check `tsconfig.json` includes transformed files
|
|
227
|
+
|
|
228
|
+
### Module Not Found Errors
|
|
229
|
+
|
|
230
|
+
- Confirm runtime import path is correct
|
|
231
|
+
- For CJS, ensure `@babel/plugin-transform-modules-commonjs` is configured
|
|
232
|
+
- Check module resolution in your bundler
|
|
233
|
+
|
|
234
|
+
## Contributing
|
|
235
|
+
|
|
236
|
+
See the main [CLAUDE.md](../../CLAUDE.md) for contribution guidelines.
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT
|