@intlify/unplugin-vue-i18n 0.8.1 → 0.9.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
@@ -213,6 +213,20 @@ const app = createApp()
213
213
  app.use(i18n).mount('#app')
214
214
  ```
215
215
 
216
+ Change your vite.config.ts file accordingly to import all the files from locales folder on the root. Change `'./locales/**'` to path of your locales.
217
+ ```ts
218
+ // vite.config.ts
219
+ import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
220
+ import path from 'path'
221
+
222
+ export default defineConfig({
223
+ plugins: [
224
+ VueI18nPlugin({
225
+ include: [path.resolve(__dirname, './locales/**')],
226
+ }),
227
+ ],
228
+ })
229
+ ```
216
230
  ### Types
217
231
 
218
232
  If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplugin-vue-i18n/messages` to `compilerOptions.types` of your tsconfig:
@@ -228,7 +242,7 @@ If you want type definition of `@intlify/unplugin-vue-i18n/messages`, add `unplu
228
242
 
229
243
  ## 📦 Automatic bundling
230
244
  ### For Vue I18n
231
- As noted [here](https://vue-i18n.intlify.dev/installation.html#explanation-of-different-builds), NPM provides many different builds of Vue I18n.
245
+ As noted [here](https://vue-i18n.intlify.dev/guide/installation.html#explanation-of-different-builds), NPM provides many different builds of Vue I18n.
232
246
 
233
247
  This plugin will automatically select and bundle Vue I18n build according to the following behavior:
234
248
 
@@ -259,10 +273,21 @@ This plugin will automatically select and bundle `petite-vue-i18n` build accordi
259
273
  - json5
260
274
  - yaml
261
275
  - yml
276
+ - js
277
+ - ts
262
278
  ```
263
279
 
264
280
  Note `json` resources matches this option, it will be handled **before the internal json plugin of bundler, and will not be processed afterwards**, else the option doesn't match, the bundler side will handle.
265
281
 
282
+ Note that `js` and `ts` resources are limited to **simple export (`export default`) as locale messages object only**, such as programmatically dynamic resource construction is not guaranteed to work currently.
283
+
284
+ ```js
285
+ export default {
286
+ hello: 'Hello, {name}!',
287
+ // ...
288
+ }
289
+ ```
290
+
266
291
  If nothing is specified for this option, i.e. `undefined`, nothing is done to the resource in the above format.
267
292
 
268
293
  ### `runtimeOnly`
package/lib/index.cjs CHANGED
@@ -15,14 +15,6 @@ const yaml = require('js-yaml');
15
15
  const fs = require('fs');
16
16
  const pc = require('picocolors');
17
17
 
