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

feat(unplugin-vue-i18n): support module option #394

Merged
merged 1 commit into from
Sep 22, 2024
Merged
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
28 changes: 21 additions & 7 deletions packages/unplugin-vue-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,6 @@ This plugin will automatically select and bundle Vue I18n build according to the

About details, See the [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#improve-performance-and-reduce-bundle-size-with-runtime-build-only)

### For `petite-vue-i18n`

This plugin will automatically select and bundle `petite-vue-i18n` build according to the following vite behavior:

- vite dev: `petite-vue-i18n.esm-bundler.js`
- vite build: `petite-vue-i18n.runtime.esm-bundler.js`


## 🔧 Options

Expand Down Expand Up @@ -302,6 +295,27 @@ This plugin will automatically select and bundle `petite-vue-i18n` build accordi
> [!WARNING]
If you use the `js` and `ts` resources formats, set the paths, so your application code is not targeted. We recommend that resources be isolated from the application code.

### `module`

- **Type:** `string`
- **Default:** `'vue-i18n'`

> [!NOTE]
This options is supported from v5.1.0, and works with vue-i18n v10 and later.

Bundle target vue-i18n module. You can specify either `‘vue-i18n’` or `‘petite-vue-i18n’`.

The default is `'vue-i18n'`, and the following installed in node_modules will be bundled.

- development: `vue-i18n.esm-bundler.js`
- production: `vue-i18n.runtime.esm-bundler.js`

In the case of `‘petite-vue-i18n’`, the following installed in node_modules will be bundled.

- development: `petite-vue-i18n.esm-bundler.js`
- production: `petite-vue-i18n.runtime.esm-bundler.js`

If you are using petite-vue-i18n, you will need to set this value.

### `strictMessage`

Expand Down
1 change: 0 additions & 1 deletion packages/unplugin-vue-i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"fast-glob": "^3.2.12",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"mlly": "^1.7.1",
"pathe": "^1.0.0",
"picocolors": "^1.0.0",
"source-map-js": "^1.0.2",
Expand Down
13 changes: 6 additions & 7 deletions packages/unplugin-vue-i18n/src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { isString, isBoolean, isArray } from '@intlify/shared'

import type { PluginOptions } from '../types'
import type { TranslationDirectiveResolveIndetifier } from '../vue'
import type { InstalledPackageInfo } from '../utils'

export function resolveOptions(
options: PluginOptions,
installedPkgInfo: InstalledPackageInfo
) {
export function resolveOptions(options: PluginOptions) {
const moduleType = (options.module || 'vue-i18n') as string

// normalize for `options.onlyLocales`
let onlyLocales: string[] = []
if (options.onlyLocales) {
Expand Down Expand Up @@ -43,14 +41,14 @@ export function resolveOptions(
const dropMessageCompiler = !!options.dropMessageCompiler

// prettier-ignore
const compositionOnly = installedPkgInfo.pkg === 'vue-i18n'
const compositionOnly = moduleType === 'vue-i18n'
? isBoolean(options.compositionOnly)
? options.compositionOnly
: true
: true

// prettier-ignore
const fullInstall = installedPkgInfo.pkg === 'vue-i18n'
const fullInstall = moduleType === 'vue-i18n'
? isBoolean(options.fullInstall)
? options.fullInstall
: true
Expand Down Expand Up @@ -85,6 +83,7 @@ export function resolveOptions(
return {
include,
exclude,
module: moduleType,
onlyLocales,
forceStringify,
defaultSFCLang,
Expand Down
9 changes: 4 additions & 5 deletions packages/unplugin-vue-i18n/src/core/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import type {
TransformResult
} from 'unplugin'
import type { ResolvedOptions } from '../core/options'
import type { InstalledPackageInfo } from '../utils'
import type { VueQuery } from '../vue'
import type { PluginOptions } from '../types'

Expand All @@ -50,6 +49,7 @@ export function resourcePlugin(
onlyLocales,
include,
exclude,
module,
forceStringify,
defaultSFCLang,
globalSFCScope,
Expand All @@ -63,18 +63,17 @@ export function resourcePlugin(
escapeHtml,
transformI18nBlock
}: ResolvedOptions,
meta: UnpluginContextMeta,
installedPkgInfo: InstalledPackageInfo
meta: UnpluginContextMeta
): UnpluginOptions {
const filter = createFilter(include, exclude)
const getVueI18nAliasPath = ({ ssr = false, runtimeOnly = false }) => {
return `${installedPkgInfo.alias}/dist/${installedPkgInfo.pkg}${runtimeOnly ? '.runtime' : ''}.${
return `${module}/dist/${module}${runtimeOnly ? '.runtime' : ''}.${
!ssr ? 'esm-bundler.js' /* '.mjs' */ : 'node.mjs'
}`
}
let isProduction = false
let sourceMap = false
const vueI18nAliasName = installedPkgInfo.alias
const vueI18nAliasName = module
debug(`vue-i18n alias name: ${vueI18nAliasName}`)

let vuePlugin: RollupPlugin | null = null
Expand Down
7 changes: 3 additions & 4 deletions packages/unplugin-vue-i18n/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { createUnplugin } from 'unplugin'
import createDebug from 'debug'
import { raiseError, checkInstallPackage, resolveNamespace } from './utils'
import { raiseError, resolveNamespace } from './utils'
import { resolveOptions, resourcePlugin, directivePlugin } from './core'

import type { UnpluginFactory, UnpluginInstance } from 'unplugin'
import type { PluginOptions } from './types'

const debug = createDebug(resolveNamespace('root'))
const installedPkgInfo = checkInstallPackage(debug)

export * from './types'

Expand All @@ -22,10 +21,10 @@ export const unpluginFactory: UnpluginFactory<PluginOptions | undefined> = (
}

debug('plugin options (resolving):', options)
const resolvedOptions = resolveOptions(options, installedPkgInfo)
const resolvedOptions = resolveOptions(options)
debug('plugin options (resolved):', resolvedOptions)

const plugins = [resourcePlugin(resolvedOptions, meta, installedPkgInfo)]
const plugins = [resourcePlugin(resolvedOptions, meta)]
if (resolvedOptions.optimizeTranslationDirective) {
if (meta.framework === 'webpack') {
raiseError(
Expand Down
3 changes: 3 additions & 0 deletions packages/unplugin-vue-i18n/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export type SFCLangFormat = 'json' | 'json5' | 'yml' | 'yaml'
export type VueI18nModule = 'vue-i18n' | 'petite-vue-i18n'

export interface PluginOptions {
include?: string | string[]
onlyLocales?: string | string[]
allowDynamic?: boolean
module?: VueI18nModule
dropMessageCompiler?: boolean
runtimeOnly?: boolean
compositionOnly?: boolean
Expand Down
2 changes: 0 additions & 2 deletions packages/unplugin-vue-i18n/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from './log'
export * from './plugin'
export * from './resolver'
export * from './pkg'
139 changes: 0 additions & 139 deletions packages/unplugin-vue-i18n/src/utils/pkg.ts

This file was deleted.

60 changes: 0 additions & 60 deletions packages/unplugin-vue-i18n/src/utils/resolver.ts

This file was deleted.

Loading