Skip to content
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

fix: pwa icon generation and angular vue path #572

Merged
merged 3 commits into from
Dec 12, 2023
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
9 changes: 2 additions & 7 deletions src/platforms/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class AndroidAssetGenerator extends AssetGenerator {

const collected = await Promise.all(
icons.map(async (icon) => {
const [dest, outputInfo] = await this.generateLegacyLauncherIcon(project, asset, icon, pipe);
const [dest, outputInfo] = await this.generateLegacyLauncherIcon(project, asset, icon);

return new OutputAsset(
icon,
Expand All @@ -248,7 +248,7 @@ export class AndroidAssetGenerator extends AssetGenerator {
collected.push(
...(await Promise.all(
icons.map(async (icon) => {
const [dest, outputInfo] = await this.generateRoundLauncherIcon(project, asset, icon, pipe);
const [dest, outputInfo] = await this.generateRoundLauncherIcon(project, asset, icon);

return new OutputAsset(
icon,
Expand All @@ -270,11 +270,7 @@ export class AndroidAssetGenerator extends AssetGenerator {
project: Project,
asset: InputAsset,
template: AndroidOutputAssetTemplate,
pipe: Sharp,
): Promise<[string, OutputInfo]> {
const radius = 4;
const svg = `<svg width="${template.width}" height="${template.height}"><rect x="0" y="0" width="${template.width}" height="${template.height}" rx="${radius}" fill="#ffffff"/></svg>`;

const resPath = this.getResPath(project);
const parentDir = join(resPath, `mipmap-${template.density}`);
if (!(await pathExists(parentDir))) {
Expand Down Expand Up @@ -308,7 +304,6 @@ export class AndroidAssetGenerator extends AssetGenerator {
project: Project,
asset: InputAsset,
template: AndroidOutputAssetTemplate,
pipe: Sharp,
): Promise<[string, OutputInfo]> {
const svg = `<svg width="${template.width}" height="${template.height}"><circle cx="${template.width / 2}" cy="${
template.height / 2
Expand Down
31 changes: 18 additions & 13 deletions src/platforms/pwa/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirp, pathExists, readFile, readJSON, writeJSON } from '@ionic/utils-fs';
import { mkdirp, pathExists, readFile, readJSON, rmSync, writeJSON } from '@ionic/utils-fs';
import fetch from 'node-fetch';
import parse from 'node-html-parser';
import { basename, extname, join, posix, relative, sep } from 'path';
Expand Down Expand Up @@ -118,7 +118,7 @@ export class PwaAssetGenerator extends AssetGenerator {

const generated: OutputAsset[] = [];

const splashes = await Promise.all(assetSizes.map((a) => this._generateSplashFromLogo(project, asset, a, pipe)));
const splashes = await Promise.all(assetSizes.map((a) => this._generateSplashFromLogo(project, asset, a)));

generated.push(...splashes.flat());

Expand All @@ -129,7 +129,6 @@ export class PwaAssetGenerator extends AssetGenerator {
project: Project,
asset: InputAsset,
sizeString: string,
pipe: Sharp,
): Promise<OutputAsset[]> {
const parts = sizeString.split('@');
const sizeParts = parts[0].split('x');
Expand Down Expand Up @@ -291,8 +290,8 @@ export class PwaAssetGenerator extends AssetGenerator {
private async getPWADirectory(projectRoot?: string): Promise<string> {
if (await pathExists(join(projectRoot ?? '', 'public')) /* React */) {
return join(projectRoot ?? '', 'public');
} else if (await pathExists(join(projectRoot ?? '', 'src/assets')) /* Angular and Vue */) {
return join(projectRoot ?? '', 'src/assets');
} else if (await pathExists(join(projectRoot ?? '', 'src')) /* Angular and Vue */) {
return join(projectRoot ?? '', 'src');
} else if (await pathExists(join(projectRoot ?? '', 'www'))) {
return join(projectRoot ?? '', 'www');
} else {
Expand Down Expand Up @@ -353,19 +352,28 @@ export class PwaAssetGenerator extends AssetGenerator {
manifestJson = await readJSON(manifestPath);
}

const icons = manifestJson['icons'] || [];

let icons = manifestJson['icons'] || [];
const replacedIcons = [];
for (const asset of pwaAssets) {
const src = asset.template.name;
const fname = basename(src);
const relativePath = relative(pwaDir, join(pwaAssetDir, PWA_ASSET_PATH, fname));
replacedIcons.push(this.makeIconManifestEntry(asset.template, relativePath));
}

const existing = !!icons.find((i: any) => i.src === relativePath);
if (!existing) {
icons.push(this.makeIconManifestEntry(asset.template, relativePath));
// Delete icons that were replaced
for (const icon of icons) {
if (await pathExists(join(pwaDir, icon.src))) {
const exists = !!pwaAssets.find((i: any) => i.sizes === icon.sizes);
if (!exists) {
rmSync(join(pwaDir, icon.src));
warn(`DELETE ${icon.src}`);
}
}
}

icons = replacedIcons;

// Update the manifest background color to the splash one if provided to ensure
// platform automatic splash generation works
if (this.options.splashBackgroundColor) {
Expand Down Expand Up @@ -444,9 +452,6 @@ export class PwaAssetGenerator extends AssetGenerator {
}
const dest = join(destDir, name);

// console.log(width, height);
const targetLogoWidthPercent = this.options.logoSplashScale ?? 0.2;
const targetWidth = Math.floor(width * targetLogoWidthPercent);
const outputInfo = await pipe.resize(width, height).png().toFile(dest);

const template: PwaOutputAssetTemplate = {
Expand Down
Loading