idraw/scripts/build-module.ts

102 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2022-12-31 15:21:57 +00:00
import ts from 'typescript';
2025-09-06 05:39:47 +00:00
import type { CompilerOptions } from 'typescript';
2023-02-25 03:27:41 +00:00
import { Project } from 'ts-morph';
2025-09-06 05:39:47 +00:00
import type { CompilerOptions as TsMorphCompilerOptions } from 'ts-morph';
2022-12-31 15:21:57 +00:00
import path from 'path';
2024-06-09 14:59:47 +00:00
import fs from 'fs';
2023-08-13 12:04:21 +00:00
import * as glob from 'glob';
2022-12-31 15:21:57 +00:00
import { packages } from './config';
2024-02-08 01:39:34 +00:00
import { joinPackagePath } from './util/project';
2023-02-12 03:39:24 +00:00
import { removeFullDir } from './util/file';
2022-12-31 15:21:57 +00:00
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/`);
2023-02-12 03:39:24 +00:00
removeFullDir(`${pkgDir}/dist`);
2022-12-31 15:21:57 +00:00
buildPackage(dirName);
2024-06-09 14:59:47 +00:00
checkPackageDts(dirName);
2022-12-31 15:21:57 +00:00
console.log(`Build ESM of ${dirName} successfully!`);
}
}
2024-06-09 14:59:47 +00:00
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!`);
}
2023-02-12 03:39:24 +00:00
function buildPackage(dirName: string) {
2022-12-31 15:21:57 +00:00
const pattern = '**/*.ts';
2022-12-31 15:29:45 +00:00
const cwd = joinPackagePath(dirName, 'src');
2022-12-31 15:21:57 +00:00
const files = glob.sync(pattern, { cwd });
const targetFiles = files.map((file) => {
2022-12-31 15:29:45 +00:00
return joinPackagePath(dirName, 'src', file);
2022-12-31 15:21:57 +00:00
});
// build ts -> esm
{
2023-02-12 03:39:24 +00:00
// const tsConfig = getTsConfig();
// const compilerOptions = tsConfig.compilerOptions;
2025-09-06 05:39:47 +00:00
const compilerOptions: TsMorphCompilerOptions = {
2023-02-12 03:39:24 +00:00
noUnusedLocals: true,
declaration: true,
sourceMap: false,
target: ts.ScriptTarget.ES2015,
moduleResolution: ts.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
};
2023-02-25 03:27:41 +00:00
const project = new Project({
compilerOptions
// tsConfigFilePath: joinProjectPath('tsconfig.web.json')
});
2025-09-06 05:16:34 +00:00
const program = ts.createProgram({
2025-09-06 05:39:47 +00:00
rootNames: targetFiles,
options: compilerOptions as CompilerOptions
2025-09-06 05:16:34 +00:00
});
2023-02-12 03:39:24 +00:00
2023-02-25 03:27:41 +00:00
// 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));
2023-02-12 03:39:24 +00:00
throw Error('TS build error!');
}
2022-12-31 15:21:57 +00:00
program.emit();
}
}