Skip to content

Commit

Permalink
fix: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Feb 23, 2024
1 parent bb1ee49 commit b48bcb3
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions packages/vite-plugins/lib/favicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,36 @@ const { log, HELPERS } = require('@rm/logger')
* @returns {import('vite').Plugin}
*/
const faviconPlugin = (isDevelopment) => {
try {
const basePath = '../../../public/favicon'
const fallback = resolve(__dirname, `${basePath}/fallback.ico`)
const singleDomainPath = resolve(`${basePath}/favicon.ico`)
const multiDomainPath = resolve(
`${basePath}/${
process.env.NODE_CONFIG_ENV ? `-${process.env.NODE_CONFIG_ENV}` : ''
}.ico`,
)
const favicon = fs.existsSync(multiDomainPath)
? multiDomainPath
: fs.existsSync(singleDomainPath)
? singleDomainPath
: fallback
return {
name: 'vite-plugin-favicon',
generateBundle() {
if (isDevelopment) return
const basePath = resolve(__dirname, '../../../public/favicon')
const fallback = resolve(basePath, `fallback.ico`)
const custom = process.env.NODE_CONFIG_ENV
? resolve(basePath, `${process.env.NODE_CONFIG_ENV}.ico`)
: resolve(basePath, `favicon.ico`)
const favicon = fs.existsSync(custom) ? custom : fallback
return {
name: 'vite-plugin-favicon',
generateBundle() {
if (isDevelopment) return
try {
this.emitFile({
type: 'asset',
fileName: 'favicon.ico',
source: fs.readFileSync(favicon),
})
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url === '/favicon.ico') {
res.writeHead(200, { 'Content-Type': 'image/x-icon' })
res.end(fs.readFileSync(favicon))
return
}
next()
})
},
}
} catch (e) {
log.error(HELPERS.build, 'Error loading favicon', e)
return { name: 'vite-plugin-favicon' }
} catch (e) {
log.error(HELPERS.build, 'Error loading favicon', e)
}
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url === '/favicon.ico') {
res.writeHead(200, { 'Content-Type': 'image/x-icon' })
res.end(fs.readFileSync(favicon))
return
}
next()
})
},
}
}

Expand Down

0 comments on commit b48bcb3

Please sign in to comment.