{"version":3,"sources":["../../../../src/build/babel/plugins/next-page-config.ts"],"sourcesContent":["import { types as BabelTypes } from 'next/dist/compiled/babel/core'\nimport type {\n  PluginObj,\n  PluginPass,\n  Visitor,\n  NodePath,\n} from 'next/dist/compiled/babel/core'\nimport type { PageConfig } from '../../../types'\nimport { STRING_LITERAL_DROP_BUNDLE } from '../../../shared/lib/constants'\n\nconst CONFIG_KEY = 'config'\n\n// replace program path with just a variable with the drop identifier\nfunction replaceBundle(path: any, t: typeof BabelTypes): void {\n  path.parentPath.replaceWith(\n    t.program(\n      [\n        t.variableDeclaration('const', [\n          t.variableDeclarator(\n            t.identifier(STRING_LITERAL_DROP_BUNDLE),\n            t.stringLiteral(`${STRING_LITERAL_DROP_BUNDLE} ${Date.now()}`)\n          ),\n        ]),\n      ],\n      []\n    )\n  )\n}\n\nfunction errorMessage(state: any, details: string): string {\n  const pageName =\n    (state.filename || '').split(state.cwd || '').pop() || 'unknown'\n  return `Invalid page config export found. ${details} in file ${pageName}. See: https://nextjs.org/docs/messages/invalid-page-config`\n}\n\ninterface ConfigState extends PluginPass {\n  bundleDropped?: boolean\n}\n\n// config to parsing pageConfig for client bundles\nexport default function nextPageConfig({\n  types: t,\n}: {\n  types: typeof BabelTypes\n}): PluginObj {\n  return {\n    visitor: {\n      Program: {\n        enter(path, state) {\n          path.traverse(\n            {\n              ExportDeclaration(exportPath, exportState) {\n                if (\n                  BabelTypes.isExportNamedDeclaration(exportPath.node) &&\n                  exportPath.node.specifiers?.some((specifier) => {\n                    return (\n                      (t.isIdentifier(specifier.exported)\n                        ? specifier.exported.name\n                        : specifier.exported.value) === CONFIG_KEY\n                    )\n                  }) &&\n                  BabelTypes.isStringLiteral(\n                    (exportPath.node as BabelTypes.ExportNamedDeclaration)\n                      .source\n                  )\n                ) {\n                  throw new Error(\n                    errorMessage(\n                      exportState,\n                      'Expected object but got export from'\n                    )\n                  )\n                }\n              },\n              ExportNamedDeclaration(\n                exportPath: NodePath<BabelTypes.ExportNamedDeclaration>,\n                exportState: any\n              ) {\n                if (\n                  exportState.bundleDropped ||\n                  (!exportPath.node.declaration &&\n                    exportPath.node.specifiers.length === 0)\n                ) {\n                  return\n                }\n\n                const config: PageConfig = {}\n                const declarations: BabelTypes.VariableDeclarator[] = [\n                  ...((\n                    exportPath.node\n                      .declaration as BabelTypes.VariableDeclaration\n                  )?.declarations || []),\n                  exportPath.scope.getBinding(CONFIG_KEY)?.path\n                    .node as BabelTypes.VariableDeclarator,\n                ].filter(Boolean)\n\n                for (const specifier of exportPath.node.specifiers) {\n                  if (\n                    (t.isIdentifier(specifier.exported)\n                      ? specifier.exported.name\n                      : specifier.exported.value) === CONFIG_KEY\n                  ) {\n                    // export {} from 'somewhere'\n                    if (BabelTypes.isStringLiteral(exportPath.node.source)) {\n                      throw new Error(\n                        errorMessage(\n                          exportState,\n                          `Expected object but got import`\n                        )\n                      )\n                      // import hello from 'world'\n                      // export { hello as config }\n                    } else if (\n                      BabelTypes.isIdentifier(\n                        (specifier as BabelTypes.ExportSpecifier).local\n                      )\n                    ) {\n                      if (\n                        BabelTypes.isImportSpecifier(\n                          exportPath.scope.getBinding(\n                            (specifier as BabelTypes.ExportSpecifier).local.name\n                          )?.path.node\n                        )\n                      ) {\n                        throw new Error(\n                          errorMessage(\n                            exportState,\n                            `Expected object but got import`\n                          )\n                        )\n                      }\n                    }\n                  }\n                }\n\n                for (const declaration of declarations) {\n                  if (\n                    !BabelTypes.isIdentifier(declaration.id, {\n                      name: CONFIG_KEY,\n                    })\n                  ) {\n                    continue\n                  }\n\n                  let { init } = declaration\n                  if (BabelTypes.isTSAsExpression(init)) {\n                    init = init.expression\n                  }\n\n                  if (!BabelTypes.isObjectExpression(init)) {\n                    const got = init ? init.type : 'undefined'\n                    throw new Error(\n                      errorMessage(\n                        exportState,\n                        `Expected object but got ${got}`\n                      )\n                    )\n                  }\n\n                  for (const prop of init.properties) {\n                    if (BabelTypes.isSpreadElement(prop)) {\n                      throw new Error(\n                        errorMessage(\n                          exportState,\n                          `Property spread is not allowed`\n                        )\n                      )\n                    }\n                    const { name } = prop.key as BabelTypes.Identifier\n                    if (BabelTypes.isIdentifier(prop.key, { name: 'amp' })) {\n                      if (!BabelTypes.isObjectProperty(prop)) {\n                        throw new Error(\n                          errorMessage(\n                            exportState,\n                            `Invalid property \"${name}\"`\n                          )\n                        )\n                      }\n                      if (\n                        !BabelTypes.isBooleanLiteral(prop.value) &&\n                        !BabelTypes.isStringLiteral(prop.value)\n                      ) {\n                        throw new Error(\n                          errorMessage(\n                            exportState,\n                            `Invalid value for \"${name}\"`\n                          )\n                        )\n                      }\n                      config.amp = prop.value.value as PageConfig['amp']\n                    }\n                  }\n                }\n\n                if (config.amp === true) {\n                  if (!exportState.file?.opts?.caller.isDev) {\n                    // don't replace bundle in development so HMR can track\n                    // dependencies and trigger reload when they are changed\n                    replaceBundle(exportPath, t)\n                  }\n                  exportState.bundleDropped = true\n                  return\n                }\n              },\n            },\n            state\n          )\n        },\n      },\n    } as Visitor<ConfigState>,\n  }\n}\n"],"names":["nextPageConfig","CONFIG_KEY","replaceBundle","path","t","parentPath","replaceWith","program","variableDeclaration","variableDeclarator","identifier","STRING_LITERAL_DROP_BUNDLE","stringLiteral","Date","now","errorMessage","state","details","pageName","filename","split","cwd","pop","types","visitor","Program","enter","traverse","ExportDeclaration","exportPath","exportState","BabelTypes","isExportNamedDeclaration","node","specifiers","some","specifier","isIdentifier","exported","name","value","isStringLiteral","source","Error","ExportNamedDeclaration","bundleDropped","declaration","length","config","declarations","scope","getBinding","filter","Boolean","local","isImportSpecifier","id","init","isTSAsExpression","expression","isObjectExpression","got","type","prop","properties","isSpreadElement","key","isObjectProperty","isBooleanLiteral","amp","file","opts","caller","isDev"],"mappings":";;;;+BAuCA,kDAAkD;AAClD;;;eAAwBA;;;sBAxCY;2BAQO;AAE3C,MAAMC,aAAa;AAEnB,qEAAqE;AACrE,SAASC,cAAcC,IAAS,EAAEC,CAAoB;IACpDD,KAAKE,UAAU,CAACC,W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