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
@ -1942,4 +1942,157 @@ describe('ngc transformer command-line', () => {
|
||||
expect(emittedFile('hello-world.js')).toContain('ngComponentDef');
|
||||
});
|
||||
});
|
||||
|
||||
describe('tree shakeable services', () => {
|
||||
|
||||
function compileService(source: string): string {
|
||||
write('service.ts', source);
|
||||
|
||||
const exitCode = main(['-p', path.join(basePath, 'tsconfig.json')], errorSpy);
|
||||
expect(exitCode).toEqual(0);
|
||||
|
||||
const servicePath = path.resolve(outDir, 'service.js');
|
||||
return fs.readFileSync(servicePath, 'utf8');
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
writeConfig(`{
|
||||
"extends": "./tsconfig-base.json",
|
||||
"files": ["service.ts"]
|
||||
}`);
|
||||
write('module.ts', `
|
||||
import {NgModule} from '@angular/core';
|
||||
|
||||
@NgModule({})
|
||||
export class Module {}
|
||||
`);
|
||||
});
|
||||
|
||||
describe(`doesn't break existing injectables`, () => {
|
||||
it('on simple services', () => {
|
||||
const source = compileService(`
|
||||
import {Injectable, NgModule} from '@angular/core';
|
||||
|
||||
@Injectable()
|
||||
export class Service {
|
||||
constructor(public param: string) {}
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
providers: [{provide: Service, useValue: new Service('test')}],
|
||||
})
|
||||
export class ServiceModule {}
|
||||
`);
|
||||
expect(source).not.toMatch(/ngInjectableDef/);
|
||||
});
|
||||
it('on a service with a base class service', () => {
|
||||
const source = compileService(`
|
||||
import {Injectable, NgModule} from '@angular/core';
|
||||
|
||||
@Injectable()
|
||||
export class Dep {}
|
||||
|
||||
export class Base {
|
||||
constructor(private dep: Dep) {}
|
||||
}
|
||||
@Injectable()
|
||||
export class Service extends Base {}
|
||||
|
||||
@NgModule({
|
||||
providers: [Service],
|
||||
})
|
||||
export class ServiceModule {}
|
||||
`);
|
||||
expect(source).not.toMatch(/ngInjectableDef/);
|
||||
});
|
||||
});
|
||||
|
||||
it('compiles a basic InjectableDef', () => {
|
||||
const source = compileService(`
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Module} from './module';
|
||||
|
||||
@Injectable({
|
||||
scope: Module,
|
||||
})
|
||||
export class Service {}
|
||||
`);
|
||||
expect(source).toMatch(/ngInjectableDef = .+\.defineInjectable\(/);
|
||||
expect(source).toMatch(/ngInjectableDef.*token: Service/);
|
||||
expect(source).toMatch(/ngInjectableDef.*scope: .+\.Module/);
|
||||
});
|
||||
|
||||
it('compiles a useValue InjectableDef', () => {
|
||||
const source = compileService(`
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Module} from './module';
|
||||
|
||||
export const CONST_SERVICE: Service = null;
|
||||
|
||||
@Injectable({
|
||||
scope: Module,
|
||||
useValue: CONST_SERVICE
|
||||
})
|
||||
export class Service {}
|
||||
`);
|
||||
expect(source).toMatch(/ngInjectableDef.*return CONST_SERVICE/);
|
||||
});
|
||||
|
||||
it('compiles a useExisting InjectableDef', () => {
|
||||
const source = compileService(`
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Module} from './module';
|
||||
|
||||
@Injectable()
|
||||
export class Existing {}
|
||||
|
||||
@Injectable({
|
||||
scope: Module,
|
||||
useExisting: Existing,
|
||||
})
|
||||
export class Service {}
|
||||
`);
|
||||
expect(source).toMatch(/ngInjectableDef.*return ..\.inject\(Existing\)/);
|
||||
});
|
||||
|
||||
it('compiles a useFactory InjectableDef with optional dep', () => {
|
||||
const source = compileService(`
|
||||
import {Injectable, Optional} from '@angular/core';
|
||||
import {Module} from './module';
|
||||
|
||||
@Injectable()
|
||||
export class Existing {}
|
||||
|
||||
@Injectable({
|
||||
scope: Module,
|
||||
useFactory: (existing: Existing|null) => new Service(existing),
|
||||
deps: [[new Optional(), Existing]],
|
||||
})
|
||||
export class Service {
|
||||
constructor(e: Existing|null) {}
|
||||
}
|
||||
`);
|
||||
expect(source).toMatch(/ngInjectableDef.*return ..\(..\.inject\(Existing, null, 0\)/);
|
||||
});
|
||||
|
||||
it('compiles a useFactory InjectableDef with skip-self dep', () => {
|
||||
const source = compileService(`
|
||||
import {Injectable, SkipSelf} from '@angular/core';
|
||||
import {Module} from './module';
|
||||
|
||||
@Injectable()
|
||||
export class Existing {}
|
||||
|
||||
@Injectable({
|
||||
scope: Module,
|
||||
useFactory: (existing: Existing) => new Service(existing),
|
||||
deps: [[new SkipSelf(), Existing]],
|
||||
})
|
||||
export class Service {
|
||||
constructor(e: Existing) {}
|
||||
}
|
||||
`);
|
||||
expect(source).toMatch(/ngInjectableDef.*return ..\(..\.inject\(Existing, undefined, 1\)/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user