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

[DRAFT] feat: merge static locale file chains #3243

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineBuildConfig({
'@babel/parser',
'unplugin-vue-router',
'unplugin-vue-router/options',
'chokidar'
'chokidar',
'confbox'
]
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"bumpp": "^9.4.1",
"changelogithub": "^0.13.7",
"chokidar": "^4.0.1",
"confbox": "^0.1.8",
"consola": "^3",
"eslint": "^9.5.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
38 changes: 18 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function extendBundler({ options: nuxtOptions }: I18nNuxtContext, n
const i18nModulePaths =
nuxtOptions?.i18nModules?.map(module => resolve(nuxt.options._layers[0].config.rootDir, module.langDir ?? '')) ?? []
debug('i18nModulePaths -', i18nModulePaths)
const localePaths = [...langPaths, ...i18nModulePaths]
const localePaths = [...langPaths, ...i18nModulePaths, resolve(nuxt.options.buildDir, 'i18n')]
const localeIncludePaths = localePaths.length ? localePaths.map(x => resolve(x, './**')) : undefined

const sourceMapOptions: BundlerPluginOptions = {
Expand All @@ -42,7 +42,7 @@ export async function extendBundler({ options: nuxtOptions }: I18nNuxtContext, n
compositionOnly: nuxtOptions.bundle.compositionOnly,
onlyLocales: nuxtOptions.bundle.onlyLocales,
dropMessageCompiler: nuxtOptions.bundle.dropMessageCompiler,
optimizeTranslationDirective: true,
optimizeTranslationDirective: false,
strictMessage: nuxtOptions.compilation.strictMessage,
escapeHtml: nuxtOptions.compilation.escapeHtml,
include: localeIncludePaths
Expand Down
98 changes: 96 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { defineNuxtModule } from '@nuxt/kit'
import { addTemplate, defineNuxtModule } from '@nuxt/kit'
import { setupAlias } from './alias'
import { setupPages } from './pages'
import { setupNitro } from './nitro'
import { extendBundler } from './bundler'
import { NUXT_I18N_MODULE_ID, DEFAULT_OPTIONS } from './constants'
import type { HookResult } from '@nuxt/schema'
import type { I18nPublicRuntimeConfig, LocaleObject, NuxtI18nOptions } from './types'
import type { I18nPublicRuntimeConfig, LocaleInfo, LocaleObject, LocaleType, NuxtI18nOptions } from './types'
import type { Locale } from 'vue-i18n'
import { createContext } from './context'
import { prepareOptions } from './prepare/options'
Expand All @@ -19,6 +19,11 @@ import { prepareLayers } from './prepare/layers'
import { prepareTranspile } from './prepare/transpile'
import { prepareVite } from './prepare/vite'
import { prepareTypeGeneration } from './prepare/type-generation'
import { deepCopy } from '@intlify/shared'
import { parseJSON, parseJSON5, parseYAML } from 'confbox'
import { convertToImportId, getHash, readFile } from './utils'
import { relative, resolve, parse as parsePath } from 'pathe'
import { genSafeVariableName } from 'knitwork'

export * from './types'

Expand Down Expand Up @@ -71,6 +76,95 @@ export default defineNuxtModule<NuxtI18nOptions>({
*/
await setupAlias(ctx, nuxt)

const processed: Record<string, { type: LocaleType; files: NonNullable<LocaleInfo['meta']> }[]> = {}

// Create an array of file arrays grouped by their LocaleType
for (const l of ctx.localeInfo) {
processed[l.code] ??= []
for (const f of l?.meta ?? []) {
if (processed[l.code].length === 0 || processed[l.code].at(-1)!.type !== f.type) {
processed[l.code].push({ type: f.type, files: [] })
}

processed[l.code].at(-1)!.files.push(f)
}
}

// Read and merge grouped static files and write to merged file
for (const code in processed) {
const localeChains = processed[code]

for (let entryIndex = 0; entryIndex < localeChains.length; entryIndex++) {
const entry = localeChains[entryIndex]
if (entry.type !== 'static') continue
const msg = {}

for (let i = 0; i < entry.files.length; i++) {
const f = entry.files[i]

const fileCode = await readFile(f.path)
let contents: unknown

if (/ya?ml/.test(f.parsed.ext)) {
contents = await parseYAML(fileCode)
}

if (/json5/.test(f.parsed.ext)) {
contents = await parseJSON5(fileCode)
}

if (/json$/.test(f.parsed.ext)) {
contents = await parseJSON(fileCode)
}

if (contents != null) {
deepCopy(contents, msg)
}
}

if (entry.type === 'static') {
const staticFile = resolve(nuxt.options.buildDir, `i18n/${code}-static-${entryIndex}.json`)

addTemplate({
filename: `i18n/${code}-static-${entryIndex}.json`,
write: true,
getContents() {
return JSON.stringify(msg, null, 2)
}
})

const currentLocaleInfo = ctx.localeInfo.find(localInfoEntry => localInfoEntry.code === code)!

// Find and replace source static files with generated merged file
let start = 0
let end = 0
for (let lFileIndex = 0; lFileIndex < currentLocaleInfo.files.length; lFileIndex++) {
if (entry.files.at(0)!.path === currentLocaleInfo.files[lFileIndex].path) {
start = lFileIndex
}

if (entry.files.at(-1)!.path === currentLocaleInfo.files[lFileIndex].path) {
end = lFileIndex
}
}

const staticFilePath = resolve(nuxt.options.buildDir, staticFile)
const processedStaticFile = { path: staticFilePath, cache: true }

currentLocaleInfo.files.splice(start, end + 1, processedStaticFile)
currentLocaleInfo.meta!.splice(start, end + 1, {
path: staticFilePath,
loadPath: relative(nuxt.options.buildDir, staticFilePath),
file: processedStaticFile,
hash: getHash(staticFilePath),
key: genSafeVariableName(`locale_${convertToImportId(relative(nuxt.options.buildDir, staticFilePath))}`),
parsed: parsePath(staticFilePath),
type: 'static'
})
}
}
}

/**
* add plugin and templates
*/
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const IMPORT_ID_CACHES = new Map<string, string>()

export const normalizeWithUnderScore = (name: string) => name.replace(/-/g, '_').replace(/\./g, '_').replace(/\//g, '_')

function convertToImportId(file: string) {
export function convertToImportId(file: string) {
if (IMPORT_ID_CACHES.has(file)) {
return IMPORT_ID_CACHES.get(file)
}
Expand Down
Loading