Skip to content

Commit

Permalink
refactor: cleanup bundler plugin logic (#3109)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Sep 18, 2024
1 parent d63328c commit 7802795
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 78 deletions.
77 changes: 23 additions & 54 deletions src/bundler.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -25,17 +22,9 @@ export async function extendBundler(nuxt: Nuxt, nuxtOptions: Required<NuxtI18nOp
nuxtOptions?.i18nModules?.map(module => 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
}

Expand All @@ -55,36 +44,22 @@ export async function extendBundler(nuxt: Nuxt, nuxtOptions: Required<NuxtI18nOp
dropMessageCompiler: nuxtOptions.bundle.dropMessageCompiler,
optimizeTranslationDirective: true,
strictMessage: nuxtOptions.compilation.strictMessage,
escapeHtml: nuxtOptions.compilation.escapeHtml
}

if (localePaths.length > 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)
}
Expand All @@ -104,27 +79,21 @@ export async function extendBundler(nuxt: Nuxt, nuxtOptions: Required<NuxtI18nOp
strictMessage: nuxtOptions.compilation.strictMessage,
escapeHtml: nuxtOptions.compilation.escapeHtml,
defaultSFCLang: nuxtOptions.customBlocks.defaultSFCLang,
globalSFCScope: nuxtOptions.customBlocks.globalSFCScope
}
if (localePaths.length > 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)
})
}
Expand Down
15 changes: 5 additions & 10 deletions src/nitro.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions src/transform/i18n-function-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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',
Expand Down
6 changes: 2 additions & 4 deletions src/transform/macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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',
Expand Down
6 changes: 2 additions & 4 deletions src/transform/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/transform/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7802795

Please sign in to comment.