forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
136 lines (119 loc) · 4.15 KB
/
index.ts
File metadata and controls
136 lines (119 loc) · 4.15 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { Builder, BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import assert from 'node:assert';
import { resolve as pathResolve } from 'node:path';
import { Observable, from, isObservable, of, switchMap } from 'rxjs';
import type webpack from 'webpack';
import { EmittedFiles, getEmittedFiles, getWebpackConfig } from '../../utils';
import { Schema as RealWebpackBuilderSchema } from './schema';
export type WebpackBuilderSchema = RealWebpackBuilderSchema;
export interface WebpackLoggingCallback {
(stats: webpack.Stats, config: webpack.Configuration): void;
}
export interface WebpackFactory {
(config: webpack.Configuration): Observable<webpack.Compiler | null> | webpack.Compiler | null;
}
export type BuildResult = BuilderOutput & {
emittedFiles?: EmittedFiles[];
webpackStats?: webpack.StatsCompilation;
outputPath: string;
};
export function runWebpack(
config: webpack.Configuration,
context: BuilderContext,
options: {
logging?: WebpackLoggingCallback;
webpackFactory?: WebpackFactory;
shouldProvideStats?: boolean;
} = {},
): Observable<BuildResult> {
const {
logging: log = (stats, config) => {
if (config.stats !== false) {
const statsOptions = config.stats === true ? undefined : config.stats;
context.logger.info(stats.toString(statsOptions));
}
},
shouldProvideStats = true,
} = options;
const createWebpack = (c: webpack.Configuration) => {
if (options.webpackFactory) {
const result = options.webpackFactory(c);
if (isObservable(result)) {
return result;
} else {
return of(result);
}
} else {
return from(import('webpack').then((mod) => mod.default(c)));
}
};
return createWebpack({ ...config, watch: false }).pipe(
switchMap(
(webpackCompiler) =>
new Observable<BuildResult>((obs) => {
assert(webpackCompiler, 'Webpack compiler factory did not return a compiler instance.');
const callback = (err?: Error | null, stats?: webpack.Stats) => {
if (err) {
return obs.error(err);
}
if (!stats) {
return;
}
// Log stats.
log(stats, config);
const statsOptions = typeof config.stats === 'boolean' ? undefined : config.stats;
const result = {
success: !stats.hasErrors(),
webpackStats: shouldProvideStats ? stats.toJson(statsOptions) : undefined,
emittedFiles: getEmittedFiles(stats.compilation),
outputPath: stats.compilation.outputOptions.path,
} as unknown as BuildResult;
if (config.watch) {
obs.next(result);
} else {
webpackCompiler.close(() => {
obs.next(result);
obs.complete();
});
}
};
try {
if (config.watch) {
const watchOptions = config.watchOptions || {};
const watching = webpackCompiler.watch(watchOptions, callback);
// Teardown logic. Close the watcher when unsubscribed from.
return () => {
watching?.close(() => {});
webpackCompiler.close(() => {});
};
} else {
webpackCompiler.run(callback);
}
} catch (err) {
if (err) {
context.logger.error(
`\nAn error occurred during the build:\n${err instanceof Error ? err.stack : err}`,
);
}
throw err;
}
}),
),
);
}
const builder: Builder<WebpackBuilderSchema> = createBuilder<WebpackBuilderSchema>(
(options, context) => {
const configPath = pathResolve(context.workspaceRoot, options.webpackConfig);
return from(getWebpackConfig(configPath)).pipe(
switchMap((config) => runWebpack(config, context)),
);
},
);
export default builder;