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