|
| 1 | +import { BuilderContext, Target } from '@angular-devkit/architect'; |
| 2 | + |
| 3 | +const executorToBuilderMap = new Map<string, string>([ |
| 4 | + [ |
| 5 | + '@klerick/nx-angular-mf', |
| 6 | + '@angular-devkit/build-angular:application', |
| 7 | + ], [ |
| 8 | + './dist/libs/nx-angular-mf:build', |
| 9 | + '@angular-devkit/build-angular:application', |
| 10 | + ] |
| 11 | +]); |
| 12 | + |
| 13 | +function cleanBuildTargetOptions(options: any) { |
| 14 | + delete options.mf; |
| 15 | + return options; |
| 16 | +} |
| 17 | + |
| 18 | +export function patchBuilderContext( |
| 19 | + context: BuilderContext, |
| 20 | + buildTarget: Target, |
| 21 | + externalDependencies: string[] |
| 22 | +): void { |
| 23 | + const originalGetBuilderNameForTarget = context.getBuilderNameForTarget; |
| 24 | + |
| 25 | + // We have to patch `getBuilderNameForTarget` because Angular CLI checks |
| 26 | + // whether the runnable target is `@angular-devkit/build-angular:application` |
| 27 | + // and then defines the server to run. If the `builderName` (returned by |
| 28 | + // `context.getBuilderNameForTarget`) is not an `@angular-devkit/build-angular:application`, |
| 29 | + // then it will use the Webpack server for the `dev-server target`. By patching |
| 30 | + // the return value, Angular will use the Vite server for the `dev-server` target. |
| 31 | + context.getBuilderNameForTarget = async (target) => { |
| 32 | + const builderName = await originalGetBuilderNameForTarget(target); |
| 33 | + console.log(builderName); |
| 34 | + if (executorToBuilderMap.has(builderName)) { |
| 35 | + return executorToBuilderMap.get(builderName)!; |
| 36 | + } |
| 37 | + |
| 38 | + return builderName; |
| 39 | + }; |
| 40 | + |
| 41 | + const originalGetTargetOptions = context.getTargetOptions; |
| 42 | + context.getTargetOptions = async (target) => { |
| 43 | + const options = await originalGetTargetOptions(target); |
| 44 | + if ( |
| 45 | + target.project === buildTarget.project && |
| 46 | + target.target === buildTarget.target && |
| 47 | + target.configuration === buildTarget.configuration |
| 48 | + ) { |
| 49 | + cleanBuildTargetOptions(options); |
| 50 | + } |
| 51 | + options.externalDependencies = externalDependencies; |
| 52 | + return options; |
| 53 | + }; |
| 54 | +} |
0 commit comments