vite-esbuild-typescript-checker
Version:
* Speeds up [TypeScript](https://github.com/Microsoft/TypeScript) type checking * Supports [Vue Single File Component](https://vuejs.org/v2/guide/single-file-components.html) * Displays nice error messages with the [code frame](https://babeljs.io/docs/en/
71 lines (70 loc) • 2.86 kB
JavaScript
import { isMainThread, parentPort, workerData } from 'worker_threads';
import path from 'path';
import { initVueExtension } from './vue/type-script-vue-extension.js';
import { parseConfig } from './config.js';
import { useSolutionBuilder } from './program/solution-builder.js';
import { useWatchProgram } from './program/watch-program.js';
import pc from 'picocolors';
import moment from 'moment';
import { getSystem } from './system.js';
function createCompilerHost(options, port, config) {
//console.log(config);
if (workerData.watch) {
useWatchProgram(port);
} else {
// useWatchProgram(port)
useSolutionBuilder(config, port);
}
const system = getSystem();
port.on('message', (message)=>{
switch(message.type){
case 'kill':
process.exit();
break;
case 'changeFiles':
system.invalidateCache();
message.data.changedFiles?.forEach((changedFile)=>{
system?.invokeFileChanged(changedFile);
});
message.data.deletedFiles?.forEach((deletedFile)=>{
system?.invokeFileDeleted(deletedFile);
});
message.data.changedFiles?.map((file)=>{
port.postMessage({
type: 'info',
data: pc.magenta(`[${moment().format('Y-MM-DD H:mm:ss')}] File changed: ${file}`)
});
});
message.data.deletedFiles?.map((file)=>{
port.postMessage({
type: 'info',
data: pc.cyan(`[${moment().format('Y-MM-DD H:mm:ss')}] File delete: ${file}`)
});
});
break;
}
});
}
export let workerOptions;
function startThread(options) {
console.log(pc.magenta(`[${moment().format('Y-MM-DD H:mm:ss')}] Checker Thread started`));
const basedir = options.basedir ?? __dirname;
const configFile = options.configFile ? path.isAbsolute(options.configFile) ? options.configFile : path.resolve(basedir, options.configFile) : './tsconfig.json';
workerOptions = {
buildMode: options.buildMode ?? 'readonly',
basedir: basedir,
configFile: configFile,
...options
};
// console.log('workerData:', workerData);
initVueExtension(workerOptions.vue);
parseConfig(configFile, workerOptions);
return createCompilerHost(workerOptions, parentPort, path.resolve(workerOptions.basedir, workerOptions.configFile));
}
if (!isMainThread && parentPort) {
const workerOptions = workerData;
if (!workerOptions || !workerOptions.basedir) {
throw new Error(`Error spawn new thread with data "${JSON.stringify(workerData)}"`);
}
startThread(workerData);
}