-
Notifications
You must be signed in to change notification settings - Fork 27.2k
feat(ivy): initial ngcc code dump #24897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
16c9547
style(ivy): remove underscore from TypeScriptReflectionHost._getDecla…
petebacondarwin e1c4e15
refactor(ivy): `TypeScriptReflectionHost.isClass` cannot be a type di…
petebacondarwin 5d1efa5
fix(ivy): make ngtsc `ClassMember` `node` and `declaration` optional
petebacondarwin 2cccb23
refactor(ivy): allow `ImportManager` to have configurable prefix
petebacondarwin 07e2ea8
fix(ivy): allow `FunctionExpression` to indicate a method declaration
petebacondarwin d5d232d
test(ivy): allow `makeProgram` to be more configurable
petebacondarwin 77026a4
build: add dependencies to be used by ngcc
petebacondarwin 8431588
feat(ivy): ngcc project skeleton
petebacondarwin 68b19b5
test(ivy): implement ngcc specific version of `makeProgram`
petebacondarwin 02249b3
feat(ivy): implement esm2015 and esm5 reflection hosts
petebacondarwin 5f98984
feat(ivy): implement esm2015 and esm5 file parsers
petebacondarwin 00c2f00
feat(ivy): implement ngcc `Analyzer`
petebacondarwin bdb271c
feat(ivy): implement esm2015 and esm5 ngcc file renderers
petebacondarwin 8f30c4f
feat(ivy): implement initial ngcc package transformer
petebacondarwin 3d81e06
refactor(ivy): do not deep import from ngtsc into ngcc
petebacondarwin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,21 @@ | ||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| load("//tools:defaults.bzl", "ts_library") | ||
| load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") | ||
|
|
||
| ts_library( | ||
| name = "ngcc", | ||
| srcs = glob([ | ||
| "*.ts", | ||
| "**/*.ts", | ||
| ]), | ||
| module_name = "@angular/compiler-cli/src/ngcc", | ||
| deps = [ | ||
| "//packages:types", | ||
| "//packages/compiler", | ||
| "//packages/compiler-cli/src/ngtsc/annotations", | ||
| "//packages/compiler-cli/src/ngtsc/host", | ||
| "//packages/compiler-cli/src/ngtsc/metadata", | ||
| "//packages/compiler-cli/src/ngtsc/transform", | ||
| ], | ||
| ) |
This file contains hidden or 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,30 @@ | ||
| # Angular Compatibility Compiler (ngcc) | ||
|
|
||
| This compiler will convert `node_modules` compiled with `ngc`, into `node_modules` which | ||
| appear to have been compiled with `ngtsc`. | ||
|
|
||
| This conversion will allow such "legacy" packages to be used by the Ivy rendering engine. | ||
|
|
||
| ## Building | ||
|
|
||
| The project is built using Bazel: | ||
|
|
||
| ```bash | ||
| bazel build //packages/compiler-cli/src/ngcc | ||
| ``` | ||
|
|
||
| ## Unit Testing | ||
|
|
||
| The unit tests are built and run using Bazel: | ||
|
|
||
| ```bash | ||
| bazel test //packages/compiler-cli/src/ngcc/test | ||
| ``` | ||
|
|
||
| ## Integration Testing | ||
|
|
||
| There are tests that check the behaviour of the overall executable: | ||
|
|
||
| ```bash | ||
| bazel test //packages/compiler-cli/test/ngcc | ||
| ``` |
This file contains hidden or 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,9 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
|
|
||
| export {mainNgcc} from './src/main'; |
This file contains hidden or 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,16 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * @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 {mainNgcc} from './src/main'; | ||
|
|
||
| // CLI entry point | ||
| if (require.main === module) { | ||
| const args = process.argv.slice(2); | ||
| process.exitCode = mainNgcc(args); | ||
| } | ||
This file contains hidden or 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,97 @@ | ||
| /** | ||
| * @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 * as fs from 'fs'; | ||
| import * as ts from 'typescript'; | ||
| import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, PipeDecoratorHandler, ResourceLoader, SelectorScopeRegistry} from '../../ngtsc/annotations'; | ||
| import {Decorator} from '../../ngtsc/host'; | ||
| import {CompileResult, DecoratorHandler} from '../../ngtsc/transform'; | ||
| import {NgccReflectionHost} from './host/ngcc_host'; | ||
| import {ParsedClass} from './parsing/parsed_class'; | ||
| import {ParsedFile} from './parsing/parsed_file'; | ||
| import {isDefined} from './utils'; | ||
|
|
||
| export interface AnalyzedClass<T = any> extends ParsedClass { | ||
| handler: DecoratorHandler<T>; | ||
| analysis: any; | ||
| diagnostics?: ts.Diagnostic[]; | ||
| compilation: CompileResult[]; | ||
| } | ||
|
|
||
| export interface AnalyzedFile { | ||
| analyzedClasses: AnalyzedClass[]; | ||
| sourceFile: ts.SourceFile; | ||
| } | ||
|
|
||
| export interface MatchingHandler<T> { | ||
| handler: DecoratorHandler<T>; | ||
| decorator: Decorator; | ||
| } | ||
|
|
||
| /** | ||
| * `ResourceLoader` which directly uses the filesystem to resolve resources synchronously. | ||
| */ | ||
| export class FileResourceLoader implements ResourceLoader { | ||
| load(url: string): string { return fs.readFileSync(url, 'utf8'); } | ||
| } | ||
|
|
||
| export class Analyzer { | ||
| resourceLoader = new FileResourceLoader(); | ||
| scopeRegistry = new SelectorScopeRegistry(this.typeChecker, this.host); | ||
| handlers: DecoratorHandler<any>[] = [ | ||
| new ComponentDecoratorHandler( | ||
| this.typeChecker, this.host, this.scopeRegistry, false, this.resourceLoader), | ||
| new DirectiveDecoratorHandler(this.typeChecker, this.host, this.scopeRegistry, false), | ||
| new InjectableDecoratorHandler(this.host, false), | ||
| new NgModuleDecoratorHandler(this.typeChecker, this.host, this.scopeRegistry, false), | ||
| new PipeDecoratorHandler(this.typeChecker, this.host, this.scopeRegistry, false), | ||
| ]; | ||
|
|
||
| constructor(private typeChecker: ts.TypeChecker, private host: NgccReflectionHost) {} | ||
|
|
||
| /** | ||
| * Analyize a parsed file to generate the information about decorated classes that | ||
| * should be converted to use ivy definitions. | ||
| * @param file The file to be analysed for decorated classes. | ||
| */ | ||
| analyzeFile(file: ParsedFile): AnalyzedFile { | ||
| const analyzedClasses = | ||
| file.decoratedClasses.map(clazz => this.analyzeClass(file.sourceFile, clazz)) | ||
| .filter(isDefined); | ||
|
|
||
| return { | ||
| analyzedClasses, | ||
| sourceFile: file.sourceFile, | ||
| }; | ||
| } | ||
|
|
||
| protected analyzeClass(file: ts.SourceFile, clazz: ParsedClass): AnalyzedClass|undefined { | ||
| const matchingHandlers = | ||
| this.handlers.map(handler => ({handler, decorator: handler.detect(clazz.decorators)})) | ||
| .filter(isMatchingHandler); | ||
|
|
||
| if (matchingHandlers.length > 1) { | ||
| throw new Error('TODO.Diagnostic: Class has multiple Angular decorators.'); | ||
| } | ||
|
|
||
| if (matchingHandlers.length == 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const {handler, decorator} = matchingHandlers[0]; | ||
| const {analysis, diagnostics} = handler.analyze(clazz.declaration, decorator); | ||
| let compilation = handler.compile(clazz.declaration, analysis); | ||
| if (!Array.isArray(compilation)) { | ||
| compilation = [compilation]; | ||
| } | ||
| return {...clazz, handler, analysis, diagnostics, compilation}; | ||
| } | ||
| } | ||
|
|
||
| function isMatchingHandler<T>(handler: Partial<MatchingHandler<T>>): handler is MatchingHandler<T> { | ||
| return !!handler.decorator; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need both
index.tsandmain-ngcc.ts?Couldn't we just export
mainNgccfrommain-ngcc.ts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure. This is how Alex set it up for us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mirrored the
ngcsetup. I think the goal there was to have a side-effect free import and a side effectful entrypoint.