Skip to content

Commit c2170ac

Browse files
authored
Convert next.config.js to TypeScript (#57883)
1 parent dc91be0 commit c2170ac

File tree

4 files changed

+12
-32
lines changed

4 files changed

+12
-32
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ WORKDIR $APP_HOME
8484
# Source code
8585
COPY --chown=node:node src src/
8686
COPY --chown=node:node package.json ./
87-
COPY --chown=node:node next.config.js ./
87+
COPY --chown=node:node next.config.ts ./
8888
COPY --chown=node:node tsconfig.json ./
8989

9090
# From the clones stage
@@ -125,7 +125,7 @@ WORKDIR $APP_HOME
125125
# Source code
126126
COPY --chown=node:node src src/
127127
COPY --chown=node:node package.json ./
128-
COPY --chown=node:node next.config.js ./
128+
COPY --chown=node:node next.config.ts ./
129129
COPY --chown=node:node tsconfig.json ./
130130

131131
# From clones stage

next.config.js renamed to next.config.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import type { NextConfig } from 'next'
34

45
import frontmatter from '@gr2m/gray-matter'
5-
// Hardcoded log level function since next.config.js cannot import from TypeScript files
6-
// Matches ./src/observability/logger/lib/log-levels
7-
function getLogLevelNumber() {
8-
const LOG_LEVELS = {
9-
error: 0,
10-
warn: 1,
11-
info: 2,
12-
debug: 3,
13-
}
6+
import { getLogLevelNumber } from '@/observability/logger/lib/log-levels'
147

15-
let defaultLogLevel = 'info'
16-
if (
17-
!process.env.LOG_LEVEL &&
18-
(process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test')
19-
) {
20-
defaultLogLevel = 'debug'
21-
}
22-
23-
const envLogLevel = process.env.LOG_LEVEL?.toLowerCase() || defaultLogLevel
24-
const logLevel = LOG_LEVELS[envLogLevel] !== undefined ? envLogLevel : defaultLogLevel
25-
26-
return LOG_LEVELS[logLevel]
27-
}
28-
29-
// Replace imports with hardcoded values
308
const ROOT = process.env.ROOT || '.'
319

32-
// Hard-coded language keys to avoid TypeScript import in config file
10+
// Language keys are defined here because Next.js config compilation doesn't resolve the @/ path alias
11+
// Importing from src/languages/lib/languages.ts would fail when it tries to import @/frame/lib/constants
12+
// This must match the languages defined in src/languages/lib/languages.ts
3313
const languageKeys = ['en', 'es', 'ja', 'pt', 'zh', 'ru', 'fr', 'ko', 'de']
3414

3515
const homepage = path.posix.join(ROOT, 'content/index.md')
3616
const { data } = frontmatter(fs.readFileSync(homepage, 'utf8'))
37-
const productIds = data.children
17+
const productIds = data.children as string[]
3818

39-
export default {
19+
const config: NextConfig = {
4020
// Transpile @primer/react so Next's webpack can process its CSS and other assets
4121
// This ensures CSS in node_modules/@primer/react is handled by the app's loaders.
4222
transpilePackages: ['@primer/react'],
@@ -106,3 +86,5 @@ export default {
10686
styledComponents: true,
10787
},
10888
}
89+
90+
export default config

src/observability/logger/lib/log-levels.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ The log level is controlled by the `LOG_LEVEL` environment variable, where lower
44
if log level is 'info', only 'info', 'warn', and 'error' logs will be output
55
if log level is 'debug', all logs will be output
66
if log level is 'error', only 'error' logs will be output
7-
8-
NOTE: This file is `.js` because next.config.js does not yet support importing
97
*/
108
export const LOG_LEVELS = {
119
error: 0,

src/pages/_error.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Error.getInitialProps = async (ctx: NextPageContext) => {
2626
// You can't import `../lib/failbot.js` here in this
2727
// file because it gets imported by webpack to be used in the
2828
// client-side JS bundle. It *could* be solved by overriding
29-
// the webpack configuration in our `next.config.js` but this is prone
29+
// the webpack configuration in our `next.config.ts` but this is prone
3030
// to be fragile since ignoring code can be hard to get right
3131
// and the more we override there, the harder it will become to
3232
// upgrade NextJS in the future because of moving parts.

0 commit comments

Comments
 (0)