@intlify/bundle-utils 11.0.0-beta.0 → 11.0.0-beta.4
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 +459 -630
- package/lib/index.d.cts +23 -14
- package/lib/index.d.mts +23 -14
- package/lib/index.d.ts +23 -14
- package/lib/index.mjs +457 -628
- package/package.json +9 -18
package/lib/index.mjs
CHANGED
|
@@ -1,13 +1,61 @@
|
|
|
1
|
-
import
|
|
1
|
+
import module from 'node:module';
|
|
2
2
|
import { isBoolean, format, escapeHtml, friendlyJSONstringify, isString, isNumber } from '@intlify/shared';
|
|
3
|
-
import {
|
|
3
|
+
import { parse } from 'acorn';
|
|
4
|
+
import { generate as generate$4 } from 'escodegen';
|
|
5
|
+
import { walk } from 'estree-walker';
|
|
4
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';
|
|
5
10
|
import { parseYAML, getStaticYAMLValue, traverseNodes as traverseNodes$1 } from 'yaml-eslint-parser';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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,248 +315,312 @@ function excludeLocales({
|
|
|
267
315
|
return _messages;
|
|
268
316
|
}
|
|
269
317
|
|
|
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
|
-
|
|
316
|
-
|
|
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
|
+
}
|
|
317
369
|
}
|
|
318
|
-
const
|
|
319
|
-
const codeMaps = _generate$2(generator, ast, options);
|
|
370
|
+
const codeMaps = _generate$2(generator, ast, _options);
|
|
320
371
|
const { code, map } = generator.context();
|
|
321
|
-
const newMap = map
|
|
372
|
+
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, _options.inSourceMap) || null : null;
|
|
322
373
|
return {
|
|
323
374
|
ast,
|
|
324
375
|
code,
|
|
325
376
|
map: newMap != null ? newMap : void 0
|
|
326
377
|
};
|
|
327
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
|
+
}
|
|
328
396
|
function _generate$2(generator, node, options = {}) {
|
|
329
397
|
const propsCountStack = [];
|
|
330
398
|
const pathStack = [];
|
|
331
399
|
const itemsCountStack = [];
|
|
400
|
+
const skipStack = [];
|
|
332
401
|
const { forceStringify } = generator.context();
|
|
333
402
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
334
403
|
const { type, sourceMap, isGlobal, locale, jit } = options;
|
|
335
|
-
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
|
+
}
|
|
336
410
|
const componentNamespace = "_Component";
|
|
337
|
-
|
|
338
|
-
|
|
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
|
+
}
|
|
339
471
|
switch (node2.type) {
|
|
340
472
|
case "Program":
|
|
341
473
|
if (type === "plain") {
|
|
342
474
|
generator.push(`const resource = `);
|
|
343
475
|
} else if (type === "sfc") {
|
|
344
|
-
const
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
generator.push(`${exportSyntax} function (Component) {`);
|
|
476
|
+
const localeName = JSON.stringify(locale ?? '""');
|
|
477
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
478
|
+
generator.push(`export default function (Component) {`);
|
|
348
479
|
generator.indent();
|
|
349
|
-
const
|
|
350
|
-
generator.pushline(
|
|
351
|
-
`const ${componentNamespace} = ${componentVariable}`
|
|
352
|
-
);
|
|
480
|
+
generator.pushline(`const ${componentNamespace} = Component`);
|
|
353
481
|
generator.pushline(
|
|
354
482
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
355
483
|
);
|
|
356
484
|
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
357
485
|
generator.indent();
|
|
358
|
-
generator.pushline(`"locale": ${
|
|
486
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
359
487
|
generator.push(`"resource": `);
|
|
360
488
|
}
|
|
361
489
|
break;
|
|
362
|
-
case "
|
|
363
|
-
generator.push(
|
|
490
|
+
case "ObjectExpression":
|
|
491
|
+
generator.push("{");
|
|
364
492
|
generator.indent();
|
|
365
493
|
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
494
|
break;
|
|
373
|
-
case "
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
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);
|
|
383
514
|
} else {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
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);
|
|
397
538
|
}
|
|
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
539
|
}
|
|
403
|
-
const lastIndex = propsCountStack.length - 1;
|
|
404
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
405
540
|
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);
|
|
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}`);
|
|
416
544
|
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);
|
|
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);
|
|
428
554
|
} 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
|
-
}
|
|
555
|
+
generator.push(strValue);
|
|
437
556
|
}
|
|
557
|
+
skipStack.push(false);
|
|
558
|
+
} else {
|
|
559
|
+
skipStack.push(true);
|
|
438
560
|
}
|
|
439
|
-
itemsCountStack[lastIndex2] = --itemsCountStack[lastIndex2];
|
|
440
561
|
}
|
|
441
562
|
break;
|
|
442
563
|
}
|
|
443
564
|
},
|
|
444
|
-
|
|
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) {
|
|
445
573
|
switch (node2.type) {
|
|
446
574
|
case "Program":
|
|
447
|
-
if (type === "
|
|
575
|
+
if (type === "plain") {
|
|
576
|
+
generator.push("\n");
|
|
577
|
+
generator.push("export default resource");
|
|
578
|
+
} else if (type === "sfc") {
|
|
448
579
|
generator.deindent();
|
|
449
|
-
generator.push(
|
|
580
|
+
generator.push("})");
|
|
450
581
|
generator.deindent();
|
|
451
|
-
generator.pushline(
|
|
452
|
-
} else if (type === "plain") {
|
|
453
|
-
generator.push(`
|
|
454
|
-
`);
|
|
455
|
-
generator.push("export default resource");
|
|
582
|
+
generator.pushline("}");
|
|
456
583
|
}
|
|
457
584
|
break;
|
|
458
|
-
case "
|
|
585
|
+
case "ObjectExpression":
|
|
459
586
|
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
460
587
|
pathStack.pop();
|
|
461
588
|
propsCountStack.pop();
|
|
462
589
|
}
|
|
463
590
|
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
|
-
}
|
|
471
|
-
break;
|
|
472
|
-
case "JSONProperty":
|
|
473
|
-
if (propsCountStack[propsCountStack.length - 1] !== 0) {
|
|
474
|
-
pathStack.pop();
|
|
475
|
-
generator.pushline(`,`);
|
|
476
|
-
}
|
|
591
|
+
generator.push("}");
|
|
477
592
|
break;
|
|
478
|
-
case "
|
|
593
|
+
case "ArrayExpression":
|
|
479
594
|
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
480
595
|
pathStack.pop();
|
|
481
596
|
itemsCountStack.pop();
|
|
482
597
|
}
|
|
483
598
|
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
|
-
}
|
|
599
|
+
generator.push("]");
|
|
501
600
|
break;
|
|
502
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
|
+
}
|
|
503
609
|
}
|
|
504
610
|
});
|
|
505
611
|
return codeMaps;
|
|
506
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
|
+
}
|
|
507
619
|
|
|
508
|
-
function generate$
|
|
620
|
+
function generate$2(targetSource, {
|
|
509
621
|
type = "plain",
|
|
510
622
|
onlyLocales = [],
|
|
511
|
-
filename = "vue-i18n-loader.
|
|
623
|
+
filename = "vue-i18n-loader.json",
|
|
512
624
|
inSourceMap = void 0,
|
|
513
625
|
locale = "",
|
|
514
626
|
isGlobal = false,
|
|
@@ -536,27 +648,27 @@ function generate$1(targetSource, {
|
|
|
536
648
|
escapeHtml,
|
|
537
649
|
jit
|
|
538
650
|
};
|
|
539
|
-
let ast =
|
|
651
|
+
let ast = parseJSON(value, { filePath: filename });
|
|
540
652
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
541
|
-
const messages =
|
|
653
|
+
const messages = getStaticJSONValue(ast);
|
|
542
654
|
value = JSON.stringify(
|
|
543
655
|
excludeLocales({
|
|
544
656
|
messages,
|
|
545
657
|
onlyLocales
|
|
546
658
|
})
|
|
547
659
|
);
|
|
548
|
-
ast =
|
|
660
|
+
ast = parseJSON(value, { filePath: filename });
|
|
549
661
|
}
|
|
550
662
|
if (locale && onlyLocales?.length && !onlyLocales.includes(locale)) {
|
|
551
663
|
value = JSON.stringify({});
|
|
552
|
-
ast =
|
|
664
|
+
ast = parseJSON(value, { filePath: filename });
|
|
553
665
|
options.locale = "";
|
|
554
666
|
options.source = void 0;
|
|
555
667
|
}
|
|
556
668
|
const generator = createCodeGenerator(options);
|
|
557
669
|
const codeMaps = _generate$1(generator, ast, options);
|
|
558
670
|
const { code, map } = generator.context();
|
|
559
|
-
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
671
|
+
const newMap = map && !jit ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
560
672
|
return {
|
|
561
673
|
ast,
|
|
562
674
|
code,
|
|
@@ -570,111 +682,81 @@ function _generate$1(generator, node, options = {}) {
|
|
|
570
682
|
const { forceStringify } = generator.context();
|
|
571
683
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
572
684
|
const { type, sourceMap, isGlobal, locale, jit } = options;
|
|
573
|
-
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
|
+
}
|
|
574
691
|
const componentNamespace = "_Component";
|
|
575
|
-
traverseNodes
|
|
692
|
+
traverseNodes(node, {
|
|
576
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
|
+
}
|
|
577
703
|
switch (node2.type) {
|
|
578
704
|
case "Program":
|
|
579
705
|
if (type === "plain") {
|
|
580
706
|
generator.push(`const resource = `);
|
|
581
707
|
} else if (type === "sfc") {
|
|
582
|
-
const variableName =
|
|
583
|
-
const localeName =
|
|
584
|
-
|
|
585
|
-
generator.push(`${exportSyntax} function (Component) {`);
|
|
708
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
709
|
+
const localeName = JSON.stringify(locale ?? `""`);
|
|
710
|
+
generator.push(`export default function (Component) {`);
|
|
586
711
|
generator.indent();
|
|
587
|
-
const
|
|
588
|
-
generator.pushline(
|
|
589
|
-
`const ${componentNamespace} = ${componentVariable}`
|
|
590
|
-
);
|
|
712
|
+
generator.pushline(`const ${componentNamespace} = Component`);
|
|
591
713
|
generator.pushline(
|
|
592
714
|
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
593
715
|
);
|
|
594
716
|
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
595
717
|
generator.indent();
|
|
596
|
-
generator.pushline(`"locale": ${
|
|
718
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
597
719
|
generator.push(`"resource": `);
|
|
598
720
|
}
|
|
599
721
|
break;
|
|
600
|
-
case "
|
|
722
|
+
case "JSONObjectExpression":
|
|
601
723
|
generator.push(`{`);
|
|
602
724
|
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];
|
|
725
|
+
propsCountStack.push(node2.properties.length);
|
|
643
726
|
break;
|
|
644
|
-
case "
|
|
727
|
+
case "JSONArrayExpression":
|
|
645
728
|
generator.push(`[`);
|
|
646
729
|
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);
|
|
730
|
+
itemsCountStack.push(node2.elements.length);
|
|
654
731
|
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
|
-
}
|
|
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);
|
|
676
746
|
}
|
|
677
|
-
|
|
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);
|
|
678
760
|
}
|
|
679
761
|
break;
|
|
680
762
|
}
|
|
@@ -686,68 +768,61 @@ function _generate$1(generator, node, options = {}) {
|
|
|
686
768
|
generator.deindent();
|
|
687
769
|
generator.push(`})`);
|
|
688
770
|
generator.deindent();
|
|
689
|
-
generator.
|
|
771
|
+
generator.pushline(`}`);
|
|
690
772
|
} else if (type === "plain") {
|
|
691
773
|
generator.push(`
|
|
692
774
|
`);
|
|
693
775
|
generator.push("export default resource");
|
|
694
776
|
}
|
|
695
777
|
break;
|
|
696
|
-
case "
|
|
778
|
+
case "JSONObjectExpression":
|
|
697
779
|
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
698
780
|
pathStack.pop();
|
|
699
781
|
propsCountStack.pop();
|
|
700
782
|
}
|
|
701
783
|
generator.deindent();
|
|
702
784
|
generator.push(`}`);
|
|
703
|
-
if (parent.type === "YAMLSequence") {
|
|
704
|
-
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
|
|
705
|
-
pathStack.pop();
|
|
706
|
-
generator.pushline(`,`);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
785
|
break;
|
|
710
|
-
case "
|
|
711
|
-
if (propsCountStack[propsCountStack.length - 1] !== 0) {
|
|
712
|
-
pathStack.pop();
|
|
713
|
-
generator.pushline(`,`);
|
|
714
|
-
}
|
|
715
|
-
break;
|
|
716
|
-
case "YAMLSequence":
|
|
786
|
+
case "JSONArrayExpression":
|
|
717
787
|
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
718
788
|
pathStack.pop();
|
|
719
789
|
itemsCountStack.pop();
|
|
720
790
|
}
|
|
721
791
|
generator.deindent();
|
|
722
792
|
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
793
|
break;
|
|
740
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
|
+
}
|
|
741
802
|
}
|
|
742
803
|
});
|
|
743
804
|
return codeMaps;
|
|
744
805
|
}
|
|
745
806
|
|
|
746
|
-
|
|
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);
|
|
747
820
|
}
|
|
821
|
+
|
|
748
822
|
function generate(targetSource, {
|
|
749
823
|
type = "plain",
|
|
750
|
-
|
|
824
|
+
onlyLocales = [],
|
|
825
|
+
filename = "vue-i18n-loader.yaml",
|
|
751
826
|
inSourceMap = void 0,
|
|
752
827
|
locale = "",
|
|
753
828
|
isGlobal = false,
|
|
@@ -755,14 +830,11 @@ function generate(targetSource, {
|
|
|
755
830
|
env = "development",
|
|
756
831
|
forceStringify = false,
|
|
757
832
|
onError = void 0,
|
|
758
|
-
onWarn = void 0,
|
|
759
833
|
strictMessage = true,
|
|
760
834
|
escapeHtml = false,
|
|
761
|
-
allowDynamic = false,
|
|
762
835
|
jit = false
|
|
763
836
|
}) {
|
|
764
|
-
|
|
765
|
-
let value = target;
|
|
837
|
+
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
766
838
|
const options = {
|
|
767
839
|
type,
|
|
768
840
|
source: value,
|
|
@@ -774,60 +846,28 @@ function generate(targetSource, {
|
|
|
774
846
|
filename,
|
|
775
847
|
forceStringify,
|
|
776
848
|
onError,
|
|
777
|
-
onWarn,
|
|
778
849
|
strictMessage,
|
|
779
850
|
escapeHtml,
|
|
780
851
|
jit
|
|
781
852
|
};
|
|
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
|
-
}
|
|
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 });
|
|
798
863
|
}
|
|
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
|
-
}
|
|
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;
|
|
830
869
|
}
|
|
870
|
+
const generator = createCodeGenerator(options);
|
|
831
871
|
const codeMaps = _generate(generator, ast, options);
|
|
832
872
|
const { code, map } = generator.context();
|
|
833
873
|
const newMap = map ? mapLinesColumns(map.toJSON(), codeMaps, inSourceMap) || null : null;
|
|
@@ -837,94 +877,38 @@ function generate(targetSource, {
|
|
|
837
877
|
map: newMap != null ? newMap : void 0
|
|
838
878
|
};
|
|
839
879
|
}
|
|
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
880
|
function _generate(generator, node, options = {}) {
|
|
859
881
|
const propsCountStack = [];
|
|
860
882
|
const pathStack = [];
|
|
861
883
|
const itemsCountStack = [];
|
|
862
|
-
const skipStack = [];
|
|
863
884
|
const { forceStringify } = generator.context();
|
|
864
885
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
865
886
|
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
|
-
);
|
|
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;
|
|
903
892
|
}
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
case "ImportDeclaration":
|
|
916
|
-
case "VariableDeclaration":
|
|
917
|
-
case "VariableDeclarator":
|
|
918
|
-
this.skip();
|
|
919
|
-
}
|
|
893
|
+
const componentNamespace = "_Component";
|
|
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];
|
|
920
904
|
}
|
|
921
905
|
switch (node2.type) {
|
|
922
906
|
case "Program":
|
|
923
907
|
if (type === "plain") {
|
|
924
908
|
generator.push(`const resource = `);
|
|
925
909
|
} else if (type === "sfc") {
|
|
926
|
-
const variableName =
|
|
927
|
-
const localeName =
|
|
910
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
911
|
+
const localeName = JSON.stringify(locale ?? `""`);
|
|
928
912
|
generator.push(`export default function (Component) {`);
|
|
929
913
|
generator.indent();
|
|
930
914
|
generator.pushline(`const ${componentNamespace} = Component`);
|
|
@@ -933,255 +917,100 @@ Variable declarations are not optimized - found ${variableDeclarations.join(", "
|
|
|
933
917
|
);
|
|
934
918
|
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
935
919
|
generator.indent();
|
|
936
|
-
generator.pushline(`"locale": ${
|
|
920
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
937
921
|
generator.push(`"resource": `);
|
|
938
922
|
}
|
|
939
923
|
break;
|
|
940
|
-
case "
|
|
924
|
+
case "YAMLMapping":
|
|
941
925
|
generator.push(`{`);
|
|
942
926
|
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
|
-
}
|
|
927
|
+
propsCountStack.push(node2.pairs.length);
|
|
1007
928
|
break;
|
|
1008
|
-
case "
|
|
929
|
+
case "YAMLSequence":
|
|
1009
930
|
generator.push(`[`);
|
|
1010
931
|
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);
|
|
932
|
+
itemsCountStack.push(node2.entries.length);
|
|
1018
933
|
break;
|
|
1019
|
-
case "
|
|
1020
|
-
|
|
1021
|
-
|
|
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)}: `);
|
|
954
|
+
}
|
|
1022
955
|
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);
|
|
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);
|
|
1046
964
|
} else {
|
|
1047
|
-
|
|
965
|
+
generator.push(strValue);
|
|
1048
966
|
}
|
|
1049
|
-
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
1050
967
|
}
|
|
1051
968
|
break;
|
|
1052
969
|
}
|
|
1053
970
|
},
|
|
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) {
|
|
971
|
+
leaveNode(node2, parent) {
|
|
1062
972
|
switch (node2.type) {
|
|
1063
973
|
case "Program":
|
|
1064
974
|
if (type === "sfc") {
|
|
1065
975
|
generator.deindent();
|
|
1066
976
|
generator.push(`})`);
|
|
1067
977
|
generator.deindent();
|
|
1068
|
-
generator.
|
|
978
|
+
generator.push(`}`);
|
|
1069
979
|
} else if (type === "plain") {
|
|
1070
980
|
generator.push(`
|
|
1071
981
|
`);
|
|
1072
982
|
generator.push("export default resource");
|
|
1073
983
|
}
|
|
1074
984
|
break;
|
|
1075
|
-
case "
|
|
985
|
+
case "YAMLMapping":
|
|
1076
986
|
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
1077
987
|
pathStack.pop();
|
|
1078
988
|
propsCountStack.pop();
|
|
1079
989
|
}
|
|
1080
990
|
generator.deindent();
|
|
1081
991
|
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
|
-
break;
|
|
1091
|
-
case "Property":
|
|
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
992
|
break;
|
|
1101
|
-
case "
|
|
993
|
+
case "YAMLSequence":
|
|
1102
994
|
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
1103
995
|
pathStack.pop();
|
|
1104
996
|
itemsCountStack.pop();
|
|
1105
997
|
}
|
|
1106
998
|
generator.deindent();
|
|
1107
999
|
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
1000
|
break;
|
|
1127
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
|
+
}
|
|
1128
1011
|
}
|
|
1129
1012
|
});
|
|
1130
1013
|
return codeMaps;
|
|
1131
1014
|
}
|
|
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
1015
|
|
|
1187
|
-
export {
|
|
1016
|
+
export { checkInstallPackage, generate$2 as generateJSON, generate$3 as generateJavaScript, generate$1 as generateTypescript, generate as generateYAML, getVueI18nVersion };
|