|
| 1 | +import{execFile}from'node:child_process' |
| 2 | +import{mkdtemp,writeFile}from'node:fs/promises' |
| 3 | +import{tmpdir}from'node:os' |
| 4 | +import{join}from'node:path' |
| 5 | +importprocessfrom'node:process' |
| 6 | +import{fileURLToPath}from'node:url' |
| 7 | +import{promisify}from'node:util' |
| 8 | + |
| 9 | +import{describe,expect,it}from'vitest' |
| 10 | + |
| 11 | +constexecFileAsync=promisify(execFile) |
| 12 | + |
| 13 | +constloaderPath=fileURLToPath(newURL('../../../src/utils/nuxt-config.ts',import.meta.url)) |
| 14 | + |
| 15 | +/** |
| 16 | + * `getNuxtConfig` picks between Node's own loader and `jiti` by inspecting the |
| 17 | + * error codes Node raises, and Vitest resolves dynamic imports through Vite |
| 18 | + * rather than Node. Every case therefore runs in a real Node process, otherwise |
| 19 | + * the branch under test is never the one taken. |
| 20 | + */ |
| 21 | +asyncfunctionloadConfig(files: Record<string,string>){ |
| 22 | +constcwd=awaitmkdtemp(join(tmpdir(),'nuxi-nuxt-config-')) |
| 23 | +for(const[name,contents]ofObject.entries(files)){ |
| 24 | +awaitwriteFile(join(cwd,name),contents,'utf8') |
| 25 | +} |
| 26 | + |
| 27 | +constscript=` |
| 28 | + const { getNuxtConfig } = await import(${JSON.stringify(loaderPath)}) |
| 29 | + process.stdout.write(JSON.stringify(await getNuxtConfig(${JSON.stringify(cwd)}))) |
| 30 | + ` |
| 31 | +const{ stdout, stderr }=awaitexecFileAsync(process.execPath,['--input-type=module','--eval',script],{ cwd }) |
| 32 | +return{config: JSON.parse(stdout), stderr } |
| 33 | +} |
| 34 | + |
| 35 | +describe('getNuxtConfig',()=>{ |
| 36 | +it('should load a config with Node\'s own loader',async()=>{ |
| 37 | +const{ config }=awaitloadConfig({ |
| 38 | +'package.json': '{"name":"native","type":"module"}', |
| 39 | +'nuxt.config.ts': 'export default defineNuxtConfig({ modules: [\'@nuxt/image\'] })\n', |
| 40 | +}) |
| 41 | + |
| 42 | +expect(config).toMatchObject({modules: ['@nuxt/image']}) |
| 43 | +}) |
| 44 | + |
| 45 | +it('should strip erasable types',async()=>{ |
| 46 | +const{ config }=awaitloadConfig({ |
| 47 | +'package.json': '{"name":"typed","type":"module"}', |
| 48 | +'nuxt.config.ts': 'const ssr: boolean = false\nexport default defineNuxtConfig({ ssr })\n', |
| 49 | +}) |
| 50 | + |
| 51 | +expect(config).toMatchObject({ssr: false}) |
| 52 | +}) |
| 53 | + |
| 54 | +it('should fall back to `jiti` for aliases Node cannot resolve',async()=>{ |
| 55 | +const{ config }=awaitloadConfig({ |
| 56 | +'package.json': '{"name":"aliased","type":"module"}', |
| 57 | +'module.ts': 'export const mod = \'@nuxt/content\'\n', |
| 58 | +'nuxt.config.ts': 'import { mod } from \'~/module\'\n\nexport default defineNuxtConfig({ modules: [mod] })\n', |
| 59 | +}) |
| 60 | + |
| 61 | +expect(config).toMatchObject({modules: ['@nuxt/content']}) |
| 62 | +}) |
| 63 | + |
| 64 | +it('should fall back to `jiti` for TypeScript that cannot be stripped',async()=>{ |
| 65 | +const{ config }=awaitloadConfig({ |
| 66 | +'package.json': '{"name":"enums","type":"module"}', |
| 67 | +'nuxt.config.ts': 'enum Mod { Image = \'@nuxt/image\' }\n\nexport default defineNuxtConfig({ modules: [Mod.Image] })\n', |
| 68 | +}) |
| 69 | + |
| 70 | +expect(config).toMatchObject({modules: ['@nuxt/image']}) |
| 71 | +}) |
| 72 | + |
| 73 | +it('should load a plain JavaScript config',async()=>{ |
| 74 | +const{ config }=awaitloadConfig({ |
| 75 | +'package.json': '{"name":"plain","type":"module"}', |
| 76 | +'nuxt.config.mjs': 'export default defineNuxtConfig({ ssr: false })\n', |
| 77 | +}) |
| 78 | + |
| 79 | +expect(config).toMatchObject({ssr: false}) |
| 80 | +}) |
| 81 | + |
| 82 | +it('should return an empty config when there is no config file',async()=>{ |
| 83 | +const{ config }=awaitloadConfig({'package.json': '{"name":"bare","type":"module"}'}) |
| 84 | + |
| 85 | +expect(config).toEqual({}) |
| 86 | +}) |
| 87 | + |
| 88 | +it('should return an empty config when the config throws',async()=>{ |
| 89 | +const{ config }=awaitloadConfig({ |
| 90 | +'package.json': '{"name":"broken","type":"module"}', |
| 91 | +'nuxt.config.ts': 'throw new Error(\'boom\')\n', |
| 92 | +}) |
| 93 | + |
| 94 | +expect(config).toEqual({}) |
| 95 | +}) |
| 96 | + |
| 97 | +it('should not warn about module type for a config without `type: module`',async()=>{ |
| 98 | +const{ config, stderr }=awaitloadConfig({ |
| 99 | +'package.json': '{"name":"typeless"}', |
| 100 | +'nuxt.config.ts': 'export default defineNuxtConfig({ ssr: true })\n', |
| 101 | +}) |
| 102 | + |
| 103 | +expect(config).toMatchObject({ssr: true}) |
| 104 | +expect(stderr).not.toContain('MODULE_TYPELESS_PACKAGE_JSON') |
| 105 | +}) |
| 106 | +}) |
0 commit comments