@intlify/unplugin-vue-i18n 5.0.0-beta.3 → 5.0.0-beta.4

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
@@ -583,6 +583,53 @@ If do you will use this option, you need to enable `jitCompilation` option.
583
583
  > [!WARNING]
584
584
  About for manually signature, see the details [vue-i18n-extensions API docs](https://github.com/intlify/vue-i18n-extensions/blob/next/docs/%40intlify/vue-i18n-extensions-api.md#translationsignatures) and [usecase from vue-i18n-extensions PR](https://github.com/intlify/vue-i18n-extensions/pull/217/files#diff-3fb9543f91e011d4b0dc9beff44082fe1a99c9eab70c1afab23c3c34352b7c38R121-R200)
585
585
 
586
+ ### `transformI18nBlock`
587
+
588
+ - **Type**: `function`
589
+ - **Default:** `undefined`
590
+
591
+ This hook allows a user to modify the `<i18n>` block before the plugin generates the translations. The hook is passed the source of the `<ii8n>` block as a `string` after the SFC is read from disk.
592
+
593
+ **Plugin**
594
+ ```javascript
595
+ function transformI18nBlock(source) {
596
+ // transformation logic
597
+ }
598
+
599
+ // Plugin
600
+ vueI18n({
601
+ transformI18nBlock
602
+ })
603
+ ```
604
+
605
+ **Before**
606
+ ```html
607
+ <i18n>
608
+ [
609
+ 'slug-one',
610
+ 'slug-two'
611
+ ]
612
+ <i18n>
613
+ ```
614
+
615
+ **After**
616
+ ```html
617
+ <i18n>
618
+ {
619
+ 'en': {
620
+ 'slug-one': 'foo',
621
+ 'slug-two': 'bar'
622
+ },
623
+ ja: {
624
+ 'slug-one': 'foo',
625
+ 'slug-two': 'bar'
626
+ }
627
+ }
628
+ </i18n>
629
+ ```
630
+ > [!IMPORTANT]
631
+ The function **must** return a string or the build will fail.
632
+
586
633
  ## 📜 Changelog
587
634
 
588
635
  Details changes for each release are documented in the [CHANGELOG.md](https://github.com/intlify/bundle-tools/blob/main/packages/unplugin-vue-i18n/CHANGELOG.md)
package/lib/index.cjs CHANGED
@@ -182,6 +182,7 @@ function resolveOptions(options, installedPkgInfo) {
182
182
  const escapeHtml = !!options.escapeHtml;
183
183
  const optimizeTranslationDirective = shared.isString(options.optimizeTranslationDirective) || shared.isArray(options.optimizeTranslationDirective) ? options.optimizeTranslationDirective : !!options.optimizeTranslationDirective;
184
184
  const translationIdentifiers = /* @__PURE__ */ new Map();
185
+ const transformI18nBlock = typeof options.transformI18nBlock === "function" ? options.transformI18nBlock : null;
185
186
  return {
186
187
  include,
187
188
  exclude,
@@ -198,7 +199,8 @@ function resolveOptions(options, installedPkgInfo) {
198
199
  strictMessage,
199
200
  escapeHtml,
200
201
  optimizeTranslationDirective,
201
- translationIdentifiers
202
+ translationIdentifiers,
203
+ transformI18nBlock
202
204
  };
203
205
  }
204
206
 
@@ -284,7 +286,8 @@ function resourcePlugin({
284
286
  ssrBuild,
285
287
  strictMessage,
286
288
  allowDynamic,
287
- escapeHtml
289
+ escapeHtml,
290
+ transformI18nBlock
288
291
  }, meta, installedPkgInfo) {
289
292
  const filter = pluginutils.createFilter(include, exclude);
290
293
  const getVueI18nAliasPath = ({ ssr = false, runtimeOnly: runtimeOnly2 = false }) => {
@@ -566,7 +569,7 @@ function resourcePlugin({
566
569
  }
567
570
  );
568
571
  debug$2("parseOptions", parseOptions);
569
- const source = await getCode(
572
+ let source = await getCode(
570
573
  code,
571
574
  filename,
572
575
  sourceMap,
@@ -574,6 +577,14 @@ function resourcePlugin({
574
577
  getSfcParser(),
575
578
  meta.framework
576
579
  );
580
+ if (typeof transformI18nBlock === "function") {
581
+ const modifiedSource = transformI18nBlock(source);
582
+ if (modifiedSource && typeof modifiedSource === "string") {
583
+ source = modifiedSource;
584
+ } else {
585
+ warn("transformI18nBlock should return a string");
586
+ }
587
+ }
577
588
  const { code: generatedCode, map } = generate(source, parseOptions);
578
589
  debug$2("generated code", generatedCode);
579
590
  debug$2("sourcemap", map, sourceMap);
@@ -627,7 +638,8 @@ async function generateBundleResources(resources, isProduction, {
627
638
  onlyLocales = [],
628
639
  strictMessage = true,
629
640
  escapeHtml = false,
630
- jit = true
641
+ jit = true,
642
+ transformI18nBlock = void 0
631
643
  }) {
632
644
  const codes = [];
633
645
  for (const res of resources) {
@@ -642,7 +654,8 @@ async function generateBundleResources(resources, isProduction, {
642
654
  onlyLocales,
643
655
  strictMessage,
644
656
  escapeHtml,
645
- forceStringify
657
+ forceStringify,
658
+ transformI18nBlock
646
659
  });
647
660
  parseOptions.type = "bare";
648
661
  const { code } = generate(source, parseOptions);
@@ -714,7 +727,8 @@ function getOptions(filename, isProduction, query, sourceMap, {
714
727
  allowDynamic = false,
715
728
  strictMessage = true,
716
729
  escapeHtml = false,
717
- jit = true
730
+ jit = true,
731
+ transformI18nBlock = null
718
732
  }) {
719
733
  const mode = isProduction ? "production" : "development";
720
734
  const baseOptions = {
@@ -728,6 +742,7 @@ function getOptions(filename, isProduction, query, sourceMap, {
728
742
  jit,
729
743
  onlyLocales,
730
744
  env: mode,
745
+ transformI18nBlock,
731
746
  onWarn: (msg) => {
732
747
  warn(`${filename} ${msg}`);
733
748
  },
package/lib/index.mjs CHANGED
@@ -167,6 +167,7 @@ function resolveOptions(options, installedPkgInfo) {
167
167
  const escapeHtml = !!options.escapeHtml;
168
168
  const optimizeTranslationDirective = isString(options.optimizeTranslationDirective) || isArray(options.optimizeTranslationDirective) ? options.optimizeTranslationDirective : !!options.optimizeTranslationDirective;
169
169
  const translationIdentifiers = /* @__PURE__ */ new Map();
170
+ const transformI18nBlock = typeof options.transformI18nBlock === "function" ? options.transformI18nBlock : null;
170
171
  return {
171
172
  include,
172
173
  exclude,
@@ -183,7 +184,8 @@ function resolveOptions(options, installedPkgInfo) {
183
184
  strictMessage,
184
185
  escapeHtml,
185
186
  optimizeTranslationDirective,
186
- translationIdentifiers
187
+ translationIdentifiers,
188
+ transformI18nBlock
187
189
  };
188
190
  }
189
191
 
@@ -269,7 +271,8 @@ function resourcePlugin({
269
271
  ssrBuild,
270
272
  strictMessage,
271
273
  allowDynamic,
272
- escapeHtml
274
+ escapeHtml,
275
+ transformI18nBlock
273
276
  }, meta, installedPkgInfo) {
274
277
  const filter = createFilter(include, exclude);
275
278
  const getVueI18nAliasPath = ({ ssr = false, runtimeOnly: runtimeOnly2 = false }) => {
@@ -551,7 +554,7 @@ function resourcePlugin({
551
554
  }
552
555
  );
553
556
  debug$2("parseOptions", parseOptions);
554
- const source = await getCode(
557
+ let source = await getCode(
555
558
  code,
556
559
  filename,
557
560
  sourceMap,
@@ -559,6 +562,14 @@ function resourcePlugin({
559
562
  getSfcParser(),
560
563
  meta.framework
561
564
  );
565
+ if (typeof transformI18nBlock === "function") {
566
+ const modifiedSource = transformI18nBlock(source);
567
+ if (modifiedSource && typeof modifiedSource === "string") {
568
+ source = modifiedSource;
569
+ } else {
570
+ warn("transformI18nBlock should return a string");
571
+ }
572
+ }
562
573
  const { code: generatedCode, map } = generate(source, parseOptions);
563
574
  debug$2("generated code", generatedCode);
564
575
  debug$2("sourcemap", map, sourceMap);
@@ -612,7 +623,8 @@ async function generateBundleResources(resources, isProduction, {
612
623
  onlyLocales = [],
613
624
  strictMessage = true,
614
625
  escapeHtml = false,
615
- jit = true
626
+ jit = true,
627
+ transformI18nBlock = void 0
616
628
  }) {
617
629
  const codes = [];
618
630
  for (const res of resources) {
@@ -627,7 +639,8 @@ async function generateBundleResources(resources, isProduction, {
627
639
  onlyLocales,
628
640
  strictMessage,
629
641
  escapeHtml,
630
- forceStringify
642
+ forceStringify,
643
+ transformI18nBlock
631
644
  });
632
645
  parseOptions.type = "bare";
633
646
  const { code } = generate(source, parseOptions);
@@ -699,7 +712,8 @@ function getOptions(filename, isProduction, query, sourceMap, {
699
712
  allowDynamic = false,
700
713
  strictMessage = true,
701
714
  escapeHtml = false,
702
- jit = true
715
+ jit = true,
716
+ transformI18nBlock = null
703
717
  }) {
704
718
  const mode = isProduction ? "production" : "development";
705
719
  const baseOptions = {
@@ -713,6 +727,7 @@ function getOptions(filename, isProduction, query, sourceMap, {
713
727
  jit,
714
728
  onlyLocales,
715
729
  env: mode,
730
+ transformI18nBlock,
716
731
  onWarn: (msg) => {
717
732
  warn(`${filename} ${msg}`);
718
733
  },
package/lib/types.d.cts CHANGED
@@ -14,6 +14,7 @@ interface PluginOptions {
14
14
  strictMessage?: boolean;
15
15
  escapeHtml?: boolean;
16
16
  optimizeTranslationDirective?: boolean | string | string[];
17
+ transformI18nBlock?: (src: string | Buffer) => string;
17
18
  }
18
19
 
19
20
  export type { PluginOptions, SFCLangFormat };
package/lib/types.d.mts CHANGED
@@ -14,6 +14,7 @@ interface PluginOptions {
14
14
  strictMessage?: boolean;
15
15
  escapeHtml?: boolean;
16
16
  optimizeTranslationDirective?: boolean | string | string[];
17
+ transformI18nBlock?: (src: string | Buffer) => string;
17
18
  }
18
19
 
19
20
  export type { PluginOptions, SFCLangFormat };
package/lib/types.d.ts CHANGED
@@ -14,6 +14,7 @@ interface PluginOptions {
14
14
  strictMessage?: boolean;
15
15
  escapeHtml?: boolean;
16
16
  optimizeTranslationDirective?: boolean | string | string[];
17
+ transformI18nBlock?: (src: string | Buffer) => string;
17
18
  }
18
19
 
19
20
  export type { PluginOptions, SFCLangFormat };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/unplugin-vue-i18n",
3
- "version": "5.0.0-beta.3",
3
+ "version": "5.0.0-beta.4",
4
4
  "description": "unplugin for Vue I18n",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",