Skip to content

Commit

Permalink
refactor: improve error reporting on wrong configuration when using lazy
Browse files Browse the repository at this point in the history
Resolves #1101
  • Loading branch information
rchl committed Mar 9, 2021
1 parent 3ea3d4d commit 5a8b1d9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/core/hooks.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 0 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 5a8b1d9

Please sign in to comment.