Skip to content

Commit

Permalink
refactor: improve error reporting on wrong configuration when using l…
Browse files Browse the repository at this point in the history
…azy (#1102)

Resolves #1101
  • Loading branch information
rchl authored Mar 9, 2021
2 parents 3ea3d4d + 398c02d commit c658024
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
6 changes: 0 additions & 6 deletions src/core/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ export function createExtendRoutesHook (moduleContainer, options) {
}

export function buildHook (moduleContainer, options) {
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')
}
}

if (options.strategy === STRATEGIES.NO_PREFIX && options.differentDomains) {
// eslint-disable-next-line no-console
console.warn('[' + MODULE_NAME + '] The `differentDomains` option and `no_prefix` strategy are not compatible. Change strategy or disable `differentDomains` option.')
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ export default function (userOptions) {
return
}

// Resolve langDir
if (options.langDir) {
if (options.lazy) {
if (!options.langDir) {
throw new Error(`[${MODULE_NAME}] When using the "lazy" option you must also set the "langDir" option.`)
}
if (!options.locales.length || typeof options.locales[0] === 'string') {
throw new Error(`[${MODULE_NAME}] When using the "langDir" option the "locales" option must be a list of objects.`)
}
for (const locale of options.locales) {
if (!locale[LOCALE_FILE_KEY]) {
throw new Error(`[${MODULE_NAME}] All locale objects must have the "file" property set when using "lazy".\nFound none in:\n${JSON.stringify(locale, null, 2)}.`)
}
}
options.langDir = this.nuxt.resolver.resolveAlias(options.langDir)
}

Expand Down
2 changes: 1 addition & 1 deletion src/templates/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ for (const [key, value] of Object.entries(options)) {
}
%>

<% if (options.langDir) { %>
<% if (options.lazy && options.langDir) { %>
export const ASYNC_LOCALES = {
<%= Array.from(
new Set(options.locales.map(l => `'${l.file}': () => import('../${relativeToBuild(options.langDir, l.file)}' /* webpackChunkName: "lang-${l.file}" */)`))
Expand Down
4 changes: 2 additions & 2 deletions src/templates/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
LOCALE_CODE_KEY,
LOCALE_FILE_KEY,
MODULE_NAME/* <% if (options.langDir) { %> */,
MODULE_NAME/* <% if (options.lazy && options.langDir) { %> */,
ASYNC_LOCALES/* <% } %> */
} from './options'

Expand All @@ -22,7 +22,7 @@ export async function loadLanguageAsync (context, locale) {
if (localeObject) {
const file = localeObject[LOCALE_FILE_KEY]
if (file) {
/* <% if (options.langDir) { %> */
/* <% if (options.lazy && options.langDir) { %> */
let messages
if (process.client) {
const { nuxtState } = context
Expand Down

0 comments on commit c658024

Please sign in to comment.