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