0

I am trying to better organize my gatsby config file, but I"m not sure that I'm doing it right. Does anyone know if the following setup will work:

module.exports = {
  plugins: [
    { gatsby_plugin__manifest },
    { gatsby_source__file_system__images },
    { gatsby_source__file_system__posts },
  ],
};

const gatsby_plugin__manifest = {
  resolve: `gatsby-plugin-manifest`,
  options: {
    name: `gatsby-starter-default`,
    short_name: `starter`,
    start_url: `/`,
    background_color: `#663399`,
    theme_color: `#663399`,
    display: `minimal-ui`,
    icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
  },
}

const gatsby_source__file_system__images = {
  resolve: `gatsby-source-filesystem`,
  options: {
    name: `images`,
    path: `${__dirname}/src/images`,
  },
}

const gatsby_source__file_system__posts = {
  resolve: `gatsby-source-filesystem`,
  options: {
    name: `posts`,
    path: `${__dirname}/src/posts/`,
  },
}

I got an error saying gatsby_plugin__manifest is not defined and I'm wondering if it is because of how I set up the file?

Moshe
  • 6,011
  • 16
  • 60
  • 112

2 Answers2

3

I see two issues here:

According to the Gatbsy docs, plugins is an array of strings or objects. In ES6 { gatsby_plugin__manifest }, is shorthand for { gatsby_plugin__manifest: gatsby_plugin__manifest }, a key-value pair. Dropping the object syntax resolves that issue.

Second, declaring gatsby_plugin__manifest before it is referenced in the export causes a ReferenceError, for reasons described in this answer.

To summarize these recommendations, for just one of the plugins:

// Declare gatsby_plugin__manifest before export
const gatsby_plugin__manifest = {
  resolve: `gatsby-plugin-manifest`,
  options: {
    // ...configuration here
  },
}

// Remove object syntax around gatsby_plugin__manifest
module.exports = {
  plugins: [
    gatsby_plugin__manifest,
  ],
};

Gatsby Config Docs

Jake Worth
  • 5,490
  • 1
  • 25
  • 35
1

Variables declared with const or let are not hoisted so they can't be referenced before declaration. Put the module.exports below the declarations and it should work.

Adam Romeo
  • 123
  • 7