-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
webpack.impl.ts
122 lines (106 loc) · 3.07 KB
/
webpack.impl.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
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
import 'dotenv/config';
import { ExecutorContext } from '@nrwl/devkit';
import { readCachedProjectGraph } from '@nrwl/devkit';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { getRootTsConfigPath } from '@nrwl/workspace/src/utilities/typescript';
import { map, tap } from 'rxjs/operators';
import { eachValueFrom } from 'rxjs-for-await';
import { resolve } from 'path';
import { register } from 'ts-node';
import { getNodeWebpackConfig } from '../../utils/node.config';
import { BuildNodeBuilderOptions } from '../../utils/types';
import { normalizeBuildOptions } from '../../utils/normalize';
import { deleteOutputDir } from '../../utils/fs';
import { runWebpack } from '../../utils/run-webpack';
export type NodeBuildEvent = {
outfile: string;
success: boolean;
};
export async function* webpackExecutor(
rawOptions: BuildNodeBuilderOptions,
context: ExecutorContext
) {
const { sourceRoot, root } = context.workspace.projects[context.projectName];
if (!sourceRoot) {
throw new Error(`${context.projectName} does not have a sourceRoot.`);
}
if (!root) {
throw new Error(`${context.projectName} does not have a root.`);
}
const options = normalizeBuildOptions(
rawOptions,
context.root,
sourceRoot,
root
);
if (options.webpackConfig.some((x) => x.endsWith('.ts'))) {
registerTsNode();
}
const projGraph = readCachedProjectGraph();
if (!options.buildLibsFromSource) {
const { target, dependencies } = calculateProjectDependencies(
projGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);
options.tsConfig = createTmpTsConfig(
options.tsConfig,
context.root,
target.data.root,
dependencies
);
if (
!checkDependentProjectsHaveBeenBuilt(
context.root,
context.projectName,
context.targetName,
dependencies
)
) {
return { success: false } as any;
}
}
// Delete output path before bundling
if (options.deleteOutputPath) {
deleteOutputDir(context.root, options.outputPath);
}
const config = await options.webpackConfig.reduce(
async (currentConfig, plugin) => {
return require(plugin)(await currentConfig, {
options,
configuration: context.configurationName,
});
},
Promise.resolve(getNodeWebpackConfig(context, projGraph, options))
);
return yield* eachValueFrom(
runWebpack(config).pipe(
tap((stats) => {
console.info(stats.toString(config.stats));
}),
map((stats) => {
return {
success: !stats.hasErrors(),
outfile: resolve(
context.root,
options.outputPath,
options.outputFileName
),
} as NodeBuildEvent;
})
)
);
}
function registerTsNode() {
const rootTsConfig = getRootTsConfigPath();
register({
...(rootTsConfig ? { project: rootTsConfig } : null),
});
}
export default webpackExecutor;