Skip to content

Commit 78e2006

Browse files
authored
feat(custom-esbuild): expose current target to index html transform (#1877)
1 parent bc9f042 commit 78e2006

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

packages/custom-esbuild/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ The `@angular-builders/custom-esbuild:dev-server` is an enhanced version of the
212212
Since Angular 8, `index.html` is not generated as part of the build. If you want to modify your `index.html`, you should use the `indexHtmlTransformer` option. `indexHtmlTransformer` is a path (relative to the workspace root) to a `.js` or `.ts` file that exports a transformation function for `index.html`. If `indexHtmlTransformer` is written in TypeScript, the application's `tsConfig` file will be used by `tsnode` for its execution:
213213
214214
```typescript
215-
(indexHtmlContent: string) => string | Promise<string>;
215+
(indexHtmlContent: string, target: Target) => string | Promise<string>;
216216
```
217217
218218
or, in other words, the function receives target options and original `index.html` content (generated by Angular CLI) and returns a new content as `string` or `Promise<string>`.

packages/custom-esbuild/src/application/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { buildApplication } from '@angular-devkit/build-angular';
44
import { getSystemPath, json, normalize } from '@angular-devkit/core';
55
import type { ApplicationBuilderExtensions } from '@angular/build/src/builders/application/options';
66
import { defer, switchMap } from 'rxjs';
7-
import { loadModule } from '@angular-builders/common';
87

98
import { loadPlugins } from '../load-plugins';
109
import { CustomEsbuildApplicationSchema } from '../custom-esbuild-schema';
10+
import { loadIndexHtmlTransformer } from '../load-index-html-transformer';
1111

1212
export function buildCustomEsbuildApplication(
1313
options: CustomEsbuildApplicationSchema,
@@ -20,10 +20,11 @@ export function buildCustomEsbuildApplication(
2020
const codePlugins = await loadPlugins(options.plugins, workspaceRoot, tsConfig, context.logger);
2121

2222
const indexHtmlTransformer = options.indexHtmlTransformer
23-
? await loadModule(
23+
? await loadIndexHtmlTransformer(
2424
path.join(workspaceRoot, options.indexHtmlTransformer),
2525
tsConfig,
26-
context.logger
26+
context.logger,
27+
context.target
2728
)
2829
: undefined;
2930

packages/custom-esbuild/src/dev-server/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
DevServerBuilderOutput,
66
executeDevServerBuilder,
77
} from '@angular-devkit/build-angular';
8-
import type { IndexHtmlTransform } from '@angular/build/src/utils/index-file/index-html-generator';
98
import { getSystemPath, json, normalize } from '@angular-devkit/core';
109
import { Observable, from, switchMap } from 'rxjs';
1110
import { loadModule } from '@angular-builders/common';
@@ -17,6 +16,7 @@ import {
1716
CustomEsbuildApplicationSchema,
1817
CustomEsbuildDevServerSchema,
1918
} from '../custom-esbuild-schema';
19+
import { loadIndexHtmlTransformer } from '../load-index-html-transformer';
2020

2121
export function executeCustomDevServerBuilder(
2222
options: CustomEsbuildDevServerSchema,
@@ -58,11 +58,12 @@ export function executeCustomDevServerBuilder(
5858
context.logger
5959
);
6060

61-
const indexHtmlTransformer: IndexHtmlTransform = buildOptions.indexHtmlTransformer
62-
? await loadModule(
61+
const indexHtmlTransformer = buildOptions.indexHtmlTransformer
62+
? await loadIndexHtmlTransformer(
6363
path.join(workspaceRoot, buildOptions.indexHtmlTransformer),
6464
tsConfig,
65-
context.logger
65+
context.logger,
66+
context.target
6667
)
6768
: undefined;
6869

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { loadIndexHtmlTransformer } from './load-index-html-transformer';
2+
import { Target } from '@angular-devkit/architect';
3+
4+
describe('loadIndexHtmlTransformer', () => {
5+
beforeEach(() => {
6+
jest.resetModules();
7+
jest.clearAllMocks();
8+
});
9+
10+
it('should pass the target to the transformer', async () => {
11+
const transformIndexHtml = jest.fn();
12+
jest.mock('test/test-index-transform.js', () => transformIndexHtml, { virtual: true });
13+
const mockTarget = { project: 'test', target: 'test' } as Target;
14+
const transform = await loadIndexHtmlTransformer(
15+
'test/test-index-transform.js',
16+
'./tsconfig.json',
17+
null as any,
18+
mockTarget
19+
);
20+
21+
await transform('Hello world!');
22+
expect(transformIndexHtml).toHaveBeenCalledWith('Hello world!', mockTarget);
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { loadModule } from '@angular-builders/common';
2+
import { logging } from '@angular-devkit/core';
3+
import { Target } from '@angular-devkit/architect';
4+
import type { IndexHtmlTransform } from '@angular/build/src/utils/index-file/index-html-generator';
5+
6+
export async function loadIndexHtmlTransformer(
7+
indexHtmlTransformerPath: string,
8+
tsConfig: string,
9+
logger: logging.LoggerApi,
10+
target: Target
11+
): Promise<IndexHtmlTransform> {
12+
const transformer = await loadModule<(indexHtml: string, target: Target) => Promise<string>>(
13+
indexHtmlTransformerPath,
14+
tsConfig,
15+
logger
16+
);
17+
return (indexHtml: string) => transformer(indexHtml, target);
18+
}

0 commit comments

Comments
 (0)