Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_createOrUpdateProgram() {
if (this._JitMode) {
// Create the TypeScript program.
benchmark_1.time('TypeChecker._createOrUpdateProgram.ts.createProgram');
this._program = ts.createProgram(this._rootNames, this._compilerOptions, this._compilerHost, this._program);
benchmark_1.timeEnd('TypeChecker._createOrUpdateProgram.ts.createProgram');
}
else {
benchmark_1.time('TypeChecker._createOrUpdateProgram.ng.createProgram');
// Create the Angular program.
this._program = compiler_cli_1.createProgram({
rootNames: this._rootNames,
options: this._compilerOptions,
host: this._compilerHost,
oldProgram: this._program,
});
benchmark_1.timeEnd('TypeChecker._createOrUpdateProgram.ng.createProgram');
}
}
_diagnose(cancellationToken) {
private _listLazyRoutesFromProgram(): LazyRouteMap {
let entryRoute: string | undefined;
let ngProgram: Program;
if (this._JitMode) {
if (!this.entryModule) {
return {};
}
time('AngularCompilerPlugin._listLazyRoutesFromProgram.createProgram');
ngProgram = createProgram({
rootNames: this._rootNames,
options: { ...this._compilerOptions, genDir: '', collectAllErrors: true },
host: this._compilerHost,
});
timeEnd('AngularCompilerPlugin._listLazyRoutesFromProgram.createProgram');
entryRoute = workaroundResolve(this.entryModule.path) + '#' + this.entryModule.className;
} else {
ngProgram = this._program as Program;
}
time('AngularCompilerPlugin._listLazyRoutesFromProgram.listLazyRoutes');
// entryRoute will only be defined in JIT.
// In AOT all routes within the program are returned.
const lazyRoutes = ngProgram.listLazyRoutes(entryRoute);
timeEnd('AngularCompilerPlugin._listLazyRoutesFromProgram.listLazyRoutes');
private _createOrUpdateProgram() {
if (this._JitMode) {
// Create the TypeScript program.
time('TypeChecker._createOrUpdateProgram.ts.createProgram');
this._program = ts.createProgram(
this._rootNames,
this._compilerOptions,
this._compilerHost,
this._program as ts.Program,
) as ts.Program;
timeEnd('TypeChecker._createOrUpdateProgram.ts.createProgram');
} else {
time('TypeChecker._createOrUpdateProgram.ng.createProgram');
// Create the Angular program.
this._program = createProgram({
rootNames: this._rootNames,
options: this._compilerOptions,
host: this._compilerHost,
oldProgram: this._program as Program,
}) as Program;
timeEnd('TypeChecker._createOrUpdateProgram.ng.createProgram');
}
}
const transformSources = (
tsConfig: TsConfig,
transformers: ts.TransformerFactory[]
): ts.TransformationResult => {
const compilerHost: ng.CompilerHost = ng.createCompilerHost({
options: tsConfig.options
});
const program: ng.Program = ng.createProgram({
rootNames: [...tsConfig.rootNames],
options: tsConfig.options,
host: compilerHost
});
const sourceFiles = program.getTsProgram().getSourceFiles();
const transformationResult: ts.TransformationResult = ts.transform(
// XX: circumvent tsc compile error in 2.6
Array.from((sourceFiles as any) as ts.SourceFile[]),
transformers,
tsConfig.options
);
return transformationResult;
};
setup() {
const {rootNames, options} = readConfiguration(this.projectPath);
// https://github.com/angular/angular/commit/ec4381dd401f03bded652665b047b6b90f2b425f made Ivy
// the default. This breaks the assumption that "createProgram" from compiler-cli returns the
// NGC program. In order to ensure that the migration runs properly, we set "enableIvy" to
// false.
options.enableIvy = false;
const aotProgram = createProgram({rootNames, options, host: this.host});
// The "AngularCompilerProgram" does not expose the "AotCompiler" instance, nor does it
// expose the logic that is necessary to analyze the determined modules. We work around
// this by just accessing the necessary private properties using the bracket notation.
this.compiler = (aotProgram as any)['compiler'];
this.metadataResolver = this.compiler !['_metadataResolver'];
// Modify the "DirectiveNormalizer" to not normalize any referenced external stylesheets.
// This is necessary because in CLI projects preprocessor files are commonly referenced
// and we don't want to parse them in order to extract relative style references. This
// breaks the analysis of the project because we instantiate a standalone AOT compiler
// program which does not contain the custom logic by the Angular CLI Webpack compiler plugin.
const directiveNormalizer = this.metadataResolver !['_directiveNormalizer'];
directiveNormalizer['_normalizeStylesheet'] = function(metadata: CompileStylesheetMetadata) {
return new CompileStylesheetMetadata(
{styles: metadata.styles, styleUrls: [], moduleUrl: metadata.moduleUrl !});
// https://github.com/angular/angular/commit/ec4381dd401f03bded652665b047b6b90f2b425f made Ivy
// the default. This breaks the assumption that "createProgram" from compiler-cli returns the
// NGC program. In order to ensure that the migration runs properly, we set "enableIvy" to false.
options.enableIvy = false;
const host = createHost(options);
// For this migration, we never need to read resources and can just return
// an empty string for requested resources. We need to handle requested resources
// because our created NGC compiler program does not know about special resolutions
// which are set up by the Angular CLI. i.e. resolving stylesheets through "tilde".
host.readResource = () => '';
host.resourceNameToFileName = () => '$fake-file$';
const ngcProgram = createProgram({rootNames, options, host});
// The "AngularCompilerProgram" does not expose the "AotCompiler" instance, nor does it
// expose the logic that is necessary to analyze the determined modules. We work around
// this by just accessing the necessary private properties using the bracket notation.
const compiler: AotCompiler = (ngcProgram as any)['compiler'];
const program = ngcProgram.getTsProgram();
return {host, ngcProgram, program, compiler};
}
.then( () => {
if (!host) {
host = createCompilerHost({options});
}
program = createProgram({rootNames, host, options, oldProgram});
return program.loadNgStructureAsync()
})
.then( () => {
export function getProgram(rootNames: string[] = createSourceFiles()) {
const options: ngc.CompilerOptions = clone(COMPILER_OPTIONS);
options.basePath = ROOT;
options.moduleResolution = ts.ModuleResolutionKind.NodeJs;
options.module = ts.ModuleKind.ES2015;
options.target = ts.ScriptTarget.ES5;
options.lib = ['dom', 'es2017'];
options.inlineSourceMap = true;
options.inlineSources = true;
delete options.baseUrl;
const host: ngc.CompilerHost = ngc.createCompilerHost({ options });
return ngc.createProgram({
rootNames,
options,
host
});
}