valaxy 1.1.0 → 1.2.1
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/client/components/ValaxyCopyright.vue +2 -1
- package/client/composables/collections.ts +13 -9
- package/client/composables/decrypt.ts +8 -1
- package/client/composables/outline/anchor.ts +20 -8
- package/client/index.d.ts +0 -1
- package/client/layouts/README.md +1 -1
- package/client/main.ts +35 -20
- package/client/modules/valaxy.ts +14 -5
- package/client/shims.d.ts +20 -0
- package/client/utils/layouts.ts +359 -0
- package/client/utils/path.ts +30 -0
- package/dist/node/cli/index.mjs +4 -11
- package/dist/node/index.d.mts +68 -45
- package/dist/node/index.mjs +4 -11
- package/dist/shared/{valaxy.BU27Owzs.mjs → valaxy.DvkRJomf.mjs} +627 -1886
- package/dist/shared/{valaxy.I4MpBkZO.d.mts → valaxy.EzGcrVef.d.mts} +90 -1
- package/dist/types/index.d.mts +2 -2
- package/package.json +11 -18
- package/types/config.ts +83 -0
- package/types/vue-router.d.ts +8 -0
- package/vendor/README.md +8 -8
- package/vendor/sources.json +0 -31
- package/vendor/i18n/LICENSE +0 -20
- package/vendor/i18n/index.d.mts +0 -7
- package/vendor/i18n/index.mjs +0 -1389
- package/vendor/i18n/types.d.ts +0 -48
- package/vendor/layouts/LICENSE +0 -21
- package/vendor/layouts/client.d.ts +0 -11
- package/vendor/layouts/index.d.mts +0 -86
- package/vendor/layouts/index.mjs +0 -235
- package/vendor/layouts/layoutName-DVkP7Qpo.mjs +0 -82
- package/vendor/layouts/runtime.d.mts +0 -72
- package/vendor/layouts/runtime.mjs +0 -284
package/vendor/i18n/index.mjs
DELETED
|
@@ -1,1389 +0,0 @@
|
|
|
1
|
-
import createDebug from 'debug';
|
|
2
|
-
import { createUnplugin } from 'unplugin';
|
|
3
|
-
import { isArray, isString, isBoolean, assign, isEmptyObject, isNumber, generateCodeFrame } from '@intlify/shared';
|
|
4
|
-
import { normalize, parse, resolve, dirname } from 'pathe';
|
|
5
|
-
import { generateJSON, generateYAML, generateJavaScript, generateTypescript } from '@intlify/bundle-utils';
|
|
6
|
-
import { createFilter } from '@rollup/pluginutils';
|
|
7
|
-
import { glob as fg } from 'tinyglobby';
|
|
8
|
-
import { promises } from 'node:fs';
|
|
9
|
-
import { parse as parse$1 } from 'vue/compiler-sfc';
|
|
10
|
-
import pc from 'picocolors';
|
|
11
|
-
import path from 'node:path';
|
|
12
|
-
import eslintUitls from '@eslint-community/eslint-utils';
|
|
13
|
-
import { transformVTDirective } from '@intlify/vue-i18n-extensions';
|
|
14
|
-
import { analyze } from '@typescript-eslint/scope-manager';
|
|
15
|
-
|
|
16
|
-
function normalizeGlobOption(val) {
|
|
17
|
-
if (!val) return void 0;
|
|
18
|
-
return isString(val) ? [normalize(val)] : val.map(normalize);
|
|
19
|
-
}
|
|
20
|
-
function resolveOptions(options) {
|
|
21
|
-
const moduleType = options.module || "vue-i18n";
|
|
22
|
-
let onlyLocales = [];
|
|
23
|
-
if (options.onlyLocales) {
|
|
24
|
-
onlyLocales = isArray(options.onlyLocales) ? options.onlyLocales : [options.onlyLocales];
|
|
25
|
-
}
|
|
26
|
-
const forceStringify = !!options.forceStringify;
|
|
27
|
-
const defaultSFCLang = isString(options.defaultSFCLang) ? options.defaultSFCLang : "json";
|
|
28
|
-
const globalSFCScope = !!options.globalSFCScope;
|
|
29
|
-
const runtimeOnly = isBoolean(options.runtimeOnly) ? options.runtimeOnly : true;
|
|
30
|
-
const dropMessageCompiler = !!options.dropMessageCompiler;
|
|
31
|
-
const compositionOnly = moduleType === "vue-i18n" ? isBoolean(options.compositionOnly) ? options.compositionOnly : true : true;
|
|
32
|
-
const fullInstall = moduleType === "vue-i18n" ? isBoolean(options.fullInstall) ? options.fullInstall : true : false;
|
|
33
|
-
const ssrBuild = !!options.ssr;
|
|
34
|
-
const allowDynamic = !!options.allowDynamic;
|
|
35
|
-
const strictMessage = isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
36
|
-
const escapeHtml = !!options.escapeHtml;
|
|
37
|
-
const optimizeTranslationDirective = isString(options.optimizeTranslationDirective) || isArray(options.optimizeTranslationDirective) ? options.optimizeTranslationDirective : !!options.optimizeTranslationDirective;
|
|
38
|
-
const translationIdentifiers = /* @__PURE__ */ new Map();
|
|
39
|
-
const transformI18nBlock = typeof options.transformI18nBlock === "function" ? options.transformI18nBlock : null;
|
|
40
|
-
const treeShaking = options.treeShaking ? typeof options.treeShaking === "object" ? {
|
|
41
|
-
safelist: options.treeShaking.safelist || [],
|
|
42
|
-
dynamicKeyStrategy: options.treeShaking.dynamicKeyStrategy || "keep-all",
|
|
43
|
-
scanPatterns: options.treeShaking.scanPatterns
|
|
44
|
-
} : { safelist: [], dynamicKeyStrategy: "keep-all" } : null;
|
|
45
|
-
return {
|
|
46
|
-
include: normalizeGlobOption(options.include || []),
|
|
47
|
-
exclude: normalizeGlobOption(options.exclude),
|
|
48
|
-
module: moduleType,
|
|
49
|
-
onlyLocales,
|
|
50
|
-
forceStringify,
|
|
51
|
-
defaultSFCLang,
|
|
52
|
-
globalSFCScope,
|
|
53
|
-
runtimeOnly,
|
|
54
|
-
dropMessageCompiler,
|
|
55
|
-
compositionOnly,
|
|
56
|
-
fullInstall,
|
|
57
|
-
ssrBuild,
|
|
58
|
-
allowDynamic,
|
|
59
|
-
strictMessage,
|
|
60
|
-
escapeHtml,
|
|
61
|
-
optimizeTranslationDirective,
|
|
62
|
-
translationIdentifiers,
|
|
63
|
-
transformI18nBlock,
|
|
64
|
-
treeShaking
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const PKG_NAME = "unplugin-vue-i18n";
|
|
69
|
-
|
|
70
|
-
function warn(...args) {
|
|
71
|
-
console.warn(pc.yellow(pc.bold(`[${PKG_NAME}] `)), ...args);
|
|
72
|
-
}
|
|
73
|
-
function error(...args) {
|
|
74
|
-
console.error(pc.red(pc.bold(`[${PKG_NAME}] `)), ...args);
|
|
75
|
-
}
|
|
76
|
-
function raiseError(message) {
|
|
77
|
-
throw new Error(`[${PKG_NAME}] ${message}`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function resolveNamespace(name) {
|
|
81
|
-
return `${PKG_NAME}:${name}`;
|
|
82
|
-
}
|
|
83
|
-
function getVitePlugin(config, name) {
|
|
84
|
-
return config.plugins.find((p) => p.name === name);
|
|
85
|
-
}
|
|
86
|
-
function checkVuePlugin(vuePlugin) {
|
|
87
|
-
if (vuePlugin == null || !vuePlugin.api) {
|
|
88
|
-
error(
|
|
89
|
-
"`@vitejs/plugin-vue` plugin is not found or invalid version. Please install `@vitejs/plugin-vue` v4.3.4 or later version."
|
|
90
|
-
);
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
const isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
96
|
-
const windowsSlashRE = /\\/g;
|
|
97
|
-
function slash(p) {
|
|
98
|
-
return p.replace(windowsSlashRE, "/");
|
|
99
|
-
}
|
|
100
|
-
function normalizePath(id) {
|
|
101
|
-
return path.posix.normalize(isWindows ? slash(id) : id);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function parseVueRequest(id) {
|
|
105
|
-
const [filename, rawQuery] = id.split(`?`, 2);
|
|
106
|
-
const params = new URLSearchParams(rawQuery);
|
|
107
|
-
const ret = {};
|
|
108
|
-
const langPart = Object.keys(Object.fromEntries(params)).find((key) => /lang\./i.test(key));
|
|
109
|
-
ret.vue = params.has("vue");
|
|
110
|
-
ret.global = params.has("global");
|
|
111
|
-
ret.src = params.has("src");
|
|
112
|
-
ret.raw = params.has("raw");
|
|
113
|
-
if (params.has("type")) {
|
|
114
|
-
ret.type = params.get("type");
|
|
115
|
-
}
|
|
116
|
-
if (params.has("blockType")) {
|
|
117
|
-
ret.blockType = params.get("blockType");
|
|
118
|
-
}
|
|
119
|
-
if (params.has("index")) {
|
|
120
|
-
ret.index = Number(params.get("index"));
|
|
121
|
-
}
|
|
122
|
-
if (params.has("locale")) {
|
|
123
|
-
ret.locale = params.get("locale");
|
|
124
|
-
}
|
|
125
|
-
if (langPart) {
|
|
126
|
-
const [, lang] = langPart.split(".");
|
|
127
|
-
ret.lang = lang;
|
|
128
|
-
} else if (params.has("lang")) {
|
|
129
|
-
ret.lang = params.get("lang");
|
|
130
|
-
}
|
|
131
|
-
if (params.has("issuerPath")) {
|
|
132
|
-
ret.issuerPath = params.get("issuerPath");
|
|
133
|
-
}
|
|
134
|
-
return {
|
|
135
|
-
filename,
|
|
136
|
-
query: ret
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function getVueCompiler(vuePlugin) {
|
|
141
|
-
return vuePlugin?.api?.options.compiler;
|
|
142
|
-
}
|
|
143
|
-
function getVuePluginOptions(vuePlugin) {
|
|
144
|
-
return {
|
|
145
|
-
isProduction: vuePlugin?.api?.options.isProduction,
|
|
146
|
-
root: vuePlugin?.api?.options.root,
|
|
147
|
-
template: vuePlugin?.api?.options.template,
|
|
148
|
-
compiler: vuePlugin?.api?.options.compiler
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function createDescriptor(filename, source, { template, compiler }) {
|
|
153
|
-
const { descriptor, errors } = compiler.parse(source, {
|
|
154
|
-
filename,
|
|
155
|
-
templateParseOptions: template?.compilerOptions
|
|
156
|
-
});
|
|
157
|
-
return { descriptor, errors };
|
|
158
|
-
}
|
|
159
|
-
function getDescriptor(filename, code, options) {
|
|
160
|
-
const { descriptor, errors } = createDescriptor(filename, code, options);
|
|
161
|
-
if (errors.length) {
|
|
162
|
-
throw errors[0];
|
|
163
|
-
}
|
|
164
|
-
return descriptor;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
|
|
168
|
-
const VIRTUAL_PREFIX = "virtual:";
|
|
169
|
-
const RE_RESOURCE_FORMAT = /\.(json5?|ya?ml|[c|m]?[j|t]s)$/;
|
|
170
|
-
const RE_SFC_I18N_CUSTOM_BLOCK = /\?vue&type=i18n/;
|
|
171
|
-
const RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK = /blockType=i18n/;
|
|
172
|
-
const VITE_VIRTUAL_PREFIX = "virtual:intlify-i18n-";
|
|
173
|
-
const debug$3 = createDebug(resolveNamespace("resource"));
|
|
174
|
-
function resourcePlugin({
|
|
175
|
-
onlyLocales,
|
|
176
|
-
include,
|
|
177
|
-
exclude,
|
|
178
|
-
module,
|
|
179
|
-
forceStringify,
|
|
180
|
-
defaultSFCLang,
|
|
181
|
-
globalSFCScope,
|
|
182
|
-
runtimeOnly,
|
|
183
|
-
dropMessageCompiler,
|
|
184
|
-
compositionOnly,
|
|
185
|
-
fullInstall,
|
|
186
|
-
ssrBuild,
|
|
187
|
-
strictMessage,
|
|
188
|
-
allowDynamic,
|
|
189
|
-
escapeHtml,
|
|
190
|
-
transformI18nBlock
|
|
191
|
-
}, meta, collector) {
|
|
192
|
-
function resolveIncludeExclude() {
|
|
193
|
-
const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
|
|
194
|
-
return include ? [[...include, customBlockInclude], exclude] : [[RE_RESOURCE_FORMAT, customBlockInclude], exclude];
|
|
195
|
-
}
|
|
196
|
-
function resolveIncludeExcludeForLegacy() {
|
|
197
|
-
const customBlockInclude = meta.framework === "vite" ? RE_SFC_I18N_CUSTOM_BLOCK : RE_SFC_I18N_WEBPACK_CUSTOM_BLOCK;
|
|
198
|
-
return include ? [[...include, customBlockInclude], void 0] : [void 0, ["**/**"]];
|
|
199
|
-
}
|
|
200
|
-
let _filter = null;
|
|
201
|
-
async function getFilter() {
|
|
202
|
-
if (_filter != null) {
|
|
203
|
-
return _filter;
|
|
204
|
-
}
|
|
205
|
-
if (meta.framework == "webpack") {
|
|
206
|
-
debug$3("Using filter for webpack");
|
|
207
|
-
_filter = createFilter(...resolveIncludeExclude());
|
|
208
|
-
} else if (hasViteJsonPlugin) {
|
|
209
|
-
debug$3("Using filter for rollup-vite");
|
|
210
|
-
_filter = createFilter(...resolveIncludeExcludeForLegacy());
|
|
211
|
-
} else {
|
|
212
|
-
debug$3("Using filter for rolldown-vite");
|
|
213
|
-
_filter = createFilter(...resolveIncludeExclude());
|
|
214
|
-
}
|
|
215
|
-
return _filter;
|
|
216
|
-
}
|
|
217
|
-
const getVueI18nAliasPath = ({ ssr = false, runtimeOnly: runtimeOnly2 = false }) => {
|
|
218
|
-
return `${module}/dist/${module}${runtimeOnly2 ? ".runtime" : ""}.${!ssr ? "esm-bundler.js" : "node.mjs"}`;
|
|
219
|
-
};
|
|
220
|
-
let isProduction = false;
|
|
221
|
-
let sourceMap = false;
|
|
222
|
-
let hasViteJsonPlugin = false;
|
|
223
|
-
const vueI18nAliasName = module;
|
|
224
|
-
debug$3(`vue-i18n alias name: ${vueI18nAliasName}`);
|
|
225
|
-
let vuePlugin = null;
|
|
226
|
-
const getSfcParser = () => {
|
|
227
|
-
return vuePlugin ? getVueCompiler(vuePlugin).parse : parse$1;
|
|
228
|
-
};
|
|
229
|
-
const virtualIdToRealPath = /* @__PURE__ */ new Map();
|
|
230
|
-
const realPathToVirtualId = /* @__PURE__ */ new Map();
|
|
231
|
-
let virtualCounter = 0;
|
|
232
|
-
function intlifyVirtualize(realPath) {
|
|
233
|
-
const normalized = normalize(realPath);
|
|
234
|
-
let virtualId = realPathToVirtualId.get(normalized);
|
|
235
|
-
if (!virtualId) {
|
|
236
|
-
virtualId = `${VITE_VIRTUAL_PREFIX}${virtualCounter++}`;
|
|
237
|
-
virtualIdToRealPath.set(virtualId, normalized);
|
|
238
|
-
realPathToVirtualId.set(normalized, virtualId);
|
|
239
|
-
}
|
|
240
|
-
return virtualId;
|
|
241
|
-
}
|
|
242
|
-
function isIntlifyVirtualId(id) {
|
|
243
|
-
return virtualIdToRealPath.has(id);
|
|
244
|
-
}
|
|
245
|
-
return {
|
|
246
|
-
name: resolveNamespace("resource"),
|
|
247
|
-
/**
|
|
248
|
-
* NOTE:
|
|
249
|
-
*
|
|
250
|
-
* For vite, If we have json (including SFC's custom block),
|
|
251
|
-
* transform it first because it will be transformed into javascript code by `vite:json` plugin.
|
|
252
|
-
*
|
|
253
|
-
* For webpack, This plugin will handle with 'post', because vue-loader generate the request query.
|
|
254
|
-
*/
|
|
255
|
-
enforce: meta.framework === "vite" ? "pre" : "post",
|
|
256
|
-
vite: {
|
|
257
|
-
config() {
|
|
258
|
-
const defineConfig = {
|
|
259
|
-
define: {
|
|
260
|
-
__VUE_I18N_LEGACY_API__: !compositionOnly,
|
|
261
|
-
__VUE_I18N_FULL_INSTALL__: fullInstall,
|
|
262
|
-
__INTLIFY_DROP_MESSAGE_COMPILER__: dropMessageCompiler,
|
|
263
|
-
__VUE_I18N_PROD_DEVTOOLS__: false
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
debug$3("define Config:", defineConfig);
|
|
267
|
-
const aliasConfig = {
|
|
268
|
-
resolve: {
|
|
269
|
-
alias: {
|
|
270
|
-
[vueI18nAliasName]: getVueI18nAliasPath({
|
|
271
|
-
ssr: ssrBuild,
|
|
272
|
-
runtimeOnly
|
|
273
|
-
})
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
debug$3("alias Config:", aliasConfig);
|
|
278
|
-
return assign(defineConfig, aliasConfig);
|
|
279
|
-
},
|
|
280
|
-
async configResolved(config) {
|
|
281
|
-
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
282
|
-
if (!checkVuePlugin(vuePlugin)) {
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
isProduction = config.isProduction;
|
|
286
|
-
sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
|
|
287
|
-
debug$3(`configResolved: isProduction = ${isProduction}, sourceMap = ${sourceMap}`);
|
|
288
|
-
const jsonPlugin = getVitePlugin(config, "vite:json");
|
|
289
|
-
hasViteJsonPlugin = !!jsonPlugin;
|
|
290
|
-
if (jsonPlugin && jsonPlugin.transform) {
|
|
291
|
-
const transform = jsonPlugin.transform;
|
|
292
|
-
const isObjectHook = typeof transform !== "function" && "handler" in transform;
|
|
293
|
-
const orgTransform = isObjectHook ? transform.handler : transform;
|
|
294
|
-
async function overrideJson(code, id) {
|
|
295
|
-
const filter = await getFilter();
|
|
296
|
-
if (!/\.json$/.test(id) || filter(id)) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
const { query } = parseVueRequest(id);
|
|
300
|
-
if (query.vue) {
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
debug$3("org json plugin");
|
|
304
|
-
return orgTransform.apply(this, [code, id]);
|
|
305
|
-
}
|
|
306
|
-
if (isObjectHook) {
|
|
307
|
-
transform.handler = overrideJson;
|
|
308
|
-
} else {
|
|
309
|
-
jsonPlugin.transform = overrideJson;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
},
|
|
313
|
-
handleHotUpdate({ file, modules, server }) {
|
|
314
|
-
const key = normalize(file);
|
|
315
|
-
const virtualId = realPathToVirtualId.get(key);
|
|
316
|
-
const inBundle = !!(include && createFilter(include, exclude)(key));
|
|
317
|
-
if (!virtualId && !inBundle) return;
|
|
318
|
-
const affected = new Set(modules);
|
|
319
|
-
const addById = (id) => {
|
|
320
|
-
const mod = id && server.moduleGraph.getModuleById(id);
|
|
321
|
-
if (mod) affected.add(mod);
|
|
322
|
-
};
|
|
323
|
-
addById(virtualId);
|
|
324
|
-
if (inBundle) {
|
|
325
|
-
addById(asVirtualId(INTLIFY_BUNDLE_IMPORT_ID, meta.framework));
|
|
326
|
-
}
|
|
327
|
-
if (affected.size === modules.length) return;
|
|
328
|
-
return [...affected];
|
|
329
|
-
}
|
|
330
|
-
},
|
|
331
|
-
webpack(compiler) {
|
|
332
|
-
isProduction = compiler.options.mode !== "development";
|
|
333
|
-
sourceMap = !!compiler.options.devtool;
|
|
334
|
-
debug$3(`webpack: isProduction = ${isProduction}, sourceMap = ${sourceMap}`);
|
|
335
|
-
compiler.options.resolve = normalizeConfigResolveAlias(
|
|
336
|
-
compiler.options.resolve,
|
|
337
|
-
meta.framework
|
|
338
|
-
);
|
|
339
|
-
compiler.options.resolve.alias[vueI18nAliasName] = getVueI18nAliasPath({
|
|
340
|
-
ssr: ssrBuild,
|
|
341
|
-
runtimeOnly
|
|
342
|
-
});
|
|
343
|
-
debug$3(
|
|
344
|
-
`set ${vueI18nAliasName}: ${getVueI18nAliasPath({
|
|
345
|
-
ssr: ssrBuild,
|
|
346
|
-
runtimeOnly
|
|
347
|
-
})}`
|
|
348
|
-
);
|
|
349
|
-
compiler.options.plugins.push(
|
|
350
|
-
new compiler.webpack.DefinePlugin({
|
|
351
|
-
__VUE_I18N_LEGACY_API__: JSON.stringify(compositionOnly),
|
|
352
|
-
__VUE_I18N_FULL_INSTALL__: JSON.stringify(fullInstall),
|
|
353
|
-
__INTLIFY_PROD_DEVTOOLS__: "false"
|
|
354
|
-
})
|
|
355
|
-
);
|
|
356
|
-
debug$3(`set __VUE_I18N_LEGACY_API__ is '${compositionOnly}'`);
|
|
357
|
-
debug$3(`set __VUE_I18N_FULL_INSTALL__ is '${fullInstall}'`);
|
|
358
|
-
const filter = createFilter(...resolveIncludeExclude());
|
|
359
|
-
if (compiler.options.module) {
|
|
360
|
-
compiler.options.module.rules.push({
|
|
361
|
-
test: RE_RESOURCE_FORMAT,
|
|
362
|
-
type: "javascript/auto",
|
|
363
|
-
include(resource) {
|
|
364
|
-
debug$3("webpack resource include", resource);
|
|
365
|
-
return filter(resource);
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
},
|
|
370
|
-
resolveId: {
|
|
371
|
-
async handler(id, importer) {
|
|
372
|
-
if (id === INTLIFY_BUNDLE_IMPORT_ID) {
|
|
373
|
-
return asVirtualId(id, meta.framework);
|
|
374
|
-
}
|
|
375
|
-
if (meta.framework === "vite" && !hasViteJsonPlugin) {
|
|
376
|
-
if (id.includes("?vue&type=i18n") && /[?&]lang\.(?:json|json5)(?:$|&)/.test(id)) {
|
|
377
|
-
const { filename, query } = parseVueRequest(id);
|
|
378
|
-
let virtualKey = id;
|
|
379
|
-
if (query.src && importer) {
|
|
380
|
-
const importerPath = (isIntlifyVirtualId(importer) ? virtualIdToRealPath.get(importer) : importer).split("?")[0];
|
|
381
|
-
if (importerPath.endsWith(".vue")) {
|
|
382
|
-
const resolvedSidecar = resolve(dirname(importerPath), filename);
|
|
383
|
-
virtualKey = id.replace(filename, resolvedSidecar);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
return intlifyVirtualize(virtualKey);
|
|
387
|
-
}
|
|
388
|
-
const idPath = id.split("?")[0];
|
|
389
|
-
if (!RE_RESOURCE_FORMAT.test(idPath)) return;
|
|
390
|
-
let resolvedPath;
|
|
391
|
-
if (idPath.startsWith(".")) {
|
|
392
|
-
const realImporter = importer && isIntlifyVirtualId(importer) ? virtualIdToRealPath.get(importer) : importer;
|
|
393
|
-
if (!realImporter) return;
|
|
394
|
-
resolvedPath = resolve(dirname(realImporter), idPath);
|
|
395
|
-
} else if (idPath.startsWith("/") || /^[a-z]:[/\\]/i.test(idPath)) {
|
|
396
|
-
resolvedPath = idPath;
|
|
397
|
-
} else {
|
|
398
|
-
return;
|
|
399
|
-
}
|
|
400
|
-
const filter = await getFilter();
|
|
401
|
-
if (!filter(resolvedPath)) return;
|
|
402
|
-
return intlifyVirtualize(resolvedPath);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
},
|
|
406
|
-
load: {
|
|
407
|
-
async handler(id) {
|
|
408
|
-
debug$3("load", id);
|
|
409
|
-
if (INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework) && include) {
|
|
410
|
-
let resourcePaths = [];
|
|
411
|
-
for (const inc of include) {
|
|
412
|
-
resourcePaths = [...resourcePaths, ...await fg(inc, { absolute: true })];
|
|
413
|
-
}
|
|
414
|
-
resourcePaths = resourcePaths.filter((el, pos) => resourcePaths.indexOf(el) === pos);
|
|
415
|
-
const code = await generateBundleResources(resourcePaths, isProduction, {
|
|
416
|
-
forceStringify,
|
|
417
|
-
strictMessage,
|
|
418
|
-
escapeHtml,
|
|
419
|
-
usedKeyFilter: collector ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
|
|
420
|
-
});
|
|
421
|
-
return {
|
|
422
|
-
moduleType: "js",
|
|
423
|
-
code,
|
|
424
|
-
map: { mappings: "" }
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
if (isIntlifyVirtualId(id)) {
|
|
428
|
-
const realId = virtualIdToRealPath.get(id);
|
|
429
|
-
if (realId.includes("?vue&type=i18n")) {
|
|
430
|
-
const { filename, query } = parseVueRequest(realId);
|
|
431
|
-
let source;
|
|
432
|
-
if (query.src) {
|
|
433
|
-
this.addWatchFile(filename);
|
|
434
|
-
source = await promises.readFile(filename, "utf-8");
|
|
435
|
-
} else {
|
|
436
|
-
this.addWatchFile(filename);
|
|
437
|
-
const sfcSource = await promises.readFile(filename, "utf-8");
|
|
438
|
-
const { descriptor } = getSfcParser()(sfcSource, {
|
|
439
|
-
sourceMap,
|
|
440
|
-
filename
|
|
441
|
-
});
|
|
442
|
-
const block = descriptor.customBlocks[query.index];
|
|
443
|
-
if (!block) return;
|
|
444
|
-
source = block.content;
|
|
445
|
-
}
|
|
446
|
-
if (typeof transformI18nBlock === "function") {
|
|
447
|
-
const modifiedSource = transformI18nBlock(source);
|
|
448
|
-
if (modifiedSource && typeof modifiedSource === "string") {
|
|
449
|
-
source = modifiedSource;
|
|
450
|
-
} else {
|
|
451
|
-
warn("transformI18nBlock should return a string");
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
let langInfo2 = defaultSFCLang;
|
|
455
|
-
if (isString(query.lang)) {
|
|
456
|
-
langInfo2 = query.src ? query.lang === "i18n" ? defaultSFCLang : query.lang : query.lang;
|
|
457
|
-
}
|
|
458
|
-
if (/\.?[cm]?[jt]s$/.test(langInfo2)) {
|
|
459
|
-
source = `export default ${source.replace(/^[\s;]+/, "")}`;
|
|
460
|
-
}
|
|
461
|
-
const generate2 = getGenerator(langInfo2, generateYAML);
|
|
462
|
-
const isGlobalBlock = globalSFCScope || !!query.global;
|
|
463
|
-
const parseOptions2 = getOptions(
|
|
464
|
-
filename,
|
|
465
|
-
isProduction,
|
|
466
|
-
query,
|
|
467
|
-
sourceMap,
|
|
468
|
-
{
|
|
469
|
-
isGlobal: globalSFCScope,
|
|
470
|
-
allowDynamic,
|
|
471
|
-
jit: true,
|
|
472
|
-
strictMessage,
|
|
473
|
-
escapeHtml,
|
|
474
|
-
onlyLocales,
|
|
475
|
-
forceStringify,
|
|
476
|
-
usedKeyFilter: collector && isGlobalBlock ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
|
|
477
|
-
}
|
|
478
|
-
);
|
|
479
|
-
const { code: generatedCode2 } = await generate2(source, parseOptions2);
|
|
480
|
-
return {
|
|
481
|
-
moduleType: "js",
|
|
482
|
-
code: generatedCode2,
|
|
483
|
-
map: { mappings: "" }
|
|
484
|
-
};
|
|
485
|
-
}
|
|
486
|
-
this.addWatchFile(realId);
|
|
487
|
-
const code = await promises.readFile(realId, "utf-8");
|
|
488
|
-
const langInfo = parse(realId).ext;
|
|
489
|
-
const generate = getGenerator(langInfo);
|
|
490
|
-
const parseOptions = getOptions(
|
|
491
|
-
realId,
|
|
492
|
-
isProduction,
|
|
493
|
-
{},
|
|
494
|
-
sourceMap,
|
|
495
|
-
{
|
|
496
|
-
isGlobal: globalSFCScope,
|
|
497
|
-
allowDynamic,
|
|
498
|
-
strictMessage,
|
|
499
|
-
escapeHtml,
|
|
500
|
-
jit: true,
|
|
501
|
-
onlyLocales,
|
|
502
|
-
forceStringify,
|
|
503
|
-
usedKeyFilter: collector ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
|
|
504
|
-
}
|
|
505
|
-
);
|
|
506
|
-
const { code: generatedCode } = await generate(code, parseOptions);
|
|
507
|
-
return {
|
|
508
|
-
moduleType: "js",
|
|
509
|
-
code: generatedCode,
|
|
510
|
-
map: { mappings: "" }
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
},
|
|
515
|
-
transform: {
|
|
516
|
-
async handler(code, id) {
|
|
517
|
-
if (isIntlifyVirtualId(id)) return;
|
|
518
|
-
const filter = await getFilter();
|
|
519
|
-
if (!filter(id)) {
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
const { filename, query } = parseVueRequest(id);
|
|
523
|
-
debug$3("transform", id, JSON.stringify(query), filename);
|
|
524
|
-
let langInfo = defaultSFCLang;
|
|
525
|
-
let inSourceMap;
|
|
526
|
-
if (!query.vue) {
|
|
527
|
-
langInfo = parse(filename).ext;
|
|
528
|
-
const generate = getGenerator(langInfo);
|
|
529
|
-
const parseOptions = getOptions(
|
|
530
|
-
filename,
|
|
531
|
-
isProduction,
|
|
532
|
-
query,
|
|
533
|
-
sourceMap,
|
|
534
|
-
{
|
|
535
|
-
inSourceMap,
|
|
536
|
-
isGlobal: globalSFCScope,
|
|
537
|
-
allowDynamic,
|
|
538
|
-
strictMessage,
|
|
539
|
-
escapeHtml,
|
|
540
|
-
jit: true,
|
|
541
|
-
onlyLocales,
|
|
542
|
-
forceStringify,
|
|
543
|
-
usedKeyFilter: collector ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
|
|
544
|
-
}
|
|
545
|
-
);
|
|
546
|
-
debug$3("parseOptions", parseOptions);
|
|
547
|
-
const { code: generatedCode, map } = await generate(code, parseOptions);
|
|
548
|
-
debug$3("generated code", generatedCode);
|
|
549
|
-
debug$3("sourcemap", map, sourceMap);
|
|
550
|
-
if (code === generatedCode) return;
|
|
551
|
-
return {
|
|
552
|
-
moduleType: "js",
|
|
553
|
-
code: generatedCode,
|
|
554
|
-
// prettier-ignore
|
|
555
|
-
map: { mappings: "" }
|
|
556
|
-
};
|
|
557
|
-
} else {
|
|
558
|
-
if (isCustomBlock(query)) {
|
|
559
|
-
if (isString(query.lang)) {
|
|
560
|
-
langInfo = query.src ? query.lang === "i18n" ? defaultSFCLang : query.lang : query.lang;
|
|
561
|
-
} else if (defaultSFCLang) {
|
|
562
|
-
langInfo = defaultSFCLang;
|
|
563
|
-
}
|
|
564
|
-
debug$3("langInfo", langInfo);
|
|
565
|
-
const generate = getGenerator(langInfo, generateYAML);
|
|
566
|
-
const isGlobalBlock = globalSFCScope || !!query.global;
|
|
567
|
-
const parseOptions = getOptions(
|
|
568
|
-
filename,
|
|
569
|
-
isProduction,
|
|
570
|
-
query,
|
|
571
|
-
sourceMap,
|
|
572
|
-
{
|
|
573
|
-
inSourceMap,
|
|
574
|
-
isGlobal: globalSFCScope,
|
|
575
|
-
allowDynamic,
|
|
576
|
-
jit: true,
|
|
577
|
-
strictMessage,
|
|
578
|
-
escapeHtml,
|
|
579
|
-
onlyLocales,
|
|
580
|
-
forceStringify,
|
|
581
|
-
usedKeyFilter: collector && isGlobalBlock ? (keyPath) => collector.shouldKeepKey(keyPath) : void 0
|
|
582
|
-
}
|
|
583
|
-
);
|
|
584
|
-
debug$3("parseOptions", parseOptions);
|
|
585
|
-
let source = await getCode(
|
|
586
|
-
code,
|
|
587
|
-
filename,
|
|
588
|
-
sourceMap,
|
|
589
|
-
query,
|
|
590
|
-
getSfcParser(),
|
|
591
|
-
meta.framework
|
|
592
|
-
);
|
|
593
|
-
if (typeof transformI18nBlock === "function") {
|
|
594
|
-
const modifiedSource = transformI18nBlock(source);
|
|
595
|
-
if (modifiedSource && typeof modifiedSource === "string") {
|
|
596
|
-
source = modifiedSource;
|
|
597
|
-
} else {
|
|
598
|
-
warn("transformI18nBlock should return a string");
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
if (/\.?[cm]?[jt]s$/.test(langInfo)) {
|
|
602
|
-
source = `export default ${source.replace(/^[\s;]+/, "")}`;
|
|
603
|
-
}
|
|
604
|
-
const { code: generatedCode, map } = await generate(source, parseOptions);
|
|
605
|
-
debug$3("generated code", generatedCode);
|
|
606
|
-
debug$3("sourcemap", map, sourceMap);
|
|
607
|
-
if (code === generatedCode) return;
|
|
608
|
-
return {
|
|
609
|
-
moduleType: "js",
|
|
610
|
-
code: generatedCode,
|
|
611
|
-
// prettier-ignore
|
|
612
|
-
map: { mappings: "" }
|
|
613
|
-
};
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
function getGenerator(ext, fallback = generateJSON) {
|
|
621
|
-
if (/\.?json5?$/.test(ext)) {
|
|
622
|
-
return generateJSON;
|
|
623
|
-
}
|
|
624
|
-
if (/\.?ya?ml$/.test(ext)) {
|
|
625
|
-
return generateYAML;
|
|
626
|
-
}
|
|
627
|
-
if (/\.?[cm]?js$/.test(ext)) {
|
|
628
|
-
return generateJavaScript;
|
|
629
|
-
}
|
|
630
|
-
if (/\.?[cm]?ts$/.test(ext)) {
|
|
631
|
-
return generateTypescript;
|
|
632
|
-
}
|
|
633
|
-
return fallback;
|
|
634
|
-
}
|
|
635
|
-
function normalizeConfigResolveAlias(resolve2, framework) {
|
|
636
|
-
if (resolve2 && resolve2.alias) {
|
|
637
|
-
return resolve2;
|
|
638
|
-
}
|
|
639
|
-
if (!resolve2) {
|
|
640
|
-
if (framework === "vite") {
|
|
641
|
-
return { alias: [] };
|
|
642
|
-
} else if (framework === "webpack") {
|
|
643
|
-
return { alias: {} };
|
|
644
|
-
}
|
|
645
|
-
} else if (!resolve2.alias) {
|
|
646
|
-
if (framework === "vite") {
|
|
647
|
-
resolve2.alias = [];
|
|
648
|
-
return resolve2;
|
|
649
|
-
} else if (framework === "webpack") {
|
|
650
|
-
resolve2.alias = {};
|
|
651
|
-
return resolve2;
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
async function generateBundleResources(resources, isProduction, {
|
|
656
|
-
forceStringify = false,
|
|
657
|
-
isGlobal = false,
|
|
658
|
-
onlyLocales = [],
|
|
659
|
-
strictMessage = true,
|
|
660
|
-
escapeHtml = false,
|
|
661
|
-
jit = true,
|
|
662
|
-
transformI18nBlock = void 0,
|
|
663
|
-
usedKeyFilter = void 0
|
|
664
|
-
}) {
|
|
665
|
-
const codes = [];
|
|
666
|
-
for (const res of resources) {
|
|
667
|
-
debug$3(`${res} bundle loading ...`);
|
|
668
|
-
if (/\.(json5?|ya?ml)$/.test(res)) {
|
|
669
|
-
const { ext, name } = parse(res);
|
|
670
|
-
const source = await getRaw(res);
|
|
671
|
-
const generate = /json5?/.test(ext) ? generateJSON : generateYAML;
|
|
672
|
-
const parseOptions = getOptions(res, isProduction, {}, false, {
|
|
673
|
-
isGlobal,
|
|
674
|
-
jit,
|
|
675
|
-
onlyLocales,
|
|
676
|
-
strictMessage,
|
|
677
|
-
escapeHtml,
|
|
678
|
-
forceStringify,
|
|
679
|
-
transformI18nBlock,
|
|
680
|
-
usedKeyFilter
|
|
681
|
-
});
|
|
682
|
-
parseOptions.type = "bare";
|
|
683
|
-
const { code } = generate(source, parseOptions);
|
|
684
|
-
debug$3("generated code", code);
|
|
685
|
-
codes.push(`${JSON.stringify(name)}: ${code}`);
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
return `const isObject = (item) => item && typeof item === 'object' && !Array.isArray(item);
|
|
689
|
-
|
|
690
|
-
const mergeDeep = (target, ...sources) => {
|
|
691
|
-
if (!sources.length) return target;
|
|
692
|
-
const source = sources.shift();
|
|
693
|
-
|
|
694
|
-
if (isObject(target) && isObject(source)) {
|
|
695
|
-
for (const key in source) {
|
|
696
|
-
if (isObject(source[key])) {
|
|
697
|
-
if (!target[key]) Object.assign(target, { [key]: {} });
|
|
698
|
-
mergeDeep(target[key], source[key]);
|
|
699
|
-
} else {
|
|
700
|
-
Object.assign(target, { [key]: source[key] });
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
return mergeDeep(target, ...sources);
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
export default mergeDeep({},
|
|
709
|
-
${codes.map((code) => `{${code}}`).join(",\n")}
|
|
710
|
-
);`;
|
|
711
|
-
}
|
|
712
|
-
async function getCode(source, filename, sourceMap, query, parser, framework = "vite") {
|
|
713
|
-
const { index, issuerPath } = query;
|
|
714
|
-
if (!isNumber(index)) {
|
|
715
|
-
raiseError(`unexpected index: ${index}`);
|
|
716
|
-
}
|
|
717
|
-
if (framework === "webpack") {
|
|
718
|
-
if (issuerPath) {
|
|
719
|
-
debug$3(`getCode (webpack) ${index} via issuerPath`, issuerPath);
|
|
720
|
-
return await getRaw(filename);
|
|
721
|
-
} else {
|
|
722
|
-
const result = parser(await getRaw(filename), {
|
|
723
|
-
sourceMap,
|
|
724
|
-
filename
|
|
725
|
-
});
|
|
726
|
-
const block = result.descriptor.customBlocks[index];
|
|
727
|
-
if (block) {
|
|
728
|
-
const code = block.src ? await getRaw(resolveBlockSrc(block.src, filename)) : block.content;
|
|
729
|
-
debug$3(`getCode (webpack) ${index} from SFC`, code);
|
|
730
|
-
return code;
|
|
731
|
-
} else {
|
|
732
|
-
return source;
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
} else {
|
|
736
|
-
return source;
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
function isCustomBlock(query) {
|
|
740
|
-
return !isEmptyObject(query) && "vue" in query && (query["type"] === "custom" || // for webpack (vue-loader)
|
|
741
|
-
query["type"] === "i18n" || // for vite (@vite/plugin-vue)
|
|
742
|
-
query["blockType"] === "i18n");
|
|
743
|
-
}
|
|
744
|
-
function getOptions(filename, isProduction, query, sourceMap, {
|
|
745
|
-
inSourceMap = void 0,
|
|
746
|
-
forceStringify = false,
|
|
747
|
-
isGlobal = false,
|
|
748
|
-
onlyLocales = [],
|
|
749
|
-
allowDynamic = false,
|
|
750
|
-
strictMessage = true,
|
|
751
|
-
escapeHtml = false,
|
|
752
|
-
jit = true,
|
|
753
|
-
transformI18nBlock = null,
|
|
754
|
-
usedKeyFilter = void 0
|
|
755
|
-
}) {
|
|
756
|
-
const mode = isProduction ? "production" : "development";
|
|
757
|
-
const baseOptions = {
|
|
758
|
-
filename,
|
|
759
|
-
sourceMap,
|
|
760
|
-
inSourceMap,
|
|
761
|
-
forceStringify,
|
|
762
|
-
allowDynamic,
|
|
763
|
-
strictMessage,
|
|
764
|
-
escapeHtml,
|
|
765
|
-
jit,
|
|
766
|
-
onlyLocales,
|
|
767
|
-
usedKeyFilter,
|
|
768
|
-
env: mode,
|
|
769
|
-
transformI18nBlock,
|
|
770
|
-
onWarn: (msg) => {
|
|
771
|
-
warn(`${filename} ${msg}`);
|
|
772
|
-
},
|
|
773
|
-
onError: (msg, extra) => {
|
|
774
|
-
const codeFrame = generateCodeFrame(
|
|
775
|
-
extra?.source || extra?.location?.source || "",
|
|
776
|
-
extra?.location?.start.column,
|
|
777
|
-
extra?.location?.end.column
|
|
778
|
-
);
|
|
779
|
-
const errMssage = `${msg} (error code: ${extra?.code}) in ${filename}
|
|
780
|
-
target message: ${extra?.source}
|
|
781
|
-
target message path: ${extra?.path}
|
|
782
|
-
|
|
783
|
-
${codeFrame}
|
|
784
|
-
`;
|
|
785
|
-
error(errMssage);
|
|
786
|
-
throw new Error(errMssage);
|
|
787
|
-
}
|
|
788
|
-
};
|
|
789
|
-
if (isCustomBlock(query)) {
|
|
790
|
-
return assign(baseOptions, {
|
|
791
|
-
type: "sfc",
|
|
792
|
-
locale: isString(query.locale) ? query.locale : "",
|
|
793
|
-
isGlobal: isGlobal || !!query.global
|
|
794
|
-
});
|
|
795
|
-
} else {
|
|
796
|
-
return assign(baseOptions, {
|
|
797
|
-
type: "plain",
|
|
798
|
-
isGlobal: false
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
function resolveBlockSrc(src, sfcFilename) {
|
|
803
|
-
if (src.startsWith(".") || !src.startsWith("/") && !/^[a-z]:[/\\]/i.test(src)) {
|
|
804
|
-
return resolve(dirname(sfcFilename), src);
|
|
805
|
-
}
|
|
806
|
-
return src;
|
|
807
|
-
}
|
|
808
|
-
function getVirtualId(id, framework = "vite") {
|
|
809
|
-
return framework === "vite" ? id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : "" : id;
|
|
810
|
-
}
|
|
811
|
-
function asVirtualId(id, framework = "vite") {
|
|
812
|
-
return framework === "vite" ? VIRTUAL_PREFIX + id : id;
|
|
813
|
-
}
|
|
814
|
-
async function getRaw(path) {
|
|
815
|
-
return promises.readFile(path, { encoding: "utf-8" });
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
const debug$2 = createDebug(resolveNamespace("directive"));
|
|
819
|
-
const tsEstree = {
|
|
820
|
-
parse: void 0,
|
|
821
|
-
simpleTraverse: void 0,
|
|
822
|
-
AST_NODE_TYPES: void 0
|
|
823
|
-
};
|
|
824
|
-
function directivePlugin({
|
|
825
|
-
optimizeTranslationDirective,
|
|
826
|
-
translationIdentifiers
|
|
827
|
-
}) {
|
|
828
|
-
let vuePlugin = null;
|
|
829
|
-
let vuePluginOptions = null;
|
|
830
|
-
const excludeLangs = ["pug", "jsx", "tsx"];
|
|
831
|
-
return {
|
|
832
|
-
name: resolveNamespace("directive"),
|
|
833
|
-
enforce: "pre",
|
|
834
|
-
vite: {
|
|
835
|
-
async config(config) {
|
|
836
|
-
await import('@typescript-eslint/typescript-estree').then((r) => {
|
|
837
|
-
tsEstree.parse = r.parse;
|
|
838
|
-
tsEstree.simpleTraverse = r.simpleTraverse;
|
|
839
|
-
tsEstree.AST_NODE_TYPES = r.AST_NODE_TYPES;
|
|
840
|
-
return;
|
|
841
|
-
});
|
|
842
|
-
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
843
|
-
if (!checkVuePlugin(vuePlugin)) {
|
|
844
|
-
return;
|
|
845
|
-
}
|
|
846
|
-
if (optimizeTranslationDirective) {
|
|
847
|
-
vuePlugin.api.options = resolveVueOptions(
|
|
848
|
-
vuePlugin,
|
|
849
|
-
optimizeTranslationDirective,
|
|
850
|
-
translationIdentifiers
|
|
851
|
-
);
|
|
852
|
-
debug$2(`vite:vue options['template']:`, vuePlugin.api.options);
|
|
853
|
-
}
|
|
854
|
-
},
|
|
855
|
-
configResolved(config) {
|
|
856
|
-
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
857
|
-
if (!checkVuePlugin(vuePlugin)) {
|
|
858
|
-
return;
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
},
|
|
862
|
-
async transform(code, id) {
|
|
863
|
-
if (id.endsWith(".vue")) {
|
|
864
|
-
const { filename, query } = parseVueRequest(id);
|
|
865
|
-
if (!excludeLangs.includes(query.lang ?? "")) {
|
|
866
|
-
if (vuePluginOptions == null) {
|
|
867
|
-
vuePluginOptions = getVuePluginOptions(vuePlugin);
|
|
868
|
-
}
|
|
869
|
-
if (vuePluginOptions?.compiler) {
|
|
870
|
-
analyzeIdentifiers(
|
|
871
|
-
getDescriptor(filename, code, vuePluginOptions),
|
|
872
|
-
vuePluginOptions,
|
|
873
|
-
translationIdentifiers
|
|
874
|
-
);
|
|
875
|
-
return {
|
|
876
|
-
code,
|
|
877
|
-
map: { version: 3, mappings: "", sources: [] }
|
|
878
|
-
};
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
};
|
|
884
|
-
}
|
|
885
|
-
function resolveVueOptions(vuePlugin, optimizeTranslationDirective, translationIdentifiers) {
|
|
886
|
-
const vueOptions = vuePlugin.api.options;
|
|
887
|
-
vueOptions.template ||= {};
|
|
888
|
-
vueOptions.template.compilerOptions ||= {};
|
|
889
|
-
vueOptions.template.compilerOptions.directiveTransforms ||= {};
|
|
890
|
-
const translationSignatureResolver = (context, baseResolver) => {
|
|
891
|
-
const { filename } = context;
|
|
892
|
-
const vuePluginOptions = getVuePluginOptions(vuePlugin);
|
|
893
|
-
const normalizedFilename = normalizePath(path.relative(vuePluginOptions.root, filename));
|
|
894
|
-
const resolveIdentifier2 = translationIdentifiers.get(normalizedFilename);
|
|
895
|
-
debug$2("resolved vue-i18n Identifier", resolveIdentifier2);
|
|
896
|
-
if (resolveIdentifier2 == null) {
|
|
897
|
-
return void 0;
|
|
898
|
-
}
|
|
899
|
-
if (resolveIdentifier2.type === "identifier") {
|
|
900
|
-
return baseResolver(context, resolveIdentifier2.key);
|
|
901
|
-
} else {
|
|
902
|
-
const resolvedSignature = baseResolver(context, resolveIdentifier2.key);
|
|
903
|
-
return resolveIdentifier2?.style === "script-setup" ? `${resolvedSignature}.t` : resolvedSignature;
|
|
904
|
-
}
|
|
905
|
-
};
|
|
906
|
-
vueOptions.template.compilerOptions.directiveTransforms.t = transformVTDirective({
|
|
907
|
-
translationSignatures: isBoolean(optimizeTranslationDirective) ? translationSignatureResolver : optimizeTranslationDirective
|
|
908
|
-
});
|
|
909
|
-
return vueOptions;
|
|
910
|
-
}
|
|
911
|
-
function analyzeIdentifiers(descriptor, { root }, translationIdentifiers) {
|
|
912
|
-
const source = descriptor.scriptSetup?.content || descriptor.script?.content;
|
|
913
|
-
debug$2("getDescriptor content", source);
|
|
914
|
-
if (!source) {
|
|
915
|
-
return;
|
|
916
|
-
}
|
|
917
|
-
const ast = tsEstree.parse(source, { range: true });
|
|
918
|
-
tsEstree.simpleTraverse(ast, {
|
|
919
|
-
enter(node, parent) {
|
|
920
|
-
if (parent) {
|
|
921
|
-
node.parent = parent;
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
});
|
|
925
|
-
const scopeManager = analyze(ast, { sourceType: "module" });
|
|
926
|
-
const scope = getScope(scopeManager, ast);
|
|
927
|
-
const importLocalName = getImportLocalName(scope, "vue-i18n", "useI18n");
|
|
928
|
-
if (importLocalName == null) {
|
|
929
|
-
return;
|
|
930
|
-
}
|
|
931
|
-
debug$2("importLocalName", importLocalName);
|
|
932
|
-
const resolvedIdentifier = getVueI18nIdentifier(scope, importLocalName);
|
|
933
|
-
if (resolvedIdentifier) {
|
|
934
|
-
const normalizedFilename = normalizePath(path.relative(root, descriptor.filename));
|
|
935
|
-
debug$2("set vue-i18n resolved identifier: ", resolvedIdentifier);
|
|
936
|
-
translationIdentifiers.set(normalizedFilename, resolvedIdentifier);
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
function getScope(manager, node) {
|
|
940
|
-
const scope = manager.acquire(node, true);
|
|
941
|
-
if (scope) {
|
|
942
|
-
if (scope.type === "function-expression-name") {
|
|
943
|
-
return scope.childScopes[0];
|
|
944
|
-
}
|
|
945
|
-
return scope;
|
|
946
|
-
}
|
|
947
|
-
return manager.scopes[0];
|
|
948
|
-
}
|
|
949
|
-
function getImportLocalName(scope, source, imported) {
|
|
950
|
-
const importDecl = getImportDeclaration(scope, source);
|
|
951
|
-
if (importDecl) {
|
|
952
|
-
const specifierNode = importDecl.specifiers.find(
|
|
953
|
-
(specifierNode2) => isImportedIdentifierInImportClause(specifierNode2) && specifierNode2.imported.name === imported
|
|
954
|
-
);
|
|
955
|
-
return specifierNode ? specifierNode.local.name : null;
|
|
956
|
-
}
|
|
957
|
-
return null;
|
|
958
|
-
}
|
|
959
|
-
function getImportDeclaration(scope, source) {
|
|
960
|
-
const tracker = new eslintUitls.ReferenceTracker(scope);
|
|
961
|
-
const traceMap = {
|
|
962
|
-
[source]: {
|
|
963
|
-
[eslintUitls.ReferenceTracker.ESM]: true,
|
|
964
|
-
[eslintUitls.ReferenceTracker.READ]: true
|
|
965
|
-
}
|
|
966
|
-
};
|
|
967
|
-
const refs = Array.from(tracker.iterateEsmReferences(traceMap));
|
|
968
|
-
return refs.length ? refs[0].node : null;
|
|
969
|
-
}
|
|
970
|
-
function isImportedIdentifierInImportClause(node) {
|
|
971
|
-
return "imported" in node;
|
|
972
|
-
}
|
|
973
|
-
function getVueI18nIdentifier(scope, local) {
|
|
974
|
-
const { callExpression, returnStatement } = getCallExpressionAndReturnStatement(scope, local);
|
|
975
|
-
if (callExpression == null) {
|
|
976
|
-
return null;
|
|
977
|
-
}
|
|
978
|
-
const id = getVariableDeclarationIdFrom(callExpression);
|
|
979
|
-
if (id == null) {
|
|
980
|
-
return null;
|
|
981
|
-
}
|
|
982
|
-
const variableIdPairs = parseVariableId(id);
|
|
983
|
-
debug$2("variableIdPairs:", variableIdPairs);
|
|
984
|
-
const returnVariableIdPairs = parseReturnStatement(returnStatement);
|
|
985
|
-
debug$2("returnVariableIdPairs:", returnVariableIdPairs);
|
|
986
|
-
return resolveIdentifier(variableIdPairs, returnVariableIdPairs);
|
|
987
|
-
}
|
|
988
|
-
const EMPTY_NODE_RETURN = {
|
|
989
|
-
callExpression: null,
|
|
990
|
-
returnStatement: null
|
|
991
|
-
};
|
|
992
|
-
function getCallExpressionAndReturnStatement(scope, local) {
|
|
993
|
-
const variable = eslintUitls.findVariable(scope, local);
|
|
994
|
-
if (variable == null) {
|
|
995
|
-
return EMPTY_NODE_RETURN;
|
|
996
|
-
}
|
|
997
|
-
const callExpressionRef = variable.references.find((ref) => {
|
|
998
|
-
return ref.identifier.parent?.type === tsEstree.AST_NODE_TYPES.CallExpression;
|
|
999
|
-
});
|
|
1000
|
-
if (callExpressionRef == null) {
|
|
1001
|
-
return EMPTY_NODE_RETURN;
|
|
1002
|
-
}
|
|
1003
|
-
let returnStatement = null;
|
|
1004
|
-
if (callExpressionRef.from.type === "function" && callExpressionRef.from.block.type === tsEstree.AST_NODE_TYPES.FunctionExpression && // @ts-expect-error -- FIXME: type error
|
|
1005
|
-
callExpressionRef.from.block.parent.type === tsEstree.AST_NODE_TYPES.Property && // @ts-expect-error -- FIXME: type error
|
|
1006
|
-
callExpressionRef.from.block.parent.key.type === tsEstree.AST_NODE_TYPES.Identifier && // @ts-expect-error -- FIXME: type error
|
|
1007
|
-
callExpressionRef.from.block.parent.key.name === "setup") {
|
|
1008
|
-
returnStatement = callExpressionRef.from.block.body.body.find((statement) => {
|
|
1009
|
-
return statement.type === tsEstree.AST_NODE_TYPES.ReturnStatement;
|
|
1010
|
-
});
|
|
1011
|
-
}
|
|
1012
|
-
return {
|
|
1013
|
-
// @ts-expect-error -- FIXME: type error
|
|
1014
|
-
callExpression: callExpressionRef.identifier.parent,
|
|
1015
|
-
returnStatement
|
|
1016
|
-
};
|
|
1017
|
-
}
|
|
1018
|
-
function getVariableDeclarationIdFrom(node) {
|
|
1019
|
-
if (node.parent?.type !== tsEstree.AST_NODE_TYPES.VariableDeclarator) {
|
|
1020
|
-
return null;
|
|
1021
|
-
}
|
|
1022
|
-
return node.parent.id;
|
|
1023
|
-
}
|
|
1024
|
-
function parseVariableId(node) {
|
|
1025
|
-
if (node.type === tsEstree.AST_NODE_TYPES.Identifier) {
|
|
1026
|
-
return [{ key: node.name, value: null }];
|
|
1027
|
-
} else {
|
|
1028
|
-
const props = node.properties.filter(
|
|
1029
|
-
// ignore RestElement
|
|
1030
|
-
(prop) => prop.type === tsEstree.AST_NODE_TYPES.Property
|
|
1031
|
-
);
|
|
1032
|
-
const pairs = [];
|
|
1033
|
-
for (const prop of props) {
|
|
1034
|
-
if (prop?.key.type === tsEstree.AST_NODE_TYPES.Identifier && prop?.value.type === tsEstree.AST_NODE_TYPES.Identifier) {
|
|
1035
|
-
pairs.push({ key: prop.key.name, value: prop.value.name });
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
return pairs;
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
function parseReturnStatement(node) {
|
|
1042
|
-
const pairs = [];
|
|
1043
|
-
if (node == null || node.argument == null) {
|
|
1044
|
-
return pairs;
|
|
1045
|
-
}
|
|
1046
|
-
if (node.argument.type === tsEstree.AST_NODE_TYPES.ObjectExpression) {
|
|
1047
|
-
for (const prop of node.argument.properties) {
|
|
1048
|
-
if (prop.type === tsEstree.AST_NODE_TYPES.Property) {
|
|
1049
|
-
if (prop.key.type === tsEstree.AST_NODE_TYPES.Identifier && prop.value.type === tsEstree.AST_NODE_TYPES.Identifier) {
|
|
1050
|
-
pairs.push({ key: prop.key.name, value: prop.value.name });
|
|
1051
|
-
} else if (prop.key.type === tsEstree.AST_NODE_TYPES.Identifier && prop.value.type === tsEstree.AST_NODE_TYPES.MemberExpression && prop.value.object.type === tsEstree.AST_NODE_TYPES.Identifier && prop.value.property.type === tsEstree.AST_NODE_TYPES.Identifier) {
|
|
1052
|
-
pairs.push({
|
|
1053
|
-
key: prop.key.name,
|
|
1054
|
-
value: `${prop.value.object.name}.${prop.value.property.name}`
|
|
1055
|
-
});
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
return pairs;
|
|
1060
|
-
} else if (node.argument.type === tsEstree.AST_NODE_TYPES.Identifier) {
|
|
1061
|
-
return pairs;
|
|
1062
|
-
} else {
|
|
1063
|
-
return pairs;
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
function resolveIdentifier(localVariables, returnVariable) {
|
|
1067
|
-
if (returnVariable.length === 0) {
|
|
1068
|
-
const variable = localVariables.find((pair) => pair.key === "t");
|
|
1069
|
-
if (variable && variable.value) {
|
|
1070
|
-
return { type: "identifier", key: variable.value };
|
|
1071
|
-
}
|
|
1072
|
-
const identifierOnly = localVariables.find((pair) => pair.value === null);
|
|
1073
|
-
if (identifierOnly && identifierOnly.key) {
|
|
1074
|
-
return { type: "object", key: identifierOnly.key, style: "script-setup" };
|
|
1075
|
-
}
|
|
1076
|
-
return null;
|
|
1077
|
-
} else {
|
|
1078
|
-
const variable = localVariables.find((pair) => pair.key === "t");
|
|
1079
|
-
if (variable && variable.value) {
|
|
1080
|
-
const returnVar = returnVariable.find((pair) => pair.value === variable.value);
|
|
1081
|
-
if (returnVar && returnVar.key) {
|
|
1082
|
-
return { type: "identifier", key: returnVar.key };
|
|
1083
|
-
}
|
|
1084
|
-
}
|
|
1085
|
-
const identifierOnly = localVariables.find((pair) => pair.value === null);
|
|
1086
|
-
if (identifierOnly && identifierOnly.key) {
|
|
1087
|
-
const targetKey = identifierOnly.key;
|
|
1088
|
-
const returnVar = returnVariable.find((pair) => pair.value?.startsWith(targetKey));
|
|
1089
|
-
if (returnVar && returnVar.key) {
|
|
1090
|
-
return { type: "object", key: returnVar.key, style: "setup-hook" };
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
return null;
|
|
1094
|
-
}
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
function matchSafelistPattern(keyPath, pattern) {
|
|
1098
|
-
const regexStr = pattern.replace(/\./g, "\\.").replace(/\*\*/g, "{{GLOBSTAR}}").replace(/\*/g, "[^.]*").replace(/\{\{GLOBSTAR\}\}/g, ".*");
|
|
1099
|
-
return new RegExp(`^${regexStr}$`).test(keyPath);
|
|
1100
|
-
}
|
|
1101
|
-
function createUsedKeysCollector(options) {
|
|
1102
|
-
const usedKeys = /* @__PURE__ */ new Set();
|
|
1103
|
-
const safelistPatterns = options.safelist || [];
|
|
1104
|
-
const dynamicKeyStrategy = options.dynamicKeyStrategy || "keep-all";
|
|
1105
|
-
const removedKeys = /* @__PURE__ */ new Map();
|
|
1106
|
-
let dynamicKeysDetected = false;
|
|
1107
|
-
return {
|
|
1108
|
-
get usedKeys() {
|
|
1109
|
-
return usedKeys;
|
|
1110
|
-
},
|
|
1111
|
-
get dynamicKeysDetected() {
|
|
1112
|
-
return dynamicKeysDetected;
|
|
1113
|
-
},
|
|
1114
|
-
set dynamicKeysDetected(val) {
|
|
1115
|
-
dynamicKeysDetected = val;
|
|
1116
|
-
},
|
|
1117
|
-
safelistPatterns,
|
|
1118
|
-
removedKeys,
|
|
1119
|
-
addKey(key) {
|
|
1120
|
-
usedKeys.add(key);
|
|
1121
|
-
},
|
|
1122
|
-
markDynamic() {
|
|
1123
|
-
dynamicKeysDetected = true;
|
|
1124
|
-
},
|
|
1125
|
-
shouldKeepKey(keyPath) {
|
|
1126
|
-
if (dynamicKeysDetected && dynamicKeyStrategy === "keep-all") {
|
|
1127
|
-
return true;
|
|
1128
|
-
}
|
|
1129
|
-
if (safelistPatterns.some((pattern) => matchSafelistPattern(keyPath, pattern))) {
|
|
1130
|
-
return true;
|
|
1131
|
-
}
|
|
1132
|
-
if (usedKeys.has(keyPath)) {
|
|
1133
|
-
return true;
|
|
1134
|
-
}
|
|
1135
|
-
for (const key of usedKeys) {
|
|
1136
|
-
if (key.startsWith(keyPath + ".")) {
|
|
1137
|
-
return true;
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
return false;
|
|
1141
|
-
},
|
|
1142
|
-
reportRemoved(filename, keyPath) {
|
|
1143
|
-
const existing = removedKeys.get(filename);
|
|
1144
|
-
if (existing) {
|
|
1145
|
-
existing.push(keyPath);
|
|
1146
|
-
} else {
|
|
1147
|
-
removedKeys.set(filename, [keyPath]);
|
|
1148
|
-
}
|
|
1149
|
-
},
|
|
1150
|
-
getDiagnostics() {
|
|
1151
|
-
let totalRemoved = 0;
|
|
1152
|
-
for (const keys of removedKeys.values()) {
|
|
1153
|
-
totalRemoved += keys.length;
|
|
1154
|
-
}
|
|
1155
|
-
return { totalRemoved, byFile: removedKeys };
|
|
1156
|
-
}
|
|
1157
|
-
};
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
const I18N_FUNCTIONS = /* @__PURE__ */ new Set(["t", "$t", "tc", "$tc", "te", "$te", "d", "$d", "n", "$n"]);
|
|
1161
|
-
const TEMPLATE_T_REGEX = /\$?t\s*\(\s*['"]([^'"]+)['"]/g;
|
|
1162
|
-
const VT_DIRECTIVE_STRING_REGEX = /v-t\s*=\s*"'([^']+)'"/g;
|
|
1163
|
-
const VT_DIRECTIVE_PATH_REGEX = /v-t\s*=\s*"\{[^}]*path\s*:\s*'([^']+)'/g;
|
|
1164
|
-
const TEMPLATE_DYNAMIC_T_REGEX = /\$?t\s*\(\s*[a-zA-Z_$]/g;
|
|
1165
|
-
async function extractKeysFromScript(source) {
|
|
1166
|
-
const keys = [];
|
|
1167
|
-
let hasDynamic = false;
|
|
1168
|
-
let tsEstree;
|
|
1169
|
-
try {
|
|
1170
|
-
tsEstree = await import('@typescript-eslint/typescript-estree');
|
|
1171
|
-
} catch {
|
|
1172
|
-
return extractKeysFromScriptRegex(source);
|
|
1173
|
-
}
|
|
1174
|
-
try {
|
|
1175
|
-
const ast = tsEstree.parse(source, {
|
|
1176
|
-
range: true,
|
|
1177
|
-
jsx: true,
|
|
1178
|
-
allowInvalidAST: true,
|
|
1179
|
-
suppressDeprecatedPropertyWarnings: true
|
|
1180
|
-
});
|
|
1181
|
-
tsEstree.simpleTraverse(ast, {
|
|
1182
|
-
enter(node) {
|
|
1183
|
-
if (node.type !== tsEstree.AST_NODE_TYPES.CallExpression) {
|
|
1184
|
-
return;
|
|
1185
|
-
}
|
|
1186
|
-
const calleeName = getCalleeName(node, tsEstree.AST_NODE_TYPES);
|
|
1187
|
-
if (!calleeName || !I18N_FUNCTIONS.has(calleeName)) {
|
|
1188
|
-
return;
|
|
1189
|
-
}
|
|
1190
|
-
const firstArg = node.arguments[0];
|
|
1191
|
-
if (!firstArg) {
|
|
1192
|
-
return;
|
|
1193
|
-
}
|
|
1194
|
-
if (firstArg.type === tsEstree.AST_NODE_TYPES.Literal && typeof firstArg.value === "string") {
|
|
1195
|
-
keys.push(firstArg.value);
|
|
1196
|
-
} else if (firstArg.type === tsEstree.AST_NODE_TYPES.TemplateLiteral && firstArg.expressions.length === 0 && firstArg.quasis.length === 1) {
|
|
1197
|
-
const value = firstArg.quasis[0].value.cooked;
|
|
1198
|
-
if (value) {
|
|
1199
|
-
keys.push(value);
|
|
1200
|
-
}
|
|
1201
|
-
} else {
|
|
1202
|
-
hasDynamic = true;
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
});
|
|
1206
|
-
} catch {
|
|
1207
|
-
return extractKeysFromScriptRegex(source);
|
|
1208
|
-
}
|
|
1209
|
-
return { keys, hasDynamic };
|
|
1210
|
-
}
|
|
1211
|
-
function getCalleeName(node, AST_NODE_TYPES) {
|
|
1212
|
-
const callee = node.callee;
|
|
1213
|
-
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
1214
|
-
return callee.name;
|
|
1215
|
-
}
|
|
1216
|
-
if (callee.type === AST_NODE_TYPES.MemberExpression && callee.property.type === AST_NODE_TYPES.Identifier) {
|
|
1217
|
-
return callee.property.name;
|
|
1218
|
-
}
|
|
1219
|
-
return null;
|
|
1220
|
-
}
|
|
1221
|
-
function extractKeysFromScriptRegex(source) {
|
|
1222
|
-
const keys = [];
|
|
1223
|
-
let match;
|
|
1224
|
-
const regex = /\b(?:\$?t|tc|\$tc|te|\$te|d|\$d|n|\$n)\s*\(\s*['"]([^'"]+)['"]/g;
|
|
1225
|
-
while ((match = regex.exec(source)) !== null) {
|
|
1226
|
-
keys.push(match[1]);
|
|
1227
|
-
}
|
|
1228
|
-
return { keys, hasDynamic: false };
|
|
1229
|
-
}
|
|
1230
|
-
function extractKeysFromTemplate(templateContent) {
|
|
1231
|
-
const keys = [];
|
|
1232
|
-
let hasDynamic = false;
|
|
1233
|
-
let match;
|
|
1234
|
-
TEMPLATE_T_REGEX.lastIndex = 0;
|
|
1235
|
-
VT_DIRECTIVE_STRING_REGEX.lastIndex = 0;
|
|
1236
|
-
VT_DIRECTIVE_PATH_REGEX.lastIndex = 0;
|
|
1237
|
-
TEMPLATE_DYNAMIC_T_REGEX.lastIndex = 0;
|
|
1238
|
-
while ((match = TEMPLATE_T_REGEX.exec(templateContent)) !== null) {
|
|
1239
|
-
keys.push(match[1]);
|
|
1240
|
-
}
|
|
1241
|
-
while ((match = VT_DIRECTIVE_STRING_REGEX.exec(templateContent)) !== null) {
|
|
1242
|
-
keys.push(match[1]);
|
|
1243
|
-
}
|
|
1244
|
-
while ((match = VT_DIRECTIVE_PATH_REGEX.exec(templateContent)) !== null) {
|
|
1245
|
-
keys.push(match[1]);
|
|
1246
|
-
}
|
|
1247
|
-
if (TEMPLATE_DYNAMIC_T_REGEX.test(templateContent)) {
|
|
1248
|
-
hasDynamic = true;
|
|
1249
|
-
}
|
|
1250
|
-
return { keys, hasDynamic };
|
|
1251
|
-
}
|
|
1252
|
-
async function analyzeVueSFC(content) {
|
|
1253
|
-
const keys = [];
|
|
1254
|
-
let hasDynamic = false;
|
|
1255
|
-
let parse;
|
|
1256
|
-
try {
|
|
1257
|
-
const compilerSfc = await import('vue/compiler-sfc');
|
|
1258
|
-
parse = compilerSfc.parse;
|
|
1259
|
-
} catch {
|
|
1260
|
-
const templateResult = extractKeysFromTemplate(content);
|
|
1261
|
-
const scriptResult = await extractKeysFromScript(content);
|
|
1262
|
-
return {
|
|
1263
|
-
keys: [...templateResult.keys, ...scriptResult.keys],
|
|
1264
|
-
hasDynamic: templateResult.hasDynamic || scriptResult.hasDynamic
|
|
1265
|
-
};
|
|
1266
|
-
}
|
|
1267
|
-
const { descriptor } = parse(content);
|
|
1268
|
-
if (descriptor.template?.content) {
|
|
1269
|
-
const templateResult = extractKeysFromTemplate(descriptor.template.content);
|
|
1270
|
-
keys.push(...templateResult.keys);
|
|
1271
|
-
if (templateResult.hasDynamic) {
|
|
1272
|
-
hasDynamic = true;
|
|
1273
|
-
}
|
|
1274
|
-
}
|
|
1275
|
-
const scriptContent = descriptor.scriptSetup?.content || descriptor.script?.content;
|
|
1276
|
-
if (scriptContent) {
|
|
1277
|
-
const scriptResult = await extractKeysFromScript(scriptContent);
|
|
1278
|
-
keys.push(...scriptResult.keys);
|
|
1279
|
-
if (scriptResult.hasDynamic) {
|
|
1280
|
-
hasDynamic = true;
|
|
1281
|
-
}
|
|
1282
|
-
}
|
|
1283
|
-
return { keys, hasDynamic };
|
|
1284
|
-
}
|
|
1285
|
-
async function analyzeFile(content, filePath, collector) {
|
|
1286
|
-
let result;
|
|
1287
|
-
if (filePath.endsWith(".vue")) {
|
|
1288
|
-
result = await analyzeVueSFC(content);
|
|
1289
|
-
} else {
|
|
1290
|
-
result = await extractKeysFromScript(content);
|
|
1291
|
-
}
|
|
1292
|
-
for (const key of result.keys) {
|
|
1293
|
-
collector.addKey(key);
|
|
1294
|
-
}
|
|
1295
|
-
if (result.hasDynamic) {
|
|
1296
|
-
collector.markDynamic();
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
|
|
1300
|
-
const debug$1 = createDebug(resolveNamespace("tree-shaking"));
|
|
1301
|
-
function treeShakingPlugin(resolvedOptions, collector) {
|
|
1302
|
-
let projectRoot = "";
|
|
1303
|
-
return {
|
|
1304
|
-
name: resolveNamespace("tree-shaking"),
|
|
1305
|
-
enforce: "pre",
|
|
1306
|
-
vite: {
|
|
1307
|
-
configResolved(config) {
|
|
1308
|
-
projectRoot = config.root;
|
|
1309
|
-
}
|
|
1310
|
-
},
|
|
1311
|
-
webpack(compiler) {
|
|
1312
|
-
projectRoot = compiler.options.context || process.cwd();
|
|
1313
|
-
},
|
|
1314
|
-
async buildStart() {
|
|
1315
|
-
const treeShaking = resolvedOptions.treeShaking;
|
|
1316
|
-
const patterns = treeShaking?.scanPatterns || [`${projectRoot}/src/**/*.{vue,ts,js,tsx,jsx}`];
|
|
1317
|
-
debug$1("scanning patterns:", patterns);
|
|
1318
|
-
const files = await fg(patterns, {
|
|
1319
|
-
ignore: ["**/node_modules/**"],
|
|
1320
|
-
absolute: true
|
|
1321
|
-
});
|
|
1322
|
-
debug$1(`found ${files.length} source files to scan`);
|
|
1323
|
-
for (const file of files) {
|
|
1324
|
-
try {
|
|
1325
|
-
const content = await promises.readFile(file, "utf-8");
|
|
1326
|
-
await analyzeFile(content, file, collector);
|
|
1327
|
-
} catch (err) {
|
|
1328
|
-
debug$1(`failed to analyze file: ${file}`, err);
|
|
1329
|
-
}
|
|
1330
|
-
}
|
|
1331
|
-
debug$1(
|
|
1332
|
-
`scan complete: ${collector.usedKeys.size} used keys found, dynamic: ${collector.dynamicKeysDetected}`
|
|
1333
|
-
);
|
|
1334
|
-
if (collector.dynamicKeysDetected) {
|
|
1335
|
-
const strategy = treeShaking?.dynamicKeyStrategy || "keep-all";
|
|
1336
|
-
if (strategy === "keep-all") {
|
|
1337
|
-
warn(
|
|
1338
|
-
"Tree-shaking: dynamic key usage detected. All keys will be preserved (dynamicKeyStrategy: keep-all)."
|
|
1339
|
-
);
|
|
1340
|
-
} else {
|
|
1341
|
-
warn(
|
|
1342
|
-
"Tree-shaking: dynamic key usage detected. Unused keys will still be removed (dynamicKeyStrategy: ignore)."
|
|
1343
|
-
);
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1346
|
-
},
|
|
1347
|
-
buildEnd() {
|
|
1348
|
-
const diagnostics = collector.getDiagnostics();
|
|
1349
|
-
if (diagnostics.totalRemoved > 0) {
|
|
1350
|
-
debug$1(`tree-shaking: removed ${diagnostics.totalRemoved} unused message keys`);
|
|
1351
|
-
for (const [file, keys] of diagnostics.byFile) {
|
|
1352
|
-
debug$1(` ${file}: removed ${keys.length} keys (${keys.join(", ")})`);
|
|
1353
|
-
}
|
|
1354
|
-
} else {
|
|
1355
|
-
debug$1("tree-shaking: no keys removed");
|
|
1356
|
-
}
|
|
1357
|
-
}
|
|
1358
|
-
};
|
|
1359
|
-
}
|
|
1360
|
-
|
|
1361
|
-
const debug = createDebug(resolveNamespace("root"));
|
|
1362
|
-
const unpluginFactory = (options = {}, meta) => {
|
|
1363
|
-
debug("meta framework", meta.framework);
|
|
1364
|
-
if (!["vite", "webpack"].includes(meta.framework)) {
|
|
1365
|
-
raiseError(`This plugin is supported 'vite' and 'webpack' only`);
|
|
1366
|
-
}
|
|
1367
|
-
debug("plugin options (resolving):", options);
|
|
1368
|
-
const resolvedOptions = resolveOptions(options);
|
|
1369
|
-
debug("plugin options (resolved):", resolvedOptions);
|
|
1370
|
-
const collector = resolvedOptions.treeShaking ? createUsedKeysCollector(resolvedOptions.treeShaking) : null;
|
|
1371
|
-
const plugins = [];
|
|
1372
|
-
if (resolvedOptions.treeShaking && collector) {
|
|
1373
|
-
plugins.push(treeShakingPlugin(resolvedOptions, collector));
|
|
1374
|
-
}
|
|
1375
|
-
plugins.push(resourcePlugin(resolvedOptions, meta, collector));
|
|
1376
|
-
if (resolvedOptions.optimizeTranslationDirective) {
|
|
1377
|
-
if (meta.framework === "webpack") {
|
|
1378
|
-
raiseError(
|
|
1379
|
-
`The 'optimizeTranslationDirective' option still is not supported for webpack.
|
|
1380
|
-
We are waiting for your Pull Request \u{1F642}.`
|
|
1381
|
-
);
|
|
1382
|
-
}
|
|
1383
|
-
plugins.push(directivePlugin(resolvedOptions));
|
|
1384
|
-
}
|
|
1385
|
-
return plugins;
|
|
1386
|
-
};
|
|
1387
|
-
const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
1388
|
-
|
|
1389
|
-
export { unplugin as default, unplugin, unpluginFactory };
|