mirror of
https://github.com/idrawjs/idraw
synced 2026-05-21 16:48:23 +00:00
101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
import ts from 'typescript';
|
|
import type { CompilerOptions } from 'typescript';
|
|
import { Project, ScriptTarget, ModuleResolutionKind } from 'ts-morph';
|
|
import type { CompilerOptions as TsMorphCompilerOptions } from 'ts-morph';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import * as glob from 'glob';
|
|
import { packages } from './config';
|
|
import { joinPackagePath } from './util/project';
|
|
import { removeFullDir } from './util/file';
|
|
|
|
build();
|
|
|
|
async function build() {
|
|
for (let i = 0; i < packages.length; i++) {
|
|
const pkg = packages[i];
|
|
const { dirName } = pkg;
|
|
const pkgDir = path.resolve(`packages/${dirName}`);
|
|
console.log(`Start to build ESM for ${dirName}`);
|
|
console.log(`Remove packages/${dirName}/dist/`);
|
|
removeFullDir(`${pkgDir}/dist`);
|
|
buildPackage(dirName);
|
|
checkPackageDts(dirName);
|
|
console.log(`Build ESM of ${dirName} successfully!`);
|
|
}
|
|
}
|
|
|
|
function checkPackageDts(dirName: string) {
|
|
console.log(`Checking dts files for ${dirName} ...`);
|
|
const pattern = '**/*.js';
|
|
const cwd = joinPackagePath(dirName, 'dist');
|
|
const files = glob.sync(pattern, { cwd });
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
const jsFilePath = files[i];
|
|
const dtsFilePath = jsFilePath.replace(/.js$/, '.d.ts');
|
|
const dtsAbsolutePath = path.join(cwd, dtsFilePath);
|
|
if (!(fs.existsSync(dtsAbsolutePath) && fs.statSync(dtsAbsolutePath).isFile())) {
|
|
throw Error(`ERROR: lack ${dirName}/dist/${dtsFilePath} `);
|
|
}
|
|
}
|
|
console.log(`Check dts files of ${dirName} successfully!`);
|
|
}
|
|
|
|
function buildPackage(dirName: string) {
|
|
const pattern = '**/*.ts';
|
|
const cwd = joinPackagePath(dirName, 'src');
|
|
const files = glob.sync(pattern, { cwd });
|
|
|
|
const targetFiles = files.map((file) => {
|
|
return joinPackagePath(dirName, 'src', file);
|
|
});
|
|
|
|
// build ts -> esm
|
|
{
|
|
// const tsConfig = getTsConfig();
|
|
// const compilerOptions = tsConfig.compilerOptions;
|
|
const compilerOptions: TsMorphCompilerOptions = {
|
|
noUnusedLocals: true,
|
|
declaration: true,
|
|
sourceMap: false,
|
|
target: ScriptTarget.ES2015,
|
|
moduleResolution: ModuleResolutionKind.NodeJs,
|
|
allowJs: false,
|
|
strict: true,
|
|
experimentalDecorators: true,
|
|
resolveJsonModule: true,
|
|
esModuleInterop: true,
|
|
removeComments: true,
|
|
// lib: ['ES2016', 'dom'],
|
|
outDir: joinPackagePath(dirName, 'dist', 'esm'),
|
|
rootDir: joinPackagePath(dirName, 'src'),
|
|
skipLibCheck: true,
|
|
};
|
|
const project = new Project({
|
|
compilerOptions,
|
|
// tsConfigFilePath: joinProjectPath('tsconfig.web.json')
|
|
});
|
|
|
|
const program = ts.createProgram({
|
|
rootNames: targetFiles,
|
|
options: compilerOptions as CompilerOptions,
|
|
});
|
|
|
|
// const diagnostics = ts.getPreEmitDiagnostics(program);
|
|
// if (diagnostics.length) {
|
|
// console.error(diagnostics);
|
|
// for (const diagnostic of diagnostics) {
|
|
// console.log(JSON.stringify(diagnostic.messageText, null, 2));
|
|
// }
|
|
// throw Error('TS build error!');
|
|
// }
|
|
const diagnostics = project.getPreEmitDiagnostics();
|
|
if (diagnostics.length > 0) {
|
|
console.error(project.formatDiagnosticsWithColorAndContext(diagnostics));
|
|
throw Error('TS build error!');
|
|
}
|
|
|
|
program.emit();
|
|
}
|
|
}
|