-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(gatsby): Optimise create page action validation (#24684)
* chore(gatsby): Optimise create page action validation * Remove change that will go iin a different PR * Correctly use passed plugin name * Fix snapshot * Switch cache to use Set Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: gatsbybot <[email protected]>
- Loading branch information
1 parent
317aa65
commit 64858b1
Showing
3 changed files
with
143 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import path from "path" | ||
import fs from "fs-extra" | ||
import { IGatsbyPage } from "../redux/types" | ||
|
||
const validationCache = new Set<string>() | ||
|
||
interface IErrorMeta { | ||
id: string | ||
context: Record<string, unknown> | ||
} | ||
|
||
export function validatePageComponent( | ||
page: IGatsbyPage, | ||
directory: string, | ||
pluginName: string | ||
): { message?: string; error?: IErrorMeta; panicOnBuild?: boolean } { | ||
const { component } = page | ||
if (!component) { | ||
throw new Error(`11322`) | ||
} | ||
if (validationCache.has(component)) { | ||
return {} | ||
} | ||
if (!path.isAbsolute(component)) { | ||
return { | ||
error: { | ||
id: `11326`, | ||
context: { | ||
pluginName, | ||
pageObject: page, | ||
component: component, | ||
}, | ||
}, | ||
message: `${pluginName} must set the absolute path to the page component when create creating a page`, | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== `test`) { | ||
if (!fs.existsSync(component)) { | ||
return { | ||
error: { | ||
id: `11325`, | ||
context: { | ||
pluginName, | ||
pageObject: page, | ||
component: component, | ||
}, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
// Validate that the page component imports React and exports something | ||
// (hopefully a component). | ||
// | ||
|
||
if ( | ||
!component.includes(`/.cache/`) && | ||
process.env.NODE_ENV === `production` | ||
) { | ||
const fileContent = fs.readFileSync(component, `utf-8`) | ||
|
||
if (fileContent === ``) { | ||
const relativePath = path.relative(directory, component) | ||
|
||
return { | ||
error: { | ||
id: `11327`, | ||
context: { | ||
relativePath, | ||
}, | ||
}, | ||
panicOnBuild: true, | ||
} | ||
} | ||
|
||
const includesDefaultExport = | ||
fileContent.includes(`export default`) || | ||
fileContent.includes(`module.exports`) || | ||
fileContent.includes(`exports.default`) || | ||
fileContent.includes(`exports["default"]`) || | ||
fileContent.match(/export \{.* as default.*\}/s) || | ||
// this check only applies to js and ts, not mdx | ||
/\.(jsx?|tsx?)/.test(path.extname(component)) | ||
|
||
if (!includesDefaultExport) { | ||
return { | ||
error: { | ||
id: `11328`, | ||
context: { | ||
component, | ||
}, | ||
}, | ||
panicOnBuild: true, | ||
} | ||
} | ||
} | ||
|
||
validationCache.add(component) | ||
return {} | ||
} | ||
|
||
export function clearValidationCache(): void { | ||
validationCache.clear() | ||
} |