Skip to content

Commit

Permalink
refactor(@angular-devkit/build-angular): clean up linker detection code
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-agius4 committed Mar 15, 2021
1 parent 9ea34ba commit 217a02b
Showing 1 changed file with 6 additions and 41 deletions.
47 changes: 6 additions & 41 deletions packages/angular_devkit/build_angular/src/babel/webpack-loader.ts
Expand Up @@ -5,6 +5,7 @@
* 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 '@angular/compiler-cli/linker';
import { custom } from 'babel-loader';
import { ScriptTarget } from 'typescript';
import { ApplicationPresetOptions } from './presets/application';
Expand All @@ -16,45 +17,17 @@ interface AngularCustomOptions {
i18n: ApplicationPresetOptions['i18n'];
}

/**
* 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(
function requiresLinking(
path: string,
source: string,
): Promise<{ hasLinkerSupport?: boolean; requiresLinking: boolean }> {
): boolean {
// @angular/core and @angular/compiler will cause false positives
// Also, TypeScript files do not require linking
if (/[\\\/]@angular[\\\/](?:compiler|core)|\.tsx?$/.test(path)) {
return { requiresLinking: false };
return 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'),
};
return needsLinking(path, source);
}

export default custom<AngularCustomOptions>(() => {
Expand All @@ -80,15 +53,7 @@ export default custom<AngularCustomOptions>(() => {
};

// Analyze file for linking
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 {
customOptions.shouldLink = requiresLinking;
}
customOptions.shouldLink = await requiresLinking(this.resourcePath, source);
shouldProcess ||= customOptions.shouldLink;

// Analyze for ES target processing
Expand Down

0 comments on commit 217a02b

Please sign in to comment.