Skip to content

Commit

Permalink
fix(lazy): error importing file when having imports within "locale.fi…
Browse files Browse the repository at this point in the history
…le" (#836)

Resolves #835
  • Loading branch information
rchl authored Aug 5, 2020
2 parents b5f6d2c + deac338 commit fbc0bc6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 58 deletions.
31 changes: 0 additions & 31 deletions src/core/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,11 @@ import { MODULE_NAME, ROOT_DIR, LOCALE_CODE_KEY, LOCALE_ISO_KEY, LOCALE_DOMAIN_K
export async function buildHook (moduleContainer, options) {
const nuxtOptions = moduleContainer.options

let defaultLangFile
let hasNonDefaultLangFiles = false

// Copy lang files to the build directory.
if (options.langDir) {
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 +26,6 @@ export async function buildHook (moduleContainer, options) {
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY,
defaultLangFile,
hasNonDefaultLangFiles,
localeCodes,
trailingSlash
}
Expand Down
15 changes: 8 additions & 7 deletions src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,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
1 change: 0 additions & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('locales as string array', () => {
// Override those after merging to overwrite original values.
testConfig.i18n.locales = ['en', 'fr']

console.info({ testConfig })
nuxt = (await setup(testConfig)).nuxt
})

Expand Down

0 comments on commit fbc0bc6

Please sign in to comment.