66 lines
2.5 KiB
JavaScript
66 lines
2.5 KiB
JavaScript
const fs = require('fs')
|
|
const path = require('path')
|
|
const imagemin = require('imagemin')
|
|
const favicons = require('favicons')
|
|
const chalk = require('chalk')
|
|
const log = console.log;
|
|
|
|
const get_options = config => {
|
|
return {
|
|
appName: null,
|
|
appDescription: null,
|
|
developerName: null,
|
|
developerURL: null,
|
|
background: "#212121",
|
|
theme_color: "#212121",
|
|
path: path.join(config.basepath, config.dist_path),
|
|
display: 'standalone',
|
|
orientation: 'portrait',
|
|
start_url: '/?homescreen=1',
|
|
version: config.root_url,
|
|
logging: false,
|
|
online: false,
|
|
preferOnline: false,
|
|
icons: {
|
|
android: true,
|
|
appleIcon: true,
|
|
appleStartup: true,
|
|
windows: true,
|
|
favicons: true,
|
|
firefox: false,
|
|
coast: false,
|
|
yandex: false
|
|
}
|
|
}
|
|
}
|
|
|
|
// generate raw favicon data
|
|
const generate_favicon_data = config => new Promise((resolve, reject) => favicons(path.join(config.basepath, 'icon.png'), get_options(config), (err, response) => err ? reject(err) : resolve(response)))
|
|
// write icon files to destination folder
|
|
const write_icon_files = async (config, favicon_data) => Promise.all(favicon_data.images.map(image => new Promise((res, rej) => fs.writeFile(path.join(config.basepath, config.dist_path, image.name), image.contents, err => err ? rej(err) : res()))))
|
|
// fix html image file references
|
|
const fix_html_references = (config, favicon_data) => Object.assign({}, favicon_data, {html: favicon_data.html.map(item => item.replace(path.join(config.basepath, config.dist_path), '/')).join('')})
|
|
|
|
// optimize image data
|
|
const optimize_icons = async (config, favicon_data) => {
|
|
const plugins = [
|
|
require('imagemin-pngcrush')(),
|
|
require('imagemin-advpng')(),
|
|
require('imagemin-optipng')(),
|
|
]
|
|
const images = favicon_data.images.map(image => imagemin.buffer(image.contents, plugins))
|
|
const optimized_images = await Promise.all(images)
|
|
return optimized_images.map((image, idx) => path.extname(favicon_data.images[idx].name) === '.png' ? Object.assign({}, favicon_data.images[idx], {contents: image}) : favicon_data.images[idx])
|
|
}
|
|
|
|
|
|
module.exports = async config => {
|
|
log(' ', chalk.green('>>'), 'Generating raw favicon data')
|
|
let favicon_data = await generate_favicon_data(config)
|
|
favicon_data = fix_html_references(config, favicon_data)
|
|
log(' ', chalk.green('>>'), 'Optimizing icons')
|
|
favicon_data.images = await optimize_icons(config, favicon_data)
|
|
log(' ', chalk.green('>>'), 'Writing icon files')
|
|
await write_icon_files(config, favicon_data)
|
|
return favicon_data
|
|
} |