feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking of services which are never injected. Ordinarily, this tree-shaking is prevented by the existence of a hard dependency on the service by the module in which it is declared. Firstly, @Injectable() is modified to accept a 'scope' parameter, which points to an @NgModule(). This reverses the dependency edge, permitting the module to not depend on the service which it "provides". Secondly, the runtime is modified to understand the new relationship created above. When a module receives a request to inject a token, and cannot find that token in its list of providers, it will then look at the token for a special ngInjectableDef field which indicates which module the token is scoped to. If that module happens to be in the injector, it will behave as if the token itself was in the injector to begin with. Thirdly, the compiler is modified to read the @Injectable() metadata and to generate the special ngInjectableDef field as part of TS compilation, using the PartialModules system. Additionally, this commit adds several unit and integration tests of various flavors to test this change. PR Close #22005
This commit is contained in:

committed by
Miško Hevery

parent
2d5e7d1b52
commit
235a235fab
@ -7,7 +7,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {AotCompiler, AotCompilerHost, AotCompilerOptions, EmitterVisitorContext, FormattedMessageChain, GeneratedFile, MessageBundle, NgAnalyzedFile, NgAnalyzedModules, ParseSourceSpan, PartialModule, Position, Serializer, TypeScriptEmitter, Xliff, Xliff2, Xmb, core, createAotCompiler, getParseErrors, isFormattedError, isSyntaxError} from '@angular/compiler';
|
||||
import {AotCompiler, AotCompilerHost, AotCompilerOptions, EmitterVisitorContext, FormattedMessageChain, GeneratedFile, MessageBundle, NgAnalyzedFile, NgAnalyzedFileWithInjectables, NgAnalyzedModules, ParseSourceSpan, PartialModule, Position, Serializer, TypeScriptEmitter, Xliff, Xliff2, Xmb, core, createAotCompiler, getParseErrors, isFormattedError, isSyntaxError} from '@angular/compiler';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as ts from 'typescript';
|
||||
@ -22,7 +22,7 @@ import {MetadataCache, MetadataTransformer} from './metadata_cache';
|
||||
import {getAngularEmitterTransformFactory} from './node_emitter_transform';
|
||||
import {PartialModuleMetadataTransformer} from './r3_metadata_transform';
|
||||
import {getAngularClassTransformerFactory} from './r3_transform';
|
||||
import {GENERATED_FILES, StructureIsReused, createMessageDiagnostic, isInRootDir, ngToTsDiagnostic, tsStructureIsReused, userError} from './util';
|
||||
import {DTS, GENERATED_FILES, StructureIsReused, TS, createMessageDiagnostic, isInRootDir, ngToTsDiagnostic, tsStructureIsReused, userError} from './util';
|
||||
|
||||
|
||||
|
||||
@ -62,6 +62,7 @@ class AngularCompilerProgram implements Program {
|
||||
private _hostAdapter: TsCompilerAotCompilerTypeCheckHostAdapter;
|
||||
private _tsProgram: ts.Program;
|
||||
private _analyzedModules: NgAnalyzedModules|undefined;
|
||||
private _analyzedInjectables: NgAnalyzedFileWithInjectables[]|undefined;
|
||||
private _structuralDiagnostics: Diagnostic[]|undefined;
|
||||
private _programWithStubs: ts.Program|undefined;
|
||||
private _optionsDiagnostics: Diagnostic[] = [];
|
||||
@ -191,13 +192,15 @@ class AngularCompilerProgram implements Program {
|
||||
}
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
const {tmpProgram, sourceFiles, rootNames} = this._createProgramWithBasicStubs();
|
||||
return this.compiler.loadFilesAsync(sourceFiles).then(analyzedModules => {
|
||||
if (this._analyzedModules) {
|
||||
throw new Error('Angular structure loaded both synchronously and asynchronously');
|
||||
}
|
||||
this._updateProgramWithTypeCheckStubs(tmpProgram, analyzedModules, rootNames);
|
||||
});
|
||||
const {tmpProgram, sourceFiles, tsFiles, rootNames} = this._createProgramWithBasicStubs();
|
||||
return this.compiler.loadFilesAsync(sourceFiles, tsFiles)
|
||||
.then(({analyzedModules, analyzedInjectables}) => {
|
||||
if (this._analyzedModules) {
|
||||
throw new Error('Angular structure loaded both synchronously and asynchronously');
|
||||
}
|
||||
this._updateProgramWithTypeCheckStubs(
|
||||
tmpProgram, analyzedModules, analyzedInjectables, rootNames);
|
||||
});
|
||||
})
|
||||
.catch(e => this._createProgramOnError(e));
|
||||
}
|
||||
@ -304,8 +307,12 @@ class AngularCompilerProgram implements Program {
|
||||
}
|
||||
this.writeFile(outFileName, outData, writeByteOrderMark, onError, genFile, sourceFiles);
|
||||
};
|
||||
const tsCustomTransformers = this.calculateTransforms(
|
||||
genFileByFileName, /* partialModules */ undefined, customTransformers);
|
||||
|
||||
const modules = this._analyzedInjectables &&
|
||||
this.compiler.emitAllPartialModules2(this._analyzedInjectables);
|
||||
|
||||
const tsCustomTransformers =
|
||||
this.calculateTransforms(genFileByFileName, modules, customTransformers);
|
||||
const emitOnlyDtsFiles = (emitFlags & (EmitFlags.DTS | EmitFlags.JS)) == EmitFlags.DTS;
|
||||
// Restore the original references before we emit so TypeScript doesn't emit
|
||||
// a reference to the .d.ts file.
|
||||
@ -491,9 +498,11 @@ class AngularCompilerProgram implements Program {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const {tmpProgram, sourceFiles, rootNames} = this._createProgramWithBasicStubs();
|
||||
const analyzedModules = this.compiler.loadFilesSync(sourceFiles);
|
||||
this._updateProgramWithTypeCheckStubs(tmpProgram, analyzedModules, rootNames);
|
||||
const {tmpProgram, sourceFiles, tsFiles, rootNames} = this._createProgramWithBasicStubs();
|
||||
const {analyzedModules, analyzedInjectables} =
|
||||
this.compiler.loadFilesSync(sourceFiles, tsFiles);
|
||||
this._updateProgramWithTypeCheckStubs(
|
||||
tmpProgram, analyzedModules, analyzedInjectables, rootNames);
|
||||
} catch (e) {
|
||||
this._createProgramOnError(e);
|
||||
}
|
||||
@ -520,6 +529,7 @@ class AngularCompilerProgram implements Program {
|
||||
tmpProgram: ts.Program,
|
||||
rootNames: string[],
|
||||
sourceFiles: string[],
|
||||
tsFiles: string[],
|
||||
} {
|
||||
if (this._analyzedModules) {
|
||||
throw new Error(`Internal Error: already initialized!`);
|
||||
@ -553,17 +563,23 @@ class AngularCompilerProgram implements Program {
|
||||
|
||||
const tmpProgram = ts.createProgram(rootNames, this.options, this.hostAdapter, oldTsProgram);
|
||||
const sourceFiles: string[] = [];
|
||||
const tsFiles: string[] = [];
|
||||
tmpProgram.getSourceFiles().forEach(sf => {
|
||||
if (this.hostAdapter.isSourceFile(sf.fileName)) {
|
||||
sourceFiles.push(sf.fileName);
|
||||
}
|
||||
if (TS.test(sf.fileName) && !DTS.test(sf.fileName)) {
|
||||
tsFiles.push(sf.fileName);
|
||||
}
|
||||
});
|
||||
return {tmpProgram, sourceFiles, rootNames};
|
||||
return {tmpProgram, sourceFiles, tsFiles, rootNames};
|
||||
}
|
||||
|
||||
private _updateProgramWithTypeCheckStubs(
|
||||
tmpProgram: ts.Program, analyzedModules: NgAnalyzedModules, rootNames: string[]) {
|
||||
tmpProgram: ts.Program, analyzedModules: NgAnalyzedModules,
|
||||
analyzedInjectables: NgAnalyzedFileWithInjectables[], rootNames: string[]) {
|
||||
this._analyzedModules = analyzedModules;
|
||||
this._analyzedInjectables = analyzedInjectables;
|
||||
tmpProgram.getSourceFiles().forEach(sf => {
|
||||
if (sf.fileName.endsWith('.ngfactory.ts')) {
|
||||
const {generate, baseFileName} = this.hostAdapter.shouldGenerateFile(sf.fileName);
|
||||
|
@ -26,7 +26,7 @@ export function getAngularClassTransformerFactory(modules: PartialModule[]): Tra
|
||||
return function(context: ts.TransformationContext) {
|
||||
return function(sourceFile: ts.SourceFile): ts.SourceFile {
|
||||
const module = moduleMap.get(sourceFile.fileName);
|
||||
if (module) {
|
||||
if (module && module.statements.length > 0) {
|
||||
const [newSourceFile] = updateSourceFile(sourceFile, module, context);
|
||||
return newSourceFile;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import {CompilerOptions, DEFAULT_ERROR_CODE, Diagnostic, SOURCE} from './api';
|
||||
|
||||
export const GENERATED_FILES = /(.*?)\.(ngfactory|shim\.ngstyle|ngstyle|ngsummary)\.(js|d\.ts|ts)$/;
|
||||
export const DTS = /\.d\.ts$/;
|
||||
export const TS = /^(?!.*\.d\.ts$).*\.ts$/;
|
||||
|
||||
export const enum StructureIsReused {Not = 0, SafeModules = 1, Completely = 2}
|
||||
|
||||
|
Reference in New Issue
Block a user