18
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
19
-
20
- const createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
21
- const fg__default = /*#__PURE__*/_interopDefaultLegacy(fg);
22
- const JSON5__default = /*#__PURE__*/_interopDefaultLegacy(JSON5);
23
- const yaml__default = /*#__PURE__*/_interopDefaultLegacy(yaml);
24
- const pc__default = /*#__PURE__*/_interopDefaultLegacy(pc);
25
-
26
18
  function parseVueRequest(id) {
27
19
  const [filename, rawQuery] = id.split(`?`, 2);
28
20
  const params = new URLSearchParams(rawQuery);
@@ -75,20 +67,20 @@ function convert(source, lang) {
75
67
  switch (lang) {
76
68
  case "yaml":
77
69
  case "yml":
78
- const data = yaml__default.load(source);
70
+ const data = yaml.load(source);
79
71
  return JSON.stringify(data, void 0, " ");
80
72
  case "json5":
81
- return JSON.stringify(JSON5__default.parse(source));
73
+ return JSON.stringify(JSON5.parse(source));
82
74
  default:
83
75
  return source;
84
76
  }
85
77
  }
86
78
 
87
79
  function warn(...args) {
88
- console.warn(pc__default.yellow(pc__default.bold(`[unplugin-vue-i18n] `)), ...args);
80
+ console.warn(pc.yellow(pc.bold(`[unplugin-vue-i18n] `)), ...args);
89
81
  }
90
82
  function error(...args) {
91
- console.error(pc__default.red(pc__default.bold(`[unplugin-vue-i18n] `)), ...args);
83
+ console.error(pc.red(pc.bold(`[unplugin-vue-i18n] `)), ...args);
92
84
  }
93
85
  async function getRaw(path) {
94
86
  return fs.promises.readFile(path, { encoding: "utf-8" });
@@ -100,7 +92,7 @@ function raiseError(message) {
100
92
  const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
101
93
  const INTLIFY_BUNDLE_IMPORT_DEPRECTED_ID = "@intlify/vite-plugin-vue-i18n/messages";
102
94
  const VIRTUAL_PREFIX = "\0";
103
- const debug = createDebug__default("unplugin-vue-i18n");
95
+ const debug = createDebug("unplugin-vue-i18n");
104
96
  const installedPkg = bundleUtils.checkInstallPackage("@intlify/unplugin-vue-i18n", debug);
105
97
  const installedVueI18nBridge = bundleUtils.checkVueI18nBridgeInstallPackage(debug);
106
98
  const vueI18nVersion = bundleUtils.getVueI18nVersion(debug);
@@ -231,6 +223,59 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
231
223
  return orgTransform.apply(this, [code, id]);
232
224
  };
233
225
  }
226
+ const esbuildPlugin = config.plugins.find(
227
+ (p) => p.name === "vite:esbuild"
228
+ );
229
+ if (esbuildPlugin) {
230
+ const orgTransform = esbuildPlugin.transform;
231
+ esbuildPlugin.transform = async function(code, id) {
232
+ const result = await orgTransform.apply(this, [
233
+ code,
234
+ id
235
+ ]);
236
+ if (result == null) {
237
+ return result;
238
+ }
239
+ const { filename, query } = parseVueRequest(id);
240
+ if (!query.vue && filter(id) && /\.[c|m]?ts$/.test(id)) {
241
+ const [_code, inSourceMap] = shared.isString(result) ? [result, void 0] : [result.code, result.map];
242
+ let langInfo = defaultSFCLang;
243
+ langInfo = pathe.parse(filename).ext;
244
+ const generate = getGenerator(langInfo);
245
+ const parseOptions = getOptions(
246
+ filename,
247
+ isProduction,
248
+ query,
249
+ sourceMap,
250
+ {
251
+ inSourceMap,
252
+ isGlobal: globalSFCScope,
253
+ useClassComponent,
254
+ bridge,
255
+ exportESM: esm,
256
+ forceStringify
257
+ }
258
+ );
259
+ debug("parseOptions", parseOptions);
260
+ const { code: generatedCode, map } = generate(
261
+ _code,
262
+ parseOptions,
263
+ bridge ? createBridgeCodeGenerator(_code, query) : void 0
264
+ );
265
+ debug("generated code", generatedCode);
266
+ console.log("generated code", generatedCode);
267
+ debug("sourcemap", map, sourceMap);
268
+ if (_code === generatedCode)
269
+ return;
270
+ return {
271
+ code: generatedCode,
272
+ map: sourceMap ? map : { mappings: "" }
273
+ };
274
+ } else {
275
+ return result;
276
+ }
277
+ };
278
+ }
234
279
  },
