Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/common/typescript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function onHostEvent<F extends string, T extends {[key in F]?: (...args:
host: T,
functionName: F,
before?: (...args: Parameters<NonNullable<T[F]>>) => void,
after?: (res: ReturnType<NonNullable<T[F]>>) => void,
after?: (res: ReturnType<NonNullable<T[F]>>, ...args: Parameters<NonNullable<T[F]>>) => void,
) {
const originalFunction = host[functionName];

Expand All @@ -100,7 +100,7 @@ export function onHostEvent<F extends string, T extends {[key in F]?: (...args:
}

if (after) {
after(result);
after(result, ...args);
}
return result;
}) as T[F];
Expand Down
90 changes: 54 additions & 36 deletions src/common/typescript/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import {createTransformPathsToLocalModules} from './transformers';
import {displayFilename, getTsProjectConfigPath, onHostEvent} from './utils';
import {formatDiagnosticBrief} from './diagnostic';

/** @see https://github.com/microsoft/TypeScript/blob/9059e5bda0bb603ae6b41eca09dcd2a071af45fd/src/compiler/diagnosticMessages.json#L5400-L5403 */
const COMPILATION_COMPLETE_WITH_ERROR = 6193;

/** @see https://github.com/microsoft/TypeScript/blob/9059e5bda0bb603ae6b41eca09dcd2a071af45fd/src/compiler/diagnosticMessages.json#L5404-L5407 */
const COMPILATION_COMPLETE_WITH_N_ERRORS = 6194;

export function watch(
ts: typeof Typescript,
projectPath: string,
Expand All @@ -19,58 +25,63 @@ export function watch(

const createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;

const host = ts.createWatchCompilerHost(
configPath,
{
noEmit: false,
noEmitOnError: false,
inlineSourceMap: enableSourceMap,
inlineSources: enableSourceMap,
...(enableSourceMap ? {sourceMap: false} : undefined),
},
const host = ts.createSolutionBuilderWithWatchHost(
ts.sys,
createProgram,
reportDiagnostic,
reportDiagnostic,
reportWatchStatusChanged,
);

host.readFile = displayFilename(host.readFile, 'Reading', logger);

onHostEvent(
host,
'createProgram',
() => {
(_rootnames, _options, host) => {
logger.verbose("We're about to create the program");
// @ts-expect-error
host.readFile.enableDisplay();
},
() => {
// @ts-expect-error
const count = host.readFile.disableDisplay();
logger.verbose(`Program created, read ${count} files`);
},
);

onHostEvent(
host,
'afterProgramCreate',
(program) => {
logger.verbose('We finished making the program! Emitting...');
const transformPathsToLocalModules = createTransformPathsToLocalModules(ts);
program.emit(undefined, undefined, undefined, undefined, {
after: [transformPathsToLocalModules],
afterDeclarations: [transformPathsToLocalModules],
});
logger.verbose('Emit completed!');
if (host) {
host.readFile = displayFilename(host.readFile, 'Reading', logger);

// @ts-expect-error
host.readFile.enableDisplay();
}
},
() => {
onAfterFilesEmitted?.();
(_result, _rootnames, _options, host) => {
if (host) {
// @ts-expect-error
const count = host.readFile.disableDisplay();

logger.verbose(`Program created, read ${count} files`);
}
},
);

// `createWatchProgram` creates an initial program, watches files, and updates
onHostEvent(host, 'afterProgramEmitAndDiagnostics', (program) => {
const project = program.getCompilerOptions().configFilePath;

logger.verbose(
typeof project === 'string'
? `Emit completed for ${project.replace(process.cwd(), '')}!`
: 'Emit completed!',
);
});

// `createSolutionBuilderWithWatch` creates an initial program, watches files, and updates
// the program over time.
ts.createWatchProgram(host);
const solutionBuilder = ts.createSolutionBuilderWithWatch(host, [configPath], {
noEmit: false,
noEmitOnError: false,
inlineSourceMap: enableSourceMap,
inlineSources: enableSourceMap,
...(enableSourceMap ? {sourceMap: false} : undefined),
});

const transformPathsToLocalModules = createTransformPathsToLocalModules(ts);

solutionBuilder.build(undefined, undefined, undefined, () => ({
after: [transformPathsToLocalModules],
afterDeclarations: [transformPathsToLocalModules],
}));

function reportDiagnostic(diagnostic: Typescript.Diagnostic) {
const formatHost = {
Expand All @@ -93,5 +104,12 @@ export function watch(
if (diagnostic.messageText) {
logger.message(ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine));
}

if (
diagnostic.code === COMPILATION_COMPLETE_WITH_ERROR ||
diagnostic.code === COMPILATION_COMPLETE_WITH_N_ERRORS
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of successful compilation it is n = 0 errors

) {
onAfterFilesEmitted?.();
}
}
}
Loading