2021-10-07 13:53:31 +00:00
|
|
|
import { promises as fsp } from 'fs'
|
2021-10-22 22:33:22 +00:00
|
|
|
import { join, relative, resolve } from 'pathe'
|
2021-10-07 13:53:31 +00:00
|
|
|
import { Nuxt, TSReference } from '@nuxt/kit'
|
2021-10-13 20:01:50 +00:00
|
|
|
import type { TSConfig } from 'pkg-types'
|
2021-10-07 13:53:31 +00:00
|
|
|
import { getModulePaths, getNearestPackage } from './cjs'
|
|
|
|
|
|
|
|
export const writeTypes = async (nuxt: Nuxt) => {
|
|
|
|
const modulePaths = getModulePaths(nuxt.options.modulesDir)
|
|
|
|
|
2021-10-13 20:01:50 +00:00
|
|
|
const tsConfig: TSConfig = {
|
|
|
|
compilerOptions: {
|
|
|
|
target: 'ESNext',
|
|
|
|
module: 'ESNext',
|
|
|
|
moduleResolution: 'Node',
|
|
|
|
strict: true,
|
|
|
|
allowJs: true,
|
|
|
|
noEmit: true,
|
|
|
|
resolveJsonModule: true,
|
|
|
|
types: ['node'],
|
|
|
|
baseUrl: relative(nuxt.options.buildDir, nuxt.options.rootDir),
|
|
|
|
paths: {}
|
2021-10-22 22:33:22 +00:00
|
|
|
},
|
|
|
|
include: [
|
|
|
|
'./nuxt.d.ts',
|
|
|
|
join(relative(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'),
|
|
|
|
...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : []
|
|
|
|
]
|
2021-10-13 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const aliases = {
|
|
|
|
...nuxt.options.alias,
|
2021-10-22 22:25:49 +00:00
|
|
|
'#build': nuxt.options.buildDir,
|
|
|
|
// The `@nuxt/nitro` types will be overwritten by packages/nitro/types/shims.d.ts
|
|
|
|
'#storage': '@nuxt/nitro',
|
|
|
|
'#assets': '@nuxt/nitro'
|
2021-10-13 20:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const alias in aliases) {
|
|
|
|
const relativePath = relative(nuxt.options.rootDir, aliases[alias]).replace(/(?<=\w)\.\w+$/g, '') /* remove extension */ || '.'
|
|
|
|
tsConfig.compilerOptions.paths[alias] = [relativePath]
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { isDirectory } = await fsp.stat(resolve(nuxt.options.rootDir, relativePath))
|
|
|
|
if (isDirectory) {
|
|
|
|
tsConfig.compilerOptions.paths[`${alias}/*`] = [`${relativePath}/*`]
|
|
|
|
}
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
2021-10-07 13:53:31 +00:00
|
|
|
const references: TSReference[] = [
|
|
|
|
...nuxt.options.buildModules,
|
|
|
|
...nuxt.options.modules,
|
|
|
|
...nuxt.options._modules
|
|
|
|
]
|
|
|
|
.filter(f => typeof f === 'string')
|
|
|
|
.map(id => ({ types: getNearestPackage(id, modulePaths)?.name || id }))
|
|
|
|
|
|
|
|
const declarations: string[] = []
|
|
|
|
|
|
|
|
await nuxt.callHook('builder:generateApp')
|
2021-10-13 20:01:50 +00:00
|
|
|
await nuxt.callHook('prepare:types', { references, declarations, tsConfig })
|
2021-10-07 13:53:31 +00:00
|
|
|
|
|
|
|
const declaration = [
|
|
|
|
...references.map((ref) => {
|
|
|
|
if ('path' in ref) {
|
2021-10-22 22:33:22 +00:00
|
|
|
ref.path = relative(nuxt.options.buildDir, ref.path)
|
2021-10-07 13:53:31 +00:00
|
|
|
}
|
|
|
|
return `/// <reference ${renderAttrs(ref)} />`
|
|
|
|
}),
|
|
|
|
...declarations,
|
2021-10-22 22:33:22 +00:00
|
|
|
'',
|
2021-10-07 13:53:31 +00:00
|
|
|
'export {}',
|
|
|
|
''
|
|
|
|
].join('\n')
|
|
|
|
|
2021-10-18 14:22:02 +00:00
|
|
|
async function writeFile () {
|
2021-10-22 22:33:22 +00:00
|
|
|
const GeneratedBy = '// Generated by nuxi'
|
|
|
|
|
2021-10-18 14:22:02 +00:00
|
|
|
const tsConfigPath = resolve(nuxt.options.buildDir, 'tsconfig.json')
|
2021-10-22 22:33:22 +00:00
|
|
|
await fsp.writeFile(tsConfigPath, GeneratedBy + '\n' + JSON.stringify(tsConfig, null, 2))
|
|
|
|
|
|
|
|
const declarationPath = resolve(nuxt.options.buildDir, 'nuxt.d.ts')
|
|
|
|
await fsp.writeFile(declarationPath, GeneratedBy + '\n' + declaration)
|
2021-10-18 14:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is needed for Nuxt 2 which clears the build directory again before building
|
|
|
|
// https://github.com/nuxt/nuxt.js/blob/dev/packages/builder/src/builder.js#L144
|
|
|
|
nuxt.hook('builder:prepared', writeFile)
|
|
|
|
|
|
|
|
await writeFile()
|
2021-10-07 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderAttrs (obj: Record<string, string>) {
|
|
|
|
return Object.entries(obj).map(e => renderAttr(e[0], e[1])).join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderAttr (key: string, value: string) {
|
|
|
|
return value ? `${key}="${value}"` : ''
|
|
|
|
}
|