235
280
  async handleHotUpdate({ file, server }) {
236
281
  if (/\.(json5?|ya?ml)$/.test(file)) {
@@ -317,7 +362,7 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
317
362
  let resourcePaths = [];
318
363
  const includePaths = shared.isArray(include) ? include : [include];
319
364
  for (const inc of includePaths) {
320
- resourcePaths = [...resourcePaths, ...await fg__default(inc)];
365
+ resourcePaths = [...resourcePaths, ...await fg(inc)];
321
366
  }
322
367
  resourcePaths = resourcePaths.filter(
323
368
  (el, pos) => resourcePaths.indexOf(el) === pos
@@ -354,9 +399,9 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
354
399
  let langInfo = defaultSFCLang;
355
400
  let inSourceMap;
356
401
  if (!query.vue) {
357
- if (/\.(json5?|ya?ml)$/.test(id) && filter(id)) {
402
+ if (/\.(json5?|ya?ml|[c|m]?js)$/.test(id) && filter(id)) {
358
403
  langInfo = pathe.parse(filename).ext;
359
- const generate = /\.?json5?/.test(langInfo) ? bundleUtils.generateJSON : bundleUtils.generateYAML;
404
+ const generate = getGenerator(langInfo);
360
405
  const parseOptions = getOptions(
361
406
  filename,
362
407
  isProduction,
@@ -435,6 +480,9 @@ const unplugin = unplugin$1.createUnplugin((options = {}, meta) => {
435
480
  }
436
481
  };
437
482
  });
483
+ function getGenerator(ext, defaultGen = bundleUtils.generateJSON) {
484
+ return /\.?json5?$/.test(ext) ? bundleUtils.generateJSON : /\.ya?ml$/.test(ext) ? bundleUtils.generateYAML : /\.([c|m]?js|[c|m]?ts)$/.test(ext) ? bundleUtils.generateJavaScript : defaultGen;
485
+ }
438
486
  function normalizeConfigResolveAlias(resolve, framework) {
439
487
  if (resolve && resolve.alias) {
440
488
  return resolve;
@@ -587,5 +635,5 @@ function asVirtualId(id, framework = "vite") {
587
635
  return framework === "vite" ? VIRTUAL_PREFIX + id : id;
588
636
  }
589
637
 
590
- exports["default"] = unplugin;
638
+ exports.default = unplugin;
591
639
  exports.unplugin = unplugin;
package/lib/index.d.ts CHANGED
@@ -2,6 +2,6 @@ import * as _unplugin from 'unplugin';
2
2
  import { PluginOptions } from './types.js';
3
3
  export { PluginOptions, SFCLangFormat } from './types.js';
4
4
 
5
- declare const unplugin: _unplugin.UnpluginInstance<PluginOptions, false>;
5
+ declare const unplugin: _unplugin.UnpluginInstance<PluginOptions, boolean>;
6
6
 
7
7
  export { unplugin as default, unplugin };
package/lib/index.mjs CHANGED
@@ -4,7 +4,7 @@ import createDebug from 'debug';
4
4
  import fg from 'fast-glob';
5
5
  import { isString, isArray, isBoolean, isObject, isNumber, isEmptyObject, assign, generateCodeFrame } from '@intlify/shared';
6
6
  import { createFilter } from '@rollup/pluginutils';
7
- import { checkInstallPackage, checkVueI18nBridgeInstallPackage, getVueI18nVersion, generateJSON, generateYAML } from '@intlify/bundle-utils';
7
+ import { checkInstallPackage, checkVueI18nBridgeInstallPackage, getVueI18nVersion, generateJSON, generateYAML, generateJavaScript } from '@intlify/bundle-utils';
8
8
  import { parse as parse$1 } from '@vue/compiler-sfc';
9
9
  import JSON5 from 'json5';
10
10
  import yaml from 'js-yaml';
@@ -219,6 +219,59 @@ const unplugin = createUnplugin((options = {}, meta) => {
219
219
  return orgTransform.apply(this, [code, id]);
220
220
  };
221
221
  }
222
+ const esbuildPlugin = config.plugins.find(
223
+ (p) => p.name === "vite:esbuild"
224
+ );
225
+ if (esbuildPlugin) {
226
+ const orgTransform = esbuildPlugin.transform;
227
+ esbuildPlugin.transform = async function(code, id) {
228
+ const result = await orgTransform.apply(this, [
229
+ code,
230
+ id
231
+ ]);
232
+ if (result == null) {
233
+ return result;
234
+ }
235
+ const { filename, query } = parseVueRequest(id);
236
+ if (!query.vue && filter(id) && /\.[c|m]?ts$/.test(id)) {
237
+ const [_code, inSourceMap] = isString(result) ? [result, void 0] : [result.code, result.map];
238
+ let langInfo = defaultSFCLang;
239
+ langInfo = parse(filename).ext;
240
+ const generate = getGenerator(langInfo);
241
+ const parseOptions = getOptions(
242
+ filename,
243
+ isProduction,
244
+ query,
245
+ sourceMap,
246
+ {
247
+ inSourceMap,
248
+ isGlobal: globalSFCScope,
249
+ useClassComponent,
250
+ bridge,
251
+ exportESM: esm,
252
+ forceStringify
253
+ }
254
+ );
255
+ debug("parseOptions", parseOptions);
256
+ const { code: generatedCode, map } = generate(
257
+ _code,
258
+ parseOptions,
259
+ bridge ? createBridgeCodeGenerator(_code, query) : void 0
260
+ );
261
+ debug("generated code", generatedCode);
262
+ console.log("generated code", generatedCode);
263
+ debug("sourcemap", map, sourceMap);
264
+ if (_code === generatedCode)
265
+ return;
266
+ return {
267
+ code: generatedCode,
268
+ map: sourceMap ? map : { mappings: "" }
269
+ };
270
+ } else {
271
+ return result;
272
+ }
273
+ };
274
+ }
222
275
  },
223
276
  async handleHotUpdate({ file, server }) {
224
277
  if (/\.(json5?|ya?ml)$/.test(file)) {
@@ -342,9 +395,9 @@ const unplugin = createUnplugin((options = {}, meta) => {
342
395
  let langInfo = defaultSFCLang;
343
396
  let inSourceMap;
344
397
  if (!query.vue) {
345
- if (/\.(json5?|ya?ml)$/.test(id) && filter(id)) {
398
+ if (/\.(json5?|ya?ml|[c|m]?js)$/.test(id) && filter(id)) {
346
399
  langInfo = parse(filename).ext;
347
- const generate = /\.?json5?/.test(langInfo) ? generateJSON : generateYAML;
400
+ const generate = getGenerator(langInfo);
348
401
  const parseOptions = getOptions(
349
402
  filename,
350
403
  isProduction,
@@ -423,6 +476,9 @@ const unplugin = createUnplugin((options = {}, meta) => {
423
476
  }
424
477
  };
425
478
  });
479
+ function getGenerator(ext, defaultGen = generateJSON) {
480
+ return /\.?json5?$/.test(ext) ? generateJSON : /\.ya?ml$/.test(ext) ? generateYAML : /\.([c|m]?js|[c|m]?ts)$/.test(ext) ? generateJavaScript : defaultGen;
481
+ }
426
482
  function normalizeConfigResolveAlias(resolve, framework) {
427
483
  if (resolve && resolve.alias) {
428
484
  return resolve;
package/lib/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare type SFCLangFormat = 'json' | 'json5' | 'yml' | 'yaml';
1
+ type SFCLangFormat = 'json' | 'json5' | 'yml' | 'yaml';
2
2
  interface PluginOptions {
3
3
  include?: string | string[];
4
4
  runtimeOnly?: boolean;
package/lib/vite.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as vite from 'vite';
2
2
  import { PluginOptions } from './types.js';
3
3
 
4
- declare const _default: (options: PluginOptions) => vite.Plugin;
4
+ declare const _default: (options: PluginOptions) => vite.Plugin | vite.Plugin[];
5
5
 
6
6
  export { _default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/unplugin-vue-i18n",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "unplugin for Vue I18n",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
@@ -26,22 +26,22 @@
26
26
  }
27
27
  },
28
28
  "dependencies": {
29
- "@intlify/bundle-utils": "^3.4.0",
29
+ "@intlify/bundle-utils": "^5.0.0",
30
30
  "@intlify/shared": "next",
31
- "@rollup/pluginutils": "^4.2.0",
32
- "@vue/compiler-sfc": "^3.2.45",
33
- "debug": "^4.3.1",
34
- "fast-glob": "^3.2.5",
31
+ "@rollup/pluginutils": "^5.0.2",
32
+ "@vue/compiler-sfc": "^3.2.47",
33
+ "debug": "^4.3.3",
34
+ "fast-glob": "^3.2.12",
35
35
  "js-yaml": "^4.1.0",
36
- "json5": "^2.2.0",
36
+ "json5": "^2.2.3",
37
37
  "pathe": "^1.0.0",
38
38
  "picocolors": "^1.0.0",
39
39
  "source-map": "0.6.1",
40
- "unplugin": "^1.0.0"
40
+ "unplugin": "^1.1.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "mlly": "^1.0.0",
44
- "unbuild": "^0.8.11"
44
+ "unbuild": "^1.0.2"
45
45
  },
46
46
  "engines": {
47
47
  "node": ">= 14.16"