Skip to content

Commit

Permalink
feat: ssr option (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Sep 29, 2023
1 parent ec71c82 commit fb7a16c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
10 changes: 10 additions & 0 deletions packages/unplugin-vue-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
47 changes: 35 additions & 12 deletions packages/unplugin-vue-i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export const unplugin = createUnplugin<PluginOptions>((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`)
Expand All @@ -131,10 +134,16 @@ export const unplugin = createUnplugin<PluginOptions>((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)
Expand Down Expand Up @@ -175,12 +184,15 @@ export const unplugin = createUnplugin<PluginOptions>((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({
Expand All @@ -191,7 +203,10 @@ export const unplugin = createUnplugin<PluginOptions>((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'] =
Expand All @@ -200,7 +215,11 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
}
debug(
`set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath(
vueI18nAliasName
vueI18nAliasName,
{
ssr: ssrBuild,
runtimeOnly
}
)}`
)
if (installedVueI18nBridge) {
Expand Down Expand Up @@ -392,19 +411,23 @@ export const unplugin = createUnplugin<PluginOptions>((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(
Expand Down
1 change: 1 addition & 0 deletions packages/unplugin-vue-i18n/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PluginOptions {
dropMessageCompiler?: boolean
runtimeOnly?: boolean
compositionOnly?: boolean
ssr?: boolean
fullInstall?: boolean
esm?: boolean
forceStringify?: boolean
Expand Down

0 comments on commit fb7a16c

Please sign in to comment.