@intlify/unplugin-vue-i18n 11.2.6 → 11.2.7

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
@@ -238,6 +238,18 @@ export default defineConfig({
238
238
 
239
239
  unplugin-vue-i18n will automatically merge locale files into `@intlify/unplugin-vue-i18n/messages`. This allows locales to be split across multiple files, for example `src/locales/fruits/en.json` and `src/locales/vegetables/en.json`.
240
240
 
241
+ > [!NOTE]
242
+ > On v11, `@intlify/unplugin-vue-i18n/messages` bundles JSON and YAML resources only. JS / TS resources under `include` are left out of it, and the plugin warns about them. v12 bundles JS / TS resources too.
243
+
244
+ To load JS / TS resources at once on v11, import them directly, or collect them with `import.meta.glob`:
245
+
246
+ ```ts
247
+ const modules = import.meta.glob('./src/locales/*.{ts,js}', { eager: true, import: 'default' })
248
+ const messages = Object.fromEntries(
249
+ Object.entries(modules).map(([path, m]) => [path.replace(/^.*\/|\.[jt]s$/g, ''), m])
250
+ )
251
+ ```
252
+
241
253
  ### Types
242
254
 
243
255
  If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplugin-vue-i18n/messages` to `compilerOptions.types` of your tsconfig:
package/lib/index.cjs CHANGED
@@ -178,7 +178,6 @@ const VIRTUAL_PREFIX = "virtual:";
178
178
  const RE_RESOURCE_FORMAT = /\.(json5?|ya?ml|[c|m]?[j|t]s)$/;
179
179
  const RE_SFC_I18N_CUSTOM_BLOCK = /\?vue&type=i18n/;
180
180
  const RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK = /blockType=i18n/;
181
- const VITE_VIRTUAL_PREFIX = "virtual:intlify-i18n-";
182
181
  const debug$3 = createDebug__default(resolveNamespace("resource"));
183
182
  function resourcePlugin({
184
183
  onlyLocales,
@@ -204,7 +203,7 @@ function resourcePlugin({
204
203
  }
205
204
  function resolveIncludeExcludeForLegacy() {
206
205
  const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
207
- return include ? [[...include, customBlockInclude], void 0] : [void 0, ["**/**"]];
206
+ return include ? [[...include, customBlockInclude], exclude] : [void 0, ["**/**"]];
208
207
  }
209
208
  let _filter = null;
210
209
  async function getFilter() {
@@ -229,28 +228,13 @@ function resourcePlugin({
229
228
  let isProduction = false;
230
229
  let sourceMap = false;
231
230
  let hasViteJsonPlugin = false;
231
+ let warnedScriptResources = false;
232
232
  const vueI18nAliasName = module;
233
233
  debug$3(`vue-i18n alias name: ${vueI18nAliasName}`);
234
234
  let vuePlugin = null;
235
235
  const getSfcParser = () => {
236
236
  return vuePlugin ? getVueCompiler(vuePlugin).parse : compilerSfc.parse;
237
237
  };
238
- const virtualIdToRealPath = /* @__PURE__ */ new Map();
239
- const realPathToVirtualId = /* @__PURE__ */ new Map();
240
- let virtualCounter = 0;
241
- function intlifyVirtualize(realPath) {
242
- const normalized = pathe.normalize(realPath);
243
- let virtualId = realPathToVirtualId.get(normalized);
244
- if (!virtualId) {
245
- virtualId = `${VITE_VIRTUAL_PREFIX}${virtualCounter++}`;
246
- virtualIdToRealPath.set(virtualId, normalized);
247
- realPathToVirtualId.set(normalized, virtualId);
248
- }
249
- return virtualId;
250
- }
251
- function isIntlifyVirtualId(id) {
252
- return virtualIdToRealPath.has(id);
253
- }
254
238
  return {
255
239
  name: resolveNamespace("resource"),
256
240
  /**
@@ -321,18 +305,12 @@ function resourcePlugin({
321
305
  },
322
306
  handleHotUpdate({ file, modules, server }) {
323
307
  const key = pathe.normalize(file);
324
- const virtualId = realPathToVirtualId.get(key);
325
- const inBundle = !!(include && pluginutils.createFilter(include, exclude)(key));
326
- if (!virtualId && !inBundle) return;
308
+ if (!(include && pluginutils.createFilter(include, exclude)(key))) return;
327
309
  const affected = new Set(modules);
328
- const addById = (id) => {
329
- const mod = id && server.moduleGraph.getModuleById(id);
330
- if (mod) affected.add(mod);
331
- };
332
- addById(virtualId);
333
- if (inBundle) {
334
- addById(asVirtualId(INTLIFY_BUNDLE_IMPORT_ID, meta.framework));
335
- }
310
+ const mod = server.moduleGraph.getModuleById(
311
+ asVirtualId(INTLIFY_BUNDLE_IMPORT_ID, meta.framework)
312
+ );
313
+ if (mod) affected.add(mod);
336
314
  if (affected.size === modules.length) return;
337
315
  return [...affected];
338
316
  }
@@ -377,43 +355,15 @@ function resourcePlugin({
377
355
  }
378
356
  },
379
357
  resolveId: {
380
- async handler(id, importer) {
358
+ async handler(id) {
381
359
  if (id === INTLIFY_BUNDLE_IMPORT_ID) {
382
360
  return asVirtualId(id, meta.framework);
383
361
  }
384
- if (meta.framework === "vite" && !hasViteJsonPlugin) {
385
- if (id.includes("?vue&type=i18n") && /[?&]lang\.(?:json|json5)(?:$|&)/.test(id)) {
386
- const { filename, query } = parseVueRequest(id);
387
- let virtualKey = id;
388
- if (query.src && importer) {
389
- const importerPath = (isIntlifyVirtualId(importer) ? virtualIdToRealPath.get(importer) : importer).split("?")[0];
390
- if (importerPath.endsWith(".vue")) {
391
- const resolvedSidecar = pathe.resolve(pathe.dirname(importerPath), filename);
392
- virtualKey = id.replace(filename, resolvedSidecar);
393
- }
394
- }
395
- return intlifyVirtualize(virtualKey);
396
- }
397
- const idPath = id.split("?")[0];
398
- if (!/\.json5?$/.test(idPath)) return;
399
- let resolvedPath;
400
- if (idPath.startsWith(".")) {
401
- const realImporter = importer && isIntlifyVirtualId(importer) ? virtualIdToRealPath.get(importer) : importer;
402
- if (!realImporter) return;
403
- resolvedPath = pathe.resolve(pathe.dirname(realImporter), idPath);
404
- } else if (idPath.startsWith("/") || /^[a-z]:[/\\]/i.test(idPath)) {
405
- resolvedPath = idPath;
406
- } else {
407
- return;
408
- }
409
- const filter = await getFilter();
410
- if (!filter(resolvedPath)) return;
411
- const stat = await node_fs.promises.stat(resolvedPath).catch(() => null);
412
- if (!stat?.isFile()) return;
413
- return intlifyVirtualize(resolvedPath);
414
- }
415
362
  }
416
363
  },
364
+ loadInclude(id) {
365
+ return INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework);
366
+ },
417
367
  load: {
418
368
  async handler(id) {
419
369
  debug$3("load", id);
@@ -422,113 +372,60 @@ function resourcePlugin({
422
372
  for (const inc of include) {
423
373
  resourcePaths = [
424
374
  ...resourcePaths,
425
- ...await tinyglobby.glob(inc, { cwd: process.cwd(), expandDirectories: false, absolute: true })
375
+ ...await tinyglobby.glob(inc, {
376
+ cwd: process.cwd(),
377
+ expandDirectories: false,
378
+ absolute: true,
379
+ ignore: exclude
380
+ })
426
381
  ];
427
382
  }
428
383
  resourcePaths = resourcePaths.filter((el, pos) => resourcePaths.indexOf(el) === pos);
384
+ if (!warnedScriptResources) {
385
+ const scripts = resourcePaths.filter((path) => /\.[cm]?[jt]s$/.test(path)).sort();
386
+ if (scripts.length > 0) {
387
+ warnedScriptResources = true;
388
+ const listed = scripts.slice(0, 10).map((path) => pathe.relative(process.cwd(), path));
389
+ const more = scripts.length > listed.length ? `, and ${scripts.length - listed.length} more` : "";
390
+ warn(
391
+ `JS / TS files under \`include\` are not bundled into \`${INTLIFY_BUNDLE_IMPORT_ID}\` (v11 bundles JSON / YAML only): ${listed.join(", ")}${more}. Import them directly, or collect them with \`import.meta.glob\`.`
392
+ );
393
+ }
394
+ }
395
+ for (const path of resourcePaths) {
396
+ if (/\.(?:json5?|ya?ml)$/.test(path)) {
397
+ this.addWatchFile(path);
398
+ }
399
+ }
429
400
  const code = await generateBundleResources(resourcePaths, isProduction, {
430
401
  forceStringify,
431
402
  strictMessage,
432
403
  escapeHtml,
433
404
  usedKeyFilter: collector ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
434
405
  });
