nuxt-intlayer
Version:
Easily internationalize i18n your Nuxt applications with type-safe multilingual content management.
1 lines • 6.17 kB
Source Map (JSON)
{"version":3,"sources":["../../src/module.ts"],"sourcesContent":["import { prepareIntlayer, watch } from '@intlayer/chokidar';\nimport { getAlias, getConfiguration } from '@intlayer/config';\nimport { addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';\nimport type { NuxtModule } from '@nuxt/schema';\nimport { resolve } from 'path';\nimport { intlayerMiddleware, intlayerPrune } from 'vite-intlayer';\n\n// @ts-ignore fix instantiation is excessively deep and possibly infinite\nexport const module: NuxtModule = defineNuxtModule({\n meta: {\n name: 'nuxt-intlayer',\n },\n setup({}, nuxt) {\n const configuration = getConfiguration();\n\n const { optimize } = configuration.build;\n /**\n * -------------------------------------------------\n * RUNTIME PLUGIN REGISTRATION\n * -------------------------------------------------\n * Automatically install `vue-intlayer` on the client\n * so developers don't need to add the plugin manually\n * in every Nuxt project.\n */\n const resolver = createResolver(import.meta.url);\n\n addPlugin({\n src: resolver.resolve('./runtime/intlayer-plugin'),\n mode: 'all',\n });\n\n addPlugin({\n src: resolver.resolve('./runtime/html-lang'),\n mode: 'all',\n });\n\n // // Inject the intlayer middleware plugin into Nuxt's Vite config\n nuxt.hook('vite:extendConfig', (viteConfig, { isServer }) => {\n if (isServer) {\n // We only need the middleware on the server side during development\n // viteConfig.plugins can be undefined at this stage\n (viteConfig.plugins ?? []).push(intlayerMiddleware() as any);\n }\n if (optimize) {\n // viteConfig.plugins can be undefined at this stage\n viteConfig.plugins?.push(intlayerPrune(configuration) as any);\n }\n });\n\n // // Equivalent to buildStart in Vite plugin\n nuxt.hook('build:before', async () => prepareIntlayer(configuration));\n\n // // Equivalent to configureServer in Vite plugin\n nuxt.hook('ready', async () => {\n if (configuration.content.watch) {\n // Start watching when dev server is ready\n watch({ configuration });\n }\n });\n\n nuxt.options.alias = {\n ...nuxt.options.alias,\n ...getAlias({\n configuration,\n formatter: (value: string) => resolve(value),\n }),\n };\n\n // After setting up aliases, extend Nuxt pages with locale-aware routes\n nuxt.hook('pages:extend', (pages) => {\n const {\n internationalization: { locales, defaultLocale },\n middleware: { prefixDefault, noPrefix },\n } = configuration;\n\n // If noPrefix strategy is enabled we keep Nuxt original routing untouched\n if (noPrefix) return;\n\n // Build a RegExp string with supported locales (e.g. \"en|fr|de\") to ensure the param only accepts known locales\n const filteredLocales = prefixDefault\n ? locales\n : locales.filter((locale) => locale !== defaultLocale);\n const localeGroupRegex = filteredLocales.map(String).join('|');\n\n // If no locales remain to prefix (e.g., only default locale and prefixDefault is false) skip extension\n if (!localeGroupRegex) return;\n\n // Clone the original page list to avoid infinite loops when pushing\n const originalPages = [...pages];\n\n for (const page of originalPages) {\n // Skip already localised routes (prevent double processing)\n if (page.path.startsWith('/:locale')) continue;\n\n // Determine the paths we need to add depending on prefixDefault flag\n // We always add the dynamic \":locale\" prefixed variant so that `/fr/about` works\n const dynPathPrefix = `/:locale(${localeGroupRegex})`;\n const isIndex = page.path === '/';\n const prefixedPath = isIndex\n ? `${dynPathPrefix}`\n : `${dynPathPrefix}${page.path}`;\n\n // If prefixDefault is false and this is the default locale, we keep the original\n // route (already in pages) and add the dynamic variant which will match non default\n // locales. When prefixDefault is true, we still keep the original route for SSR\n // convenience (middleware will redirect anyway) but also expose locale variants.\n\n // Create a shallow copy of the page with the new path\n pages.push({\n ...page,\n path: prefixedPath,\n name: page.name ? `${page.name}___i18n` : undefined,\n });\n }\n });\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,gBAAAA;AAAA;AAAA;AAAA,sBAAuC;AACvC,oBAA2C;AAC3C,iBAA4D;AAE5D,kBAAwB;AACxB,2BAAkD;AALlD;AAQO,MAAMA,cAAqB,6BAAiB;AAAA,EACjD,MAAM;AAAA,IACJ,MAAM;AAAA,EACR;AAAA,EACA,MAAM,CAAC,GAAG,MAAM;AACd,UAAM,oBAAgB,gCAAiB;AAEvC,UAAM,EAAE,SAAS,IAAI,cAAc;AASn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