forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
181 lines (163 loc) · 5.79 KB
/
index.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @license
* Copyright Google Inc. 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.io/license
*/
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import { runWebpack } from '@angular-devkit/build-webpack';
import { json, tags } from '@angular-devkit/core';
import * as path from 'path';
import { Observable, from } from 'rxjs';
import { concatMap, map } from 'rxjs/operators';
import { ScriptTarget } from 'typescript';
import * as webpack from 'webpack';
import { ExecutionTransformer } from '../transforms';
import { NormalizedBrowserBuilderSchema, deleteOutputDir } from '../utils';
import { i18nInlineEmittedFiles } from '../utils/i18n-inlining';
import { I18nOptions } from '../utils/i18n-options';
import { ensureOutputPaths } from '../utils/output-paths';
import { readTsconfig } from '../utils/read-tsconfig';
import { assertCompatibleAngularVersion } from '../utils/version';
import { generateI18nBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config';
import {
getAotConfig,
getCommonConfig,
getServerConfig,
getStatsConfig,
getStylesConfig,
} from '../webpack/configs';
import { JsonCompilationStats, webpackStatsLogger } from '../webpack/utils/stats';
import { Schema as ServerBuilderOptions } from './schema';
// If success is true, outputPath should be set.
export type ServerBuilderOutput = json.JsonObject & BuilderOutput & {
baseOutputPath: string;
outputPaths: string[];
/**
* @deprecated in version 9. Use 'outputPaths' instead.
*/
outputPath: string;
};
export { ServerBuilderOptions };
export function execute(
options: ServerBuilderOptions,
context: BuilderContext,
transforms: {
webpackConfiguration?: ExecutionTransformer<webpack.Configuration>;
} = {},
): Observable<ServerBuilderOutput> {
const root = context.workspaceRoot;
// Check Angular version.
assertCompatibleAngularVersion(root, context.logger);
const tsConfig = readTsconfig(options.tsConfig, root);
const target = tsConfig.options.target || ScriptTarget.ES5;
const baseOutputPath = path.resolve(root, options.outputPath);
let outputPaths: undefined | Map<string, string>;
if (typeof options.bundleDependencies === 'string') {
options.bundleDependencies = options.bundleDependencies === 'all';
context.logger.warn(`Option 'bundleDependencies' string value is deprecated since version 9. Use a boolean value instead.`);
}
if (!options.bundleDependencies && tsConfig.options.enableIvy) {
// tslint:disable-next-line: no-implicit-dependencies
const { __processed_by_ivy_ngcc__, main = '' } = require('@angular/core/package.json');
if (
!__processed_by_ivy_ngcc__ ||
!__processed_by_ivy_ngcc__.main ||
(main as string).includes('__ivy_ngcc__')
) {
context.logger.warn(tags.stripIndent`
Warning: Turning off 'bundleDependencies' with Ivy may result in undefined behaviour
unless 'node_modules' are transformed using the standalone Angular compatibility compiler (NGCC).
See: https://angular.io/guide/ivy#ivy-and-universal-app-shell
`);
}
}
return from(initialize(options, context, transforms.webpackConfiguration)).pipe(
concatMap(({ config, i18n }) => {
return runWebpack(config, context, {
webpackFactory: require('webpack') as typeof webpack,
logging: (stats, config) => {
if (options.verbose) {
context.logger.info(stats.toString(config.stats));
}
},
}).pipe(
concatMap(async output => {
const { emittedFiles = [], outputPath, webpackStats } = output;
if (!webpackStats) {
throw new Error('Webpack stats build result is required.');
}
let success = output.success;
if (success && i18n.shouldInline) {
outputPaths = ensureOutputPaths(baseOutputPath, i18n);
success = await i18nInlineEmittedFiles(
context,
emittedFiles,
i18n,
baseOutputPath,
Array.from(outputPaths.values()),
[],
outputPath,
target <= ScriptTarget.ES5,
options.i18nMissingTranslation,
);
}
webpackStatsLogger(context.logger, webpackStats as JsonCompilationStats, config);
return { ...output, success };
}),
);
}),
map(output => {
if (!output.success) {
return output as ServerBuilderOutput;
}
return {
...output,
baseOutputPath,
outputPath: baseOutputPath,
outputPaths: outputPaths || [baseOutputPath],
} as ServerBuilderOutput;
}),
);
}
export default createBuilder<json.JsonObject & ServerBuilderOptions, ServerBuilderOutput>(
execute,
);
async function initialize(
options: ServerBuilderOptions,
context: BuilderContext,
webpackConfigurationTransform?: ExecutionTransformer<webpack.Configuration>,
): Promise<{
config: webpack.Configuration;
i18n: I18nOptions;
}> {
const originalOutputPath = options.outputPath;
const { config, i18n } = await generateI18nBrowserWebpackConfigFromContext(
{
...options,
buildOptimizer: false,
aot: true,
platform: 'server',
} as NormalizedBrowserBuilderSchema,
context,
wco => [
getCommonConfig(wco),
getServerConfig(wco),
getStylesConfig(wco),
getStatsConfig(wco),
getAotConfig(wco),
],
);
let transformedConfig;
if (webpackConfigurationTransform) {
transformedConfig = await webpackConfigurationTransform(config);
}
if (options.deleteOutputPath) {
deleteOutputDir(
context.workspaceRoot,
originalOutputPath,
);
}
return { config: transformedConfig || config, i18n };
}