Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webpack bundler #3123

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ declare module '#internal/i18n/locale.detector.mjs' {
export const localeDetector: LocaleDetector
}

declare module 'virtual:nuxt-i18n-logger' {
declare module '~nuxt-i18n/logger' {
import type { ConsolaInstance } from 'consola'

export function createLogger(label: string): ConsolaInstance
Expand Down
5 changes: 4 additions & 1 deletion src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TransformMacroPlugin } from './transform/macros'
import { ResourcePlugin } from './transform/resource'
import { TransformI18nFunctionPlugin } from './transform/i18n-function-injection'
import { getLayerLangPaths } from './layers'
// import { I18nVirtualLoggerPlugin } from './virtual-logger'

import type { Nuxt } from '@nuxt/schema'
import type { PluginOptions } from '@intlify/unplugin-vue-i18n'
Expand Down Expand Up @@ -42,12 +43,13 @@ export async function extendBundler({ options: nuxtOptions }: I18nNuxtContext, n
compositionOnly: nuxtOptions.bundle.compositionOnly,
onlyLocales: nuxtOptions.bundle.onlyLocales,
dropMessageCompiler: nuxtOptions.bundle.dropMessageCompiler,
optimizeTranslationDirective: true,
optimizeTranslationDirective: false,
strictMessage: nuxtOptions.compilation.strictMessage,
escapeHtml: nuxtOptions.compilation.escapeHtml,
include: localeIncludePaths
}

// addWebpackPlugin(I18nVirtualLoggerPlugin.webpack({ debug: nuxt.options.debug }))
addWebpackPlugin(VueI18nWebpackPlugin(webpackPluginOptions))
addWebpackPlugin(TransformMacroPlugin.webpack(sourceMapOptions))
addWebpackPlugin(ResourcePlugin.webpack(sourceMapOptions))
Expand Down Expand Up @@ -83,6 +85,7 @@ export async function extendBundler({ options: nuxtOptions }: I18nNuxtContext, n
include: localeIncludePaths
}

// addVitePlugin(I18nVirtualLoggerPlugin.vite({ debug: nuxt.options.debug }))
addVitePlugin(VueI18nVitePlugin(vitePluginOptions))
addVitePlugin(TransformMacroPlugin.vite(sourceMapOptions))
addVitePlugin(ResourcePlugin.vite(sourceMapOptions))
Expand Down
4 changes: 2 additions & 2 deletions src/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import type { Nuxt } from '@nuxt/schema'
import type { LocaleInfo } from './types'
import { resolveI18nDir } from './layers'
import { i18nVirtualLoggerPlugin } from './virtual-logger'
import { I18nVirtualLoggerPlugin } from './virtual-logger'
import type { I18nNuxtContext } from './context'

const debug = createDebug('@nuxtjs/i18n:nitro')
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function setupNitro(
: [nitroConfig.rollupConfig.plugins]

// @ts-ignore NOTE: A type error occurs due to a mismatch between Vite plugins and those of Rollup
nitroConfig.rollupConfig.plugins.push(i18nVirtualLoggerPlugin(nuxtOptions.debug))
nitroConfig.rollupConfig.plugins.push(I18nVirtualLoggerPlugin.rollup({ debug: nuxtOptions.debug }))

const yamlPaths = getResourcePaths(additionalParams.localeInfo, /\.ya?ml$/)
if (yamlPaths.length > 0) {
Expand Down
17 changes: 13 additions & 4 deletions src/prepare/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Nuxt } from '@nuxt/schema'
import type { I18nNuxtContext } from '../context'
import { i18nVirtualLoggerPlugin, RESOLVED_VIRTUAL_NUXT_I18N_LOGGER, VIRTUAL_NUXT_I18N_LOGGER } from '../virtual-logger'
import { I18nVirtualLoggerPlugin, RESOLVED_VIRTUAL_NUXT_I18N_LOGGER, VIRTUAL_NUXT_I18N_LOGGER } from '../virtual-logger'
import { defu } from 'defu'
import { addPlugin, addTemplate, addTypeTemplate } from '@nuxt/kit'
import { addPlugin, addTemplate, addTypeTemplate, addVitePlugin, addWebpackPlugin } from '@nuxt/kit'
import { generateTemplateNuxtI18nOptions } from '../template'
import { generateI18nTypes, generateLoaderOptions, simplifyLocaleOptions } from '../gen'
import { NUXT_I18N_TEMPLATE_OPTIONS_KEY } from '../constants'
Expand Down Expand Up @@ -53,12 +53,21 @@ export function prepareRuntime(ctx: I18nNuxtContext, nuxt: Nuxt) {
nuxt.options.imports.transform.include ??= []
nuxt.options.imports.transform.include.push(new RegExp(`${RESOLVED_VIRTUAL_NUXT_I18N_LOGGER}$`))

nuxt.hook('vite:extendConfig', cfg => {
addVitePlugin(I18nVirtualLoggerPlugin.vite({ debug: options.debug }))
addWebpackPlugin(I18nVirtualLoggerPlugin.webpack({ debug: options.debug }))

/*nuxt.hook('vite:extendConfig', cfg => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
cfg.plugins ||= []
// @ts-ignore NOTE: A type error occurs due to a mismatch between Vite plugins and those of Rollup
cfg.plugins.push(i18nVirtualLoggerPlugin(options.debug))
cfg.plugins.push(I18nVirtualLoggerPlugin.vite({ debug: options.debug }))
})
nuxt.hook('webpack:config', cfg => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
cfg.plugins ||= []
// @ts-ignore NOTE: A type error occurs due to a mismatch between Vite plugins and those of Rollup
cfg.plugins.push(I18nVirtualLoggerPlugin.webpack({ debug: options.debug }))
})*/

/**
* `$i18n` type narrowing based on 'legacy' or 'composition'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { NUXT_I18N_MODULE_ID, DEFAULT_COOKIE_KEY, isSSG, localeCodes, normalizedLocales } from '#build/i18n.options.mjs'
import { findBrowserLocale, getLocalesRegex, getRouteName } from './routing/utils'
import { initCommonComposableOptions, type CommonComposableOptions } from './utils'
import { createLogger } from 'virtual:nuxt-i18n-logger'
import { createLogger } from '~nuxt-i18n/logger'

import type { Locale } from 'vue-i18n'
import type { DetectBrowserLanguageOptions, LocaleObject } from './shared-types'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepCopy, isFunction, isArray, isObject, isString } from '@intlify/shared'
import { createLogger } from 'virtual:nuxt-i18n-logger'
import { createLogger } from '~nuxt-i18n/logger'

import type { I18nOptions, Locale, FallbackLocale, LocaleMessages, DefineLocaleMessage } from 'vue-i18n'
import type { NuxtApp } from '#app'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { inBrowser, resolveBaseUrl } from '../routing/utils'
import { extendI18n, createLocaleFromRouteGetter } from '../routing/extends'
import { setLocale, getLocale, mergeLocaleMessage, setLocaleProperty } from '../compatibility'
import { createLogger } from 'virtual:nuxt-i18n-logger'
import { createLogger } from '~nuxt-i18n/logger'

import type { NuxtI18nPluginInjections } from '../injections'
import type { Locale, I18nOptions } from 'vue-i18n'
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
setLocaleProperty,
setLocaleCookie
} from './compatibility'
import { createLogger } from 'virtual:nuxt-i18n-logger'
import { createLogger } from '~nuxt-i18n/logger'
import { createLocaleFromRouteGetter } from './routing/extends/router'

import type { I18n, Locale } from 'vue-i18n'
Expand Down
21 changes: 12 additions & 9 deletions src/virtual-logger.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import type { Plugin } from 'vite'
import { createUnplugin } from 'unplugin'

export const VIRTUAL_NUXT_I18N_LOGGER = 'virtual:nuxt-i18n-logger'
export const VIRTUAL_NUXT_I18N_LOGGER = '~nuxt-i18n/logger'
export const RESOLVED_VIRTUAL_NUXT_I18N_LOGGER = `\0${VIRTUAL_NUXT_I18N_LOGGER}`

export function i18nVirtualLoggerPlugin(debug: boolean | 'verbose') {
return <Plugin>{
export const I18nVirtualLoggerPlugin = createUnplugin<{ debug: boolean | 'verbose' }>(options => {
return {
name: 'nuxtjs:i18n-logger',
enforce: 'pre',
resolveId(id) {
if (id === VIRTUAL_NUXT_I18N_LOGGER) return RESOLVED_VIRTUAL_NUXT_I18N_LOGGER
return id === VIRTUAL_NUXT_I18N_LOGGER ? RESOLVED_VIRTUAL_NUXT_I18N_LOGGER : undefined
},
loadInclude(id) {
return id === RESOLVED_VIRTUAL_NUXT_I18N_LOGGER
},
load(id) {
if (id !== RESOLVED_VIRTUAL_NUXT_I18N_LOGGER) return
if (id !== RESOLVED_VIRTUAL_NUXT_I18N_LOGGER) return undefined

// return stub if debug logging is disabled
if (!debug) {
if (!options.debug) {
return `export function createLogger() {}`
}

return `
import { createConsola } from 'consola'

const debugLogger = createConsola({ level: ${debug === 'verbose' ? 999 : 4} }).withTag('i18n')
const debugLogger = createConsola({ level: ${options.debug === 'verbose' ? 999 : 4} }).withTag('i18n')

export function createLogger(label) {
return debugLogger.withTag(label)
}`
}
}
}
})