Skip to content

Commit d1953bf

Browse files
gkalpakclydin
authored andcommitted
fix(@angular-devkit/build-angular): correctly generate ServiceWorker config on Windows
Since #20518, the generation of the ServiceWorker configuration has been broken on Windows. The reason is the use of `path.posix.*` methods on non-POSIX paths, resulting in broken paths. I.e. we ended up with something like the following: ```js path.posix.relative('C:\\foo', 'C:\\foo\\bar/baz'); // Expected result: `bar/baz` // Actual result: `../C:\\foo\\bar/baz` ``` This caused the config generator to fail to find any files and thus fail to populate the config with cacheable assets. This commit fixes this by using platform-specific `path.*` methods for path manipulation and manually normalizing the path separators before returning the results. Fixes #20894
1 parent 1878411 commit d1953bf

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

packages/angular_devkit/build_angular/src/utils/service-worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CliFilesystem implements Filesystem {
4646
for await (const entry of await fs.opendir(dir)) {
4747
if (entry.isFile()) {
4848
// Uses posix paths since the service worker expects URLs
49-
items.push('/' + path.posix.relative(this.base, path.posix.join(dir, entry.name)));
49+
items.push('/' + path.relative(this.base, path.join(dir, entry.name)).replace(/\\/g, '/'));
5050
} else if (entry.isDirectory()) {
5151
subdirectories.push(path.join(dir, entry.name));
5252
}

tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,39 @@ export default async function () {
99
await expectFileToExist(join(process.cwd(), 'src/manifest.webmanifest'));
1010

1111
// Angular PWA doesn't install as a dependency
12-
const { dependencies, devDependencies } = JSON.parse(await readFile(join(process.cwd(), 'package.json')));
13-
const hasPWADep = Object.keys({ ...dependencies, ...devDependencies })
14-
.some(d => d === '@angular/pwa');
12+
const { dependencies, devDependencies } = JSON.parse(
13+
await readFile(join(process.cwd(), 'package.json')),
14+
);
15+
const hasPWADep = Object.keys({ ...dependencies, ...devDependencies }).some(
16+
(d) => d === '@angular/pwa',
17+
);
1518
if (hasPWADep) {
1619
throw new Error(`Expected 'package.json' not to contain a dependency on '@angular/pwa'.`);
1720
}
21+
22+
// It should generate a SW configuration file (`ngsw.json`).
23+
const workspaceJson = JSON.parse(await readFile('angular.json'));
24+
const outputPath = workspaceJson.projects['test-project'].architect.build.options.outputPath;
25+
const ngswPath = join(process.cwd(), outputPath, 'ngsw.json');
26+
27+
await ng('build');
28+
await expectFileToExist(ngswPath);
29+
30+
// It should correctly generate assetGroups and include at least one URL in each group.
31+
const ngswJson = JSON.parse(await readFile(ngswPath));
32+
const assetGroups = ngswJson.assetGroups.map(({ name, urls }) => ({
33+
name,
34+
urlCount: urls.length,
35+
}));
36+
const emptyAssetGroups = assetGroups.filter(({ urlCount }) => urlCount === 0);
37+
38+
if (assetGroups.length === 0) {
39+
throw new Error("Expected 'ngsw.json' to contain at least one asset-group.");
40+
}
41+
if (emptyAssetGroups.length > 0) {
42+
throw new Error(
43+
'Expected all asset-groups to contain at least one URL, but the following groups are empty: ' +
44+
emptyAssetGroups.map(({ name }) => name).join(', '),
45+
);
46+
}
1847
}

0 commit comments

Comments
 (0)