From 6510687b54ea2f83e5ff0ac2a84190fcfb6ce196 Mon Sep 17 00:00:00 2001 From: Jude Agboola Date: Tue, 18 Jan 2022 12:16:21 +0100 Subject: [PATCH] revert removal of _gatsby-config.json functionality in gatsby-plugin-gatsby-cloud --- .../src/__tests__/create-site-config.js | 50 +++++++++++++++++++ .../src/constants.js | 1 + .../src/create-site-config.js | 15 ++++++ .../src/gatsby-node.js | 2 + 4 files changed, 68 insertions(+) create mode 100644 packages/gatsby-plugin-gatsby-cloud/src/__tests__/create-site-config.js create mode 100644 packages/gatsby-plugin-gatsby-cloud/src/create-site-config.js 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), ])