Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: infuse for external resource bundling #200

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export async function loadNamespaceDictionary (path: string) {
type ParsedLocaleMessagePathInfo = {
locale: Locale
filename?: string
base?: string
}

function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): ParsedLocaleMessagePathInfo {
Expand All @@ -329,16 +330,18 @@ function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): Pars
const match = re.exec(fullPath)
debug('getLocaleMessagePathInfo: regex match', match)
if (!match) {
return { locale: '', filename: '' }
return { locale: '', filename: '', base: '' }
} else {
if (match.groups) {
const locale = match.groups.locale || ''
const filename = match.groups.filename || ''
return { locale, filename }
const filename = match.groups.filename || match.groups.basekey || ''
const base = match.groups.base || ''
return { locale, filename, base }
} else {
return {
locale: match[1] ? match[1] : '',
filename: match[2] ? match[2] : ''
filename: match[2] ? match[2] : '',
base: ''
}
}
}
Expand Down Expand Up @@ -389,6 +392,7 @@ type ExternalLocaleMessagesParseInfo = {
namespace: string
locale: Locale
filename?: string
base?: string
}

// TODO: should be selected more other library ...
Expand All @@ -406,18 +410,50 @@ export function splitLocaleMessages (
if (!bundle) { return { sfc: messages } }

const bundleTargetPaths = bundle.split(',').filter(p => p)
const externalLocaleMessagesParseInfo = bundleTargetPaths.reduce((info, targetPath) => {
const externalExists = bundleTargetPaths.reduce((info, targetPath) => {
const namespace = dictionary[targetPath] || ''
const globedPaths = glob.sync(targetPath).map(p => resolve(p))
debug('splitLocaleMessages globedPaths', globedPaths)
return globedPaths.reduce((info, fullPath) => {
const { locale, filename } = getLocaleMessagePathInfo(fullPath, bundleMatch)
const { locale, filename, base } = getLocaleMessagePathInfo(fullPath, bundleMatch)
if (!locale) { return info }
info.push({ path: fullPath, locale, namespace, filename })
info.set(fullPath, { path: fullPath, locale, namespace, filename, base })
return info
}, info)
}, new Map<string, ExternalLocaleMessagesParseInfo>())
const _externalExists = [...externalExists.values()]
debug('splitLocaleMessages: externalLocaleMessagesParseInfo (exisit):', _externalExists)

const locales = Object.keys(messages)
const externalAdditional = _externalExists.reduce((ext, { path, locale, namespace, filename, base }) => {
const additionalLocales = locales.filter(l => l !== locale)
additionalLocales.forEach(l => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (bundleMatch && filename && base && messages[l] && (messages[l] as any)[namespace] && ((messages[l] as any)[namespace] as any)[filename]) {
const re = new RegExp(bundleMatch, 'ig')
const match = re.exec(path)
debug('splitLocaleMessages: regex match', match)
if (match && match.groups) {
const groupLen = Object.keys(match.groups).length
let buildingPath = ''
for (let i = 0; i < groupLen; i++) {
if (match[i + 1]) {
buildingPath = `${buildingPath}${match[i + 1] === filename ? filename : match[i + 1] === l ? l : match[i + 1]}/`
}
if (i === groupLen - 1) {
buildingPath = `${buildingPath}.json`
}
}
ext.push({ path: buildingPath, locale: l, namespace, filename, base })
}
}
})
return ext
}, [] as ExternalLocaleMessagesParseInfo[])
debug('splitLocaleMessages: externalLocaleMessagesParseInfo:', externalLocaleMessagesParseInfo)
debug('splitLocaleMessages: externalLocaleMessagesParseInfo (addiotnal):', externalAdditional)

const externalLocaleMessagesParseInfo = [..._externalExists, ...externalAdditional]
debug('splitLocaleMessages: externalLocaleMessagesParseInfo (added):', externalLocaleMessagesParseInfo)

debug('splitLocaleMessages: messages (before):', messages)
const metaExternalLocaleMessages = externalLocaleMessagesParseInfo.reduce((meta, { path, locale, namespace, filename }) => {
Expand Down