Skip to content

Commit

Permalink
fix: favicon error catching for dummies
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Feb 8, 2024
1 parent 5028cb2 commit b6c0fae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
57 changes: 32 additions & 25 deletions packages/vite-plugins/lib/favicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,43 @@
const { resolve } = require('path')
const fs = require('fs')

const { log, HELPERS } = require('@rm/logger')

/**
* @param {boolean} isDevelopment
* @returns {import('vite').Plugin}
*/
const faviconPlugin = (isDevelopment) => {
const favicon = fs.existsSync(
resolve(__dirname, '../../../public/favicon/favicon.ico'),
)
? resolve(__dirname, '../../../public/favicon/favicon.ico')
: resolve(__dirname, '../../../public/favicon/fallback.ico')
return {
name: 'vite-plugin-locales',
generateBundle() {
if (isDevelopment) return
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()
})
},
try {
const favicon = fs.existsSync(
resolve(__dirname, '../../../public/favicon/favicon.ico'),
)
? resolve(__dirname, '../../../public/favicon/favicon.ico')
: resolve(__dirname, '../../../public/favicon/fallback.ico')
return {
name: 'vite-plugin-favicon',
generateBundle() {
if (isDevelopment) return
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' }
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugins/lib/muteWarnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const muteWarningsPlugin = (warningsToIgnore) => {
const mutedMessages = new Set()
return {
name: 'mute-warnings',
name: 'vite-mute-warnings',
enforce: 'pre',
config: (userConfig) => ({
build: {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"prettier:fix": "prettier --write \"**/*.{css,html,js,jsx,yml}\""
},
"dependencies": {
"@rm/locales": "*"
"@rm/locales": "*",
"@rm/logger": "*"
},
"devDependencies": {
"vite": "5.0.12"
Expand Down

0 comments on commit b6c0fae

Please sign in to comment.