@soda-gql/babel-transformer 0.2.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.
@@ -0,0 +1,205 @@
1
+ import { GqlCallFragment, GqlCallOperation, GqlDefinitionMetadata, PluginError } from "@soda-gql/plugin-common";
2
+ import { Result } from "neverthrow";
3
+ import * as t from "@babel/types";
4
+ import { Expression } from "@babel/types";
5
+ import { types as types$1 } from "@babel/core";
6
+ import { CanonicalId, createCanonicalTracker } from "@soda-gql/common";
7
+ import { NodePath } from "@babel/traverse";
8
+ import { BuilderArtifact, BuilderArtifactElement, GraphqlSystemIdentifyHelper } from "@soda-gql/builder";
9
+ import { ResolvedSodaGqlConfig } from "@soda-gql/config";
10
+
11
+ //#region packages/babel-transformer/src/ast/metadata.d.ts
12
+ type GqlDefinitionMetadataMap = WeakMap<types$1.CallExpression, GqlDefinitionMetadata>;
13
+ type CanonicalTrackerFactory = typeof createCanonicalTracker;
14
+ type CollectArgs = {
15
+ readonly programPath: NodePath<types$1.Program>;
16
+ readonly filename: string;
17
+ readonly createTracker?: CanonicalTrackerFactory;
18
+ };
19
+ declare const collectGqlDefinitionMetadata: ({
20
+ programPath,
21
+ filename,
22
+ createTracker
23
+ }: CollectArgs) => GqlDefinitionMetadataMap;
24
+ //#endregion
25
+ //#region packages/babel-transformer/src/ast/analysis.d.ts
26
+ type ArtifactLookup = (canonicalId: CanonicalId) => BuilderArtifactElement | undefined;
27
+ type BabelGqlCallFragment = GqlCallFragment & {
28
+ readonly nodePath: NodePath<types$1.CallExpression>;
29
+ };
30
+ type BabelGqlCallOperation = GqlCallOperation & {
31
+ readonly nodePath: NodePath<types$1.CallExpression>;
32
+ };
33
+ type BabelGqlCall = BabelGqlCallFragment | BabelGqlCallOperation;
34
+ type ExtractGqlCallArgs = {
35
+ readonly nodePath: NodePath<types$1.CallExpression>;
36
+ readonly filename: string;
37
+ readonly metadata: GqlDefinitionMetadataMap;
38
+ readonly getArtifact: ArtifactLookup;
39
+ };
40
+ declare const extractGqlCall: ({
41
+ nodePath,
42
+ filename,
43
+ metadata,
44
+ getArtifact
45
+ }: ExtractGqlCallArgs) => Result<BabelGqlCall, PluginError>;
46
+ //#endregion
47
+ //#region packages/babel-transformer/src/ast/ast.d.ts
48
+ declare const buildLiteralFromValue: (value: unknown) => Result<Expression, PluginError>;
49
+ declare const clone: <T extends t.Node>(node: T) => T;
50
+ declare const cloneCallExpression: (node: t.CallExpression) => t.CallExpression;
51
+ declare const stripTypeAnnotations: (node: t.Node) => void;
52
+ declare const buildObjectExpression: <K extends string>(properties: Record<K, t.Expression | t.PatternLike>) => t.ObjectExpression;
53
+ //#endregion
54
+ //#region packages/babel-transformer/src/ast/imports.d.ts
55
+ /**
56
+ * Ensure that the gqlRuntime require exists in the program for CJS output.
57
+ * Injects: const __soda_gql_runtime = require("@soda-gql/runtime");
58
+ */
59
+ declare const ensureGqlRuntimeRequire: (programPath: NodePath<types$1.Program>) => void;
60
+ /**
61
+ * Ensure that the gqlRuntime import exists in the program.
62
+ * gqlRuntime is always imported from @soda-gql/runtime.
63
+ */
64
+ declare const ensureGqlRuntimeImport: (programPath: NodePath<types$1.Program>) => void;
65
+ /**
66
+ * Remove the graphql-system import (runtimeModule) and gql-related exports from the program.
67
+ * After transformation, gqlRuntime is imported from @soda-gql/runtime instead,
68
+ * so the original graphql-system import should be completely removed.
69
+ *
70
+ * This handles both ESM imports and CommonJS require() statements.
71
+ */
72
+ declare const removeGraphqlSystemImports: (programPath: NodePath<types$1.Program>, graphqlSystemIdentifyHelper: GraphqlSystemIdentifyHelper, filename: string) => void;
73
+ //#endregion
74
+ //#region packages/babel-transformer/src/ast/runtime.d.ts
75
+ declare const buildFragmentRuntimeCall: ({
76
+ artifact
77
+ }: BabelGqlCallFragment & {
78
+ filename: string;
79
+ }) => Result<types$1.Expression, PluginError>;
80
+ declare const buildOperationRuntimeComponents: ({
81
+ artifact
82
+ }: BabelGqlCallOperation & {
83
+ filename: string;
84
+ }) => Result<{
85
+ referenceCall: types$1.Expression;
86
+ runtimeCall: types$1.Expression;
87
+ }, PluginError>;
88
+ //#endregion
89
+ //#region packages/babel-transformer/src/ast/transformer.d.ts
90
+ type TransformCallExpressionArgs = {
91
+ readonly callPath: NodePath<types$1.CallExpression>;
92
+ readonly filename: string;
93
+ readonly metadata: GqlDefinitionMetadataMap;
94
+ readonly getArtifact: ArtifactLookup;
95
+ };
96
+ type TransformCallExpressionResult = {
97
+ readonly transformed: false;
98
+ } | {
99
+ readonly transformed: true;
100
+ readonly runtimeCall?: types$1.Expression;
101
+ };
102
+ declare const transformCallExpression: ({
103
+ callPath,
104
+ filename,
105
+ metadata,
106
+ getArtifact
107
+ }: TransformCallExpressionArgs) => Result<TransformCallExpressionResult, PluginError>;
108
+ declare const insertRuntimeCalls: (programPath: NodePath<types$1.Program>, runtimeCalls: readonly types$1.Expression[]) => void;
109
+ //#endregion
110
+ //#region packages/babel-transformer/src/transform.d.ts
111
+ /**
112
+ * Options for creating a transformer.
113
+ */
114
+ type TransformOptions = {
115
+ /** Resolved soda-gql configuration */
116
+ config: ResolvedSodaGqlConfig;
117
+ /** Pre-built artifact from the builder */
118
+ artifact: BuilderArtifact;
119
+ /** Whether to generate source maps */
120
+ sourceMap?: boolean;
121
+ };
122
+ /**
123
+ * Input for the transform function.
124
+ */
125
+ type TransformInput = {
126
+ /** Source code to transform */
127
+ sourceCode: string;
128
+ /** Path to the source file */
129
+ sourcePath: string;
130
+ /** Input source map from previous transformer (JSON string) */
131
+ inputSourceMap?: string;
132
+ };
133
+ /**
134
+ * Output from the transform function.
135
+ */
136
+ type TransformOutput = {
137
+ /** Whether any transformation was performed */
138
+ transformed: boolean;
139
+ /** The transformed source code (or original if no transformation) */
140
+ sourceCode: string;
141
+ /** Source map JSON, if source map generation was enabled */
142
+ sourceMap?: string;
143
+ };
144
+ /**
145
+ * Transformer interface (matches swc-transformer).
146
+ */
147
+ interface Transformer {
148
+ transform(input: TransformInput): TransformOutput;
149
+ }
150
+ /**
151
+ * Create a transformer instance.
152
+ *
153
+ * @param options - Transform options including config and artifact
154
+ * @returns A transformer that can transform source files
155
+ */
156
+ declare const createBabelTransformer: (options: TransformOptions) => Transformer;
157
+ /**
158
+ * Transform a single source file (one-shot).
159
+ *
160
+ * For transforming multiple files, use createBabelTransformer() to reuse the artifact.
161
+ *
162
+ * @param input - Transform input including source, path, artifact, and config
163
+ * @returns Transform output
164
+ */
165
+ declare const transform: (input: TransformInput & {
166
+ artifact: BuilderArtifact;
167
+ config: ResolvedSodaGqlConfig;
168
+ sourceMap?: boolean;
169
+ }) => TransformOutput;
170
+ //#endregion
171
+ //#region packages/babel-transformer/src/types.d.ts
172
+ /**
173
+ * Context for transforming a program.
174
+ */
175
+ type TransformProgramContext = {
176
+ readonly filename: string;
177
+ readonly artifactLookup: (id: CanonicalId) => BuilderArtifactElement | undefined;
178
+ };
179
+ /**
180
+ * Result of a transform pass.
181
+ */
182
+ type TransformPassResult = {
183
+ readonly transformed: boolean;
184
+ readonly runtimeArtifacts?: ReadonlyArray<unknown>;
185
+ };
186
+ //#endregion
187
+ //#region packages/babel-transformer/src/transformer.d.ts
188
+ /**
189
+ * Creates a Babel transformer with a single transform() method.
190
+ * This matches the pattern used in the TypeScript plugin.
191
+ */
192
+ declare const createTransformer: ({
193
+ programPath,
194
+ types,
195
+ config
196
+ }: {
197
+ readonly programPath: NodePath<types$1.Program>;
198
+ readonly types: typeof types$1;
199
+ readonly config: ResolvedSodaGqlConfig;
200
+ }) => {
201
+ transform: (context: TransformProgramContext) => TransformPassResult;
202
+ };
203
+ //#endregion
204
+ export { ArtifactLookup, BabelGqlCall, BabelGqlCallFragment, BabelGqlCallOperation, ExtractGqlCallArgs, GqlDefinitionMetadataMap, type TransformInput, type TransformOptions, type TransformOutput, type TransformPassResult, type TransformProgramContext, type Transformer, buildFragmentRuntimeCall, buildLiteralFromValue, buildObjectExpression, buildOperationRuntimeComponents, clone, cloneCallExpression, collectGqlDefinitionMetadata, createBabelTransformer, createTransformer, ensureGqlRuntimeImport, ensureGqlRuntimeRequire, extractGqlCall, insertRuntimeCalls, removeGraphqlSystemImports, stripTypeAnnotations, transform, transformCallExpression };
205
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/ast/metadata.ts","../src/ast/analysis.ts","../src/ast/ast.ts","../src/ast/imports.ts","../src/ast/runtime.ts","../src/ast/transformer.ts","../src/transform.ts","../src/types.ts","../src/transformer.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;KAKY,wBAAA,GAA2B,QAAQ,OAAA,CAAE,gBAAgB;KAE5D,uBAAA,UAAiC;KAEjC,WAAA;wBACmB,SAAS,OAAA,CAAE;;2BAER;;AAPf,cAaC,4BAbuB,EAAA,CAAA;EAAA,WAAA;EAAA,QAAA;EAAA;AAAA,CAAA,EAamD,WAbnD,EAAA,GAaiE,wBAbjE;;;KCWxB,cAAA,iBAA+B,gBAAgB;KAG/C,oBAAA,GAAuB;qBAAuC,SAAS,OAAA,CAAE;;ADdzE,KCeA,qBAAA,GAAwB,gBDfA,GAAA;EAAW,SAAE,QAAA,ECgB5B,QDhB4B,CCgBnB,OAAA,CAAE,cDhBiB,CAAA;CAAgB;AAA1B,KCmB3B,YAAA,GAAe,oBDnBY,GCmBW,qBDnBX;AAAO,KCqBlC,kBAAA,GDrBkC;EAEzC,SAAA,QAAA,ECoBgB,QDpBO,CCoBE,OAAA,CAAE,cDpBM,CAAA;EAEjC,SAAA,QAAW,EAAA,MAAA;EACiB,SAAE,QAAA,ECmBd,wBDnBc;EAAX,SAAA,WAAA,ECoBA,cDpBA;CAEG;AAAuB,cCqBrC,cDrBqC,EAAA,CAAA;EAAA,QAAA;EAAA,QAAA;EAAA,QAAA;EAAA;AAAA,CAAA,EC0B/C,kBD1B+C,EAAA,GC0B1B,MD1B0B,CC0BnB,YD1BmB,EC0BL,WD1BK,CAAA;;;cEErC,2CAA0C,OAAO,YAAY;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