|
1 |
| -import { Command, Option, CommandScope } from '../models/command'; |
2 |
| -import { CliConfig } from '../models/config'; |
3 |
| -import { BuildOptions } from '../models/build-options'; |
| 1 | +import { ArchitectCommand } from '../models/architect-command'; |
| 2 | +import { Option, CommandScope } from '../models/command'; |
4 | 3 | import { Version } from '../upgrade/version';
|
5 |
| -import { oneLine } from 'common-tags'; |
6 |
| -import { getAppFromConfig } from '../utilities/app-utils'; |
7 |
| -import { join } from 'path'; |
8 |
| -import { RenderUniversalTaskOptions } from '../tasks/render-universal'; |
9 | 4 |
|
10 |
| -const SilentError = require('silent-error'); |
11 |
| - |
12 |
| -const config = CliConfig.fromProject() || CliConfig.fromGlobal(); |
13 |
| -const buildConfigDefaults = config.getPaths('defaults.build', [ |
14 |
| - 'sourcemaps', 'baseHref', 'progress', 'poll', 'deleteOutputPath', 'preserveSymlinks', |
15 |
| - 'showCircularDependencies', 'commonChunk', 'namedChunks' |
16 |
| -]); |
17 |
| - |
18 |
| -// defaults for BuildOptions |
19 |
| -export const baseBuildCommandOptions: Option[] = [ |
20 |
| - { |
21 |
| - name: 'target', |
22 |
| - type: String, |
23 |
| - default: 'development', |
24 |
| - // TODO: re-add support for `--prod` |
25 |
| - aliases: ['t'], |
26 |
| - description: 'Defines the build target.' |
27 |
| - }, |
28 |
| - { |
29 |
| - name: 'environment', |
30 |
| - type: String, |
31 |
| - aliases: ['e'], |
32 |
| - description: 'Defines the build environment.' |
33 |
| - }, |
34 |
| - { |
35 |
| - name: 'output-path', |
36 |
| - type: 'Path', |
37 |
| - aliases: ['op'], |
38 |
| - description: 'Path where output will be placed.' |
39 |
| - }, |
40 |
| - { |
41 |
| - name: 'aot', |
42 |
| - type: Boolean, |
43 |
| - description: 'Build using Ahead of Time compilation.' |
44 |
| - }, |
45 |
| - { |
46 |
| - name: 'sourcemaps', |
47 |
| - type: Boolean, |
48 |
| - aliases: ['sm', 'sourcemap'], |
49 |
| - description: 'Output sourcemaps.', |
50 |
| - default: buildConfigDefaults['sourcemaps'] |
51 |
| - }, |
52 |
| - { |
53 |
| - name: 'vendor-chunk', |
54 |
| - type: Boolean, |
55 |
| - aliases: ['vc'], |
56 |
| - description: 'Use a separate bundle containing only vendor libraries.' |
57 |
| - }, |
58 |
| - { |
59 |
| - name: 'common-chunk', |
60 |
| - type: Boolean, |
61 |
| - default: buildConfigDefaults['commonChunk'], |
62 |
| - aliases: ['cc'], |
63 |
| - description: 'Use a separate bundle containing code used across multiple bundles.' |
64 |
| - }, |
65 |
| - { |
66 |
| - name: 'base-href', |
67 |
| - type: String, |
68 |
| - aliases: ['bh'], |
69 |
| - description: 'Base url for the application being built.', |
70 |
| - default: buildConfigDefaults['baseHref'] |
71 |
| - }, |
72 |
| - { |
73 |
| - name: 'deploy-url', |
74 |
| - type: String, |
75 |
| - aliases: ['d'], |
76 |
| - description: 'URL where files will be deployed.' |
77 |
| - }, |
78 |
| - { |
79 |
| - name: 'verbose', |
80 |
| - type: Boolean, |
81 |
| - default: false, |
82 |
| - aliases: ['v'], |
83 |
| - description: 'Adds more details to output logging.' |
84 |
| - }, |
85 |
| - { |
86 |
| - name: 'progress', |
87 |
| - type: Boolean, |
88 |
| - aliases: ['pr'], |
89 |
| - description: 'Log progress to the console while building.', |
90 |
| - default: typeof buildConfigDefaults['progress'] !== 'undefined' |
91 |
| - ? buildConfigDefaults['progress'] |
92 |
| - : process.stdout.isTTY === true |
93 |
| - }, |
94 |
| - { |
95 |
| - name: 'i18n-file', |
96 |
| - type: String, |
97 |
| - description: 'Localization file to use for i18n.' |
98 |
| - }, |
99 |
| - { |
100 |
| - name: 'i18n-format', |
101 |
| - type: String, |
102 |
| - description: 'Format of the localization file specified with --i18n-file.' |
103 |
| - }, |
104 |
| - { |
105 |
| - name: 'locale', |
106 |
| - type: String, |
107 |
| - description: 'Locale to use for i18n.' |
108 |
| - }, |
109 |
| - { |
110 |
| - name: 'missing-translation', |
111 |
| - type: String, |
112 |
| - description: 'How to handle missing translations for i18n.' |
113 |
| - }, |
114 |
| - { |
115 |
| - name: 'extract-css', |
116 |
| - type: Boolean, |
117 |
| - aliases: ['ec'], |
118 |
| - description: 'Extract css from global styles onto css files instead of js ones.' |
119 |
| - }, |
120 |
| - { |
121 |
| - name: 'watch', |
122 |
| - type: Boolean, |
123 |
| - default: false, |
124 |
| - aliases: ['w'], |
125 |
| - description: 'Run build when files change.' |
126 |
| - }, |
127 |
| - { |
128 |
| - name: 'output-hashing', |
129 |
| - type: String, |
130 |
| - values: ['none', 'all', 'media', 'bundles'], |
131 |
| - description: 'Define the output filename cache-busting hashing mode.', |
132 |
| - aliases: ['oh'] |
133 |
| - }, |
134 |
| - { |
135 |
| - name: 'poll', |
136 |
| - type: Number, |
137 |
| - description: 'Enable and define the file watching poll time period (milliseconds).', |
138 |
| - default: buildConfigDefaults['poll'] |
139 |
| - }, |
140 |
| - { |
141 |
| - name: 'app', |
142 |
| - type: String, |
143 |
| - aliases: ['a'], |
144 |
| - description: 'Specifies app name or index to use.' |
145 |
| - }, |
146 |
| - { |
147 |
| - name: 'delete-output-path', |
148 |
| - type: Boolean, |
149 |
| - aliases: ['dop'], |
150 |
| - description: 'Delete output path before build.', |
151 |
| - default: buildConfigDefaults['deleteOutputPath'], |
152 |
| - }, |
153 |
| - { |
154 |
| - name: 'preserve-symlinks', |
155 |
| - type: Boolean, |
156 |
| - description: 'Do not use the real path when resolving modules.', |
157 |
| - default: buildConfigDefaults['preserveSymlinks'] |
158 |
| - }, |
159 |
| - { |
160 |
| - name: 'extract-licenses', |
161 |
| - type: Boolean, |
162 |
| - description: 'Extract all licenses in a separate file, in the case of production builds only.' |
163 |
| - }, |
164 |
| - { |
165 |
| - name: 'show-circular-dependencies', |
166 |
| - type: Boolean, |
167 |
| - aliases: ['scd'], |
168 |
| - description: 'Show circular dependency warnings on builds.', |
169 |
| - default: buildConfigDefaults['showCircularDependencies'] |
170 |
| - }, |
171 |
| - { |
172 |
| - name: 'build-optimizer', |
173 |
| - type: Boolean, |
174 |
| - description: 'Enables @angular-devkit/build-optimizer optimizations when using `--aot`.' |
175 |
| - }, |
176 |
| - { |
177 |
| - name: 'named-chunks', |
178 |
| - type: Boolean, |
179 |
| - aliases: ['nc'], |
180 |
| - description: 'Use file name for lazy loaded chunks.', |
181 |
| - default: buildConfigDefaults['namedChunks'] |
182 |
| - }, |
183 |
| - { |
184 |
| - name: 'subresource-integrity', |
185 |
| - type: Boolean, |
186 |
| - default: false, |
187 |
| - aliases: ['sri'], |
188 |
| - description: 'Enables the use of subresource integrity validation.' |
189 |
| - }, |
190 |
| - { |
191 |
| - name: 'bundle-dependencies', |
192 |
| - type: ['none', 'all'], |
193 |
| - default: 'none', |
194 |
| - description: 'Available on server platform only. Which external dependencies to bundle into ' |
195 |
| - + 'the module. By default, all of node_modules will be kept as requires.' |
196 |
| - }, |
197 |
| - { |
198 |
| - name: 'service-worker', |
199 |
| - type: Boolean, |
200 |
| - default: true, |
201 |
| - aliases: ['sw'], |
202 |
| - description: 'Generates a service worker config for production builds, if the app has ' |
203 |
| - + 'service worker enabled.' |
204 |
| - }, |
205 |
| - { |
206 |
| - name: 'skip-app-shell', |
207 |
| - type: Boolean, |
208 |
| - description: 'Flag to prevent building an app shell', |
209 |
| - default: false |
210 |
| - } |
211 |
| -]; |
212 |
| - |
213 |
| -export interface BuildTaskOptions extends BuildOptions { |
214 |
| - statsJson?: boolean; |
| 5 | +export interface Options { |
| 6 | + app?: string; |
| 7 | + configuration?: string; |
| 8 | + prod: boolean; |
215 | 9 | }
|
216 | 10 |
|
217 |
| -export default class BuildCommand extends Command { |
| 11 | +export default class BuildCommand extends ArchitectCommand { |
218 | 12 | public readonly name = 'build';
|
| 13 | + public readonly target = 'browser'; |
219 | 14 | public readonly description =
|
220 | 15 | 'Builds your app and places it into the output path (dist/ by default).';
|
221 | 16 | public static aliases = ['b'];
|
222 | 17 | public scope = CommandScope.inProject;
|
223 |
| - public arguments: string[]; |
224 |
| - public options = baseBuildCommandOptions.concat([ |
225 |
| - { |
226 |
| - name: 'stats-json', |
227 |
| - type: Boolean, |
228 |
| - default: false, |
229 |
| - description: oneLine`Generates a \`stats.json\` file which can be analyzed using tools |
230 |
| - such as: \`webpack-bundle-analyzer\` or https://webpack.github.io/analyse.` |
231 |
| - } |
232 |
| - ]); |
| 18 | + public arguments: string[] = ['app']; |
| 19 | + public options: Option[] = [ |
| 20 | + this.prodOption, |
| 21 | + this.configurationOption |
| 22 | + ]; |
233 | 23 |
|
234 |
| - public validate(_options: BuildTaskOptions) { |
| 24 | + public validate(_options: Options) { |
235 | 25 | Version.assertAngularVersionIs2_3_1OrHigher(this.project.root);
|
236 | 26 | Version.assertTypescriptVersion(this.project.root);
|
237 | 27 | return true;
|
238 | 28 | }
|
239 | 29 |
|
240 |
| - public async run(options: BuildTaskOptions) { |
241 |
| - // Add trailing slash if missing to prevent https://github.com/angular/angular-cli/issues/7295 |
242 |
| - if (options.deployUrl && options.deployUrl.substr(-1) !== '/') { |
243 |
| - options.deployUrl += '/'; |
244 |
| - } |
245 |
| - |
246 |
| - const BuildTask = require('../tasks/build').default; |
247 |
| - |
248 |
| - const buildTask = new BuildTask({ |
249 |
| - project: this.project, |
250 |
| - ui: this.ui, |
251 |
| - }); |
252 |
| - |
253 |
| - const clientApp = getAppFromConfig(options.app); |
254 |
| - |
255 |
| - const doAppShell = options.target === 'production' && |
256 |
| - (options.aot === undefined || options.aot === true) && |
257 |
| - !options.skipAppShell; |
258 |
| - |
259 |
| - let serverApp: any = null; |
260 |
| - if (clientApp.appShell && doAppShell) { |
261 |
| - serverApp = getAppFromConfig(clientApp.appShell.app); |
262 |
| - if (serverApp.platform !== 'server') { |
263 |
| - throw new SilentError(`Shell app's platform is not "server"`); |
264 |
| - } |
265 |
| - } |
266 |
| - |
267 |
| - const buildTaskResult = await buildTask.run(options); |
268 |
| - if (!clientApp.appShell || !doAppShell) { |
269 |
| - return buildTaskResult; |
| 30 | + public async run(options: Options) { |
| 31 | + let configuration = options.configuration; |
| 32 | + if (!configuration && options.prod) { |
| 33 | + configuration = 'production'; |
270 | 34 | }
|
271 |
| - |
272 |
| - const serverOptions = { |
273 |
| - ...options, |
274 |
| - app: clientApp.appShell.app |
275 |
| - }; |
276 |
| - await buildTask.run(serverOptions); |
277 |
| - |
278 |
| - const RenderUniversalTask = require('../tasks/render-universal').default; |
279 |
| - |
280 |
| - const renderUniversalTask = new RenderUniversalTask({ |
281 |
| - project: this.project, |
282 |
| - ui: this.ui, |
| 35 | + const overrides = {...options}; |
| 36 | + delete overrides.app; |
| 37 | + delete overrides.prod; |
| 38 | + return this.runArchitect({ |
| 39 | + app: options.app, |
| 40 | + configuration, |
| 41 | + overrides |
283 | 42 | });
|
284 |
| - const renderUniversalOptions: RenderUniversalTaskOptions = { |
285 |
| - inputIndexPath: join(this.project.root, clientApp.outDir, clientApp.index), |
286 |
| - route: clientApp.appShell.route, |
287 |
| - serverOutDir: join(this.project.root, serverApp.outDir), |
288 |
| - outputIndexPath: join(this.project.root, clientApp.outDir, clientApp.index) |
289 |
| - }; |
290 |
| - |
291 |
| - return await renderUniversalTask.run(renderUniversalOptions); |
292 | 43 | }
|
293 | 44 | }
|
0 commit comments