gatsby-source-prismic
Version:
Gatsby source plugin for building websites using Prismic as a data source
1 lines • 3.56 kB
Source Map (JSON)
{"version":3,"file":"createDocumentNodes.cjs","sources":["../../../src/lib/createDocumentNodes.ts"],"sourcesContent":["import type {\n\tCustomTypeModel,\n\tPrismicDocument,\n\tSharedSliceModel,\n} from \"@prismicio/client\";\nimport { NodeInput, NodePluginArgs } from \"gatsby\";\n\nimport type { PluginOptions, PrismicDocumentForModel } from \"../types\";\n\nimport { fmtLog } from \"./fmtLog\";\nimport { normalizeDocument } from \"./normalizeDocument\";\nimport { pascalCase } from \"./pascalCase\";\n\ntype CreateDocumentNodesArgs = {\n\tdocuments: PrismicDocument[];\n\tcustomTypeModels: CustomTypeModel[];\n\tsharedSliceModels: SharedSliceModel[];\n\tgatsbyNodeArgs: NodePluginArgs;\n\tpluginOptions: PluginOptions;\n};\n\nexport const createDocumentNodes = async (\n\targs: CreateDocumentNodesArgs,\n): Promise<void> => {\n\t// A list of document types that we have already warned about not have\n\t// an accompanying Custom Type model. Documents of these types will not\n\t// be added to the Node store.\n\tconst alreadyWarnedAboutMissingCustomTypeModels: string[] = [];\n\n\tawait Promise.all(\n\t\targs.documents.map(async (document) => {\n\t\t\tconst model = args.customTypeModels.find((customTypeModel) => {\n\t\t\t\treturn customTypeModel.id === document.type;\n\t\t\t});\n\n\t\t\tif (model) {\n\t\t\t\tconst normalizedDocument = await normalizeDocument({\n\t\t\t\t\tdocument: document as PrismicDocumentForModel<typeof model>,\n\t\t\t\t\tmodel,\n\t\t\t\t\tpluginOptions: args.pluginOptions,\n\t\t\t\t\tgatsbyNodeArgs: args.gatsbyNodeArgs,\n\t\t\t\t\tsharedSliceModels: args.sharedSliceModels,\n\t\t\t\t});\n\n\t\t\t\tconst node: NodeInput = {\n\t\t\t\t\t...normalizedDocument,\n\t\t\t\t\tid: args.gatsbyNodeArgs.createNodeId(document.id),\n\t\t\t\t\tprismicId: document.id,\n\t\t\t\t\traw: document,\n\t\t\t\t\tinternal: {\n\t\t\t\t\t\ttype: pascalCase(\n\t\t\t\t\t\t\t\"Prismic\",\n\t\t\t\t\t\t\targs.pluginOptions.typePrefix,\n\t\t\t\t\t\t\tdocument.type,\n\t\t\t\t\t\t),\n\t\t\t\t\t\tcontentDigest: args.gatsbyNodeArgs.createContentDigest(document),\n\t\t\t\t\t},\n\t\t\t\t};\n\n\t\t\t\targs.gatsbyNodeArgs.actions.createNode(node);\n\t\t\t} else {\n\t\t\t\tif (\n\t\t\t\t\t!alreadyWarnedAboutMissingCustomTypeModels.includes(document.type)\n\t\t\t\t) {\n\t\t\t\t\talreadyWarnedAboutMissingCustomTypeModels.push(document.type);\n\t\t\t\t\targs.gatsbyNodeArgs.reporter.warn(\n\t\t\t\t\t\tfmtLog(\n\t\t\t\t\t\t\targs.pluginOptions.repositoryName,\n\t\t\t\t\t\t\t`A \"${document.type}\" Custom Type model was not provided to the plugin. All documents of this type will not be queryable.`,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}),\n\t);\n};\n"],"names":["normalizeDocument","pascalCase","fmtLog"],"mappings":";;;;;AAqBa,MAAA,sBAAsB,OAClC,SACkB;AAIlB,QAAM,4CAAsD,CAAA;AAE5D,QAAM,QAAQ,IACb,KAAK,UAAU,IAAI,OAAO,aAAY;AACrC,UAAM,QAAQ,KAAK,iBAAiB,KAAK,CAAC,oBAAmB;AACrD,aAAA,gBAAgB,OAAO,SAAS;AAAA,IAAA,CACvC;AAED,QAAI,OAAO;AACJ,YAAA,qBAAqB,MAAMA,oCAAkB;AAAA,QAClD;AAAA,QACA;AAAA,QACA,eAAe,KAAK;AAAA,QACpB,gBAAgB,KAAK;AAAA,QACrB,mBAAmB,KAAK;AAAA,MAAA,CACxB;AAED,YAAM,OAAkB;AAAA,QACvB,GAAG;AAAA,QACH,IAAI,KAAK,eAAe,aAAa,SAAS,EAAE;AAAA,QAChD,WAAW,SAAS;AAAA,QACpB,KAAK;AAAA,QACL,UAAU;AAAA,UACT,MAAMC,WACL,WAAA,WACA,KAAK,cAAc,YACnB,SAAS,IAAI;AAAA,UAEd,eAAe,KAAK,eAAe,oBAAoB,QAAQ;AAAA,QAC/D;AAAA,MAAA;AAGG,WAAA,eAAe,QAAQ,WAAW,IAAI;AAAA,IAAA,OACrC;AACN,UACC,CAAC,0CAA0C,SAAS,SAAS,IAAI,GAChE;AACyC,kDAAA,KAAK,SAAS,IAAI;AACvD,aAAA,eAAe,SAAS,KAC5BC,OAAAA,OACC,KAAK,cAAc,gBACnB,MAAM,SAAS,2GAA2G,CAC1H;AAAA,MAEF;AAAA,IACD;AAAA,EACD,CAAA,CAAC;AAEJ;;"}