-
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): Migrate utils/page-data to TypeScript (#23991)
* chore(gatsby): Migrate utils/page-data to TypeScript This commit migrates the existing `src/utils/page-data.ts` file to TypeScript by trying to stick as close as possible to the original JavaScript file. To take advantage of existing Redux typings, I tried to extend the typings from the `IGatsbyPage` interface describing page data. * Improve function naming * convert to functions * update yarn.lock * skip typechecking gatsby-admin Co-authored-by: Blaine Kasten <blainekasten@gmail.com>
- Loading branch information
1 parent
6c99659
commit a6ef610
Showing
9 changed files
with
113 additions
and
123 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 was deleted.
Oops, something went wrong.
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,78 @@ | ||
import fs from "fs-extra" | ||
import { ExecutionResult } from "graphql" | ||
import path from "path" | ||
import { IGatsbyPage } from "../redux/types" | ||
import { store } from "../redux" | ||
|
||
interface IPageData { | ||
componentChunkName: IGatsbyPage["componentChunkName"] | ||
matchPath?: IGatsbyPage["matchPath"] | ||
path: IGatsbyPage["path"] | ||
} | ||
|
||
interface IPageDataWithQueryResult extends IPageData { | ||
result: ExecutionResult | ||
} | ||
|
||
export function fixedPagePath(pagePath: string): string { | ||
return pagePath === `/` ? `index` : pagePath | ||
} | ||
|
||
export function getFilePath(publicDir: string, pagePath: string): string { | ||
return path.join( | ||
publicDir, | ||
`page-data`, | ||
fixedPagePath(pagePath), | ||
`page-data.json` | ||
) | ||
} | ||
|
||
export async function readPageData( | ||
publicDir: string, | ||
pagePath: string | ||
): Promise<IPageDataWithQueryResult> { | ||
const filePath = getFilePath(publicDir, pagePath) | ||
const rawPageData = await fs.readFile(filePath, `utf-8`) | ||
|
||
return JSON.parse(rawPageData) | ||
} | ||
|
||
export async function removePageData( | ||
publicDir: string, | ||
pagePath: string | ||
): Promise<void> { | ||
const filePath = getFilePath(publicDir, pagePath) | ||
|
||
if (fs.existsSync(filePath)) { | ||
return await fs.remove(filePath) | ||
} | ||
|
||
return Promise.resolve() | ||
} | ||
|
||
export async function writePageData( | ||
publicDir: string, | ||
{ componentChunkName, matchPath, path }: IPageData, | ||
result: IPageDataWithQueryResult["result"] | ||
): Promise<void> { | ||
const filePath = getFilePath(publicDir, path) | ||
const body = { | ||
componentChunkName, | ||
path, | ||
matchPath, | ||
result, | ||
} | ||
const bodyStr = JSON.stringify(body) | ||
// transform asset size to kB (from bytes) to fit 64 bit to numbers | ||
const pageDataSize = Buffer.byteLength(bodyStr) / 1000 | ||
|
||
store.dispatch({ | ||
type: `ADD_PAGE_DATA_STATS`, | ||
payload: { | ||
filePath, | ||
size: pageDataSize, | ||
}, | ||
}) | ||
|
||
await fs.outputFile(filePath, bodyStr) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// Note: this doesn't check for conflicts between module exports | ||
export { getFilePath } from "./page-data" | ||
export { getFilePath } from "../page-data" | ||
export { renderHTML } from "./render-html" |
This file was deleted.
Oops, something went wrong.
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