Skip to content

Commit b2583b5

Browse files
authored
improvement: infuse for external resource bundling (#200)
1 parent d7fc7eb commit b2583b5

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/utils.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ export async function loadNamespaceDictionary (path: string) {
319319
type ParsedLocaleMessagePathInfo = {
320320
locale: Locale
321321
filename?: string
322+
base?: string
322323
}
323324

324325
function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): ParsedLocaleMessagePathInfo {
@@ -329,16 +330,18 @@ function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): Pars
329330
const match = re.exec(fullPath)
330331
debug('getLocaleMessagePathInfo: regex match', match)
331332
if (!match) {
332-
return { locale: '', filename: '' }
333+
return { locale: '', filename: '', base: '' }
333334
} else {
334335
if (match.groups) {
335336
const locale = match.groups.locale || ''
336-
const filename = match.groups.filename || ''
337-
return { locale, filename }
337+
const filename = match.groups.filename || match.groups.basekey || ''
338+
const base = match.groups.base || ''
339+
return { locale, filename, base }
338340
} else {
339341
return {
340342
locale: match[1] ? match[1] : '',
341-
filename: match[2] ? match[2] : ''
343+
filename: match[2] ? match[2] : '',
344+
base: ''
342345
}
343346
}
344347
}
@@ -389,6 +392,7 @@ type ExternalLocaleMessagesParseInfo = {
389392
namespace: string
390393
locale: Locale
391394
filename?: string
395+
base?: string
392396
}
393397

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

408412
const bundleTargetPaths = bundle.split(',').filter(p => p)
409-
const externalLocaleMessagesParseInfo = bundleTargetPaths.reduce((info, targetPath) => {
413+
const externalExists = bundleTargetPaths.reduce((info, targetPath) => {
410414
const namespace = dictionary[targetPath] || ''
411415
const globedPaths = glob.sync(targetPath).map(p => resolve(p))
412416
debug('splitLocaleMessages globedPaths', globedPaths)
413417
return globedPaths.reduce((info, fullPath) => {
414-
const { locale, filename } = getLocaleMessagePathInfo(fullPath, bundleMatch)
418+
const { locale, filename, base } = getLocaleMessagePathInfo(fullPath, bundleMatch)
415419
if (!locale) { return info }
416-
info.push({ path: fullPath, locale, namespace, filename })
420+
info.set(fullPath, { path: fullPath, locale, namespace, filename, base })
417421
return info
418422
}, info)
423+
}, new Map<string, ExternalLocaleMessagesParseInfo>())
424+
const _externalExists = [...externalExists.values()]
425+
debug('splitLocaleMessages: externalLocaleMessagesParseInfo (exisit):', _externalExists)
426+
427+
const locales = Object.keys(messages)
428+
const externalAdditional = _externalExists.reduce((ext, { path, locale, namespace, filename, base }) => {
429+
const additionalLocales = locales.filter(l => l !== locale)
430+
additionalLocales.forEach(l => {
431+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
432+
if (bundleMatch && filename && base && messages[l] && (messages[l] as any)[namespace] && ((messages[l] as any)[namespace] as any)[filename]) {
433+
const re = new RegExp(bundleMatch, 'ig')
434+
const match = re.exec(path)
435+
debug('splitLocaleMessages: regex match', match)
436+
if (match && match.groups) {
437+
const groupLen = Object.keys(match.groups).length
438+
let buildingPath = ''
439+
for (let i = 0; i < groupLen; i++) {
440+
if (match[i + 1]) {
441+
buildingPath = `${buildingPath}${match[i + 1] === filename ? filename : match[i + 1] === l ? l : match[i + 1]}/`
442+
}
443+
if (i === groupLen - 1) {
444+
buildingPath = `${buildingPath}.json`
445+
}
446+
}
447+
ext.push({ path: buildingPath, locale: l, namespace, filename, base })
448+
}
449+
}
450+
})
451+
return ext
419452
}, [] as ExternalLocaleMessagesParseInfo[])
420-
debug('splitLocaleMessages: externalLocaleMessagesParseInfo:', externalLocaleMessagesParseInfo)
453+
debug('splitLocaleMessages: externalLocaleMessagesParseInfo (addiotnal):', externalAdditional)
454+
455+
const externalLocaleMessagesParseInfo = [..._externalExists, ...externalAdditional]
456+
debug('splitLocaleMessages: externalLocaleMessagesParseInfo (added):', externalLocaleMessagesParseInfo)
421457

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

0 commit comments

Comments
 (0)