-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
chore(gatsby): Migrate utils/page-data to TypeScript #23436
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import fs from "fs-extra" | ||
import path from "path" | ||
import { IGatsbyPage } from "../redux/types" | ||
import { store } from "../redux" | ||
|
||
interface IPageDataContext { | ||
publicDir: string | ||
} | ||
|
||
interface IPageData | ||
extends Pick<IGatsbyPage, "componentChunkName" | "matchPath" | "path"> { | ||
result: any | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a bit of a nit. But I find these utility generics hard to understand. Can we change this to this? interface IPageData {
componentChunkName: IGatsbyPage["componentChunkName"]
matchPath: IGatsbyPage["matchPath"]
path: IGatsbyPage["path"]
result: any // also, can this be improved?
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
export const fixedPagePath = (pagePath: string): string => | ||
pagePath === `/` ? `index` : pagePath | ||
|
||
const getFilePath = ( | ||
{ publicDir }: IPageDataContext, | ||
pagePath: string | ||
): string => | ||
path.join(publicDir, `page-data`, fixedPagePath(pagePath), `page-data.json`) | ||
|
||
export const read = async ( | ||
{ publicDir }: IPageDataContext, | ||
pagePath: string | ||
): Promise<IPageData> => { | ||
const filePath = getFilePath({ publicDir }, pagePath) | ||
const rawPageData = await fs.readFile(filePath, `utf-8`) | ||
|
||
return JSON.parse(rawPageData) | ||
} | ||
|
||
export const remove = async ( | ||
{ publicDir }: IPageDataContext, | ||
pagePath: string | ||
): Promise<void> => { | ||
const filePath = getFilePath({ publicDir }, pagePath) | ||
|
||
if (fs.existsSync(filePath)) { | ||
return await fs.remove(filePath) | ||
} | ||
|
||
return Promise.resolve() | ||
} | ||
|
||
export const write = async ( | ||
{ publicDir }: IPageDataContext, | ||
{ componentChunkName, matchPath, path }: Exclude<IPageData, "result">, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this Exclude. We try to avoid complex typing. I think it would be easier to just have two interfaces. One that extends the other and adds result. |
||
result: IPageData["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) | ||
} | ||
|
||
export default { | ||
fixedPagePath, | ||
read, | ||
remove, | ||
write, | ||
} | ||
Comment on lines
+74
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a redundancy in the file exports to ensure the compatibility of the import statements in existing JavaScript files using the default statement in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove this and just update all the references to this file! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this isn't the scope of this, but can we actually change this to not wrap these in an object? All of the invocations that are like
fn({ publicDir })
could and should just befn(publicDir)
. So this will require updating the references too