Skip to content

fix(@schematics/angular): ensure production configuration when migrating #11438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions packages/schematics/angular/migrations/update-6/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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}`,
Expand All @@ -354,6 +345,12 @@ function extractProjectsConfig(

return acc;
}, {} as JsonObject);

if (!configurations['production']) {
configurations['production'] = { ...productionPartial };
}

return configurations;
}

function _serveConfigurations(): JsonObject {
Expand Down
46 changes: 46 additions & 0 deletions packages/schematics/angular/migrations/update-6/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down