refactor(@angular/cli): remove deprecated --prod flag

BREAKING CHANGE:

Deprecated option `--prod` has been removed from all builders. `--configuration production`/`-c production` should be used instead if the default configuration of the builder is not configured to `production`.
This commit is contained in:
Alan Agius 2022-01-28 12:07:31 +01:00 committed by Douglas Parker
parent 69ecddaa7d
commit 2fc7c73d7e
9 changed files with 232 additions and 218 deletions

View File

@ -14,14 +14,9 @@
}
},
"configuration": {
"description": "One or more named builder configurations as a comma-separated list as specified in the \"configurations\" section of angular.json.\nThe builder uses the named configurations to run the given target.\nFor more information, see https://angular.io/guide/workspace-config#alternate-build-configurations.\nSetting this explicitly overrides the \"--prod\" flag.",
"description": "One or more named builder configurations as a comma-separated list as specified in the \"configurations\" section of angular.json.\nThe builder uses the named configurations to run the given target.\nFor more information, see https://angular.io/guide/workspace-config#alternate-build-configurations.",
"type": "string",
"aliases": ["c"]
},
"prod": {
"description": "Shorthand for \"--configuration=production\".\nSet the build configuration to the production target.\nBy default, the production target is set up in the workspace configuration such that all builds make use of bundling, limited tree-shaking, and also limited dead code elimination.",
"type": "boolean",
"x-deprecated": "Use `--configuration production` instead."
}
}
},

View File