435
- return {
436
- moduleType: "js",
437
- code,
438
- map: { mappings: "" }
439
- };
440
- }
441
- if (isIntlifyVirtualId(id)) {
442
- const realId = virtualIdToRealPath.get(id);
443
- if (realId.includes("?vue&type=i18n")) {
444
- const { filename, query } = parseVueRequest(realId);
445
- let source;
446
- if (query.src) {
447
- this.addWatchFile(filename);
448
- source = await node_fs.promises.readFile(filename, "utf-8");
449
- } else {
450
- this.addWatchFile(filename);
451
- const sfcSource = await node_fs.promises.readFile(filename, "utf-8");
452
- const { descriptor } = getSfcParser()(sfcSource, {
453
- sourceMap,
454
- filename
455
- });
456
- const block = descriptor.customBlocks[query.index];
457
- if (!block) return;
458
- source = block.content;
459
- }
460
- if (typeof transformI18nBlock === "function") {
461
- const modifiedSource = transformI18nBlock(source);
462
- if (modifiedSource && typeof modifiedSource === "string") {
463
- source = modifiedSource;
464
- } else {
465
- warn("transformI18nBlock should return a string");
466
- }
467
- }
468
- let langInfo2 = defaultSFCLang;
469
- if (shared.isString(query.lang)) {
470
- langInfo2 = query.src ? query.lang === "i18n" ? defaultSFCLang : query.lang : query.lang;
471
- }
472
- if (/\.?[cm]?[jt]s$/.test(langInfo2)) {
473
- source = `export default ${source.replace(/^[\s;]+/, "")}`;
474
- }
475
- const generate2 = getGenerator(langInfo2, bundleUtils.generateYAML);
476
- const isGlobalBlock = globalSFCScope || !!query.global;
477
- const parseOptions2 = getOptions(
478
- filename,
479
- isProduction,
480
- query,
481
- sourceMap,
482
- {
483
- isGlobal: globalSFCScope,
484
- allowDynamic,
485
- jit: true,
486
- strictMessage,
487
- escapeHtml,
488
- onlyLocales,
489
- forceStringify,
490
- usedKeyFilter: collector && isGlobalBlock ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
491
- }
492
- );
493
- const { code: generatedCode2 } = await generate2(source, parseOptions2);
494
- return {
495
- moduleType: "js",
496
- code: generatedCode2,
497
- map: { mappings: "" }
498
- };
406
+ if (meta.framework !== "vite") {
407
+ return { code };
499
408
  }
500
- this.addWatchFile(realId);
501
- const code = await node_fs.promises.readFile(realId, "utf-8");
502
- const langInfo = pathe.parse(realId).ext;
503
- const generate = getGenerator(langInfo);
504
- const parseOptions = getOptions(
505
- realId,
506
- isProduction,
507
- {},
508
- sourceMap,
509
- {
510
- isGlobal: globalSFCScope,
511
- allowDynamic,
512
- strictMessage,
513
- escapeHtml,
514
- jit: true,
515
- onlyLocales,
516
- forceStringify,
517
- usedKeyFilter: collector ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
518
- }
519
- );
520
- const { code: generatedCode } = await generate(code, parseOptions);
521
409
  return {
522
410
  moduleType: "js",
523
- code: generatedCode,
411
+ code,
524
412
  map: { mappings: "" }
525
413
  };
526
414
  }
527
415
  }
