-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler-cli): expose function to allow short-circuiting of link…
…ing (#40137) The linker is implemented using a Babel transform such that Babel needs to parse and walk a source file to find the declarations that need to be compiled. If it can be determined that a source file is known not to contain any declarations the parsing and walking can be skipped as a performance improvement. This commit adds an exposed function for tools that integrate the linker to use to allow short-circuiting of the linker transform. PR Close #40137
- Loading branch information
1 parent
e4fbab9
commit 7dcf286
Showing
8 changed files
with
119 additions
and
12 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
28 changes: 28 additions & 0 deletions
28
packages/compiler-cli/linker/src/file_linker/needs_linking.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,28 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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 {declarationFunctions} from './partial_linkers/partial_linker_selector'; | ||
|
||
/** | ||
* Determines if the provided source file may need to be processed by the linker, i.e. whether it | ||
* potentially contains any declarations. If true is returned, then the source file should be | ||
* processed by the linker as it may contain declarations that need to be fully compiled. If false | ||
* is returned, parsing and processing of the source file can safely be skipped to improve | ||
* performance. | ||
* | ||
* This function may return true even for source files that don't actually contain any declarations | ||
* that need to be compiled. | ||
* | ||
* @param path the absolute path of the source file for which to determine whether linking may be | ||
* needed. | ||
* @param source the source file content as a string. | ||
* @returns whether the source file may contain declarations that need to be linked. | ||
*/ | ||
export function needsLinking(path: string, source: string): boolean { | ||
return declarationFunctions.some(fn => source.includes(fn)); | ||
} |
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
67 changes: 67 additions & 0 deletions
67
packages/compiler-cli/linker/test/file_linker/needs_linking_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,67 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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 {needsLinking} from '../../src/file_linker/needs_linking'; | ||
|
||
describe('needsLinking', () => { | ||
it('should return true for directive declarations', () => { | ||
expect(needsLinking('file.js', ` | ||
export class Dir { | ||
ɵdir = ɵɵngDeclareDirective({type: Dir}); | ||
} | ||
`)).toBeTrue(); | ||
}); | ||
|
||
it('should return true for namespaced directive declarations', () => { | ||
expect(needsLinking('file.js', ` | ||
export class Dir { | ||
ɵdir = ng.ɵɵngDeclareDirective({type: Dir}); | ||
} | ||
`)).toBeTrue(); | ||
}); | ||
|
||
it('should return true for unrelated usages of ɵɵngDeclareDirective', () => { | ||
expect(needsLinking('file.js', ` | ||
const fnName = 'ɵɵngDeclareDirective'; | ||
`)).toBeTrue(); | ||
}); | ||
|
||
it('should return false when the file does not contain ɵɵngDeclareDirective', () => { | ||
expect(needsLinking('file.js', ` | ||
const foo = ngDeclareDirective; | ||
`)).toBeFalse(); | ||
}); | ||
|
||
it('should return true for component declarations', () => { | ||
expect(needsLinking('file.js', ` | ||
export class Cmp { | ||
ɵdir = ɵɵngDeclareComponent({type: Cmp}); | ||
} | ||
`)).toBeTrue(); | ||
}); | ||
|
||
it('should return true for namespaced component declarations', () => { | ||
expect(needsLinking('file.js', ` | ||
export class Cmp { | ||
ɵdir = ng.ɵɵngDeclareComponent({type: Cmp}); | ||
} | ||
`)).toBeTrue(); | ||
}); | ||
|
||
it('should return true for unrelated usages of ɵɵngDeclareComponent', () => { | ||
expect(needsLinking('file.js', ` | ||
const fnName = 'ɵɵngDeclareComponent'; | ||
`)).toBeTrue(); | ||
}); | ||
|
||
it('should return false when the file does not contain ɵɵngDeclareComponent', () => { | ||
expect(needsLinking('file.js', ` | ||
const foo = ngDeclareComponent; | ||
`)).toBeFalse(); | ||
}); | ||
}); |
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
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
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
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