@intlify/unplugin-vue-i18n 7.0.0-beta.1 → 12.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +447 -667
- package/lib/index.mjs +446 -668
- package/lib/rspack.cjs +10 -7
- package/lib/rspack.mjs +10 -7
- 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 +10 -7
- package/lib/vite.mjs +10 -7
- package/lib/webpack.cjs +10 -7
- package/lib/webpack.mjs +10 -7
- package/package.json +22 -14
package/lib/index.cjs
CHANGED
|
@@ -2,29 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const unplugin$1 = require('unplugin');
|
|
6
5
|
const createDebug = require('debug');
|
|
7
|
-
const
|
|
8
|
-
const path = require('node:path');
|
|
9
|
-
const pathe = require('pathe');
|
|
6
|
+
const unplugin$1 = require('unplugin');
|
|
10
7
|
const shared = require('@intlify/shared');
|
|
11
|
-
const bundleUtils = require('@intlify/bundle-utils');
|
|
12
|
-
const pluginutils = require('@rollup/pluginutils');
|
|
13
|
-
const fg = require('fast-glob');
|
|
14
|
-
const node_fs = require('node:fs');
|
|
15
|
-
const compilerSfc = require('vue/compiler-sfc');
|
|
16
8
|
const vueI18nExtensions = require('@intlify/vue-i18n-extensions');
|
|
17
9
|
const scopeManager = require('@typescript-eslint/scope-manager');
|
|
18
10
|
const typescriptEstree = require('@typescript-eslint/typescript-estree');
|
|
11
|
+
const path = require('node:path');
|
|
19
12
|
const eslintUitls = require('@eslint-community/eslint-utils');
|
|
13
|
+
const pc = require('picocolors');
|
|
14
|
+
const defu = require('defu');
|
|
15
|
+
const bundleUtils = require('@intlify/bundle-utils');
|
|
16
|
+
const pluginutils = require('@rollup/pluginutils');
|
|
17
|
+
const fg = require('fast-glob');
|
|
18
|
+
const knitwork = require('knitwork');
|
|
19
|
+
const mlly = require('mlly');
|
|
20
|
+
const node_crypto = require('node:crypto');
|
|
21
|
+
const fs = require('node:fs');
|
|
22
|
+
const compilerSfc = require('vue/compiler-sfc');
|
|
20
23
|
|
|
21
24
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
22
25
|
|
|
23
26
|
const createDebug__default = /*#__PURE__*/_interopDefaultCompat(createDebug);
|
|
24
|
-
const pc__default = /*#__PURE__*/_interopDefaultCompat(pc);
|
|
25
27
|
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
26
|
-
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
27
28
|
const eslintUitls__default = /*#__PURE__*/_interopDefaultCompat(eslintUitls);
|
|
29
|
+
const pc__default = /*#__PURE__*/_interopDefaultCompat(pc);
|
|
30
|
+
const defu__default = /*#__PURE__*/_interopDefaultCompat(defu);
|
|
31
|
+
const fg__default = /*#__PURE__*/_interopDefaultCompat(fg);
|
|
32
|
+
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
28
33
|
|
|
29
34
|
const PKG_NAME = "unplugin-vue-i18n";
|
|
30
35
|
|
|
@@ -62,64 +67,26 @@ function normalizePath(id) {
|
|
|
62
67
|
return path__default.posix.normalize(isWindows ? slash(id) : id);
|
|
63
68
|
}
|
|
64
69
|
|
|
65
|
-
function
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
} else if (shared.isString(include)) {
|
|
77
|
-
include = pathe.normalize(include);
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
exclude = "**/**";
|
|
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
81
|
}
|
|
82
|
-
|
|
83
|
-
const defaultSFCLang = shared.isString(options.defaultSFCLang) ? options.defaultSFCLang : "json";
|
|
84
|
-
const globalSFCScope = !!options.globalSFCScope;
|
|
85
|
-
const runtimeOnly = shared.isBoolean(options.runtimeOnly) ? options.runtimeOnly : true;
|
|
86
|
-
const dropMessageCompiler = !!options.dropMessageCompiler;
|
|
87
|
-
const fullInstall = moduleType === "vue-i18n" ? shared.isBoolean(options.fullInstall) ? options.fullInstall : true : false;
|
|
88
|
-
const ssrBuild = !!options.ssr;
|
|
89
|
-
const allowDynamic = !!options.allowDynamic;
|
|
90
|
-
const strictMessage = shared.isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
91
|
-
const escapeHtml = !!options.escapeHtml;
|
|
92
|
-
const optimizeTranslationDirective = shared.isString(options.optimizeTranslationDirective) || shared.isArray(options.optimizeTranslationDirective) ? options.optimizeTranslationDirective : !!options.optimizeTranslationDirective;
|
|
93
|
-
const translationIdentifiers = /* @__PURE__ */ new Map();
|
|
94
|
-
const transformI18nBlock = typeof options.transformI18nBlock === "function" ? options.transformI18nBlock : null;
|
|
95
|
-
return {
|
|
96
|
-
include,
|
|
97
|
-
exclude,
|
|
98
|
-
module: moduleType,
|
|
99
|
-
onlyLocales,
|
|
100
|
-
forceStringify,
|
|
101
|
-
defaultSFCLang,
|
|
102
|
-
globalSFCScope,
|
|
103
|
-
runtimeOnly,
|
|
104
|
-
dropMessageCompiler,
|
|
105
|
-
fullInstall,
|
|
106
|
-
ssrBuild,
|
|
107
|
-
allowDynamic,
|
|
108
|
-
strictMessage,
|
|
109
|
-
escapeHtml,
|
|
110
|
-
optimizeTranslationDirective,
|
|
111
|
-
translationIdentifiers,
|
|
112
|
-
transformI18nBlock
|
|
113
|
-
};
|
|
82
|
+
return descriptor;
|
|
114
83
|
}
|
|
115
84
|
|
|
116
85
|
function parseVueRequest(id) {
|
|
117
86
|
const [filename, rawQuery] = id.split(`?`, 2);
|
|
118
87
|
const params = new URLSearchParams(rawQuery);
|
|
119
88
|
const ret = {};
|
|
120
|
-
const langPart = Object.keys(Object.fromEntries(params)).find(
|
|
121
|
-
(key) => /lang\./i.test(key)
|
|
122
|
-
);
|
|
89
|
+
const langPart = Object.keys(Object.fromEntries(params)).find((key) => /lang\./i.test(key));
|
|
123
90
|
ret.vue = params.has("vue");
|
|
124
91
|
ret.global = params.has("global");
|
|
125
92
|
ret.src = params.has("src");
|
|
@@ -163,587 +130,7 @@ function getVuePluginOptions(vuePlugin) {
|
|
|
163
130
|
};
|
|
164
131
|
}
|
|
165
132
|
|
|
166
|
-
|
|
167
|
-
const { descriptor, errors } = compiler.parse(source, {
|
|
168
|
-
filename,
|
|
169
|
-
templateParseOptions: template?.compilerOptions
|
|
170
|
-
});
|
|
171
|
-
return { descriptor, errors };
|
|
172
|
-
}
|
|
173
|
-
function getDescriptor(filename, code, options) {
|
|
174
|
-
const { descriptor, errors } = createDescriptor(filename, code, options);
|
|
175
|
-
if (errors.length) {
|
|
176
|
-
throw errors[0];
|
|
177
|
-
}
|
|
178
|
-
return descriptor;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
|
|
182
|
-
const VIRTUAL_PREFIX = "\0";
|
|
183
|
-
const debug$2 = createDebug__default(resolveNamespace("resource"));
|
|
184
|
-
function resourcePlugin({
|
|
185
|
-
onlyLocales,
|
|
186
|
-
include,
|
|
187
|
-
exclude,
|
|
188
|
-
module,
|
|
189
|
-
forceStringify,
|
|
190
|
-
defaultSFCLang,
|
|
191
|
-
globalSFCScope,
|
|
192
|
-
runtimeOnly,
|
|
193
|
-
dropMessageCompiler,
|
|
194
|
-
fullInstall,
|
|
195
|
-
ssrBuild,
|
|
196
|
-
strictMessage,
|
|
197
|
-
allowDynamic,
|
|
198
|
-
escapeHtml,
|
|
199
|
-
transformI18nBlock
|
|
200
|
-
}, meta) {
|
|
201
|
-
const filter = pluginutils.createFilter(include, exclude);
|
|
202
|
-
const getVueI18nAliasPath = ({ ssr = false, runtimeOnly: runtimeOnly2 = false }) => {
|
|
203
|
-
return `${module}/dist/${module}${runtimeOnly2 ? ".runtime" : ""}.${!ssr ? "esm-bundler.js" : "node.js"}`;
|
|
204
|
-
};
|
|
205
|
-
let isProduction = false;
|
|
206
|
-
let sourceMap = false;
|
|
207
|
-
const vueI18nAliasName = module;
|
|
208
|
-
debug$2(`vue-i18n alias name: ${vueI18nAliasName}`);
|
|
209
|
-
let vuePlugin = null;
|
|
210
|
-
const getSfcParser = () => {
|
|
211
|
-
return vuePlugin ? getVueCompiler(vuePlugin).parse : compilerSfc.parse;
|
|
212
|
-
};
|
|
213
|
-
return {
|
|
214
|
-
name: resolveNamespace("resource"),
|
|
215
|
-
/**
|
|
216
|
-
* NOTE:
|
|
217
|
-
*
|
|
218
|
-
* For vite, If we have json (including SFC's custom block),
|
|
219
|
-
* transform it first because it will be transformed into javascript code by `vite:json` plugin.
|
|
220
|
-
*
|
|
221
|
-
* For webpack, This plugin will handle with ‘post’, because vue-loader generate the request query.
|
|
222
|
-
*/
|
|
223
|
-
enforce: meta.framework === "vite" ? "pre" : "post",
|
|
224
|
-
vite: {
|
|
225
|
-
config() {
|
|
226
|
-
const defineConfig = {
|
|
227
|
-
define: {
|
|
228
|
-
__VUE_I18N_FULL_INSTALL__: fullInstall,
|
|
229
|
-
__INTLIFY_DROP_MESSAGE_COMPILER__: dropMessageCompiler,
|
|
230
|
-
__VUE_I18N_PROD_DEVTOOLS__: false
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
debug$2("define Config:", defineConfig);
|
|
234
|
-
const aliasConfig = {
|
|
235
|
-
resolve: {
|
|
236
|
-
alias: {
|
|
237
|
-
[vueI18nAliasName]: getVueI18nAliasPath({
|
|
238
|
-
ssr: ssrBuild,
|
|
239
|
-
runtimeOnly
|
|
240
|
-
})
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
debug$2("alias Config:", aliasConfig);
|
|
245
|
-
return shared.assign(defineConfig, aliasConfig);
|
|
246
|
-
},
|
|
247
|
-
configResolved(config) {
|
|
248
|
-
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
249
|
-
if (!checkVuePlugin(vuePlugin)) {
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
isProduction = config.isProduction;
|
|
253
|
-
sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
|
|
254
|
-
debug$2(
|
|
255
|
-
`configResolved: isProduction = ${isProduction}, sourceMap = ${sourceMap}`
|
|
256
|
-
);
|
|
257
|
-
const jsonPlugin = getVitePlugin(config, "vite:json");
|
|
258
|
-
if (jsonPlugin) {
|
|
259
|
-
const orgTransform = jsonPlugin.transform;
|
|
260
|
-
jsonPlugin.transform = async function(code, id) {
|
|
261
|
-
if (!/\.json$/.test(id) || filter(id)) {
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
const { query } = parseVueRequest(id);
|
|
265
|
-
if (query.vue) {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
debug$2("org json plugin");
|
|
269
|
-
return orgTransform.apply(this, [code, id]);
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
const esbuildPlugin = getVitePlugin(config, "vite:esbuild");
|
|
273
|
-
if (esbuildPlugin) {
|
|
274
|
-
const orgTransform = esbuildPlugin.transform;
|
|
275
|
-
esbuildPlugin.transform = async function(code, id) {
|
|
276
|
-
const result = await orgTransform.apply(this, [
|
|
277
|
-
code,
|
|
278
|
-
id
|
|
279
|
-
]);
|
|
280
|
-
if (result == null) {
|
|
281
|
-
return result;
|
|
282
|
-
}
|
|
283
|
-
const { filename, query } = parseVueRequest(id);
|
|
284
|
-
if (!query.vue && filter(id) && /\.[c|m]?ts$/.test(id)) {
|
|
285
|
-
const [_code, inSourceMap] = shared.isString(result) ? [result, void 0] : [result.code, result.map];
|
|
286
|
-
let langInfo = defaultSFCLang;
|
|
287
|
-
langInfo = pathe.parse(filename).ext;
|
|
288
|
-
const generate = getGenerator(langInfo);
|
|
289
|
-
const parseOptions = getOptions(
|
|
290
|
-
filename,
|
|
291
|
-
isProduction,
|
|
292
|
-
query,
|
|
293
|
-
sourceMap,
|
|
294
|
-
{
|
|
295
|
-
inSourceMap,
|
|
296
|
-
isGlobal: globalSFCScope,
|
|
297
|
-
allowDynamic,
|
|
298
|
-
strictMessage,
|
|
299
|
-
escapeHtml,
|
|
300
|
-
jit: true,
|
|
301
|
-
onlyLocales,
|
|
302
|
-
forceStringify
|
|
303
|
-
}
|
|
304
|
-
);
|
|
305
|
-
debug$2("parseOptions", parseOptions);
|
|
306
|
-
const { code: generatedCode, map } = generate(_code, parseOptions);
|
|
307
|
-
debug$2("generated code", generatedCode);
|
|
308
|
-
debug$2("sourcemap", map, sourceMap);
|
|
309
|
-
if (_code === generatedCode)
|
|
310
|
-
return;
|
|
311
|
-
return {
|
|
312
|
-
code: generatedCode,
|
|
313
|
-
map: { mappings: "" }
|
|
314
|
-
};
|
|
315
|
-
} else {
|
|
316
|
-
return result;
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
},
|
|
321
|
-
async handleHotUpdate({ file, server }) {
|
|
322
|
-
if (/\.(json5?|ya?ml)$/.test(file)) {
|
|
323
|
-
const module2 = server.moduleGraph.getModuleById(
|
|
324
|
-
asVirtualId(INTLIFY_BUNDLE_IMPORT_ID, meta.framework)
|
|
325
|
-
);
|
|
326
|
-
if (module2) {
|
|
327
|
-
server.moduleGraph.invalidateModule(module2);
|
|
328
|
-
return [module2];
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
},
|
|
333
|
-
webpack(compiler) {
|
|
334
|
-
webpackLike(compiler, "webpack", {
|
|
335
|
-
isProduction,
|
|
336
|
-
meta,
|
|
337
|
-
sourceMap,
|
|
338
|
-
getVueI18nAliasPath,
|
|
339
|
-
vueI18nAliasName,
|
|
340
|
-
ssrBuild,
|
|
341
|
-
runtimeOnly,
|
|
342
|
-
fullInstall,
|
|
343
|
-
filter
|
|
344
|
-
});
|
|
345
|
-
},
|
|
346
|
-
rspack(compiler) {
|
|
347
|
-
webpackLike(compiler, "rspack", {
|
|
348
|
-
isProduction,
|
|
349
|
-
meta,
|
|
350
|
-
sourceMap,
|
|
351
|
-
getVueI18nAliasPath,
|
|
352
|
-
vueI18nAliasName,
|
|
353
|
-
ssrBuild,
|
|
354
|
-
runtimeOnly,
|
|
355
|
-
fullInstall,
|
|
356
|
-
filter
|
|
357
|
-
});
|
|
358
|
-
},
|
|
359
|
-
resolveId(id, importer) {
|
|
360
|
-
debug$2("resolveId", id, importer);
|
|
361
|
-
if (id === INTLIFY_BUNDLE_IMPORT_ID) {
|
|
362
|
-
return asVirtualId(id, meta.framework);
|
|
363
|
-
}
|
|
364
|
-
},
|
|
365
|
-
loadInclude(id) {
|
|
366
|
-
return INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework);
|
|
367
|
-
},
|
|
368
|
-
async load(id) {
|
|
369
|
-
debug$2("load", id);
|
|
370
|
-
if (INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework) && include) {
|
|
371
|
-
let resourcePaths = [];
|
|
372
|
-
const includePaths = shared.isArray(include) ? include : [include];
|
|
373
|
-
for (const inc of includePaths) {
|
|
374
|
-
resourcePaths = [...resourcePaths, ...await fg__default(inc)];
|
|
375
|
-
}
|
|
376
|
-
resourcePaths = resourcePaths.filter(
|
|
377
|
-
(el, pos) => resourcePaths.indexOf(el) === pos
|
|
378
|
-
);
|
|
379
|
-
const code = await generateBundleResources(
|
|
380
|
-
resourcePaths,
|
|
381
|
-
isProduction,
|
|
382
|
-
{
|
|
383
|
-
forceStringify,
|
|
384
|
-
strictMessage,
|
|
385
|
-
escapeHtml
|
|
386
|
-
}
|
|
387
|
-
);
|
|
388
|
-
return {
|
|
389
|
-
code,
|
|
390
|
-
map: { mappings: "" }
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
},
|
|
394
|
-
transformInclude(id) {
|
|
395
|
-
debug$2("transformInclude", id);
|
|
396
|
-
if (meta.framework === "vite") {
|
|
397
|
-
return true;
|
|
398
|
-
} else {
|
|
399
|
-
const { filename } = parseVueRequest(id);
|
|
400
|
-
return filename.endsWith("vue") || filename.endsWith(INTLIFY_BUNDLE_IMPORT_ID) || /\.(json5?|ya?ml)$/.test(filename) && filter(filename);
|
|
401
|
-
}
|
|
402
|
-
},
|
|
403
|
-
async transform(code, id) {
|
|
404
|
-
const { filename, query } = parseVueRequest(id);
|
|
405
|
-
debug$2("transform", id, JSON.stringify(query), filename);
|
|
406
|
-
let langInfo = defaultSFCLang;
|
|
407
|
-
let inSourceMap;
|
|
408
|
-
if (!query.vue) {
|
|
409
|
-
if (/\.(json5?|ya?ml|[c|m]?js)$/.test(id) && filter(id)) {
|
|
410
|
-
langInfo = pathe.parse(filename).ext;
|
|
411
|
-
const generate = getGenerator(langInfo);
|
|
412
|
-
const parseOptions = getOptions(
|
|
413
|
-
filename,
|
|
414
|
-
isProduction,
|
|
415
|
-
query,
|
|
416
|
-
sourceMap,
|
|
417
|
-
{
|
|
418
|
-
inSourceMap,
|
|
419
|
-
isGlobal: globalSFCScope,
|
|
420
|
-
allowDynamic,
|
|
421
|
-
strictMessage,
|
|
422
|
-
escapeHtml,
|
|
423
|
-
jit: true,
|
|
424
|
-
onlyLocales,
|
|
425
|
-
forceStringify
|
|
426
|
-
}
|
|
427
|
-
);
|
|
428
|
-
debug$2("parseOptions", parseOptions);
|
|
429
|
-
const { code: generatedCode, map } = generate(code, parseOptions);
|
|
430
|
-
debug$2("generated code", generatedCode);
|
|
431
|
-
debug$2("sourcemap", map, sourceMap);
|
|
432
|
-
if (code === generatedCode)
|
|
433
|
-
return;
|
|
434
|
-
return {
|
|
435
|
-
code: generatedCode,
|
|
436
|
-
// prettier-ignore
|
|
437
|
-
map: { mappings: "" }
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
} else {
|
|
441
|
-
if (isCustomBlock(query)) {
|
|
442
|
-
if (shared.isString(query.lang)) {
|
|
443
|
-
langInfo = query.src ? query.lang === "i18n" ? defaultSFCLang : query.lang : query.lang;
|
|
444
|
-
} else if (defaultSFCLang) {
|
|
445
|
-
langInfo = defaultSFCLang;
|
|
446
|
-
}
|
|
447
|
-
debug$2("langInfo", langInfo);
|
|
448
|
-
const generate = /\.?json5?/.test(langInfo) ? bundleUtils.generateJSON : bundleUtils.generateYAML;
|
|
449
|
-
const parseOptions = getOptions(
|
|
450
|
-
filename,
|
|
451
|
-
isProduction,
|
|
452
|
-
query,
|
|
453
|
-
sourceMap,
|
|
454
|
-
{
|
|
455
|
-
inSourceMap,
|
|
456
|
-
isGlobal: globalSFCScope,
|
|
457
|
-
jit: true,
|
|
458
|
-
strictMessage,
|
|
459
|
-
escapeHtml,
|
|
460
|
-
onlyLocales,
|
|
461
|
-
forceStringify
|
|
462
|
-
}
|
|
463
|
-
);
|
|
464
|
-
debug$2("parseOptions", parseOptions);
|
|
465
|
-
let source = await getCode(
|
|
466
|
-
code,
|
|
467
|
-
filename,
|
|
468
|
-
sourceMap,
|
|
469
|
-
query,
|
|
470
|
-
getSfcParser(),
|
|
471
|
-
meta.framework
|
|
472
|
-
);
|
|
473
|
-
if (typeof transformI18nBlock === "function") {
|
|
474
|
-
const modifiedSource = transformI18nBlock(source);
|
|
475
|
-
if (modifiedSource && typeof modifiedSource === "string") {
|
|
476
|
-
source = modifiedSource;
|
|
477
|
-
} else {
|
|
478
|
-
warn("transformI18nBlock should return a string");
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
const { code: generatedCode, map } = generate(source, parseOptions);
|
|
482
|
-
debug$2("generated code", generatedCode);
|
|
483
|
-
debug$2("sourcemap", map, sourceMap);
|
|
484
|
-
if (code === generatedCode)
|
|
485
|
-
return;
|
|
486
|
-
return {
|
|
487
|
-
code: generatedCode,
|
|
488
|
-
// prettier-ignore
|
|
489
|
-
map: { mappings: "" }
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
};
|
|
495
|
-
}
|
|
496
|
-
function getGenerator(ext, defaultGen = bundleUtils.generateJSON) {
|
|
497
|
-
return /\.?json5?$/.test(ext) ? bundleUtils.generateJSON : /\.ya?ml$/.test(ext) ? bundleUtils.generateYAML : /\.([c|m]?js|[c|m]?ts)$/.test(ext) ? bundleUtils.generateJavaScript : defaultGen;
|
|
498
|
-
}
|
|
499
|
-
function webpackLike(compiler, framework, {
|
|
500
|
-
isProduction,
|
|
501
|
-
sourceMap,
|
|
502
|
-
meta,
|
|
503
|
-
getVueI18nAliasPath,
|
|
504
|
-
vueI18nAliasName,
|
|
505
|
-
ssrBuild,
|
|
506
|
-
runtimeOnly,
|
|
507
|
-
fullInstall,
|
|
508
|
-
filter
|
|
509
|
-
}) {
|
|
510
|
-
isProduction = compiler.options.mode !== "development";
|
|
511
|
-
sourceMap = !!compiler.options.devtool;
|
|
512
|
-
debug$2(
|
|
513
|
-
`${framework}: isProduction = ${isProduction}, sourceMap = ${sourceMap}`
|
|
514
|
-
);
|
|
515
|
-
compiler.options.resolve = normalizeConfigResolveAlias(
|
|
516
|
-
compiler.options.resolve,
|
|
517
|
-
meta.framework
|
|
518
|
-
);
|
|
519
|
-
compiler.options.resolve.alias[vueI18nAliasName] = getVueI18nAliasPath({
|
|
520
|
-
ssr: ssrBuild,
|
|
521
|
-
runtimeOnly
|
|
522
|
-
});
|
|
523
|
-
debug$2(
|
|
524
|
-
`set ${vueI18nAliasName}: ${getVueI18nAliasPath({
|
|
525
|
-
ssr: ssrBuild,
|
|
526
|
-
runtimeOnly
|
|
527
|
-
})}`
|
|
528
|
-
);
|
|
529
|
-
const loader = framework === "webpack" ? loadWebpack : loadRspack;
|
|
530
|
-
loader().then((mod) => {
|
|
531
|
-
if (mod) {
|
|
532
|
-
compiler.options.plugins.push(
|
|
533
|
-
// @ts-expect-error type issue
|
|
534
|
-
new mod.DefinePlugin({
|
|
535
|
-
__VUE_I18N_FULL_INSTALL__: JSON.stringify(fullInstall),
|
|
536
|
-
__INTLIFY_PROD_DEVTOOLS__: "false"
|
|
537
|
-
})
|
|
538
|
-
);
|
|
539
|
-
debug$2(`set __VUE_I18N_FULL_INSTALL__ is '${fullInstall}'`);
|
|
540
|
-
} else {
|
|
541
|
-
debug$2(`ignore vue-i18n feature flags with ${framework}.DefinePlugin`);
|
|
542
|
-
}
|
|
543
|
-
});
|
|
544
|
-
if (compiler.options.module) {
|
|
545
|
-
compiler.options.module.rules.push({
|
|
546
|
-
test: /\.(json5?|ya?ml)$/,
|
|
547
|
-
type: "javascript/auto",
|
|
548
|
-
include(resource) {
|
|
549
|
-
return filter(resource);
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
function normalizeConfigResolveAlias(resolve, framework) {
|
|
555
|
-
if (resolve && resolve.alias) {
|
|
556
|
-
return resolve;
|
|
557
|
-
}
|
|
558
|
-
const webpackLike2 = framework === "webpack" || framework === "rspack";
|
|
559
|
-
if (!resolve) {
|
|
560
|
-
if (framework === "vite") {
|
|
561
|
-
return { alias: [] };
|
|
562
|
-
} else if (webpackLike2) {
|
|
563
|
-
return { alias: {} };
|
|
564
|
-
}
|
|
565
|
-
} else if (!resolve.alias) {
|
|
566
|
-
if (framework === "vite") {
|
|
567
|
-
resolve.alias = [];
|
|
568
|
-
return resolve;
|
|
569
|
-
} else if (webpackLike2) {
|
|
570
|
-
resolve.alias = {};
|
|
571
|
-
return resolve;
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
async function loadWebpack() {
|
|
576
|
-
try {
|
|
577
|
-
const mod = await import('webpack');
|
|
578
|
-
return mod.default || mod;
|
|
579
|
-
} catch (_e) {
|
|
580
|
-
warn(`webpack not found, please install webpack.`);
|
|
581
|
-
}
|
|
582
|
-
return null;
|
|
583
|
-
}
|
|
584
|
-
async function loadRspack() {
|
|
585
|
-
try {
|
|
586
|
-
const { rspack } = await import('@rspack/core');
|
|
587
|
-
return rspack;
|
|
588
|
-
} catch (_e) {
|
|
589
|
-
warn(`rspack not found, please install rspack.`);
|
|
590
|
-
}
|
|
591
|
-
return null;
|
|
592
|
-
}
|
|
593
|
-
async function generateBundleResources(resources, isProduction, {
|
|
594
|
-
forceStringify = false,
|
|
595
|
-
isGlobal = false,
|
|
596
|
-
onlyLocales = [],
|
|
597
|
-
strictMessage = true,
|
|
598
|
-
escapeHtml = false,
|
|
599
|
-
jit = true,
|
|
600
|
-
transformI18nBlock = void 0
|
|
601
|
-
}) {
|
|
602
|
-
const codes = [];
|
|
603
|
-
for (const res of resources) {
|
|
604
|
-
debug$2(`${res} bundle loading ...`);
|
|
605
|
-
if (/\.(json5?|ya?ml)$/.test(res)) {
|
|
606
|
-
const { ext, name } = pathe.parse(res);
|
|
607
|
-
const source = await getRaw(res);
|
|
608
|
-
const generate = /json5?/.test(ext) ? bundleUtils.generateJSON : bundleUtils.generateYAML;
|
|
609
|
-
const parseOptions = getOptions(res, isProduction, {}, false, {
|
|
610
|
-
isGlobal,
|
|
611
|
-
jit,
|
|
612
|
-
onlyLocales,
|
|
613
|
-
strictMessage,
|
|
614
|
-
escapeHtml,
|
|
615
|
-
forceStringify,
|
|
616
|
-
transformI18nBlock
|
|
617
|
-
});
|
|
618
|
-
parseOptions.type = "bare";
|
|
619
|
-
const { code } = generate(source, parseOptions);
|
|
620
|
-
debug$2("generated code", code);
|
|
621
|
-
codes.push(`${JSON.stringify(name)}: ${code}`);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
return `const isObject = (item) => item && typeof item === 'object' && !Array.isArray(item);
|
|
625
|
-
|
|
626
|
-
const mergeDeep = (target, ...sources) => {
|
|
627
|
-
if (!sources.length) return target;
|
|
628
|
-
const source = sources.shift();
|
|
629
|
-
|
|
630
|
-
if (isObject(target) && isObject(source)) {
|
|
631
|
-
for (const key in source) {
|
|
632
|
-
if (isObject(source[key])) {
|
|
633
|
-
if (!target[key]) Object.assign(target, { [key]: {} });
|
|
634
|
-
mergeDeep(target[key], source[key]);
|
|
635
|
-
} else {
|
|
636
|
-
Object.assign(target, { [key]: source[key] });
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
return mergeDeep(target, ...sources);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
export default mergeDeep({},
|
|
645
|
-
${codes.map((code) => `{${code}}`).join(",\n")}
|
|
646
|
-
);`;
|
|
647
|
-
}
|
|
648
|
-
async function getCode(source, filename, sourceMap, query, parser, framework = "vite") {
|
|
649
|
-
const { index, issuerPath } = query;
|
|
650
|
-
if (!shared.isNumber(index)) {
|
|
651
|
-
raiseError(`unexpected index: ${index}`);
|
|
652
|
-
}
|
|
653
|
-
if (framework === "webpack" || framework === "rspack") {
|
|
654
|
-
if (issuerPath) {
|
|
655
|
-
debug$2(`getCode (${framework}) ${index} via issuerPath`, issuerPath);
|
|
656
|
-
return await getRaw(filename);
|
|
657
|
-
} else {
|
|
658
|
-
const result = parser(await getRaw(filename), {
|
|
659
|
-
sourceMap,
|
|
660
|
-
filename
|
|
661
|
-
});
|
|
662
|
-
const block = result.descriptor.customBlocks[index];
|
|
663
|
-
if (block) {
|
|
664
|
-
const code = block.src ? await getRaw(block.src) : block.content;
|
|
665
|
-
debug$2(`getCode (${framework}) ${index} from SFC`, code);
|
|
666
|
-
return code;
|
|
667
|
-
} else {
|
|
668
|
-
return source;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
} else {
|
|
672
|
-
return source;
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
function isCustomBlock(query) {
|
|
676
|
-
return !shared.isEmptyObject(query) && "vue" in query && (query["type"] === "custom" || // for vite (@vite-plugin-vue)
|
|
677
|
-
query["type"] === "i18n" || // for webpack (vue-loader)
|
|
678
|
-
query["blockType"] === "i18n");
|
|
679
|
-
}
|
|
680
|
-
function getOptions(filename, isProduction, query, sourceMap, {
|
|
681
|
-
inSourceMap = void 0,
|
|
682
|
-
forceStringify = false,
|
|
683
|
-
isGlobal = false,
|
|
684
|
-
onlyLocales = [],
|
|
685
|
-
allowDynamic = false,
|
|
686
|
-
strictMessage = true,
|
|
687
|
-
escapeHtml = false,
|
|
688
|
-
jit = true,
|
|
689
|
-
transformI18nBlock = null
|
|
690
|
-
}) {
|
|
691
|
-
const mode = isProduction ? "production" : "development";
|
|
692
|
-
const baseOptions = {
|
|
693
|
-
filename,
|
|
694
|
-
sourceMap,
|
|
695
|
-
inSourceMap,
|
|
696
|
-
forceStringify,
|
|
697
|
-
allowDynamic,
|
|
698
|
-
strictMessage,
|
|
699
|
-
escapeHtml,
|
|
700
|
-
jit,
|
|
701
|
-
onlyLocales,
|
|
702
|
-
env: mode,
|
|
703
|
-
transformI18nBlock,
|
|
704
|
-
onWarn: (msg) => {
|
|
705
|
-
warn(`${filename} ${msg}`);
|
|
706
|
-
},
|
|
707
|
-
onError: (msg, extra) => {
|
|
708
|
-
const codeFrame = shared.generateCodeFrame(
|
|
709
|
-
extra?.source || extra?.location?.source || "",
|
|
710
|
-
extra?.location?.start.column,
|
|
711
|
-
extra?.location?.end.column
|
|
712
|
-
);
|
|
713
|
-
const errMssage = `${msg} (error code: ${extra?.code}) in ${filename}
|
|
714
|
-
target message: ${extra?.source}
|
|
715
|
-
target message path: ${extra?.path}
|
|
716
|
-
|
|
717
|
-
${codeFrame}
|
|
718
|
-
`;
|
|
719
|
-
error(errMssage);
|
|
720
|
-
throw new Error(errMssage);
|
|
721
|
-
}
|
|
722
|
-
};
|
|
723
|
-
if (isCustomBlock(query)) {
|
|
724
|
-
return shared.assign(baseOptions, {
|
|
725
|
-
type: "sfc",
|
|
726
|
-
locale: shared.isString(query.locale) ? query.locale : "",
|
|
727
|
-
isGlobal: isGlobal || !!query.global
|
|
728
|
-
});
|
|
729
|
-
} else {
|
|
730
|
-
return shared.assign(baseOptions, {
|
|
731
|
-
type: "plain",
|
|
732
|
-
isGlobal: false
|
|
733
|
-
});
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
function getVirtualId(id, framework = "vite") {
|
|
737
|
-
return framework === "vite" ? id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : "" : id;
|
|
738
|
-
}
|
|
739
|
-
function asVirtualId(id, framework = "vite") {
|
|
740
|
-
return framework === "vite" ? VIRTUAL_PREFIX + id : id;
|
|
741
|
-
}
|
|
742
|
-
async function getRaw(path) {
|
|
743
|
-
return node_fs.promises.readFile(path, { encoding: "utf-8" });
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
const debug$1 = createDebug__default(resolveNamespace("directive"));
|
|
133
|
+
const debug$2 = createDebug__default(resolveNamespace("directive"));
|
|
747
134
|
function directivePlugin({
|
|
748
135
|
optimizeTranslationDirective,
|
|
749
136
|
translationIdentifiers
|
|
@@ -766,7 +153,7 @@ function directivePlugin({
|
|
|
766
153
|
optimizeTranslationDirective,
|
|
767
154
|
translationIdentifiers
|
|
768
155
|
);
|
|
769
|
-
debug$
|
|
156
|
+
debug$2(`vite:vue options['template']:`, vuePlugin.api.options);
|
|
770
157
|
}
|
|
771
158
|
},
|
|
772
159
|
configResolved(config) {
|
|
@@ -808,11 +195,9 @@ function resolveVueOptions(vuePlugin, optimizeTranslationDirective, translationI
|
|
|
808
195
|
const translationSignatureResolver = (context, baseResolver) => {
|
|
809
196
|
const { filename } = context;
|
|
810
197
|
const vuePluginOptions = getVuePluginOptions(vuePlugin);
|
|
811
|
-
const normalizedFilename = normalizePath(
|
|
812
|
-
path__default.relative(vuePluginOptions.root, filename)
|
|
813
|
-
);
|
|
198
|
+
const normalizedFilename = normalizePath(path__default.relative(vuePluginOptions.root, filename));
|
|
814
199
|
const resolveIdentifier2 = translationIdentifiers.get(normalizedFilename);
|
|
815
|
-
debug$
|
|
200
|
+
debug$2("resolved vue-i18n Identifier", resolveIdentifier2);
|
|
816
201
|
if (resolveIdentifier2 == null) {
|
|
817
202
|
return void 0;
|
|
818
203
|
}
|
|
@@ -830,7 +215,7 @@ function resolveVueOptions(vuePlugin, optimizeTranslationDirective, translationI
|
|
|
830
215
|
}
|
|
831
216
|
function analyzeIdentifiers(descriptor, { root }, translationIdentifiers) {
|
|
832
217
|
const source = descriptor.scriptSetup?.content || descriptor.script?.content;
|
|
833
|
-
debug$
|
|
218
|
+
debug$2("getDescriptor content", source);
|
|
834
219
|
if (!source) {
|
|
835
220
|
return;
|
|
836
221
|
}
|
|
@@ -848,13 +233,11 @@ function analyzeIdentifiers(descriptor, { root }, translationIdentifiers) {
|
|
|
848
233
|
if (importLocalName == null) {
|
|
849
234
|
return;
|
|
850
235
|
}
|
|
851
|
-
debug$
|
|
236
|
+
debug$2("importLocalName", importLocalName);
|
|
852
237
|
const resolvedIdentifier = getVueI18nIdentifier(scope, importLocalName);
|
|
853
238
|
if (resolvedIdentifier) {
|
|
854
|
-
const normalizedFilename = normalizePath(
|
|
855
|
-
|
|
856
|
-
);
|
|
857
|
-
debug$1("set vue-i18n resolved identifier: ", resolvedIdentifier);
|
|
239
|
+
const normalizedFilename = normalizePath(path__default.relative(root, descriptor.filename));
|
|
240
|
+
debug$2("set vue-i18n resolved identifier: ", resolvedIdentifier);
|
|
858
241
|
translationIdentifiers.set(normalizedFilename, resolvedIdentifier);
|
|
859
242
|
}
|
|
860
243
|
}
|
|
@@ -902,9 +285,9 @@ function getVueI18nIdentifier(scope, local) {
|
|
|
902
285
|
return null;
|
|
903
286
|
}
|
|
904
287
|
const variableIdPairs = parseVariableId(id);
|
|
905
|
-
debug$
|
|
288
|
+
debug$2("variableIdPairs:", variableIdPairs);
|
|
906
289
|
const returnVariableIdPairs = parseReturnStatement(returnStatement);
|
|
907
|
-
debug$
|
|
290
|
+
debug$2("returnVariableIdPairs:", returnVariableIdPairs);
|
|
908
291
|
return resolveIdentifier(variableIdPairs, returnVariableIdPairs);
|
|
909
292
|
}
|
|
910
293
|
const EMPTY_NODE_RETURN = {
|
|
@@ -924,11 +307,9 @@ function getCallExpressionAndReturnStatement(scope, local) {
|
|
|
924
307
|
}
|
|
925
308
|
let returnStatement = null;
|
|
926
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") {
|
|
927
|
-
returnStatement = callExpressionRef.from.block.body.body.find(
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
}
|
|
931
|
-
);
|
|
310
|
+
returnStatement = callExpressionRef.from.block.body.body.find((statement) => {
|
|
311
|
+
return statement.type === typescriptEstree.AST_NODE_TYPES.ReturnStatement;
|
|
312
|
+
});
|
|
932
313
|
}
|
|
933
314
|
return {
|
|
934
315
|
callExpression: callExpressionRef.identifier.parent,
|
|
@@ -997,9 +378,7 @@ function resolveIdentifier(localVariables, returnVariable) {
|
|
|
997
378
|
} else {
|
|
998
379
|
const variable = localVariables.find((pair) => pair.key === "t");
|
|
999
380
|
if (variable && variable.value) {
|
|
1000
|
-
const returnVar = returnVariable.find(
|
|
1001
|
-
(pair) => pair.value === variable.value
|
|
1002
|
-
);
|
|
381
|
+
const returnVar = returnVariable.find((pair) => pair.value === variable.value);
|
|
1003
382
|
if (returnVar && returnVar.key) {
|
|
1004
383
|
return { type: "identifier", key: returnVar.key };
|
|
1005
384
|
}
|
|
@@ -1007,9 +386,7 @@ function resolveIdentifier(localVariables, returnVariable) {
|
|
|
1007
386
|
const identifierOnly = localVariables.find((pair) => pair.value === null);
|
|
1008
387
|
if (identifierOnly && identifierOnly.key) {
|
|
1009
388
|
const targetKey = identifierOnly.key;
|
|
1010
|
-
const returnVar = returnVariable.find(
|
|
1011
|
-
(pair) => pair.value?.startsWith(targetKey)
|
|
1012
|
-
);
|
|
389
|
+
const returnVar = returnVariable.find((pair) => pair.value?.startsWith(targetKey));
|
|
1013
390
|
if (returnVar && returnVar.key) {
|
|
1014
391
|
return { type: "object", key: returnVar.key, style: "setup-hook" };
|
|
1015
392
|
}
|
|
@@ -1018,6 +395,409 @@ function resolveIdentifier(localVariables, returnVariable) {
|
|
|
1018
395
|
}
|
|
1019
396
|
}
|
|
1020
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
|
+
|
|
448
|
+
const INTLIFY_BUNDLE_IMPORT_ID = "@intlify/unplugin-vue-i18n/messages";
|
|
449
|
+
const VIRTUAL_PREFIX = "\0";
|
|
450
|
+
const debug$1 = createDebug__default(resolveNamespace("resource"));
|
|
451
|
+
function resourcePlugin(opts, meta) {
|
|
452
|
+
const _filter = pluginutils.createFilter(opts.include, opts.exclude);
|
|
453
|
+
const importedResources = /* @__PURE__ */ new Set();
|
|
454
|
+
const filter = (val) => _filter(val) || importedResources.has(val);
|
|
455
|
+
debug$1(`vue-i18n alias name: ${opts.module}`);
|
|
456
|
+
const ctx = {
|
|
457
|
+
prod: false,
|
|
458
|
+
sourceMap: false
|
|
459
|
+
};
|
|
460
|
+
let vuePlugin = null;
|
|
461
|
+
const getSfcParser = () => vuePlugin ? getVueCompiler(vuePlugin).parse : compilerSfc.parse;
|
|
462
|
+
const resourcePaths = /* @__PURE__ */ new Set();
|
|
463
|
+
for (const inc of opts.include || []) {
|
|
464
|
+
for (const resourcePath of fg__default.sync(inc, { ignore: opts.exclude })) {
|
|
465
|
+
resourcePaths.add(resourcePath);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
function result(code) {
|
|
469
|
+
if (meta.framework === "vite") {
|
|
470
|
+
return { code, map: { mappings: "" } };
|
|
471
|
+
}
|
|
472
|
+
return { code };
|
|
473
|
+
}
|
|
474
|
+
function sharedWebpackLikePlugin(compiler) {
|
|
475
|
+
ctx.prod = compiler.options.mode !== "development";
|
|
476
|
+
ctx.sourceMap = !!compiler.options.devtool;
|
|
477
|
+
debug$1(`${meta.framework}: isProduction = ${ctx.prod}, sourceMap = ${ctx.sourceMap}`);
|
|
478
|
+
compiler.options.resolve.alias = {
|
|
479
|
+
...compiler.options.resolve.alias,
|
|
480
|
+
[opts.module]: opts.vueI18nAliasPath
|
|
481
|
+
};
|
|
482
|
+
debug$1(`set ${opts.module}: ${opts.vueI18nAliasPath}`);
|
|
483
|
+
const loader = meta.framework === "webpack" ? loadWebpack : loadRspack;
|
|
484
|
+
loader().then((mod) => {
|
|
485
|
+
if (mod) {
|
|
486
|
+
compiler.options.plugins.push(
|
|
487
|
+
// @ts-expect-error -- FIXME: webpack type
|
|
488
|
+
new mod.DefinePlugin({
|
|
489
|
+
__VUE_I18N_FULL_INSTALL__: JSON.stringify(opts.fullInstall),
|
|
490
|
+
__INTLIFY_PROD_DEVTOOLS__: "false"
|
|
491
|
+
})
|
|
492
|
+
);
|
|
493
|
+
debug$1(`set __VUE_I18N_FULL_INSTALL__ is '${opts.fullInstall}'`);
|
|
494
|
+
} else {
|
|
495
|
+
debug$1(`ignore vue-i18n feature flags with ${meta.framework}.DefinePlugin`);
|
|
496
|
+
}
|
|
497
|
+
}).catch((_e) => {
|
|
498
|
+
warn(`${meta.framework} not found, please install ${meta.framework}.`);
|
|
499
|
+
});
|
|
500
|
+
compiler.options.module.rules.push({
|
|
501
|
+
test: /\.(json5?|ya?ml)$/,
|
|
502
|
+
type: "javascript/auto",
|
|
503
|
+
include: (resource) => filter(resource)
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
const supportedFileExtensionsRE = /\.(json5?|ya?ml|[c|m]?[j|t]s)$/;
|
|
507
|
+
return {
|
|
508
|
+
name: resolveNamespace("resource"),
|
|
509
|
+
/**
|
|
510
|
+
* NOTE:
|
|
511
|
+
* vite: early ('pre') transform for json and SFC's custom blocks
|
|
512
|
+
* because these are transformed into javascript code by `vite:json` plugin.
|
|
513
|
+
*
|
|
514
|
+
* webpack/rspack: late ('post') transform because we rely on vue-loader to generate the request queries.
|
|
515
|
+
*/
|
|
516
|
+
enforce: meta.framework === "vite" ? "pre" : "post",
|
|
517
|
+
rspack: sharedWebpackLikePlugin,
|
|
518
|
+
webpack: sharedWebpackLikePlugin,
|
|
519
|
+
vite: {
|
|
520
|
+
config() {
|
|
521
|
+
const defineConfig = {
|
|
522
|
+
define: {
|
|
523
|
+
__VUE_I18N_FULL_INSTALL__: opts.fullInstall,
|
|
524
|
+
__INTLIFY_DROP_MESSAGE_COMPILER__: opts.dropMessageCompiler,
|
|
525
|
+
__VUE_I18N_PROD_DEVTOOLS__: false
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
debug$1("define Config:", defineConfig);
|
|
529
|
+
const aliasConfig = {
|
|
530
|
+
resolve: {
|
|
531
|
+
alias: {
|
|
532
|
+
[opts.module]: opts.vueI18nAliasPath
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
debug$1("alias Config:", aliasConfig);
|
|
537
|
+
return shared.assign(defineConfig, aliasConfig);
|
|
538
|
+
},
|
|
539
|
+
configResolved(config) {
|
|
540
|
+
vuePlugin = getVitePlugin(config, "vite:vue");
|
|
541
|
+
if (!checkVuePlugin(vuePlugin)) {
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
ctx.prod = config.isProduction;
|
|
545
|
+
ctx.sourceMap = config.command === "build" ? !!config.build.sourcemap : false;
|
|
546
|
+
debug$1(`configResolved: isProduction = ${ctx.prod}, sourceMap = ${ctx.sourceMap}`);
|
|
547
|
+
const jsonPlugin = getVitePlugin(config, "vite:json");
|
|
548
|
+
if (jsonPlugin) {
|
|
549
|
+
const orgTransform = jsonPlugin.transform;
|
|
550
|
+
jsonPlugin.transform = async function(code, id) {
|
|
551
|
+
if (!/\.json$/.test(id) || filter(id)) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
const { query } = parseVueRequest(id);
|
|
555
|
+
if (query.vue) {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
debug$1("org json plugin");
|
|
559
|
+
return orgTransform.apply(this, [code, id]);
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
resolveId(id, importer) {
|
|
565
|
+
debug$1("resolveId", id, importer);
|
|
566
|
+
if (id === INTLIFY_BUNDLE_IMPORT_ID) {
|
|
567
|
+
return asVirtualId(id, meta.framework);
|
|
568
|
+
}
|
|
569
|
+
},
|
|
570
|
+
loadInclude(id) {
|
|
571
|
+
return INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework);
|
|
572
|
+
},
|
|
573
|
+
load(id) {
|
|
574
|
+
debug$1("load", id);
|
|
575
|
+
if (INTLIFY_BUNDLE_IMPORT_ID === getVirtualId(id, meta.framework) && resourcePaths.size > 0) {
|
|
576
|
+
const code = generateBundleResources(resourcePaths);
|
|
577
|
+
for (const p of resourcePaths) {
|
|
578
|
+
this.addWatchFile(p);
|
|
579
|
+
}
|
|
580
|
+
return result(code);
|
|
581
|
+
}
|
|
582
|
+
},
|
|
583
|
+
transformInclude(id) {
|
|
584
|
+
debug$1("transformInclude", id);
|
|
585
|
+
if (meta.framework === "vite") {
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
const { filename, query } = parseVueRequest(id);
|
|
589
|
+
if (filename.endsWith("vue") || filename.endsWith(INTLIFY_BUNDLE_IMPORT_ID)) {
|
|
590
|
+
return true;
|
|
591
|
+
}
|
|
592
|
+
let isResourcePath = resourcePaths.has(id);
|
|
593
|
+
if (!isResourcePath && "issuerPath" in query) {
|
|
594
|
+
isResourcePath = resourcePaths.has(query.issuerPath);
|
|
595
|
+
}
|
|
596
|
+
return supportedFileExtensionsRE.test(filename) && filter(filename) && isResourcePath;
|
|
597
|
+
},
|
|
598
|
+
async transform(code, id) {
|
|
599
|
+
const { filename, query } = parseVueRequest(id);
|
|
600
|
+
debug$1("transform", id, JSON.stringify(query), filename);
|
|
601
|
+
let langInfo = opts.defaultSFCLang;
|
|
602
|
+
if (filter(id)) {
|
|
603
|
+
const imports = mlly.findStaticImports(code);
|
|
604
|
+
for (const p of imports) {
|
|
605
|
+
const res = path.resolve(path.dirname(id), p.specifier);
|
|
606
|
+
importedResources.add(res);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
if (!query.vue && supportedFileExtensionsRE.test(id) && filter(id)) {
|
|
610
|
+
langInfo = path.parse(filename).ext;
|
|
611
|
+
const generate = getGenerator(langInfo);
|
|
612
|
+
const parseOptions = getOptions(filename, ctx, query, {
|
|
613
|
+
...opts,
|
|
614
|
+
transformI18nBlock: void 0
|
|
615
|
+
});
|
|
616
|
+
debug$1("parseOptions", parseOptions);
|
|
617
|
+
try {
|
|
618
|
+
const { code: generatedCode, map } = generate(code, parseOptions);
|
|
619
|
+
debug$1("generated code", generatedCode);
|
|
620
|
+
debug$1("sourcemap", map, ctx.sourceMap);
|
|
621
|
+
if (code === generatedCode)
|
|
622
|
+
return;
|
|
623
|
+
return result(generatedCode);
|
|
624
|
+
} catch (err) {
|
|
625
|
+
if (err instanceof bundleUtils.DynamicResourceError) {
|
|
626
|
+
error(
|
|
627
|
+
`Unable to precompile or optimize \`${filename}\` - excluding from virtual bundle \`${INTLIFY_BUNDLE_IMPORT_ID}\`.
|
|
628
|
+
`,
|
|
629
|
+
err
|
|
630
|
+
);
|
|
631
|
+
return `export default {}`;
|
|
632
|
+
} else {
|
|
633
|
+
throw err;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
if (isCustomBlock(query)) {
|
|
638
|
+
langInfo = opts.defaultSFCLang;
|
|
639
|
+
if (query.lang && query.lang !== "i18n") {
|
|
640
|
+
langInfo = query.lang;
|
|
641
|
+
}
|
|
642
|
+
debug$1("langInfo", langInfo);
|
|
643
|
+
const parseOptions = getOptions(filename, ctx, query, {
|
|
644
|
+
...opts,
|
|
645
|
+
allowDynamic: false,
|
|
646
|
+
transformI18nBlock: void 0
|
|
647
|
+
});
|
|
648
|
+
debug$1("parseOptions", parseOptions);
|
|
649
|
+
let source = getCode(code, filename, ctx.sourceMap, query, getSfcParser(), meta);
|
|
650
|
+
if (typeof opts.transformI18nBlock === "function") {
|
|
651
|
+
const modifiedSource = opts.transformI18nBlock(source);
|
|
652
|
+
if (modifiedSource && typeof modifiedSource === "string") {
|
|
653
|
+
source = modifiedSource;
|
|
654
|
+
} else {
|
|
655
|
+
warn("transformI18nBlock should return a string");
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
const generate = getGenerator(langInfo, bundleUtils.generateYAML);
|
|
659
|
+
const { code: generatedCode, map } = generate(source, parseOptions);
|
|
660
|
+
debug$1("generated code", generatedCode);
|
|
661
|
+
debug$1("sourcemap", map, ctx.sourceMap);
|
|
662
|
+
if (code !== generatedCode) {
|
|
663
|
+
return result(generatedCode);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
function getGenerator(ext, fallback = bundleUtils.generateJSON) {
|
|
670
|
+
if (/\.?json5?$/.test(ext)) {
|
|
671
|
+
return bundleUtils.generateJSON;
|
|
672
|
+
}
|
|
673
|
+
if (/\.ya?ml$/.test(ext)) {
|
|
674
|
+
return bundleUtils.generateYAML;
|
|
675
|
+
}
|
|
676
|
+
if (/\.[c|m]?[j|t]s$/.test(ext)) {
|
|
677
|
+
return bundleUtils.generateJavaScript;
|
|
678
|
+
}
|
|
679
|
+
return fallback;
|
|
680
|
+
}
|
|
681
|
+
async function loadWebpack() {
|
|
682
|
+
const mod = await import('webpack');
|
|
683
|
+
return mod.default || mod;
|
|
684
|
+
}
|
|
685
|
+
async function loadRspack() {
|
|
686
|
+
const { rspack } = await import('@rspack/core');
|
|
687
|
+
return rspack;
|
|
688
|
+
}
|
|
689
|
+
function generateBundleResources(resources) {
|
|
690
|
+
const codes = [];
|
|
691
|
+
const imports = [];
|
|
692
|
+
for (const filename of resources) {
|
|
693
|
+
debug$1(`${filename} bundle loading ...`);
|
|
694
|
+
const { name } = path.parse(filename);
|
|
695
|
+
const varName = knitwork.genSafeVariableName(
|
|
696
|
+
[name, node_crypto.createHash("sha256").update(filename).digest("hex").substring(0, 8)].join("_")
|
|
697
|
+
);
|
|
698
|
+
imports.push({ statement: knitwork.genImport(filename, varName), varName });
|
|
699
|
+
codes.push(`${JSON.stringify(name)}: ${varName}`);
|
|
700
|
+
}
|
|
701
|
+
const importStatements = imports.map((x) => x.statement).join("\n");
|
|
702
|
+
return `
|
|
703
|
+
${importStatements}
|
|
704
|
+
|
|
705
|
+
const isObject = (item) => item && typeof item === 'object' && !Array.isArray(item);
|
|
706
|
+
|
|
707
|
+
const mergeDeep = (target, ...sources) => {
|
|
708
|
+
if (!sources.length) return target;
|
|
709
|
+
const source = sources.shift();
|
|
710
|
+
|
|
711
|
+
if (isObject(target) && isObject(source)) {
|
|
712
|
+
for (const key in source) {
|
|
713
|
+
if (isObject(source[key])) {
|
|
714
|
+
if (!target[key]) Object.assign(target, { [key]: {} });
|
|
715
|
+
mergeDeep(target[key], source[key]);
|
|
716
|
+
} else {
|
|
717
|
+
Object.assign(target, { [key]: source[key] });
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
return mergeDeep(target, ...sources);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
export default mergeDeep({},
|
|
726
|
+
${codes.map((code) => `{ ${code} }`).join(",\n ")}
|
|
727
|
+
);`;
|
|
728
|
+
}
|
|
729
|
+
function getCode(source, filename, sourceMap, query, parser, meta) {
|
|
730
|
+
if (query.index == null) {
|
|
731
|
+
raiseError(`unexpected index: ${query.index}`);
|
|
732
|
+
}
|
|
733
|
+
if (meta.framework === "vite") {
|
|
734
|
+
return source;
|
|
735
|
+
}
|
|
736
|
+
if (query.issuerPath) {
|
|
737
|
+
debug$1(`getCode (${meta.framework}) ${query.index} via issuerPath`, query.issuerPath);
|
|
738
|
+
return readFile(filename);
|
|
739
|
+
}
|
|
740
|
+
const result = parser(readFile(filename), { sourceMap, filename });
|
|
741
|
+
const block = result.descriptor.customBlocks[query.index];
|
|
742
|
+
if (block) {
|
|
743
|
+
const code = block.src ? readFile(block.src) : block.content;
|
|
744
|
+
debug$1(`getCode (${meta.framework}) ${query.index} from SFC`, code);
|
|
745
|
+
return code;
|
|
746
|
+
}
|
|
747
|
+
return source;
|
|
748
|
+
}
|
|
749
|
+
function isCustomBlock(query) {
|
|
750
|
+
return !shared.isEmptyObject(query) && "vue" in query && (query["type"] === "custom" || // for vite (@vite-plugin-vue)
|
|
751
|
+
query["type"] === "i18n" || // for webpack (vue-loader)
|
|
752
|
+
query["blockType"] === "i18n");
|
|
753
|
+
}
|
|
754
|
+
function getOptions(filename, ctx, query, opts) {
|
|
755
|
+
const baseOptions = {
|
|
756
|
+
...opts,
|
|
757
|
+
filename,
|
|
758
|
+
jit: true,
|
|
759
|
+
// allow overriding ctx.sourceMap with opts
|
|
760
|
+
sourceMap: opts.sourceMap ?? ctx.sourceMap,
|
|
761
|
+
env: ctx.prod ? "production" : "development",
|
|
762
|
+
onWarn: (msg) => warn(`${filename} ${msg}`),
|
|
763
|
+
onError: (msg, extra) => {
|
|
764
|
+
const codeFrame = shared.generateCodeFrame(
|
|
765
|
+
extra?.source || extra?.location?.source || "",
|
|
766
|
+
extra?.location?.start.column,
|
|
767
|
+
extra?.location?.end.column
|
|
768
|
+
);
|
|
769
|
+
const errMessage = `${msg} (error code: ${extra?.code}) in ${filename}
|
|
770
|
+
target message: ${extra?.source}
|
|
771
|
+
target message path: ${extra?.path}
|
|
772
|
+
|
|
773
|
+
${codeFrame}
|
|
774
|
+
`;
|
|
775
|
+
error(errMessage);
|
|
776
|
+
throw new Error(errMessage);
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
if (isCustomBlock(query)) {
|
|
780
|
+
return shared.assign(baseOptions, {
|
|
781
|
+
type: "sfc",
|
|
782
|
+
locale: query.locale ?? "",
|
|
783
|
+
isGlobal: opts.globalSFCScope || !!query.global
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
return shared.assign(baseOptions, {
|
|
787
|
+
type: "plain",
|
|
788
|
+
isGlobal: false
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
function getVirtualId(id, framework = "vite") {
|
|
792
|
+
return framework === "vite" ? id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : "" : id;
|
|
793
|
+
}
|
|
794
|
+
function asVirtualId(id, framework = "vite") {
|
|
795
|
+
return framework === "vite" ? VIRTUAL_PREFIX + id : id;
|
|
796
|
+
}
|
|
797
|
+
function readFile(path) {
|
|
798
|
+
return fs__default.readFileSync(path, "utf-8");
|
|
799
|
+
}
|
|
800
|
+
|
|
1021
801
|
const debug = createDebug__default(resolveNamespace("root"));
|
|
1022
802
|
const unpluginFactory = (options = {}, meta) => {
|
|
1023
803
|
debug("meta framework", meta.framework);
|