angular/packages/platform-browser-dynamic/src/compiler_factory.ts
Matthieu Riegler 40113f653c feat(core): Remove deprecated CompilerOptions.useJit andCompilerOptions.missingTranslation. (#49672)
Both properties have been deprecated in v13 and are unused in v16.

PR Close #49672
2023-10-10 09:35:55 -07:00

76 lines
2.3 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {CompilerConfig} from '@angular/compiler';
import {Compiler, CompilerFactory, CompilerOptions, Injector, StaticProvider, ViewEncapsulation} from '@angular/core';
export const COMPILER_PROVIDERS =
<StaticProvider[]>[{provide: Compiler, useFactory: () => new Compiler()}];
/**
* @publicApi
*
* @deprecated
* Ivy JIT mode doesn't require accessing this symbol.
* See [JIT API changes due to ViewEngine deprecation](guide/deprecations#jit-api-changes) for
* additional context.
*/
export class JitCompilerFactory implements CompilerFactory {
private _defaultOptions: CompilerOptions[];
/** @internal */
constructor(defaultOptions: CompilerOptions[]) {
const compilerOptions: CompilerOptions = {
defaultEncapsulation: ViewEncapsulation.Emulated,
};
this._defaultOptions = [compilerOptions, ...defaultOptions];
}
createCompiler(options: CompilerOptions[] = []): Compiler {
const opts = _mergeOptions(this._defaultOptions.concat(options));
const injector = Injector.create({
providers: [
COMPILER_PROVIDERS, {
provide: CompilerConfig,
useFactory: () => {
return new CompilerConfig({
defaultEncapsulation: opts.defaultEncapsulation,
preserveWhitespaces: opts.preserveWhitespaces,
});
},
deps: []
},
opts.providers!
]
});
return injector.get(Compiler);
}
}
function _mergeOptions(optionsArr: CompilerOptions[]): CompilerOptions {
return {
defaultEncapsulation: _lastDefined(optionsArr.map(options => options.defaultEncapsulation)),
providers: _mergeArrays(optionsArr.map(options => options.providers!)),
preserveWhitespaces: _lastDefined(optionsArr.map(options => options.preserveWhitespaces)),
};
}
function _lastDefined<T>(args: T[]): T|undefined {
for (let i = args.length - 1; i >= 0; i--) {
if (args[i] !== undefined) {
return args[i];
}
}
return undefined;
}
function _mergeArrays(parts: any[][]): any[] {
const result: any[] = [];
parts.forEach((part) => part && result.push(...part));
return result;
}