Skip to content

Commit

Permalink
feat(custom-webpack): support TS for indexTransform (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
vixnguyen authored Feb 27, 2020
1 parent de633f7 commit f97905c
Show file tree
Hide file tree
Showing 12 changed files with 14,367 additions and 11 deletions.
16 changes: 15 additions & 1 deletion packages/custom-webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ module.exports = async config => {
**_Requires `@angular-devkit/build-angular@0.801` and `@angular-builders/custom-webpack@8.1.0`._**
Since Angular 8 `index.html` is not generated as part of the Webpack build. If you want to modify your `index.html` you should use `indexTransform` option.
`indexTransform` is a path (relative to workspace root) to a `.js` file that exports transformation function for `index.html`.
`indexTransform` is a path (relative to workspace root) to a `.js` or `.ts` file that exports transformation function for `index.html`.
Function signature is as following:
```typescript
Expand Down Expand Up @@ -409,6 +409,20 @@ module.exports = (targetOptions, indexHtml) => {
};
```
Alternatively, using TypeScript:
```ts
import { TargetOptions } from '@angular-builders/custom-webpack';

export default (targetOptions: TargetOptions, indexHtml: string) => {
const i = indexHtml.indexOf('</body>');
const config = `<p>Configuration: ${targetOptions.configuration}</p>`;
return `${indexHtml.slice(0, i)}
${config}
${indexHtml.slice(i)}`;
};
```
In the example we add a paragraph with build configuration to your `index.html`. It is a very simple example without any asynchronous code but you can also return a `Promise` from this function.
Full example [here](https://github.com/just-jeb/angular-builders/tree/master/packages/custom-webpack/examples/full-cycle-app).
Expand Down
10 changes: 10 additions & 0 deletions packages/custom-webpack/examples/full-cycle-app/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
"maximumError": "10kb"
}
]
},
"itwcw": {
"customWebpackConfig": null,
"indexTransform": "./index-html.transform.ts"
}
}
},
Expand All @@ -79,6 +83,9 @@
"configurations": {
"production": {
"browserTarget": "full-cycle-app:build:production"
},
"itwcw": {
"browserTarget": "full-cycle-app:build:itwcw"
}
}
},
Expand Down Expand Up @@ -128,6 +135,9 @@
"configurations": {
"production": {
"devServerTarget": "full-cycle-app:serve:production"
},
"itwcw": {
"devServerTarget": "full-cycle-app:serve:itwcw"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = require('./protractor-ci.conf').config;

config.specs = ['./src/**/*.e2e-spec-itwcw.ts'];

exports.config = config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AppPage } from './app.po';

describe('workspace-project App', () => {
let page: AppPage;

beforeEach(() => {
page = new AppPage();
});

it('should display paragraph coming from transform function', async () => {
await page.navigateTo();
expect(await page.getParagraphText()).toEqual('Configuration: itwcw');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TargetOptions } from '@angular-builders/custom-webpack';

export default (targetOptions: TargetOptions, indexHtml: string) => {
const i = indexHtml.indexOf('</body>');
const config = `<p>Configuration: ${targetOptions.configuration}</p>`;
return `${indexHtml.slice(0, i)}
${config}
${indexHtml.slice(i)}`;
};
Loading

0 comments on commit f97905c

Please sign in to comment.