Skip to content

Commit

Permalink
refactor(icon_resize): Refactor icon generator to execute in a single…
Browse files Browse the repository at this point in the history
… run

Nuxt does not warn about it, but it does not execute modules more than once. Because of that, the second icon generator entry in the nuxt config did not run.
  • Loading branch information
Hanziness committed Sep 16, 2024
1 parent 6436e65 commit 0db5c7d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
14 changes: 7 additions & 7 deletions modules/build/icon_resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { join } from 'node:path'
import { existsSync, mkdirSync } from 'fs'
import sharp from 'sharp'

interface IconVariant {
src: string,
prefix: string,
export interface IconVariant {
src: string
prefix: string
purpose: string
size: number[]
bgColor?: string
}

interface IconResizerPluginOptions {
sizes: number[],
export interface IconResizerPluginOptions {
outputFolder: string,
variants: IconVariant[],
}
Expand All @@ -31,10 +31,10 @@ export default defineNuxtModule({
mkdirSync(outputFolder, { recursive: true })
}

console.info(`Generating ${iconConfig.src} into sizes [${moduleOptions.sizes.join(', ')}] -> ${moduleOptions.outputFolder}`)
console.info(`Generating ${iconConfig.src} into sizes [${iconConfig.size.join(', ')}] -> ${moduleOptions.outputFolder}`)

const promises = []
for (const size of moduleOptions.sizes) {
for (const size of iconConfig.size) {
const outputFileName = `${iconConfig.prefix}${size}.png`
let basePromise = sharp(join(nuxt.options.rootDir, iconConfig.src)).resize(size)

Expand Down
32 changes: 15 additions & 17 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,38 @@ import { fileURLToPath } from 'url'
import VueI18nVitePlugin from '@intlify/unplugin-vue-i18n/vite'
import StylelintPlugin from 'vite-plugin-stylelint'
import { AppPlatform } from './platforms/platforms'
import type { IconResizerPluginOptions } from './modules/build/icon_resize'

const packageJson = fs.readFileSync('./package.json').toString()
const version = JSON.parse(packageJson).version || 0

const iconConfig = {
const iconConfig: IconResizerPluginOptions = {
outputFolder: 'icons',
sizes: [64, 120, 144, 152, 192, 384, 512],
variants: [
{
src: '/public/icon.png',
prefix: 'icon-maskable-',
purpose: 'maskable'
purpose: 'maskable',
size: [64, 120, 144, 152, 192, 384, 512],
},
{
src: '/public/icon_monochrome.png',
prefix: 'icon-monochrome-',
purpose: 'monochrome'
purpose: 'monochrome',
size: [64, 120, 144, 152, 192, 384, 512],
},
{
src: '/public/favicon.png',
prefix: 'icon-base-',
purpose: 'any'
purpose: 'any',
size: [64, 120, 144, 152, 192, 384, 512],
},
{
src: '/public/icon.png',
prefix: 'icon-apple-',
purpose: 'any',
bgColor: '#fee2e2',
size: [192]
}
]
}
Expand Down Expand Up @@ -115,18 +125,6 @@ export default defineNuxtConfig({
// Doc: https://github.com/nuxt-community/stylelint-module
'@pinia/nuxt',
['./modules/build/icon_resize', iconConfig],
['./modules/build/icon_resize', {
outputFolder: 'icons',
sizes: [192],
variants: [
{
src: '/public/icon.png',
prefix: 'icon-apple-',
purpose: 'any',
bgColor: '#fee2e2'
}
]
}],
['modules/build/pwa', { swPath: 'serviceworker.js' }]
// '@nuxtjs/sitemap'
],
Expand Down

0 comments on commit 0db5c7d

Please sign in to comment.