Skip to content

Commit

Permalink
fix(gatsby): Allow "gatsby-node" directory for Parcel Compilation (#3…
Browse files Browse the repository at this point in the history
…6712)

Co-authored-by: Ty Hopp <tyhopp@users.noreply.github.com>
Co-authored-by: Lennart <lekoarts@gmail.com>
  • Loading branch information
3 people authored Jan 5, 2023
1 parent 56ace78 commit 57607f1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
25 changes: 25 additions & 0 deletions packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { onPreInit } from "./gatsby-node/on-pre-init"
export { onPreInit }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { GatsbyNode } from "gatsby"
import { working } from "../../utils/say-what-ts"

export const onPreInit: GatsbyNode["onPreInit"] = ({ reporter }) => {
reporter.info(working)
}
15 changes: 11 additions & 4 deletions packages/gatsby/src/utils/parcel/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ export async function compileGatsbyFiles(
retry: number = 0
): Promise<void> {
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 <fs.Dirent> 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) {
Expand All @@ -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
}
}
Expand All @@ -93,7 +100,7 @@ export async function compileGatsbyFiles(
reporter.panic({
id: `10128`,
context: {
configName,
configName: gatsbyNodeName,
nearMatch,
isTSX,
},
Expand Down

0 comments on commit 57607f1

Please sign in to comment.