2

Reading how to exclude a directory on build I ran across this issue in Gatsby and it indicated to ignore a directory in gatsby-source-filesystem you can ignore based on env with:

{
  resolve: `gatsby-source-filesystem`,
  options: {
    path: `${__dirname}/content`,
    ignore: process.env.NODE_ENV === `production` && [`**/draft-*`]
  }
}

ref but when I try to use a similar approach with:

{
  resolve: `gatsby-source-filesystem`,
  options: {
    name: `designs`,
    path: `${__dirname}/src/designs/`,
    ignore: process.env.NODE_ENV === `production` && [`*`],
  },
},

it throws a terminal error of:

Invalid plugin options for "gatsby-source-filesystem":

  • "ignore" must be an array

I am bringing in dotenv at the top of gatsby-config.js with:

require('dotenv').config({
  path: `.env.${process.env.NODE_ENV}`,
})

and if the env condition is removed it works:

{
  resolve: `gatsby-source-filesystem`,
  options: {
    name: `designs`,
    path: `${__dirname}/src/designs/`,
    ignore: [`*`],
  },
},

I was reading "How to unpublish a gatsby page without deleting it?" that led me to this question. I do not have an issue with it working on npm run build just with npm run develop. In Gatsby develop is there a way to use an ignore with an env condition?

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

1 Answers1

1

I don't know exactly the reason why but I faced the same issue with different plugins (like generating different sitemap.xml for different environments or stuff like that). I bypassed it by generating the variable outside the scope of the module.exports like:

 require(`dotenv`).config({
   path: `.env.${process.env.NODE_ENV}`
 });
 
 let ignore= process.env.NODE_ENV === `production` ? [`*`] : [];
 
 module.exports = {
   plugins: [
     {
       resolve: `gatsby-source-filesystem`,
       options: {
         name: `designs`,
         path: `${__dirname}/src/designs/`,
         ignore: ignore,
       },
     },
   ]

Tweak the ternary condition to add process.env.NODE_ENV === `production` && [`*`] if needed.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67