Skip to content

Commit

Permalink
fix(vite-plugin-angular): add reporting of compilation warnings and e…
Browse files Browse the repository at this point in the history
…rrors (#902)
  • Loading branch information
brandonroberts authored Feb 26, 2024
1 parent 73698f5 commit 49f8518
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ interface EmitFileResult {
map?: string;
dependencies: readonly string[];
hash?: Uint8Array;
errors: string[];
warnings: string[];
}
type FileEmitter = (file: string) => Promise<EmitFileResult | undefined>;

Expand Down Expand Up @@ -303,6 +305,17 @@ export function angular(options?: PluginOptions): Plugin[] {

const typescriptResult = await fileEmitter?.(id);

if (
typescriptResult?.warnings &&
typescriptResult?.warnings.length > 0
) {
this.warn(`${typescriptResult.warnings.join('\n')}`);
}

if (typescriptResult?.errors && typescriptResult?.errors.length > 0) {
this.error(`${typescriptResult.errors.join('\n')}`);
}

// return fileEmitter
let data = typescriptResult?.content ?? '';

Expand Down Expand Up @@ -454,14 +467,19 @@ export function angular(options?: PluginOptions): Plugin[] {
compilerCli.readConfiguration(pluginOptions.tsconfig, {
suppressOutputPathCheck: true,
outDir: undefined,
sourceMap: false,
inlineSourceMap: !isProd,
inlineSources: !isProd,
declaration: false,
declarationMap: false,
allowEmptyCodegenFiles: false,
annotationsAs: 'decorators',
enableResourceInlining: false,
noEmitOnError: false,
mapRoot: undefined,
sourceRoot: undefined,
supportTestBed: false,
supportJitMode: false,
});

if (pluginOptions.supportAnalogFormat) {
Expand Down Expand Up @@ -498,7 +516,7 @@ export function angular(options?: PluginOptions): Plugin[] {
| ts.BuilderProgram
| ts.EmitAndSemanticDiagnosticsBuilderProgram;
let typeScriptProgram: ts.Program;
let angularCompiler: any;
let angularCompiler: NgtscProgram['compiler'];

if (!jit) {
// Create the Angular specific program that contains the Angular compiler
Expand Down Expand Up @@ -562,24 +580,38 @@ export function angular(options?: PluginOptions): Plugin[] {
afterDeclarations:
pluginOptions.advanced.tsTransformers.afterDeclarations,
},
jit ? {} : angularCompiler.prepareEmit().transformers
jit ? {} : angularCompiler!.prepareEmit().transformers
),
() => []
() => [],
angularCompiler!
);
}
}

export function createFileEmitter(
program: ts.BuilderProgram,
transformers: ts.CustomTransformers = {},
onAfterEmit?: (sourceFile: ts.SourceFile) => void
onAfterEmit?: (sourceFile: ts.SourceFile) => void,
angularCompiler?: NgtscProgram['compiler']
): FileEmitter {
return async (file: string) => {
const sourceFile = program.getSourceFile(file);
if (!sourceFile) {
return undefined;
}

const diagnostics = angularCompiler
? angularCompiler.getDiagnosticsForFile(sourceFile, 1)
: [];

const errors = diagnostics
.filter((d) => d.category === ts.DiagnosticCategory.Error)
.map((d) => d.messageText);

const warnings = diagnostics
.filter((d) => d.category === ts.DiagnosticCategory.Warning)
.map((d) => d.messageText);

let content: string | undefined;
program.emit(
sourceFile,
Expand All @@ -595,6 +627,6 @@ export function createFileEmitter(

onAfterEmit?.(sourceFile);

return { content, dependencies: [] };
return { content, dependencies: [], errors, warnings };
};
}

0 comments on commit 49f8518

Please sign in to comment.