Skip to content

Commit ae80876

Browse files
committed
feat: add setRuntimeConfig module utility
1 parent b9a109c commit ae80876

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

build.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineBuildConfig({
99
'src/experimental',
1010
'src/config',
1111
'src/module.ts',
12+
isStub ? { input: 'src/module-utils/', outDir: 'dist/module-utils', format: 'esm' } : 'src/module-utils/index.mjs',
1213
'src/vitest-environment',
1314
isStub ? { input: 'src/runtime-utils/', outDir: 'dist/runtime-utils', format: 'esm' } : 'src/runtime-utils/index.mjs',
1415
{ input: 'src/runtime/', outDir: 'dist/runtime', format: 'esm' }
@@ -17,5 +18,6 @@ export default defineBuildConfig({
1718
"#app/entry",
1819
"#build/root-component.mjs",
1920
"#imports",
20-
]
21+
],
22+
failOnWarn: false,
2123
})

module-utils.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dist/module-utils/index.mjs'

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"./e2e": "./dist/e2e.mjs",
1616
"./experimental": "./dist/experimental.mjs",
1717
"./module": "./dist/module.mjs",
18+
"./module-utils": "./dist/module-utils/index.mjs",
1819
"./runtime": "./dist/runtime-utils/index.mjs",
1920
"./vitest-environment": "./dist/vitest-environment.mjs"
2021
},
@@ -81,6 +82,7 @@
8182
"nuxt": "3.8.2",
8283
"playwright-core": "1.40.1",
8384
"rollup": "4.7.0",
85+
"scule": "^1.1.1",
8486
"semver": "7.5.4",
8587
"unbuild": "latest",
8688
"unimport": "3.6.1",

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/server.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import * as _kit from '@nuxt/kit'
66
import { resolve } from 'pathe'
77
import { useTestContext } from './context'
88

9+
910
// @ts-expect-error type cast
1011
// eslint-disable-next-line
1112
const kit: typeof _kit = _kit.default || _kit
1213

13-
export async function startServer () {
14+
export async function startServer (env: Record<string, unknown> = {}) {
1415
const ctx = useTestContext()
1516
await stopServer()
1617
const host = '127.0.0.1'
@@ -26,7 +27,8 @@ export async function startServer () {
2627
_PORT: String(port), // Used by internal _dev command
2728
PORT: String(port),
2829
HOST: host,
29-
NODE_ENV: 'development'
30+
NODE_ENV: 'development',
31+
...env
3032
}
3133
})
3234
await waitForPort(port, { retries: 32, host }).catch(() => {})
@@ -53,7 +55,8 @@ export async function startServer () {
5355
...process.env,
5456
PORT: String(port),
5557
HOST: host,
56-
NODE_ENV: 'test'
58+
NODE_ENV: 'test',
59+
...env
5760
}
5861
})
5962
await waitForPort(port, { retries: 20, host })

src/module-utils/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { setRuntimeConfig } from './runtime-config'

src/module-utils/runtime-config.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { snakeCase } from "scule"
2+
import { startServer } from "../e2e";
3+
4+
const NUXT_ENV_PREFIX = 'NUXT_'
5+
6+
export function flattenObject(obj: Record<string, unknown> = {}) {
7+
const flattened: Record<string, unknown> = {}
8+
9+
for (const key in obj) {
10+
if (!(key in obj)) continue
11+
12+
const entry = obj[key]
13+
if (typeof entry !== 'object' || entry == null) {
14+
flattened[key] = obj[key]
15+
continue
16+
}
17+
const flatObject = flattenObject(entry as Record<string, unknown>)
18+
19+
for (const x in flatObject) {
20+
if (!(x in flatObject)) continue
21+
22+
flattened[key + '_' + x] = flatObject[x]
23+
}
24+
}
25+
26+
return flattened
27+
}
28+
29+
export function convertObjectToConfig(obj: Record<string, unknown>) {
30+
const makeEnvKey = (str: string) => `${NUXT_ENV_PREFIX}${snakeCase(str).toUpperCase()}`
31+
32+
const env: Record<string, unknown> = {}
33+
const flattened = flattenObject(obj)
34+
for (const key in flattened) {
35+
env[makeEnvKey(key)] = flattened[key]
36+
}
37+
38+
return env
39+
}
40+
41+
export async function setRuntimeConfig(env: Record<string, unknown>) {
42+
const converted = convertObjectToConfig(env)
43+
await startServer(converted)
44+
45+
// restore
46+
return async () => startServer()
47+
}

0 commit comments

Comments
 (0)