Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ import {

function* updateBuildTarget(
projectName: string,
projectRoot: string,
buildTarget: TargetDefinition,
serverTarget: TargetDefinition | undefined,
tree: Tree,
context: SchematicContext,
): Iterable<Rule> {
// Update builder target and options
buildTarget.builder = Builders.Application;
const baseNgswConfigPath = buildTarget.options?.['ngswConfigPath'];

for (const [, options] of allTargetOptions(buildTarget, false)) {
if (options['index'] === '') {
Expand All @@ -57,7 +59,13 @@ function* updateBuildTarget(
if (serverTarget && typeof options['browser'] === 'string') {
options['server'] = dirname(options['browser']) + '/main.server.ts';
}
options['serviceWorker'] = options['ngswConfigPath'] ?? options['serviceWorker'];
if (options['serviceWorker'] === true) {
// The application builder only accepts a path, while the browser builder defaults it in code.
options['serviceWorker'] =
options['ngswConfigPath'] ?? baseNgswConfigPath ?? join(projectRoot, 'ngsw-config.json');
} else {
options['serviceWorker'] = options['ngswConfigPath'] ?? options['serviceWorker'];
}
Comment thread
alan-agius4 marked this conversation as resolved.

if (typeof options['polyfills'] === 'string') {
options['polyfills'] = [options['polyfills']];
Expand Down Expand Up @@ -197,7 +205,9 @@ function updateProjects(tree: Tree, context: SchematicContext) {

const serverTarget = project.targets.get('server');

rules.push(...updateBuildTarget(name, buildTarget, serverTarget, tree, context));
rules.push(
...updateBuildTarget(name, project.root, buildTarget, serverTarget, tree, context),
);

// Delete all redundant targets
for (const [key, target] of project.targets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,40 @@ describe(`Migration to use the application builder`, () => {
});
});

it(`should replace 'serviceWorker: true' with the default 'ngsw-config.json' path`, async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const workspaceContent = tree.readJson('/angular.json') as Record<string, any>;
workspaceContent['projects']['app']['architect']['build']['options']['serviceWorker'] = true;
tree.overwrite('/angular.json', JSON.stringify(workspaceContent));

const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));

const { serviceWorker, ngswConfigPath } = app.architect['build'].options;
expect(serviceWorker).toBe('/project/app/ngsw-config.json');
expect(ngswConfigPath).toBeUndefined();
});

it(`should replace 'serviceWorker: true' in a configuration with the base 'ngswConfigPath'`, async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const workspaceContent = tree.readJson('/angular.json') as Record<string, any>;
const build = workspaceContent['projects']['app']['architect']['build'];
build['options']['ngswConfigPath'] = 'src/ngsw-config.json';
build['configurations'] = { production: { serviceWorker: true } };
tree.overwrite('/angular.json', JSON.stringify(workspaceContent));

const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { app },
} = JSON.parse(newTree.readContent('/angular.json'));

expect(app.architect['build'].configurations.production.serviceWorker).toBe(
'src/ngsw-config.json',
);
});

it(`should remove 'builderMode' from karma options`, async () => {
addWorkspaceTarget(tree, 'test', {
'builder': Builders.Karma,
Expand Down
Loading