@soda-gql/babel 0.11.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,652 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+ let __soda_gql_builder_plugin_support = require("@soda-gql/builder/plugin-support");
29
+ let neverthrow = require("neverthrow");
30
+ let __babel_types = require("@babel/types");
31
+ __babel_types = __toESM(__babel_types);
32
+ let __babel_core = require("@babel/core");
33
+ let __soda_gql_common = require("@soda-gql/common");
34
+ let __soda_gql_builder = require("@soda-gql/builder");
35
+
36
+ //#region packages/babel/src/ast/analysis.ts
37
+ const extractGqlCall = ({ nodePath, filename, metadata, getArtifact }) => {
38
+ const callExpression = nodePath.node;
39
+ const meta = metadata.get(callExpression);
40
+ if (!meta) return (0, neverthrow.err)(createMetadataMissingError({ filename }));
41
+ const canonicalId = (0, __soda_gql_builder_plugin_support.resolveCanonicalId)(filename, meta.astPath);
42
+ const artifact = getArtifact(canonicalId);
43
+ if (!artifact) return (0, neverthrow.err)(createArtifactMissingError({
44
+ filename,
45
+ canonicalId
46
+ }));
47
+ if (artifact.type === "fragment") return (0, neverthrow.ok)({
48
+ nodePath,
49
+ canonicalId,
50
+ type: "fragment",
51
+ artifact
52
+ });
53
+ if (artifact.type === "operation") return (0, neverthrow.ok)({
54
+ nodePath,
55
+ canonicalId,
56
+ type: "operation",
57
+ artifact
58
+ });
59
+ return (0, neverthrow.err)(createUnsupportedArtifactTypeError({
60
+ filename,
61
+ canonicalId,
62
+ artifactType: artifact.type
63
+ }));
64
+ };
65
+ const createMetadataMissingError = ({ filename }) => ({
66
+ type: "PluginError",
67
+ stage: "analysis",
68
+ code: "SODA_GQL_METADATA_NOT_FOUND",
69
+ message: `No GraphQL metadata found for ${filename}`,
70
+ cause: { filename },
71
+ filename
72
+ });
73
+ const createArtifactMissingError = ({ filename, canonicalId }) => ({
74
+ type: "PluginError",
75
+ stage: "analysis",
76
+ code: "SODA_GQL_ANALYSIS_ARTIFACT_NOT_FOUND",
77
+ message: `No builder artifact found for canonical ID ${canonicalId}`,
78
+ cause: {
79
+ filename,
80
+ canonicalId
81
+ },
82
+ filename,
83
+ canonicalId
84
+ });
85
+ const createUnsupportedArtifactTypeError = ({ filename, canonicalId, artifactType }) => ({
86
+ type: "PluginError",
87
+ stage: "analysis",
88
+ code: "SODA_GQL_UNSUPPORTED_ARTIFACT_TYPE",
89
+ message: `Unsupported builder artifact type "${artifactType}" for canonical ID ${canonicalId}`,
90
+ cause: {
91
+ filename,
92
+ canonicalId,
93
+ artifactType
94
+ },
95
+ filename,
96
+ canonicalId,
97
+ artifactType
98
+ });
99
+
100
+ //#endregion
101
+ //#region packages/babel/src/ast/ast.ts
102
+ const createUnsupportedValueTypeError = (valueType) => ({
103
+ type: "PluginError",
104
+ stage: "transform",
105
+ code: "SODA_GQL_TRANSFORM_UNSUPPORTED_VALUE_TYPE",
106
+ message: `Unsupported value type: ${valueType}`,
107
+ cause: { valueType },
108
+ valueType
109
+ });
110
+ const buildLiteralFromValue = (value) => {
111
+ if (value === null) return (0, neverthrow.ok)(__babel_types.nullLiteral());
112
+ if (typeof value === "string") return (0, neverthrow.ok)(__babel_types.stringLiteral(value));
113
+ if (typeof value === "number") return (0, neverthrow.ok)(__babel_types.numericLiteral(value));
114
+ if (typeof value === "boolean") return (0, neverthrow.ok)(__babel_types.booleanLiteral(value));
115
+ if (Array.isArray(value)) {
116
+ const elements = [];
117
+ for (const item of value) {
118
+ const result = buildLiteralFromValue(item);
119
+ if (result.isErr()) return result;
120
+ elements.push(result.value);
121
+ }
122
+ return (0, neverthrow.ok)(__babel_types.arrayExpression(elements));
123
+ }
124
+ if (typeof value === "object") {
125
+ const properties = [];
126
+ for (const [key, val] of Object.entries(value)) {
127
+ const result = buildLiteralFromValue(val);
128
+ if (result.isErr()) return result;
129
+ properties.push(__babel_types.objectProperty(__babel_types.identifier(key), result.value));
130
+ }
131
+ return (0, neverthrow.ok)(__babel_types.objectExpression(properties));
132
+ }
133
+ return (0, neverthrow.err)(createUnsupportedValueTypeError(typeof value));
134
+ };
135
+ const clone = (node) => __babel_types.cloneNode(node, true);
136
+ const cloneCallExpression = (node) => __babel_types.callExpression(clone(node.callee), node.arguments.map(clone));
137
+ const stripTypeAnnotations = (node) => {
138
+ if ("typeParameters" in node) delete node.typeParameters;
139
+ if ("typeAnnotation" in node) delete node.typeAnnotation;
140
+ if ("returnType" in node) delete node.returnType;
141
+ };
142
+ const buildObjectExpression = (properties) => __babel_types.objectExpression(Object.entries(properties).map(([key, value]) => __babel_types.objectProperty(__babel_types.identifier(key), value)));
143
+
144
+ //#endregion
145
+ //#region packages/babel/src/ast/imports.ts
146
+ const RUNTIME_MODULE = "@soda-gql/runtime";
147
+ /**
148
+ * Ensure that the gqlRuntime require exists in the program for CJS output.
149
+ * Injects: const __soda_gql_runtime = require("@soda-gql/runtime");
150
+ */
151
+ const ensureGqlRuntimeRequire = (programPath) => {
152
+ if (programPath.node.body.find((statement) => __babel_core.types.isVariableDeclaration(statement) && statement.declarations.some((decl) => {
153
+ if (!__babel_core.types.isIdentifier(decl.id) || decl.id.name !== "__soda_gql_runtime") return false;
154
+ if (!decl.init || !__babel_core.types.isCallExpression(decl.init)) return false;
155
+ const callExpr = decl.init;
156
+ if (!__babel_core.types.isIdentifier(callExpr.callee) || callExpr.callee.name !== "require") return false;
157
+ const arg = callExpr.arguments[0];
158
+ return arg && __babel_core.types.isStringLiteral(arg) && arg.value === RUNTIME_MODULE;
159
+ }))) return;
160
+ const requireCall = __babel_core.types.callExpression(__babel_core.types.identifier("require"), [__babel_core.types.stringLiteral(RUNTIME_MODULE)]);
161
+ const variableDeclaration = __babel_core.types.variableDeclaration("const", [__babel_core.types.variableDeclarator(__babel_core.types.identifier("__soda_gql_runtime"), requireCall)]);
162
+ programPath.node.body.unshift(variableDeclaration);
163
+ };
164
+ /**
165
+ * Ensure that the gqlRuntime import exists in the program.
166
+ * gqlRuntime is always imported from @soda-gql/runtime.
167
+ */
168
+ const ensureGqlRuntimeImport = (programPath) => {
169
+ const existing = programPath.node.body.find((statement) => statement.type === "ImportDeclaration" && statement.source.value === RUNTIME_MODULE);
170
+ if (existing && __babel_core.types.isImportDeclaration(existing)) {
171
+ if (!existing.specifiers.some((specifier) => specifier.type === "ImportSpecifier" && specifier.imported.type === "Identifier" && specifier.imported.name === "gqlRuntime")) existing.specifiers = [...existing.specifiers, __babel_core.types.importSpecifier(__babel_core.types.identifier("gqlRuntime"), __babel_core.types.identifier("gqlRuntime"))];
172
+ return;
173
+ }
174
+ programPath.node.body.unshift(__babel_core.types.importDeclaration([__babel_core.types.importSpecifier(__babel_core.types.identifier("gqlRuntime"), __babel_core.types.identifier("gqlRuntime"))], __babel_core.types.stringLiteral(RUNTIME_MODULE)));
175
+ };
176
+ /**
177
+ * Remove the graphql-system import (runtimeModule) and gql-related exports from the program.
178
+ * After transformation, gqlRuntime is imported from @soda-gql/runtime instead,
179
+ * so the original graphql-system import should be completely removed.
180
+ *
181
+ * This handles both ESM imports and CommonJS require() statements.
182
+ */
183
+ const removeGraphqlSystemImports = (programPath, graphqlSystemIdentifyHelper, filename) => {
184
+ const toRemove = [];
185
+ programPath.traverse({
186
+ ImportDeclaration(path) {
187
+ if (__babel_core.types.isStringLiteral(path.node.source)) {
188
+ if (graphqlSystemIdentifyHelper.isGraphqlSystemImportSpecifier({
189
+ filePath: filename,
190
+ specifier: path.node.source.value
191
+ })) toRemove.push(path);
192
+ }
193
+ },
194
+ VariableDeclaration(path) {
195
+ if (path.node.declarations.every((decl) => {
196
+ const specifier = extractRequireTargetSpecifier(decl.init);
197
+ if (!specifier) return false;
198
+ return graphqlSystemIdentifyHelper.isGraphqlSystemImportSpecifier({
199
+ filePath: filename,
200
+ specifier
201
+ });
202
+ })) toRemove.push(path);
203
+ }
204
+ });
205
+ for (const path of toRemove) path.remove();
206
+ };
207
+ /**
208
+ * Check if an expression is a require() call and extract its target specifier.
209
+ * Handles multiple patterns:
210
+ * - require("@/graphql-system")
211
+ * - Object(require("@/graphql-system")) (interop helper pattern)
212
+ */
213
+ const extractRequireTargetSpecifier = (expr) => {
214
+ if (!expr) return;
215
+ if (__babel_core.types.isCallExpression(expr)) {
216
+ if (__babel_core.types.isIdentifier(expr.callee) && expr.callee.name === "require") {
217
+ const arg = expr.arguments[0];
218
+ if (arg && __babel_core.types.isStringLiteral(arg)) return arg.value;
219
+ }
220
+ if (__babel_core.types.isIdentifier(expr.callee) && (expr.callee.name === "Object" || expr.callee.name.startsWith("__import"))) {
221
+ const arg = expr.arguments[0];
222
+ if (arg && __babel_core.types.isCallExpression(arg)) {
223
+ if (__babel_core.types.isIdentifier(arg.callee) && arg.callee.name === "require") {
224
+ const requireArg = arg.arguments[0];
225
+ if (requireArg && __babel_core.types.isStringLiteral(requireArg)) return requireArg.value;
226
+ }
227
+ }
228
+ }
229
+ }
230
+ };
231
+
232
+ //#endregion
233
+ //#region packages/babel/src/ast/metadata.ts
234
+ const collectGqlDefinitionMetadata = ({ programPath, filename, createTracker }) => {
235
+ const exportBindings = collectExportBindings(programPath.node);
236
+ const tracker = (createTracker ?? __soda_gql_common.createCanonicalTracker)({
237
+ filePath: filename,
238
+ getExportName: (localName) => exportBindings.get(localName)
239
+ });
240
+ const getAnonymousName = createAnonymousNameFactory();
241
+ const scopeHandles = /* @__PURE__ */ new WeakMap();
242
+ const metadata = /* @__PURE__ */ new WeakMap();
243
+ programPath.traverse({
244
+ enter(path) {
245
+ if (path.isCallExpression() && isGqlDefinitionCall(path.node)) {
246
+ const depthBeforeRegister = tracker.currentDepth();
247
+ const { astPath } = tracker.registerDefinition();
248
+ const isTopLevel = depthBeforeRegister <= 1;
249
+ const exportInfo = isTopLevel ? resolveTopLevelExport(path, exportBindings) : null;
250
+ metadata.set(path.node, {
251
+ astPath,
252
+ isTopLevel,
253
+ isExported: exportInfo?.isExported ?? false,
254
+ exportBinding: exportInfo?.exportBinding
255
+ });
256
+ path.skip();
257
+ return;
258
+ }
259
+ const handle = maybeEnterScope(path, tracker, getAnonymousName);
260
+ if (handle) scopeHandles.set(path, handle);
261
+ },
262
+ exit(path) {
263
+ const handle = scopeHandles.get(path);
264
+ if (handle) {
265
+ tracker.exitScope(handle);
266
+ scopeHandles.delete(path);
267
+ }
268
+ }
269
+ });
270
+ return metadata;
271
+ };
272
+ const collectExportBindings = (program) => {
273
+ const bindings = /* @__PURE__ */ new Map();
274
+ for (const statement of program.body) {
275
+ if (__babel_core.types.isExportNamedDeclaration(statement) && statement.declaration) {
276
+ const { declaration } = statement;
277
+ if (__babel_core.types.isVariableDeclaration(declaration)) {
278
+ for (const declarator of declaration.declarations) if (__babel_core.types.isIdentifier(declarator.id)) bindings.set(declarator.id.name, declarator.id.name);
279
+ continue;
280
+ }
281
+ if ((__babel_core.types.isFunctionDeclaration(declaration) || __babel_core.types.isClassDeclaration(declaration)) && declaration.id) bindings.set(declaration.id.name, declaration.id.name);
282
+ continue;
283
+ }
284
+ if (__babel_core.types.isExpressionStatement(statement) && __babel_core.types.isAssignmentExpression(statement.expression)) {
285
+ const exportName = getCommonJsExportName(statement.expression.left);
286
+ if (exportName) bindings.set(exportName, exportName);
287
+ }
288
+ }
289
+ return bindings;
290
+ };
291
+ const getCommonJsExportName = (node) => {
292
+ if (!__babel_core.types.isMemberExpression(node) || node.computed) return null;
293
+ const isExports = __babel_core.types.isIdentifier(node.object, { name: "exports" });
294
+ const isModuleExports = __babel_core.types.isMemberExpression(node.object) && __babel_core.types.isIdentifier(node.object.object, { name: "module" }) && __babel_core.types.isIdentifier(node.object.property, { name: "exports" });
295
+ if (!isExports && !isModuleExports) return null;
296
+ if (__babel_core.types.isIdentifier(node.property)) return node.property.name;
297
+ if (__babel_core.types.isStringLiteral(node.property)) return node.property.value;
298
+ return null;
299
+ };
300
+ const createAnonymousNameFactory = () => {
301
+ const counters = /* @__PURE__ */ new Map();
302
+ return (kind) => {
303
+ const count = counters.get(kind) ?? 0;
304
+ counters.set(kind, count + 1);
305
+ return `${kind}#${count}`;
306
+ };
307
+ };
308
+ const isGqlDefinitionCall = (node) => __babel_core.types.isCallExpression(node) && __babel_core.types.isMemberExpression(node.callee) && isGqlReference(node.callee.object) && node.arguments.length > 0 && __babel_core.types.isArrowFunctionExpression(node.arguments[0]);
309
+ const isGqlReference = (expr) => {
310
+ if (__babel_core.types.isIdentifier(expr, { name: "gql" })) return true;
311
+ if (!__babel_core.types.isMemberExpression(expr) || expr.computed) return false;
312
+ if (__babel_core.types.isIdentifier(expr.property) && expr.property.name === "gql" || __babel_core.types.isStringLiteral(expr.property) && expr.property.value === "gql") return true;
313
+ return isGqlReference(expr.object);
314
+ };
315
+ const resolveTopLevelExport = (callPath, exportBindings) => {
316
+ const declarator = callPath.parentPath;
317
+ if (declarator?.isVariableDeclarator()) {
318
+ const { id } = declarator.node;
319
+ if (__babel_core.types.isIdentifier(id)) {
320
+ const exportBinding = exportBindings.get(id.name);
321
+ if (exportBinding) return {
322
+ isExported: true,
323
+ exportBinding
324
+ };
325
+ }
326
+ }
327
+ const assignment = callPath.parentPath;
328
+ if (assignment?.isAssignmentExpression()) {
329
+ const exportName = getCommonJsExportName(assignment.node.left);
330
+ if (exportName && exportBindings.has(exportName)) return {
331
+ isExported: true,
332
+ exportBinding: exportName
333
+ };
334
+ }
335
+ return null;
336
+ };
337
+ const maybeEnterScope = (path, tracker, getAnonymousName) => {
338
+ if (path.isAssignmentExpression()) {
339
+ const exportName = getCommonJsExportName(path.node.left);
340
+ if (exportName) return tracker.enterScope({
341
+ segment: exportName,
342
+ kind: "variable",
343
+ stableKey: `var:${exportName}`
344
+ });
345
+ }
346
+ if (path.isVariableDeclarator() && __babel_core.types.isIdentifier(path.node.id)) {
347
+ const name = path.node.id.name;
348
+ return tracker.enterScope({
349
+ segment: name,
350
+ kind: "variable",
351
+ stableKey: `var:${name}`
352
+ });
353
+ }
354
+ if (path.isArrowFunctionExpression()) {
355
+ const name = getAnonymousName("arrow");
356
+ return tracker.enterScope({
357
+ segment: name,
358
+ kind: "function",
359
+ stableKey: "arrow"
360
+ });
361
+ }
362
+ if (path.isFunctionDeclaration() || path.isFunctionExpression()) {
363
+ const name = path.node.id?.name ?? getAnonymousName("function");
364
+ return tracker.enterScope({
365
+ segment: name,
366
+ kind: "function",
367
+ stableKey: `func:${name}`
368
+ });
369
+ }
370
+ if (path.isClassDeclaration()) {
371
+ const name = path.node.id?.name ?? getAnonymousName("class");
372
+ return tracker.enterScope({
373
+ segment: name,
374
+ kind: "class",
375
+ stableKey: `class:${name}`
376
+ });
377
+ }
378
+ if (path.isClassMethod() && __babel_core.types.isIdentifier(path.node.key)) {
379
+ const name = path.node.key.name;
380
+ return tracker.enterScope({
381
+ segment: name,
382
+ kind: "method",
383
+ stableKey: `member:${name}`
384
+ });
385
+ }
386
+ if (path.isClassProperty() && __babel_core.types.isIdentifier(path.node.key)) {
387
+ const name = path.node.key.name;
388
+ return tracker.enterScope({
389
+ segment: name,
390
+ kind: "property",
391
+ stableKey: `member:${name}`
392
+ });
393
+ }
394
+ if (path.isObjectProperty()) {
395
+ const key = path.node.key;
396
+ const name = __babel_core.types.isIdentifier(key) ? key.name : __babel_core.types.isStringLiteral(key) ? key.value : null;
397
+ if (name) return tracker.enterScope({
398
+ segment: name,
399
+ kind: "property",
400
+ stableKey: `prop:${name}`
401
+ });
402
+ }
403
+ return null;
404
+ };
405
+
406
+ //#endregion
407
+ //#region packages/babel/src/ast/runtime.ts
408
+ const buildFragmentRuntimeCall = ({ artifact }) => {
409
+ const prebuildProps = { typename: __babel_core.types.stringLiteral(artifact.prebuild.typename) };
410
+ if (artifact.prebuild.key !== void 0) prebuildProps.key = __babel_core.types.stringLiteral(artifact.prebuild.key);
411
+ return (0, neverthrow.ok)(__babel_core.types.callExpression(__babel_core.types.memberExpression(__babel_core.types.identifier("gqlRuntime"), __babel_core.types.identifier("fragment")), [buildObjectExpression({ prebuild: buildObjectExpression(prebuildProps) })]));
412
+ };
413
+ const buildOperationRuntimeComponents = ({ artifact }) => {
414
+ const runtimeCall = __babel_core.types.callExpression(__babel_core.types.memberExpression(__babel_core.types.identifier("gqlRuntime"), __babel_core.types.identifier("operation")), [buildObjectExpression({
415
+ prebuild: __babel_core.types.callExpression(__babel_core.types.memberExpression(__babel_core.types.identifier("JSON"), __babel_core.types.identifier("parse")), [__babel_core.types.stringLiteral(JSON.stringify(artifact.prebuild))]),
416
+ runtime: buildObjectExpression({})
417
+ })]);
418
+ return (0, neverthrow.ok)({
419
+ referenceCall: __babel_core.types.callExpression(__babel_core.types.memberExpression(__babel_core.types.identifier("gqlRuntime"), __babel_core.types.identifier("getOperation")), [__babel_core.types.stringLiteral(artifact.prebuild.operationName)]),
420
+ runtimeCall
421
+ });
422
+ };
423
+
424
+ //#endregion
425
+ //#region packages/babel/src/ast/transformer.ts
426
+ const transformCallExpression = ({ callPath, filename, metadata, getArtifact }) => {
427
+ if (!metadata.has(callPath.node)) return (0, neverthrow.ok)({ transformed: false });
428
+ const gqlCallResult = extractGqlCall({
429
+ nodePath: callPath,
430
+ filename,
431
+ metadata,
432
+ getArtifact
433
+ });
434
+ if (gqlCallResult.isErr()) return (0, neverthrow.err)(gqlCallResult.error);
435
+ const gqlCall = gqlCallResult.value;
436
+ return replaceWithRuntimeCall(callPath, gqlCall, filename);
437
+ };
438
+ const replaceWithRuntimeCall = (callPath, gqlCall, filename) => {
439
+ if (gqlCall.type === "fragment") {
440
+ const result = buildFragmentRuntimeCall({
441
+ ...gqlCall,
442
+ filename
443
+ });
444
+ if (result.isErr()) return (0, neverthrow.err)(result.error);
445
+ callPath.replaceWith(result.value);
446
+ return (0, neverthrow.ok)({ transformed: true });
447
+ }
448
+ if (gqlCall.type === "operation") {
449
+ const result = buildOperationRuntimeComponents({
450
+ ...gqlCall,
451
+ filename
452
+ });
453
+ if (result.isErr()) return (0, neverthrow.err)(result.error);
454
+ const { referenceCall, runtimeCall } = result.value;
455
+ callPath.replaceWith(referenceCall);
456
+ return (0, neverthrow.ok)({
457
+ transformed: true,
458
+ runtimeCall
459
+ });
460
+ }
461
+ return (0, neverthrow.ok)({ transformed: false });
462
+ };
463
+ const insertRuntimeCalls = (programPath, runtimeCalls) => {
464
+ if (runtimeCalls.length === 0) return;
465
+ programPath.traverse({ ImportDeclaration(importDeclPath) {
466
+ if (importDeclPath.node.source.value === "@soda-gql/runtime") importDeclPath.insertAfter([...runtimeCalls]);
467
+ } });
468
+ };
469
+
470
+ //#endregion
471
+ //#region packages/babel/src/transformer.ts
472
+ /**
473
+ * Creates a Babel transformer with a single transform() method.
474
+ * This matches the pattern used in the TypeScript plugin.
475
+ */
476
+ const createTransformer = ({ programPath, types, config }) => {
477
+ const graphqlSystemIdentifyHelper = (0, __soda_gql_builder.createGraphqlSystemIdentifyHelper)(config);
478
+ /**
479
+ * Check if a node is a require() or __webpack_require__() call.
480
+ */
481
+ const isRequireCall = (node) => {
482
+ if (!types.isCallExpression(node)) return false;
483
+ const callee = node.callee;
484
+ return types.isIdentifier(callee) && (callee.name === "require" || callee.name === "__webpack_require__");
485
+ };
486
+ /**
487
+ * Find the last statement that loads a module (import or require).
488
+ * Handles both ESM imports and CommonJS require() calls.
489
+ */
490
+ const findLastModuleLoader = () => {
491
+ const bodyPaths = programPath.get("body");
492
+ let lastLoader = null;
493
+ for (const path of bodyPaths) {
494
+ if (path.isImportDeclaration()) {
495
+ lastLoader = path;
496
+ continue;
497
+ }
498
+ if (path.isVariableDeclaration()) {
499
+ for (const declarator of path.node.declarations) if (declarator.init && isRequireCall(declarator.init)) {
500
+ lastLoader = path;
501
+ break;
502
+ }
503
+ continue;
504
+ }
505
+ if (path.isExpressionStatement()) {
506
+ if (isRequireCall(path.node.expression)) lastLoader = path;
507
+ }
508
+ }
509
+ return lastLoader;
510
+ };
511
+ return { transform: (context) => {
512
+ const metadata = collectGqlDefinitionMetadata({
513
+ programPath,
514
+ filename: context.filename
515
+ });
516
+ const runtimeCalls = [];
517
+ let transformed = false;
518
+ programPath.traverse({ CallExpression: (callPath) => {
519
+ const result = transformCallExpression({
520
+ callPath,
521
+ filename: context.filename,
522
+ metadata,
523
+ getArtifact: context.artifactLookup
524
+ });
525
+ if (result.isErr()) {
526
+ console.error(`[@soda-gql/babel] ${(0, __soda_gql_builder_plugin_support.formatPluginError)(result.error)}`);
527
+ return;
528
+ }
529
+ const transformResult = result.value;
530
+ if (transformResult.transformed) {
531
+ transformed = true;
532
+ if (transformResult.runtimeCall) runtimeCalls.push(transformResult.runtimeCall);
533
+ }
534
+ } });
535
+ if (!transformed) return {
536
+ transformed: false,
537
+ runtimeArtifacts: void 0
538
+ };
539
+ ensureGqlRuntimeImport(programPath);
540
+ if (runtimeCalls.length > 0) {
541
+ const statements = runtimeCalls.map((expr) => types.expressionStatement(expr));
542
+ const lastLoaderPath = findLastModuleLoader();
543
+ if (lastLoaderPath) lastLoaderPath.insertAfter(statements);
544
+ else programPath.unshiftContainer("body", statements);
545
+ }
546
+ programPath.scope.crawl();
547
+ removeGraphqlSystemImports(programPath, graphqlSystemIdentifyHelper, context.filename);
548
+ return {
549
+ transformed: true,
550
+ runtimeArtifacts: void 0
551
+ };
552
+ } };
553
+ };
554
+
555
+ //#endregion
556
+ Object.defineProperty(exports, '__toESM', {
557
+ enumerable: true,
558
+ get: function () {
559
+ return __toESM;
560
+ }
561
+ });
562
+ Object.defineProperty(exports, 'buildFragmentRuntimeCall', {
563
+ enumerable: true,
564
+ get: function () {
565
+ return buildFragmentRuntimeCall;
566
+ }
567
+ });
568
+ Object.defineProperty(exports, 'buildLiteralFromValue', {
569
+ enumerable: true,
570
+ get: function () {
571
+ return buildLiteralFromValue;
572
+ }
573
+ });
574
+ Object.defineProperty(exports, 'buildObjectExpression', {
575
+ enumerable: true,
576
+ get: function () {
577
+ return buildObjectExpression;
578
+ }
579
+ });
580
+ Object.defineProperty(exports, 'buildOperationRuntimeComponents', {
581
+ enumerable: true,
582
+ get: function () {
583
+ return buildOperationRuntimeComponents;
584
+ }
585
+ });
586
+ Object.defineProperty(exports, 'clone', {
587
+ enumerable: true,
588
+ get: function () {
589
+ return clone;
590
+ }
591
+ });
592
+ Object.defineProperty(exports, 'cloneCallExpression', {
593
+ enumerable: true,
594
+ get: function () {
595
+ return cloneCallExpression;
596
+ }
597
+ });
598
+ Object.defineProperty(exports, 'collectGqlDefinitionMetadata', {
599
+ enumerable: true,
600
+ get: function () {
601
+ return collectGqlDefinitionMetadata;
602
+ }
603
+ });
604
+ Object.defineProperty(exports, 'createTransformer', {
605
+ enumerable: true,
606
+ get: function () {
607
+ return createTransformer;
608
+ }
609
+ });
610
+ Object.defineProperty(exports, 'ensureGqlRuntimeImport', {
611
+ enumerable: true,
612
+ get: function () {
613
+ return ensureGqlRuntimeImport;
614
+ }
615
+ });
616
+ Object.defineProperty(exports, 'ensureGqlRuntimeRequire', {
617
+ enumerable: true,
618
+ get: function () {
619
+ return ensureGqlRuntimeRequire;
620
+ }
621
+ });
622
+ Object.defineProperty(exports, 'extractGqlCall', {
623
+ enumerable: true,
624
+ get: function () {
625
+ return extractGqlCall;
626
+ }
627
+ });
628
+ Object.defineProperty(exports, 'insertRuntimeCalls', {
629
+ enumerable: true,
630
+ get: function () {
631
+ return insertRuntimeCalls;
632
+ }
633
+ });
634
+ Object.defineProperty(exports, 'removeGraphqlSystemImports', {
635
+ enumerable: true,
636
+ get: function () {
637
+ return removeGraphqlSystemImports;
638
+ }
639
+ });
640
+ Object.defineProperty(exports, 'stripTypeAnnotations', {
641
+ enumerable: true,
642
+ get: function () {
643
+ return stripTypeAnnotations;
644
+ }
645
+ });
646
+ Object.defineProperty(exports, 'transformCallExpression', {
647
+ enumerable: true,
648
+ get: function () {
649
+ return transformCallExpression;
650
+ }
651
+ });
652
+ //# sourceMappingURL=transformer-C9UM_hw8.cjs.map