Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Change 'gatsby build' directory? #18975

Closed
PiccoloYu opened this issue Oct 24, 2019 · 16 comments
Closed

How to Change 'gatsby build' directory? #18975

PiccoloYu opened this issue Oct 24, 2019 · 16 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@PiccoloYu
Copy link
Contributor

How to Change 'gatsby build' directory?

@gatsbot gatsbot bot added the type: question or discussion Issue discussing or asking a question about Gatsby label Oct 24, 2019
@lannonbr
Copy link
Contributor

So when gatsby build runs, it will output all the files you need to deploy to the public folder inside the root of the site folder. That said, after the site is built, you could just take the contents of such and move it into another directory completely.

@sidharthachatterjee
Copy link
Contributor

@PiccoloYu We currently don't support a configurable output directory and don't plan to doing so in the near future.

Let's follow along the conversation in #1878

Closing this

@abraaoz
Copy link

abraaoz commented Feb 26, 2020

Yes, you can.
To change the folder from public to dist, add the following code in your gatsby-node.js:

const path = require("path")
const fs = require("fs")

exports.onPreInit = () => {
  if (process.argv[2] === "build") {
    fs.rmdirSync(path.join(__dirname, "dist"), { recursive: true })
    fs.renameSync(
      path.join(__dirname, "public"),
      path.join(__dirname, "public_dev")
    )
  }
}

exports.onPostBuild = () => {
  fs.renameSync(path.join(__dirname, "public"), path.join(__dirname, "dist"))
  fs.renameSync(
    path.join(__dirname, "public_dev"),
    path.join(__dirname, "public")
  )
}

@211211
Copy link

211211 commented Apr 1, 2020

Your solution works for me @abraaoz.
Thank you very much.

@seyacat
Copy link

seyacat commented Apr 1, 2020

based on abraaoz y resolve like this

const path = require("path")
const fs = require("fs-extra")

exports.onPostBuild = () => {
fs.copySync(path.join(__dirname, "public"), path.join(__dirname, "../public"),{ overwrite: true })

}

lukasziegler added a commit to ClimateMind/climatemind.org that referenced this issue Apr 22, 2020
Allowing us to run Github pages from master branch, while keeping the source code in the same file.

Related resources:
gatsbyjs/gatsby#18975
https://help.github.com/articles/configuring-a-publishing-source-for-github-pages/
@noorvir
Copy link

noorvir commented Jun 7, 2020

@abraaoz's solution works great. However, gatsby serve now fails because it tries to serve the build from the /public directory.

Is there a way to override this as well? The docs don't seem to provide an option to serve a specific path.

@martin-braun
Copy link

martin-braun commented Jan 5, 2021

If you need the files in a different directory after build, you can

npm i rsync -D

and modify the npm run build script to do

gatsby build && rsync -av --delete public/ path/to/new/folder

This will make sure that path/to/new/folder does not contain files, that are not in public/ and copies files over in one command. Using the rsync npm package will ensure cross-compatibility.

This will leave the public folder intact, so gastby serve should still work, @noorvir.

@jamo
Copy link
Contributor

jamo commented Jan 5, 2021

Hey all, could you help me understand what would be the use-case for making the build path configurable?

@martin-braun
Copy link

martin-braun commented Jan 5, 2021

Hey all, could you help me understand what would be the use-case for making the build path configurable?

Sure. I'm building a partial static page with WordPress (using WPGraphQL, etc.). So I want the landing page to be static. It's mostly a one page website / app that uses WordPress as headless CMS, but it also has some pages from WordPress itself, i.e. WooCommerce and account management, so I don't want to go for gatsby in full.

I wanted to config the build path, so I can build into the child theme's sub-folder to end up with a theme that contains all the relevant stuff I need. My theme also handles some htaccess entries to make sure that the static landing page (and its JS / CSS / JSON) will open when navigating to / in my website.

It's certainly an edgy use-case, I agree, but it's the most cost-efficient route to get good SEO (due to insanely good loading speeds) and have most control about landing page, at the moment.

@eugenedm
Copy link

Hey all, could you help me understand what would be the use-case for making the build path configurable?

I think this WOULD help a lot... I run gatsby for my blog on "/blog" and I run rails app on my "/" root... So, this is a need if you have multiple applications running on the same domain... I can't use a subdomain because I want to rank on SEO...

@y1n0
Copy link

y1n0 commented Dec 15, 2021

I believe the gatsby build command should have an option to configure this

@jolares
Copy link

jolares commented Feb 19, 2022

Hey all, could you help me understand what would be the use-case for making the build path configurable?

for sure, projects with more complex build/scaffolding/deployment/serving requirements, such as those in many enterprise projects, could find this useful. For example, an organization using monorepo design patterns and having a standard build process (e.g distribution bundles being built to some other common location in the repository before getting picked up, optimized, combined, or served by some other processes).

Basically without this capability, orgs need to then customize the build/serve/deploy tool just to handle this path instead (which is not a big deal btw, but i am just providing use-cases).

@willkuerlich
Copy link

My use-case would be, to be able to keep my Gatsby develop process running (which is pretty time consuming to start due to complex sourcing etc) while being able to create build while developing. Otherwise I need to do this by using the same git branch in 2 directories (1 directory for using for my actual local development and 1 directory for building the development environment online).
More of a development comfort issue here, but nevertheless ;)

@biel-fyq
Copy link

image
image
why?

@martin-braun
Copy link

@biel-fyq How about translating the error, so people can help?

@ra9r
Copy link

ra9r commented Oct 11, 2022

I would like to see this feature as well. In my situation I use gatsby to generate the website in a Firebase hosting project, but the project that contains the website also contains the code for functions, security rules, storage configuration and a host of other directories. Having Gatsby enforce an out directory that is not configurable makes it necessary to move/delete the public directory to some where that I need it. It's a very common thing to do in anything except the most basic websites. You really should consider adding this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests