Skip to content
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

docs(custom-esbuild): align the HTML transformer signature #1705

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 10 additions & 22 deletions packages/custom-esbuild/README.md
Expand Up @@ -187,19 +187,10 @@ The `@angular-builders/custom-esbuild:dev-server` is an enhanced version of the
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:

```typescript
(options: TargetOptions, indexHtmlContent: string) => string | Promise<string>;
(indexHtmlContent: string) => string | Promise<string>;
```

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>`.
`TargetOptions` follows `target` definition from [this](https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/architect/src/input-schema.json) schema and looks like this:

```typescript
export interface Target {
configuration?: string;
project: string;
target: string;
}
```
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>`.

It is useful when you want to transform your `index.html` according to the build options.

Expand All @@ -221,28 +212,25 @@ It is useful when you want to transform your `index.html` according to the build
`index-html-transformer.js`:

```js
module.exports = (targetOptions, indexHtml) => {
module.exports = indexHtml => {
const gitSha = process.env.GIT_SHA;
const i = indexHtml.indexOf('</body>');
const config = `<p>Configuration: ${targetOptions.configuration}</p>`;
const lastCommitSha = `<p>Last commit SHA: ${gitSha}</p>`;
return `${indexHtml.slice(0, i)}
${config}
${lastCommitSha}
${indexHtml.slice(i)}`;
};
```

Alternatively, using TypeScript:

```ts
import { Target } from '@angular-devkit/architect/src/input-schema';
import { json } from '@angular-devkit/core';

type TargetOptions = json.JsonObject & Target;

export default (targetOptions: TargetOptions, indexHtml: string) => {
export default (indexHtml: string) => {
const gitSha = process.env['GIT_SHA'];
const i = indexHtml.indexOf('</body>');
const config = `<p>Configuration: ${targetOptions.configuration}</p>`;
const lastCommitSha = `<p>Last commit SHA1: ${gitSha}</p>`;
return `${indexHtml.slice(0, i)}
${config}
${lastCommitSha}
${indexHtml.slice(i)}`;
};
```
Expand Down