528
416
  },
417
+ // webpack puts the transform loader on every module this lets through. A loader in the chain
418
+ // stops html-webpack-plugin from rendering its template, and turns binary assets into UTF-8
419
+ // text, so let through only what `transform` handles (the same filter as the one in it)
420
+ transformInclude(id) {
421
+ if (meta.framework !== "webpack") {
422
+ return true;
423
+ }
424
+ _filter ??= pluginutils.createFilter(...resolveIncludeExclude());
425
+ return _filter(id);
426
+ },
529
427
  transform: {
530
428
  async handler(code, id) {
531
- if (isIntlifyVirtualId(id)) return;
532
429
  const filter = await getFilter();
533
430
  if (!filter(id)) {
534
431
  return;
@@ -537,7 +434,7 @@ function resourcePlugin({
537
434
  debug$3("transform", id, JSON.stringify(query), filename);
538
435
  let langInfo = defaultSFCLang;
539
436
  let inSourceMap;
540
- if (!query.vue) {
437
+ if (!query.vue && RE_RESOURCE_FORMAT.test(id)) {
541
438
  langInfo = pathe.parse(filename).ext;
542
439
  const generate = getGenerator(langInfo);
543
440
  const parseOptions = getOptions(
@@ -612,10 +509,7 @@ function resourcePlugin({
612
509
  warn("transformI18nBlock should return a string");
613
510
  }
614
511
  }
615
- if (/\.?[cm]?[jt]s$/.test(langInfo)) {
616
- source = `export default ${source.replace(/^[\s;]+/, "")}`;
617
- }
618
- const { code: generatedCode, map } = await generate(source, parseOptions);
512
+ const { code: generatedCode, map } = /\.?[cm]?[jt]s$/.test(langInfo) ? await generateJsTsBlock(generate, source, parseOptions) : await generate(source, parseOptions);
619
513
  debug$3("generated code", generatedCode);
620
514
  debug$3("sourcemap", map, sourceMap);
621
515
  if (code === generatedCode) return;
@@ -646,6 +540,17 @@ function getGenerator(ext, fallback = bundleUtils.generateJSON) {
646
540
  }
647
541
  return fallback;
648
542
  }
543
+ async function generateJsTsBlock(generate, source, options) {
544
+ try {
545
+ return await generate(source, options);
546
+ } catch (error2) {
547
+ try {
548
+ return await generate(`export default ${source.replace(/^[\s;]+/, "")}`, options);
549
+ } catch (wrappedError) {
550
+ throw /^\s*export\s+default\b/m.test(source) ? error2 : wrappedError;
551
+ }
552
+ }
553
+ }
649
554
  function normalizeConfigResolveAlias(resolve2, framework) {
650
555
  if (resolve2 && resolve2.alias) {
651
556
  return resolve2;
package/lib/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import createDebug from 'debug';
2
2
  import { createUnplugin } from 'unplugin';
3
3
  import { isArray, isString, isBoolean, assign, isEmptyObject, isNumber, generateCodeFrame } from '@intlify/shared';
4
- import { normalize, parse, resolve, dirname } from 'pathe';
4
+ import { normalize, parse, relative, resolve, dirname } from 'pathe';
5
5
  import { generateJSON, generateYAML, generateJavaScript, generateTypescript } from '@intlify/bundle-utils';
6
6
  import { createFilter } from '@rollup/pluginutils';
7
7
  import { promises } from 'node:fs';
@@ -168,7 +168,6 @@ const VIRTUAL_PREFIX = "virtual:";
168
168
  const RE_RESOURCE_FORMAT = /\.(json5?|ya?ml|[c|m]?[j|t]s)$/;
169
169
  const RE_SFC_I18N_CUSTOM_BLOCK = /\?vue&type=i18n/;
170
170
  const RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK = /blockType=i18n/;
171
- const VITE_VIRTUAL_PREFIX = "virtual:intlify-i18n-";
172
171
  const debug$3 = createDebug(resolveNamespace("resource"));
173
172
  function resourcePlugin({
174
173
  onlyLocales,
@@ -194,7 +193,7 @@ function resourcePlugin({
194
193
  }
195
194
  function resolveIncludeExcludeForLegacy() {
196
195
  const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
197
- return include ? [[...include, customBlockInclude], void 0] : [void 0, ["**/**"]];
196
+ return include ? [[...include, customBlockInclude], exclude] : [void 0, ["**/**"]];
198
197
  }
199
198
  let _filter = null;
200
199
  async function getFilter() {
@@ -219,28 +218,13 @@ function resourcePlugin({
219
218
  let isProduction = false;
220
219
  let sourceMap = false;
221
220
  let hasViteJsonPlugin = false;
221
+ let warnedScriptResources = false;
222
222
  const vueI18nAliasName = module;
223
223
  debug$3(`vue-i18n alias name: ${vueI18nAliasName}`);
224
224
  let vuePlugin = null;
225
225
  const getSfcParser = () => {
226
226
  return vuePlugin ? getVueCompiler(vuePlugin).parse : parse$1;
227
227
  };
228
- const virtualIdToRealPath = /* @__PURE__ */ new Map();
229
- const realPathToVirtualId = /* @__PURE__ */ new Map();
230
- let virtualCounter = 0;
231
- function intlifyVirtualize(realPath) {
232
- const normalized = normalize(realPath);
233
- let virtualId = realPathToVirtualId.get(normalized);
234
- if (!virtualId) {
235
- virtualId = `${VITE_VIRTUAL_PREFIX}${virtualCounter++}`;
236
- virtualIdToRealPath.set(virtualId, normalized);
237
- realPathToVirtualId.set(normalized, virtualId);
238
- }
239
- return virtualId;
240
- }
241
- function isIntlifyVirtualId(id) {
242
- return virtualIdToRealPath.has(id);
243
- }
244
228
  return {
245
229
  name: resolveNamespace("resource"),
246
230
  /**
@@ -311,18 +295,12 @@ function resourcePlugin({
311
295
  },
312
296
  handleHotUpdate({ file, modules, server }) {
313
297
  const key = normalize(file);
314
- const virtualId = realPathToVirtualId.get(key);
315
- const inBundle = !!(include && createFilter(include, exclude)(key));
316
- if (!virtualId && !inBundle) return;
298
+ if (!(include && createFilter(include, exclude)(key))) return;
317
299
  const affected = new Set(modules);
318
- const addById = (id) => {
319
- const mod = id && server.moduleGraph.getModuleById(id);
320
- if (mod) affected.add(mod);
321
- };
322
- addById(virtualId);
323
- if (inBundle) {
324
- addById(asVirtualId(INTLIFY_BUNDLE_IMPORT_ID, meta.framework));
325
- }
300
+ const mod = server.moduleGraph.getModuleById(
301
+ asVirtualId(INTLIFY_BUNDLE_IMPORT_ID, meta.framework)
302
+ );
303
+ if (mod) affected.add(mod);
326
304
  if (affected.size === modules.length) return;
327
305
  return [...affected];
328
306
  }
@@ -367,43 +345,15 @@ function resourcePlugin({
367
345
  }
368
346
  },
369
347
  resolveId: {
370
- async handler(id, importer) {
348
+ async handler(id) {
371
349
  if (id === INTLIFY_BUNDLE_IMPORT_ID) {
372
350
  return asVirtualId(id, meta.framework);
373
351
  }
374
- if (meta.framework === "vite" && !hasViteJsonPlugin) {
375
- if (id.includes("?vue&type=i18n") && /[?&]lang\.(?:json|json5)(?:$|&)/.test(id)) {
376
- const { filename, query } = parseVueRequest(id);
377
- let virtualKey = id;
378
- if (query.src && importer) {
379
- const importerPath = (isIntlifyVirtualId(importer) ? virtualIdToRealPath.get(importer) : importer).split("?")[0];
380
- if (importerPath.endsWith(".vue")) {
381
- const resolvedSidecar = resolve(dirname(importerPath), filename);
382
- virtualKey = id.replace(filename, resolvedSidecar);
383
- }
384
- }
385
- return intlifyVirtualize(virtualKey);
386
- }
387
- const idPath = id.split("?")[0];
388
- if (!/\.json5?$/.test(idPath)) return;
389
- let resolvedPath;
390
- if (idPath.startsWith(".")) {
391
- const realImporter = importer && isIntlifyVirtualId(importer) ? virtualIdToRealPath.get(importer) : importer;
392
- if (!realImporter) return;
393
- resolvedPath = resolve(dirname(realImporter), idPath);
394
- } else if (idPath.startsWith("/") || /^[a-z]:[/\\]/i.test(idPath)) {
395
- resolvedPath = idPath;
396
- } else {
397
- return;
398
- }
399
- const filter = await getFilter();
400
- if (!filter(resolvedPath)) return;
401
- const stat = await promises.stat(resolvedPath).catch(() => null);
402
- if (!stat?.isFile()) return;
403
- return intlifyVirtualize(resolvedPath);
404
- }
405
352
  }
406
353
  },
354
+ loadInclude(id) {
355
+ return INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework);
356
+ },
407
357
  load: {
408
358
  async handler(id) {
409
359
  debug$3("load", id);
@@ -412,113 +362,60 @@ function resourcePlugin({
412
362
  for (const inc of include) {
413
363
  resourcePaths = [
414
364
  ...resourcePaths,
415
- ...await glob(inc, { cwd: process.cwd(), expandDirectories: false, absolute: true })
365
+ ...await glob(inc, {
366
+ cwd: process.cwd(),
367
+ expandDirectories: false,
368
+ absolute: true,
369
+ ignore: exclude
370
+ })
416
371
  ];
417
372
  }
418
373
  resourcePaths = resourcePaths.filter((el, pos) => resourcePaths.indexOf(el) === pos);
374
+ if (!warnedScriptResources) {
375
+ const scripts = resourcePaths.filter((path) => /\.[cm]?[jt]s$/.test(path)).sort();
376
+ if (scripts.length > 0) {
377
+ warnedScriptResources = true;
378
+ const listed = scripts.slice(0, 10).map((path) => relative(process.cwd(), path));
379
+ const more = scripts.length > listed.length ? `, and ${scripts.length - listed.length} more` : "";
380
+ warn(
381
+ `JS / TS files under \`include\` are not bundled into \`${INTLIFY_BUNDLE_IMPORT_ID}\` (v11 bundles JSON / YAML only): ${listed.join(", ")}${more}. Import them directly, or collect them with \`import.meta.glob\`.`
382
+ );
383
+ }
384
+ }
385
+ for (const path of resourcePaths) {
386
+ if (/\.(?:json5?|ya?ml)$/.test(path)) {
387
+ this.addWatchFile(path);
388
+ }
389
+ }
419
390
  const code = await generateBundleResources(resourcePaths, isProduction, {
420
391
  forceStringify,
421
392
  strictMessage,
422
393
  escapeHtml,
423
394
  usedKeyFilter: collector ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
424
395
  });
425
- return {
426
- moduleType: "js",
427
- code,
428
- map: { mappings: "" }
429
- };
430
- }
431
- if (isIntlifyVirtualId(id)) {
432
- const realId = virtualIdToRealPath.get(id);
433
- if (realId.includes("?vue&type=i18n")) {
434
- const { filename, query } = parseVueRequest(realId);
435
- let source;
436
- if (query.src) {
437
- this.addWatchFile(filename);
438
- source = await promises.readFile(filename, "utf-8");
439
- } else {
440
- this.addWatchFile(filename);
441
- const sfcSource = await promises.readFile(filename, "utf-8");
442
- const { descriptor } = getSfcParser()(sfcSource, {
443
- sourceMap,
444
- filename
445
- });
446
- const block = descriptor.customBlocks[query.index];
447
- if (!block) return;
448
- source = block.content;
449
- }
450
- if (typeof transformI18nBlock === "function") {
451
- const modifiedSource = transformI18nBlock(source);
452
- if (modifiedSource && typeof modifiedSource === "string") {
453
- source = modifiedSource;
454
- } else {
455
- warn("transformI18nBlock should return a string");
456
- }
457
- }
458
- let langInfo2 = defaultSFCLang;
459
- if (isString(query.lang)) {
460
- langInfo2 = query.src ? query.lang === "i18n" ? defaultSFCLang : query.lang : query.lang;
461
- }
462
- if (/\.?[cm]?[jt]s$/.test(langInfo2)) {
463
- source = `export default ${source.replace(/^[\s;]+/, "")}`;
464
- }
465
- const generate2 = getGenerator(langInfo2, generateYAML);
466
- const isGlobalBlock = globalSFCScope || !!query.global;
467
- const parseOptions2 = getOptions(
468
- filename,
469
- isProduction,
470
- query,
471
- sourceMap,
472
- {
473
- isGlobal: globalSFCScope,
474
- allowDynamic,
475
- jit: true,
476
- strictMessage,
477
- escapeHtml,
478
- onlyLocales,
479
- forceStringify,
480
- usedKeyFilter: collector && isGlobalBlock ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
481
- }
482
- );
483
- const { code: generatedCode2 } = await generate2(source, parseOptions2);
484
- return {
485
- moduleType: "js",
486
- code: generatedCode2,
487
- map: { mappings: "" }
488
- };
396
+ if (meta.framework !== "vite") {
397
+ return { code };
489
398
  }
490
- this.addWatchFile(realId);
491
- const code = await promises.readFile(realId, "utf-8");
492
- const langInfo = parse(realId).ext;
493
- const generate = getGenerator(langInfo);
494
- const parseOptions = getOptions(
495
- realId,
496
- isProduction,
497
- {},
498
- sourceMap,
499
- {
500
- isGlobal: globalSFCScope,
501
- allowDynamic,
502
- strictMessage,
503
- escapeHtml,
504
- jit: true,
505
- onlyLocales,
506
- forceStringify,
507
- usedKeyFilter: collector ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
508
- }
509
- );
510
- const { code: generatedCode } = await generate(code, parseOptions);
511
399
  return {
512
400
  moduleType: "js",
513
- code: generatedCode,
401
+ code,
514
402
  map: { mappings: "" }
515
403
  };
516
404
  }
517
405
  }
518
406
  },
407
+ // webpack puts the transform loader on every module this lets through. A loader in the chain
408
+ // stops html-webpack-plugin from rendering its template, and turns binary assets into UTF-8
409
+ // text, so let through only what `transform` handles (the same filter as the one in it)
410
+ transformInclude(id) {
411
+ if (meta.framework !== "webpack") {
412
+ return true;
413
+ }
414
+ _filter ??= createFilter(...resolveIncludeExclude());
415
+ return _filter(id);
416
+ },
519
417
  transform: {
520
418
  async handler(code, id) {
521
- if (isIntlifyVirtualId(id)) return;
522
419
  const filter = await getFilter();
523
420
  if (!filter(id)) {
524
421
  return;
@@ -527,7 +424,7 @@ function resourcePlugin({
527
424
  debug$3("transform", id, JSON.stringify(query), filename);
528
425
  let langInfo = defaultSFCLang;
529
426
  let inSourceMap;
530
- if (!query.vue) {
427
+ if (!query.vue && RE_RESOURCE_FORMAT.test(id)) {
531
428
  langInfo = parse(filename).ext;
532
429
  const generate = getGenerator(langInfo);
533
430
  const parseOptions = getOptions(
@@ -602,10 +499,7 @@ function resourcePlugin({
602
499
  warn("transformI18nBlock should return a string");
603
500
  }
604
501
  }
605
- if (/\.?[cm]?[jt]s$/.test(langInfo)) {
606
- source = `export default ${source.replace(/^[\s;]+/, "")}`;
607
- }
608
- const { code: generatedCode, map } = await generate(source, parseOptions);
502
+ const { code: generatedCode, map } = /\.?[cm]?[jt]s$/.test(langInfo) ? await generateJsTsBlock(generate, source, parseOptions) : await generate(source, parseOptions);
609
503
  debug$3("generated code", generatedCode);
610
504
  debug$3("sourcemap", map, sourceMap);
611
505
  if (code === generatedCode) return;
@@ -636,6 +530,17 @@ function getGenerator(ext, fallback = generateJSON) {
636
530
  }
637
531
  return fallback;
638
532
  }
533
+ async function generateJsTsBlock(generate, source, options) {
534
+ try {
535
+ return await generate(source, options);
536
+ } catch (error2) {
537
+ try {
538
+ return await generate(`export default ${source.replace(/^[\s;]+/, "")}`, options);
539
+ } catch (wrappedError) {
540
+ throw /^\s*export\s+default\b/m.test(source) ? error2 : wrappedError;
541
+ }
542
+ }
543
+ }
639
544
  function normalizeConfigResolveAlias(resolve2, framework) {
640
545
  if (resolve2 && resolve2.alias) {
641
546
  return resolve2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/unplugin-vue-i18n",
3
- "version": "11.2.6",
3
+ "version": "11.2.7",
4
4
  "description": "unplugin for Vue I18n",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
@@ -27,7 +27,7 @@
27
27
  }
28
28
  },
29
29
  "dependencies": {
30
- "@intlify/shared": "~11.4.2",
30
+ "@intlify/shared": "~11.4.15",
31
31
  "@intlify/vue-i18n-extensions": "^8.0.0",
32
32
  "@rollup/pluginutils": "^5.1.0",
33
33
  "@typescript-eslint/scope-manager": "^8.13.0",
@@ -38,7 +38,7 @@
38
38
  "tinyglobby": "^0.2.15",
39
39
  "unplugin": "^2.3.4",
40
40
  "vue": "~3.5.34",
41
- "@intlify/bundle-utils": "11.2.6"
41
+ "@intlify/bundle-utils": "11.2.7"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@intlify/core-base": "next",
@@ -54,6 +54,7 @@
54
54
  "unbuild": "^3.6.1",
55
55
  "vite": "^8.0.0",
56
56
  "rollup-vite": "npm:vite@6.3.5",
57
+ "vite7": "npm:vite@^7.0.0",
57
58
  "vue-loader": "~17.4.2",
58
59
  "webpack": "~5.106.2",
59
60
  "webpack-merge": "^5.9.0"