2021-09-26 01:29:07 +08:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs/promises'
|
|
|
|
import { bold } from 'chalk'
|
|
|
|
import glob from 'fast-glob'
|
2021-09-28 20:28:47 +08:00
|
|
|
import { Project, ScriptTarget, ModuleKind } from 'ts-morph'
|
2021-09-26 01:29:07 +08:00
|
|
|
import { parallel } from 'gulp'
|
|
|
|
import { epRoot, buildOutput, projRoot } from './utils/paths'
|
|
|
|
import { yellow, green } from './utils/log'
|
|
|
|
import { buildConfig } from './info'
|
|
|
|
import { withTaskName } from './utils/gulp'
|
|
|
|
import { run } from './utils/process'
|
|
|
|
import type { Module } from './info'
|
|
|
|
|
|
|
|
import type { SourceFile } from 'ts-morph'
|
|
|
|
|
2021-09-28 20:28:47 +08:00
|
|
|
const TSCONFIG_PATH = path.resolve(projRoot, 'tsconfig.json')
|
2021-09-26 01:29:07 +08:00
|
|
|
|
|
|
|
export const genEntryTypes = async () => {
|
|
|
|
const files = await glob('*.ts', {
|
|
|
|
cwd: epRoot,
|
|
|
|
absolute: true,
|
2021-09-28 20:28:47 +08:00
|
|
|
onlyFiles: true,
|
2021-09-26 01:29:07 +08:00
|
|
|
})
|
|
|
|
const project = new Project({
|
|
|
|
compilerOptions: {
|
2021-09-28 20:28:47 +08:00
|
|
|
module: ModuleKind.ESNext,
|
2021-09-26 01:29:07 +08:00
|
|
|
allowJs: true,
|
|
|
|
emitDeclarationOnly: true,
|
|
|
|
noEmitOnError: false,
|
|
|
|
outDir: path.resolve(buildOutput, 'entry/types'),
|
|
|
|
target: ScriptTarget.ESNext,
|
2021-09-28 20:28:47 +08:00
|
|
|
rootDir: epRoot,
|
|
|
|
strict: false,
|
2021-09-26 01:29:07 +08:00
|
|
|
},
|
|
|
|
skipFileDependencyResolution: true,
|
|
|
|
tsConfigFilePath: TSCONFIG_PATH,
|
|
|
|
skipAddingFilesFromTsConfig: true,
|
|
|
|
})
|
|
|
|
const sourceFiles: SourceFile[] = []
|
|
|
|
files.map((f) => {
|
|
|
|
const sourceFile = project.addSourceFileAtPath(f)
|
|
|
|
sourceFiles.push(sourceFile)
|
|
|
|
})
|
2021-09-28 20:28:47 +08:00
|
|
|
project.addSourceFilesAtPaths(path.resolve(projRoot, 'typings', '*.d.ts'))
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
yellow(`Emitting file: ${bold(sourceFile.getFilePath())}`)
|
2021-09-28 20:28:47 +08:00
|
|
|
|
2021-09-26 01:29:07 +08:00
|
|
|
const emitOutput = sourceFile.getEmitOutput()
|
|
|
|
for (const outputFile of emitOutput.getOutputFiles()) {
|
|
|
|
const filepath = outputFile.getFilePath()
|
|
|
|
|
2021-09-28 20:28:47 +08:00
|
|
|
await fs.mkdir(path.dirname(filepath), { recursive: true })
|
2021-09-26 01:29:07 +08:00
|
|
|
await fs.writeFile(
|
|
|
|
filepath,
|
|
|
|
outputFile.getText().replaceAll('@element-plus', '.'),
|
|
|
|
'utf8'
|
|
|
|
)
|
|
|
|
green(`Definition for file: ${bold(sourceFile.getBaseName())} generated`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await Promise.all(tasks)
|
|
|
|
}
|
|
|
|
|
2021-09-28 20:28:47 +08:00
|
|
|
export const copyEntryTypes = (() => {
|
|
|
|
const src = path.resolve(buildOutput, 'entry/types')
|
2021-09-26 01:29:07 +08:00
|
|
|
const copy = (module: Module) =>
|
2021-09-28 20:28:47 +08:00
|
|
|
parallel(
|
|
|
|
withTaskName(`copyEntryTypes:${module}`, () =>
|
|
|
|
run(`rsync -a ${src}/ ${buildConfig[module].output.path}/`)
|
|
|
|
),
|
|
|
|
withTaskName('copyEntryDefinitions', async () => {
|
|
|
|
const files = await glob('*.d.ts', {
|
|
|
|
cwd: epRoot,
|
|
|
|
absolute: true,
|
|
|
|
onlyFiles: true,
|
|
|
|
})
|
|
|
|
await run(
|
|
|
|
`rsync -a ${files.join(' ')} ${buildConfig[module].output.path}/`
|
|
|
|
)
|
|
|
|
})
|
2021-09-26 01:29:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
return parallel(copy('esm'), copy('cjs'))
|
2021-09-28 20:28:47 +08:00
|
|
|
})()
|