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