diff --git a/packages/schematics/angular/migrations/update-6/index.ts b/packages/schematics/angular/migrations/update-6/index.ts index c597044129b3..b879e955bc67 100644 --- a/packages/schematics/angular/migrations/update-6/index.ts +++ b/packages/schematics/angular/migrations/update-6/index.ts @@ -292,11 +292,25 @@ function extractProjectsConfig( const environments = app.environments; const serviceWorker = app.serviceWorker; + const productionPartial = { + optimization: true, + outputHashing: 'all', + sourceMap: false, + extractCss: true, + namedChunks: false, + aot: true, + extractLicenses: true, + vendorChunk: false, + buildOptimizer: true, + ...(serviceWorker ? {serviceWorker: true, ngswConfigPath: '/src/ngsw-config.json'} : {}), + ...(app.budgets ? { budgets: app.budgets as JsonArray} : {}), + }; + if (!environments) { - return {}; + return { production: productionPartial }; } - return Object.keys(environments).reduce((acc, environment) => { + const configurations = Object.keys(environments).reduce((acc, environment) => { if (source === environments[environment]) { return acc; } @@ -319,31 +333,8 @@ function extractProjectsConfig( configurationName = environment; } - let swConfig: JsonObject | null = null; - if (serviceWorker) { - swConfig = { - serviceWorker: true, - ngswConfigPath: '/src/ngsw-config.json', - }; - } - acc[configurationName] = { - ...(isProduction - ? { - optimization: true, - outputHashing: 'all', - sourceMap: false, - extractCss: true, - namedChunks: false, - aot: true, - extractLicenses: true, - vendorChunk: false, - buildOptimizer: true, - } - : {} - ), - ...(isProduction && swConfig ? swConfig : {}), - ...(isProduction && app.budgets ? { budgets: app.budgets as JsonArray } : {}), + ...(isProduction ? productionPartial : {}), fileReplacements: [ { replace: `${app.root}/${source}`, @@ -354,6 +345,12 @@ function extractProjectsConfig( return acc; }, {} as JsonObject); + + if (!configurations['production']) { + configurations['production'] = { ...productionPartial }; + } + + return configurations; } function _serveConfigurations(): JsonObject { diff --git a/packages/schematics/angular/migrations/update-6/index_spec.ts b/packages/schematics/angular/migrations/update-6/index_spec.ts index d2627791ee08..556826ad6ead 100644 --- a/packages/schematics/angular/migrations/update-6/index_spec.ts +++ b/packages/schematics/angular/migrations/update-6/index_spec.ts @@ -584,6 +584,52 @@ describe('Migration to v6', () => { ).toBe(true); }); + it('should add production configuration when no environments', () => { + delete baseConfig.apps[0].environments; + tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2)); + tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree); + const config = getConfig(tree); + expect(config.projects.foo.architect.build.configurations).toEqual({ + production: { + optimization: true, + outputHashing: 'all', + sourceMap: false, + extractCss: true, + namedChunks: false, + aot: true, + extractLicenses: true, + vendorChunk: false, + buildOptimizer: true, + }, + }); + }); + + it('should add production configuration when no production environment', () => { + tree.delete('/src/environments/environment.prod.ts'); + tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2)); + tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree); + const config = getConfig(tree); + expect(config.projects.foo.architect.build.configurations).toEqual({ + prod: { + fileReplacements: [{ + replace: 'src/environments/environment.ts', + with: 'src/environments/environment.prod.ts', + }], + }, + production: { + optimization: true, + outputHashing: 'all', + sourceMap: false, + extractCss: true, + namedChunks: false, + aot: true, + extractLicenses: true, + vendorChunk: false, + buildOptimizer: true, + }, + }); + }); + it('should set the serve target', () => { tree.create(oldConfigPath, JSON.stringify(baseConfig, null, 2)); tree = schematicRunner.runSchematic('migration-01', defaultOptions, tree);