From fb7a16c146a15546d256447e7fcc380f6a0bbd48 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Fri, 29 Sep 2023 11:21:17 +0900 Subject: [PATCH] feat: `ssr` option (#315) --- packages/unplugin-vue-i18n/README.md | 10 ++++++ packages/unplugin-vue-i18n/src/index.ts | 47 ++++++++++++++++++------- packages/unplugin-vue-i18n/src/types.ts | 1 + 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/packages/unplugin-vue-i18n/README.md b/packages/unplugin-vue-i18n/README.md index 28aea78..3df3177 100644 --- a/packages/unplugin-vue-i18n/README.md +++ b/packages/unplugin-vue-i18n/README.md @@ -377,6 +377,16 @@ This option works with vue-i18n v9.3 and later. > ⚠️ NOTE: If you enable this option, **you should check resources in your application are pre-compiled with this plugin.** If you will be loading resources dynamically from the back-end via the API, enabling this option do not work because there is not message compiler. +### `ssr` + +- **Type:** `boolean` +- **Default:** `false` + + Whether to bundle vue-i18n module for SSR at build time + + > ⚠️ NOTE: + This option works with vue-i18n v9.4 and later. + ### `runtimeOnly` - **Type:** `boolean` diff --git a/packages/unplugin-vue-i18n/src/index.ts b/packages/unplugin-vue-i18n/src/index.ts index 129cbc3..2359e85 100644 --- a/packages/unplugin-vue-i18n/src/index.ts +++ b/packages/unplugin-vue-i18n/src/index.ts @@ -114,6 +114,9 @@ export const unplugin = createUnplugin((options = {}, meta) => { : false debug('fullInstall', fullInstall) + const ssrBuild = !!options.ssr + debug('ssr', ssrBuild) + const useVueI18nImportName = options.useVueI18nImportName if (useVueI18nImportName != null) { warn(`'useVueI18nImportName' option is experimental`) @@ -131,10 +134,16 @@ export const unplugin = createUnplugin((options = {}, meta) => { const getVueI18nBridgeAliasPath = () => `vue-i18n-bridge/dist/vue-i18n-bridge.runtime.esm-bundler.js` - const getVueI18nAliasPath = (aliasName: string) => - vueI18nVersion === '8' + const getVueI18nAliasPath = ( + aliasName: string, + { ssr = false, runtimeOnly = false } + ) => { + return vueI18nVersion === '8' ? `${aliasName}/dist/${aliasName}.esm.js` // for vue-i18n@8 - : `${aliasName}/dist/${installedPkg}.runtime.esm-bundler.js` + : `${aliasName}/dist/${installedPkg}${runtimeOnly ? '.runtime' : ''}.${ + !ssr ? 'esm-bundler.js' /* '.mjs' */ : 'node.mjs' + }` + } const esm = isBoolean(options.esm) ? options.esm : true debug('esm', esm) @@ -175,12 +184,15 @@ export const unplugin = createUnplugin((options = {}, meta) => { meta.framework ) - if (command === 'build' && runtimeOnly) { + if (command === 'build') { debug(`vue-i18n alias name: ${vueI18nAliasName}`) if (isArray(config.resolve!.alias)) { config.resolve!.alias.push({ find: vueI18nAliasName, - replacement: getVueI18nAliasPath(vueI18nAliasName) + replacement: getVueI18nAliasPath(vueI18nAliasName, { + ssr: ssrBuild, + runtimeOnly + }) }) if (installedVueI18nBridge) { config.resolve!.alias.push({ @@ -191,7 +203,10 @@ export const unplugin = createUnplugin((options = {}, meta) => { } else if (isObject(config.resolve!.alias)) { // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(config.resolve!.alias as any)[vueI18nAliasName] = - getVueI18nAliasPath(vueI18nAliasName) + getVueI18nAliasPath(vueI18nAliasName, { + ssr: ssrBuild, + runtimeOnly + }) if (installedVueI18nBridge) { // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(config.resolve!.alias as any)['vue-i18n-bridge'] = @@ -200,7 +215,11 @@ export const unplugin = createUnplugin((options = {}, meta) => { } debug( `set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath( - vueI18nAliasName + vueI18nAliasName, + { + ssr: ssrBuild, + runtimeOnly + } )}` ) if (installedVueI18nBridge) { @@ -392,19 +411,23 @@ export const unplugin = createUnplugin((options = {}, meta) => { meta.framework ) - if (isProduction && runtimeOnly) { + if (isProduction) { // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(compiler.options.resolve!.alias as any)[vueI18nAliasName] = - getVueI18nAliasPath(vueI18nAliasName) + getVueI18nAliasPath(vueI18nAliasName, { + ssr: ssrBuild, + runtimeOnly + }) if (installedVueI18nBridge) { // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(compiler.options.resolve!.alias as any)['vue-i18n-bridge'] = getVueI18nBridgeAliasPath() } debug( - `set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath( - vueI18nAliasName - )}` + `set ${vueI18nAliasName}: ${getVueI18nAliasPath(vueI18nAliasName, { + ssr: ssrBuild, + runtimeOnly + })}` ) if (installedVueI18nBridge) { debug( diff --git a/packages/unplugin-vue-i18n/src/types.ts b/packages/unplugin-vue-i18n/src/types.ts index 9cfb200..2bf72bd 100644 --- a/packages/unplugin-vue-i18n/src/types.ts +++ b/packages/unplugin-vue-i18n/src/types.ts @@ -7,6 +7,7 @@ export interface PluginOptions { dropMessageCompiler?: boolean runtimeOnly?: boolean compositionOnly?: boolean + ssr?: boolean fullInstall?: boolean esm?: boolean forceStringify?: boolean