Skip to content

Commit

Permalink
refactor: use nuxt module default schem (nuxt-modules#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Oct 11, 2022
1 parent 3532f60 commit b252b78
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 13 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"@nuxt/kit": "npm:@nuxt/kit-edge@3.0.0-rc.12-27755545.c905a23",
"cookie-es": "^0.5.0",
"debug": "^4.3.2",
"defu": "^6.0.0",
"is-https": "^4.0.0",
"js-cookie": "^3.0.1",
"knitwork": "^0.1.2",
Expand Down
6 changes: 4 additions & 2 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import I18nModule from '../dist/module.mjs'
import Module1 from './module1'

import type { NuxtApp } from 'nuxt/dist/app/index'

// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
modules: [Module1, I18nModule],
Expand Down Expand Up @@ -60,10 +62,10 @@ export default defineNuxtConfig({
// // cookieKey: 'my_custom_cookie_name',
// // redirectOn: 'root'
// },
onBeforeLanguageSwitch: (oldLocale, newLocale, initial, context) => {
onBeforeLanguageSwitch: (oldLocale: string, newLocale: string, initial: boolean, context: NuxtApp) => {
console.log('onBeforeLanguageSwitch', oldLocale, newLocale, initial)
},
onLanguageSwitched: (oldLocale, newLocale) => {
onLanguageSwitched: (oldLocale: string, newLocale: string) => {
console.log('onLanguageSwitched', oldLocale, newLocale)
},
vueI18n: {
Expand Down
7 changes: 2 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ export const REDIRECT_ON_OPTIONS = {

export const COMPONENT_OPTIONS_KEY = 'nuxtI18n'

// TODO: implement comment out options

export const DEFAULT_OPTIONS = {
vueI18n: undefined,
// vueI18nLoader: false,
locales: [],
locales: [] as string[],
defaultLocale: '',
defaultDirection: 'ltr',
routesNameSeparator: '___',
Expand Down Expand Up @@ -60,10 +58,9 @@ export const DEFAULT_OPTIONS = {
parsePages: true,
pages: {},
skipSettingLocaleOnNavigate: false,
onBeforeLanguageSwitch: () => ({}),
onBeforeLanguageSwitch: () => '',
onLanguageSwitched: () => null,
debug: false
// bridge: false
} as const

export type NuxtI18nOptionsDefault = typeof DEFAULT_OPTIONS
5 changes: 2 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import createDebug from 'debug'
import { isObject, isString } from '@intlify/shared'
import { defineNuxtModule, isNuxt2, isNuxt3, getNuxtVersion, addPlugin, addTemplate, addImports } from '@nuxt/kit'
import { resolve } from 'pathe'
import defu from 'defu'
import { setupAlias } from './alias'
import { setupPages } from './pages'
import { extendMessages } from './messages'
Expand All @@ -28,9 +27,9 @@ export default defineNuxtModule<NuxtI18nOptions>({
bridge: false
}
},
defaults: {}, // TODO: should implement from `constants.ts`
defaults: DEFAULT_OPTIONS,
async setup(i18nOptions, nuxt) {
const options = defu(i18nOptions, DEFAULT_OPTIONS) as Required<NuxtI18nOptions>
const options = i18nOptions as Required<NuxtI18nOptions>
debug('options', options)

checkOptions(options)
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/gen.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const resolveNuxtI18nOptions = async (context) => {
return nuxtI18nOptions
}
export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",parsePages: true,pages: Object({}),skipSettingLocaleOnNavigate: false,onBeforeLanguageSwitch: (() => ({})),onLanguageSwitched: (() => null),debug: false})
export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",parsePages: true,pages: Object({}),skipSettingLocaleOnNavigate: false,onBeforeLanguageSwitch: (() => \\"\\"),onLanguageSwitched: (() => null),debug: false})
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
"
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineConfig({
globals: true,
clearMocks: true,
restoreMocks: true,
threads: false,
testTimeout: 300000,
deps: {
inline: [/@nuxt\/test-utils-edge/]
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,6 @@ __metadata:
changelogithub: ^0.12.4
cookie-es: ^0.5.0
debug: ^4.3.3
defu: ^6.0.0
eslint: ^8.24.0
eslint-config-prettier: ^8.5.0
eslint-plugin-prettier: ^4.0.0
Expand Down

0 comments on commit b252b78

Please sign in to comment.