From 57607f1fb4e95e3cb2a3ec14cbe17813942bf812 Mon Sep 17 00:00:00 2001 From: Grayson Hicks Date: Thu, 5 Jan 2023 05:43:00 -0600 Subject: [PATCH] fix(gatsby): Allow "gatsby-node" directory for Parcel Compilation (#36712) Co-authored-by: Ty Hopp Co-authored-by: Lennart --- .../parcel/__tests__/compile-gatsby-files.ts | 25 +++++++++++++++++++ .../gatsby-node-as-directory/gatsby-node.ts | 2 ++ .../gatsby-node/on-pre-init.ts | 6 +++++ .../src/utils/parcel/compile-gatsby-files.ts | 15 ++++++++--- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node.ts create mode 100644 packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node/on-pre-init.ts diff --git a/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts b/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts index 845360af684eb..76ec8a9fabaf2 100644 --- a/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts +++ b/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts @@ -17,6 +17,7 @@ const dir = { tsOnlyInLocal: `${__dirname}/fixtures/ts-only-in-local-plugin`, misnamedJS: `${__dirname}/fixtures/misnamed-js`, misnamedTS: `${__dirname}/fixtures/misnamed-ts`, + gatsbyNodeAsDirectory: `${__dirname}/fixtures/gatsby-node-as-directory`, } jest.setTimeout(15000) @@ -176,6 +177,30 @@ describe(`gatsby file compilation`, () => { }) }) +describe(`gatsby-node directory is allowed`, () => { + beforeAll(async () => { + process.chdir(dir.gatsbyNodeAsDirectory) + await remove(`${dir.gatsbyNodeAsDirectory}/.cache`) + }) + beforeEach(() => { + reporterPanicMock.mockClear() + }) + it(`should not panic on gatsby-node dir`, async () => { + await compileGatsbyFiles(dir.gatsbyNodeAsDirectory) + expect(reporterPanicMock).not.toHaveBeenCalled() + }) + + it(`should compile gatsby-node file and its dir files`, async () => { + const compiledGatsbyNode = await readFile( + `${dir.gatsbyNodeAsDirectory}/.cache/compiled/gatsby-node.js`, + `utf-8` + ) + + expect(compiledGatsbyNode).toContain(`I am working!`) + expect(reporterPanicMock).not.toHaveBeenCalled() + }) +}) + describe(`misnamed gatsby-node files`, () => { beforeEach(() => { reporterPanicMock.mockClear() diff --git a/packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node.ts b/packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node.ts new file mode 100644 index 0000000000000..3d0bf5c969d34 --- /dev/null +++ b/packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node.ts @@ -0,0 +1,2 @@ +import { onPreInit } from "./gatsby-node/on-pre-init" +export { onPreInit } diff --git a/packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node/on-pre-init.ts b/packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node/on-pre-init.ts new file mode 100644 index 0000000000000..24d340be40c2f --- /dev/null +++ b/packages/gatsby/src/utils/parcel/__tests__/fixtures/gatsby-node-as-directory/gatsby-node/on-pre-init.ts @@ -0,0 +1,6 @@ +import { GatsbyNode } from "gatsby" +import { working } from "../../utils/say-what-ts" + +export const onPreInit: GatsbyNode["onPreInit"] = ({ reporter }) => { + reporter.info(working) +} \ No newline at end of file diff --git a/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts b/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts index a2ad269033d59..5b71db2cee73b 100644 --- a/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts +++ b/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts @@ -61,11 +61,17 @@ export async function compileGatsbyFiles( retry: number = 0 ): Promise { try { + const gatsbyNodeName = `gatsby-node` + // Check for gatsby-node.jsx and gatsby-node.tsx (or other misnamed variations) - const files = await readdir(siteRoot) + // We want to filter out directory names so we can use "withFileTypes" + // With "withFileTypes" the array will contain objects + const filesAndDirectories = await readdir(siteRoot, { withFileTypes: true }) + const files = filesAndDirectories + .filter(i => !i.isDirectory()) + .map(i => i.name) let nearMatch = `` - const configName = `gatsby-node` for (const file of files) { if (nearMatch) { @@ -82,7 +88,8 @@ export async function compileGatsbyFiles( break } - if (isNearMatch(name, configName, 3)) { + // Check for likely misnamed files + if (isNearMatch(name, gatsbyNodeName, 3)) { nearMatch = file } } @@ -93,7 +100,7 @@ export async function compileGatsbyFiles( reporter.panic({ id: `10128`, context: { - configName, + configName: gatsbyNodeName, nearMatch, isTSX, },