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