element-plus/build/types-definitions.ts

125 lines
3.6 KiB
TypeScript
Raw Normal View History

import path from 'path'
2021-09-26 01:29:07 +08:00
import fs from 'fs/promises'
import * as vueCompiler from 'vue/compiler-sfc'
import { Project } from 'ts-morph'
2021-09-26 01:29:07 +08:00
import glob from 'fast-glob'
import { bold } from 'chalk'
import { errorAndExit, green, yellow } from './utils/log'
import { buildOutput, epRoot, pkgRoot, projRoot } from './utils/paths'
2021-09-26 01:29:07 +08:00
import { excludeFiles, pathRewriter } from './utils/pkg'
import type { SourceFile } from 'ts-morph'
2021-09-26 01:29:07 +08:00
const TSCONFIG_PATH = path.resolve(projRoot, 'tsconfig.json')
const outDir = path.resolve(buildOutput, 'types')
/**
* fork = require( https://github.com/egoist/vue-dts-gen/blob/main/src/index.ts
*/
export const generateTypesDefinitions = async () => {
const project = new Project({
compilerOptions: {
emitDeclarationOnly: true,
outDir,
2021-09-26 01:29:07 +08:00
baseUrl: projRoot,
paths: {
'@element-plus/*': ['packages/*'],
},
},
tsConfigFilePath: TSCONFIG_PATH,
skipAddingFilesFromTsConfig: true,
})
const filePaths = excludeFiles(
await glob(['**/*.{js,ts,vue}', '!element-plus/**/*'], {
cwd: pkgRoot,
absolute: true,
onlyFiles: true,
2021-09-26 01:29:07 +08:00
})
)
const epPaths = excludeFiles(
await glob('**/*.{js,ts,vue}', {
cwd: epRoot,
onlyFiles: true,
})
)
2021-09-26 01:29:07 +08:00
const sourceFiles: SourceFile[] = []
await Promise.all([
...filePaths.map(async (file) => {
if (file.endsWith('.vue')) {
2021-09-26 01:29:07 +08:00
const content = await fs.readFile(file, 'utf-8')
const sfc = vueCompiler.parse(content)
const { script, scriptSetup } = sfc.descriptor
if (script || scriptSetup) {
let content = ''
let isTS = false
if (script && script.content) {
content += script.content
if (script.lang === 'ts') isTS = true
}
if (scriptSetup) {
const compiled = vueCompiler.compileScript(sfc.descriptor, {
id: 'xxx',
})
content += compiled.content
if (scriptSetup.lang === 'ts') isTS = true
}
const sourceFile = project.createSourceFile(
path.relative(process.cwd(), file) + (isTS ? '.ts' : '.js'),
content
)
sourceFiles.push(sourceFile)
}
} else {
const sourceFile = project.addSourceFileAtPath(file)
sourceFiles.push(sourceFile)
}
}),
...epPaths.map(async (file) => {
const content = await fs.readFile(path.resolve(epRoot, file), 'utf-8')
sourceFiles.push(
project.createSourceFile(path.resolve(pkgRoot, file), content)
)
}),
])
const diagnostics = project.getPreEmitDiagnostics()
console.log(project.formatDiagnosticsWithColorAndContext(diagnostics))
await project.emit({
emitOnlyDtsFiles: true,
})
2021-09-26 01:29:07 +08:00
const tasks = sourceFiles.map(async (sourceFile) => {
const relativePath = path.relative(pkgRoot, sourceFile.getFilePath())
2021-09-26 01:29:07 +08:00
yellow(`Generating definition for file: ${bold(relativePath)}`)
const emitOutput = sourceFile.getEmitOutput()
const emitFiles = emitOutput.getOutputFiles()
if (emitFiles.length === 0) {
errorAndExit(new Error(`Emit no file: ${bold(relativePath)}`))
}
const tasks = emitFiles.map(async (outputFile) => {
const filepath = outputFile.getFilePath()
2021-09-26 01:29:07 +08:00
await fs.mkdir(path.dirname(filepath), {
recursive: true,
})
2021-09-26 01:29:07 +08:00
await fs.writeFile(
filepath,
pathRewriter('esm')(outputFile.getText()),
'utf8'
)
2021-09-26 01:29:07 +08:00
green(`Definition for file: ${bold(relativePath)} generated`)
})
2021-09-26 01:29:07 +08:00
await Promise.all(tasks)
})
await Promise.all(tasks)
}