Skip to content

Commit c3ad789

Browse files
committed
fix(@schematics/angular): ensure production configuration when migrating
Fixes: #11431
1 parent 79a0c41 commit c3ad789

File tree

2 files changed

+69
-26
lines changed

2 files changed

+69
-26
lines changed

packages/schematics/angular/migrations/update-6/index.ts

+23-26
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,25 @@ function extractProjectsConfig(
292292
const environments = app.environments;
293293
const serviceWorker = app.serviceWorker;
294294

295+
const productionPartial = {
296+
optimization: true,
297+
outputHashing: 'all',
298+
sourceMap: false,
299+
extractCss: true,
300+
namedChunks: false,
301+
aot: true,
302+
extractLicenses: true,
303+
vendorChunk: false,
304+
buildOptimizer: true,
305+
...(serviceWorker ? {serviceWorker: true, ngswConfigPath: '/src/ngsw-config.json'} : {}),
306+
...(app.budgets ? { budgets: app.budgets as JsonArray} : {}),
307+
};
308+
295309
if (!environments) {
296-
return {};
310+
return { production: productionPartial };
297311
}
298312

299-
return Object.keys(environments).reduce((acc, environment) => {
313+
const configurations = Object.keys(environments).reduce((acc, environment) => {
300314
if (source === environments[environment]) {
301315
return acc;
302316
}
@@ -319,31 +333,8 @@ function extractProjectsConfig(
319333
configurationName = environment;
320334
}
321335

322-
let swConfig: JsonObject | null = null;
323-
if (serviceWorker) {
324-
swConfig = {
325-
serviceWorker: true,
326-
ngswConfigPath: '/src/ngsw-config.json',
327-
};
328-
}
329-
330336
acc[configurationName] = {
331-
...(isProduction
332-
? {
333-
optimization: true,
334-
outputHashing: 'all',
335-
sourceMap: false,
336-
extractCss: true,
337-
namedChunks: false,
338-
aot: true,
339-
extractLicenses: true,
340-
vendorChunk: false,
341-
buildOptimizer: true,
342-
}
343-
: {}
344-
),
345-
...(isProduction && swConfig ? swConfig : {}),
346-
...(isProduction && app.budgets ? { budgets: app.budgets as JsonArray } : {}),
337+
...(isProduction ? productionPartial : {}),
347338
fileReplacements: [
348339
{
349340
replace: `${app.root}/${source}`,
@@ -354,6 +345,12 @@ function extractProjectsConfig(
354345

355346
return acc;
356347
}, {} as JsonObject);
348+
349+
if (!configurations['production']) {
350+
configurations['production'] = { ...productionPartial };
351+
}
352+
353+
return configurations;
357354
}
358355

359356
function _serveConfigurations(): JsonObject {

packages/schematics/angular/migrations/update-6/index_spec.ts

+46
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,52 @@ describe('Migration to v6', () => {
584584
).toBe(true);
585585
});
586586

587+
it('should add production configuration when no environments', () => {
588+
delete baseConfig.apps[0].environments;
589+
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
590+
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
591+
const config = getConfig(tree);
592+
expect(config.projects.foo.architect.build.configurations).toEqual({
593+
production: {
594+
optimization: true,
595+
outputHashing: 'all',
596+
sourceMap: false,
597+
extractCss: true,
598+
namedChunks: false,
599+
aot: true,
600+
extractLicenses: true,
601+
vendorChunk: false,
602+
buildOptimizer: true,
603+
},
604+
});
605+
});
606+
607+
it('should add production configuration when no production environment', () => {
608+
tree.delete('/src/environments/environment.prod.ts');
609+
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
610+
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);
611+
const config = getConfig(tree);
612+
expect(config.projects.foo.architect.build.configurations).toEqual({
613+
prod: {
614+
fileReplacements: [{
615+
replace: 'src/environments/environment.ts',
616+
with: 'src/environments/environment.prod.ts',
617+
}],
618+
},
619+
production: {
620+
optimization: true,
621+
outputHashing: 'all',
622+
sourceMap: false,
623+
extractCss: true,
624+
namedChunks: false,
625+
aot: true,
626+
extractLicenses: true,
627+
vendorChunk: false,
628+
buildOptimizer: true,
629+
},
630+
});
631+
});
632+
587633
it('should set the serve target', () => {
588634
tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2));
589635
tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);

0 commit comments

Comments
 (0)