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
6 changes: 3 additions & 3 deletions packages/gatsby-cli/src/structured-errors/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ const errors = {
},
"10124": {
text: (context): string =>
`It looks like you were trying to add the config file? Please rename "${
context.nearMatch
}" to "${context.configName}.${context.isTSX ? `ts` : `js`}"`,
` the config file? Please rename "${context.nearMatch}" to "${
context.configName
}.${context.isTSX ? `ts` : `js`}"`,
type: Type.CONFIG,
level: Level.ERROR,
category: ErrorCategory.USER,
Expand Down
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 @@ -173,6 +174,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!');
}
27 changes: 23 additions & 4 deletions packages/gatsby/src/utils/parcel/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { LMDBCache, Cache } from "@parcel/cache"
import path from "path"
import type { Diagnostic } from "@parcel/diagnostic"
import reporter from "gatsby-cli/lib/reporter"
import { ensureDir, emptyDir, existsSync, remove, readdir } from "fs-extra"
import {
ensureDir,
emptyDir,
existsSync,
remove,
readdir,
lstat,
graysonhicks marked this conversation as resolved.
Show resolved Hide resolved
} from "fs-extra"
import telemetry from "gatsby-telemetry"
import { isNearMatch } from "../is-near-match"

Expand Down Expand Up @@ -61,24 +68,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