refactor: minify recipe.json by remove emojis and get it dynamically

This commit is contained in:
YunYouJun
2022-12-19 06:14:03 +08:00
parent 395a65f6bc
commit f72fd0b3bb
10 changed files with 27 additions and 23 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

View File

@@ -4,7 +4,6 @@ import path from 'path'
import url from 'url' import url from 'url'
import consola from 'consola' import consola from 'consola'
import type { Recipe, RecipeItem } from '~/types' import type { Recipe, RecipeItem } from '~/types'
import { generateEmojisFromStuff } from '~/utils'
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const recipeCsvFile = path.resolve(__dirname, '../src/data/recipe.csv') const recipeCsvFile = path.resolve(__dirname, '../src/data/recipe.csv')
@@ -30,7 +29,6 @@ function run() {
recipeJson.push({ recipeJson.push({
name: attrs[0].trim(), name: attrs[0].trim(),
stuff, stuff,
emojis: generateEmojisFromStuff(stuff),
// link: attrs[2].trim(), // link: attrs[2].trim(),
// bv id // bv id
bv: attrs[2].trim().replace('https://www.bilibili.com/video/', ''), bv: attrs[2].trim().replace('https://www.bilibili.com/video/', ''),

View File

@@ -8,7 +8,7 @@ onBeforeMount(() => {
displayICP.value = ['cook.yunyoujun.cn', 'localhost', '127.0.0.1'].includes(window.location.hostname) displayICP.value = ['cook.yunyoujun.cn', 'localhost', '127.0.0.1'].includes(window.location.hostname)
}) })
const commitSha = import.meta.env.CF_PAGES_COMMIT_SHA || '' const commitSha = import.meta.env.VITE_COMMIT_REF || ''
const now = import.meta.env.VITE_APP_BUILD_TIME const now = import.meta.env.VITE_APP_BUILD_TIME
const buildDate = (new Date(parseInt(now) * 1000)).toLocaleDateString() const buildDate = (new Date(parseInt(now) * 1000)).toLocaleDateString()
</script> </script>

View File

@@ -5,7 +5,6 @@ import type { StuffItem } from '~/data/food'
import { meat, staple, tools, vegetable } from '~/data/food' import { meat, staple, tools, vegetable } from '~/data/food'
import recipeData from '~/data/recipe.json' import recipeData from '~/data/recipe.json'
import type { Recipe, RecipeItem } from '~/types' import type { Recipe, RecipeItem } from '~/types'
import { useRecipeStore } from '~/store/recipe'
import { useInvisibleElement } from '~/composables/helper' import { useInvisibleElement } from '~/composables/helper'
import { useEmojiAnimation } from '~/composables/animation' import { useEmojiAnimation } from '~/composables/animation'

View File

@@ -2,7 +2,9 @@
import { useGtm } from '@gtm-support/vue-gtm' import { useGtm } from '@gtm-support/vue-gtm'
import { tools } from '~/data/food' import { tools } from '~/data/food'
import type { RecipeItem } from '~/types' import type { RecipeItem } from '~/types'
defineProps<{ import { getEmojisFromStuff } from '~/utils'
const props = defineProps<{
dish: RecipeItem dish: RecipeItem
}>() }>()
@@ -20,6 +22,11 @@ const triggerGtm = (val: string) => {
action: val, action: val,
}) })
} }
const dishLabel = computed(() => {
const emojis = getEmojisFromStuff(props.dish.stuff)
return `${props.dish.tags?.includes('杂烩') ? '🍲' : emojis.join(' ')} ${props.dish.name}`
})
</script> </script>
<template> <template>
@@ -30,7 +37,7 @@ const triggerGtm = (val: string) => {
@click="triggerGtm(dish.name)" @click="triggerGtm(dish.name)"
> >
<span m="r-1" class="inline-flex justify-center items-center" text="sm blue-700 dark:blue-200"> <span m="r-1" class="inline-flex justify-center items-center" text="sm blue-700 dark:blue-200">
{{ `${dish.tags?.includes('杂烩') ? '🍲' : dish.emojis.join(' ')} ${dish.name}` }} {{ dishLabel }}
</span> </span>
<span v-for="tool, i in tools" :key="i" inline-flex> <span v-for="tool, i in tools" :key="i" inline-flex>
<div v-if="dish.tools?.includes(tool.name)" :class="tool.icon" /> <div v-if="dish.tools?.includes(tool.name)" :class="tool.icon" />

View File

@@ -85,7 +85,6 @@ export const vegetable: StuffItem[] = [
{ {
name: '包菜', name: '包菜',
emoji: '🥗', emoji: '🥗',
// image: '/images/cabbage-dog.jpg',
}, },
{ {
name: '白菜', name: '白菜',

View File

@@ -1,9 +1,9 @@
import Toast from 'vue-toastification' // import Toast from 'vue-toastification'
import type { UserModule } from '~/types' import type { UserModule } from '~/types'
import 'vue-toastification/dist/index.css' // import 'vue-toastification/dist/index.css'
export const install: UserModule = ({ app }) => { export const install: UserModule = ({ app }) => {
// add google tag manager, and add GA4 in gtag // add google tag manager, and add GA4 in gtag
app.use(Toast) // app.use(Toast)
} }

View File

@@ -20,7 +20,7 @@ export interface RecipeItem {
/** /**
* 根据材料生成 * 根据材料生成
*/ */
emojis: string[] emojis?: string[]
/** /**
* 难度 * 难度
*/ */

View File

@@ -1,20 +1,17 @@
import { meat, staple, vegetable } from '~/data/food' import { meat, staple, vegetable } from '~/data/food'
const foodItems = [...vegetable, ...meat, ...staple]
const foodEmojiMap = new Map()
foodItems.forEach((item) => {
foodEmojiMap.set(item.name, item.emoji)
})
/** /**
* 从材料生成 Emoji * get emojis from stuff name array
* @param stuff * @param stuff
* @returns * @returns
*/ */
export function generateEmojisFromStuff(stuff: string[]) { export function getEmojisFromStuff(stuff: string[]) {
const emojis: string[] = [] const emojis: string[] = stuff.map(name => foodEmojiMap.get(name)).filter(item => !!item)
stuff.forEach((item) => {
const kinds = [vegetable, meat, staple]
kinds.forEach((kind) => {
kind.forEach((m) => {
if (m.name === item)
emojis.push(m.emoji)
})
})
})
return emojis return emojis
} }

View File

@@ -15,6 +15,10 @@ import Unocss from 'unocss/vite'
import legacy from '@vitejs/plugin-legacy' import legacy from '@vitejs/plugin-legacy'
Object.assign(process.env, {
VITE_COMMIT_REF: process.env.CF_PAGES_COMMIT_SHA || '',
})
export default defineConfig({ export default defineConfig({
resolve: { resolve: {
alias: { alias: {