@ -422,21 +422,6 @@ export abstract class ArchitectCommand<
} else {
project = commandOptions.project;
target = this.target;
if (commandOptions.prod) {
const defaultConfig =
project &&
target &&
this.workspace?.projects.get(project)?.targets.get(target)?.defaultConfiguration;
this.logger.warn(
defaultConfig === 'production'
? 'Option "--prod" is deprecated: No need to use this option as this builder defaults to configuration "production".'
: 'Option "--prod" is deprecated: Use "--configuration production" instead.',
);
// The --prod flag will always be the first configuration, available to be overwritten
// by following configurations.
configuration = 'production';
}
if (commandOptions.configuration) {
configuration = `${configuration ? `${configuration},` : ''}${
commandOptions.configuration

View File

@ -12,7 +12,7 @@ Run `ng generate component component-name` to generate a new component. You can
## Build
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory.
## Running unit tests

View File

@ -16,7 +16,9 @@ export default async function () {
// Add app routing.
// This is done automatically on a new app with --routing.
await writeFile(appRoutingModulePath, `
await writeFile(
appRoutingModulePath,
`
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
@ -27,18 +29,24 @@ export default async function () {
exports: [RouterModule]
})
export class AppRoutingModule { }
`);
await prependToFile('src/app/app.module.ts',
`import { AppRoutingModule } from './app-routing.module';`);
`,
);
await prependToFile(
'src/app/app.module.ts',
`import { AppRoutingModule } from './app-routing.module';`,
);
await replaceInFile('src/app/app.module.ts', `imports: [`, `imports: [ AppRoutingModule,`);
await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>');
const originalAppRoutingModule = await readFile(appRoutingModulePath);
// helper to replace loadChildren
const replaceLoadChildren = async (route: string) => {
const content = originalAppRoutingModule.replace('const routes: Routes = [];', `
const content = originalAppRoutingModule.replace(
'const routes: Routes = [];',
`
const routes: Routes = [{ path: 'lazy', loadChildren: ${route} }];
`);
`,
);
return writeFile(appRoutingModulePath, content);
};
@ -46,13 +54,19 @@ export default async function () {
// Add lazy route.
await ng('generate', 'module', 'lazy', '--routing');
await ng('generate', 'component', 'lazy/lazy-comp');
await replaceInFile('src/app/lazy/lazy-routing.module.ts', 'const routes: Routes = [];', `
await replaceInFile(
'src/app/lazy/lazy-routing.module.ts',
'const routes: Routes = [];',
`
import { LazyCompComponent } from './lazy-comp/lazy-comp.component';
const routes: Routes = [{ path: '', component: LazyCompComponent }];
`);
`,
);
// Add lazy route e2e
await writeFile('e2e/src/app.e2e-spec.ts', `
await writeFile(
'e2e/src/app.e2e-spec.ts',
`
import { browser, logging, element, by } from 'protractor';
describe('workspace-project App', () => {
@ -69,11 +83,12 @@ export default async function () {
}));
});
});
`);
`,
);
// Convert the default config to use JIT and prod to just do AOT.
// This way we can use `ng e2e` to test JIT and `ng e2e --prod` to test AOT.
await updateJsonFile('angular.json', json => {
// This way we can use `ng e2e` to test JIT and `ng e2e --configuration=production` to test AOT.
await updateJsonFile('angular.json', (json) => {
const buildTarget = json['projects'][projectName]['architect']['build'];
buildTarget['options']['aot'] = true;
buildTarget['configurations']['development']['aot'] = false;

View File

@ -7,7 +7,7 @@ import { isPrereleaseCli, updateJsonFile } from '../../utils/project';
const snapshots = require('../../ng-snapshot/package.json');
export default async function () {
const tag = await isPrereleaseCli() ? '@next' : '';
const tag = (await isPrereleaseCli()) ? '@next' : '';
await ng('add', `@angular/material${tag}`, '--skip-confirmation');
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
@ -72,5 +72,5 @@ export default async function () {
`,
);
await ng('e2e', '--prod');
await ng('e2e', '--configuration=production');
}

View File

@ -15,7 +15,11 @@ export default async function() {
let error;
try {
await execAndWaitForOutputToMatch('ng', ['serve', '--prod'], webpackGoodRegEx);
await execAndWaitForOutputToMatch(
'ng',
['serve', '--configuration=production'],
webpackGoodRegEx,
);
await wait(4000);

View File

@ -3,17 +3,19 @@ import { installWorkspacePackages } from '../../utils/packages';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
export default function () {
return updateJsonFile('package.json', packageJson => {
export default async function () {
await updateJsonFile('package.json', (packageJson) => {
// Install ngrx
packageJson['dependencies']['@ngrx/effects'] = '^9.1.0';
packageJson['dependencies']['@ngrx/schematics'] = '^9.1.0';
packageJson['dependencies']['@ngrx/store'] = '^9.1.0';
packageJson['dependencies']['@ngrx/store-devtools'] = '^9.1.0';
})
.then(() => installWorkspacePackages())
});
await installWorkspacePackages();
// Create an app that uses ngrx decorators and has e2e tests.
.then(_ => writeMultipleFiles({
await writeMultipleFiles({
'./e2e/src/app.po.ts': `
import { browser, by, element } from 'protractor';
export class AppPage {
@ -155,7 +157,8 @@ export default function () {
}
}
`,
}))
// Run the e2e tests against a prod build.
.then(() => ng('e2e', '--prod'));
});
// Run the e2e tests against a production build.
await ng('e2e', '--configuration=production');
}

View File

@ -4,7 +4,10 @@ import { updateJsonFile } from '../../utils/project';
export default function () {
// Tests run in 'dev' environment by default.
return writeFile('src/app/environment.spec.ts', `
return (
writeFile(
'src/app/environment.spec.ts',
`
import { environment } from '../environments/environment';
describe('Test environment', () => {
@ -12,9 +15,11 @@ export default function () {
expect(environment.production).toBe(false);
});
});
`)
`,
)
.then(() => ng('test', '--watch=false'))
.then(() => updateJsonFile('angular.json', configJson => {
.then(() =>
updateJsonFile('angular.json', (configJson) => {
const appArchitect = configJson.projects['test-project'].architect;
appArchitect.test.configurations = {
production: {
@ -22,14 +27,18 @@ export default function () {
{
src: 'src/environments/environment.ts',
replaceWith: 'src/environments/environment.prod.ts',
}
},
],
}
},
};
}))
}),
)
// Tests can run in different environment.
.then(() => writeFile('src/app/environment.spec.ts', `
.then(() =>
writeFile(
'src/app/environment.spec.ts',
`
import { environment } from '../environments/environment';
describe('Test environment', () => {
@ -37,6 +46,9 @@ export default function () {
expect(environment.production).toBe(true);
});
});
`))
.then(() => ng('test', '--prod', '--watch=false'));
`,
),
)
.then(() => ng('test', '--configuration=production', '--watch=false'))
);
}

View File

@ -49,9 +49,9 @@ export default async function () {
await ng('generate', 'component', 'my-comp');
await ng('test', '--watch=false');
await ng('e2e');
await ng('e2e', '--prod');
await ng('e2e', '--configuration=production');
// Verify project now creates bundles
await noSilentNg('build', '--prod');
await noSilentNg('build', '--configuration=production');
await expectFileMatchToExist('dist/nine-project/', /main\.[0-9a-f]{16}\.js/);
}