@intlify/unplugin-vue-i18n 11.0.7 → 11.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,6 +17,7 @@ unplugin for Vue I18n
17
17
  - Locale of i18n resource definition
18
18
  - Locale of i18n resource definition for global scope
19
19
  - i18n resource formatting
20
+ - Locale message tree-shaking (remove unused message keys)
20
21
 
21
22
  ## 💿 Installation
22
23
 
@@ -249,6 +250,52 @@ If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplu
249
250
  }
250
251
  ```
251
252
 
253
+ ### Locale message tree-shaking
254
+
255
+ unplugin-vue-i18n can statically analyze your application code to detect which locale message keys are used via `t()`, `$t()`, `v-t` directive, etc., and remove unused keys from the production bundle.
256
+
257
+ This is useful for large applications with many locale message keys where only a subset is actually used.
258
+
259
+ ```ts
260
+ // vite.config.ts
261
+ import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
262
+ import path from 'path'
263
+
264
+ export default defineConfig({
265
+ plugins: [
266
+ VueI18nPlugin({
267
+ include: [path.resolve(__dirname, './src/locales/**')],
268
+ treeShaking: true
269
+ })
270
+ ]
271
+ })
272
+ ```
273
+
274
+ With advanced options:
275
+
276
+ ```ts
277
+ VueI18nPlugin({
278
+ include: [path.resolve(__dirname, './src/locales/**')],
279
+ treeShaking: {
280
+ // Keys to always keep (glob patterns)
281
+ safelist: ['errors.*', 'validation.**'],
282
+ // Strategy when dynamic keys like t(variable) are detected
283
+ // 'keep-all' (default): disable tree-shaking, keep all keys
284
+ // 'ignore': continue tree-shaking despite dynamic keys
285
+ dynamicKeyStrategy: 'keep-all'
286
+ }
287
+ })
288
+ ```
289
+
290
+ > [!NOTE]
291
+ > Tree-shaking only runs in production builds. In development mode, all keys are preserved.
292
+
293
+ > [!NOTE]
294
+ > Tree-shaking currently supports JSON and YAML locale files for global scope. Component-scoped `<i18n>` blocks (without `global` attribute) and JS/TS locale files are not tree-shaken.
295
+
296
+ > [!WARNING]
297
+ > If your application uses dynamic keys (e.g., `t(variable)`, ``t(`prefix.${suffix}`)``), tree-shaking may remove keys that are only referenced dynamically. By default (`dynamicKeyStrategy: 'keep-all'`), tree-shaking is automatically disabled when dynamic key usage is detected. If you set `dynamicKeyStrategy: 'ignore'`, make sure to add dynamically accessed keys to `safelist`.
298
+
252
299
  ## 📦 Automatic bundling
253
300
 
254
301
  ### For Vue I18n
@@ -607,6 +654,50 @@ If do you will use this option, you need to enable `jitCompilation` option.
607
654
 
608
655
  By using it you can exclude from the bundle those localizations that are not specified in this option.
609
656
 
657
+ ### `treeShaking`
658
+
659
+ - **Type:** `boolean | TreeShakingOptions`
660
+ - **Default:** `false`
661
+
662
+ Enable locale message tree-shaking to remove unused message keys from the production bundle. The plugin statically analyzes your source files to detect which keys are used via `t()`, `$t()`, `tc()`, `te()`, `d()`, `n()`, `v-t` directive, etc., and removes keys that are not referenced.
663
+
664
+ When set to `true`, default options are used. You can also pass an object for fine-grained control:
665
+
666
+ ```ts
667
+ interface TreeShakingOptions {
668
+ /**
669
+ * Message key patterns to always keep (never tree-shake).
670
+ * Supports glob-like patterns.
671
+ * `*` matches a single level, `**` matches multiple levels.
672
+ * @example ['errors.*', 'validation.**']
673
+ */
674
+ safelist?: string[]
675
+ /**
676
+ * Strategy when dynamic key usage (e.g., t(variable)) is detected.
677
+ * - 'keep-all': Disable tree-shaking and keep all keys (safe default).
678
+ * - 'ignore': Continue tree-shaking despite dynamic keys (use with safelist).
679
+ * @default 'keep-all'
680
+ */
681
+ dynamicKeyStrategy?: 'keep-all' | 'ignore'
682
+ /**
683
+ * Glob patterns for source files to scan for used message keys.
684
+ * @default ['${projectRoot}/src/**\/*.{vue,ts,js,tsx,jsx}']
685
+ */
686
+ scanPatterns?: string[]
687
+ }
688
+ ```
689
+
690
+ **Supported scope:**
691
+ - Global locale files (JSON/YAML matched by `include` option)
692
+ - SFC `<i18n global>` custom blocks
693
+
694
+ **Not supported (keys are always preserved):**
695
+ - Component-scoped `<i18n>` custom blocks (without `global` attribute)
696
+ - JS/TS locale files
697
+
698
+ > [!NOTE]
699
+ > Tree-shaking only runs in production builds.
700
+
610
701
  ### `useVueI18nImportName` (Experimental)
611
702
 
612
703
  - **Type:** `boolean`