# Conflicts: # TODO.md # docs/QA_체크리스트.md # frontend/src/b/BLayout.tsx # frontend/vite.config.ts
84 lines
4.9 KiB
TypeScript
84 lines
4.9 KiB
TypeScript
import { readFileSync, writeFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
import tailwindcss from '@tailwindcss/vite'
|
|
import react from '@vitejs/plugin-react'
|
|
import { defineConfig, loadEnv, type Plugin } from 'vite'
|
|
|
|
type Gak = { name: string; description: string }
|
|
|
|
const gaks: Gak[] = JSON.parse(readFileSync(resolve(import.meta.dirname, 'seo/gak.json'), 'utf8'))
|
|
const gakList = `<ul>${gaks.map((g) => `<li><b>${g.name}</b>: ${g.description}</li>`).join('')}</ul>`
|
|
|
|
/** 검색 로봇이 처음 받는 페이지별 HTML. 바뀌지 않는 소개 글만 넣고, 날씨 값은 앱이 뜬 뒤 불러온다 */
|
|
const PAGES: { path: string; title: string; description: string; body: string }[] = [
|
|
{
|
|
path: 'gak',
|
|
title: '오늘의 각 · 세차각·빨래각·러닝각 점수 | 날씨각',
|
|
description: `세차각, 빨래각, 러닝각, 피크닉각처럼 오늘 하고 싶은 일 ${gaks.length}가지가 우리 동네 날씨에 맞는지 아침·낮·저녁·밤 점수로 알려 드려요.`,
|
|
body: `<h1>날씨각 · 오늘의 각</h1><p>오늘 하고 싶은 일이 날씨에 맞는지 점수로 알려 드려요. 기상청 예보와 미세먼지로 아침, 낮, 저녁, 밤마다 따로 계산해요. 비 오는 날엔 우산각·파전각·감성노래각이, 맑은 날엔 세차각·빨래각·피크닉각이 올라가요.</p>${gakList}`,
|
|
},
|
|
{
|
|
path: 'sky',
|
|
title: '오늘 노을 · 노을 시간과 노을 명소 | 날씨각',
|
|
description: '오늘 우리 동네 노을이 얼마나 붉게 물들지, 몇 시에 가장 붉은지, 가까운 노을 명소는 어디인지 날씨각에서 확인하세요. 해돋이와 달도 함께 알려 드려요.',
|
|
body: '<h1>날씨각 · 오늘 노을</h1><p>구름의 높이와 양, 공기의 맑기로 오늘 노을이 얼마나 붉게 물들지 점수로 알려 드려요. 보통 해 진 뒤 10~20분이 가장 붉어요.</p><p>가까운 노을 명소와 해 지는 방향, 사흘 치 노을 예보, 해돋이와 오늘 밤 달 모양도 볼 수 있어요.</p>',
|
|
},
|
|
{
|
|
path: 'week',
|
|
title: '이번 주말 날씨 · 나들이와 축제 | 날씨각',
|
|
description: '이번 주말 날씨와 나들이하기 좋은 곳, 가까운 축제·전시·공연·팝업을 날씨각에서 한 번에 확인하세요.',
|
|
body: '<h1>날씨각 · 주말과 행사</h1><p>토요일과 일요일 날씨로 나들이하기 좋은 정도를 점수로 알려 드려요. 비 소식이 있으면 실내에서 즐길 거리를 먼저 보여 드려요.</p><p>우리 동네와 가까운 지역의 축제, 전시, 공연, 팝업 스토어를 모아 드려요.</p>',
|
|
},
|
|
{
|
|
path: 'season',
|
|
title: '계절 소식과 날씨 상식 | 날씨각',
|
|
description: '지금 계절에 볼 만한 단풍·꽃·제철 소식과 알아 두면 좋은 날씨 상식을 날씨각에서 읽어 보세요.',
|
|
body: '<h1>날씨각 · 계절과 상식</h1><p>이번 주 절기와 제철 풍경, 지금 계절에 하기 좋은 일을 알려 드려요.</p><p>불쾌지수, 체감온도, 미세먼지 등급처럼 알아 두면 좋은 날씨 상식도 읽을 수 있어요.</p>',
|
|
},
|
|
]
|
|
|
|
const attr = (s: string) => s.replace(/&/g, '&').replace(/"/g, '"')
|
|
|
|
function seoPages(): Plugin {
|
|
let outDir = 'dist'
|
|
return {
|
|
name: 'seo-pages',
|
|
apply: 'build',
|
|
configResolved(c) {
|
|
outDir = resolve(c.root, c.build.outDir)
|
|
},
|
|
closeBundle() {
|
|
const base = readFileSync(resolve(outDir, 'index.html'), 'utf8')
|
|
const site = process.env.VITE_SITE_URL || 'https://weather.o2o.kr'
|
|
for (const p of PAGES) {
|
|
const url = `${site}/${p.path}`
|
|
const html = base
|
|
.replace(/<title>[^<]*<\/title>/, `<title>${p.title}</title>`)
|
|
.replace(/(<meta (?:name|property)="(?:description|og:description|twitter:description)" content=")[^"]*"/g, `$1${attr(p.description)}"`)
|
|
.replace(/(<meta (?:property|name)="(?:og:title|twitter:title)" content=")[^"]*"/g, `$1${attr(p.title)}"`)
|
|
.replace(/(<meta property="og:url" content=")[^"]*"/, `$1${url}"`)
|
|
.replace(/(<link rel="canonical" href=")[^"]*"/, `$1${url}"`)
|
|
.replace(/<main>[\s\S]*<\/main>/, `<main>${p.body}<p><a href="/">날씨각 처음으로</a></p></main>`)
|
|
writeFileSync(resolve(outDir, `${p.path}.html`), html)
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
// 토스 빌드는 토스 도메인에서 열리므로 API·사이트 주소를 HTTPS 절대 주소로 받아야 한다
|
|
function checkTossEnv(mode: string) {
|
|
if (mode !== 'toss') return
|
|
const env = loadEnv(mode, process.cwd())
|
|
for (const key of ['VITE_API_BASE_URL', 'VITE_SITE_URL']) {
|
|
if (!env[key]?.startsWith('https://')) throw new Error(`토스 빌드: ${key} 에 https:// 주소가 필요해요 (frontend/.env.toss)`)
|
|
}
|
|
}
|
|
|
|
// https://vite.dev/config/
|
|
export default defineConfig(({ mode }) => {
|
|
checkTossEnv(mode)
|
|
return {
|
|
plugins: [react(), tailwindcss(), ...(mode === 'toss' ? [] : [seoPages()])],
|
|
}
|
|
})
|