Skip to content

Commit d49f56a

Browse files
author
Tiffany Le-Nguyen
authored
chore: 🚙 type some helper files and constants (#860)
* chore: type a couple files * chore: update snapidoos * chore: revert snapidoo
1 parent 6c167f3 commit d49f56a

File tree

4 files changed

+168
-28
lines changed

4 files changed

+168
-28
lines changed

‎src/constants.js renamed to ‎src/constants.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const HANDLER_FUNCTION_NAME = '___netlify-handler'
2-
const ODB_FUNCTION_NAME = '___netlify-odb-handler'
3-
const IMAGE_FUNCTION_NAME = '_ipx'
1+
export const HANDLER_FUNCTION_NAME = '___netlify-handler'
2+
export const ODB_FUNCTION_NAME = '___netlify-odb-handler'
3+
export const IMAGE_FUNCTION_NAME = '_ipx'
44

55
// These are paths in .next that shouldn't be publicly accessible
6-
const HIDDEN_PATHS = [
6+
export const HIDDEN_PATHS = [
77
'/cache/*',
88
'/server/*',
99
'/serverless/*',
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
const {
2-
posix: { join },
3-
} = require('path')
1+
import { posix } from 'path'
2+
3+
export const restoreCache = async ({ cache, publish }) => {
4+
const cacheDir = posix.join(publish, 'cache')
45

5-
exports.restoreCache = async ({ cache, publish }) => {
6-
const cacheDir = join(publish, 'cache')
76
if (await cache.restore(cacheDir)) {
87
console.log('Next.js cache restored.')
98
} else {
109
console.log('No Next.js cache to restore.')
1110
}
1211
}
1312

14-
exports.saveCache = async ({ cache, publish }) => {
15-
const cacheDir = join(publish, 'cache')
13+
export const saveCache = async ({ cache, publish }) => {
14+
const cacheDir = posix.join(publish, 'cache')
1615

17-
const buildManifest = join(publish, 'build-manifest.json')
16+
const buildManifest = posix.join(publish, 'build-manifest.json')
1817
if (await cache.save(cacheDir, { digests: [buildManifest] })) {
1918
console.log('Next.js cache saved.')
2019
} else {
2120
console.log('No Next.js cache to save.')
2221
}
23-
}
22+
}

‎src/helpers/config.js renamed to ‎src/helpers/config.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
/* eslint-disable max-lines */
22

3-
const { yellowBright } = require('chalk')
4-
const { readJSON, existsSync } = require('fs-extra')
5-
const { outdent } = require('outdent')
6-
const { join, dirname, relative } = require('pathe')
7-
const slash = require('slash')
3+
import { NetlifyConfig } from '@netlify/build'
4+
import { yellowBright } from 'chalk'
5+
import { readJSON } from 'fs-extra'
6+
import { PrerenderManifest } from 'next/dist/build'
7+
import { outdent } from 'outdent'
8+
import { join, dirname, relative } from 'pathe'
9+
import slash from 'slash'
810

9-
const defaultFailBuild = (message, { error }) => {
11+
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, HIDDEN_PATHS } from '../constants'
12+
13+
import { RequiredServerFiles } from './requiredServerFilesType'
14+
15+
const defaultFailBuild = (message: string, { error } ): never => {
1016
throw new Error(`${message}\n${error && error.stack}`)
1117
}
1218

13-
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, HIDDEN_PATHS } = require('../constants')
14-
1519
const ODB_FUNCTION_PATH = `/.netlify/builders/${ODB_FUNCTION_NAME}`
1620
const HANDLER_FUNCTION_PATH = `/.netlify/functions/${HANDLER_FUNCTION_NAME}`
1721

1822
const CATCH_ALL_REGEX = /\/\[\.{3}(.*)](.json)?$/
1923
const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)]{2}(.json)?$/
2024
const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)]/g
2125

