/
transform-factories.ts
55 lines (47 loc) · 1.92 KB
/
transform-factories.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as path from 'node:path';
import { BuilderContext, Target } from '@angular-devkit/architect';
import { ExecutionTransformer } from '@angular-devkit/build-angular';
import { IndexHtmlTransform } from '@angular-devkit/build-angular/src/utils/index-file/index-html-generator';
import { getSystemPath, normalize } from '@angular-devkit/core';
import { Configuration } from 'webpack';
import { loadModule } from '@angular-builders/common';
import { CustomWebpackBuilder } from './custom-webpack-builder';
import { CustomWebpackSchema } from './custom-webpack-schema';
interface IndexHtmlTransformWithOptions {
(options: Target, indexHtml: string): Promise<string>;
}
export const customWebpackConfigTransformFactory: (
options: CustomWebpackSchema,
context: BuilderContext
) => ExecutionTransformer<Configuration> =
(options, { workspaceRoot, target, logger }) =>
browserWebpackConfig => {
return CustomWebpackBuilder.buildWebpackConfig(
normalize(workspaceRoot),
options.customWebpackConfig,
browserWebpackConfig,
options,
target,
logger
);
};
export const indexHtmlTransformFactory: (
options: CustomWebpackSchema,
context: BuilderContext
) => IndexHtmlTransform = (options, { workspaceRoot, target, logger }) => {
if (!options.indexTransform) return null;
const transformPath = path.join(getSystemPath(normalize(workspaceRoot)), options.indexTransform);
const tsConfig = path.join(getSystemPath(normalize(workspaceRoot)), options.tsConfig);
return async (indexHtml: string) => {
const transform = await loadModule<IndexHtmlTransformWithOptions>(
transformPath,
tsConfig,
logger
);
return transform(target, indexHtml);
};
};
export const getTransforms = (options: CustomWebpackSchema, context: BuilderContext) => ({
webpackConfiguration: customWebpackConfigTransformFactory(options, context),
indexHtml: indexHtmlTransformFactory(options, context),
});