Skip to content

Commit 6f3808e

Browse files
committed
Compiler: Support boolean and number primtives in next.config defines (#92731)
Expand `config.compiler.define` to support number and boolean primitives as well. The support is already in Turbopack's rewrite engine, as well as Webpack's DefinePlugin. We just need to modify the zod schema to actually support passing numbers and bools. Added an E2E test that should check all supported types.
1 parent fbc7684 commit 6f3808e

File tree

7 files changed

+65
-5
lines changed

7 files changed

+65
-5
lines changed

packages/next/src/build/define-env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const DEFINE_ENV_EXPRESSION = Symbol('DEFINE_ENV_EXPRESSION')
4646
interface DefineEnv {
4747
[key: string]:
4848
| string
49+
| number
4950
| string[]
5051
| boolean
5152
| { [DEFINE_ENV_EXPRESSION]: string }

packages/next/src/server/config-schema.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,12 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
509509
useLightningcss: z.boolean().optional(),
510510
}),
511511
]),
512-
define: z.record(z.string(), z.string()).optional(),
513-
defineServer: z.record(z.string(), z.string()).optional(),
512+
define: z
513+
.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))
514+
.optional(),
515+
defineServer: z
516+
.record(z.string(), z.union([z.string(), z.number(), z.boolean()]))
517+
.optional(),
514518
runAfterProductionCompile: z
515519
.function()
516520
.returns(z.promise(z.void()))

packages/next/src/server/config-shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,13 +1453,13 @@ export interface NextConfig {
14531453
* Replaces variables in your code during compile time. Each key will be
14541454
* replaced with the respective values.
14551455
*/
1456-
define?: Record<string, string>
1456+
define?: Record<string, string | number | boolean>
14571457

14581458
/**
14591459
* Replaces server-only (Node.js and Edge) variables in your code during compile time.
14601460
* Each key will be replaced with the respective values.
14611461
*/
1462-
defineServer?: Record<string, string>
1462+
defineServer?: Record<string, string | number | boolean>
14631463

14641464
/**
14651465
* A hook function that executes after production build compilation finishes,

test/e2e/define/app/client-component.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,23 @@ export function ClientExpr() {
1616
</>
1717
)
1818
}
19+
20+
export function ClientNumber() {
21+
return (
22+
<>
23+
{typeof MY_NUMBER_VARIABLE === 'number'
24+
? String(MY_NUMBER_VARIABLE)
25+
: 'not set'}
26+
</>
27+
)
28+
}
29+
30+
export function ClientBoolean() {
31+
return (
32+
<>
33+
{typeof MY_BOOLEAN_VARIABLE === 'boolean'
34+
? String(MY_BOOLEAN_VARIABLE)
35+
: 'not set'}
36+
</>
37+
)
38+
}

test/e2e/define/app/page.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/* eslint-disable no-undef */
2-
import { ClientValue, ClientExpr } from './client-component'
2+
import {
3+
ClientValue,
4+
ClientExpr,
5+
ClientNumber,
6+
ClientBoolean,
7+
} from './client-component'
38

49
export default function Page() {
510
return (
@@ -20,6 +25,24 @@ export default function Page() {
2025
<li>
2126
Client expr: <ClientExpr />
2227
</li>
28+
<li>
29+
Server number:{' '}
30+
{typeof MY_NUMBER_VARIABLE === 'number'
31+
? String(MY_NUMBER_VARIABLE)
32+
: 'not set'}
33+
</li>
34+
<li>
35+
Client number: <ClientNumber />
36+
</li>
37+
<li>
38+
Server boolean:{' '}
39+
{typeof MY_BOOLEAN_VARIABLE === 'boolean'
40+
? String(MY_BOOLEAN_VARIABLE)
41+
: 'not set'}
42+
</li>
43+
<li>
44+
Client boolean: <ClientBoolean />
45+
</li>
2346
</ul>
2447
)
2548
}

test/e2e/define/define.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ describe('compiler.define', () => {
3131
expect(loadedText).toContain('Server expr: barbaz')
3232
expect(loadedText).toContain('Client expr: barbaz')
3333
})
34+
35+
it('should render a number variable on server and client side', async () => {
36+
expect(loadedText).toContain('Server number: 42')
37+
expect(loadedText).toContain('Client number: 42')
38+
})
39+
40+
it('should render a boolean variable on server and client side', async () => {
41+
expect(loadedText).toContain('Server boolean: true')
42+
expect(loadedText).toContain('Client boolean: true')
43+
})
3444
})
3545

3646
describe('compiler.defineServer', () => {

test/e2e/define/next.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = {
33
define: {
44
MY_MAGIC_VARIABLE: 'foobar',
55
'process.env.MY_MAGIC_EXPR': 'barbaz',
6+
MY_NUMBER_VARIABLE: 42,
7+
MY_BOOLEAN_VARIABLE: true,
68
},
79
defineServer: {
810
MY_SERVER_VARIABLE: 'server',

0 commit comments

Comments
 (0)