Skip to content

Commit

Permalink
fix(compiler): emit preamble in generated files.
Browse files Browse the repository at this point in the history
  • Loading branch information
tbosch authored and matsko committed Sep 7, 2017
1 parent 5ef6e63 commit b1055a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 9 additions & 7 deletions packages/compiler-cli/src/transformers/node_emitter.ts
Expand Up @@ -24,19 +24,21 @@ export class TypeScriptNodeEmitter {
// stmts.
const statements: any[] = [].concat(
...stmts.map(stmt => stmt.visitStatement(converter, null)).filter(stmt => stmt != null));
const newSourceFile = ts.updateSourceFileNode(
sourceFile, [...converter.getReexports(), ...converter.getImports(), ...statements]);
const preambleStmts: ts.Statement[] = [];
if (preamble) {
if (preamble.startsWith('/*') && preamble.endsWith('*/')) {
preamble = preamble.substr(2, preamble.length - 4);
}
if (!statements.length) {
statements.push(ts.createEmptyStatement());
}
statements[0] = ts.setSyntheticLeadingComments(
statements[0],
const commentStmt = ts.createNotEmittedStatement(sourceFile);
ts.setSyntheticLeadingComments(
commentStmt,
[{kind: ts.SyntaxKind.MultiLineCommentTrivia, text: preamble, pos: -1, end: -1}]);
ts.setEmitFlags(commentStmt, ts.EmitFlags.CustomPrologue);
preambleStmts.push(commentStmt);
}
const newSourceFile = ts.updateSourceFileNode(
sourceFile,
[...preambleStmts, ...converter.getReexports(), ...converter.getImports(), ...statements]);
return [newSourceFile, converter.getNodeMap()];
}
}
Expand Down
11 changes: 9 additions & 2 deletions packages/compiler-cli/src/transformers/node_emitter_transform.ts
Expand Up @@ -11,6 +11,13 @@ import * as ts from 'typescript';

import {TypeScriptNodeEmitter} from './node_emitter';

const PREAMBLE = `/**
* @fileoverview This file is generated by the Angular template compiler.
* Do not edit.
* @suppress {suspiciousCode,uselessCode,missingProperties,missingOverride}
* tslint:disable
*/`;

export function getAngularEmitterTransformFactory(generatedFiles: GeneratedFile[]): () =>
(sourceFile: ts.SourceFile) => ts.SourceFile {
return function() {
Expand All @@ -20,10 +27,10 @@ export function getAngularEmitterTransformFactory(generatedFiles: GeneratedFile[
return function(sourceFile: ts.SourceFile): ts.SourceFile {
const g = map.get(sourceFile.fileName);
if (g && g.stmts) {
const [newSourceFile] = emitter.updateSourceFile(sourceFile, g.stmts);
const [newSourceFile] = emitter.updateSourceFile(sourceFile, g.stmts, PREAMBLE);
return newSourceFile;
}
return sourceFile;
};
};
}
}

0 comments on commit b1055a5

Please sign in to comment.