"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const expo_pwa_1 = require("expo-pwa");
const ModifyHtmlWebpackPlugin_1 = __importDefault(require("./ModifyHtmlWebpackPlugin"));
function logNotice(type, message) {
console.log(chalk_1.default.magenta(`\u203A ${type}: ${chalk_1.default.gray(message)}`));
}
function logWarning(type, message) {
console.log(chalk_1.default.yellow(`\u203A ${type}: ${chalk_1.default.gray(message)}`));
}
class ApplePwaWebpackPlugin extends ModifyHtmlWebpackPlugin_1.default {
constructor(pwaOptions, meta, icon, startupImage) {
super();
this.pwaOptions = pwaOptions;
this.meta = meta;
this.icon = icon;
this.startupImage = startupImage;
}
async modifyAsync(compiler, compilation, data) {
// Meta
var _a, _b;
const hasMetaTagWithName = (name) => {
return this.pwaOptions.meta.some(v => v.name === name);
};
if (this.meta.isWebAppCapable) {
if (!hasMetaTagWithName('mobile-web-app-capable')) {
data.assetTags.meta.push(metaTag('mobile-web-app-capable', 'yes'));
}
if (!hasMetaTagWithName('apple-mobile-web-app-capable')) {
data.assetTags.meta.push(metaTag('apple-mobile-web-app-capable', 'yes'));
}
}
if (this.meta.isFullScreen && !hasMetaTagWithName('apple-touch-fullscreen')) {
data.assetTags.meta.push(metaTag('apple-touch-fullscreen', 'yes'));
}
if (this.meta.name && !hasMetaTagWithName('apple-mobile-web-app-title')) {
data.assetTags.meta.push(metaTag('apple-mobile-web-app-title', this.meta.name));
}
if (this.meta.barStyle && !hasMetaTagWithName('apple-mobile-web-app-status-bar-style')) {
data.assetTags.meta.push(metaTag('apple-mobile-web-app-status-bar-style', this.meta.barStyle));
}
// App Icon
if (this.icon) {
const links = this.pwaOptions.links
.filter(v => v.rel === 'apple-touch-icon')
.map(v => v.sizes);
const targetSizes = [180];
const requiredSizes = [];
for (const size of targetSizes) {
const sizes = `${size}x${size}`;
if (links.includes(sizes)) {
logNotice('Safari Icons', `Using custom `);
}
else {
requiredSizes.push(size);
}
}
const iconAssets = await (0, expo_pwa_1.generateAppleIconAsync)(this.pwaOptions, this.icon, {
sizes: requiredSizes,
});
for (const asset of iconAssets) {
const size = (_a = asset.tag) === null || _a === void 0 ? void 0 : _a.attributes.sizes;
if (size && links.includes(size)) {
logNotice('Safari Icons', `Using custom `);
}
else {
compilation.assets[asset.asset.path] = {
source: () => asset.asset.source,
size: () => asset.asset.source.length,
};
data.assetTags.meta.push(asset.tag);
}
}
}
else {
logWarning('Safari Icons', `No template image found, skipping auto generation...`);
}
// Splash screens
if (this.startupImage) {
const assets = await (0, expo_pwa_1.generateSplashAsync)(this.pwaOptions, this.startupImage);
const links = this.pwaOptions.links
.filter(v => v.rel === 'apple-touch-startup-image')
.map(v => v.media);
for (const asset of assets) {
const media = (_b = asset.tag) === null || _b === void 0 ? void 0 : _b.attributes.media;
if (media && links.includes(media)) {
logNotice('Safari Splash Screens', `Using custom `);
}
else {
compilation.assets[asset.asset.path] = {
source: () => asset.asset.source,
size: () => asset.asset.source.length,
};
data.assetTags.meta.push(asset.tag);
}
}
}
else {
logWarning('Safari Splash Screens', `No template image found, skipping auto generation...`);
}
return data;
}
}
exports.default = ApplePwaWebpackPlugin;
function metaTag(name, content) {
return {
tagName: 'meta',
voidTag: true,
attributes: {
name,
content,
},
};
}
//# sourceMappingURL=ApplePwaWebpackPlugin.js.map