diff --git a/src/bundler.ts b/src/bundler.ts index d86ebd8c6..5840cf6d4 100644 --- a/src/bundler.ts +++ b/src/bundler.ts @@ -1,20 +1,17 @@ import createDebug from 'debug' import { resolve } from 'pathe' -import { extendWebpackConfig, extendViteConfig, addWebpackPlugin, addVitePlugin } from '@nuxt/kit' +import { extendViteConfig, addWebpackPlugin, addVitePlugin } from '@nuxt/kit' import VueI18nWebpackPlugin from '@intlify/unplugin-vue-i18n/webpack' import VueI18nVitePlugin from '@intlify/unplugin-vue-i18n/vite' import { TransformMacroPlugin } from './transform/macros' import { ResourcePlugin } from './transform/resource' import { TransformI18nFunctionPlugin } from './transform/i18n-function-injection' -import { assign } from '@intlify/shared' import { getLayerLangPaths } from './layers' import type { Nuxt } from '@nuxt/schema' import type { PluginOptions } from '@intlify/unplugin-vue-i18n' import type { NuxtI18nOptions } from './types' -import type { TransformMacroPluginOptions } from './transform/macros' -import type { ResourcePluginOptions } from './transform/resource' -import type { TransformI18nFunctionPluginOptions } from './transform/i18n-function-injection' +import type { BundlerPluginOptions } from './transform/utils' const debug = createDebug('@nuxtjs/i18n:bundler') @@ -25,17 +22,9 @@ export async function extendBundler(nuxt: Nuxt, nuxtOptions: Required resolve(nuxt.options._layers[0].config.rootDir, module.langDir ?? '')) ?? [] debug('i18nModulePaths -', i18nModulePaths) const localePaths = [...langPaths, ...i18nModulePaths] + const localeIncludePaths = localePaths.length ? localePaths.map(x => resolve(x, './**')) : undefined - // extract macros from components - const macroOptions: TransformMacroPluginOptions = { - sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client - } - - const resourceOptions: ResourcePluginOptions = { - sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client - } - - const i18nFunctionOptions: TransformI18nFunctionPluginOptions = { + const sourceMapOptions: BundlerPluginOptions = { sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client } @@ -55,36 +44,22 @@ export async function extendBundler(nuxt: Nuxt, nuxtOptions: Required 0) { - webpackPluginOptions.include = localePaths.map(x => resolve(x, './**')) + escapeHtml: nuxtOptions.compilation.escapeHtml, + include: localeIncludePaths } addWebpackPlugin(VueI18nWebpackPlugin(webpackPluginOptions)) - addWebpackPlugin(TransformMacroPlugin.webpack(macroOptions)) - addWebpackPlugin(ResourcePlugin.webpack(resourceOptions)) + addWebpackPlugin(TransformMacroPlugin.webpack(sourceMapOptions)) + addWebpackPlugin(ResourcePlugin.webpack(sourceMapOptions)) if (nuxtOptions.experimental.autoImportTranslationFunctions) { - addWebpackPlugin(TransformI18nFunctionPlugin.webpack(i18nFunctionOptions)) + addWebpackPlugin(TransformI18nFunctionPlugin.webpack(sourceMapOptions)) } - - extendWebpackConfig(config => { - config.plugins!.push( - new webpack.DefinePlugin( - assign( - getFeatureFlags({ - compositionOnly: nuxtOptions.bundle.compositionOnly, - fullInstall: nuxtOptions.bundle.fullInstall, - dropMessageCompiler: nuxtOptions.bundle.dropMessageCompiler - }), - { - __DEBUG__: String(!!nuxtOptions.debug) - } - ) - ) - ) - }) + addWebpackPlugin( + new webpack.DefinePlugin({ + ...getFeatureFlags(nuxtOptions.bundle), + __DEBUG__: String(!!nuxtOptions.debug) + }) + ) } catch (e: unknown) { debug((e as Error).message) } @@ -104,27 +79,21 @@ export async function extendBundler(nuxt: Nuxt, nuxtOptions: Required 0) { - vitePluginOptions.include = localePaths.map(x => resolve(x, './**')) + globalSFCScope: nuxtOptions.customBlocks.globalSFCScope, + include: localeIncludePaths } addVitePlugin(VueI18nVitePlugin(vitePluginOptions)) - addVitePlugin(TransformMacroPlugin.vite(macroOptions)) - addVitePlugin(ResourcePlugin.vite(resourceOptions)) + addVitePlugin(TransformMacroPlugin.vite(sourceMapOptions)) + addVitePlugin(ResourcePlugin.vite(sourceMapOptions)) if (nuxtOptions.experimental.autoImportTranslationFunctions) { - addVitePlugin(TransformI18nFunctionPlugin.vite(i18nFunctionOptions)) + addVitePlugin(TransformI18nFunctionPlugin.vite(sourceMapOptions)) } extendViteConfig(config => { - if (config.define) { - config.define['__DEBUG__'] = JSON.stringify(!!nuxtOptions.debug) - } else { - config.define = { - __DEBUG__: JSON.stringify(!!nuxtOptions.debug) - } - } + config.define ??= {} + config.define['__DEBUG__'] = JSON.stringify(!!nuxtOptions.debug) + debug('vite.config.define', config.define) }) } diff --git a/src/nitro.ts b/src/nitro.ts index d200ff38d..5b956b90e 100644 --- a/src/nitro.ts +++ b/src/nitro.ts @@ -1,5 +1,4 @@ import createDebug from 'debug' -import { assign, isArray } from '@intlify/shared' import { resolveModuleExportNames } from 'mlly' import { defu } from 'defu' import { resolve } from 'pathe' @@ -50,7 +49,7 @@ export async function setupNitro( // eslint-disable-next-line @typescript-eslint/no-floating-promises -- FIXME nitroConfig.rollupConfig.plugins = (await nitroConfig.rollupConfig.plugins) || [] // eslint-disable-next-line @typescript-eslint/no-floating-promises -- FIXME - nitroConfig.rollupConfig.plugins = isArray(nitroConfig.rollupConfig.plugins) + nitroConfig.rollupConfig.plugins = Array.isArray(nitroConfig.rollupConfig.plugins) ? nitroConfig.rollupConfig.plugins : [nitroConfig.rollupConfig.plugins] @@ -114,14 +113,10 @@ export { localeDetector } if (nuxt.options.ssr) { // vue-i18n feature flags configuration for server-side (server api, server middleware, etc...) - nitroConfig.replace = assign( - nitroConfig.replace, - getFeatureFlags({ - compositionOnly: nuxtOptions.bundle.compositionOnly, - fullInstall: nuxtOptions.bundle.fullInstall, - dropMessageCompiler: nuxtOptions.bundle.dropMessageCompiler - }) - ) + nitroConfig.replace = { + ...nitroConfig.replace, + ...getFeatureFlags(nuxtOptions.bundle) + } } // setup debug flag diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index a98424f15..0a64c33a8 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -306,7 +306,7 @@ export async function navigate( } // TODO: resolve type errors for nuxt context extensions - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + redirectPath = nuxtApp.$localePath(redirectPath, locale) __DEBUG__ && logger.log('rootRedirect mode', { redirectPath, status }) return _navigate(redirectPath, status) diff --git a/src/transform/i18n-function-injection.ts b/src/transform/i18n-function-injection.ts index 462590d45..76272dbce 100644 --- a/src/transform/i18n-function-injection.ts +++ b/src/transform/i18n-function-injection.ts @@ -15,10 +15,7 @@ import { isVue } from './utils' import type { Node } from 'estree-walker' import type { CallExpression, Pattern } from 'estree' - -export interface TransformI18nFunctionPluginOptions { - sourcemap?: boolean -} +import type { BundlerPluginOptions } from './utils' const debug = createDebug('@nuxtjs/i18n:function:injection') @@ -33,7 +30,7 @@ const TRANSLATION_FUNCTIONS_MAP: Record<(typeof TRANSLATION_FUNCTIONS)[number], $te: 'te: $te' } -export const TransformI18nFunctionPlugin = createUnplugin((options: TransformI18nFunctionPluginOptions) => { +export const TransformI18nFunctionPlugin = createUnplugin((options: BundlerPluginOptions) => { return { name: 'nuxtjs:i18n-function-injection', enforce: 'pre', diff --git a/src/transform/macros.ts b/src/transform/macros.ts index 8d7cc7c59..0e6924f44 100644 --- a/src/transform/macros.ts +++ b/src/transform/macros.ts @@ -13,9 +13,7 @@ import { parse as parseSFC } from '@vue/compiler-sfc' import { VIRTUAL_PREFIX_HEX, isVue } from './utils' import { NUXT_I18N_COMPOSABLE_DEFINE_ROUTE } from '../constants' -export interface TransformMacroPluginOptions { - sourcemap?: boolean -} +import type { BundlerPluginOptions } from './utils' const debug = createDebug('@nuxtjs/i18n:transform:macros') @@ -25,7 +23,7 @@ const debug = createDebug('@nuxtjs/i18n:transform:macros') * ref: https://github.com/posva/unplugin-vue-router */ -export const TransformMacroPlugin = createUnplugin((options: TransformMacroPluginOptions) => { +export const TransformMacroPlugin = createUnplugin((options: BundlerPluginOptions) => { return { name: 'nuxtjs:i18n-macros-transform', enforce: 'pre', diff --git a/src/transform/resource.ts b/src/transform/resource.ts index 23f69fd90..e0552f8fe 100644 --- a/src/transform/resource.ts +++ b/src/transform/resource.ts @@ -6,13 +6,11 @@ import MagicString from 'magic-string' import { VIRTUAL_PREFIX_HEX } from './utils' import { NUXT_I18N_COMPOSABLE_DEFINE_LOCALE, NUXT_I18N_COMPOSABLE_DEFINE_CONFIG } from '../constants' -export interface ResourcePluginOptions { - sourcemap?: boolean -} +import type { BundlerPluginOptions } from './utils' const debug = createDebug('@nuxtjs/i18n:transform:resource') -export const ResourcePlugin = createUnplugin((options: ResourcePluginOptions) => { +export const ResourcePlugin = createUnplugin((options: BundlerPluginOptions) => { debug('options', options) return { diff --git a/src/transform/utils.ts b/src/transform/utils.ts index 315978d4e..633f53f40 100644 --- a/src/transform/utils.ts +++ b/src/transform/utils.ts @@ -52,3 +52,7 @@ export function isVue(id: string, opts: { type?: Array<'template' | 'script' | ' // Query `?vue&type=template` (in Webpack or external template) return true } + +export type BundlerPluginOptions = { + sourcemap?: boolean +}