Skip to content

Commit e050413

Browse files
committed
Step 2 - Resolve serve startup error by adopting custom-esbuild approach; apply patch-builder-context method.
1 parent 73ec47e commit e050413

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './utils';
22
export * from './prepare-config'
33
export * from './dependencies'
4+
export * from './patch-builder-context'
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

libs/nx-angular-mf/src/builders/serve/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
import { ServeExecutorSchema } from './schema';
1313
import { BuildExecutorSchema } from '../build/schema';
14-
import { deepMergeObject, getMapName, prepareConfig } from '../helpers';
14+
import { deepMergeObject, getMapName, patchBuilderContext, prepareConfig } from '../helpers';
1515

1616

1717
function getBuilderAction() {
@@ -59,6 +59,8 @@ export async function* runBuilder(
5959
(i) => i.packageName
6060
);
6161

62+
patchBuilderContext(context, buildTarget, externalDependencies);
63+
6264
const normalizeOuterOptions = await normalizeOptions(
6365
context,
6466
context.target.project,

0 commit comments

Comments
 (0)