Skip to content

Commit 7fc79de

Browse files
authored
fix: add server imports for composables (#5)
1 parent b84117f commit 7fc79de

File tree

6 files changed

+424
-2176
lines changed

6 files changed

+424
-2176
lines changed

pnpm-lock.yaml

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

src/module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ export default defineNitroPlugin(() => {
248248
registerNitroPlugin(nuxt, pluginPath.dst)
249249
}
250250

251+
// Ensure server/Nitro bundles can resolve auto-imported composables.
252+
addServerImportsDir(resolver.resolve('./runtime/composables'))
251253
addImportsDir(resolver.resolve('./runtime/composables'))
252254

253255
nuxt.hook('eslint:config:addons', (addons) => {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { execSync } from 'node:child_process'
2+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
3+
import { join } from 'node:path'
4+
import { fileURLToPath } from 'node:url'
5+
import { describe, expect, it } from 'vitest'
6+
7+
function listFiles(dir: string): string[] {
8+
const out: string[] = []
9+
for (const entry of readdirSync(dir)) {
10+
const p = join(dir, entry)
11+
const st = statSync(p)
12+
if (st.isDirectory())
13+
out.push(...listFiles(p))
14+
else
15+
out.push(p)
16+
}
17+
return out
18+
}
19+
20+
describe('cloudflare preset auto-imports', () => {
21+
it('does not leave useSafeRuntimeConfig as an unresolved global', () => {
22+
const fixtureDir = fileURLToPath(new URL('./fixtures/cloudflare-auto-import', import.meta.url))
23+
execSync('pnpm nuxi build', { cwd: fixtureDir, stdio: 'pipe' })
24+
25+
const serverDir = join(fixtureDir, '.output/server')
26+
expect(existsSync(serverDir)).toBe(true)
27+
28+
const failing: string[] = []
29+
for (const file of listFiles(serverDir).filter(f => f.endsWith('.mjs'))) {
30+
const src = readFileSync(file, 'utf-8')
31+
if (!src.includes('useSafeRuntimeConfig('))
32+
continue
33+
34+
const hasBinding = (
35+
/\bfunction\s+useSafeRuntimeConfig\b/.test(src)
36+
|| /\b(?:const|let|var)\s+useSafeRuntimeConfig\b/.test(src)
37+
|| (src.includes('import') && src.includes('useSafeRuntimeConfig'))
38+
)
39+
40+
if (!hasBinding)
41+
failing.push(file)
42+
}
43+
44+
expect(failing).toEqual([])
45+
}, 120000)
46+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { object, string } from 'valibot'
2+
import SafeRuntimeConfig from '../../../src/module'
3+
4+
const runtimeConfigSchema = object({
5+
public: object({
6+
apiBase: string(),
7+
}),
8+
secretKey: string(),
9+
})
10+
11+
export default defineNuxtConfig({
12+
modules: [SafeRuntimeConfig],
13+
14+
nitro: {
15+
preset: 'cloudflare_module',
16+
},
17+
18+
runtimeConfig: {
19+
secretKey: 'test-secret-key',
20+
public: {
21+
apiBase: 'https://api.test.com',
22+
},
23+
},
24+
25+
safeRuntimeConfig: {
26+
$schema: runtimeConfigSchema,
27+
},
28+
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "nuxt-safe-runtime-config-test-cloudflare-auto-import",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"prepare": "nuxt prepare"
7+
},
8+
"dependencies": {
9+
"valibot": "catalog:"
10+
},
11+
"devDependencies": {
12+
"nuxt": "catalog:"
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default defineEventHandler(() => {
2+
const config = useSafeRuntimeConfig() as any
3+
return { ok: true, apiBase: config.public.apiBase }
4+
})

0 commit comments

Comments
 (0)