diff --git a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/create-site-config.js b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/create-site-config.js new file mode 100644 index 0000000000000..2b4db649280f5 --- /dev/null +++ b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/create-site-config.js @@ -0,0 +1,50 @@ +import createSiteConfig from "../create-site-config" +import * as fs from "fs-extra" + +jest.mock(`fs-extra`, () => { + return { + writeJSON: jest.fn(), + } +}) + +describe(`create-site-config`, () => { + beforeEach(() => { + fs.writeJSON.mockClear() + }) + + it(`should generate a site-config file with pathPrefix null`, async () => { + await createSiteConfig( + { + pathPrefix: ``, + publicFolder: file => `public/${file}`, + }, + {} + ) + + expect(fs.writeJSON).toBeCalledTimes(1) + expect(fs.writeJSON.mock.calls[0][0]).toBe(`public/_gatsby-config.json`) + expect(fs.writeJSON.mock.calls[0][1]).toMatchInlineSnapshot(` + Object { + "pathPrefix": null, + } + `) + }) + + it(`should generate a site-config file with pathPrefix set`, async () => { + await createSiteConfig( + { + pathPrefix: `/nested`, + publicFolder: file => `public/${file}`, + }, + {} + ) + + expect(fs.writeJSON).toBeCalledTimes(1) + expect(fs.writeJSON.mock.calls[0][0]).toBe(`public/_gatsby-config.json`) + expect(fs.writeJSON.mock.calls[0][1]).toMatchInlineSnapshot(` + Object { + "pathPrefix": "/nested", + } + `) + }) +}) diff --git a/packages/gatsby-plugin-gatsby-cloud/src/constants.js b/packages/gatsby-plugin-gatsby-cloud/src/constants.js index 281c23c4de92c..c0a66db906e56 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/constants.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/constants.js @@ -8,6 +8,7 @@ export const BUILD_CSS_STAGE = `build-css` export const HEADERS_FILENAME = `_headers.json` export const REDIRECTS_FILENAME = `_redirects.json` export const PUBLIC_FUNCTIONS_FILENAME = `_functions.json` +export const SITE_CONFIG_FILENAME = `_gatsby-config.json` export const CACHE_FUNCTIONS_FILENAME = `manifest.json` export const DEFAULT_OPTIONS = { diff --git a/packages/gatsby-plugin-gatsby-cloud/src/create-site-config.js b/packages/gatsby-plugin-gatsby-cloud/src/create-site-config.js new file mode 100644 index 0000000000000..778e2711af2bd --- /dev/null +++ b/packages/gatsby-plugin-gatsby-cloud/src/create-site-config.js @@ -0,0 +1,15 @@ +import * as fs from "fs-extra" +import { SITE_CONFIG_FILENAME } from "./constants" + +/* + * @deprecated TODO(v5): Will be Remove in V5 since we're now sending config over IPC + * see https://github.com/gatsbyjs/gatsby/pull/34411/files#:~:text=process.send(%7B,%7D) + */ +export default async function createSiteConfig(pluginData, _pluginOptions) { + const { publicFolder } = pluginData + const siteConfig = { + pathPrefix: pluginData.pathPrefix ? pluginData.pathPrefix : null, + } + + return fs.writeJSON(publicFolder(SITE_CONFIG_FILENAME), siteConfig) +} diff --git a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js index 879016546d989..2cae64980f707 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js @@ -10,6 +10,7 @@ import makePluginData from "./plugin-data" import buildHeadersProgram from "./build-headers-program" import copyFunctionsManifest from "./copy-functions-manifest" import createRedirects from "./create-redirects" +import createSiteConfig from "./create-site-config" import { DEFAULT_OPTIONS, BUILD_HTML_STAGE, BUILD_CSS_STAGE } from "./constants" import { emitRoutes, emitFileNodes } from "./ipc" @@ -111,6 +112,7 @@ exports.onPostBuild = async ({ store, getNodesByType }, userPluginOptions) => { await Promise.all([ ensureEmittingFileNodesFinished, buildHeadersProgram(pluginData, pluginOptions), + createSiteConfig(pluginData, pluginOptions), createRedirects(pluginData, redirects, rewrites), copyFunctionsManifest(pluginData), ])