22-
const getNetlifyRoutes = (nextRoute) => {
26+
const getNetlifyRoutes = (nextRoute: string): Array<string> => {
2327
let netlifyRoutes = [nextRoute]
2428

2529
// If the route is an optional catch-all route, we need to add a second
@@ -54,8 +58,12 @@ const getNetlifyRoutes = (nextRoute) => {
5458
return netlifyRoutes
5559
}
5660

57-
exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
58-
const { dynamicRoutes, routes: staticRoutes } = await readJSON(
61+
export const generateRedirects = async ({ netlifyConfig, basePath, i18n }: {
62+
netlifyConfig: NetlifyConfig,
63+
basePath: string,
64+
i18n
65+
}) => {
66+
const { dynamicRoutes, routes: staticRoutes }: PrerenderManifest = await readJSON(
5967
join(netlifyConfig.build.publish, 'prerender-manifest.json'),
6068
)
6169

@@ -123,6 +131,8 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
123131
from: `${basePath}/*`,
124132
to: HANDLER_FUNCTION_PATH,
125133
status: 200,
134+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
135+
// @ts-ignore
126136
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
127137
force: true,
128138
},
@@ -158,14 +168,16 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
158168
}
159169
}
160170

161-
exports.getNextConfig = async function getNextConfig({ publish, failBuild = defaultFailBuild }) {
171+
export const getNextConfig = async function getNextConfig({ publish, failBuild = defaultFailBuild }) {
162172
try {
163-
const { config, appDir, ignore } = await readJSON(join(publish, 'required-server-files.json'))
173+
const { config, appDir, ignore }: RequiredServerFiles = await readJSON(join(publish, 'required-server-files.json'))
164174
if (!config) {
175+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
176+
// @ts-ignore
165177
return failBuild('Error loading your Next config')
166178
}
167179
return { ...config, appDir, ignore }
168-
} catch (error) {
180+
} catch (error: unknown) {
169181
return failBuild('Error loading your Next config', { error })
170182
}
171183
}
@@ -180,7 +192,7 @@ const resolveModuleRoot = (moduleName) => {
180192

181193
const DEFAULT_EXCLUDED_MODULES = ['sharp', 'electron']
182194

183-
exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
195+
export const configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
184196
/* eslint-disable no-underscore-dangle */
185197
netlifyConfig.functions._ipx ||= {}
186198
netlifyConfig.functions._ipx.node_bundler = 'nft'
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* eslint-disable eslint-comments/disable-enable-pair, @typescript-eslint/no-empty-interface, no-use-before-define */
2+
/**
3+
* This was generated using @see {@link https://quicktype.io/}, and using the
4+
* demo's {@code demos/default/.next/required-server-files.json} file. I was
5+
* unable to find any types for this file so instead this was manually generated
6+
*/
7+
8+
export interface RequiredServerFiles {
9+
version?: number;
10+
config?: Config;
11+
appDir?: string;
12+
files?: string[];
13+
ignore?: string[];
14+
}
15+
16+
export interface Env {
17+
}
18+
19+
export interface Config {
20+
env?: Env;
21+
webpack?: null;
22+
webpackDevMiddleware?: null;
23+
eslint?: Eslint;
24+
typescript?: Typescript;
25+
distDir?: string;
26+
cleanDistDir?: boolean;
27+
assetPrefix?: string;
28+
configOrigin?: string;
29+
useFileSystemPublicRoutes?: boolean;
30+
generateEtags?: boolean;
31+
pageExtensions?: string[];
32+
target?: string;
33+
poweredByHeader?: boolean;
34+
compress?: boolean;
35+
analyticsId?: string;
36+
images?: Images;
37+
devIndicators?: DevIndicators;
38+
onDemandEntries?: OnDemandEntries;
39+
amp?: Amp;
40+
basePath?: string;
41+
sassOptions?: Env;
42+
trailingSlash?: boolean;
43+
i18n?: I18N;
44+
productionBrowserSourceMaps?: boolean;
45+
optimizeFonts?: boolean;
46+
excludeDefaultMomentLocales?: boolean;
47+
serverRuntimeConfig?: Env;
48+
publicRuntimeConfig?: Env;
49+
reactStrictMode?: boolean;
50+
httpAgentOptions?: HTTPAgentOptions;
51+
outputFileTracing?: boolean;
52+
staticPageGenerationTimeout?: number;
53+
swcMinify?: boolean;
54+
experimental?: Experimental;
55+
future?: Future;
56+
configFileName?: string;
57+
}
58+
59+
export interface Amp {
60+
canonicalBase?: string;
61+
}
62+
63+
export interface DevIndicators {
64+
buildActivity?: boolean;
65+
buildActivityPosition?: string;
66+
}
67+
68+
export interface Eslint {
69+
ignoreDuringBuilds?: boolean;
70+
}
71+
72+
export interface Experimental {
73+
cpus?: number;
74+
sharedPool?: boolean;
75+
plugins?: boolean;
76+
profiling?: boolean;
77+
isrFlushToDisk?: boolean;
78+
workerThreads?: boolean;
79+
pageEnv?: boolean;
80+
optimizeImages?: boolean;
81+
optimizeCss?: boolean;
82+
scrollRestoration?: boolean;
83+
externalDir?: boolean;
84+
reactRoot?: boolean;
85+
disableOptimizedLoading?: boolean;
86+
gzipSize?: boolean;
87+
craCompat?: boolean;
88+
esmExternals?: boolean;
89+
isrMemoryCacheSize?: number;
90+
concurrentFeatures?: boolean;
91+
serverComponents?: boolean;
92+
fullySpecified?: boolean;
93+
outputFileTracingRoot?: string;
94+
outputStandalone?: boolean;
95+
}
96+
97+
export interface Future {
98+
strictPostcssConfiguration?: boolean;
99+
}
100+
101+
export interface HTTPAgentOptions {
102+
keepAlive?: boolean;
103+
}
104+
105+
export interface I18N {
106+
defaultLocale?: string;
107+
locales?: string[];
108+
}
109+
110+
export interface Images {
111+
deviceSizes?: number[];
112+
imageSizes?: number[];
113+
path?: string;
114+
loader?: string;
115+
domains?: any[];
116+
disableStaticImages?: boolean;
117+
minimumCacheTTL?: number;
118+
formats?: string[];
119+
}
120+
121+
export interface OnDemandEntries {
122+
maxInactiveAge?: number;
123+
pagesBufferLength?: number;
124+
}
125+
126+
export interface Typescript {
127+
ignoreBuildErrors?: boolean;
128+
tsconfigPath?: string;
129+
}

0 commit comments

Comments
 (0)