@intlify/bundle-utils 10.0.1 → 11.0.0-beta.3
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 +464 -571
- package/lib/index.d.cts +23 -12
- package/lib/index.d.mts +23 -12
- package/lib/index.d.ts +23 -12
- package/lib/index.mjs +463 -571
- package/package.json +9 -15
package/lib/index.cjs
CHANGED
|
@@ -1,20 +1,69 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const module$1 = require('node:module');
|
|
4
4
|
const shared = require('@intlify/shared');
|
|
5
|
-
const sourceMapJs = require('source-map-js');
|
|
6
|
-
const messageCompiler = require('@intlify/message-compiler');
|
|
7
|
-
const yamlEslintParser = require('yaml-eslint-parser');
|
|
8
5
|
const acorn = require('acorn');
|
|
9
6
|
const escodegen = require('escodegen');
|
|
10
7
|
const estreeWalker = require('estree-walker');
|
|
11
|
-
const
|
|
8
|
+
const messageCompiler = require('@intlify/message-compiler');
|
|
9
|
+
const sourceMapJs = require('source-map-js');
|
|
10
|
+
const jsoncEslintParser = require('jsonc-eslint-parser');
|
|
11
|
+
const esbuild = require('esbuild');
|
|
12
|
+
const yamlEslintParser = require('yaml-eslint-parser');
|
|
12
13
|
|
|
13
14
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
14
15
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
15
16
|
|
|
16
17
|
const module__default = /*#__PURE__*/_interopDefaultCompat(module$1);
|
|
17
18
|
|
|
19
|
+
const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
20
|
+
function checkInstallPackage(pkg, debug) {
|
|
21
|
+
let installedVueI18n = false;
|
|
22
|
+
try {
|
|
23
|
+
debug(`vue-i18n load path: ${_require.resolve("vue-i18n")}`);
|
|
24
|
+
installedVueI18n = true;
|
|
25
|
+
} catch (e) {
|
|
26
|
+
debug(`cannot find 'vue-i18n'`, e);
|
|
27
|
+
}
|
|
28
|
+
let installedPetiteVueI18n = false;
|
|
29
|
+
try {
|
|
30
|
+
debug(`petite-vue-i18n load path: ${_require.resolve("petite-vue-i18n")}`);
|
|
31
|
+
installedPetiteVueI18n = true;
|
|
32
|
+
} catch (e) {
|
|
33
|
+
debug(`cannot find 'petite-vue-i18n'`, e);
|
|
34
|
+
}
|
|
35
|
+
if (installedVueI18n) {
|
|
36
|
+
return "vue-i18n";
|
|
37
|
+
}
|
|
38
|
+
if (installedPetiteVueI18n) {
|
|
39
|
+
return "petite-vue-i18n";
|
|
40
|
+
}
|
|
41
|
+
throw new Error(
|
|
42
|
+
`${pkg} requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
function getVueI18nVersion(debug, pkg = "vue-i18n") {
|
|
46
|
+
const VueI18n = loadModule(pkg, debug);
|
|
47
|
+
if (VueI18n == null) {
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
if (VueI18n.VERSION && VueI18n.VERSION.startsWith("9.")) {
|
|
51
|
+
return "9";
|
|
52
|
+
}
|
|
53
|
+
if (VueI18n.VERSION && VueI18n.VERSION.startsWith("10.")) {
|
|
54
|
+
return "10";
|
|
55
|
+
}
|
|
56
|
+
return "unknown";
|
|
57
|
+
}
|
|
58
|
+
function loadModule(moduleName, debug) {
|
|
59
|
+
try {
|
|
60
|
+
return _require(moduleName);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
debug(`cannot load '${moduleName}'`, e);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
18
67
|
function createCodeGenerator(options = {
|
|
19
68
|
filename: "bundle.json",
|
|
20
69
|
sourceMap: false,
|
|
@@ -273,248 +322,312 @@ function excludeLocales({
|
|
|
273
322
|
return _messages;
|
|
274
323
|
}
|
|
275
324
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
325
|
+
class DynamicResourceError extends Error {
|
|
326
|
+
}
|
|
327
|
+
const DEFAULT_OPTIONS = {
|
|
328
|
+
type: "plain",
|
|
329
|
+
filename: "vue-i18n-loader.js",
|
|
330
|
+
inSourceMap: void 0,
|
|
331
|
+
locale: "",
|
|
332
|
+
isGlobal: false,
|
|
333
|
+
sourceMap: false,
|
|
334
|
+
env: "development",
|
|
335
|
+
forceStringify: false,
|
|
336
|
+
onError: void 0,
|
|
337
|
+
onWarn: void 0,
|
|
338
|
+
strictMessage: true,
|
|
339
|
+
escapeHtml: false,
|
|
340
|
+
allowDynamic: false,
|
|
341
|
+
jit: false
|
|
342
|
+
};
|
|
343
|
+
function generate$3(targetSource, options) {
|
|
344
|
+
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
345
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, {
|
|
346
|
+
source: value
|
|
347
|
+
});
|
|
348
|
+
const generator = createCodeGenerator(_options);
|
|
349
|
+
const ast = acorn.parse(value, {
|
|
350
|
+
ecmaVersion: "latest",
|
|
351
|
+
sourceType: "module",
|
|
352
|
+
sourceFile: _options.filename,
|
|
353
|
+
allowImportExportEverywhere: true
|
|
354
|
+
});
|
|
355
|
+
const exportResult = scanAst(ast);
|
|
356
|
+
if (!_options.allowDynamic) {
|
|
357
|
+
if (!exportResult) {
|
|
358
|
+
throw new Error(`You need to define an object as the locale message with 'export default'.`);
|
|
359
|
+
}
|
|
360
|
+
if (exportResult !== "object") {
|
|
361
|
+
throw new DynamicResourceError(
|
|
362
|
+
`You need to define an object as the locale message with 'export default'.`
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
} else {
|
|
366
|
+
if (!exportResult) {
|
|
367
|
+
throw new Error(`You need to define 'export default' that will return the locale messages.`);
|
|
368
|
+
}
|
|
369
|
+
if (exportResult !== "object") {
|
|
370
|
+
return {
|
|
371
|
+
ast,
|
|
372
|
+
code: value,
|
|
373
|
+
map: _options.inSourceMap
|
|
374
|
+
};
|
|
375
|
+
}
|
|
323
376
|
}
|
|
324
|
-
const
|
|
325
|
-
const codeMaps = _generate$2(generator, ast, options);
|
|
377
|
+
const codeMaps = _generate$2(generator, ast, _options);
|
|
326
378
|
const { code, map } = generator.context();
|
|
327
|
-
const newMap = map
|
|
379
|
+
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, _options.inSourceMap) || null : null;
|
|
328
380
|
return {
|
|
329
381
|
ast,
|
|
330
382
|
code,
|
|
331
383
|
map: newMap != null ? newMap : void 0
|
|
332
384
|
};
|
|
333
385
|
}
|
|
386
|
+
function scanAst(ast) {
|
|
387
|
+
if (ast.type !== "Program") {
|
|
388
|
+
throw new Error("Invalid AST: does not have Program node");
|
|
389
|
+
}
|
|
390
|
+
for (const node of ast.body) {
|
|
391
|
+
if (node.type !== "ExportDefaultDeclaration") continue;
|
|
392
|
+
switch (node.declaration.type) {
|
|
393
|
+
case "ObjectExpression":
|
|
394
|
+
return "object";
|
|
395
|
+
case "FunctionDeclaration":
|
|
396
|
+
return "function";
|
|
397
|
+
case "ArrowFunctionExpression":
|
|
398
|
+
return "arrow-function";
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
334
403
|
function _generate$2(generator, node, options = {}) {
|
|
335
404
|
const propsCountStack = [];
|
|
336
405
|
const pathStack = [];
|
|
337
406
|
const itemsCountStack = [];
|
|
407
|
+
const skipStack = [];
|
|
338
408
|
const { forceStringify } = generator.context();
|
|
339
409
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
340
410
|
const { type, sourceMap, isGlobal, locale, jit } = options;
|
|
341
|
-
const
|
|
411
|
+
const _codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
412
|
+
function codegenFn(value) {
|
|
413
|
+
const { code, map } = _codegenFn(value, options, pathStack);
|
|
414
|
+
sourceMap && map != null && codeMaps.set(value, map);
|
|
415
|
+
return code;
|
|
416
|
+
}
|
|
342
417
|
const componentNamespace = "_Component";
|
|
343
|
-
|
|
344
|
-
|
|
418
|
+
const variableDeclarations = [];
|
|
419
|
+
estreeWalker.walk(node, {
|
|
420
|
+
/**
|
|
421
|
+
* NOTE:
|
|
422
|
+
* force cast to Node of `estree-walker@3.x`,
|
|
423
|
+
* because `estree-walker@3.x` is not dual packages,
|
|
424
|
+
* so it's support only esm only ...
|
|
425
|
+
*/
|
|
426
|
+
// @ts-ignore
|
|
427
|
+
enter(node2, _parent) {
|
|
428
|
+
if (_parent?.type != null) this.skip();
|
|
429
|
+
switch (node2.type) {
|
|
430
|
+
case "ExportDefaultDeclaration":
|
|
431
|
+
this.skip();
|
|
432
|
+
break;
|
|
433
|
+
case "ImportDeclaration":
|
|
434
|
+
generator.push(options.source?.slice(node2.start, node2.end));
|
|
435
|
+
generator.newline();
|
|
436
|
+
break;
|
|
437
|
+
case "VariableDeclaration":
|
|
438
|
+
generator.push(options.source?.slice(node2.start, node2.end));
|
|
439
|
+
generator.newline();
|
|
440
|
+
variableDeclarations.push(
|
|
441
|
+
...node2.declarations.map((x) => `\`${x.id.name}\``)
|
|
442
|
+
);
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
if (variableDeclarations.length > 0) {
|
|
448
|
+
options?.onWarn?.(
|
|
449
|
+
`
|
|
450
|
+
Variable declarations are not optimized - found ${variableDeclarations.join(", ")}`
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
estreeWalker.walk(node, {
|
|
454
|
+
/**
|
|
455
|
+
* NOTE:
|
|
456
|
+
* force cast to Node of `estree-walker@3.x`,
|
|
457
|
+
* because `estree-walker@3.x` is not dual packages,
|
|
458
|
+
* so it's support only esm only ...
|
|
459
|
+
*/
|
|
460
|
+
// @ts-ignore
|
|
461
|
+
enter(node2, parent) {
|
|
462
|
+
if (parent?.type === "Program") {
|
|
463
|
+
switch (node2.type) {
|
|
464
|
+
case "ImportDeclaration":
|
|
465
|
+
case "VariableDeclaration":
|
|
466
|
+
case "VariableDeclarator":
|
|
467
|
+
this.skip();
|
|
468
|
+
}
|
|
469
|
+
} else if (parent?.type === "ArrayExpression") {
|
|
470
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
471
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
472
|
+
pathStack.push(currentCount.toString());
|
|
473
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
474
|
+
} else if (parent?.type === "ObjectExpression") {
|
|
475
|
+
const lastIndex = propsCountStack.length - 1;
|
|
476
|
+
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
477
|
+
}
|
|
345
478
|
switch (node2.type) {
|
|
346
479
|
case "Program":
|
|
347
480
|
if (type === "plain") {
|
|
348
481
|
generator.push(`const resource = `);
|
|
349
482
|
} else if (type === "sfc") {
|
|
350
|
-
const
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
generator.push(`${exportSyntax} function (Component) {`);
|
|
483
|
+
const localeName = JSON.stringify(locale ?? '""');
|
|
484
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
485
|
+
generator.push(`export default function (Component) {`);
|
|
354
486
|
generator.indent();
|
|
355
|
-
const
|
|
356
|
-
generator.pushline(
|
|
357
|
-
`const ${componentNamespace} = ${componentVariable}`
|
|
358
|
-
);
|
|
487
|
+
generator.pushline(`const ${componentNamespace} = Component`);
|
|
359
488
|
generator.pushline(
|
|
360
489
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
361
490
|
);
|
|
362
491
|
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
363
492
|
generator.indent();
|
|
364
|
-
generator.pushline(`"locale": ${
|
|
493
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
365
494
|
generator.push(`"resource": `);
|
|
366
495
|
}
|
|
367
496
|
break;
|
|
368
|
-
case "
|
|
369
|
-
generator.push(
|
|
497
|
+
case "ObjectExpression":
|
|
498
|
+
generator.push("{");
|
|
370
499
|
generator.indent();
|
|
371
500
|
propsCountStack.push(node2.properties.length);
|
|
372
|
-
if (parent.type === "JSONArrayExpression") {
|
|
373
|
-
const lastIndex2 = itemsCountStack.length - 1;
|
|
374
|
-
const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
|
|
375
|
-
pathStack.push(currentCount.toString());
|
|
376
|
-
itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
|
|
377
|
-
}
|
|
378
501
|
break;
|
|
379
|
-
case "
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
502
|
+
case "ArrayExpression":
|
|
503
|
+
generator.push("[");
|
|
504
|
+
generator.indent();
|
|
505
|
+
itemsCountStack.push(node2.elements.length);
|
|
506
|
+
break;
|
|
507
|
+
case "Property":
|
|
508
|
+
if (parent?.type !== "ObjectExpression") break;
|
|
509
|
+
if (node2.key.type !== "Literal" && node2.key.type !== "Identifier") break;
|
|
510
|
+
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
511
|
+
const strName = JSON.stringify(name);
|
|
512
|
+
if (isJSONablePrimitiveLiteral(node2.value)) {
|
|
513
|
+
generator.push(`${strName}: `);
|
|
514
|
+
pathStack.push(name);
|
|
515
|
+
const value = getValue(node2.value);
|
|
516
|
+
const strValue = JSON.stringify(value);
|
|
517
|
+
if (node2.value.type === "Literal" && shared.isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
|
|
518
|
+
generator.push(codegenFn(value), node2.value, value);
|
|
519
|
+
} else if (forceStringify) {
|
|
520
|
+
generator.push(codegenFn(strValue), node2.value, strValue);
|
|
389
521
|
} else {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
522
|
+
generator.push(strValue);
|
|
523
|
+
}
|
|
524
|
+
skipStack.push(false);
|
|
525
|
+
} else if (node2.value.type === "ArrayExpression" || node2.value.type === "ObjectExpression") {
|
|
526
|
+
generator.push(`${strName}: `);
|
|
527
|
+
pathStack.push(name);
|
|
528
|
+
skipStack.push(false);
|
|
529
|
+
} else if (node2.value.type === "FunctionExpression" || node2.value.type === "ArrowFunctionExpression") {
|
|
530
|
+
generator.push(`${strName}: `);
|
|
531
|
+
pathStack.push(name);
|
|
532
|
+
const code = escodegen.generate(node2.value, {
|
|
533
|
+
format: { compact: true }
|
|
534
|
+
});
|
|
535
|
+
generator.push(code, node2.value, code);
|
|
536
|
+
skipStack.push(false);
|
|
537
|
+
} else {
|
|
538
|
+
const skipProperty = "regex" in node2.value;
|
|
539
|
+
if (!skipProperty && node2.type === "Property") {
|
|
540
|
+
const identifierName = node2.value.type === "Identifier" && String(node2.value.name) || node2.value.type === "Literal" && String(node2.value.value);
|
|
541
|
+
generator.push(`${strName}: ${identifierName || name}`);
|
|
542
|
+
skipStack.push(false);
|
|
543
|
+
} else {
|
|
544
|
+
skipStack.push(true);
|
|
403
545
|
}
|
|
404
|
-
} else if ((node2.value.type === "JSONObjectExpression" || node2.value.type === "JSONArrayExpression") && (node2.key.type === "JSONLiteral" || node2.key.type === "JSONIdentifier")) {
|
|
405
|
-
const name = node2.key.type === "JSONLiteral" ? node2.key.value : node2.key.name;
|
|
406
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
407
|
-
pathStack.push(name.toString());
|
|
408
546
|
}
|
|
409
|
-
const lastIndex = propsCountStack.length - 1;
|
|
410
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
411
547
|
break;
|
|
412
|
-
case "
|
|
413
|
-
|
|
414
|
-
generator.
|
|
415
|
-
if (parent.type === "JSONArrayExpression") {
|
|
416
|
-
const lastIndex2 = itemsCountStack.length - 1;
|
|
417
|
-
const currentCount = parent.elements.length - itemsCountStack[lastIndex2];
|
|
418
|
-
pathStack.push(currentCount.toString());
|
|
419
|
-
itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
|
|
420
|
-
}
|
|
421
|
-
itemsCountStack.push(node2.elements.length);
|
|
548
|
+
case "SpreadElement":
|
|
549
|
+
const spreadIdentifier = node2.argument.type === "Identifier" && String(node2.argument.name) || node2.argument.type === "Literal" && String(node2.argument.value);
|
|
550
|
+
generator.push(`...${spreadIdentifier}`);
|
|
422
551
|
break;
|
|
423
|
-
|
|
424
|
-
if (parent
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
if (
|
|
431
|
-
|
|
432
|
-
sourceMap && map != null && codeMaps.set(value, map);
|
|
433
|
-
generator.push(`${code}`, node2, value);
|
|
552
|
+
default:
|
|
553
|
+
if (parent?.type === "ArrayExpression") {
|
|
554
|
+
if (isJSONablePrimitiveLiteral(node2)) {
|
|
555
|
+
const value = getValue(node2);
|
|
556
|
+
const strValue = JSON.stringify(value);
|
|
557
|
+
if (node2.type === "Literal" && shared.isString(node2.value) || node2.type === "TemplateLiteral") {
|
|
558
|
+
generator.push(codegenFn(value), node2, value);
|
|
559
|
+
} else if (forceStringify) {
|
|
560
|
+
generator.push(codegenFn(strValue), node2, strValue);
|
|
434
561
|
} else {
|
|
435
|
-
|
|
436
|
-
const strValue = JSON.stringify(value);
|
|
437
|
-
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
438
|
-
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
439
|
-
generator.push(`${code}`, node2, strValue);
|
|
440
|
-
} else {
|
|
441
|
-
generator.push(`${JSON.stringify(value)}`);
|
|
442
|
-
}
|
|
562
|
+
generator.push(strValue);
|
|
443
563
|
}
|
|
564
|
+
skipStack.push(false);
|
|
565
|
+
} else {
|
|
566
|
+
skipStack.push(true);
|
|
444
567
|
}
|
|
445
|
-
itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
|
|
446
568
|
}
|
|
447
569
|
break;
|
|
448
570
|
}
|
|
449
571
|
},
|
|
450
|
-
|
|
572
|
+
/**
|
|
573
|
+
* NOTE:
|
|
574
|
+
* force cast to Node of `estree-walker@3.x`,
|
|
575
|
+
* because `estree-walker@3.x` is not dual packages,
|
|
576
|
+
* so it's support only esm only ...
|
|
577
|
+
*/
|
|
578
|
+
// @ts-ignore
|
|
579
|
+
leave(node2, parent) {
|
|
451
580
|
switch (node2.type) {
|
|
452
581
|
case "Program":
|
|
453
|
-
if (type === "
|
|
582
|
+
if (type === "plain") {
|
|
583
|
+
generator.push("\n");
|
|
584
|
+
generator.push("export default resource");
|
|
585
|
+
} else if (type === "sfc") {
|
|
454
586
|
generator.deindent();
|
|
455
|
-
generator.push(
|
|
587
|
+
generator.push("})");
|
|
456
588
|
generator.deindent();
|
|
457
|
-
generator.pushline(
|
|
458
|
-
} else if (type === "plain") {
|
|
459
|
-
generator.push(`
|
|
460
|
-
`);
|
|
461
|
-
generator.push("export default resource");
|
|
589
|
+
generator.pushline("}");
|
|
462
590
|
}
|
|
463
591
|
break;
|
|
464
|
-
case "
|
|
592
|
+
case "ObjectExpression":
|
|
465
593
|
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
466
594
|
pathStack.pop();
|
|
467
595
|
propsCountStack.pop();
|
|
468
596
|
}
|
|
469
597
|
generator.deindent();
|
|
470
|
-
generator.push(
|
|
471
|
-
if (parent.type === "JSONArrayExpression") {
|
|
472
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
473
|
-
pathStack.pop();
|
|
474
|
-
generator.pushline(`,`);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
598
|
+
generator.push("}");
|
|
477
599
|
break;
|
|
478
|
-
case "
|
|
479
|
-
if (propsCountStack[propsCountStack.length - 1] !== 0) {
|
|
480
|
-
pathStack.pop();
|
|
481
|
-
generator.pushline(`,`);
|
|
482
|
-
}
|
|
483
|
-
break;
|
|
484
|
-
case "JSONArrayExpression":
|
|
600
|
+
case "ArrayExpression":
|
|
485
601
|
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
486
602
|
pathStack.pop();
|
|
487
603
|
itemsCountStack.pop();
|
|
488
604
|
}
|
|
489
605
|
generator.deindent();
|
|
490
|
-
generator.push(
|
|
491
|
-
if (parent.type === "JSONArrayExpression") {
|
|
492
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
493
|
-
pathStack.pop();
|
|
494
|
-
generator.pushline(`,`);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
break;
|
|
498
|
-
case "JSONLiteral":
|
|
499
|
-
if (parent.type === "JSONArrayExpression") {
|
|
500
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
501
|
-
pathStack.pop();
|
|
502
|
-
generator.pushline(`,`);
|
|
503
|
-
} else {
|
|
504
|
-
generator.pushline(`,`);
|
|
505
|
-
}
|
|
506
|
-
}
|
|
606
|
+
generator.push("]");
|
|
507
607
|
break;
|
|
508
608
|
}
|
|
609
|
+
if (parent?.type === "ArrayExpression" || parent?.type === "ObjectExpression") {
|
|
610
|
+
const stackArr = node2.type === "Property" ? propsCountStack : itemsCountStack;
|
|
611
|
+
if (stackArr[stackArr.length - 1] !== 0) {
|
|
612
|
+
pathStack.pop();
|
|
613
|
+
!skipStack.pop() && generator.pushline(",");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
509
616
|
}
|
|
510
617
|
});
|
|
511
618
|
return codeMaps;
|
|
512
619
|
}
|
|
620
|
+
function isJSONablePrimitiveLiteral(node) {
|
|
621
|
+
return node.type === "Literal" && (shared.isString(node.value) || shared.isNumber(node.value) || shared.isBoolean(node.value) || node.value === null) || node.type === "TemplateLiteral";
|
|
622
|
+
}
|
|
623
|
+
function getValue(node) {
|
|
624
|
+
return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
|
|
625
|
+
}
|
|
513
626
|
|
|
514
|
-
function generate$
|
|
627
|
+
function generate$2(targetSource, {
|
|
515
628
|
type = "plain",
|
|
516
629
|
onlyLocales = [],
|
|
517
|
-
filename = "vue-i18n-loader.
|
|
630
|
+
filename = "vue-i18n-loader.json",
|
|
518
631
|
inSourceMap = void 0,
|
|
519
632
|
locale = "",
|
|
520
633
|
isGlobal = false,
|
|
@@ -542,27 +655,27 @@ function generate$1(targetSource, {
|
|
|
542
655
|
escapeHtml,
|
|
543
656
|
jit
|
|
544
657
|
};
|
|
545
|
-
let ast =
|
|
658
|
+
let ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
546
659
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
547
|
-
const messages =
|
|
660
|
+
const messages = jsoncEslintParser.getStaticJSONValue(ast);
|
|
548
661
|
value = JSON.stringify(
|
|
549
662
|
excludeLocales({
|
|
550
663
|
messages,
|
|
551
664
|
onlyLocales
|
|
552
665
|
})
|
|
553
666
|
);
|
|
554
|
-
ast =
|
|
667
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
555
668
|
}
|
|
556
669
|
if (locale && onlyLocales?.length && !onlyLocales.includes(locale)) {
|
|
557
670
|
value = JSON.stringify({});
|
|
558
|
-
ast =
|
|
671
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
559
672
|
options.locale = "";
|
|
560
673
|
options.source = void 0;
|
|
561
674
|
}
|
|
562
675
|
const generator = createCodeGenerator(options);
|
|
563
676
|
const codeMaps = _generate$1(generator, ast, options);
|
|
564
677
|
const { code, map } = generator.context();
|
|
565
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
678
|
+
const newMap = map && !jit ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
566
679
|
return {
|
|
567
680
|
ast,
|
|
568
681
|
code,
|
|
@@ -576,111 +689,81 @@ function _generate$1(generator, node, options = {}) {
|
|
|
576
689
|
const { forceStringify } = generator.context();
|
|
577
690
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
578
691
|
const { type, sourceMap, isGlobal, locale, jit } = options;
|
|
579
|
-
const
|
|
692
|
+
const _codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
693
|
+
function codegenFn(value) {
|
|
694
|
+
const { code, map } = _codegenFn(value, options, pathStack);
|
|
695
|
+
sourceMap && map != null && codeMaps.set(value, map);
|
|
696
|
+
return code;
|
|
697
|
+
}
|
|
580
698
|
const componentNamespace = "_Component";
|
|
581
|
-
|
|
699
|
+
jsoncEslintParser.traverseNodes(node, {
|
|
582
700
|
enterNode(node2, parent) {
|
|
701
|
+
if (parent?.type === "JSONArrayExpression") {
|
|
702
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
703
|
+
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
704
|
+
pathStack.push(currentCount.toString());
|
|
705
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
706
|
+
} else if (parent?.type === "JSONObjectExpression") {
|
|
707
|
+
const lastIndex = propsCountStack.length - 1;
|
|
708
|
+
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
709
|
+
}
|
|
583
710
|
switch (node2.type) {
|
|
584
711
|
case "Program":
|
|
585
712
|
if (type === "plain") {
|
|
586
713
|
generator.push(`const resource = `);
|
|
587
714
|
} else if (type === "sfc") {
|
|
588
|
-
const variableName =
|
|
589
|
-
const localeName =
|
|
590
|
-
|
|
591
|
-
generator.push(`${exportSyntax} function (Component) {`);
|
|
715
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
716
|
+
const localeName = JSON.stringify(locale ?? `""`);
|
|
717
|
+
generator.push(`export default function (Component) {`);
|
|
592
718
|
generator.indent();
|
|
593
|
-
const
|
|
594
|
-
generator.pushline(
|
|
595
|
-
`const ${componentNamespace} = ${componentVariable}`
|
|
596
|
-
);
|
|
719
|
+
generator.pushline(`const ${componentNamespace} = Component`);
|
|
597
720
|
generator.pushline(
|
|
598
721
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
599
722
|
);
|
|
600
723
|
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
601
724
|
generator.indent();
|
|
602
|
-
generator.pushline(`"locale": ${
|
|
725
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
603
726
|
generator.push(`"resource": `);
|
|
604
727
|
}
|
|
605
728
|
break;
|
|
606
|
-
case "
|
|
729
|
+
case "JSONObjectExpression":
|
|
607
730
|
generator.push(`{`);
|
|
608
731
|
generator.indent();
|
|
609
|
-
propsCountStack.push(node2.
|
|
610
|
-
if (parent.type === "YAMLSequence") {
|
|
611
|
-
const lastIndex2 = itemsCountStack.length - 1;
|
|
612
|
-
const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
|
|
613
|
-
pathStack.push(currentCount.toString());
|
|
614
|
-
itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
|
|
615
|
-
}
|
|
616
|
-
break;
|
|
617
|
-
case "YAMLPair":
|
|
618
|
-
if (node2.value && node2.value.type === "YAMLScalar" && node2.key && node2.key.type === "YAMLScalar") {
|
|
619
|
-
const name = node2.key.value;
|
|
620
|
-
const value = node2.value.value;
|
|
621
|
-
if (shared.isString(value)) {
|
|
622
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
623
|
-
name && pathStack.push(name.toString());
|
|
624
|
-
const { code, map } = codegenFn(value, options, pathStack);
|
|
625
|
-
sourceMap && map != null && codeMaps.set(value, map);
|
|
626
|
-
generator.push(`${code}`, node2.value, value);
|
|
627
|
-
} else {
|
|
628
|
-
if (forceStringify) {
|
|
629
|
-
const strValue = JSON.stringify(value);
|
|
630
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
631
|
-
name && pathStack.push(name.toString());
|
|
632
|
-
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
633
|
-
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
634
|
-
generator.push(`${code}`, node2.value, strValue);
|
|
635
|
-
} else {
|
|
636
|
-
generator.push(
|
|
637
|
-
`${JSON.stringify(name)}: ${JSON.stringify(value)}`
|
|
638
|
-
);
|
|
639
|
-
name && pathStack.push(name.toString());
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
} else if (node2.value && (node2.value.type === "YAMLMapping" || node2.value.type === "YAMLSequence") && node2.key && node2.key.type === "YAMLScalar") {
|
|
643
|
-
const name = node2.key.value;
|
|
644
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
645
|
-
name && pathStack.push(name.toString());
|
|
646
|
-
}
|
|
647
|
-
const lastIndex = propsCountStack.length - 1;
|
|
648
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
732
|
+
propsCountStack.push(node2.properties.length);
|
|
649
733
|
break;
|
|
650
|
-
case "
|
|
734
|
+
case "JSONArrayExpression":
|
|
651
735
|
generator.push(`[`);
|
|
652
736
|
generator.indent();
|
|
653
|
-
|
|
654
|
-
const lastIndex2 = itemsCountStack.length - 1;
|
|
655
|
-
const currentCount = parent.entries.length - itemsCountStack[lastIndex2];
|
|
656
|
-
pathStack.push(currentCount.toString());
|
|
657
|
-
itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
|
|
658
|
-
}
|
|
659
|
-
itemsCountStack.push(node2.entries.length);
|
|
737
|
+
itemsCountStack.push(node2.elements.length);
|
|
660
738
|
break;
|
|
661
|
-
case "
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
const { code, map } = codegenFn(strValue, options, pathStack);
|
|
676
|
-
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
677
|
-
generator.push(`${code}`, node2, strValue);
|
|
678
|
-
} else {
|
|
679
|
-
generator.push(`${JSON.stringify(value)}`);
|
|
680
|
-
}
|
|
681
|
-
}
|
|
739
|
+
case "JSONProperty": {
|
|
740
|
+
const name = node2.key.type === "JSONLiteral" ? node2.key.value : node2.key.name;
|
|
741
|
+
const strName = JSON.stringify(name);
|
|
742
|
+
generator.push(`${strName}: `);
|
|
743
|
+
pathStack.push(name.toString());
|
|
744
|
+
if (node2.value.type === "JSONLiteral") {
|
|
745
|
+
const value2 = node2.value.value;
|
|
746
|
+
const strValue2 = JSON.stringify(value2);
|
|
747
|
+
if (shared.isString(value2)) {
|
|
748
|
+
generator.push(codegenFn(value2), node2.value, value2);
|
|
749
|
+
} else if (forceStringify) {
|
|
750
|
+
generator.push(codegenFn(strValue2), node2.value, strValue2);
|
|
751
|
+
} else {
|
|
752
|
+
generator.push(strValue2);
|
|
682
753
|
}
|
|
683
|
-
|
|
754
|
+
}
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
case "JSONLiteral":
|
|
758
|
+
if (parent.type !== "JSONArrayExpression") break;
|
|
759
|
+
const value = node2.value;
|
|
760
|
+
const strValue = JSON.stringify(value);
|
|
761
|
+
if (shared.isString(value)) {
|
|
762
|
+
generator.push(codegenFn(value), node2, value);
|
|
763
|
+
} else if (forceStringify) {
|
|
764
|
+
generator.push(codegenFn(strValue), node2, strValue);
|
|
765
|
+
} else {
|
|
766
|
+
generator.push(strValue);
|
|
684
767
|
}
|
|
685
768
|
break;
|
|
686
769
|
}
|
|
@@ -692,66 +775,61 @@ function _generate$1(generator, node, options = {}) {
|
|
|
692
775
|
generator.deindent();
|
|
693
776
|
generator.push(`})`);
|
|
694
777
|
generator.deindent();
|
|
695
|
-
generator.
|
|
778
|
+
generator.pushline(`}`);
|
|
696
779
|
} else if (type === "plain") {
|
|
697
780
|
generator.push(`
|
|
698
781
|
`);
|
|
699
782
|
generator.push("export default resource");
|
|
700
783
|
}
|
|
701
784
|
break;
|
|
702
|
-
case "
|
|
785
|
+
case "JSONObjectExpression":
|
|
703
786
|
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
704
787
|
pathStack.pop();
|
|
705
788
|
propsCountStack.pop();
|
|
706
789
|
}
|
|
707
790
|
generator.deindent();
|
|
708
791
|
generator.push(`}`);
|
|
709
|
-
if (parent.type === "YAMLSequence") {
|
|
710
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
711
|
-
pathStack.pop();
|
|
712
|
-
generator.pushline(`,`);
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
792
|
break;
|
|
716
|
-
case "
|
|
717
|
-
if (propsCountStack[propsCountStack.length - 1] !== 0) {
|
|
718
|
-
pathStack.pop();
|
|
719
|
-
generator.pushline(`,`);
|
|
720
|
-
}
|
|
721
|
-
break;
|
|
722
|
-
case "YAMLSequence":
|
|
793
|
+
case "JSONArrayExpression":
|
|
723
794
|
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
724
795
|
pathStack.pop();
|
|
725
796
|
itemsCountStack.pop();
|
|
726
797
|
}
|
|
727
798
|
generator.deindent();
|
|
728
799
|
generator.push(`]`);
|
|
729
|
-
if (parent.type === "YAMLSequence") {
|
|
730
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
731
|
-
pathStack.pop();
|
|
732
|
-
generator.pushline(`,`);
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
break;
|
|
736
|
-
case "YAMLScalar":
|
|
737
|
-
if (parent.type === "YAMLSequence") {
|
|
738
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
739
|
-
pathStack.pop();
|
|
740
|
-
generator.pushline(`,`);
|
|
741
|
-
} else {
|
|
742
|
-
generator.pushline(`,`);
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
800
|
break;
|
|
746
801
|
}
|
|
802
|
+
if (parent?.type === "JSONArrayExpression" || parent?.type === "JSONObjectExpression") {
|
|
803
|
+
const stackArr = node2.type === "JSONProperty" ? propsCountStack : itemsCountStack;
|
|
804
|
+
if (stackArr[stackArr.length - 1] !== 0) {
|
|
805
|
+
pathStack.pop();
|
|
806
|
+
generator.pushline(`,`);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
747
809
|
}
|
|
748
810
|
});
|
|
749
811
|
return codeMaps;
|
|
750
812
|
}
|
|
751
813
|
|
|
814
|
+
async function generate$1(targetSource, options) {
|
|
815
|
+
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
816
|
+
const _options = Object.assign({}, DEFAULT_OPTIONS, options, {
|
|
817
|
+
source: value
|
|
818
|
+
});
|
|
819
|
+
if (_options.filename && /.[c|m]?ts$/.test(_options.filename)) {
|
|
820
|
+
const transformed = await esbuild.transform(value, { loader: "ts" });
|
|
821
|
+
if (transformed && transformed.code) {
|
|
822
|
+
value = transformed.code;
|
|
823
|
+
_options.source = transformed.code;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
return generate$3(value, _options);
|
|
827
|
+
}
|
|
828
|
+
|
|
752
829
|
function generate(targetSource, {
|
|
753
830
|
type = "plain",
|
|
754
|
-
|
|
831
|
+
onlyLocales = [],
|
|
832
|
+
filename = "vue-i18n-loader.yaml",
|
|
755
833
|
inSourceMap = void 0,
|
|
756
834
|
locale = "",
|
|
757
835
|
isGlobal = false,
|
|
@@ -761,11 +839,9 @@ function generate(targetSource, {
|
|
|
761
839
|
onError = void 0,
|
|
762
840
|
strictMessage = true,
|
|
763
841
|
escapeHtml = false,
|
|
764
|
-
allowDynamic = false,
|
|
765
842
|
jit = false
|
|
766
843
|
}) {
|
|
767
|
-
|
|
768
|
-
const value = target;
|
|
844
|
+
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
769
845
|
const options = {
|
|
770
846
|
type,
|
|
771
847
|
source: value,
|
|
@@ -781,34 +857,24 @@ function generate(targetSource, {
|
|
|
781
857
|
escapeHtml,
|
|
782
858
|
jit
|
|
783
859
|
};
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
throw new Error(
|
|
801
|
-
`You need to define 'export default' that will return the locale messages.`
|
|
802
|
-
);
|
|
803
|
-
}
|
|
804
|
-
if (exportResult !== "object") {
|
|
805
|
-
return {
|
|
806
|
-
ast,
|
|
807
|
-
code: value,
|
|
808
|
-
map: inSourceMap
|
|
809
|
-
};
|
|
810
|
-
}
|
|
860
|
+
let ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
861
|
+
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
862
|
+
const messages = yamlEslintParser.getStaticYAMLValue(ast);
|
|
863
|
+
value = JSON.stringify(
|
|
864
|
+
excludeLocales({
|
|
865
|
+
messages,
|
|
866
|
+
onlyLocales
|
|
867
|
+
})
|
|
868
|
+
);
|
|
869
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
870
|
+
}
|
|
871
|
+
if (locale && onlyLocales?.length && !onlyLocales.includes(locale)) {
|
|
872
|
+
value = JSON.stringify({});
|
|
873
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
874
|
+
options.locale = "";
|
|
875
|
+
options.source = void 0;
|
|
811
876
|
}
|
|
877
|
+
const generator = createCodeGenerator(options);
|
|
812
878
|
const codeMaps = _generate(generator, ast, options);
|
|
813
879
|
const { code, map } = generator.context();
|
|
814
880
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
@@ -818,318 +884,145 @@ function generate(targetSource, {
|
|
|
818
884
|
map: newMap != null ? newMap : void 0
|
|
819
885
|
};
|
|
820
886
|
}
|
|
821
|
-
function scanAst(ast) {
|
|
822
|
-
if (ast.type !== "Program") {
|
|
823
|
-
throw new Error("Invalid AST: does not have Program node");
|
|
824
|
-
}
|
|
825
|
-
let ret = false;
|
|
826
|
-
for (const node of ast.body) {
|
|
827
|
-
if (node.type === "ExportDefaultDeclaration") {
|
|
828
|
-
if (node.declaration.type === "ObjectExpression") {
|
|
829
|
-
ret = "object";
|
|
830
|
-
break;
|
|
831
|
-
} else if (node.declaration.type === "FunctionDeclaration") {
|
|
832
|
-
ret = "function";
|
|
833
|
-
break;
|
|
834
|
-
} else if (node.declaration.type === "ArrowFunctionExpression") {
|
|
835
|
-
ret = "arrow-function";
|
|
836
|
-
break;
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
return ret;
|
|
841
|
-
}
|
|
842
887
|
function _generate(generator, node, options = {}) {
|
|
843
888
|
const propsCountStack = [];
|
|
844
889
|
const pathStack = [];
|
|
845
890
|
const itemsCountStack = [];
|
|
846
|
-
const skipStack = [];
|
|
847
891
|
const { forceStringify } = generator.context();
|
|
848
892
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
849
893
|
const { type, sourceMap, isGlobal, locale, jit } = options;
|
|
850
|
-
const
|
|
894
|
+
const _codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
895
|
+
function codegenFn(value) {
|
|
896
|
+
const { code, map } = _codegenFn(value, options, pathStack);
|
|
897
|
+
sourceMap && map != null && codeMaps.set(value, map);
|
|
898
|
+
return code;
|
|
899
|
+
}
|
|
851
900
|
const componentNamespace = "_Component";
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
901
|
+
yamlEslintParser.traverseNodes(node, {
|
|
902
|
+
enterNode(node2, parent) {
|
|
903
|
+
if (parent?.type === "YAMLSequence") {
|
|
904
|
+
const lastIndex = itemsCountStack.length - 1;
|
|
905
|
+
const currentCount = parent.entries.length - itemsCountStack[lastIndex];
|
|
906
|
+
pathStack.push(currentCount.toString());
|
|
907
|
+
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
908
|
+
} else if (parent?.type === "YAMLMapping") {
|
|
909
|
+
const lastIndex = propsCountStack.length - 1;
|
|
910
|
+
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
911
|
+
}
|
|
861
912
|
switch (node2.type) {
|
|
862
913
|
case "Program":
|
|
863
914
|
if (type === "plain") {
|
|
864
915
|
generator.push(`const resource = `);
|
|
865
916
|
} else if (type === "sfc") {
|
|
866
|
-
const variableName =
|
|
867
|
-
const localeName =
|
|
868
|
-
|
|
869
|
-
generator.push(`${exportSyntax} function (Component) {`);
|
|
917
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
918
|
+
const localeName = JSON.stringify(locale ?? `""`);
|
|
919
|
+
generator.push(`export default function (Component) {`);
|
|
870
920
|
generator.indent();
|
|
871
|
-
const
|
|
872
|
-
generator.pushline(
|
|
873
|
-
`const ${componentNamespace} = ${componentVariable}`
|
|
874
|
-
);
|
|
921
|
+
generator.pushline(`const ${componentNamespace} = Component`);
|
|
875
922
|
generator.pushline(
|
|
876
923
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
877
924
|
);
|
|
878
925
|
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
879
926
|
generator.indent();
|
|
880
|
-
generator.pushline(`"locale": ${
|
|
927
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
881
928
|
generator.push(`"resource": `);
|
|
882
929
|
}
|
|
883
930
|
break;
|
|
884
|
-
case "
|
|
931
|
+
case "YAMLMapping":
|
|
885
932
|
generator.push(`{`);
|
|
886
933
|
generator.indent();
|
|
887
|
-
propsCountStack.push(node2.
|
|
888
|
-
if (parent != null && parent.type === "ArrayExpression") {
|
|
889
|
-
const lastIndex = itemsCountStack.length - 1;
|
|
890
|
-
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
891
|
-
pathStack.push(currentCount.toString());
|
|
892
|
-
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
893
|
-
}
|
|
894
|
-
break;
|
|
895
|
-
case "Property":
|
|
896
|
-
if (parent != null && parent.type === "ObjectExpression") {
|
|
897
|
-
if (node2 != null) {
|
|
898
|
-
if (isJSONablePrimitiveLiteral(node2.value) && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
899
|
-
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
900
|
-
if (node2.value.type === "Literal" && shared.isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
|
|
901
|
-
const value = getValue(node2.value);
|
|
902
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
903
|
-
pathStack.push(name);
|
|
904
|
-
const { code, map } = codegenFn(value, options, pathStack);
|
|
905
|
-
sourceMap && map != null && codeMaps.set(value, map);
|
|
906
|
-
generator.push(`${code}`, node2.value, value);
|
|
907
|
-
skipStack.push(false);
|
|
908
|
-
} else {
|
|
909
|
-
const value = getValue(node2.value);
|
|
910
|
-
if (forceStringify) {
|
|
911
|
-
const strValue = JSON.stringify(value);
|
|
912
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
913
|
-
pathStack.push(name);
|
|
914
|
-
const { code, map } = codegenFn(
|
|
915
|
-
strValue,
|
|
916
|
-
options,
|
|
917
|
-
pathStack
|
|
918
|
-
);
|
|
919
|
-
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
920
|
-
generator.push(`${code}`, node2.value, strValue);
|
|
921
|
-
} else {
|
|
922
|
-
generator.push(
|
|
923
|
-
`${JSON.stringify(name)}: ${JSON.stringify(value)}`
|
|
924
|
-
);
|
|
925
|
-
pathStack.push(name);
|
|
926
|
-
}
|
|
927
|
-
skipStack.push(false);
|
|
928
|
-
}
|
|
929
|
-
} else if ((node2.value.type === "FunctionExpression" || node2.value.type === "ArrowFunctionExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
930
|
-
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
931
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
932
|
-
pathStack.push(name);
|
|
933
|
-
const code = escodegen.generate(node2.value);
|
|
934
|
-
generator.push(`${code}`, node2.value, code);
|
|
935
|
-
skipStack.push(false);
|
|
936
|
-
} else if ((node2.value.type === "ObjectExpression" || node2.value.type === "ArrayExpression") && (node2.key.type === "Literal" || node2.key.type === "Identifier")) {
|
|
937
|
-
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
938
|
-
generator.push(`${JSON.stringify(name)}: `);
|
|
939
|
-
pathStack.push(name);
|
|
940
|
-
} else {
|
|
941
|
-
skipStack.push(true);
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
const lastIndex = propsCountStack.length - 1;
|
|
945
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
946
|
-
}
|
|
934
|
+
propsCountStack.push(node2.pairs.length);
|
|
947
935
|
break;
|
|
948
|
-
case "
|
|
936
|
+
case "YAMLSequence":
|
|
949
937
|
generator.push(`[`);
|
|
950
938
|
generator.indent();
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
939
|
+
itemsCountStack.push(node2.entries.length);
|
|
940
|
+
break;
|
|
941
|
+
case "YAMLPair":
|
|
942
|
+
if (node2.key?.type !== "YAMLScalar") break;
|
|
943
|
+
if (node2.value?.type === "YAMLScalar") {
|
|
944
|
+
const name = node2.key.value;
|
|
945
|
+
const value = node2.value.value;
|
|
946
|
+
const strName = JSON.stringify(name);
|
|
947
|
+
const strValue = JSON.stringify(value);
|
|
948
|
+
name && pathStack.push(name.toString());
|
|
949
|
+
generator.push(`${strName}: `);
|
|
950
|
+
if (shared.isString(value)) {
|
|
951
|
+
generator.push(codegenFn(value), node2.value, value);
|
|
952
|
+
} else if (forceStringify) {
|
|
953
|
+
generator.push(codegenFn(strValue), node2.value, strValue);
|
|
954
|
+
} else {
|
|
955
|
+
generator.push(strValue);
|
|
956
|
+
}
|
|
957
|
+
} else if (node2.value?.type === "YAMLMapping" || node2.value?.type === "YAMLSequence") {
|
|
958
|
+
const name = node2.key.value;
|
|
959
|
+
name && pathStack.push(name.toString());
|
|
960
|
+
generator.push(`${JSON.stringify(name)}: `);
|
|
956
961
|
}
|
|
957
|
-
itemsCountStack.push(node2.elements.length);
|
|
958
962
|
break;
|
|
959
|
-
|
|
960
|
-
if (
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
sourceMap && map != null && codeMaps.set(value, map);
|
|
970
|
-
generator.push(`${code}`, node2, value);
|
|
971
|
-
} else {
|
|
972
|
-
const value = getValue(node2);
|
|
973
|
-
if (forceStringify) {
|
|
974
|
-
const strValue = JSON.stringify(value);
|
|
975
|
-
const { code, map } = codegenFn(
|
|
976
|
-
strValue,
|
|
977
|
-
options,
|
|
978
|
-
pathStack
|
|
979
|
-
);
|
|
980
|
-
sourceMap && map != null && codeMaps.set(strValue, map);
|
|
981
|
-
generator.push(`${code}`, node2, strValue);
|
|
982
|
-
} else {
|
|
983
|
-
generator.push(`${JSON.stringify(value)}`);
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
skipStack.push(false);
|
|
987
|
-
} else {
|
|
988
|
-
skipStack.push(true);
|
|
989
|
-
}
|
|
990
|
-
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
963
|
+
case "YAMLScalar":
|
|
964
|
+
if (parent.type === "YAMLSequence") {
|
|
965
|
+
const value = node2.value;
|
|
966
|
+
const strValue = JSON.stringify(value);
|
|
967
|
+
if (shared.isString(value)) {
|
|
968
|
+
generator.push(codegenFn(value), node2, value);
|
|
969
|
+
} else if (forceStringify) {
|
|
970
|
+
generator.push(codegenFn(strValue), node2, strValue);
|
|
971
|
+
} else {
|
|
972
|
+
generator.push(strValue);
|
|
991
973
|
}
|
|
992
974
|
}
|
|
993
975
|
break;
|
|
994
976
|
}
|
|
995
977
|
},
|
|
996
|
-
|
|
997
|
-
* NOTE:
|
|
998
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
999
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
1000
|
-
* so it's support only esm only ...
|
|
1001
|
-
*/
|
|
1002
|
-
// @ts-ignore
|
|
1003
|
-
leave(node2, parent) {
|
|
978
|
+
leaveNode(node2, parent) {
|
|
1004
979
|
switch (node2.type) {
|
|
1005
980
|
case "Program":
|
|
1006
981
|
if (type === "sfc") {
|
|
1007
982
|
generator.deindent();
|
|
1008
983
|
generator.push(`})`);
|
|
1009
984
|
generator.deindent();
|
|
1010
|
-
generator.
|
|
985
|
+
generator.push(`}`);
|
|
1011
986
|
} else if (type === "plain") {
|
|
1012
987
|
generator.push(`
|
|
1013
988
|
`);
|
|
1014
989
|
generator.push("export default resource");
|
|
1015
990
|
}
|
|
1016
991
|
break;
|
|
1017
|
-
case "
|
|
992
|
+
case "YAMLMapping":
|
|
1018
993
|
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
1019
994
|
pathStack.pop();
|
|
1020
995
|
propsCountStack.pop();
|
|
1021
996
|
}
|
|
1022
997
|
generator.deindent();
|
|
1023
998
|
generator.push(`}`);
|
|
1024
|
-
if (parent != null && parent.type === "ArrayExpression") {
|
|
1025
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
1026
|
-
pathStack.pop();
|
|
1027
|
-
generator.pushline(`,`);
|
|
1028
|
-
}
|
|
1029
|
-
}
|
|
1030
999
|
break;
|
|
1031
|
-
case "
|
|
1032
|
-
if (parent != null && parent.type === "ObjectExpression") {
|
|
1033
|
-
if (propsCountStack[propsCountStack.length - 1] !== 0) {
|
|
1034
|
-
pathStack.pop();
|
|
1035
|
-
if (!skipStack.pop()) {
|
|
1036
|
-
generator.pushline(`,`);
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
break;
|
|
1041
|
-
case "ArrayExpression":
|
|
1000
|
+
case "YAMLSequence":
|
|
1042
1001
|
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
1043
1002
|
pathStack.pop();
|
|
1044
1003
|
itemsCountStack.pop();
|
|
1045
1004
|
}
|
|
1046
1005
|
generator.deindent();
|
|
1047
1006
|
generator.push(`]`);
|
|
1048
|
-
if (parent != null && parent.type === "ArrayExpression") {
|
|
1049
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
1050
|
-
pathStack.pop();
|
|
1051
|
-
if (!skipStack.pop()) {
|
|
1052
|
-
generator.pushline(`,`);
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
break;
|
|
1057
|
-
case "Literal":
|
|
1058
|
-
if (parent != null && parent.type === "ArrayExpression") {
|
|
1059
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
1060
|
-
pathStack.pop();
|
|
1061
|
-
if (!skipStack.pop()) {
|
|
1062
|
-
generator.pushline(`,`);
|
|
1063
|
-
}
|
|
1064
|
-
} else {
|
|
1065
|
-
if (!skipStack.pop()) {
|
|
1066
|
-
generator.pushline(`,`);
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
1007
|
break;
|
|
1071
1008
|
}
|
|
1009
|
+
const stackArr = node2.type === "YAMLPair" ? propsCountStack : itemsCountStack;
|
|
1010
|
+
if (parent?.type === "YAMLSequence" || parent?.type === "YAMLMapping") {
|
|
1011
|
+
if (stackArr[stackArr.length - 1] !== 0) {
|
|
1012
|
+
pathStack.pop();
|
|
1013
|
+
generator.pushline(`,`);
|
|
1014
|
+
} else if (node2.type === "YAMLScalar") {
|
|
1015
|
+
generator.pushline(`,`);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1072
1018
|
}
|
|
1073
1019
|
});
|
|
1074
1020
|
return codeMaps;
|
|
1075
1021
|
}
|
|
1076
|
-
function isJSONablePrimitiveLiteral(node) {
|
|
1077
|
-
return node.type === "Literal" && (shared.isString(node.value) || shared.isNumber(node.value) || shared.isBoolean(node.value) || node.value === null) || node.type === "TemplateLiteral";
|
|
1078
|
-
}
|
|
1079
|
-
function getValue(node) {
|
|
1080
|
-
return node.type === "Literal" ? node.value : node.type === "TemplateLiteral" ? node.quasis.map((quasi) => quasi.value.cooked).join("") : void 0;
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
const _require = module__default.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
1084
|
-
function checkInstallPackage(pkg, debug) {
|
|
1085
|
-
let installedVueI18n = false;
|
|
1086
|
-
try {
|
|
1087
|
-
debug(`vue-i18n load path: ${_require.resolve("vue-i18n")}`);
|
|
1088
|
-
installedVueI18n = true;
|
|
1089
|
-
} catch (e) {
|
|
1090
|
-
debug(`cannot find 'vue-i18n'`, e);
|
|
1091
|
-
}
|
|
1092
|
-
let installedPetiteVueI18n = false;
|
|
1093
|
-
try {
|
|
1094
|
-
debug(`petite-vue-i18n load path: ${_require.resolve("petite-vue-i18n")}`);
|
|
1095
|
-
installedPetiteVueI18n = true;
|
|
1096
|
-
} catch (e) {
|
|
1097
|
-
debug(`cannot find 'petite-vue-i18n'`, e);
|
|
1098
|
-
}
|
|
1099
|
-
if (installedVueI18n) {
|
|
1100
|
-
return "vue-i18n";
|
|
1101
|
-
}
|
|
1102
|
-
if (installedPetiteVueI18n) {
|
|
1103
|
-
return "petite-vue-i18n";
|
|
1104
|
-
}
|
|
1105
|
-
throw new Error(
|
|
1106
|
-
`${pkg} requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
|
|
1107
|
-
);
|
|
1108
|
-
}
|
|
1109
|
-
function getVueI18nVersion(debug, pkg = "vue-i18n") {
|
|
1110
|
-
const VueI18n = loadModule(pkg, debug);
|
|
1111
|
-
if (VueI18n == null) {
|
|
1112
|
-
return "";
|
|
1113
|
-
}
|
|
1114
|
-
if (VueI18n.VERSION && VueI18n.VERSION.startsWith("9.")) {
|
|
1115
|
-
return "9";
|
|
1116
|
-
}
|
|
1117
|
-
if (VueI18n.VERSION && VueI18n.VERSION.startsWith("10.")) {
|
|
1118
|
-
return "10";
|
|
1119
|
-
}
|
|
1120
|
-
return "unknown";
|
|
1121
|
-
}
|
|
1122
|
-
function loadModule(moduleName, debug) {
|
|
1123
|
-
try {
|
|
1124
|
-
return _require(moduleName);
|
|
1125
|
-
} catch (e) {
|
|
1126
|
-
debug(`cannot load '${moduleName}'`, e);
|
|
1127
|
-
return null;
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
1022
|
|
|
1131
1023
|
exports.checkInstallPackage = checkInstallPackage;
|
|
1132
1024
|
exports.generateJSON = generate$2;
|
|
1133
|
-
exports.generateJavaScript = generate;
|
|
1134
|
-
exports.
|
|
1025
|
+
exports.generateJavaScript = generate$3;
|
|
1026
|
+
exports.generateTypescript = generate$1;
|
|
1027
|
+
exports.generateYAML = generate;
|
|
1135
1028
|
exports.getVueI18nVersion = getVueI18nVersion;
|