mirror of
https://github.com/Afilmory/afilmory
synced 2026-05-01 18:26:41 +00:00
- Added a check to return null for files that do not include 'locales' in their ID, improving the plugin's efficiency by preventing unnecessary transformations. Signed-off-by: Innei <tukon479@gmail.com>
35 lines
825 B
TypeScript
35 lines
825 B
TypeScript
import { set } from 'es-toolkit/compat'
|
|
import type { Plugin } from 'vite'
|
|
|
|
import { MONOREPO_ROOT_PATH } from './__internal__/constants'
|
|
|
|
export function localesJsonPlugin(): Plugin {
|
|
return {
|
|
name: 'locales-json-transform',
|
|
enforce: 'pre',
|
|
|
|
async transform(code, id) {
|
|
if (!id.includes('locales')) {
|
|
return null
|
|
}
|
|
if (!id.includes(MONOREPO_ROOT_PATH) || !id.endsWith('.json')) {
|
|
return null
|
|
}
|
|
|
|
const content = JSON.parse(code)
|
|
const obj = {}
|
|
|
|
const keys = Object.keys(content as object)
|
|
for (const accessorKey of keys) {
|
|
set(obj, accessorKey, (content as any)[accessorKey])
|
|
}
|
|
|
|
console.info('[locales-json-transform] Transformed:', id)
|
|
return {
|
|
code: JSON.stringify(obj),
|
|
map: null,
|
|
}
|
|
},
|
|
}
|
|
}
|