-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular-devkit/build-angular): integrate Angular compiler linker #19641
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
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
24 changes: 24 additions & 0 deletions
24
packages/angular_devkit/build_angular/src/babel/babel-loader.d.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,24 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
| declare module 'babel-loader' { | ||
| type BabelLoaderCustomizer<T> = ( | ||
| babel: typeof import('@babel/core'), | ||
| ) => { | ||
| customOptions?( | ||
| this: import('webpack').loader.LoaderContext, | ||
| loaderOptions: Record<string, unknown>, | ||
| loaderArguments: { source: string; map?: unknown }, | ||
| ): Promise<{ custom?: T; loader: Record<string, unknown> }>; | ||
| config?( | ||
| this: import('webpack').loader.LoaderContext, | ||
| configuration: import('@babel/core').PartialConfig, | ||
| loaderArguments: { source: string; map?: unknown; customOptions: T }, | ||
| ): import('@babel/core').TransformOptions; | ||
| }; | ||
| function custom<T>(customizer: BabelLoaderCustomizer<T>): import('webpack').loader.Loader; | ||
| } |
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
120 changes: 120 additions & 0 deletions
120
packages/angular_devkit/build_angular/src/babel/webpack-loader.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,120 @@ | ||
| /** | ||
| * @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 { custom } from 'babel-loader'; | ||
|
|
||
| interface AngularCustomOptions { | ||
| forceES5: boolean; | ||
| shouldLink: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Cached linker check utility function | ||
| * | ||
| * If undefined, not yet been imported | ||
| * If null, attempted import failed and no linker support | ||
| * If function, import succeeded and linker supported | ||
| */ | ||
| let needsLinking: undefined | null | typeof import('@angular/compiler-cli/linker').needsLinking; | ||
|
|
||
| async function checkLinking( | ||
| path: string, | ||
| source: string, | ||
| ): Promise<{ hasLinkerSupport?: boolean; requiresLinking: boolean }> { | ||
| // @angular/core and @angular/compiler will cause false positives | ||
| if (/[\\\/]@angular[\\\/](?:compiler|core)/.test(path)) { | ||
| return { requiresLinking: false }; | ||
| } | ||
|
|
||
| if (needsLinking !== null) { | ||
| try { | ||
| if (needsLinking === undefined) { | ||
| needsLinking = (await import('@angular/compiler-cli/linker')).needsLinking; | ||
| } | ||
|
|
||
| // If the linker entry point is present then there is linker support | ||
| return { hasLinkerSupport: true, requiresLinking: needsLinking(path, source) }; | ||
| } catch { | ||
| needsLinking = null; | ||
| } | ||
| } | ||
|
|
||
| // Fallback for Angular versions less than 11.1.0 with no linker support. | ||
| // This information is used to issue errors if a partially compiled library is used when unsupported. | ||
| return { | ||
| hasLinkerSupport: false, | ||
| requiresLinking: | ||
| source.includes('ɵɵngDeclareDirective') || source.includes('ɵɵngDeclareComponent'), | ||
| }; | ||
| } | ||
|
|
||
| export default custom<AngularCustomOptions>(() => { | ||
| const baseOptions = Object.freeze({ | ||
| babelrc: false, | ||
| configFile: false, | ||
| compact: false, | ||
| cacheCompression: false, | ||
| sourceType: 'unambiguous', | ||
| }); | ||
|
|
||
| return { | ||
| async customOptions({ forceES5, ...loaderOptions }, { source }) { | ||
| let shouldProcess = forceES5; | ||
|
|
||
| let shouldLink = false; | ||
| const { hasLinkerSupport, requiresLinking } = await checkLinking(this.resourcePath, source); | ||
| if (requiresLinking && !hasLinkerSupport) { | ||
| // Cannot link if there is no linker support | ||
| this.emitError( | ||
| 'File requires the Angular linker. "@angular/compiler-cli" version 11.1.0 or greater is needed.', | ||
| ); | ||
| } else { | ||
| shouldLink = requiresLinking; | ||
| } | ||
| shouldProcess ||= shouldLink; | ||
|
|
||
| const options: Record<string, unknown> = { | ||
| ...baseOptions, | ||
| ...loaderOptions, | ||
| }; | ||
|
|
||
| if (!shouldProcess) { | ||
| // Force the current file to be ignored | ||
| options.ignore = [() => true]; | ||
| } | ||
|
|
||
| return { custom: { forceES5: !!forceES5, shouldLink }, loader: options }; | ||
| }, | ||
| config(configuration, { customOptions }) { | ||
| return { | ||
| ...configuration.options, | ||
| presets: [ | ||
| ...(configuration.options.presets || []), | ||
| [ | ||
| require('./presets/application').default, | ||
| { | ||
| angularLinker: customOptions.shouldLink, | ||
| forceES5: customOptions.forceES5, | ||
| diagnosticReporter: (type, message) => { | ||
| switch (type) { | ||
| case 'error': | ||
| this.emitError(message); | ||
| break; | ||
| case 'info': | ||
| // Webpack does not currently have an informational diagnostic | ||
| case 'warning': | ||
| this.emitWarning(message); | ||
| break; | ||
| } | ||
| }, | ||
| } as import('./presets/application').ApplicationPresetOptions, | ||
| ], | ||
| ], | ||
| }; | ||
| }, | ||
| }; | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.