@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.
- package/dist/index.cjs +878 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +205 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +205 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +860 -0
- package/dist/index.mjs.map +1 -0
- package/dist/plugin.cjs +58 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.d.cts +30 -0
- package/dist/plugin.d.cts.map +1 -0
- package/dist/plugin.d.mts +30 -0
- package/dist/plugin.d.mts.map +1 -0
- package/dist/plugin.mjs +56 -0
- package/dist/plugin.mjs.map +1 -0
- package/dist/transformer-C9UM_hw8.cjs +652 -0
- package/dist/transformer-C9UM_hw8.cjs.map +1 -0
- package/dist/transformer-kTdl4bE2.mjs +529 -0
- package/dist/transformer-kTdl4bE2.mjs.map +1 -0
- package/package.json +76 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformer-C9UM_hw8.cjs","names":["t","elements: Expression[]","properties: t.ObjectProperty[]","t","toRemove: NodePath[]","createCanonicalTracker","bindings: ExportBindingMap","t","prebuildProps: Record<string, t.Expression>","t","lastLoader: NodePath<t.Statement> | null","runtimeCalls: t.Expression[]"],"sources":["../src/ast/analysis.ts","../src/ast/ast.ts","../src/ast/imports.ts","../src/ast/metadata.ts","../src/ast/runtime.ts","../src/ast/transformer.ts","../src/transformer.ts"],"sourcesContent":["import type { types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type { BuilderArtifactElement } from \"@soda-gql/builder\";\nimport type {\n GqlCallFragment,\n GqlCallOperation,\n PluginAnalysisArtifactMissingError,\n PluginAnalysisMetadataMissingError,\n PluginAnalysisUnsupportedArtifactTypeError,\n PluginError,\n} from \"@soda-gql/builder/plugin-support\";\nimport { resolveCanonicalId } from \"@soda-gql/builder/plugin-support\";\nimport type { CanonicalId } from \"@soda-gql/common\";\nimport { err, ok, type Result } from \"neverthrow\";\nimport type { GqlDefinitionMetadataMap } from \"./metadata\";\n\nexport type ArtifactLookup = (canonicalId: CanonicalId) => BuilderArtifactElement | undefined;\n\n// Babel-specific GqlCall types\nexport type BabelGqlCallFragment = GqlCallFragment & { readonly nodePath: NodePath<t.CallExpression> };\nexport type BabelGqlCallOperation = GqlCallOperation & {\n readonly nodePath: NodePath<t.CallExpression>;\n};\n\nexport type BabelGqlCall = BabelGqlCallFragment | BabelGqlCallOperation;\n\nexport type ExtractGqlCallArgs = {\n readonly nodePath: NodePath<t.CallExpression>;\n readonly filename: string;\n readonly metadata: GqlDefinitionMetadataMap;\n readonly getArtifact: ArtifactLookup;\n};\n\nexport const extractGqlCall = ({\n nodePath,\n filename,\n metadata,\n getArtifact,\n}: ExtractGqlCallArgs): Result<BabelGqlCall, PluginError> => {\n const callExpression = nodePath.node;\n\n const meta = metadata.get(callExpression);\n if (!meta) {\n return err(createMetadataMissingError({ filename }));\n }\n\n const canonicalId = resolveCanonicalId(filename, meta.astPath);\n const artifact = getArtifact(canonicalId);\n\n if (!artifact) {\n return err(createArtifactMissingError({ filename, canonicalId }));\n }\n\n if (artifact.type === \"fragment\") {\n return ok({ nodePath, canonicalId, type: \"fragment\", artifact });\n }\n\n if (artifact.type === \"operation\") {\n return ok({ nodePath, canonicalId, type: \"operation\", artifact });\n }\n\n return err(\n createUnsupportedArtifactTypeError({\n filename,\n canonicalId,\n artifactType: (artifact as { type: string }).type,\n }),\n );\n};\n\ntype MetadataErrorInput = { readonly filename: string };\ntype ArtifactMissingErrorInput = { readonly filename: string; readonly canonicalId: CanonicalId };\ntype UnsupportedArtifactTypeInput = {\n readonly filename: string;\n readonly canonicalId: CanonicalId;\n readonly artifactType: string;\n};\n\nconst createMetadataMissingError = ({ filename }: MetadataErrorInput): PluginAnalysisMetadataMissingError => ({\n type: \"PluginError\",\n stage: \"analysis\",\n code: \"SODA_GQL_METADATA_NOT_FOUND\",\n message: `No GraphQL metadata found for ${filename}`,\n cause: { filename },\n filename,\n});\n\nconst createArtifactMissingError = ({\n filename,\n canonicalId,\n}: ArtifactMissingErrorInput): PluginAnalysisArtifactMissingError => ({\n type: \"PluginError\",\n stage: \"analysis\",\n code: \"SODA_GQL_ANALYSIS_ARTIFACT_NOT_FOUND\",\n message: `No builder artifact found for canonical ID ${canonicalId}`,\n cause: { filename, canonicalId },\n filename,\n canonicalId,\n});\n\nconst createUnsupportedArtifactTypeError = ({\n filename,\n canonicalId,\n artifactType,\n}: UnsupportedArtifactTypeInput): PluginAnalysisUnsupportedArtifactTypeError => ({\n type: \"PluginError\",\n stage: \"analysis\",\n code: \"SODA_GQL_UNSUPPORTED_ARTIFACT_TYPE\",\n message: `Unsupported builder artifact type \"${artifactType}\" for canonical ID ${canonicalId}`,\n cause: { filename, canonicalId, artifactType },\n filename,\n canonicalId,\n artifactType,\n});\n","import type { Expression } from \"@babel/types\";\nimport * as t from \"@babel/types\";\nimport type { PluginError, PluginTransformUnsupportedValueTypeError } from \"@soda-gql/builder/plugin-support\";\nimport { err, ok, type Result } from \"neverthrow\";\n\nconst createUnsupportedValueTypeError = (valueType: string): PluginTransformUnsupportedValueTypeError => ({\n type: \"PluginError\",\n stage: \"transform\",\n code: \"SODA_GQL_TRANSFORM_UNSUPPORTED_VALUE_TYPE\",\n message: `Unsupported value type: ${valueType}`,\n cause: { valueType },\n valueType,\n});\n\nexport const buildLiteralFromValue = (value: unknown): Result<Expression, PluginError> => {\n if (value === null) {\n return ok(t.nullLiteral());\n }\n if (typeof value === \"string\") {\n return ok(t.stringLiteral(value));\n }\n if (typeof value === \"number\") {\n return ok(t.numericLiteral(value));\n }\n if (typeof value === \"boolean\") {\n return ok(t.booleanLiteral(value));\n }\n if (Array.isArray(value)) {\n const elements: Expression[] = [];\n for (const item of value) {\n const result = buildLiteralFromValue(item);\n if (result.isErr()) {\n return result;\n }\n elements.push(result.value);\n }\n return ok(t.arrayExpression(elements));\n }\n if (typeof value === \"object\") {\n const properties: t.ObjectProperty[] = [];\n for (const [key, val] of Object.entries(value)) {\n const result = buildLiteralFromValue(val);\n if (result.isErr()) {\n return result;\n }\n properties.push(t.objectProperty(t.identifier(key), result.value));\n }\n return ok(t.objectExpression(properties));\n }\n return err(createUnsupportedValueTypeError(typeof value));\n};\n\nexport const clone = <T extends t.Node>(node: T): T => t.cloneNode(node, true) as T;\n\nexport const cloneCallExpression = (node: t.CallExpression): t.CallExpression =>\n t.callExpression(clone(node.callee), node.arguments.map(clone));\n\nexport const stripTypeAnnotations = (node: t.Node): void => {\n if (\"typeParameters\" in node) {\n delete node.typeParameters;\n }\n if (\"typeAnnotation\" in node) {\n delete node.typeAnnotation;\n }\n if (\"returnType\" in node) {\n delete node.returnType;\n }\n};\n\nexport const buildObjectExpression = <K extends string>(\n properties: Record<K, t.Expression | t.PatternLike>,\n): t.ObjectExpression =>\n t.objectExpression(\n Object.entries<t.Expression | t.PatternLike>(properties).map(([key, value]) => t.objectProperty(t.identifier(key), value)),\n );\n","import { types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type { GraphqlSystemIdentifyHelper } from \"@soda-gql/builder\";\n\nconst RUNTIME_MODULE = \"@soda-gql/runtime\";\n\n/**\n * Ensure that the gqlRuntime require exists in the program for CJS output.\n * Injects: const __soda_gql_runtime = require(\"@soda-gql/runtime\");\n */\nexport const ensureGqlRuntimeRequire = (programPath: NodePath<t.Program>) => {\n // Check if the require already exists\n const existing = programPath.node.body.find(\n (statement): statement is t.VariableDeclaration =>\n t.isVariableDeclaration(statement) &&\n statement.declarations.some((decl) => {\n if (!t.isIdentifier(decl.id) || decl.id.name !== \"__soda_gql_runtime\") {\n return false;\n }\n if (!decl.init || !t.isCallExpression(decl.init)) {\n return false;\n }\n const callExpr = decl.init;\n if (!t.isIdentifier(callExpr.callee) || callExpr.callee.name !== \"require\") {\n return false;\n }\n const arg = callExpr.arguments[0];\n return arg && t.isStringLiteral(arg) && arg.value === RUNTIME_MODULE;\n }),\n );\n\n if (existing) {\n return;\n }\n\n // Create: const __soda_gql_runtime = require(\"@soda-gql/runtime\");\n const requireCall = t.callExpression(t.identifier(\"require\"), [t.stringLiteral(RUNTIME_MODULE)]);\n\n const variableDeclaration = t.variableDeclaration(\"const\", [\n t.variableDeclarator(t.identifier(\"__soda_gql_runtime\"), requireCall),\n ]);\n\n // Insert at the beginning of the file\n programPath.node.body.unshift(variableDeclaration);\n};\n\n/**\n * Ensure that the gqlRuntime import exists in the program.\n * gqlRuntime is always imported from @soda-gql/runtime.\n */\nexport const ensureGqlRuntimeImport = (programPath: NodePath<t.Program>) => {\n const existing = programPath.node.body.find(\n (statement) => statement.type === \"ImportDeclaration\" && statement.source.value === RUNTIME_MODULE,\n );\n\n if (existing && t.isImportDeclaration(existing)) {\n const hasSpecifier = existing.specifiers.some(\n (specifier) =>\n specifier.type === \"ImportSpecifier\" &&\n specifier.imported.type === \"Identifier\" &&\n specifier.imported.name === \"gqlRuntime\",\n );\n\n if (!hasSpecifier) {\n existing.specifiers = [...existing.specifiers, t.importSpecifier(t.identifier(\"gqlRuntime\"), t.identifier(\"gqlRuntime\"))];\n }\n\n return;\n }\n\n programPath.node.body.unshift(\n t.importDeclaration(\n [t.importSpecifier(t.identifier(\"gqlRuntime\"), t.identifier(\"gqlRuntime\"))],\n t.stringLiteral(RUNTIME_MODULE),\n ),\n );\n};\n\n/**\n * Remove the graphql-system import (runtimeModule) and gql-related exports from the program.\n * After transformation, gqlRuntime is imported from @soda-gql/runtime instead,\n * so the original graphql-system import should be completely removed.\n *\n * This handles both ESM imports and CommonJS require() statements.\n */\nexport const removeGraphqlSystemImports = (\n programPath: NodePath<t.Program>,\n graphqlSystemIdentifyHelper: GraphqlSystemIdentifyHelper,\n filename: string,\n) => {\n // After transformation, all gql usage should be replaced with gqlRuntime\n // So we can safely remove the graphql-system import\n const toRemove: NodePath[] = [];\n\n programPath.traverse({\n // Remove ESM import declarations for the graphql-system\n ImportDeclaration(path) {\n if (t.isStringLiteral(path.node.source)) {\n const isGraphqlSystem = graphqlSystemIdentifyHelper.isGraphqlSystemImportSpecifier({\n filePath: filename,\n specifier: path.node.source.value,\n });\n if (isGraphqlSystem) {\n toRemove.push(path);\n }\n }\n },\n // Remove CommonJS require() statements for the graphql-system\n // - const graphql_system_1 = require(\"../../graphql-system\");\n // - const { gql } = require(\"@/graphql-system\");\n VariableDeclaration(path) {\n const shouldRemove = path.node.declarations.every((decl) => {\n const specifier = extractRequireTargetSpecifier(decl.init);\n if (!specifier) {\n return false;\n }\n\n return graphqlSystemIdentifyHelper.isGraphqlSystemImportSpecifier({\n filePath: filename,\n specifier: specifier,\n });\n });\n\n if (shouldRemove) {\n toRemove.push(path);\n }\n },\n });\n\n for (const path of toRemove) {\n path.remove();\n }\n};\n\n/**\n * Check if an expression is a require() call and extract its target specifier.\n * Handles multiple patterns:\n * - require(\"@/graphql-system\")\n * - Object(require(\"@/graphql-system\")) (interop helper pattern)\n */\nconst extractRequireTargetSpecifier = (expr: t.Node | null | undefined): string | undefined => {\n if (!expr) {\n return undefined;\n }\n\n // Direct require(\"@/graphql-system\")\n if (t.isCallExpression(expr)) {\n if (t.isIdentifier(expr.callee) && expr.callee.name === \"require\") {\n const arg = expr.arguments[0];\n if (arg && t.isStringLiteral(arg)) {\n return arg.value;\n }\n }\n\n // Object(require(\"@/graphql-system\")) or similar interop helpers\n if (t.isIdentifier(expr.callee) && (expr.callee.name === \"Object\" || expr.callee.name.startsWith(\"__import\"))) {\n const arg = expr.arguments[0];\n if (arg && t.isCallExpression(arg)) {\n if (t.isIdentifier(arg.callee) && arg.callee.name === \"require\") {\n const requireArg = arg.arguments[0];\n if (requireArg && t.isStringLiteral(requireArg)) {\n return requireArg.value;\n }\n }\n }\n }\n }\n\n return undefined;\n};\n","import { types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type { GqlDefinitionMetadata } from \"@soda-gql/builder/plugin-support\";\nimport { type CanonicalPathTracker, createCanonicalTracker } from \"@soda-gql/common\";\n\nexport type GqlDefinitionMetadataMap = WeakMap<t.CallExpression, GqlDefinitionMetadata>;\n\ntype CanonicalTrackerFactory = typeof createCanonicalTracker;\n\ntype CollectArgs = {\n readonly programPath: NodePath<t.Program>;\n readonly filename: string;\n readonly createTracker?: CanonicalTrackerFactory;\n};\n\ntype ScopeHandle = ReturnType<CanonicalPathTracker[\"enterScope\"]>;\ntype ExportBindingMap = Map<string, string>;\n\nexport const collectGqlDefinitionMetadata = ({ programPath, filename, createTracker }: CollectArgs): GqlDefinitionMetadataMap => {\n const exportBindings = collectExportBindings(programPath.node);\n const trackerFactory = createTracker ?? createCanonicalTracker;\n const tracker = trackerFactory({\n filePath: filename,\n getExportName: (localName) => exportBindings.get(localName),\n });\n\n const getAnonymousName = createAnonymousNameFactory();\n const scopeHandles = new WeakMap<NodePath<t.Node>, ScopeHandle>();\n const metadata = new WeakMap<t.CallExpression, GqlDefinitionMetadata>();\n\n programPath.traverse({\n enter(path) {\n if (path.isCallExpression() && isGqlDefinitionCall(path.node)) {\n const depthBeforeRegister = tracker.currentDepth();\n const { astPath } = tracker.registerDefinition();\n const isTopLevel = depthBeforeRegister <= 1;\n const exportInfo = isTopLevel ? resolveTopLevelExport(path, exportBindings) : null;\n\n metadata.set(path.node, {\n astPath,\n isTopLevel,\n isExported: exportInfo?.isExported ?? false,\n exportBinding: exportInfo?.exportBinding,\n });\n\n path.skip();\n return;\n }\n\n const handle = maybeEnterScope(path, tracker, getAnonymousName);\n if (handle) {\n scopeHandles.set(path, handle);\n }\n },\n exit(path) {\n const handle = scopeHandles.get(path);\n if (handle) {\n tracker.exitScope(handle);\n scopeHandles.delete(path);\n }\n },\n });\n\n return metadata;\n};\n\nconst collectExportBindings = (program: t.Program): ExportBindingMap => {\n const bindings: ExportBindingMap = new Map();\n\n for (const statement of program.body) {\n // ESM exports: export const foo = ...\n if (t.isExportNamedDeclaration(statement) && statement.declaration) {\n const { declaration } = statement;\n if (t.isVariableDeclaration(declaration)) {\n for (const declarator of declaration.declarations) {\n if (t.isIdentifier(declarator.id)) {\n bindings.set(declarator.id.name, declarator.id.name);\n }\n }\n continue;\n }\n\n if ((t.isFunctionDeclaration(declaration) || t.isClassDeclaration(declaration)) && declaration.id) {\n bindings.set(declaration.id.name, declaration.id.name);\n }\n continue;\n }\n\n // CommonJS exports: exports.foo = ... or module.exports.foo = ...\n if (t.isExpressionStatement(statement) && t.isAssignmentExpression(statement.expression)) {\n const exportName = getCommonJsExportName(statement.expression.left);\n if (exportName) {\n bindings.set(exportName, exportName);\n }\n }\n }\n\n return bindings;\n};\n\nconst getCommonJsExportName = (node: t.LVal | t.OptionalMemberExpression): string | null => {\n if (!t.isMemberExpression(node) || node.computed) {\n return null;\n }\n\n // Check if it's exports.foo or module.exports.foo\n const isExports = t.isIdentifier(node.object, { name: \"exports\" });\n const isModuleExports =\n t.isMemberExpression(node.object) &&\n t.isIdentifier(node.object.object, { name: \"module\" }) &&\n t.isIdentifier(node.object.property, { name: \"exports\" });\n\n if (!isExports && !isModuleExports) {\n return null;\n }\n\n // Extract property name\n if (t.isIdentifier(node.property)) {\n return node.property.name;\n }\n if (t.isStringLiteral(node.property)) {\n return node.property.value;\n }\n\n return null;\n};\n\nconst createAnonymousNameFactory = (): ((kind: string) => string) => {\n const counters = new Map<string, number>();\n return (kind) => {\n const count = counters.get(kind) ?? 0;\n counters.set(kind, count + 1);\n return `${kind}#${count}`;\n };\n};\n\nconst isGqlDefinitionCall = (node: t.Node): node is t.CallExpression =>\n t.isCallExpression(node) &&\n t.isMemberExpression(node.callee) &&\n isGqlReference(node.callee.object) &&\n node.arguments.length > 0 &&\n t.isArrowFunctionExpression(node.arguments[0]);\n\nconst isGqlReference = (expr: t.Expression | t.Super): boolean => {\n if (t.isIdentifier(expr, { name: \"gql\" })) {\n return true;\n }\n if (!t.isMemberExpression(expr) || expr.computed) {\n return false;\n }\n if (\n (t.isIdentifier(expr.property) && expr.property.name === \"gql\") ||\n (t.isStringLiteral(expr.property) && expr.property.value === \"gql\")\n ) {\n return true;\n }\n return isGqlReference(expr.object);\n};\n\nconst resolveTopLevelExport = (\n callPath: NodePath<t.CallExpression>,\n exportBindings: ExportBindingMap,\n): { readonly isExported: true; readonly exportBinding: string } | null => {\n // ESM: const foo = gql.default(...); export { foo };\n const declarator = callPath.parentPath;\n if (declarator?.isVariableDeclarator()) {\n const { id } = declarator.node;\n if (t.isIdentifier(id)) {\n const exportBinding = exportBindings.get(id.name);\n if (exportBinding) {\n return { isExported: true, exportBinding };\n }\n }\n }\n\n // CommonJS: exports.foo = gql.default(...);\n const assignment = callPath.parentPath;\n if (assignment?.isAssignmentExpression()) {\n const exportName = getCommonJsExportName(assignment.node.left);\n if (exportName && exportBindings.has(exportName)) {\n return { isExported: true, exportBinding: exportName };\n }\n }\n\n return null;\n};\n\nconst maybeEnterScope = (\n path: NodePath<t.Node>,\n tracker: CanonicalPathTracker,\n getAnonymousName: (kind: string) => string,\n): ScopeHandle | null => {\n // CommonJS exports: exports.foo = ... or module.exports.foo = ...\n if (path.isAssignmentExpression()) {\n const exportName = getCommonJsExportName(path.node.left);\n if (exportName) {\n return tracker.enterScope({ segment: exportName, kind: \"variable\", stableKey: `var:${exportName}` });\n }\n }\n\n if (path.isVariableDeclarator() && t.isIdentifier(path.node.id)) {\n const name = path.node.id.name;\n return tracker.enterScope({ segment: name, kind: \"variable\", stableKey: `var:${name}` });\n }\n\n if (path.isArrowFunctionExpression()) {\n const name = getAnonymousName(\"arrow\");\n return tracker.enterScope({ segment: name, kind: \"function\", stableKey: \"arrow\" });\n }\n\n if (path.isFunctionDeclaration() || path.isFunctionExpression()) {\n const explicitName = path.node.id?.name;\n const name = explicitName ?? getAnonymousName(\"function\");\n return tracker.enterScope({ segment: name, kind: \"function\", stableKey: `func:${name}` });\n }\n\n if (path.isClassDeclaration()) {\n const explicitName = path.node.id?.name;\n const name = explicitName ?? getAnonymousName(\"class\");\n return tracker.enterScope({ segment: name, kind: \"class\", stableKey: `class:${name}` });\n }\n\n if (path.isClassMethod() && t.isIdentifier(path.node.key)) {\n const name = path.node.key.name;\n return tracker.enterScope({ segment: name, kind: \"method\", stableKey: `member:${name}` });\n }\n\n if (path.isClassProperty() && t.isIdentifier(path.node.key)) {\n const name = path.node.key.name;\n return tracker.enterScope({ segment: name, kind: \"property\", stableKey: `member:${name}` });\n }\n\n if (path.isObjectProperty()) {\n const key = path.node.key;\n const name = t.isIdentifier(key) ? key.name : t.isStringLiteral(key) ? key.value : null;\n if (name) {\n return tracker.enterScope({ segment: name, kind: \"property\", stableKey: `prop:${name}` });\n }\n }\n\n return null;\n};\n","import { types as t } from \"@babel/core\";\nimport type { PluginError } from \"@soda-gql/builder/plugin-support\";\nimport { ok, type Result } from \"neverthrow\";\nimport type { BabelGqlCallFragment, BabelGqlCallOperation } from \"./analysis\";\nimport { buildObjectExpression } from \"./ast\";\n\nexport const buildFragmentRuntimeCall = ({\n artifact,\n}: BabelGqlCallFragment & { filename: string }): Result<t.Expression, PluginError> => {\n const prebuildProps: Record<string, t.Expression> = {\n typename: t.stringLiteral(artifact.prebuild.typename),\n };\n if (artifact.prebuild.key !== undefined) {\n prebuildProps.key = t.stringLiteral(artifact.prebuild.key);\n }\n\n return ok(\n t.callExpression(t.memberExpression(t.identifier(\"gqlRuntime\"), t.identifier(\"fragment\")), [\n buildObjectExpression({\n prebuild: buildObjectExpression(prebuildProps),\n }),\n ]),\n );\n};\n\nexport const buildOperationRuntimeComponents = ({\n artifact,\n}: BabelGqlCallOperation & { filename: string }): Result<\n { referenceCall: t.Expression; runtimeCall: t.Expression },\n PluginError\n> => {\n const runtimeCall = t.callExpression(t.memberExpression(t.identifier(\"gqlRuntime\"), t.identifier(\"operation\")), [\n buildObjectExpression({\n prebuild: t.callExpression(t.memberExpression(t.identifier(\"JSON\"), t.identifier(\"parse\")), [\n t.stringLiteral(JSON.stringify(artifact.prebuild)),\n ]),\n runtime: buildObjectExpression({}),\n }),\n ]);\n\n const referenceCall = t.callExpression(t.memberExpression(t.identifier(\"gqlRuntime\"), t.identifier(\"getOperation\")), [\n t.stringLiteral(artifact.prebuild.operationName),\n ]);\n\n return ok({\n referenceCall,\n runtimeCall,\n });\n};\n","import type { types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type { PluginError } from \"@soda-gql/builder/plugin-support\";\nimport { err, ok, type Result } from \"neverthrow\";\nimport type { ArtifactLookup, BabelGqlCall } from \"./analysis\";\nimport { extractGqlCall } from \"./analysis\";\nimport type { GqlDefinitionMetadataMap } from \"./metadata\";\nimport { buildFragmentRuntimeCall, buildOperationRuntimeComponents } from \"./runtime\";\n\ntype TransformCallExpressionArgs = {\n readonly callPath: NodePath<t.CallExpression>;\n readonly filename: string;\n readonly metadata: GqlDefinitionMetadataMap;\n readonly getArtifact: ArtifactLookup;\n};\n\ntype TransformCallExpressionResult =\n | { readonly transformed: false }\n | { readonly transformed: true; readonly runtimeCall?: t.Expression };\n\nexport const transformCallExpression = ({\n callPath,\n filename,\n metadata,\n getArtifact,\n}: TransformCallExpressionArgs): Result<TransformCallExpressionResult, PluginError> => {\n // Skip if this call doesn't have GQL metadata\n if (!metadata.has(callPath.node)) {\n return ok({ transformed: false });\n }\n\n const gqlCallResult = extractGqlCall({\n nodePath: callPath,\n filename,\n metadata,\n getArtifact,\n });\n\n if (gqlCallResult.isErr()) {\n return err(gqlCallResult.error);\n }\n\n const gqlCall = gqlCallResult.value;\n\n return replaceWithRuntimeCall(callPath, gqlCall, filename);\n};\n\nconst replaceWithRuntimeCall = (\n callPath: NodePath<t.CallExpression>,\n gqlCall: BabelGqlCall,\n filename: string,\n): Result<TransformCallExpressionResult, PluginError> => {\n if (gqlCall.type === \"fragment\") {\n const result = buildFragmentRuntimeCall({ ...gqlCall, filename });\n if (result.isErr()) {\n return err(result.error);\n }\n callPath.replaceWith(result.value);\n return ok({ transformed: true });\n }\n\n if (gqlCall.type === \"operation\") {\n const result = buildOperationRuntimeComponents({ ...gqlCall, filename });\n if (result.isErr()) {\n return err(result.error);\n }\n const { referenceCall, runtimeCall } = result.value;\n callPath.replaceWith(referenceCall);\n return ok({ transformed: true, runtimeCall });\n }\n\n return ok({ transformed: false });\n};\n\nexport const insertRuntimeCalls = (programPath: NodePath<t.Program>, runtimeCalls: readonly t.Expression[]): void => {\n if (runtimeCalls.length === 0) {\n return;\n }\n\n programPath.traverse({\n ImportDeclaration(importDeclPath) {\n if (importDeclPath.node.source.value === \"@soda-gql/runtime\") {\n importDeclPath.insertAfter([...runtimeCalls]);\n }\n },\n });\n};\n","/**\n * Babel implementation of the TransformAdapter interface.\n *\n * This adapter wraps the existing Babel-specific transformation logic\n * for soda-gql zero-runtime transformations.\n */\n\nimport type { types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport { createGraphqlSystemIdentifyHelper } from \"@soda-gql/builder\";\nimport { formatPluginError } from \"@soda-gql/builder/plugin-support\";\nimport type { ResolvedSodaGqlConfig } from \"@soda-gql/config\";\nimport { ensureGqlRuntimeImport, removeGraphqlSystemImports } from \"./ast/imports\";\nimport { collectGqlDefinitionMetadata } from \"./ast/metadata\";\nimport { transformCallExpression } from \"./ast/transformer\";\nimport type { TransformPassResult, TransformProgramContext } from \"./types\";\n\n/**\n * Creates a Babel transformer with a single transform() method.\n * This matches the pattern used in the TypeScript plugin.\n */\nexport const createTransformer = ({\n programPath,\n types,\n config,\n}: {\n readonly programPath: NodePath<t.Program>;\n readonly types: typeof t;\n readonly config: ResolvedSodaGqlConfig;\n}) => {\n // Create graphql system identify helper using builder's implementation\n const graphqlSystemIdentifyHelper = createGraphqlSystemIdentifyHelper(config);\n\n /**\n * Check if a node is a require() or __webpack_require__() call.\n */\n const isRequireCall = (node: t.Node): boolean => {\n if (!types.isCallExpression(node)) {\n return false;\n }\n\n const callee = node.callee;\n return types.isIdentifier(callee) && (callee.name === \"require\" || callee.name === \"__webpack_require__\");\n };\n\n /**\n * Find the last statement that loads a module (import or require).\n * Handles both ESM imports and CommonJS require() calls.\n */\n const findLastModuleLoader = (): NodePath<t.Statement> | null => {\n const bodyPaths = programPath.get(\"body\");\n let lastLoader: NodePath<t.Statement> | null = null;\n\n for (const path of bodyPaths) {\n // ESM: import declaration\n if (path.isImportDeclaration()) {\n lastLoader = path;\n continue;\n }\n\n // CommonJS: const foo = require(\"bar\") or const foo = __webpack_require__(123)\n if (path.isVariableDeclaration()) {\n for (const declarator of path.node.declarations) {\n if (declarator.init && isRequireCall(declarator.init)) {\n lastLoader = path;\n break;\n }\n }\n continue;\n }\n\n // CommonJS: require(\"bar\") or __webpack_require__(123) as standalone expression\n if (path.isExpressionStatement()) {\n if (isRequireCall(path.node.expression)) {\n lastLoader = path;\n }\n }\n }\n\n return lastLoader;\n };\n\n return {\n transform: (context: TransformProgramContext): TransformPassResult => {\n const metadata = collectGqlDefinitionMetadata({\n programPath,\n filename: context.filename,\n });\n\n const runtimeCalls: t.Expression[] = [];\n let transformed = false;\n\n // Transform all gql call expressions\n programPath.traverse({\n CallExpression: (callPath) => {\n const result = transformCallExpression({\n callPath,\n filename: context.filename,\n metadata,\n getArtifact: context.artifactLookup,\n });\n\n if (result.isErr()) {\n // Log error and continue - don't fail the entire build for a single error\n console.error(`[@soda-gql/babel] ${formatPluginError(result.error)}`);\n return;\n }\n\n const transformResult = result.value;\n if (transformResult.transformed) {\n transformed = true;\n\n if (transformResult.runtimeCall) {\n runtimeCalls.push(transformResult.runtimeCall);\n }\n }\n },\n });\n\n if (!transformed) {\n return { transformed: false, runtimeArtifacts: undefined };\n }\n\n // Ensure runtime import\n ensureGqlRuntimeImport(programPath);\n\n // Insert runtime side effects immediately if any\n if (runtimeCalls.length > 0) {\n const statements = runtimeCalls.map((expr) => types.expressionStatement(expr));\n const lastLoaderPath = findLastModuleLoader();\n\n if (lastLoaderPath) {\n lastLoaderPath.insertAfter(statements);\n } else {\n programPath.unshiftContainer(\"body\", statements);\n }\n }\n\n // Clean up: remove graphql system imports and recrawl scope\n programPath.scope.crawl();\n removeGraphqlSystemImports(programPath, graphqlSystemIdentifyHelper, context.filename);\n\n return {\n transformed: true,\n runtimeArtifacts: undefined,\n };\n },\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,MAAa,kBAAkB,EAC7B,UACA,UACA,UACA,kBAC2D;CAC3D,MAAM,iBAAiB,SAAS;CAEhC,MAAM,OAAO,SAAS,IAAI,eAAe;AACzC,KAAI,CAAC,KACH,4BAAW,2BAA2B,EAAE,UAAU,CAAC,CAAC;CAGtD,MAAM,wEAAiC,UAAU,KAAK,QAAQ;CAC9D,MAAM,WAAW,YAAY,YAAY;AAEzC,KAAI,CAAC,SACH,4BAAW,2BAA2B;EAAE;EAAU;EAAa,CAAC,CAAC;AAGnE,KAAI,SAAS,SAAS,WACpB,2BAAU;EAAE;EAAU;EAAa,MAAM;EAAY;EAAU,CAAC;AAGlE,KAAI,SAAS,SAAS,YACpB,2BAAU;EAAE;EAAU;EAAa,MAAM;EAAa;EAAU,CAAC;AAGnE,4BACE,mCAAmC;EACjC;EACA;EACA,cAAe,SAA8B;EAC9C,CAAC,CACH;;AAWH,MAAM,8BAA8B,EAAE,gBAAwE;CAC5G,MAAM;CACN,OAAO;CACP,MAAM;CACN,SAAS,iCAAiC;CAC1C,OAAO,EAAE,UAAU;CACnB;CACD;AAED,MAAM,8BAA8B,EAClC,UACA,mBACoE;CACpE,MAAM;CACN,OAAO;CACP,MAAM;CACN,SAAS,8CAA8C;CACvD,OAAO;EAAE;EAAU;EAAa;CAChC;CACA;CACD;AAED,MAAM,sCAAsC,EAC1C,UACA,aACA,oBAC+E;CAC/E,MAAM;CACN,OAAO;CACP,MAAM;CACN,SAAS,sCAAsC,aAAa,qBAAqB;CACjF,OAAO;EAAE;EAAU;EAAa;EAAc;CAC9C;CACA;CACA;CACD;;;;AC5GD,MAAM,mCAAmC,eAAiE;CACxG,MAAM;CACN,OAAO;CACP,MAAM;CACN,SAAS,2BAA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,SAAK,MAAM,cAAc,KAAK,KAAK,aACjC,KAAI,WAAW,QAAQ,cAAc,WAAW,KAAK,EAAE;AACrD,kBAAa;AACb;;AAGJ;;AAIF,OAAI,KAAK,uBAAuB,EAC9B;QAAI,cAAc,KAAK,KAAK,WAAW,CACrC,cAAa;;;AAKnB,SAAO;;AAGT,QAAO,EACL,YAAY,YAA0D;EACpE,MAAM,WAAW,6BAA6B;GAC5C;GACA,UAAU,QAAQ;GACnB,CAAC;EAEF,MAAMC,eAA+B,EAAE;EACvC,IAAI,cAAc;AAGlB,cAAY,SAAS,EACnB,iBAAiB,aAAa;GAC5B,MAAM,SAAS,wBAAwB;IACrC;IACA,UAAU,QAAQ;IAClB;IACA,aAAa,QAAQ;IACtB,CAAC;AAEF,OAAI,OAAO,OAAO,EAAE;AAElB,YAAQ,MAAM,8EAAuC,OAAO,MAAM,GAAG;AACrE;;GAGF,MAAM,kBAAkB,OAAO;AAC/B,OAAI,gBAAgB,aAAa;AAC/B,kBAAc;AAEd,QAAI,gBAAgB,YAClB,cAAa,KAAK,gBAAgB,YAAY;;KAIrD,CAAC;AAEF,MAAI,CAAC,YACH,QAAO;GAAE,aAAa;GAAO,kBAAkB;GAAW;AAI5D,yBAAuB,YAAY;AAGnC,MAAI,aAAa,SAAS,GAAG;GAC3B,MAAM,aAAa,aAAa,KAAK,SAAS,MAAM,oBAAoB,KAAK,CAAC;GAC9E,MAAM,iBAAiB,sBAAsB;AAE7C,OAAI,eACF,gBAAe,YAAY,WAAW;OAEtC,aAAY,iBAAiB,QAAQ,WAAW;;AAKpD,cAAY,MAAM,OAAO;AACzB,6BAA2B,aAAa,6BAA6B,QAAQ,SAAS;AAEtF,SAAO;GACL,aAAa;GACb,kBAAkB;GACnB;IAEJ"}
|
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
import { formatPluginError, resolveCanonicalId } from "@soda-gql/builder/plugin-support";
|
|
2
|
+
import { err, ok } from "neverthrow";
|
|
3
|
+
import * as t from "@babel/types";
|
|
4
|
+
import { types } from "@babel/core";
|
|
5
|
+
import { createCanonicalTracker } from "@soda-gql/common";
|
|
6
|
+
import { createGraphqlSystemIdentifyHelper } from "@soda-gql/builder";
|
|
7
|
+
|
|
8
|
+
//#region packages/babel/src/ast/analysis.ts
|
|
9
|
+
const extractGqlCall = ({ nodePath, filename, metadata, getArtifact }) => {
|
|
10
|
+
const callExpression = nodePath.node;
|
|
11
|
+
const meta = metadata.get(callExpression);
|
|
12
|
+
if (!meta) return err(createMetadataMissingError({ filename }));
|
|
13
|
+
const canonicalId = resolveCanonicalId(filename, meta.astPath);
|
|
14
|
+
const artifact = getArtifact(canonicalId);
|
|
15
|
+
if (!artifact) return err(createArtifactMissingError({
|
|
16
|
+
filename,
|
|
17
|
+
canonicalId
|
|
18
|
+
}));
|
|
19
|
+
if (artifact.type === "fragment") return ok({
|
|
20
|
+
nodePath,
|
|
21
|
+
canonicalId,
|
|
22
|
+
type: "fragment",
|
|
23
|
+
artifact
|
|
24
|
+
});
|
|
25
|
+
if (artifact.type === "operation") return ok({
|
|
26
|
+
nodePath,
|
|
27
|
+
canonicalId,
|
|
28
|
+
type: "operation",
|
|
29
|
+
artifact
|
|
30
|
+
});
|
|
31
|
+
return err(createUnsupportedArtifactTypeError({
|
|
32
|
+
filename,
|
|
33
|
+
canonicalId,
|
|
34
|
+
artifactType: artifact.type
|
|
35
|
+
}));
|
|
36
|
+
};
|
|
37
|
+
const createMetadataMissingError = ({ filename }) => ({
|
|
38
|
+
type: "PluginError",
|
|
39
|
+
stage: "analysis",
|
|
40
|
+
code: "SODA_GQL_METADATA_NOT_FOUND",
|
|
41
|
+
message: `No GraphQL metadata found for ${filename}`,
|
|
42
|
+
cause: { filename },
|
|
43
|
+
filename
|
|
44
|
+
});
|
|
45
|
+
const createArtifactMissingError = ({ filename, canonicalId }) => ({
|
|
46
|
+
type: "PluginError",
|
|
47
|
+
stage: "analysis",
|
|
48
|
+
code: "SODA_GQL_ANALYSIS_ARTIFACT_NOT_FOUND",
|
|
49
|
+
message: `No builder artifact found for canonical ID ${canonicalId}`,
|
|
50
|
+
cause: {
|
|
51
|
+
filename,
|
|
52
|
+
canonicalId
|
|
53
|
+
},
|
|
54
|
+
filename,
|
|
55
|
+
canonicalId
|
|
56
|
+
});
|
|
57
|
+
const createUnsupportedArtifactTypeError = ({ filename, canonicalId, artifactType }) => ({
|
|
58
|
+
type: "PluginError",
|
|
59
|
+
stage: "analysis",
|
|
60
|
+
code: "SODA_GQL_UNSUPPORTED_ARTIFACT_TYPE",
|
|
61
|
+
message: `Unsupported builder artifact type "${artifactType}" for canonical ID ${canonicalId}`,
|
|
62
|
+
cause: {
|
|
63
|
+
filename,
|
|
64
|
+
canonicalId,
|
|
65
|
+
artifactType
|
|
66
|
+
},
|
|
67
|
+
filename,
|
|
68
|
+
canonicalId,
|
|
69
|
+
artifactType
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region packages/babel/src/ast/ast.ts
|
|
74
|
+
const createUnsupportedValueTypeError = (valueType) => ({
|
|
75
|
+
type: "PluginError",
|
|
76
|
+
stage: "transform",
|
|
77
|
+
code: "SODA_GQL_TRANSFORM_UNSUPPORTED_VALUE_TYPE",
|
|
78
|
+
message: `Unsupported value type: ${valueType}`,
|
|
79
|
+
cause: { valueType },
|
|
80
|
+
valueType
|
|
81
|
+
});
|
|
82
|
+
const buildLiteralFromValue = (value) => {
|
|
83
|
+
if (value === null) return ok(t.nullLiteral());
|
|
84
|
+
if (typeof value === "string") return ok(t.stringLiteral(value));
|
|
85
|
+
if (typeof value === "number") return ok(t.numericLiteral(value));
|
|
86
|
+
if (typeof value === "boolean") return ok(t.booleanLiteral(value));
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
const elements = [];
|
|
89
|
+
for (const item of value) {
|
|
90
|
+
const result = buildLiteralFromValue(item);
|
|
91
|
+
if (result.isErr()) return result;
|
|
92
|
+
elements.push(result.value);
|
|
93
|
+
}
|
|
94
|
+
return ok(t.arrayExpression(elements));
|
|
95
|
+
}
|
|
96
|
+
if (typeof value === "object") {
|
|
97
|
+
const properties = [];
|
|
98
|
+
for (const [key, val] of Object.entries(value)) {
|
|
99
|
+
const result = buildLiteralFromValue(val);
|
|
100
|
+
if (result.isErr()) return result;
|
|
101
|
+
properties.push(t.objectProperty(t.identifier(key), result.value));
|
|
102
|
+
}
|
|
103
|
+
return ok(t.objectExpression(properties));
|
|
104
|
+
}
|
|
105
|
+
return err(createUnsupportedValueTypeError(typeof value));
|
|
106
|
+
};
|
|
107
|
+
const clone = (node) => t.cloneNode(node, true);
|
|
108
|
+
const cloneCallExpression = (node) => t.callExpression(clone(node.callee), node.arguments.map(clone));
|
|
109
|
+
const stripTypeAnnotations = (node) => {
|
|
110
|
+
if ("typeParameters" in node) delete node.typeParameters;
|
|
111
|
+
if ("typeAnnotation" in node) delete node.typeAnnotation;
|
|
112
|
+
if ("returnType" in node) delete node.returnType;
|
|
113
|
+
};
|
|
114
|
+
const buildObjectExpression = (properties) => t.objectExpression(Object.entries(properties).map(([key, value]) => t.objectProperty(t.identifier(key), value)));
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region packages/babel/src/ast/imports.ts
|
|
118
|
+
const RUNTIME_MODULE = "@soda-gql/runtime";
|
|
119
|
+
/**
|
|
120
|
+
* Ensure that the gqlRuntime require exists in the program for CJS output.
|
|
121
|
+
* Injects: const __soda_gql_runtime = require("@soda-gql/runtime");
|
|
122
|
+
*/
|
|
123
|
+
const ensureGqlRuntimeRequire = (programPath) => {
|
|
124
|
+
if (programPath.node.body.find((statement) => types.isVariableDeclaration(statement) && statement.declarations.some((decl) => {
|
|
125
|
+
if (!types.isIdentifier(decl.id) || decl.id.name !== "__soda_gql_runtime") return false;
|
|
126
|
+
if (!decl.init || !types.isCallExpression(decl.init)) return false;
|
|
127
|
+
const callExpr = decl.init;
|
|
128
|
+
if (!types.isIdentifier(callExpr.callee) || callExpr.callee.name !== "require") return false;
|
|
129
|
+
const arg = callExpr.arguments[0];
|
|
130
|
+
return arg && types.isStringLiteral(arg) && arg.value === RUNTIME_MODULE;
|
|
131
|
+
}))) return;
|
|
132
|
+
const requireCall = types.callExpression(types.identifier("require"), [types.stringLiteral(RUNTIME_MODULE)]);
|
|
133
|
+
const variableDeclaration = types.variableDeclaration("const", [types.variableDeclarator(types.identifier("__soda_gql_runtime"), requireCall)]);
|
|
134
|
+
programPath.node.body.unshift(variableDeclaration);
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Ensure that the gqlRuntime import exists in the program.
|
|
138
|
+
* gqlRuntime is always imported from @soda-gql/runtime.
|
|
139
|
+
*/
|
|
140
|
+
const ensureGqlRuntimeImport = (programPath) => {
|
|
141
|
+
const existing = programPath.node.body.find((statement) => statement.type === "ImportDeclaration" && statement.source.value === RUNTIME_MODULE);
|
|
142
|
+
if (existing && types.isImportDeclaration(existing)) {
|
|
143
|
+
if (!existing.specifiers.some((specifier) => specifier.type === "ImportSpecifier" && specifier.imported.type === "Identifier" && specifier.imported.name === "gqlRuntime")) existing.specifiers = [...existing.specifiers, types.importSpecifier(types.identifier("gqlRuntime"), types.identifier("gqlRuntime"))];
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
programPath.node.body.unshift(types.importDeclaration([types.importSpecifier(types.identifier("gqlRuntime"), types.identifier("gqlRuntime"))], types.stringLiteral(RUNTIME_MODULE)));
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Remove the graphql-system import (runtimeModule) and gql-related exports from the program.
|
|
150
|
+
* After transformation, gqlRuntime is imported from @soda-gql/runtime instead,
|
|
151
|
+
* so the original graphql-system import should be completely removed.
|
|
152
|
+
*
|
|
153
|
+
* This handles both ESM imports and CommonJS require() statements.
|
|
154
|
+
*/
|
|
155
|
+
const removeGraphqlSystemImports = (programPath, graphqlSystemIdentifyHelper, filename) => {
|
|
156
|
+
const toRemove = [];
|
|
157
|
+
programPath.traverse({
|
|
158
|
+
ImportDeclaration(path) {
|
|
159
|
+
if (types.isStringLiteral(path.node.source)) {
|
|
160
|
+
if (graphqlSystemIdentifyHelper.isGraphqlSystemImportSpecifier({
|
|
161
|
+
filePath: filename,
|
|
162
|
+
specifier: path.node.source.value
|
|
163
|
+
})) toRemove.push(path);
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
VariableDeclaration(path) {
|
|
167
|
+
if (path.node.declarations.every((decl) => {
|
|
168
|
+
const specifier = extractRequireTargetSpecifier(decl.init);
|
|
169
|
+
if (!specifier) return false;
|
|
170
|
+
return graphqlSystemIdentifyHelper.isGraphqlSystemImportSpecifier({
|
|
171
|
+
filePath: filename,
|
|
172
|
+
specifier
|
|
173
|
+
});
|
|
174
|
+
})) toRemove.push(path);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
for (const path of toRemove) path.remove();
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Check if an expression is a require() call and extract its target specifier.
|
|
181
|
+
* Handles multiple patterns:
|
|
182
|
+
* - require("@/graphql-system")
|
|
183
|
+
* - Object(require("@/graphql-system")) (interop helper pattern)
|
|
184
|
+
*/
|
|
185
|
+
const extractRequireTargetSpecifier = (expr) => {
|
|
186
|
+
if (!expr) return;
|
|
187
|
+
if (types.isCallExpression(expr)) {
|
|
188
|
+
if (types.isIdentifier(expr.callee) && expr.callee.name === "require") {
|
|
189
|
+
const arg = expr.arguments[0];
|
|
190
|
+
if (arg && types.isStringLiteral(arg)) return arg.value;
|
|
191
|
+
}
|
|
192
|
+
if (types.isIdentifier(expr.callee) && (expr.callee.name === "Object" || expr.callee.name.startsWith("__import"))) {
|
|
193
|
+
const arg = expr.arguments[0];
|
|
194
|
+
if (arg && types.isCallExpression(arg)) {
|
|
195
|
+
if (types.isIdentifier(arg.callee) && arg.callee.name === "require") {
|
|
196
|
+
const requireArg = arg.arguments[0];
|
|
197
|
+
if (requireArg && types.isStringLiteral(requireArg)) return requireArg.value;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
//#endregion
|
|
205
|
+
//#region packages/babel/src/ast/metadata.ts
|
|
206
|
+
const collectGqlDefinitionMetadata = ({ programPath, filename, createTracker }) => {
|
|
207
|
+
const exportBindings = collectExportBindings(programPath.node);
|
|
208
|
+
const tracker = (createTracker ?? createCanonicalTracker)({
|
|
209
|
+
filePath: filename,
|
|
210
|
+
getExportName: (localName) => exportBindings.get(localName)
|
|
211
|
+
});
|
|
212
|
+
const getAnonymousName = createAnonymousNameFactory();
|
|
213
|
+
const scopeHandles = /* @__PURE__ */ new WeakMap();
|
|
214
|
+
const metadata = /* @__PURE__ */ new WeakMap();
|
|
215
|
+
programPath.traverse({
|
|
216
|
+
enter(path) {
|
|
217
|
+
if (path.isCallExpression() && isGqlDefinitionCall(path.node)) {
|
|
218
|
+
const depthBeforeRegister = tracker.currentDepth();
|
|
219
|
+
const { astPath } = tracker.registerDefinition();
|
|
220
|
+
const isTopLevel = depthBeforeRegister <= 1;
|
|
221
|
+
const exportInfo = isTopLevel ? resolveTopLevelExport(path, exportBindings) : null;
|
|
222
|
+
metadata.set(path.node, {
|
|
223
|
+
astPath,
|
|
224
|
+
isTopLevel,
|
|
225
|
+
isExported: exportInfo?.isExported ?? false,
|
|
226
|
+
exportBinding: exportInfo?.exportBinding
|
|
227
|
+
});
|
|
228
|
+
path.skip();
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const handle = maybeEnterScope(path, tracker, getAnonymousName);
|
|
232
|
+
if (handle) scopeHandles.set(path, handle);
|
|
233
|
+
},
|
|
234
|
+
exit(path) {
|
|
235
|
+
const handle = scopeHandles.get(path);
|
|
236
|
+
if (handle) {
|
|
237
|
+
tracker.exitScope(handle);
|
|
238
|
+
scopeHandles.delete(path);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
return metadata;
|
|
243
|
+
};
|
|
244
|
+
const collectExportBindings = (program) => {
|
|
245
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
246
|
+
for (const statement of program.body) {
|
|
247
|
+
if (types.isExportNamedDeclaration(statement) && statement.declaration) {
|
|
248
|
+
const { declaration } = statement;
|
|
249
|
+
if (types.isVariableDeclaration(declaration)) {
|
|
250
|
+
for (const declarator of declaration.declarations) if (types.isIdentifier(declarator.id)) bindings.set(declarator.id.name, declarator.id.name);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if ((types.isFunctionDeclaration(declaration) || types.isClassDeclaration(declaration)) && declaration.id) bindings.set(declaration.id.name, declaration.id.name);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (types.isExpressionStatement(statement) && types.isAssignmentExpression(statement.expression)) {
|
|
257
|
+
const exportName = getCommonJsExportName(statement.expression.left);
|
|
258
|
+
if (exportName) bindings.set(exportName, exportName);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return bindings;
|
|
262
|
+
};
|
|
263
|
+
const getCommonJsExportName = (node) => {
|
|
264
|
+
if (!types.isMemberExpression(node) || node.computed) return null;
|
|
265
|
+
const isExports = types.isIdentifier(node.object, { name: "exports" });
|
|
266
|
+
const isModuleExports = types.isMemberExpression(node.object) && types.isIdentifier(node.object.object, { name: "module" }) && types.isIdentifier(node.object.property, { name: "exports" });
|
|
267
|
+
if (!isExports && !isModuleExports) return null;
|
|
268
|
+
if (types.isIdentifier(node.property)) return node.property.name;
|
|
269
|
+
if (types.isStringLiteral(node.property)) return node.property.value;
|
|
270
|
+
return null;
|
|
271
|
+
};
|
|
272
|
+
const createAnonymousNameFactory = () => {
|
|
273
|
+
const counters = /* @__PURE__ */ new Map();
|
|
274
|
+
return (kind) => {
|
|
275
|
+
const count = counters.get(kind) ?? 0;
|
|
276
|
+
counters.set(kind, count + 1);
|
|
277
|
+
return `${kind}#${count}`;
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
const isGqlDefinitionCall = (node) => types.isCallExpression(node) && types.isMemberExpression(node.callee) && isGqlReference(node.callee.object) && node.arguments.length > 0 && types.isArrowFunctionExpression(node.arguments[0]);
|
|
281
|
+
const isGqlReference = (expr) => {
|
|
282
|
+
if (types.isIdentifier(expr, { name: "gql" })) return true;
|
|
283
|
+
if (!types.isMemberExpression(expr) || expr.computed) return false;
|
|
284
|
+
if (types.isIdentifier(expr.property) && expr.property.name === "gql" || types.isStringLiteral(expr.property) && expr.property.value === "gql") return true;
|
|
285
|
+
return isGqlReference(expr.object);
|
|
286
|
+
};
|
|
287
|
+
const resolveTopLevelExport = (callPath, exportBindings) => {
|
|
288
|
+
const declarator = callPath.parentPath;
|
|
289
|
+
if (declarator?.isVariableDeclarator()) {
|
|
290
|
+
const { id } = declarator.node;
|
|
291
|
+
if (types.isIdentifier(id)) {
|
|
292
|
+
const exportBinding = exportBindings.get(id.name);
|
|
293
|
+
if (exportBinding) return {
|
|
294
|
+
isExported: true,
|
|
295
|
+
exportBinding
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const assignment = callPath.parentPath;
|
|
300
|
+
if (assignment?.isAssignmentExpression()) {
|
|
301
|
+
const exportName = getCommonJsExportName(assignment.node.left);
|
|
302
|
+
if (exportName && exportBindings.has(exportName)) return {
|
|
303
|
+
isExported: true,
|
|
304
|
+
exportBinding: exportName
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
};
|
|
309
|
+
const maybeEnterScope = (path, tracker, getAnonymousName) => {
|
|
310
|
+
if (path.isAssignmentExpression()) {
|
|
311
|
+
const exportName = getCommonJsExportName(path.node.left);
|
|
312
|
+
if (exportName) return tracker.enterScope({
|
|
313
|
+
segment: exportName,
|
|
314
|
+
kind: "variable",
|
|
315
|
+
stableKey: `var:${exportName}`
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
if (path.isVariableDeclarator() && types.isIdentifier(path.node.id)) {
|
|
319
|
+
const name = path.node.id.name;
|
|
320
|
+
return tracker.enterScope({
|
|
321
|
+
segment: name,
|
|
322
|
+
kind: "variable",
|
|
323
|
+
stableKey: `var:${name}`
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
if (path.isArrowFunctionExpression()) {
|
|
327
|
+
const name = getAnonymousName("arrow");
|
|
328
|
+
return tracker.enterScope({
|
|
329
|
+
segment: name,
|
|
330
|
+
kind: "function",
|
|
331
|
+
stableKey: "arrow"
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
if (path.isFunctionDeclaration() || path.isFunctionExpression()) {
|
|
335
|
+
const name = path.node.id?.name ?? getAnonymousName("function");
|
|
336
|
+
return tracker.enterScope({
|
|
337
|
+
segment: name,
|
|
338
|
+
kind: "function",
|
|
339
|
+
stableKey: `func:${name}`
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
if (path.isClassDeclaration()) {
|
|
343
|
+
const name = path.node.id?.name ?? getAnonymousName("class");
|
|
344
|
+
return tracker.enterScope({
|
|
345
|
+
segment: name,
|
|
346
|
+
kind: "class",
|
|
347
|
+
stableKey: `class:${name}`
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
if (path.isClassMethod() && types.isIdentifier(path.node.key)) {
|
|
351
|
+
const name = path.node.key.name;
|
|
352
|
+
return tracker.enterScope({
|
|
353
|
+
segment: name,
|
|
354
|
+
kind: "method",
|
|
355
|
+
stableKey: `member:${name}`
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (path.isClassProperty() && types.isIdentifier(path.node.key)) {
|
|
359
|
+
const name = path.node.key.name;
|
|
360
|
+
return tracker.enterScope({
|
|
361
|
+
segment: name,
|
|
362
|
+
kind: "property",
|
|
363
|
+
stableKey: `member:${name}`
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
if (path.isObjectProperty()) {
|
|
367
|
+
const key = path.node.key;
|
|
368
|
+
const name = types.isIdentifier(key) ? key.name : types.isStringLiteral(key) ? key.value : null;
|
|
369
|
+
if (name) return tracker.enterScope({
|
|
370
|
+
segment: name,
|
|
371
|
+
kind: "property",
|
|
372
|
+
stableKey: `prop:${name}`
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
return null;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
//#endregion
|
|
379
|
+
//#region packages/babel/src/ast/runtime.ts
|
|
380
|
+
const buildFragmentRuntimeCall = ({ artifact }) => {
|
|
381
|
+
const prebuildProps = { typename: types.stringLiteral(artifact.prebuild.typename) };
|
|
382
|
+
if (artifact.prebuild.key !== void 0) prebuildProps.key = types.stringLiteral(artifact.prebuild.key);
|
|
383
|
+
return ok(types.callExpression(types.memberExpression(types.identifier("gqlRuntime"), types.identifier("fragment")), [buildObjectExpression({ prebuild: buildObjectExpression(prebuildProps) })]));
|
|
384
|
+
};
|
|
385
|
+
const buildOperationRuntimeComponents = ({ artifact }) => {
|
|
386
|
+
const runtimeCall = types.callExpression(types.memberExpression(types.identifier("gqlRuntime"), types.identifier("operation")), [buildObjectExpression({
|
|
387
|
+
prebuild: types.callExpression(types.memberExpression(types.identifier("JSON"), types.identifier("parse")), [types.stringLiteral(JSON.stringify(artifact.prebuild))]),
|
|
388
|
+
runtime: buildObjectExpression({})
|
|
389
|
+
})]);
|
|
390
|
+
return ok({
|
|
391
|
+
referenceCall: types.callExpression(types.memberExpression(types.identifier("gqlRuntime"), types.identifier("getOperation")), [types.stringLiteral(artifact.prebuild.operationName)]),
|
|
392
|
+
runtimeCall
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region packages/babel/src/ast/transformer.ts
|
|
398
|
+
const transformCallExpression = ({ callPath, filename, metadata, getArtifact }) => {
|
|
399
|
+
if (!metadata.has(callPath.node)) return ok({ transformed: false });
|
|
400
|
+
const gqlCallResult = extractGqlCall({
|
|
401
|
+
nodePath: callPath,
|
|
402
|
+
filename,
|
|
403
|
+
metadata,
|
|
404
|
+
getArtifact
|
|
405
|
+
});
|
|
406
|
+
if (gqlCallResult.isErr()) return err(gqlCallResult.error);
|
|
407
|
+
const gqlCall = gqlCallResult.value;
|
|
408
|
+
return replaceWithRuntimeCall(callPath, gqlCall, filename);
|
|
409
|
+
};
|
|
410
|
+
const replaceWithRuntimeCall = (callPath, gqlCall, filename) => {
|
|
411
|
+
if (gqlCall.type === "fragment") {
|
|
412
|
+
const result = buildFragmentRuntimeCall({
|
|
413
|
+
...gqlCall,
|
|
414
|
+
filename
|
|
415
|
+
});
|
|
416
|
+
if (result.isErr()) return err(result.error);
|
|
417
|
+
callPath.replaceWith(result.value);
|
|
418
|
+
return ok({ transformed: true });
|
|
419
|
+
}
|
|
420
|
+
if (gqlCall.type === "operation") {
|
|
421
|
+
const result = buildOperationRuntimeComponents({
|
|
422
|
+
...gqlCall,
|
|
423
|
+
filename
|
|
424
|
+
});
|
|
425
|
+
if (result.isErr()) return err(result.error);
|
|
426
|
+
const { referenceCall, runtimeCall } = result.value;
|
|
427
|
+
callPath.replaceWith(referenceCall);
|
|
428
|
+
return ok({
|
|
429
|
+
transformed: true,
|
|
430
|
+
runtimeCall
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
return ok({ transformed: false });
|
|
434
|
+
};
|
|
435
|
+
const insertRuntimeCalls = (programPath, runtimeCalls) => {
|
|
436
|
+
if (runtimeCalls.length === 0) return;
|
|
437
|
+
programPath.traverse({ ImportDeclaration(importDeclPath) {
|
|
438
|
+
if (importDeclPath.node.source.value === "@soda-gql/runtime") importDeclPath.insertAfter([...runtimeCalls]);
|
|
439
|
+
} });
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
//#endregion
|
|
443
|
+
//#region packages/babel/src/transformer.ts
|
|
444
|
+
/**
|
|
445
|
+
* Creates a Babel transformer with a single transform() method.
|
|
446
|
+
* This matches the pattern used in the TypeScript plugin.
|
|
447
|
+
*/
|
|
448
|
+
const createTransformer = ({ programPath, types: types$1, config }) => {
|
|
449
|
+
const graphqlSystemIdentifyHelper = createGraphqlSystemIdentifyHelper(config);
|
|
450
|
+
/**
|
|
451
|
+
* Check if a node is a require() or __webpack_require__() call.
|
|
452
|
+
*/
|
|
453
|
+
const isRequireCall = (node) => {
|
|
454
|
+
if (!types$1.isCallExpression(node)) return false;
|
|
455
|
+
const callee = node.callee;
|
|
456
|
+
return types$1.isIdentifier(callee) && (callee.name === "require" || callee.name === "__webpack_require__");
|
|
457
|
+
};
|
|
458
|
+
/**
|
|
459
|
+
* Find the last statement that loads a module (import or require).
|
|
460
|
+
* Handles both ESM imports and CommonJS require() calls.
|
|
461
|
+
*/
|
|
462
|
+
const findLastModuleLoader = () => {
|
|
463
|
+
const bodyPaths = programPath.get("body");
|
|
464
|
+
let lastLoader = null;
|
|
465
|
+
for (const path of bodyPaths) {
|
|
466
|
+
if (path.isImportDeclaration()) {
|
|
467
|
+
lastLoader = path;
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (path.isVariableDeclaration()) {
|
|
471
|
+
for (const declarator of path.node.declarations) if (declarator.init && isRequireCall(declarator.init)) {
|
|
472
|
+
lastLoader = path;
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
if (path.isExpressionStatement()) {
|
|
478
|
+
if (isRequireCall(path.node.expression)) lastLoader = path;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return lastLoader;
|
|
482
|
+
};
|
|
483
|
+
return { transform: (context) => {
|
|
484
|
+
const metadata = collectGqlDefinitionMetadata({
|
|
485
|
+
programPath,
|
|
486
|
+
filename: context.filename
|
|
487
|
+
});
|
|
488
|
+
const runtimeCalls = [];
|
|
489
|
+
let transformed = false;
|
|
490
|
+
programPath.traverse({ CallExpression: (callPath) => {
|
|
491
|
+
const result = transformCallExpression({
|
|
492
|
+
callPath,
|
|
493
|
+
filename: context.filename,
|
|
494
|
+
metadata,
|
|
495
|
+
getArtifact: context.artifactLookup
|
|
496
|
+
});
|
|
497
|
+
if (result.isErr()) {
|
|
498
|
+
console.error(`[@soda-gql/babel] ${formatPluginError(result.error)}`);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const transformResult = result.value;
|
|
502
|
+
if (transformResult.transformed) {
|
|
503
|
+
transformed = true;
|
|
504
|
+
if (transformResult.runtimeCall) runtimeCalls.push(transformResult.runtimeCall);
|
|
505
|
+
}
|
|
506
|
+
} });
|
|
507
|
+
if (!transformed) return {
|
|
508
|
+
transformed: false,
|
|
509
|
+
runtimeArtifacts: void 0
|
|
510
|
+
};
|
|
511
|
+
ensureGqlRuntimeImport(programPath);
|
|
512
|
+
if (runtimeCalls.length > 0) {
|
|
513
|
+
const statements = runtimeCalls.map((expr) => types$1.expressionStatement(expr));
|
|
514
|
+
const lastLoaderPath = findLastModuleLoader();
|
|
515
|
+
if (lastLoaderPath) lastLoaderPath.insertAfter(statements);
|
|
516
|
+
else programPath.unshiftContainer("body", statements);
|
|
517
|
+
}
|
|
518
|
+
programPath.scope.crawl();
|
|
519
|
+
removeGraphqlSystemImports(programPath, graphqlSystemIdentifyHelper, context.filename);
|
|
520
|
+
return {
|
|
521
|
+
transformed: true,
|
|
522
|
+
runtimeArtifacts: void 0
|
|
523
|
+
};
|
|
524
|
+
} };
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
//#endregion
|
|
528
|
+
export { buildOperationRuntimeComponents as a, ensureGqlRuntimeRequire as c, buildObjectExpression as d, clone as f, extractGqlCall as h, buildFragmentRuntimeCall as i, removeGraphqlSystemImports as l, stripTypeAnnotations as m, insertRuntimeCalls as n, collectGqlDefinitionMetadata as o, cloneCallExpression as p, transformCallExpression as r, ensureGqlRuntimeImport as s, createTransformer as t, buildLiteralFromValue as u };
|
|
529
|
+
//# sourceMappingURL=transformer-kTdl4bE2.mjs.map
|