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, },