From 5a8b1d9464c18701304453d1358ba8ef182aa8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Tue, 9 Mar 2021 15:03:36 +0100 Subject: [PATCH] refactor: improve error reporting on wrong configuration when using lazy Resolves #1101 --- src/core/hooks.js | 15 ++++++++++++--- src/index.js | 5 ----- src/templates/options.js | 2 +- src/templates/utils.js | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/core/hooks.js b/src/core/hooks.js index bdd68184a..415507ed7 100644 --- a/src/core/hooks.js +++ b/src/core/hooks.js @@ -1,4 +1,4 @@ -import { MODULE_NAME, STRATEGIES } from '../helpers/constants' +import { MODULE_NAME, STRATEGIES, LOCALE_FILE_KEY } from '../helpers/constants' export function createExtendRoutesHook (moduleContainer, options) { const nuxtOptions = moduleContainer.options @@ -29,10 +29,19 @@ export function createExtendRoutesHook (moduleContainer, options) { } export function buildHook (moduleContainer, options) { - 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') { - console.error('[' + MODULE_NAME + '] When using "langDir" option, the "locales" option must be a list of objects') + 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 = moduleContainer.nuxt.resolver.resolveAlias(options.langDir) } if (options.strategy === STRATEGIES.NO_PREFIX && options.differentDomains) { diff --git a/src/index.js b/src/index.js index e72d4a07b..b8313655a 100644 --- a/src/index.js +++ b/src/index.js @@ -21,11 +21,6 @@ export default function (userOptions) { return } - // Resolve langDir - if (options.langDir) { - options.langDir = this.nuxt.resolver.resolveAlias(options.langDir) - } - // Templates (including plugins). // This is done here rather than in the build hook to ensure the order the plugins are added // is predictable between different modules. diff --git a/src/templates/options.js b/src/templates/options.js index c0f91341b..8ae89a38d 100644 --- a/src/templates/options.js +++ b/src/templates/options.js @@ -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}" */)`)) diff --git a/src/templates/utils.js b/src/templates/utils.js index 084b31e2e..0e19695ce 100755 --- a/src/templates/utils.js +++ b/src/templates/utils.js @@ -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' @@ -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