From b6c0fae43ef8e4312bc61c265c8c16cd4a0c1ee5 Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:43:30 -0500 Subject: [PATCH] fix: favicon error catching for dummies --- packages/vite-plugins/lib/favicon.js | 57 +++++++++++++---------- packages/vite-plugins/lib/muteWarnings.js | 2 +- packages/vite-plugins/package.json | 3 +- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/packages/vite-plugins/lib/favicon.js b/packages/vite-plugins/lib/favicon.js index ee6ce515b..108d1c4d1 100644 --- a/packages/vite-plugins/lib/favicon.js +++ b/packages/vite-plugins/lib/favicon.js @@ -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' } } } diff --git a/packages/vite-plugins/lib/muteWarnings.js b/packages/vite-plugins/lib/muteWarnings.js index b8ba65013..79036fcc9 100644 --- a/packages/vite-plugins/lib/muteWarnings.js +++ b/packages/vite-plugins/lib/muteWarnings.js @@ -7,7 +7,7 @@ const muteWarningsPlugin = (warningsToIgnore) => { const mutedMessages = new Set() return { - name: 'mute-warnings', + name: 'vite-mute-warnings', enforce: 'pre', config: (userConfig) => ({ build: { diff --git a/packages/vite-plugins/package.json b/packages/vite-plugins/package.json index 37a252f39..798668a17 100644 --- a/packages/vite-plugins/package.json +++ b/packages/vite-plugins/package.json @@ -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"