Repository navigation
Fix g3 core imports #27998
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
Fix g3 core imports #27998
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
22 changes: 0 additions & 22 deletions
22
packages/compiler-cli/src/ngcc/src/rendering/ngcc_import_manager.ts
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
packages/compiler-cli/src/ngcc/src/rendering/ngcc_import_rewriter.ts
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,34 @@ | ||
|
|
||
| /** | ||
| * @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 {ImportRewriter, validateAndRewriteCoreSymbol} from '../../../ngtsc/imports'; | ||
|
|
||
| export class NgccFlatImportRewriter implements ImportRewriter { | ||
| shouldImportSymbol(symbol: string, specifier: string): boolean { | ||
| if (specifier === '@angular/core') { | ||
| // Don't use imports for @angular/core symbols in a flat bundle, as they'll be visible | ||
| // directly. | ||
| return false; | ||
| } else { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| rewriteSymbol(symbol: string, specifier: string): string { | ||
| if (specifier === '@angular/core') { | ||
| return validateAndRewriteCoreSymbol(symbol); | ||
| } else { | ||
| return symbol; | ||
| } | ||
| } | ||
|
|
||
| rewriteSpecifier(originalModulePath: string, inContextOfFile: string): string { | ||
| return originalModulePath; | ||
| } | ||
| } | ||
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,103 @@ | ||
| /** | ||
| * @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 {relativePathBetween} from '../../util/src/path'; | ||
|
|
||
| /** | ||
| * Rewrites imports of symbols being written into generated code. | ||
| */ | ||
| export interface ImportRewriter { | ||
| /** | ||
| * Should the given symbol be imported at all? | ||
| * | ||
| * If `true`, the symbol should be imported from the given specifier. If `false`, the symbol | ||
| * should be referenced directly, without an import. | ||
| */ | ||
| shouldImportSymbol(symbol: string, specifier: string): boolean; | ||
|
|
||
| /** | ||
| * Optionally rewrite a reference to an imported symbol, changing either the binding prefix or the | ||
| * symbol name itself. | ||
| */ | ||
| rewriteSymbol(symbol: string, specifier: string): string; | ||
|
|
||
| /** | ||
| * Optionally rewrite the given module specifier in the context of a given file. | ||
| */ | ||
| rewriteSpecifier(specifier: string, inContextOfFile: string): string; | ||
| } | ||
|
|
||
| /** | ||
| * `ImportRewriter` that does no rewriting. | ||
| */ | ||
| export class NoopImportRewriter implements ImportRewriter { | ||
| shouldImportSymbol(symbol: string, specifier: string): boolean { return true; } | ||
|
|
||
| rewriteSymbol(symbol: string, specifier: string): string { return symbol; } | ||
|
|
||
| rewriteSpecifier(specifier: string, inContextOfFile: string): string { return specifier; } | ||
| } | ||
|
|
||
| /** | ||
| * A mapping of supported symbols that can be imported from within @angular/core, and the names by | ||
| * which they're exported from r3_symbols. | ||
| */ | ||
| const CORE_SUPPORTED_SYMBOLS = new Map<string, string>([ | ||
| ['defineInjectable', 'defineInjectable'], | ||
| ['defineInjector', 'defineInjector'], | ||
| ['ɵdefineNgModule', 'defineNgModule'], | ||
| ['inject', 'inject'], | ||
| ['ɵsetClassMetadata', 'setClassMetadata'], | ||
| ['ɵInjectableDef', 'InjectableDef'], | ||
| ['ɵInjectorDef', 'InjectorDef'], | ||
| ['ɵNgModuleDefWithMeta', 'NgModuleDefWithMeta'], | ||
| ['ɵNgModuleFactory', 'NgModuleFactory'], | ||
| ]); | ||
|
|
||
| const CORE_MODULE = '@angular/core'; | ||
|
|
||
| /** | ||
| * `ImportRewriter` that rewrites imports from '@angular/core' to be imported from the r3_symbols.ts | ||
| * file instead. | ||
| */ | ||
| export class R3SymbolsImportRewriter implements ImportRewriter { | ||
| constructor(private r3SymbolsPath: string) {} | ||
|
|
||
| shouldImportSymbol(symbol: string, specifier: string): boolean { return true; } | ||
|
|
||
| rewriteSymbol(symbol: string, specifier: string): string { | ||
| if (specifier !== CORE_MODULE) { | ||
| // This import isn't from core, so ignore it. | ||
| return symbol; | ||
| } | ||
|
|
||
| return validateAndRewriteCoreSymbol(symbol); | ||
| } | ||
|
|
||
| rewriteSpecifier(specifier: string, inContextOfFile: string): string { | ||
| if (specifier !== CORE_MODULE) { | ||
| // This module isn't core, so ignore it. | ||
| return specifier; | ||
| } | ||
|
|
||
| const relativePathToR3Symbols = relativePathBetween(inContextOfFile, this.r3SymbolsPath); | ||
| if (relativePathToR3Symbols === null) { | ||
| throw new Error( | ||
| `Failed to rewrite import inside ${CORE_MODULE}: ${inContextOfFile} -> ${this.r3SymbolsPath}`); | ||
| } | ||
|
|
||
| return relativePathToR3Symbols; | ||
| } | ||
| } | ||
|
|
||
| export function validateAndRewriteCoreSymbol(name: string): string { | ||
| if (!CORE_SUPPORTED_SYMBOLS.has(name)) { | ||
| throw new Error(`Importing unexpected symbol ${name} while compiling ${CORE_MODULE}`); | ||
| } | ||
| return CORE_SUPPORTED_SYMBOLS.get(name) !; | ||
| } |
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
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.
No empty line between methods 😞
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.
Fixed.