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