@intlify/unplugin-vue-i18n 5.0.0-beta.2 → 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 +47 -0
- package/lib/index.cjs +124 -44
- package/lib/index.mjs +123 -43
- package/lib/types.d.cts +1 -0
- package/lib/types.d.mts +1 -0
- package/lib/types.d.ts +1 -0
- package/lib/vite.cjs +3 -2
- package/lib/vite.mjs +3 -2
- package/lib/webpack.cjs +3 -2
- package/lib/webpack.mjs +3 -2
- package/package.json +2 -1
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
|
@@ -4,11 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
const unplugin$1 = require('unplugin');
|
|
6
6
|
const createDebug = require('debug');
|
|
7
|
-
const fs = require('node:fs');
|
|
8
7
|
const pc = require('picocolors');
|
|
9
|
-
const module$1 = require('node:module');
|
|
10
8
|
const path = require('node:path');
|
|
9
|
+
const fs = require('node:fs');
|
|
10
|
+
const module$1 = require('node:module');
|
|
11
11
|
const pathe = require('pathe');
|
|
12
|
+
const mlly = require('mlly');
|
|
12
13
|
const shared = require('@intlify/shared');
|
|
13
14
|
const fg = require('fast-glob');
|
|
14
15
|
const pluginutils = require('@rollup/pluginutils');
|
|
@@ -23,52 +24,27 @@ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentS
|
|
|
23
24
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
24
25
|
|
|
25
26
|
const createDebug__default = /*#__PURE__*/_interopDefaultCompat(createDebug);
|
|
26
|
-
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
27
27
|
const pc__default = /*#__PURE__*/_interopDefaultCompat(pc);
|
|
28
|
-
const module__default = /*#__PURE__*/_interopDefaultCompat(module$1);
|
|
29
28
|
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
29
|
+
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
30
|
+
const module__default = /*#__PURE__*/_interopDefaultCompat(module$1);
|
|
30
31
|
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
31
32
|
const eslintUitls__default = /*#__PURE__*/_interopDefaultCompat(eslintUitls);
|
|
32
33
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
const pkgInfo = resolvePkgPath("vue-i18n", debug) || resolvePkgPath("petite-vue-i18n", debug);
|
|
36
|
-
if (!pkgInfo) {
|
|
37
|
-
throw new Error(
|
|
38
|
-
`requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
debug("installed package info:", pkgInfo);
|
|
42
|
-
return pkgInfo;
|
|
43
|
-
}
|
|
44
|
-
function resolvePkgPath(id, debug) {
|
|
45
|
-
try {
|
|
46
|
-
const resolvedPath = _require.resolve(id);
|
|
47
|
-
const pkgPath = path__default.dirname(resolvedPath);
|
|
48
|
-
const pkgJson = JSON.parse(
|
|
49
|
-
fs__default.readFileSync(path__default.join(pkgPath, "package.json"), "utf-8")
|
|
50
|
-
);
|
|
51
|
-
const pkgName = pkgJson.name.startsWith("vue-i18n") ? "vue-i18n" : pkgJson.name.startsWith("petite-vue-i18n") ? "petite-vue-i18n" : "";
|
|
52
|
-
return pkgJson ? { alias: id, pkg: pkgName } : null;
|
|
53
|
-
} catch (e) {
|
|
54
|
-
debug(`cannot find '${id}'`, e);
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
function resolveNamespace(name) {
|
|
59
|
-
return `unplugin-vue-i18n:${name}`;
|
|
60
|
-
}
|
|
34
|
+
const PKG_NAME = "unplugin-vue-i18n";
|
|
35
|
+
|
|
61
36
|
function warn(...args) {
|
|
62
|
-
console.warn(pc__default.yellow(pc__default.bold(`[
|
|
37
|
+
console.warn(pc__default.yellow(pc__default.bold(`[${PKG_NAME}] `)), ...args);
|
|
63
38
|
}
|
|
64
39
|
function error(...args) {
|
|
65
|
-
console.error(pc__default.red(pc__default.bold(`[
|
|
66
|
-
}
|
|
67
|
-
async function getRaw(path2) {
|
|
68
|
-
return fs.promises.readFile(path2, { encoding: "utf-8" });
|
|
40
|
+
console.error(pc__default.red(pc__default.bold(`[${PKG_NAME}] `)), ...args);
|
|
69
41
|
}
|
|
70
42
|
function raiseError(message) {
|
|
71
|
-
throw new Error(`[
|
|
43
|
+
throw new Error(`[${PKG_NAME}] ${message}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveNamespace(name) {
|
|
47
|
+
return `${PKG_NAME}:${name}`;
|
|
72
48
|
}
|
|
73
49
|
function getVitePlugin(config, name) {
|
|
74
50
|
return config.plugins.find((p) => p.name === name);
|
|
@@ -91,6 +67,92 @@ function normalizePath(id) {
|
|
|
91
67
|
return path__default.posix.normalize(isWindows ? slash(id) : id);
|
|
92
68
|
}
|
|
93
69
|
|
|
70
|
+
const defaultFindOptions = {
|
|
71
|
+
startingFrom: ".",
|
|
72
|
+
rootPattern: /^node_modules$/,
|
|
73
|
+
reverse: false,
|
|
74
|
+
test: (filePath) => {
|
|
75
|
+
try {
|
|
76
|
+
if (fs.statSync(filePath).isFile()) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
function findFile(filename, _options = {}) {
|
|
84
|
+
const filenames = Array.isArray(filename) ? filename : [filename];
|
|
85
|
+
const options = { ...defaultFindOptions, ..._options };
|
|
86
|
+
const basePath = pathe.resolve(options.startingFrom);
|
|
87
|
+
const leadingSlash = basePath[0] === "/";
|
|
88
|
+
const segments = basePath.split("/").filter(Boolean);
|
|
89
|
+
if (leadingSlash) {
|
|
90
|
+
segments[0] = "/" + segments[0];
|
|
91
|
+
}
|
|
92
|
+
let root = segments.findIndex((r) => r.match(options.rootPattern));
|
|
93
|
+
if (root === -1) {
|
|
94
|
+
root = 0;
|
|
95
|
+
}
|
|
96
|
+
if (options.reverse) {
|
|
97
|
+
for (let index = root + 1; index <= segments.length; index++) {
|
|
98
|
+
for (const filename2 of filenames) {
|
|
99
|
+
const filePath = pathe.join(...segments.slice(0, index), filename2);
|
|
100
|
+
if (options.test(filePath)) {
|
|
101
|
+
return filePath;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
for (let index = segments.length; index > root; index--) {
|
|
107
|
+
for (const filename2 of filenames) {
|
|
108
|
+
const filePath = pathe.join(...segments.slice(0, index), filename2);
|
|
109
|
+
if (options.test(filePath)) {
|
|
110
|
+
return filePath;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
throw new Error(
|
|
116
|
+
`Cannot find matching ${filename} in ${options.startingFrom} or parent directories`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
function findNearestFile(filename, _options = {}) {
|
|
120
|
+
return findFile(filename, _options);
|
|
121
|
+
}
|
|
122
|
+
function resolvePackageJSON(id = process.cwd(), options = {}) {
|
|
123
|
+
const resolvedPath = pathe.isAbsolute(id) ? id : mlly.resolvePathSync(id, options);
|
|
124
|
+
return findNearestFile("package.json", {
|
|
125
|
+
startingFrom: resolvedPath,
|
|
126
|
+
...options
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
131
|
+
function checkInstallPackage(debug) {
|
|
132
|
+
const pkgInfo = resolvePkgPath("vue-i18n", debug) || resolvePkgPath("petite-vue-i18n", debug);
|
|
133
|
+
if (!pkgInfo) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
debug("installed package info:", pkgInfo);
|
|
139
|
+
return pkgInfo;
|
|
140
|
+
}
|
|
141
|
+
function resolvePkgPath(id, debug) {
|
|
142
|
+
try {
|
|
143
|
+
const modPath = _require.resolve(id);
|
|
144
|
+
debug("modPath:", modPath, id);
|
|
145
|
+
const pkgJsonPath = resolvePackageJSON(modPath);
|
|
146
|
+
debug("pkgJsonPath:", pkgJsonPath);
|
|
147
|
+
const pkgJson = JSON.parse(fs__default.readFileSync(pkgJsonPath, "utf-8"));
|
|
148
|
+
const pkgName = pkgJson.name.startsWith("vue-i18n") ? "vue-i18n" : pkgJson.name.startsWith("petite-vue-i18n") ? "petite-vue-i18n" : "";
|
|
149
|
+
return pkgJson ? { alias: id, pkg: pkgName } : null;
|
|
150
|
+
} catch (e) {
|
|
151
|
+
debug(`cannot find '${id}'`, e);
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
94
156
|
function resolveOptions(options, installedPkgInfo) {
|
|
95
157
|
let onlyLocales = [];
|
|
96
158
|
if (options.onlyLocales) {
|
|
@@ -120,6 +182,7 @@ function resolveOptions(options, installedPkgInfo) {
|
|
|
120
182
|
const escapeHtml = !!options.escapeHtml;
|
|
121
183
|
const optimizeTranslationDirective = shared.isString(options.optimizeTranslationDirective) || shared.isArray(options.optimizeTranslationDirective) ? options.optimizeTranslationDirective : !!options.optimizeTranslationDirective;
|
|
122
184
|
const translationIdentifiers = /* @__PURE__ */ new Map();
|
|
185
|
+
const transformI18nBlock = typeof options.transformI18nBlock === "function" ? options.transformI18nBlock : null;
|
|
123
186
|
return {
|
|
124
187
|
include,
|
|
125
188
|
exclude,
|
|
@@ -136,7 +199,8 @@ function resolveOptions(options, installedPkgInfo) {
|
|
|
136
199
|
strictMessage,
|
|
137
200
|
escapeHtml,
|
|
138
201
|
optimizeTranslationDirective,
|
|
139
|
-
translationIdentifiers
|
|
202
|
+
translationIdentifiers,
|
|
203
|
+
transformI18nBlock
|
|
140
204
|
};
|
|
141
205
|
}
|
|
142
206
|
|
|
@@ -222,7 +286,8 @@ function resourcePlugin({
|
|
|
222
286
|
ssrBuild,
|
|
223
287
|
strictMessage,
|
|
224
288
|
allowDynamic,
|
|
225
|
-
escapeHtml
|
|
289
|
+
escapeHtml,
|
|
290
|
+
transformI18nBlock
|
|
226
291
|
}, meta, installedPkgInfo) {
|
|
227
292
|
const filter = pluginutils.createFilter(include, exclude);
|
|
228
293
|
const getVueI18nAliasPath = ({ ssr = false, runtimeOnly: runtimeOnly2 = false }) => {
|
|
@@ -504,7 +569,7 @@ function resourcePlugin({
|
|
|
504
569
|
}
|
|
505
570
|
);
|
|
506
571
|
debug$2("parseOptions", parseOptions);
|
|
507
|
-
|
|
572
|
+
let source = await getCode(
|
|
508
573
|
code,
|
|
509
574
|
filename,
|
|
510
575
|
sourceMap,
|
|
@@ -512,6 +577,14 @@ function resourcePlugin({
|
|
|
512
577
|
getSfcParser(),
|
|
513
578
|
meta.framework
|
|
514
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
|
+
}
|
|
515
588
|
const { code: generatedCode, map } = generate(source, parseOptions);
|
|
516
589
|
debug$2("generated code", generatedCode);
|
|
517
590
|
debug$2("sourcemap", map, sourceMap);
|
|
@@ -565,7 +638,8 @@ async function generateBundleResources(resources, isProduction, {
|
|
|
565
638
|
onlyLocales = [],
|
|
566
639
|
strictMessage = true,
|
|
567
640
|
escapeHtml = false,
|
|
568
|
-
jit = true
|
|
641
|
+
jit = true,
|
|
642
|
+
transformI18nBlock = void 0
|
|
569
643
|
}) {
|
|
570
644
|
const codes = [];
|
|
571
645
|
for (const res of resources) {
|
|
@@ -580,7 +654,8 @@ async function generateBundleResources(resources, isProduction, {
|
|
|
580
654
|
onlyLocales,
|
|
581
655
|
strictMessage,
|
|
582
656
|
escapeHtml,
|
|
583
|
-
forceStringify
|
|
657
|
+
forceStringify,
|
|
658
|
+
transformI18nBlock
|
|
584
659
|
});
|
|
585
660
|
parseOptions.type = "bare";
|
|
586
661
|
const { code } = generate(source, parseOptions);
|
|
@@ -652,7 +727,8 @@ function getOptions(filename, isProduction, query, sourceMap, {
|
|
|
652
727
|
allowDynamic = false,
|
|
653
728
|
strictMessage = true,
|
|
654
729
|
escapeHtml = false,
|
|
655
|
-
jit = true
|
|
730
|
+
jit = true,
|
|
731
|
+
transformI18nBlock = null
|
|
656
732
|
}) {
|
|
657
733
|
const mode = isProduction ? "production" : "development";
|
|
658
734
|
const baseOptions = {
|
|
@@ -666,6 +742,7 @@ function getOptions(filename, isProduction, query, sourceMap, {
|
|
|
666
742
|
jit,
|
|
667
743
|
onlyLocales,
|
|
668
744
|
env: mode,
|
|
745
|
+
transformI18nBlock,
|
|
669
746
|
onWarn: (msg) => {
|
|
670
747
|
warn(`${filename} ${msg}`);
|
|
671
748
|
},
|
|
@@ -704,6 +781,9 @@ function getVirtualId(id, framework = "vite") {
|
|
|
704
781
|
function asVirtualId(id, framework = "vite") {
|
|
705
782
|
return framework === "vite" ? VIRTUAL_PREFIX + id : id;
|
|
706
783
|
}
|
|
784
|
+
async function getRaw(path) {
|
|
785
|
+
return fs.promises.readFile(path, { encoding: "utf-8" });
|
|
786
|
+
}
|
|
707
787
|
|
|
708
788
|
const debug$1 = createDebug__default(resolveNamespace("directive"));
|
|
709
789
|
function directivePlugin({
|
package/lib/index.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { createUnplugin } from 'unplugin';
|
|
2
2
|
import createDebug from 'debug';
|
|
3
|
-
import fs, { promises } from 'node:fs';
|
|
4
3
|
import pc from 'picocolors';
|
|
5
|
-
import module from 'node:module';
|
|
6
4
|
import path from 'node:path';
|
|
7
|
-
import {
|
|
5
|
+
import fs, { statSync, promises } from 'node:fs';
|
|
6
|
+
import module from 'node:module';
|
|
7
|
+
import { isAbsolute, resolve, join, normalize, parse } from 'pathe';
|
|
8
|
+
import { resolvePathSync } from 'mlly';
|
|
8
9
|
import { isArray, isString, isBoolean, assign, isNumber, isEmptyObject, generateCodeFrame } from '@intlify/shared';
|
|
9
10
|
import fg from 'fast-glob';
|
|
10
11
|
import { createFilter } from '@rollup/pluginutils';
|
|
@@ -15,45 +16,20 @@ import { analyze } from '@typescript-eslint/scope-manager';
|
|
|
15
16
|
import { parse as parse$2, simpleTraverse, AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
|
|
16
17
|
import eslintUitls from '@eslint-community/eslint-utils';
|
|
17
18
|
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
const pkgInfo = resolvePkgPath("vue-i18n", debug) || resolvePkgPath("petite-vue-i18n", debug);
|
|
21
|
-
if (!pkgInfo) {
|
|
22
|
-
throw new Error(
|
|
23
|
-
`requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
debug("installed package info:", pkgInfo);
|
|
27
|
-
return pkgInfo;
|
|
28
|
-
}
|
|
29
|
-
function resolvePkgPath(id, debug) {
|
|
30
|
-
try {
|
|
31
|
-
const resolvedPath = _require.resolve(id);
|
|
32
|
-
const pkgPath = path.dirname(resolvedPath);
|
|
33
|
-
const pkgJson = JSON.parse(
|
|
34
|
-
fs.readFileSync(path.join(pkgPath, "package.json"), "utf-8")
|
|
35
|
-
);
|
|
36
|
-
const pkgName = pkgJson.name.startsWith("vue-i18n") ? "vue-i18n" : pkgJson.name.startsWith("petite-vue-i18n") ? "petite-vue-i18n" : "";
|
|
37
|
-
return pkgJson ? { alias: id, pkg: pkgName } : null;
|
|
38
|
-
} catch (e) {
|
|
39
|
-
debug(`cannot find '${id}'`, e);
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
function resolveNamespace(name) {
|
|
44
|
-
return `unplugin-vue-i18n:${name}`;
|
|
45
|
-
}
|
|
19
|
+
const PKG_NAME = "unplugin-vue-i18n";
|
|
20
|
+
|
|
46
21
|
function warn(...args) {
|
|
47
|
-
console.warn(pc.yellow(pc.bold(`[
|
|
22
|
+
console.warn(pc.yellow(pc.bold(`[${PKG_NAME}] `)), ...args);
|
|
48
23
|
}
|
|
49
24
|
function error(...args) {
|
|
50
|
-
console.error(pc.red(pc.bold(`[
|
|
51
|
-
}
|
|
52
|
-
async function getRaw(path2) {
|
|
53
|
-
return promises.readFile(path2, { encoding: "utf-8" });
|
|
25
|
+
console.error(pc.red(pc.bold(`[${PKG_NAME}] `)), ...args);
|
|
54
26
|
}
|
|
55
27
|
function raiseError(message) {
|
|
56
|
-
throw new Error(`[
|
|
28
|
+
throw new Error(`[${PKG_NAME}] ${message}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolveNamespace(name) {
|
|
32
|
+
return `${PKG_NAME}:${name}`;
|
|
57
33
|
}
|
|
58
34
|
function getVitePlugin(config, name) {
|
|
59
35
|
return config.plugins.find((p) => p.name === name);
|
|
@@ -76,6 +52,92 @@ function normalizePath(id) {
|
|
|
76
52
|
return path.posix.normalize(isWindows ? slash(id) : id);
|
|
77
53
|
}
|
|
78
54
|
|
|
55
|
+
const defaultFindOptions = {
|
|
56
|
+
startingFrom: ".",
|
|
57
|
+
rootPattern: /^node_modules$/,
|
|
58
|
+
reverse: false,
|
|
59
|
+
test: (filePath) => {
|
|
60
|
+
try {
|
|
61
|
+
if (statSync(filePath).isFile()) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
} catch {
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
function findFile(filename, _options = {}) {
|
|
69
|
+
const filenames = Array.isArray(filename) ? filename : [filename];
|
|
70
|
+
const options = { ...defaultFindOptions, ..._options };
|
|
71
|
+
const basePath = resolve(options.startingFrom);
|
|
72
|
+
const leadingSlash = basePath[0] === "/";
|
|
73
|
+
const segments = basePath.split("/").filter(Boolean);
|
|
74
|
+
if (leadingSlash) {
|
|
75
|
+
segments[0] = "/" + segments[0];
|
|
76
|
+
}
|
|
77
|
+
let root = segments.findIndex((r) => r.match(options.rootPattern));
|
|
78
|
+
if (root === -1) {
|
|
79
|
+
root = 0;
|
|
80
|
+
}
|
|
81
|
+
if (options.reverse) {
|
|
82
|
+
for (let index = root + 1; index <= segments.length; index++) {
|
|
83
|
+
for (const filename2 of filenames) {
|
|
84
|
+
const filePath = join(...segments.slice(0, index), filename2);
|
|
85
|
+
if (options.test(filePath)) {
|
|
86
|
+
return filePath;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
for (let index = segments.length; index > root; index--) {
|
|
92
|
+
for (const filename2 of filenames) {
|
|
93
|
+
const filePath = join(...segments.slice(0, index), filename2);
|
|
94
|
+
if (options.test(filePath)) {
|
|
95
|
+
return filePath;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
throw new Error(
|
|
101
|
+
`Cannot find matching ${filename} in ${options.startingFrom} or parent directories`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
function findNearestFile(filename, _options = {}) {
|
|
105
|
+
return findFile(filename, _options);
|
|
106
|
+
}
|
|
107
|
+
function resolvePackageJSON(id = process.cwd(), options = {}) {
|
|
108
|
+
const resolvedPath = isAbsolute(id) ? id : resolvePathSync(id, options);
|
|
109
|
+
return findNearestFile("package.json", {
|
|
110
|
+
startingFrom: resolvedPath,
|
|
111
|
+
...options
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const _require = module.createRequire(import.meta.url);
|
|
116
|
+
function checkInstallPackage(debug) {
|
|
117
|
+
const pkgInfo = resolvePkgPath("vue-i18n", debug) || resolvePkgPath("petite-vue-i18n", debug);
|
|
118
|
+
if (!pkgInfo) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
debug("installed package info:", pkgInfo);
|
|
124
|
+
return pkgInfo;
|
|
125
|
+
}
|
|
126
|
+
function resolvePkgPath(id, debug) {
|
|
127
|
+
try {
|
|
128
|
+
const modPath = _require.resolve(id);
|
|
129
|
+
debug("modPath:", modPath, id);
|
|
130
|
+
const pkgJsonPath = resolvePackageJSON(modPath);
|
|
131
|
+
debug("pkgJsonPath:", pkgJsonPath);
|
|
132
|
+
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
|
|
133
|
+
const pkgName = pkgJson.name.startsWith("vue-i18n") ? "vue-i18n" : pkgJson.name.startsWith("petite-vue-i18n") ? "petite-vue-i18n" : "";
|
|
134
|
+
return pkgJson ? { alias: id, pkg: pkgName } : null;
|
|
135
|
+
} catch (e) {
|
|
136
|
+
debug(`cannot find '${id}'`, e);
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
79
141
|
function resolveOptions(options, installedPkgInfo) {
|
|
80
142
|
let onlyLocales = [];
|
|
81
143
|
if (options.onlyLocales) {
|
|
@@ -105,6 +167,7 @@ function resolveOptions(options, installedPkgInfo) {
|
|
|
105
167
|
const escapeHtml = !!options.escapeHtml;
|
|
106
168
|
const optimizeTranslationDirective = isString(options.optimizeTranslationDirective) || isArray(options.optimizeTranslationDirective) ? options.optimizeTranslationDirective : !!options.optimizeTranslationDirective;
|
|
107
169
|
const translationIdentifiers = /* @__PURE__ */ new Map();
|
|
170
|
+
const transformI18nBlock = typeof options.transformI18nBlock === "function" ? options.transformI18nBlock : null;
|
|
108
171
|
return {
|
|
109
172
|
include,
|
|
110
173
|
exclude,
|
|
@@ -121,7 +184,8 @@ function resolveOptions(options, installedPkgInfo) {
|
|
|
121
184
|
strictMessage,
|
|
122
185
|
escapeHtml,
|
|
123
186
|
optimizeTranslationDirective,
|
|
124
|
-
translationIdentifiers
|
|
187
|
+
translationIdentifiers,
|
|
188
|
+
transformI18nBlock
|
|
125
189
|
};
|
|
126
190
|
}
|
|
127
191
|
|
|
@@ -207,7 +271,8 @@ function resourcePlugin({
|
|
|
207
271
|
ssrBuild,
|
|
208
272
|
strictMessage,
|
|
209
273
|
allowDynamic,
|
|
210
|
-
escapeHtml
|
|
274
|
+
escapeHtml,
|
|
275
|
+
transformI18nBlock
|
|
211
276
|
}, meta, installedPkgInfo) {
|
|
212
277
|
const filter = createFilter(include, exclude);
|
|
213
278
|
const getVueI18nAliasPath = ({ ssr = false, runtimeOnly: runtimeOnly2 = false }) => {
|
|
@@ -489,7 +554,7 @@ function resourcePlugin({
|
|
|
489
554
|
}
|
|
490
555
|
);
|
|
491
556
|
debug$2("parseOptions", parseOptions);
|
|
492
|
-
|
|
557
|
+
let source = await getCode(
|
|
493
558
|
code,
|
|
494
559
|
filename,
|
|
495
560
|
sourceMap,
|
|
@@ -497,6 +562,14 @@ function resourcePlugin({
|
|
|
497
562
|
getSfcParser(),
|
|
498
563
|
meta.framework
|
|
499
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
|
+
}
|
|
500
573
|
const { code: generatedCode, map } = generate(source, parseOptions);
|
|
501
574
|
debug$2("generated code", generatedCode);
|
|
502
575
|
debug$2("sourcemap", map, sourceMap);
|
|
@@ -550,7 +623,8 @@ async function generateBundleResources(resources, isProduction, {
|
|
|
550
623
|
onlyLocales = [],
|
|
551
624
|
strictMessage = true,
|
|
552
625
|
escapeHtml = false,
|
|
553
|
-
jit = true
|
|
626
|
+
jit = true,
|
|
627
|
+
transformI18nBlock = void 0
|
|
554
628
|
}) {
|
|
555
629
|
const codes = [];
|
|
556
630
|
for (const res of resources) {
|
|
@@ -565,7 +639,8 @@ async function generateBundleResources(resources, isProduction, {
|
|
|
565
639
|
onlyLocales,
|
|
566
640
|
strictMessage,
|
|
567
641
|
escapeHtml,
|
|
568
|
-
forceStringify
|
|
642
|
+
forceStringify,
|
|
643
|
+
transformI18nBlock
|
|
569
644
|
});
|
|
570
645
|
parseOptions.type = "bare";
|
|
571
646
|
const { code } = generate(source, parseOptions);
|
|
@@ -637,7 +712,8 @@ function getOptions(filename, isProduction, query, sourceMap, {
|
|
|
637
712
|
allowDynamic = false,
|
|
638
713
|
strictMessage = true,
|
|
639
714
|
escapeHtml = false,
|
|
640
|
-
jit = true
|
|
715
|
+
jit = true,
|
|
716
|
+
transformI18nBlock = null
|
|
641
717
|
}) {
|
|
642
718
|
const mode = isProduction ? "production" : "development";
|
|
643
719
|
const baseOptions = {
|
|
@@ -651,6 +727,7 @@ function getOptions(filename, isProduction, query, sourceMap, {
|
|
|
651
727
|
jit,
|
|
652
728
|
onlyLocales,
|
|
653
729
|
env: mode,
|
|
730
|
+
transformI18nBlock,
|
|
654
731
|
onWarn: (msg) => {
|
|
655
732
|
warn(`${filename} ${msg}`);
|
|
656
733
|
},
|
|
@@ -689,6 +766,9 @@ function getVirtualId(id, framework = "vite") {
|
|
|
689
766
|
function asVirtualId(id, framework = "vite") {
|
|
690
767
|
return framework === "vite" ? VIRTUAL_PREFIX + id : id;
|
|
691
768
|
}
|
|
769
|
+
async function getRaw(path) {
|
|
770
|
+
return promises.readFile(path, { encoding: "utf-8" });
|
|
771
|
+
}
|
|
692
772
|
|
|
693
773
|
const debug$1 = createDebug(resolveNamespace("directive"));
|
|
694
774
|
function directivePlugin({
|
package/lib/types.d.cts
CHANGED
package/lib/types.d.mts
CHANGED
package/lib/types.d.ts
CHANGED
package/lib/vite.cjs
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
const index = require('./index.cjs');
|
|
4
4
|
require('unplugin');
|
|
5
5
|
require('debug');
|
|
6
|
-
require('node:fs');
|
|
7
6
|
require('picocolors');
|
|
8
|
-
require('node:module');
|
|
9
7
|
require('node:path');
|
|
8
|
+
require('node:fs');
|
|
9
|
+
require('node:module');
|
|
10
10
|
require('pathe');
|
|
11
|
+
require('mlly');
|
|
11
12
|
require('@intlify/shared');
|
|
12
13
|
require('fast-glob');
|
|
13
14
|
require('@rollup/pluginutils');
|
package/lib/vite.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { unplugin } from './index.mjs';
|
|
2
2
|
import 'unplugin';
|
|
3
3
|
import 'debug';
|
|
4
|
-
import 'node:fs';
|
|
5
4
|
import 'picocolors';
|
|
6
|
-
import 'node:module';
|
|
7
5
|
import 'node:path';
|
|
6
|
+
import 'node:fs';
|
|
7
|
+
import 'node:module';
|
|
8
8
|
import 'pathe';
|
|
9
|
+
import 'mlly';
|
|
9
10
|
import '@intlify/shared';
|
|
10
11
|
import 'fast-glob';
|
|
11
12
|
import '@rollup/pluginutils';
|
package/lib/webpack.cjs
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
const index = require('./index.cjs');
|
|
4
4
|
require('unplugin');
|
|
5
5
|
require('debug');
|
|
6
|
-
require('node:fs');
|
|
7
6
|
require('picocolors');
|
|
8
|
-
require('node:module');
|
|
9
7
|
require('node:path');
|
|
8
|
+
require('node:fs');
|
|
9
|
+
require('node:module');
|
|
10
10
|
require('pathe');
|
|
11
|
+
require('mlly');
|
|
11
12
|
require('@intlify/shared');
|
|
12
13
|
require('fast-glob');
|
|
13
14
|
require('@rollup/pluginutils');
|
package/lib/webpack.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { unplugin } from './index.mjs';
|
|
2
2
|
import 'unplugin';
|
|
3
3
|
import 'debug';
|
|
4
|
-
import 'node:fs';
|
|
5
4
|
import 'picocolors';
|
|
6
|
-
import 'node:module';
|
|
7
5
|
import 'node:path';
|
|
6
|
+
import 'node:fs';
|
|
7
|
+
import 'node:module';
|
|
8
8
|
import 'pathe';
|
|
9
|
+
import 'mlly';
|
|
9
10
|
import '@intlify/shared';
|
|
10
11
|
import 'fast-glob';
|
|
11
12
|
import '@rollup/pluginutils';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlify/unplugin-vue-i18n",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.4",
|
|
4
4
|
"description": "unplugin for Vue I18n",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"fast-glob": "^3.2.12",
|
|
35
35
|
"js-yaml": "^4.1.0",
|
|
36
36
|
"json5": "^2.2.3",
|
|
37
|
+
"mlly": "^1.7.1",
|
|
37
38
|
"pathe": "^1.0.0",
|
|
38
39
|
"picocolors": "^1.0.0",
|
|
39
40
|
"source-map-js": "^1.0.2",
|