Skip to content

fix(@angular-devkit/build-angular): correctly generate ServiceWorker config on Windows #20897

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
May 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CliFilesystem implements Filesystem {
for await (const entry of await fs.opendir(dir)) {
if (entry.isFile()) {
// Uses posix paths since the service worker expects URLs
items.push('/' + path.posix.relative(this.base, path.posix.join(dir, entry.name)));
items.push('/' + path.relative(this.base, path.join(dir, entry.name)).replace(/\\/g, '/'));
} else if (entry.isDirectory()) {
subdirectories.push(path.join(dir, entry.name));
}
Expand Down
35 changes: 32 additions & 3 deletions tests/legacy-cli/e2e/tests/commands/add/add-pwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,39 @@ export default async function () {
await expectFileToExist(join(process.cwd(), 'src/manifest.webmanifest'));

// Angular PWA doesn't install as a dependency
const { dependencies, devDependencies } = JSON.parse(await readFile(join(process.cwd(), 'package.json')));
const hasPWADep = Object.keys({ ...dependencies, ...devDependencies })
.some(d => d === '@angular/pwa');
const { dependencies, devDependencies } = JSON.parse(
await readFile(join(process.cwd(), 'package.json')),
);
const hasPWADep = Object.keys({ ...dependencies, ...devDependencies }).some(
(d) => d === '@angular/pwa',
);
if (hasPWADep) {
throw new Error(`Expected 'package.json' not to contain a dependency on '@angular/pwa'.`);
}

// It should generate a SW configuration file (`ngsw.json`).
const workspaceJson = JSON.parse(await readFile('angular.json'));
const outputPath = workspaceJson.projects['test-project'].architect.build.options.outputPath;
const ngswPath = join(process.cwd(), outputPath, 'ngsw.json');

await ng('build');
await expectFileToExist(ngswPath);

// It should correctly generate assetGroups and include at least one URL in each group.
const ngswJson = JSON.parse(await readFile(ngswPath));
const assetGroups = ngswJson.assetGroups.map(({ name, urls }) => ({
name,
urlCount: urls.length,
}));
const emptyAssetGroups = assetGroups.filter(({ urlCount }) => urlCount === 0);

if (assetGroups.length === 0) {
throw new Error("Expected 'ngsw.json' to contain at least one asset-group.");
}
if (emptyAssetGroups.length > 0) {
throw new Error(
'Expected all asset-groups to contain at least one URL, but the following groups are empty: ' +
emptyAssetGroups.map(({ name }) => name).join(', '),
);
}
}