-
Notifications
You must be signed in to change notification settings - Fork 89
chore: 🚙 type some helper files and constants #860
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
Conversation
✔️ Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo canceled. 🔨 Explore the source changes: 2c32256 🔍 Inspect the deploy log: https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/61a74200020e9200077bfc6d |
@@ -123,6 +131,8 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => { | |||
from: `${basePath}/*`, | |||
to: HANDLER_FUNCTION_PATH, | |||
status: 200, | |||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | |||
// @ts-ignore |
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 left that in as I did the first pass to TS. I figured we'd want to get this in incrementally and worry too much about problematic missing types for now.
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.
Is this type missing in @netlify/build
then?
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.
Nah just one that would require a bit of refactoring of the function this ts-ignore refers to
if (!config) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore |
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.
Same here, expects a second object and I just ignored it as I passed through :P
return failBuild('Error loading your Next config') | ||
} | ||
return { ...config, appDir, ignore } | ||
} catch (error) { | ||
} catch (error: unknown) { |
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.
It is best practice to have errors as unknown
since there's no good way to predict the type of an error
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.
Fantastic! Thanks so much for doign this.
import { NetlifyConfig } from '@netlify/build' | ||
import { yellowBright } from 'chalk' | ||
import { readJSON } from 'fs-extra' | ||
import { PrerenderManifest } from 'next/dist/build' |
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.
Can this be import type
?
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 can't do import { PrerenderManifest } from 'next/types'
since they don't have it in their types, is that what you mean?
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.
No, I just mean use the type import syntax to make it clear that you're only importing the type:
import type { PrerenderManifest } from 'next/dist/build'
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.
Ohhh TIL
export const generateRedirects = async ({ netlifyConfig, basePath, i18n }: { | ||
netlifyConfig: NetlifyConfig, | ||
basePath: string, | ||
i18n |
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.
You can get this type as NextConfig["i18n"]
from next
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.
oo thanks! Wasn't sure where it was coming from
@@ -123,6 +131,8 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => { | |||
from: `${basePath}/*`, | |||
to: HANDLER_FUNCTION_PATH, | |||
status: 200, | |||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | |||
// @ts-ignore |
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.
Is this type missing in @netlify/build
then?
@@ -180,7 +192,7 @@ const resolveModuleRoot = (moduleName) => { | |||
|
|||
const DEFAULT_EXCLUDED_MODULES = ['sharp', 'electron'] | |||
|
|||
exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => { | |||
export const configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => { |
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.
Can these args be typed?
@@ -0,0 +1,129 @@ | |||
/* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-empty-interface, no-use-before-define */ |
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.
Ah, nice. Maybe we should contribute this upstream!
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.
Good call! Although I'm sure the definition file is missing some types since it was just auto-generated based on the demo's required-server-files.json
} = require('path') | ||
import { posix } from 'path' | ||
|
||
export const restoreCache = async ({ cache, publish }) => { |
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 htink it's a good idea if we ensure that all exported functions have typed args and return values, even if they're just defined inline.
test/__snapshots__/index.js.snap
Outdated
"fr/static.html", | ||
] | ||
`; | ||
exports[`onBuild() generates static files manifest 1`] = `Array []`; |
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.
You updated the snapshot after running ntl build
, which means the files had already been moved. This is why I added the updateSnapshot npm script: you need to just build the site, not run the plugin too.
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.
snapidoo!
export interface Config { | ||
env?: Env; | ||
webpack?: null; | ||
webpackDevMiddleware?: null; |
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.
^ these will have to be manually updated, since it's clearly wrong
Summary
Our Netlify Next Plugin is written in mostly JS, but we'd like it to be using TypeScript for readability and prevent some bugs which we've had that could have been avoided with TS. This PR changes a couple files to use TypeScript, as I go through the code and get more familiar with it.
Point of entries
src/helpers/index.js
is one of the bigger files, and uses many of the other helpers. Thus I decided to go through the rest of the helpers to minimize impact.The files touched by this conversion:
src/constants.js
src/helpers/cache.js
src/helpers/config.js
Test plan
Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal
Standard checks:
🧪 Once merged, make sure to update the version if needed and that it was published correctly.