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

fix(lazy): error importing file when having imports within "locale.file" #836

Merged
merged 3 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(lazy): error importing file when having imports within "locale.file"
Resolves #835
  • Loading branch information
rchl committed Aug 5, 2020
commit 3cb1d2af38cf3f14e5b2b942d9b0ef528af00b09
28 changes: 0 additions & 28 deletions src/core/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@ export async function buildHook (moduleContainer, options) {
if (!options.locales.length || typeof options.locales[0] === 'string') {
console.error('[' + MODULE_NAME + '] When using "langDir" option, the "locales" option must be a list of objects')
}

const uniqueFiles = new Set(options.locales.map(locale => locale.file))

if (options.defaultLocale) {
const defaultLocaleObject = options.locales.find(locale => locale.code === options.defaultLocale)
if (defaultLocaleObject) {
if (defaultLocaleObject.file) {
defaultLangFile = defaultLocaleObject.file
} else {
console.error(`[${MODULE_NAME}] Default locale is missing the "file" property (required when using "langDir" option)`)
}
} else {
console.error(`[${MODULE_NAME}] Default locale is set to "${options.defaultLocale}" but no locale with that "code" was found`)
}
}

for (const file of uniqueFiles) {
const isUsingDefaultLangFile = file === defaultLangFile
moduleContainer.addTemplate({
src: resolve(nuxtOptions.srcDir, options.langDir, file),
fileName: join(ROOT_DIR, (isUsingDefaultLangFile ? 'default-lang' : 'langs'), file)
})
if (!isUsingDefaultLangFile) {
hasNonDefaultLangFiles = true
}
}
}

const localeCodes = getLocaleCodes(options.locales)
Expand All @@ -55,8 +29,6 @@ export async function buildHook (moduleContainer, options) {
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY,
defaultLangFile,
hasNonDefaultLangFiles,
localeCodes,
trailingSlash
}
Expand Down
16 changes: 9 additions & 7 deletions src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
lazy,
LOCALE_CODE_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
localeCodes,
locales,
MODULE_NAME,
Expand Down Expand Up @@ -49,14 +50,15 @@ export default async (context) => {

if (process.server && lazy) {
context.beforeNuxtRender(({ nuxtState }) => {
const { locale, locales, defaultLocale } = app.i18n
// Add current locale if it's not the default one (that one is included in the main bundle).
if (locale && locale !== defaultLocale) {
const localeObject = locales.find(l => l[LOCALE_CODE_KEY] === locale)
if (localeObject && localeObject.file) {
nuxtState.__i18n = { langs: { [localeObject.file]: app.i18n.getLocaleMessage(app.i18n.locale) } }
}
const langs = {}
const { locale } = app.i18n
if (locale) {
langs[locale] = app.i18n.getLocaleMessage(locale)
}
if (defaultLocale && locale !== defaultLocale) {
langs[defaultLocale] = app.i18n.getLocaleMessage(defaultLocale)
}
nuxtState.__i18n = { langs }
})
}

Expand Down
23 changes: 4 additions & 19 deletions src/templates/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import {
LOCALE_CODE_KEY,
LOCALE_FILE_KEY,
MODULE_NAME,
defaultLangFile
} from './options'
/* <% if (options.defaultLangFile) { %> */
import defaultLangModule from './default-lang/<%= options.defaultLangFile %>'
/* <% } %> */
import { LOCALE_CODE_KEY, LOCALE_FILE_KEY, MODULE_NAME } from './options'

/**
* Asynchronously load messages from translation files
Expand All @@ -30,20 +22,13 @@ export async function loadLanguageAsync (context, locale) {
let messages
if (process.client) {
const { nuxtState } = context
if (nuxtState.__i18n && nuxtState.__i18n.langs[file]) {
messages = nuxtState.__i18n.langs[file]
if (nuxtState.__i18n && nuxtState.__i18n.langs[locale]) {
messages = nuxtState.__i18n.langs[locale]
}
}
if (!messages) {
try {
let langFileModule
if (file === defaultLangFile) {
langFileModule = defaultLangModule
} else {
/* <% if (options.hasNonDefaultLangFiles) { %> */
langFileModule = await import(/* webpackChunkName: "lang-[request]" */ `./langs/${file}`)
/* <% } %> */
}
const langFileModule = await import(/* webpackChunkName: "lang-[request]" */ `~/<%= options.langDir %>${file}`)
const getter = langFileModule.default || langFileModule
messages = typeof getter === 'function' ? await Promise.resolve(getter(context, locale)) : getter
} catch (error) {
Expand Down