Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ivy): @NgModule -> ngInjectorDef compilation
This adds compilation of @NgModule providers and imports into ngInjectorDef statements in generated code. All @NgModule annotations will be compiled and the @NgModule decorators removed from the resultant js output. All @Injectables will also be compiled in Ivy mode, and the decorator removed.
- Loading branch information
Showing
32 changed files
with
1,276 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
"master": { | ||
"uncompressed": { | ||
"inline": 1447, | ||
"main": 155112, | ||
"main": 157654, | ||
"polyfills": 59179 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/compiler-cli/integrationtest/bazel/injector_def/ivy_build/app/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tools:defaults.bzl", "ivy_ng_module", "ts_library") | ||
load("//packages/bazel/src:ng_rollup_bundle.bzl", "ng_rollup_bundle") | ||
|
||
ivy_ng_module( | ||
name = "app", | ||
srcs = glob( | ||
[ | ||
"src/**/*.ts", | ||
], | ||
), | ||
module_name = "app_built", | ||
deps = [ | ||
"//packages/core", | ||
"@rxjs", | ||
], | ||
) |
40 changes: 40 additions & 0 deletions
40
packages/compiler-cli/integrationtest/bazel/injector_def/ivy_build/app/src/module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 {Injectable, InjectionToken, NgModule} from '@angular/core'; | ||
|
||
export const AOT_TOKEN = new InjectionToken<string>('TOKEN'); | ||
|
||
@Injectable() | ||
export class AotService { | ||
} | ||
|
||
@NgModule({ | ||
providers: [AotService], | ||
}) | ||
export class AotServiceModule { | ||
} | ||
|
||
@NgModule({ | ||
providers: [{provide: AOT_TOKEN, useValue: 'imports'}], | ||
}) | ||
export class AotImportedModule { | ||
} | ||
|
||
@NgModule({ | ||
providers: [{provide: AOT_TOKEN, useValue: 'exports'}], | ||
}) | ||
export class AotExportedModule { | ||
} | ||
|
||
@NgModule({ | ||
imports: [AotServiceModule, AotImportedModule], | ||
exports: [AotExportedModule], | ||
}) | ||
export class AotModule { | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/compiler-cli/integrationtest/bazel/injector_def/ivy_build/app/test/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("//tools:defaults.bzl", "ts_library") | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") | ||
|
||
ts_library( | ||
name = "test_lib", | ||
testonly = 1, | ||
srcs = glob( | ||
[ | ||
"**/*.ts", | ||
], | ||
), | ||
deps = [ | ||
"//packages/compiler-cli/integrationtest/bazel/injector_def/ivy_build/app", | ||
"//packages/core", | ||
], | ||
) | ||
|
||
jasmine_node_test( | ||
name = "test", | ||
bootstrap = ["angular/tools/testing/init_node_spec.js"], | ||
deps = [ | ||
":test_lib", | ||
"//tools/testing:node", | ||
], | ||
) |
87 changes: 87 additions & 0 deletions
87
packages/compiler-cli/integrationtest/bazel/injector_def/ivy_build/app/test/module_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 {Injectable, InjectionToken, Injector, NgModule, forwardRef} from '@angular/core'; | ||
import {AOT_TOKEN, AotModule, AotService} from 'app_built/src/module'; | ||
|
||
describe('Ivy NgModule', () => { | ||
describe('AOT', () => { | ||
let injector: Injector; | ||
|
||
beforeEach(() => { injector = Injector.create({definitions: [AotModule]}); }); | ||
it('works', () => { expect(injector.get(AotService) instanceof AotService).toBeTruthy(); }); | ||
|
||
it('merges imports and exports', () => { expect(injector.get(AOT_TOKEN)).toEqual('exports'); }); | ||
}); | ||
|
||
|
||
|
||
describe('JIT', () => { | ||
@Injectable() | ||
class Service { | ||
} | ||
|
||
@NgModule({ | ||
providers: [Service], | ||
}) | ||
class JitModule { | ||
} | ||
|
||
@NgModule({ | ||
imports: [JitModule], | ||
}) | ||
class JitAppModule { | ||
} | ||
|
||
it('works', () => { Injector.create({definitions: [JitAppModule]}); }); | ||
|
||
it('throws an error on circular module dependencies', () => { | ||
@NgModule({ | ||
imports: [forwardRef(() => BModule)], | ||
}) | ||
class AModule { | ||
} | ||
|
||
@NgModule({ | ||
imports: [AModule], | ||
}) | ||
class BModule { | ||
} | ||
|
||
expect(() => Injector.create({ | ||
definitions: [AModule] | ||
})).toThrowError('Circular dependency: module AModule ends up importing itself.'); | ||
}); | ||
|
||
it('merges imports and exports', () => { | ||
const TOKEN = new InjectionToken<string>('TOKEN'); | ||
@NgModule({ | ||
providers: [{provide: TOKEN, useValue: 'provided from A'}], | ||
}) | ||
class AModule { | ||
} | ||
@NgModule({ | ||
providers: [{provide: TOKEN, useValue: 'provided from B'}], | ||
}) | ||
class BModule { | ||
} | ||
|
||
@NgModule({ | ||
imports: [AModule], | ||
exports: [BModule], | ||
}) | ||
class CModule { | ||
} | ||
|
||
const injector = Injector.create({ | ||
definitions: [CModule], | ||
}); | ||
expect(injector.get(TOKEN)).toEqual('provided from B'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.