Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): Allow "gatsby-node" directory for Parcel Compilation #36712

Merged
Merged
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`,
gatsbyNode: `${__dirname}/fixtures/gatsby-node`,
}

jest.setTimeout(15000)
Expand Down Expand Up @@ -176,6 +177,30 @@ describe(`gatsby file compilation`, () => {
})
})

describe(`gatsby-node directory is allowed`, () => {
beforeEach(() => {
reporterPanicMock.mockClear()
})
it(`should not panic on gatsby-node dir`, async () => {
process.chdir(dir.gatsbyNode)
await remove(`${dir.gatsbyNode}/.cache`)
await compileGatsbyFiles(dir.gatsbyNode)

expect(reporterPanicMock).not.toHaveBeenCalled()
})

it(`should not compile gatsby-node dir`, async () => {
process.chdir(dir.gatsbyNode)
await remove(`${dir.gatsbyNode}/.cache`)
await compileGatsbyFiles(dir.gatsbyNode)
const isCompiled = await pathExists(
`${dir.gatsbyNode}/.cache/compiled/gatsby-node`
)
expect(isCompiled).toEqual(false)
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,3 @@
export default createPages() {
console.log('createPages!')
}
18 changes: 15 additions & 3 deletions packages/gatsby/src/utils/parcel/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,36 @@ export async function compileGatsbyFiles(
retry: number = 0
): Promise<void> {
try {
const configName = `gatsby-node`

// Check for gatsby-node.jsx and gatsby-node.tsx (or other misnamed variations)
const files = await readdir(siteRoot)
const filesAndDirectories = await readdir(siteRoot, { withFileTypes: true })
const files = filesAndDirectories
.filter(dirent => !dirent.isDirectory())
.map(dirent => dirent.name)

// Check if they have a gatsby-node directory for lifecycle function code.
const hasGatsbyNodeDir =
filesAndDirectories.filter(
dirent => dirent.isDirectory() && dirent.name === configName
).length > 0
LekoArts marked this conversation as resolved.
Show resolved Hide resolved

let nearMatch = ``
const configName = `gatsby-node`

for (const file of files) {
if (nearMatch) {
break
}

const { name } = path.parse(file)

// Of course, allow valid gatsby-node files
if (file === `gatsby-node.js` || file === `gatsby-node.ts`) {
break
}

if (isNearMatch(name, configName, 3)) {
// Check for likely misnamed files, but allow a 'gatsby-node' directory.
if (isNearMatch(name, configName, 3) && !hasGatsbyNodeDir) {
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
nearMatch = file
}
}
Expand Down