-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@schematics/angular): provide migration that disables build optim…
…izer on dev server builds In #25070 we turned on `buildOptimizer` by default for server builds. This causes existing projects development builds to always run build-optimizer. This migration will set `buildOptimizer` to false, when `optimization` is disabled. (cherry picked from commit 873272f)
- Loading branch information
1 parent
35c02ca
commit b2ed7bd
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/schematics/angular/migrations/update-16/update-server-builder-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { Rule } from '@angular-devkit/schematics'; | ||
import { allTargetOptions, updateWorkspace } from '../../utility/workspace'; | ||
import { Builders } from '../../utility/workspace-models'; | ||
|
||
export default function (): Rule { | ||
return updateWorkspace((workspace) => { | ||
for (const project of workspace.projects.values()) { | ||
for (const target of project.targets.values()) { | ||
if (target.builder !== Builders.Server) { | ||
continue; | ||
} | ||
|
||
for (const [, options] of allTargetOptions(target)) { | ||
// Set 'buildOptimizer' to match the 'optimization' option. | ||
if (options.buildOptimizer === undefined && options.optimization !== undefined) { | ||
options.buildOptimizer = !!options.optimization; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} |
91 changes: 91 additions & 0 deletions
91
packages/schematics/angular/migrations/update-16/update-server-builder-config_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { JsonObject } from '@angular-devkit/core'; | ||
import { EmptyTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import { | ||
BuilderTarget, | ||
Builders, | ||
ProjectType, | ||
WorkspaceSchema, | ||
} from '../../utility/workspace-models'; | ||
|
||
function getServerTarget(tree: UnitTestTree): BuilderTarget<Builders.Server, JsonObject> { | ||
const target = (tree.readJson('/angular.json') as unknown as WorkspaceSchema).projects.app | ||
.architect?.server; | ||
|
||
return target as unknown as BuilderTarget<Builders.Server, JsonObject>; | ||
} | ||
|
||
function createWorkSpaceConfig(tree: UnitTestTree) { | ||
const angularConfig: WorkspaceSchema = { | ||
version: 1, | ||
projects: { | ||
app: { | ||
root: '', | ||
sourceRoot: 'src', | ||
projectType: ProjectType.Application, | ||
prefix: 'app', | ||
architect: { | ||
server: { | ||
builder: Builders.Server, | ||
options: { | ||
main: './server.ts', | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any, | ||
configurations: { | ||
development: { | ||
optimization: false, | ||
}, | ||
production: { | ||
optimization: true, | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); | ||
} | ||
|
||
const schematicName = 'update-server-builder-config'; | ||
|
||
describe(`Migration to update '@angular-devkit/build-angular:server' options. ${schematicName}`, () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
beforeEach(() => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
createWorkSpaceConfig(tree); | ||
}); | ||
|
||
it(`should add 'buildOptimizer: false' to 'optimization' is 'false'`, async () => { | ||
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
const { configurations } = getServerTarget(newTree); | ||
expect(configurations?.development.buildOptimizer).toBeFalse(); | ||
}); | ||
|
||
it(`should not add 'buildOptimizer' option when to 'optimization' is not defined.`, async () => { | ||
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
const { options } = getServerTarget(newTree); | ||
expect(options.buildOptimizer).toBeUndefined(); | ||
}); | ||
|
||
it(`should add 'buildOptimizer: true' to 'optimization' is 'true'`, async () => { | ||
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree); | ||
const { configurations } = getServerTarget(newTree); | ||
expect(configurations?.production.buildOptimizer).toBeTrue(); | ||
}); | ||
}); |