import path from "upath" ; import * as logger from "../shared/logger" ; import { initLoader , convertOptsToLoaderConfig , onPostInit } from "../shared" ; import type { PlasmicLoaderConfig , PlasmicOpts , PluginOptions , Substitutions , } from "../shared/types" ; let config ; let watch = process . env . NODE_ENV === "development" ; async function onGatsbyPreBootstrap ( pluginOptions ) { delete ( pluginOptions as any ) . plugins ; const defaultDir = pluginOptions . dir || process . cwd ( ) ; const plasmicDir = path . join ( defaultDir , ".cache" , ".plasmic" ) ; if ( pluginOptions . watch != null ) { watch = pluginOptions . watch ; } const defaultOptions = { initArgs : { platform : "gatsby" , "pages-dir" : "./pages" , "images-public-dir" : "../../../public" , "src-dir" : "./components" , } , projects : [ ] , dir : defaultDir , watch , plasmicDir , pageDir : path . join ( defaultDir , "src" , "pages" ) , substitutions : { } as Substitutions , } ; config = await convertOptsToLoaderConfig ( pluginOptions , defaultOptions ) ; return initLoader ( config ) ; } let createPageParam = { } ; async function onGatsbyPostBootstrap ( ) { return onPostInit ( config , watch , async ( pages ) => { const existingPages = await createPageParam . graphql ( `
      {
        allSitePage {
          edges {
            node {
              path
            }
          }
        }
      }` ) ; const existingPagesMap = Object . fromEntries ( existingPages . data . allSitePage . edges . map ( ( page ) => [ page . node . path , true , ] ) ) ; pages . filter ( ( page ) => ! existingPagesMap [ page . url ] && ! existingPagesMap [ page . url + "/" ] ) . forEach ( ( page ) => createPageParam . actions . createPage ( { path : page . url , component : page . path , context : { } , } ) ) ; } ) ; } exports . createPagesStatefully = ( opts ) => ( createPageParam = opts ) ; exports . onPreBootstrap = ( _ , pluginOptions ) => onGatsbyPreBootstrap ( pluginOptions ) . catch ( ( e ) => logger . crash ( e . message , e ) ) ; exports . onPostBootstrap = ( ) => onGatsbyPostBootstrap ( ) . catch ( ( e ) => logger . crash ( e . message , e ) ) ;