forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.impl.ts
104 lines (91 loc) · 2.76 KB
/
build.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
import { ExecutorContext } from '@nrwl/devkit';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { runWebpack } from '@nrwl/workspace/src/utilities/run-webpack';
import { map, tap } from 'rxjs/operators';
import { eachValueFrom } from 'rxjs-for-await';
import { resolve } from 'path';
import { getNodeWebpackConfig } from '../../utils/node.config';
import { OUT_FILENAME } from '../../utils/config';
import { BuildNodeBuilderOptions } from '../../utils/types';
import { normalizeBuildOptions } from '../../utils/normalize';
import { generatePackageJson } from '../../utils/generate-package-json';
try {
require('dotenv').config();
} catch (e) {}
export type NodeBuildEvent = {
outfile: string;
success: boolean;
};
export async function* buildExecutor(
rawOptions: BuildNodeBuilderOptions,
context: ExecutorContext
) {
const { webpack } = require('../../webpack/entry');
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
);
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;
}
}
if (options.generatePackageJson) {
generatePackageJson(context.projectName, projGraph, options);
}
const config = options.webpackConfig.reduce((currentConfig, plugin) => {
return require(plugin)(currentConfig, {
options,
configuration: context.configurationName,
});
}, getNodeWebpackConfig(options));
return yield* eachValueFrom(
runWebpack(config, webpack).pipe(
tap((stats) => {
console.info(stats.toString(config.stats));
}),
map((stats) => {
return {
success: !stats.hasErrors(),
outfile: resolve(context.root, options.outputPath, OUT_FILENAME),
} as NodeBuildEvent;
})
)
);
}
export default buildExecutor;