@intlify/unplugin-vue-i18n 12.0.0-alpha.0 → 12.0.0-alpha.2
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 +5 -0
- package/lib/index.cjs +83 -376
- package/lib/index.d.cts +4 -2
- package/lib/index.d.mts +1 -1
- package/lib/index.d.ts +4 -2
- package/lib/index.mjs +84 -375
- package/lib/rspack.cjs +3 -7
- package/lib/rspack.mjs +3 -7
- package/lib/types.d.cts +0 -1
- package/lib/types.d.mts +0 -1
- package/lib/types.d.ts +0 -1
- package/lib/vite.cjs +3 -7
- package/lib/vite.mjs +3 -7
- package/lib/webpack.cjs +3 -7
- package/lib/webpack.mjs +3 -7
- package/package.json +14 -18
package/README.md
CHANGED
|
@@ -661,6 +661,11 @@ If do you will use this option, you need to enable `jitCompilation` option.
|
|
|
661
661
|
|
|
662
662
|
If you want to put it manually, you can specify the signature of the translation function as a string or a string array.
|
|
663
663
|
|
|
664
|
+
> [!IMPORTANT]
|
|
665
|
+
>
|
|
666
|
+
> `optimizeTranslationDirective` option is deprecated in v12, because `v-t` custom directive will be dropped in vue-i18n v12
|
|
667
|
+
> This option will be supported with unplugin-vue-i18n v6.x (vue-i18n until v11)
|
|
668
|
+
|
|
664
669
|
> [!WARNING]
|
|
665
670
|
> 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)
|
|
666
671
|
|
package/lib/index.cjs
CHANGED
|
@@ -4,15 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
const createDebug = require('debug');
|
|
6
6
|
const unplugin$1 = require('unplugin');
|
|
7
|
-
const shared = require('@intlify/shared');
|
|
8
|
-
const vueI18nExtensions = require('@intlify/vue-i18n-extensions');
|
|
9
|
-
const scopeManager = require('@typescript-eslint/scope-manager');
|
|
10
|
-
const typescriptEstree = require('@typescript-eslint/typescript-estree');
|
|
11
|
-
const path = require('node:path');
|
|
12
|
-
const eslintUitls = require('@eslint-community/eslint-utils');
|
|
13
|
-
const pc = require('picocolors');
|
|
14
7
|
const defu = require('defu');
|
|
8
|
+
const node_path = require('node:path');
|
|
15
9
|
const bundleUtils = require('@intlify/bundle-utils');
|
|
10
|
+
const shared = require('@intlify/shared');
|
|
16
11
|
const pluginutils = require('@rollup/pluginutils');
|
|
17
12
|
const fg = require('fast-glob');
|
|
18
13
|
const knitwork = require('knitwork');
|
|
@@ -20,16 +15,60 @@ const mlly = require('mlly');
|
|
|
20
15
|
const node_crypto = require('node:crypto');
|
|
21
16
|
const fs = require('node:fs');
|
|
22
17
|
const compilerSfc = require('vue/compiler-sfc');
|
|
18
|
+
const pc = require('picocolors');
|
|
23
19
|
|
|
24
20
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
25
21
|
|
|
26
22
|
const createDebug__default = /*#__PURE__*/_interopDefaultCompat(createDebug);
|
|
27
|
-
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
28
|
-
const eslintUitls__default = /*#__PURE__*/_interopDefaultCompat(eslintUitls);
|
|
29
|
-
const pc__default = /*#__PURE__*/_interopDefaultCompat(pc);
|
|
30
23
|
const defu__default = /*#__PURE__*/_interopDefaultCompat(defu);
|
|
31
24
|
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
32
25
|
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
26
|
+
const pc__default = /*#__PURE__*/_interopDefaultCompat(pc);
|
|
27
|
+
|
|
28
|
+
function toArray(value) {
|
|
29
|
+
return Array.isArray(value) ? value : [value];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const getVueI18nAliasPath = (opts) => {
|
|
33
|
+
const filename = [
|
|
34
|
+
opts.module,
|
|
35
|
+
opts.runtimeOnly ? "runtime" : "",
|
|
36
|
+
!opts.ssr ? "esm-bundler" : "node",
|
|
37
|
+
"js"
|
|
38
|
+
].filter(Boolean).join(".");
|
|
39
|
+
return `${opts.module}/dist/${filename}`;
|
|
40
|
+
};
|
|
41
|
+
function resolveOptions(options) {
|
|
42
|
+
const res = defu__default(options, {
|
|
43
|
+
ssr: false,
|
|
44
|
+
module: "vue-i18n",
|
|
45
|
+
escapeHtml: false,
|
|
46
|
+
onlyLocales: [],
|
|
47
|
+
fullInstall: true,
|
|
48
|
+
runtimeOnly: true,
|
|
49
|
+
strictMessage: true,
|
|
50
|
+
allowDynamic: false,
|
|
51
|
+
globalSFCScope: false,
|
|
52
|
+
forceStringify: false,
|
|
53
|
+
defaultSFCLang: "json",
|
|
54
|
+
dropMessageCompiler: false,
|
|
55
|
+
transformI18nBlock: void 0
|
|
56
|
+
});
|
|
57
|
+
const include = res.include ? toArray(res.include).map((item) => node_path.normalize(item)) : void 0;
|
|
58
|
+
const _exclude = res.exclude ? toArray(res.exclude).map((item) => node_path.normalize(item)) : void 0;
|
|
59
|
+
const exclude = res.include ? _exclude : ["**/**"];
|
|
60
|
+
const onlyLocales = toArray(res.onlyLocales);
|
|
61
|
+
const fullInstall = res.module !== "vue-i18n" ? false : res.fullInstall;
|
|
62
|
+
const vueI18nAliasPath = getVueI18nAliasPath(res);
|
|
63
|
+
return {
|
|
64
|
+
...res,
|
|
65
|
+
include,
|
|
66
|
+
exclude,
|
|
67
|
+
fullInstall,
|
|
68
|
+
onlyLocales,
|
|
69
|
+
vueI18nAliasPath
|
|
70
|
+
};
|
|
71
|
+
}
|
|
33
72
|
|
|
34
73
|
const PKG_NAME = "unplugin-vue-i18n";
|
|
35
74
|
|
|
@@ -49,6 +88,23 @@ function resolveNamespace(name) {
|
|
|
49
88
|
function getVitePlugin(config, name) {
|
|
50
89
|
return config.plugins.find((p) => p.name === name);
|
|
51
90
|
}
|
|
91
|
+
function getVitePluginTransform(plugin) {
|
|
92
|
+
if (plugin.transform) {
|
|
93
|
+
return "handler" in plugin.transform ? [plugin.transform.handler, "handler"] : [plugin.transform, "transform"];
|
|
94
|
+
} else {
|
|
95
|
+
return [void 0, void 0];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function overrideVitePluginTransform(plugin, override, to) {
|
|
99
|
+
if (plugin.transform == void 0) {
|
|
100
|
+
throw new Error("plugin.transform is undefined");
|
|
101
|
+
}
|
|
102
|
+
if (to === "handler" && "handler" in plugin.transform) {
|
|
103
|
+
plugin.transform.handler = override;
|
|
104
|
+
} else {
|
|
105
|
+
plugin.transform = override;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
52
108
|
function checkVuePlugin(vuePlugin) {
|
|
53
109
|
if (vuePlugin == null || !vuePlugin.api) {
|
|
54
110
|
error(
|
|
@@ -58,29 +114,6 @@ function checkVuePlugin(vuePlugin) {
|
|
|
58
114
|
}
|
|
59
115
|
return true;
|
|
60
116
|
}
|
|
61
|
-
const isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
62
|
-
const windowsSlashRE = /\\/g;
|
|
63
|
-
function slash(p) {
|
|
64
|
-
return p.replace(windowsSlashRE, "/");
|
|
65
|
-
}
|
|
66
|
-
function normalizePath(id) {
|
|
67
|
-
return path__default.posix.normalize(isWindows ? slash(id) : id);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function createDescriptor(filename, source, { template, compiler }) {
|
|
71
|
-
const { descriptor, errors } = compiler.parse(source, {
|
|
72
|
-
filename,
|
|
73
|
-
templateParseOptions: template?.compilerOptions
|
|
74
|
-
});
|
|
75
|
-
return { descriptor, errors };
|
|
76
|
-
}
|
|
77
|
-
function getDescriptor(filename, code, options) {
|
|
78
|
-
const { descriptor, errors } = createDescriptor(filename, code, options);
|
|
79
|
-
if (errors.length) {
|
|
80
|
-
throw errors[0];
|
|
81
|
-
}
|
|
82
|
-
return descriptor;
|
|
83
|
-
}
|
|
84
117
|
|
|
85
118
|
function parseVueRequest(id) {
|
|
86
119
|
const [filename, rawQuery] = id.split(`?`, 2);
|
|
@@ -121,329 +154,6 @@ function parseVueRequest(id) {
|
|
|
121
154
|
function getVueCompiler(vuePlugin) {
|
|
122
155
|
return vuePlugin?.api?.options.compiler;
|
|
123
156
|
}
|
|
124
|
-
function getVuePluginOptions(vuePlugin) {
|
|
125
|
-
return {
|
|
126
|
-
isProduction: vuePlugin?.api?.options.isProduction,
|
|
127
|
-
root: vuePlugin?.api?.options.root,
|
|
128
|
-
template: vuePlugin?.api?.options.template,
|
|
129
|
-
compiler: vuePlugin?.api?.options.compiler
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const debug$2 = createDebug__default(resolveNamespace("directive"));
|
|
134
|
-
function directivePlugin({
|
|
135
|
-
optimizeTranslationDirective,
|
|
136
|
-
translationIdentifiers
|
|
137
|
-
}) {
|
|
138
|
-
let vuePlugin = null;
|
|
139
|
-
let vuePluginOptions = null;
|
|
140
|
-
const excludeLangs = ["pug", "jsx", "tsx"];
|
|
141
|
-
return {
|
|
142
|
-
name: resolveNamespace("directive"),
|
|
143
|
-
enforce: "pre",
|
|
144
|
-
vite: {
|
|
145
|
-
config(config) {
|
|
146
|
-
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
147
|
-
if (!checkVuePlugin(vuePlugin)) {
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
if (optimizeTranslationDirective) {
|
|
151
|
-
vuePlugin.api.options = resolveVueOptions(
|
|
152
|
-
vuePlugin,
|
|
153
|
-
optimizeTranslationDirective,
|
|
154
|
-
translationIdentifiers
|
|
155
|
-
);
|
|
156
|
-
debug$2(`vite:vue options['template']:`, vuePlugin.api.options);
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
configResolved(config) {
|
|
160
|
-
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
161
|
-
if (!checkVuePlugin(vuePlugin)) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
async transform(code, id) {
|
|
167
|
-
if (id.endsWith(".vue")) {
|
|
168
|
-
const { filename, query } = parseVueRequest(id);
|
|
169
|
-
if (!excludeLangs.includes(query.lang ?? "")) {
|
|
170
|
-
if (vuePluginOptions == null) {
|
|
171
|
-
vuePluginOptions = getVuePluginOptions(vuePlugin);
|
|
172
|
-
}
|
|
173
|
-
if (vuePluginOptions?.compiler) {
|
|
174
|
-
analyzeIdentifiers(
|
|
175
|
-
getDescriptor(filename, code, vuePluginOptions),
|
|
176
|
-
vuePluginOptions,
|
|
177
|
-
translationIdentifiers
|
|
178
|
-
);
|
|
179
|
-
return {
|
|
180
|
-
code,
|
|
181
|
-
map: { version: 3, mappings: "", sources: [] }
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
function resolveVueOptions(vuePlugin, optimizeTranslationDirective, translationIdentifiers) {
|
|
190
|
-
var _a, _b;
|
|
191
|
-
const vueOptions = vuePlugin.api.options;
|
|
192
|
-
vueOptions.template || (vueOptions.template = {});
|
|
193
|
-
(_a = vueOptions.template).compilerOptions || (_a.compilerOptions = {});
|
|
194
|
-
(_b = vueOptions.template.compilerOptions).directiveTransforms || (_b.directiveTransforms = {});
|
|
195
|
-
const translationSignatureResolver = (context, baseResolver) => {
|
|
196
|
-
const { filename } = context;
|
|
197
|
-
const vuePluginOptions = getVuePluginOptions(vuePlugin);
|
|
198
|
-
const normalizedFilename = normalizePath(path__default.relative(vuePluginOptions.root, filename));
|
|
199
|
-
const resolveIdentifier2 = translationIdentifiers.get(normalizedFilename);
|
|
200
|
-
debug$2("resolved vue-i18n Identifier", resolveIdentifier2);
|
|
201
|
-
if (resolveIdentifier2 == null) {
|
|
202
|
-
return void 0;
|
|
203
|
-
}
|
|
204
|
-
if (resolveIdentifier2.type === "identifier") {
|
|
205
|
-
return baseResolver(context, resolveIdentifier2.key);
|
|
206
|
-
} else {
|
|
207
|
-
const resolvedSignature = baseResolver(context, resolveIdentifier2.key);
|
|
208
|
-
return resolveIdentifier2?.style === "script-setup" ? `${resolvedSignature}.t` : resolvedSignature;
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
vueOptions.template.compilerOptions.directiveTransforms.t = vueI18nExtensions.transformVTDirective({
|
|
212
|
-
translationSignatures: shared.isBoolean(optimizeTranslationDirective) ? translationSignatureResolver : optimizeTranslationDirective
|
|
213
|
-
});
|
|
214
|
-
return vueOptions;
|
|
215
|
-
}
|
|
216
|
-
function analyzeIdentifiers(descriptor, { root }, translationIdentifiers) {
|
|
217
|
-
const source = descriptor.scriptSetup?.content || descriptor.script?.content;
|
|
218
|
-
debug$2("getDescriptor content", source);
|
|
219
|
-
if (!source) {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
const ast = typescriptEstree.parse(source, { range: true });
|
|
223
|
-
typescriptEstree.simpleTraverse(ast, {
|
|
224
|
-
enter(node, parent) {
|
|
225
|
-
if (parent) {
|
|
226
|
-
node.parent = parent;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
const scopeManager$1 = scopeManager.analyze(ast, { sourceType: "module" });
|
|
231
|
-
const scope = getScope(scopeManager$1, ast);
|
|
232
|
-
const importLocalName = getImportLocalName(scope, "vue-i18n", "useI18n");
|
|
233
|
-
if (importLocalName == null) {
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
debug$2("importLocalName", importLocalName);
|
|
237
|
-
const resolvedIdentifier = getVueI18nIdentifier(scope, importLocalName);
|
|
238
|
-
if (resolvedIdentifier) {
|
|
239
|
-
const normalizedFilename = normalizePath(path__default.relative(root, descriptor.filename));
|
|
240
|
-
debug$2("set vue-i18n resolved identifier: ", resolvedIdentifier);
|
|
241
|
-
translationIdentifiers.set(normalizedFilename, resolvedIdentifier);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
function getScope(manager, node) {
|
|
245
|
-
const scope = manager.acquire(node, true);
|
|
246
|
-
if (scope) {
|
|
247
|
-
if (scope.type === "function-expression-name") {
|
|
248
|
-
return scope.childScopes[0];
|
|
249
|
-
}
|
|
250
|
-
return scope;
|
|
251
|
-
}
|
|
252
|
-
return manager.scopes[0];
|
|
253
|
-
}
|
|
254
|
-
function getImportLocalName(scope, source, imported) {
|
|
255
|
-
const importDecl = getImportDeclaration(scope, source);
|
|
256
|
-
if (importDecl) {
|
|
257
|
-
const specifierNode = importDecl.specifiers.find(
|
|
258
|
-
(specifierNode2) => isImportedIdentifierInImportClause(specifierNode2) && specifierNode2.imported.name === imported
|
|
259
|
-
);
|
|
260
|
-
return specifierNode ? specifierNode.local.name : null;
|
|
261
|
-
}
|
|
262
|
-
return null;
|
|
263
|
-
}
|
|
264
|
-
function getImportDeclaration(scope, source) {
|
|
265
|
-
const tracker = new eslintUitls__default.ReferenceTracker(scope);
|
|
266
|
-
const traceMap = {
|
|
267
|
-
[source]: {
|
|
268
|
-
[eslintUitls__default.ReferenceTracker.ESM]: true,
|
|
269
|
-
[eslintUitls__default.ReferenceTracker.READ]: true
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
const refs = Array.from(tracker.iterateEsmReferences(traceMap));
|
|
273
|
-
return refs.length ? refs[0].node : null;
|
|
274
|
-
}
|
|
275
|
-
function isImportedIdentifierInImportClause(node) {
|
|
276
|
-
return "imported" in node;
|
|
277
|
-
}
|
|
278
|
-
function getVueI18nIdentifier(scope, local) {
|
|
279
|
-
const { callExpression, returnStatement } = getCallExpressionAndReturnStatement(scope, local);
|
|
280
|
-
if (callExpression == null) {
|
|
281
|
-
return null;
|
|
282
|
-
}
|
|
283
|
-
const id = getVariableDeclarationIdFrom(callExpression);
|
|
284
|
-
if (id == null) {
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
287
|
-
const variableIdPairs = parseVariableId(id);
|
|
288
|
-
debug$2("variableIdPairs:", variableIdPairs);
|
|
289
|
-
const returnVariableIdPairs = parseReturnStatement(returnStatement);
|
|
290
|
-
debug$2("returnVariableIdPairs:", returnVariableIdPairs);
|
|
291
|
-
return resolveIdentifier(variableIdPairs, returnVariableIdPairs);
|
|
292
|
-
}
|
|
293
|
-
const EMPTY_NODE_RETURN = {
|
|
294
|
-
callExpression: null,
|
|
295
|
-
returnStatement: null
|
|
296
|
-
};
|
|
297
|
-
function getCallExpressionAndReturnStatement(scope, local) {
|
|
298
|
-
const variable = eslintUitls__default.findVariable(scope, local);
|
|
299
|
-
if (variable == null) {
|
|
300
|
-
return EMPTY_NODE_RETURN;
|
|
301
|
-
}
|
|
302
|
-
const callExpressionRef = variable.references.find((ref) => {
|
|
303
|
-
return ref.identifier.parent?.type === typescriptEstree.AST_NODE_TYPES.CallExpression;
|
|
304
|
-
});
|
|
305
|
-
if (callExpressionRef == null) {
|
|
306
|
-
return EMPTY_NODE_RETURN;
|
|
307
|
-
}
|
|
308
|
-
let returnStatement = null;
|
|
309
|
-
if (callExpressionRef.from.type === "function" && callExpressionRef.from.block.type === typescriptEstree.AST_NODE_TYPES.FunctionExpression && callExpressionRef.from.block.parent.type === typescriptEstree.AST_NODE_TYPES.Property && callExpressionRef.from.block.parent.key.type === typescriptEstree.AST_NODE_TYPES.Identifier && callExpressionRef.from.block.parent.key.name === "setup") {
|
|
310
|
-
returnStatement = callExpressionRef.from.block.body.body.find((statement) => {
|
|
311
|
-
return statement.type === typescriptEstree.AST_NODE_TYPES.ReturnStatement;
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
return {
|
|
315
|
-
callExpression: callExpressionRef.identifier.parent,
|
|
316
|
-
returnStatement
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
function getVariableDeclarationIdFrom(node) {
|
|
320
|
-
if (node.parent?.type !== typescriptEstree.AST_NODE_TYPES.VariableDeclarator) {
|
|
321
|
-
return null;
|
|
322
|
-
}
|
|
323
|
-
return node.parent.id;
|
|
324
|
-
}
|
|
325
|
-
function parseVariableId(node) {
|
|
326
|
-
if (node.type === typescriptEstree.AST_NODE_TYPES.Identifier) {
|
|
327
|
-
return [{ key: node.name, value: null }];
|
|
328
|
-
} else {
|
|
329
|
-
const props = node.properties.filter(
|
|
330
|
-
// ignore RestElement
|
|
331
|
-
(prop) => prop.type === typescriptEstree.AST_NODE_TYPES.Property
|
|
332
|
-
);
|
|
333
|
-
const pairs = [];
|
|
334
|
-
for (const prop of props) {
|
|
335
|
-
if (prop?.key.type === typescriptEstree.AST_NODE_TYPES.Identifier && prop?.value.type === typescriptEstree.AST_NODE_TYPES.Identifier) {
|
|
336
|
-
pairs.push({ key: prop.key.name, value: prop.value.name });
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return pairs;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
function parseReturnStatement(node) {
|
|
343
|
-
const pairs = [];
|
|
344
|
-
if (node == null || node.argument == null) {
|
|
345
|
-
return pairs;
|
|
346
|
-
}
|
|
347
|
-
if (node.argument.type === typescriptEstree.AST_NODE_TYPES.ObjectExpression) {
|
|
348
|
-
for (const prop of node.argument.properties) {
|
|
349
|
-
if (prop.type === typescriptEstree.AST_NODE_TYPES.Property) {
|
|
350
|
-
if (prop.key.type === typescriptEstree.AST_NODE_TYPES.Identifier && prop.value.type === typescriptEstree.AST_NODE_TYPES.Identifier) {
|
|
351
|
-
pairs.push({ key: prop.key.name, value: prop.value.name });
|
|
352
|
-
} else if (prop.key.type === typescriptEstree.AST_NODE_TYPES.Identifier && prop.value.type === typescriptEstree.AST_NODE_TYPES.MemberExpression && prop.value.object.type === typescriptEstree.AST_NODE_TYPES.Identifier && prop.value.property.type === typescriptEstree.AST_NODE_TYPES.Identifier) {
|
|
353
|
-
pairs.push({
|
|
354
|
-
key: prop.key.name,
|
|
355
|
-
value: `${prop.value.object.name}.${prop.value.property.name}`
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
return pairs;
|
|
361
|
-
} else if (node.argument.type === typescriptEstree.AST_NODE_TYPES.Identifier) {
|
|
362
|
-
return pairs;
|
|
363
|
-
} else {
|
|
364
|
-
return pairs;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
function resolveIdentifier(localVariables, returnVariable) {
|
|
368
|
-
if (returnVariable.length === 0) {
|
|
369
|
-
const variable = localVariables.find((pair) => pair.key === "t");
|
|
370
|
-
if (variable && variable.value) {
|
|
371
|
-
return { type: "identifier", key: variable.value };
|
|
372
|
-
}
|
|
373
|
-
const identifierOnly = localVariables.find((pair) => pair.value === null);
|
|
374
|
-
if (identifierOnly && identifierOnly.key) {
|
|
375
|
-
return { type: "object", key: identifierOnly.key, style: "script-setup" };
|
|
376
|
-
}
|
|
377
|
-
return null;
|
|
378
|
-
} else {
|
|
379
|
-
const variable = localVariables.find((pair) => pair.key === "t");
|
|
380
|
-
if (variable && variable.value) {
|
|
381
|
-
const returnVar = returnVariable.find((pair) => pair.value === variable.value);
|
|
382
|
-
if (returnVar && returnVar.key) {
|
|
383
|
-
return { type: "identifier", key: returnVar.key };
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
const identifierOnly = localVariables.find((pair) => pair.value === null);
|
|
387
|
-
if (identifierOnly && identifierOnly.key) {
|
|
388
|
-
const targetKey = identifierOnly.key;
|
|
389
|
-
const returnVar = returnVariable.find((pair) => pair.value?.startsWith(targetKey));
|
|
390
|
-
if (returnVar && returnVar.key) {
|
|
391
|
-
return { type: "object", key: returnVar.key, style: "setup-hook" };
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
return null;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
function toArray(value) {
|
|
399
|
-
return Array.isArray(value) ? value : [value];
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
const getVueI18nAliasPath = (opts) => {
|
|
403
|
-
const filename = [
|
|
404
|
-
opts.module,
|
|
405
|
-
opts.runtimeOnly ? "runtime" : "",
|
|
406
|
-
!opts.ssr ? "esm-bundler" : "node",
|
|
407
|
-
"js"
|
|
408
|
-
].filter(Boolean).join(".");
|
|
409
|
-
return `${opts.module}/dist/${filename}`;
|
|
410
|
-
};
|
|
411
|
-
function resolveOptions(options) {
|
|
412
|
-
const res = defu__default(options, {
|
|
413
|
-
ssr: false,
|
|
414
|
-
module: "vue-i18n",
|
|
415
|
-
escapeHtml: false,
|
|
416
|
-
onlyLocales: [],
|
|
417
|
-
fullInstall: true,
|
|
418
|
-
runtimeOnly: true,
|
|
419
|
-
strictMessage: true,
|
|
420
|
-
allowDynamic: false,
|
|
421
|
-
globalSFCScope: false,
|
|
422
|
-
forceStringify: false,
|
|
423
|
-
defaultSFCLang: "json",
|
|
424
|
-
dropMessageCompiler: false,
|
|
425
|
-
transformI18nBlock: void 0,
|
|
426
|
-
optimizeTranslationDirective: false
|
|
427
|
-
});
|
|
428
|
-
const include = res.include ? toArray(res.include).map((item) => path.normalize(item)) : void 0;
|
|
429
|
-
const _exclude = res.exclude ? toArray(res.exclude).map((item) => path.normalize(item)) : void 0;
|
|
430
|
-
const exclude = res.include ? _exclude : ["**/**"];
|
|
431
|
-
const onlyLocales = toArray(res.onlyLocales);
|
|
432
|
-
const fullInstall = res.module !== "vue-i18n" ? false : res.fullInstall;
|
|
433
|
-
const optimizeTranslationDirective = typeof res.optimizeTranslationDirective === "boolean" ? res.optimizeTranslationDirective : toArray(res.optimizeTranslationDirective);
|
|
434
|
-
const translationIdentifiers = /* @__PURE__ */ new Map();
|
|
435
|
-
const vueI18nAliasPath = getVueI18nAliasPath(res);
|
|
436
|
-
return {
|
|
437
|
-
...res,
|
|
438
|
-
include,
|
|
439
|
-
exclude,
|
|
440
|
-
fullInstall,
|
|
441
|
-
onlyLocales,
|
|
442
|
-
vueI18nAliasPath,
|
|
443
|
-
translationIdentifiers,
|
|
444
|
-
optimizeTranslationDirective
|
|
445
|
-
};
|
|
446
|
-
}
|
|
447
157
|
|
|
448
158
|
const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
|
|
449
159
|
const VIRTUAL_PREFIX = "\0";
|
|
@@ -546,8 +256,11 @@ function resourcePlugin(opts, meta) {
|
|
|
546
256
|
debug$1(`configResolved: isProduction = ${ctx.prod}, sourceMap = ${ctx.sourceMap}`);
|
|
547
257
|
const jsonPlugin = getVitePlugin(config, "vite:json");
|
|
548
258
|
if (jsonPlugin) {
|
|
549
|
-
const orgTransform = jsonPlugin
|
|
550
|
-
|
|
259
|
+
const [orgTransform, transformWay] = getVitePluginTransform(jsonPlugin);
|
|
260
|
+
if (!orgTransform) {
|
|
261
|
+
throw new Error("vite:json plugin not found!");
|
|
262
|
+
}
|
|
263
|
+
async function overrideViteJsonPlugin(code, id) {
|
|
551
264
|
if (!/\.json$/.test(id) || filter(id)) {
|
|
552
265
|
return;
|
|
553
266
|
}
|
|
@@ -557,7 +270,8 @@ function resourcePlugin(opts, meta) {
|
|
|
557
270
|
}
|
|
558
271
|
debug$1("org json plugin");
|
|
559
272
|
return orgTransform.apply(this, [code, id]);
|
|
560
|
-
}
|
|
273
|
+
}
|
|
274
|
+
overrideVitePluginTransform(jsonPlugin, overrideViteJsonPlugin, transformWay);
|
|
561
275
|
}
|
|
562
276
|
}
|
|
563
277
|
},
|
|
@@ -602,12 +316,12 @@ function resourcePlugin(opts, meta) {
|
|
|
602
316
|
if (filter(id)) {
|
|
603
317
|
const imports = mlly.findStaticImports(code);
|
|
604
318
|
for (const p of imports) {
|
|
605
|
-
const res =
|
|
319
|
+
const res = node_path.resolve(node_path.dirname(id), p.specifier);
|
|
606
320
|
importedResources.add(res);
|
|
607
321
|
}
|
|
608
322
|
}
|
|
609
323
|
if (!query.vue && supportedFileExtensionsRE.test(id) && filter(id)) {
|
|
610
|
-
langInfo =
|
|
324
|
+
langInfo = node_path.parse(filename).ext;
|
|
611
325
|
const generate = getGenerator(langInfo);
|
|
612
326
|
const parseOptions = getOptions(filename, ctx, query, {
|
|
613
327
|
...opts,
|
|
@@ -615,11 +329,10 @@ function resourcePlugin(opts, meta) {
|
|
|
615
329
|
});
|
|
616
330
|
debug$1("parseOptions", parseOptions);
|
|
617
331
|
try {
|
|
618
|
-
const { code: generatedCode, map } = generate(code, parseOptions);
|
|
332
|
+
const { code: generatedCode, map } = await generate(code, parseOptions);
|
|
619
333
|
debug$1("generated code", generatedCode);
|
|
620
334
|
debug$1("sourcemap", map, ctx.sourceMap);
|
|
621
|
-
if (code === generatedCode)
|
|
622
|
-
return;
|
|
335
|
+
if (code === generatedCode) return;
|
|
623
336
|
return result(generatedCode);
|
|
624
337
|
} catch (err) {
|
|
625
338
|
if (err instanceof bundleUtils.DynamicResourceError) {
|
|
@@ -656,7 +369,7 @@ function resourcePlugin(opts, meta) {
|
|
|
656
369
|
}
|
|
657
370
|
}
|
|
658
371
|
const generate = getGenerator(langInfo, bundleUtils.generateYAML);
|
|
659
|
-
const { code: generatedCode, map } = generate(source, parseOptions);
|
|
372
|
+
const { code: generatedCode, map } = await generate(source, parseOptions);
|
|
660
373
|
debug$1("generated code", generatedCode);
|
|
661
374
|
debug$1("sourcemap", map, ctx.sourceMap);
|
|
662
375
|
if (code !== generatedCode) {
|
|
@@ -673,9 +386,12 @@ function getGenerator(ext, fallback = bundleUtils.generateJSON) {
|
|
|
673
386
|
if (/\.ya?ml$/.test(ext)) {
|
|
674
387
|
return bundleUtils.generateYAML;
|
|
675
388
|
}
|
|
676
|
-
if (/\.[c|m]?
|
|
389
|
+
if (/\.[c|m]?js$/.test(ext)) {
|
|
677
390
|
return bundleUtils.generateJavaScript;
|
|
678
391
|
}
|
|
392
|
+
if (/\.[c|m]?ts$/.test(ext)) {
|
|
393
|
+
return bundleUtils.generateTypescript;
|
|
394
|
+
}
|
|
679
395
|
return fallback;
|
|
680
396
|
}
|
|
681
397
|
async function loadWebpack() {
|
|
@@ -691,7 +407,7 @@ function generateBundleResources(resources) {
|
|
|
691
407
|
const imports = [];
|
|
692
408
|
for (const filename of resources) {
|
|
693
409
|
debug$1(`${filename} bundle loading ...`);
|
|
694
|
-
const { name } =
|
|
410
|
+
const { name } = node_path.parse(filename);
|
|
695
411
|
const varName = knitwork.genSafeVariableName(
|
|
696
412
|
[name, node_crypto.createHash("sha256").update(filename).digest("hex").substring(0, 8)].join("_")
|
|
697
413
|
);
|
|
@@ -808,15 +524,6 @@ const unpluginFactory = (options = {}, meta) => {
|
|
|
808
524
|
const resolvedOptions = resolveOptions(options);
|
|
809
525
|
debug("plugin options (resolved):", resolvedOptions);
|
|
810
526
|
const plugins = [resourcePlugin(resolvedOptions, meta)];
|
|
811
|
-
if (resolvedOptions.optimizeTranslationDirective) {
|
|
812
|
-
if (meta.framework === "webpack" || meta.framework === "rspack") {
|
|
813
|
-
raiseError(
|
|
814
|
-
`The 'optimizeTranslationDirective' option still is not supported for ${meta.framework}.
|
|
815
|
-
We are waiting for your Pull Request \u{1F642}.`
|
|
816
|
-
);
|
|
817
|
-
}
|
|
818
|
-
plugins.push(directivePlugin(resolvedOptions));
|
|
819
|
-
}
|
|
820
527
|
return plugins;
|
|
821
528
|
};
|
|
822
529
|
const unplugin = /* @__PURE__ */ unplugin$1.createUnplugin(unpluginFactory);
|
package/lib/index.d.cts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnpluginInstance, UnpluginFactory } from 'unplugin';
|
|
2
2
|
import { PluginOptions } from './types.cjs';
|
|
3
3
|
export { SFCLangFormat, VueI18nModule } from './types.cjs';
|
|
4
4
|
|
|
5
5
|
declare const unpluginFactory: UnpluginFactory<PluginOptions | undefined>;
|
|
6
6
|
declare const unplugin: UnpluginInstance<PluginOptions | undefined, boolean>;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
export = unplugin;
|
|
10
|
+
export { PluginOptions, unplugin, unpluginFactory };
|
package/lib/index.d.mts
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnpluginInstance, UnpluginFactory } from 'unplugin';
|
|
2
2
|
import { PluginOptions } from './types.js';
|
|
3
3
|
export { SFCLangFormat, VueI18nModule } from './types.js';
|
|
4
4
|
|
|
5
5
|
declare const unpluginFactory: UnpluginFactory<PluginOptions | undefined>;
|
|
6
6
|
declare const unplugin: UnpluginInstance<PluginOptions | undefined, boolean>;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
export = unplugin;
|
|
10
|
+
export { PluginOptions, unplugin, unpluginFactory };
|