@intlify/bundle-utils 11.2.5 → 11.2.7
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 +225 -209
- package/lib/index.mjs +213 -209
- package/package.json +3 -6
package/lib/index.cjs
CHANGED
|
@@ -3,8 +3,6 @@
|
|
|
3
3
|
const module$1 = require('node:module');
|
|
4
4
|
const shared = require('@intlify/shared');
|
|
5
5
|
const acorn = require('acorn');
|
|
6
|
-
const escodegen = require('escodegen');
|
|
7
|
-
const estreeWalker = require('estree-walker');
|
|
8
6
|
const messageCompiler = require('@intlify/message-compiler');
|
|
9
7
|
const sourceMapJs = require('source-map-js');
|
|
10
8
|
const jsoncEslintParser = require('jsonc-eslint-parser');
|
|
@@ -14,7 +12,20 @@ const yamlEslintParser = require('yaml-eslint-parser');
|
|
|
14
12
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
15
13
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
16
14
|
|
|
15
|
+
function _interopNamespaceCompat(e) {
|
|
16
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
17
|
+
const n = Object.create(null);
|
|
18
|
+
if (e) {
|
|
19
|
+
for (const k in e) {
|
|
20
|
+
n[k] = e[k];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
const module__default = /*#__PURE__*/_interopDefaultCompat(module$1);
|
|
28
|
+
const messageCompiler__namespace = /*#__PURE__*/_interopNamespaceCompat(messageCompiler);
|
|
18
29
|
|
|
19
30
|
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
31
|
function checkInstallPackage(pkg, debug) {
|
|
@@ -160,6 +171,14 @@ const ON_ERROR_NOOP = () => {
|
|
|
160
171
|
function parsePath(path) {
|
|
161
172
|
return path ? path.join(".") : "";
|
|
162
173
|
}
|
|
174
|
+
const COMPILE_ERROR_MESSAGES = messageCompiler__namespace.errorMessages;
|
|
175
|
+
function getCompileErrorMessage(err) {
|
|
176
|
+
if (err.message !== String(err.code)) {
|
|
177
|
+
return err.message;
|
|
178
|
+
}
|
|
179
|
+
const text = COMPILE_ERROR_MESSAGES?.[err.code];
|
|
180
|
+
return text ? text.replace(/:\s*\\?'?\{\d+\}'?/g, "") : err.message;
|
|
181
|
+
}
|
|
163
182
|
function generateMessageFunction(msg, options = {}, path) {
|
|
164
183
|
const env = options.env != null ? options.env : "development";
|
|
165
184
|
const strictMessage = shared.isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
@@ -188,7 +207,7 @@ function generateMessageFunction(msg, options = {}, path) {
|
|
|
188
207
|
domain: err.domain,
|
|
189
208
|
location: err.location
|
|
190
209
|
};
|
|
191
|
-
onError(err
|
|
210
|
+
onError(getCompileErrorMessage(err), extra);
|
|
192
211
|
errors.push(err);
|
|
193
212
|
}
|
|
194
213
|
};
|
|
@@ -300,7 +319,7 @@ function generateResourceAst(msg, options = {}, path) {
|
|
|
300
319
|
domain: err.domain,
|
|
301
320
|
location: err.location
|
|
302
321
|
};
|
|
303
|
-
onError(err
|
|
322
|
+
onError(getCompileErrorMessage(err), extra);
|
|
304
323
|
errors.push(err);
|
|
305
324
|
}
|
|
306
325
|
};
|
|
@@ -341,6 +360,30 @@ function filterMultiLocaleMessages(messages, shouldKeep) {
|
|
|
341
360
|
}
|
|
342
361
|
return result;
|
|
343
362
|
}
|
|
363
|
+
function describeResource({ type, filename }) {
|
|
364
|
+
return type === "sfc" ? `the i18n custom block of ${filename}` : `${filename}`;
|
|
365
|
+
}
|
|
366
|
+
function describeValue(value) {
|
|
367
|
+
if (Array.isArray(value)) return "an array";
|
|
368
|
+
if (value === void 0) return "undefined";
|
|
369
|
+
return `a ${typeof value}`;
|
|
370
|
+
}
|
|
371
|
+
function stringifyValue(value) {
|
|
372
|
+
if (typeof value === "number") return Object.is(value, -0) ? "-0" : String(value);
|
|
373
|
+
if (value === void 0) return "undefined";
|
|
374
|
+
return JSON.stringify(value);
|
|
375
|
+
}
|
|
376
|
+
function createResourceError(message, options) {
|
|
377
|
+
return new Error(`${message} in ${describeResource(options)}.`);
|
|
378
|
+
}
|
|
379
|
+
function createResourceParseError(err, options, columnOffset = 0) {
|
|
380
|
+
if (!(err instanceof Error)) return err;
|
|
381
|
+
const { lineNumber, column } = err;
|
|
382
|
+
const position = lineNumber != null ? ` (${lineNumber}:${column + columnOffset})` : "";
|
|
383
|
+
return new SyntaxError(`${err.message} in ${describeResource(options)}${position}`, {
|
|
384
|
+
cause: err
|
|
385
|
+
});
|
|
386
|
+
}
|
|
344
387
|
function excludeLocales({
|
|
345
388
|
messages,
|
|
346
389
|
onlyLocales
|
|
@@ -433,222 +476,121 @@ function scanAst(ast) {
|
|
|
433
476
|
return false;
|
|
434
477
|
}
|
|
435
478
|
function _generate$2(generator, node, options = {}) {
|
|
436
|
-
const propsCountStack = [];
|
|
437
|
-
const pathStack = [];
|
|
438
|
-
const itemsCountStack = [];
|
|
439
|
-
const skipStack = [];
|
|
440
479
|
const { forceStringify } = generator.context();
|
|
441
480
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
442
481
|
const { type, sourceMap, isGlobal, locale, jit } = options;
|
|
482
|
+
const source = options.source ?? "";
|
|
443
483
|
const _codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
444
|
-
function codegenFn(value) {
|
|
445
|
-
const { code, map } = _codegenFn(value, options,
|
|
484
|
+
function codegenFn(value, path) {
|
|
485
|
+
const { code, map } = _codegenFn(value, options, path);
|
|
446
486
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
447
487
|
return code;
|
|
448
488
|
}
|
|
449
489
|
const componentNamespace = "_Component";
|
|
450
490
|
const variableDeclarations = [];
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
break;
|
|
465
|
-
case "ImportDeclaration":
|
|
466
|
-
generator.push(options.source?.slice(node2.start, node2.end));
|
|
467
|
-
generator.newline();
|
|
468
|
-
break;
|
|
469
|
-
case "VariableDeclaration":
|
|
470
|
-
generator.push(options.source?.slice(node2.start, node2.end));
|
|
471
|
-
generator.newline();
|
|
472
|
-
variableDeclarations.push(
|
|
473
|
-
...node2.declarations.map((x) => `\`${x.id.name}\``)
|
|
474
|
-
);
|
|
475
|
-
break;
|
|
476
|
-
}
|
|
491
|
+
const { body } = node;
|
|
492
|
+
const exportDefault = body.find(
|
|
493
|
+
(statement) => statement.type === "ExportDefaultDeclaration"
|
|
494
|
+
);
|
|
495
|
+
const resource = exportDefault.declaration;
|
|
496
|
+
for (const statement of body) {
|
|
497
|
+
if (statement === exportDefault) continue;
|
|
498
|
+
generator.push(sourceOf(source, statement));
|
|
499
|
+
generator.newline();
|
|
500
|
+
if (statement.type === "VariableDeclaration") {
|
|
501
|
+
variableDeclarations.push(
|
|
502
|
+
...statement.declarations.map((x) => `\`${x.id.name}\``)
|
|
503
|
+
);
|
|
477
504
|
}
|
|
478
|
-
}
|
|
505
|
+
}
|
|
479
506
|
if (variableDeclarations.length > 0) {
|
|
480
507
|
options?.onWarn?.(
|
|
481
508
|
`
|
|
482
509
|
Variable declarations are not optimized - found ${variableDeclarations.join(", ")}`
|
|
483
510
|
);
|
|
484
511
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
*/
|
|
492
|
-
// @ts-ignore
|
|
493
|
-
enter(node2, parent) {
|
|
494
|
-
if (parent?.type === "Program") {
|
|
495
|
-
switch (node2.type) {
|
|
496
|
-
case "ImportDeclaration":
|
|
497
|
-
case "VariableDeclaration":
|
|
498
|
-
case "VariableDeclarator":
|
|
499
|
-
this.skip();
|
|
512
|
+
const messages = [];
|
|
513
|
+
function collect(value, path) {
|
|
514
|
+
if (value.type === "ObjectExpression") {
|
|
515
|
+
for (const property of value.properties) {
|
|
516
|
+
if (property.type === "Property" && property.kind === "init" && !property.method && !property.shorthand) {
|
|
517
|
+
collect(property.value, [...path, keyOf(source, property)]);
|
|
500
518
|
}
|
|
501
|
-
} else if (parent?.type === "ArrayExpression") {
|
|
502
|
-
const lastIndex = itemsCountStack.length - 1;
|
|
503
|
-
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
504
|
-
pathStack.push(currentCount.toString());
|
|
505
|
-
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
506
|
-
} else if (parent?.type === "ObjectExpression") {
|
|
507
|
-
const lastIndex = propsCountStack.length - 1;
|
|
508
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
509
|
-
}
|
|
510
|
-
switch (node2.type) {
|
|
511
|
-
case "Program":
|
|
512
|
-
if (type === "plain") {
|
|
513
|
-
generator.push(`const resource = `);
|
|
514
|
-
} else if (type === "sfc") {
|
|
515
|
-
const localeName = JSON.stringify(locale ?? '""');
|
|
516
|
-
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
517
|
-
generator.push(`export default function (Component) {`);
|
|
518
|
-
generator.indent();
|
|
519
|
-
generator.pushline(`const ${componentNamespace} = Component`);
|
|
520
|
-
generator.pushline(
|
|
521
|
-
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
522
|
-
);
|
|
523
|
-
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
524
|
-
generator.indent();
|
|
525
|
-
generator.pushline(`"locale": ${localeName},`);
|
|
526
|
-
generator.push(`"resource": `);
|
|
527
|
-
}
|
|
528
|
-
break;
|
|
529
|
-
case "ObjectExpression":
|
|
530
|
-
generator.push("{");
|
|
531
|
-
generator.indent();
|
|
532
|
-
propsCountStack.push(node2.properties.length);
|
|
533
|
-
break;
|
|
534
|
-
case "ArrayExpression":
|
|
535
|
-
generator.push("[");
|
|
536
|
-
generator.indent();
|
|
537
|
-
itemsCountStack.push(node2.elements.length);
|
|
538
|
-
break;
|
|
539
|
-
case "Property":
|
|
540
|
-
if (parent?.type !== "ObjectExpression") break;
|
|
541
|
-
if (node2.key.type !== "Literal" && node2.key.type !== "Identifier") break;
|
|
542
|
-
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
543
|
-
const strName = JSON.stringify(name);
|
|
544
|
-
if (isJSONablePrimitiveLiteral(node2.value)) {
|
|
545
|
-
generator.push(`${strName}: `);
|
|
546
|
-
pathStack.push(name);
|
|
547
|
-
const value = getValue(node2.value);
|
|
548
|
-
const strValue = JSON.stringify(value);
|
|
549
|
-
if (node2.value.type === "Literal" && shared.isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
|
|
550
|
-
generator.push(codegenFn(value), node2.value, value);
|
|
551
|
-
} else if (forceStringify) {
|
|
552
|
-
generator.push(codegenFn(strValue), node2.value, strValue);
|
|
553
|
-
} else {
|
|
554
|
-
generator.push(strValue);
|
|
555
|
-
}
|
|
556
|
-
skipStack.push(false);
|
|
557
|
-
} else if (node2.value.type === "ArrayExpression" || node2.value.type === "ObjectExpression") {
|
|
558
|
-
generator.push(`${strName}: `);
|
|
559
|
-
pathStack.push(name);
|
|
560
|
-
skipStack.push(false);
|
|
561
|
-
} else if (node2.value.type === "FunctionExpression" || node2.value.type === "ArrowFunctionExpression") {
|
|
562
|
-
generator.push(`${strName}: `);
|
|
563
|
-
pathStack.push(name);
|
|
564
|
-
const code = escodegen.generate(node2.value, {
|
|
565
|
-
format: { compact: true }
|
|
566
|
-
});
|
|
567
|
-
generator.push(code, node2.value, code);
|
|
568
|
-
skipStack.push(false);
|
|
569
|
-
} else {
|
|
570
|
-
const skipProperty = "regex" in node2.value;
|
|
571
|
-
if (!skipProperty && node2.type === "Property") {
|
|
572
|
-
const identifierName = node2.value.type === "Identifier" && String(node2.value.name) || node2.value.type === "Literal" && String(node2.value.value);
|
|
573
|
-
generator.push(`${strName}: ${identifierName || name}`);
|
|
574
|
-
skipStack.push(false);
|
|
575
|
-
} else {
|
|
576
|
-
skipStack.push(true);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
break;
|
|
580
|
-
case "SpreadElement":
|
|
581
|
-
const spreadIdentifier = node2.argument.type === "Identifier" && String(node2.argument.name) || node2.argument.type === "Literal" && String(node2.argument.value);
|
|
582
|
-
generator.push(`...${spreadIdentifier}`);
|
|
583
|
-
break;
|
|
584
|
-
default:
|
|
585
|
-
if (parent?.type === "ArrayExpression") {
|
|
586
|
-
if (isJSONablePrimitiveLiteral(node2)) {
|
|
587
|
-
const value = getValue(node2);
|
|
588
|
-
const strValue = JSON.stringify(value);
|
|
589
|
-
if (node2.type === "Literal" && shared.isString(node2.value) || node2.type === "TemplateLiteral") {
|
|
590
|
-
generator.push(codegenFn(value), node2, value);
|
|
591
|
-
} else if (forceStringify) {
|
|
592
|
-
generator.push(codegenFn(strValue), node2, strValue);
|
|
593
|
-
} else {
|
|
594
|
-
generator.push(strValue);
|
|
595
|
-
}
|
|
596
|
-
skipStack.push(false);
|
|
597
|
-
} else {
|
|
598
|
-
skipStack.push(true);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
break;
|
|
602
519
|
}
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
608
|
-
* so it's support only esm only ...
|
|
609
|
-
*/
|
|
610
|
-
// @ts-ignore
|
|
611
|
-
leave(node2, parent) {
|
|
612
|
-
switch (node2.type) {
|
|
613
|
-
case "Program":
|
|
614
|
-
if (type === "plain") {
|
|
615
|
-
generator.push("\n");
|
|
616
|
-
generator.push("export default resource");
|
|
617
|
-
} else if (type === "sfc") {
|
|
618
|
-
generator.deindent();
|
|
619
|
-
generator.push("})");
|
|
620
|
-
generator.deindent();
|
|
621
|
-
generator.pushline("}");
|
|
622
|
-
}
|
|
623
|
-
break;
|
|
624
|
-
case "ObjectExpression":
|
|
625
|
-
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
626
|
-
pathStack.pop();
|
|
627
|
-
propsCountStack.pop();
|
|
628
|
-
}
|
|
629
|
-
generator.deindent();
|
|
630
|
-
generator.push("}");
|
|
631
|
-
break;
|
|
632
|
-
case "ArrayExpression":
|
|
633
|
-
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
634
|
-
pathStack.pop();
|
|
635
|
-
itemsCountStack.pop();
|
|
636
|
-
}
|
|
637
|
-
generator.deindent();
|
|
638
|
-
generator.push("]");
|
|
639
|
-
break;
|
|
640
|
-
}
|
|
641
|
-
if (parent?.type === "ArrayExpression" || parent?.type === "ObjectExpression") {
|
|
642
|
-
const stackArr = node2.type === "Property" ? propsCountStack : itemsCountStack;
|
|
643
|
-
if (stackArr[stackArr.length - 1] !== 0) {
|
|
644
|
-
pathStack.pop();
|
|
645
|
-
!skipStack.pop() && generator.pushline(",");
|
|
520
|
+
} else if (value.type === "ArrayExpression") {
|
|
521
|
+
value.elements.forEach((element, index) => {
|
|
522
|
+
if (element != null && element.type !== "SpreadElement") {
|
|
523
|
+
collect(element, [...path, String(index)]);
|
|
646
524
|
}
|
|
525
|
+
});
|
|
526
|
+
} else if (isMessageLiteral(value)) {
|
|
527
|
+
const message = getValue(value);
|
|
528
|
+
if (shared.isString(message)) {
|
|
529
|
+
messages.push({ node: value, value: message, code: codegenFn(message, path) });
|
|
530
|
+
} else if (forceStringify) {
|
|
531
|
+
const strMessage = JSON.stringify(message);
|
|
532
|
+
messages.push({ node: value, value: strMessage, code: codegenFn(strMessage, path) });
|
|
647
533
|
}
|
|
648
534
|
}
|
|
649
|
-
}
|
|
535
|
+
}
|
|
536
|
+
collect(resource, []);
|
|
537
|
+
if (type === "plain") {
|
|
538
|
+
generator.push(`const resource = `);
|
|
539
|
+
} else if (type === "sfc") {
|
|
540
|
+
const localeName = JSON.stringify(locale ?? '""');
|
|
541
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
542
|
+
generator.push(`export default function (Component) {`);
|
|
543
|
+
generator.indent();
|
|
544
|
+
generator.pushline(`const ${componentNamespace} = Component`);
|
|
545
|
+
generator.pushline(
|
|
546
|
+
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
547
|
+
);
|
|
548
|
+
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
549
|
+
generator.indent();
|
|
550
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
551
|
+
generator.push(`"resource": `);
|
|
552
|
+
}
|
|
553
|
+
let offset = rangeOf(resource).start;
|
|
554
|
+
for (const message of messages) {
|
|
555
|
+
generator.push(source.slice(offset, rangeOf(message.node).start));
|
|
556
|
+
generator.push(message.code, message.node, message.value);
|
|
557
|
+
offset = rangeOf(message.node).end;
|
|
558
|
+
}
|
|
559
|
+
generator.push(source.slice(offset, rangeOf(resource).end));
|
|
560
|
+
if (type === "plain") {
|
|
561
|
+
generator.push("\n");
|
|
562
|
+
generator.push("export default resource");
|
|
563
|
+
} else if (type === "sfc") {
|
|
564
|
+
generator.deindent();
|
|
565
|
+
generator.push("})");
|
|
566
|
+
generator.deindent();
|
|
567
|
+
generator.pushline("}");
|
|
568
|
+
}
|
|
650
569
|
return codeMaps;
|
|
651
570
|
}
|
|
571
|
+
function rangeOf(node) {
|
|
572
|
+
return node;
|
|
573
|
+
}
|
|
574
|
+
function sourceOf(source, node) {
|
|
575
|
+
const { start, end } = rangeOf(node);
|
|
576
|
+
return source.slice(start, end);
|
|
577
|
+
}
|
|
578
|
+
function keyOf(source, property) {
|
|
579
|
+
const { key } = property;
|
|
580
|
+
if (property.computed) {
|
|
581
|
+
return sourceOf(source, key);
|
|
582
|
+
}
|
|
583
|
+
return key.type === "Identifier" ? key.name : String(key.value);
|
|
584
|
+
}
|
|
585
|
+
function isMessageLiteral(node) {
|
|
586
|
+
if (node.type === "Literal" && "regex" in node) {
|
|
587
|
+
return false;
|
|
588
|
+
}
|
|
589
|
+
if (node.type === "TemplateLiteral" && node.expressions.length > 0) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
return isJSONablePrimitiveLiteral(node);
|
|
593
|
+
}
|
|
652
594
|
function isJSONablePrimitiveLiteral(node) {
|
|
653
595
|
return node.type === "Literal" && (shared.isString(node.value) || shared.isNumber(node.value) || shared.isBoolean(node.value) || node.value === null) || node.type === "TemplateLiteral";
|
|
654
596
|
}
|
|
@@ -688,7 +630,25 @@ function generate$2(targetSource, {
|
|
|
688
630
|
escapeHtml,
|
|
689
631
|
jit
|
|
690
632
|
};
|
|
691
|
-
let ast
|
|
633
|
+
let ast;
|
|
634
|
+
try {
|
|
635
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
636
|
+
} catch (err) {
|
|
637
|
+
throw createResourceParseError(err, options);
|
|
638
|
+
}
|
|
639
|
+
const root = ast.body[0].expression;
|
|
640
|
+
if (root.type !== "JSONObjectExpression") {
|
|
641
|
+
const rootValue = jsoncEslintParser.getStaticJSONValue(root);
|
|
642
|
+
if (rootValue !== null) {
|
|
643
|
+
throw createResourceError(
|
|
644
|
+
`You need to define an object as the locale messages at the top level, but got ${describeValue(rootValue)}`,
|
|
645
|
+
options
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
value = JSON.stringify({});
|
|
649
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
650
|
+
options.source = void 0;
|
|
651
|
+
}
|
|
692
652
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
693
653
|
const messages = jsoncEslintParser.getStaticJSONValue(ast);
|
|
694
654
|
value = JSON.stringify(
|
|
@@ -736,6 +696,19 @@ function _generate$1(generator, node, options = {}) {
|
|
|
736
696
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
737
697
|
return code;
|
|
738
698
|
}
|
|
699
|
+
function pushExpressionValue(node2) {
|
|
700
|
+
const value = jsoncEslintParser.getStaticJSONValue(node2);
|
|
701
|
+
if (shared.isString(value)) {
|
|
702
|
+
generator.push(codegenFn(value), node2, value);
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
const strValue = stringifyValue(value);
|
|
706
|
+
if (forceStringify) {
|
|
707
|
+
generator.push(codegenFn(strValue), node2, strValue);
|
|
708
|
+
} else {
|
|
709
|
+
generator.push(strValue);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
739
712
|
const componentNamespace = "_Component";
|
|
740
713
|
jsoncEslintParser.traverseNodes(node, {
|
|
741
714
|
enterNode(node2, parent) {
|
|
@@ -792,6 +765,8 @@ function _generate$1(generator, node, options = {}) {
|
|
|
792
765
|
} else {
|
|
793
766
|
generator.push(strValue2);
|
|
794
767
|
}
|
|
768
|
+
} else if (node2.value.type !== "JSONObjectExpression" && node2.value.type !== "JSONArrayExpression") {
|
|
769
|
+
pushExpressionValue(node2.value);
|
|
795
770
|
}
|
|
796
771
|
break;
|
|
797
772
|
}
|
|
@@ -807,6 +782,14 @@ function _generate$1(generator, node, options = {}) {
|
|
|
807
782
|
generator.push(strValue);
|
|
808
783
|
}
|
|
809
784
|
break;
|
|
785
|
+
case "JSONUnaryExpression":
|
|
786
|
+
case "JSONIdentifier":
|
|
787
|
+
case "JSONTemplateLiteral":
|
|
788
|
+
case "JSONBinaryExpression":
|
|
789
|
+
if (parent.type === "JSONArrayExpression") {
|
|
790
|
+
pushExpressionValue(node2);
|
|
791
|
+
}
|
|
792
|
+
break;
|
|
810
793
|
}
|
|
811
794
|
},
|
|
812
795
|
leaveNode(node2, parent) {
|
|
@@ -857,12 +840,10 @@ async function generate$1(targetSource, options) {
|
|
|
857
840
|
const _options = Object.assign({}, DEFAULT_OPTIONS, options, {
|
|
858
841
|
source: value
|
|
859
842
|
});
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
_options.source = transformed.code;
|
|
865
|
-
}
|
|
843
|
+
const transformed = await esbuild.transform(value, { loader: "ts" });
|
|
844
|
+
if (transformed && transformed.code) {
|
|
845
|
+
value = transformed.code;
|
|
846
|
+
_options.source = transformed.code;
|
|
866
847
|
}
|
|
867
848
|
return generate$3(value, _options);
|
|
868
849
|
}
|
|
@@ -884,6 +865,9 @@ function generate(targetSource, {
|
|
|
884
865
|
usedKeyFilter = void 0
|
|
885
866
|
}) {
|
|
886
867
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
868
|
+
if (value.charCodeAt(0) === 65279) {
|
|
869
|
+
value = value.slice(1);
|
|
870
|
+
}
|
|
887
871
|
const options = {
|
|
888
872
|
type,
|
|
889
873
|
source: value,
|
|
@@ -899,9 +883,31 @@ function generate(targetSource, {
|
|
|
899
883
|
escapeHtml,
|
|
900
884
|
jit
|
|
901
885
|
};
|
|
902
|
-
let ast
|
|
886
|
+
let ast;
|
|
887
|
+
try {
|
|
888
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
889
|
+
} catch (err) {
|
|
890
|
+
throw createResourceParseError(err, options, 1);
|
|
891
|
+
}
|
|
892
|
+
const rootDocument = getRootDocument(ast, options);
|
|
893
|
+
const content = rootDocument?.content;
|
|
894
|
+
const root = content?.type === "YAMLWithMeta" ? content.value : content;
|
|
895
|
+
if (root?.type !== "YAMLMapping") {
|
|
896
|
+
const isAlias = root?.type === "YAMLAlias";
|
|
897
|
+
const rootValue = rootDocument == null || isAlias ? null : yamlEslintParser.getStaticYAMLValue(rootDocument);
|
|
898
|
+
if (isAlias || rootValue !== null) {
|
|
899
|
+
const actual = isAlias ? "an alias" : describeValue(rootValue);
|
|
900
|
+
throw createResourceError(
|
|
901
|
+
`You need to define an object as the locale messages at the top level, but got ${actual}`,
|
|
902
|
+
options
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
value = JSON.stringify({});
|
|
906
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
907
|
+
options.source = void 0;
|
|
908
|
+
}
|
|
903
909
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
904
|
-
const messages = yamlEslintParser.getStaticYAMLValue(ast);
|
|
910
|
+
const messages = yamlEslintParser.getStaticYAMLValue(getRootDocument(ast, options));
|
|
905
911
|
value = JSON.stringify(
|
|
906
912
|
excludeLocales({
|
|
907
913
|
messages,
|
|
@@ -917,7 +923,7 @@ function generate(targetSource, {
|
|
|
917
923
|
options.source = void 0;
|
|
918
924
|
}
|
|
919
925
|
if (usedKeyFilter) {
|
|
920
|
-
const messages = yamlEslintParser.getStaticYAMLValue(ast);
|
|
926
|
+
const messages = yamlEslintParser.getStaticYAMLValue(getRootDocument(ast, options));
|
|
921
927
|
const isMultiLocale = !locale && type === "sfc";
|
|
922
928
|
const filtered = isMultiLocale ? filterMultiLocaleMessages(messages, usedKeyFilter) : filterMessageKeys(messages, usedKeyFilter);
|
|
923
929
|
value = JSON.stringify(filtered);
|
|
@@ -934,6 +940,16 @@ function generate(targetSource, {
|
|
|
934
940
|
map: newMap != null ? newMap : void 0
|
|
935
941
|
};
|
|
936
942
|
}
|
|
943
|
+
function getRootDocument(ast, options) {
|
|
944
|
+
const documents = ast.body.filter((doc) => doc.content != null);
|
|
945
|
+
if (documents.length > 1) {
|
|
946
|
+
throw createResourceError(
|
|
947
|
+
`You need to define the locale messages in a single YAML document, but got ${documents.length} documents`,
|
|
948
|
+
options
|
|
949
|
+
);
|
|
950
|
+
}
|
|
951
|
+
return documents[0] ?? null;
|
|
952
|
+
}
|
|
937
953
|
function _generate(generator, node, options = {}) {
|
|
938
954
|
const propsCountStack = [];
|
|
939
955
|
const pathStack = [];
|
|
@@ -994,7 +1010,7 @@ function _generate(generator, node, options = {}) {
|
|
|
994
1010
|
const name = node2.key.value;
|
|
995
1011
|
const value = node2.value.value;
|
|
996
1012
|
const strName = JSON.stringify(name);
|
|
997
|
-
const strValue =
|
|
1013
|
+
const strValue = stringifyValue(value);
|
|
998
1014
|
name && pathStack.push(name.toString());
|
|
999
1015
|
generator.push(`${strName}: `);
|
|
1000
1016
|
if (shared.isString(value)) {
|
|
@@ -1013,7 +1029,7 @@ function _generate(generator, node, options = {}) {
|
|
|
1013
1029
|
case "YAMLScalar":
|
|
1014
1030
|
if (parent.type === "YAMLSequence") {
|
|
1015
1031
|
const value = node2.value;
|
|
1016
|
-
const strValue =
|
|
1032
|
+
const strValue = stringifyValue(value);
|
|
1017
1033
|
if (shared.isString(value)) {
|
|
1018
1034
|
generator.push(codegenFn(value), node2, value);
|
|
1019
1035
|
} else if (forceStringify) {
|
package/lib/index.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import module$1 from 'node:module';
|
|
2
2
|
import { isBoolean, format, escapeHtml, friendlyJSONstringify, isString, isNumber } from '@intlify/shared';
|
|
3
3
|
import { parse } from 'acorn';
|
|
4
|
-
import
|
|
5
|
-
import { walk } from 'estree-walker';
|
|
4
|
+
import * as messageCompiler from '@intlify/message-compiler';
|
|
6
5
|
import { LOCATION_STUB, detectHtmlTag, baseCompile } from '@intlify/message-compiler';
|
|
7
6
|
import { SourceMapGenerator, SourceMapConsumer } from 'source-map-js';
|
|
8
7
|
import { parseJSON, getStaticJSONValue, traverseNodes } from 'jsonc-eslint-parser';
|
|
@@ -153,6 +152,14 @@ const ON_ERROR_NOOP = () => {
|
|
|
153
152
|
function parsePath(path) {
|
|
154
153
|
return path ? path.join(".") : "";
|
|
155
154
|
}
|
|
155
|
+
const COMPILE_ERROR_MESSAGES = messageCompiler.errorMessages;
|
|
156
|
+
function getCompileErrorMessage(err) {
|
|
157
|
+
if (err.message !== String(err.code)) {
|
|
158
|
+
return err.message;
|
|
159
|
+
}
|
|
160
|
+
const text = COMPILE_ERROR_MESSAGES?.[err.code];
|
|
161
|
+
return text ? text.replace(/:\s*\\?'?\{\d+\}'?/g, "") : err.message;
|
|
162
|
+
}
|
|
156
163
|
function generateMessageFunction(msg, options = {}, path) {
|
|
157
164
|
const env = options.env != null ? options.env : "development";
|
|
158
165
|
const strictMessage = isBoolean(options.strictMessage) ? options.strictMessage : true;
|
|
@@ -181,7 +188,7 @@ function generateMessageFunction(msg, options = {}, path) {
|
|
|
181
188
|
domain: err.domain,
|
|
182
189
|
location: err.location
|
|
183
190
|
};
|
|
184
|
-
onError(err
|
|
191
|
+
onError(getCompileErrorMessage(err), extra);
|
|
185
192
|
errors.push(err);
|
|
186
193
|
}
|
|
187
194
|
};
|
|
@@ -293,7 +300,7 @@ function generateResourceAst(msg, options = {}, path) {
|
|
|
293
300
|
domain: err.domain,
|
|
294
301
|
location: err.location
|
|
295
302
|
};
|
|
296
|
-
onError(err
|
|
303
|
+
onError(getCompileErrorMessage(err), extra);
|
|
297
304
|
errors.push(err);
|
|
298
305
|
}
|
|
299
306
|
};
|
|
@@ -334,6 +341,30 @@ function filterMultiLocaleMessages(messages, shouldKeep) {
|
|
|
334
341
|
}
|
|
335
342
|
return result;
|
|
336
343
|
}
|
|
344
|
+
function describeResource({ type, filename }) {
|
|
345
|
+
return type === "sfc" ? `the i18n custom block of ${filename}` : `${filename}`;
|
|
346
|
+
}
|
|
347
|
+
function describeValue(value) {
|
|
348
|
+
if (Array.isArray(value)) return "an array";
|
|
349
|
+
if (value === void 0) return "undefined";
|
|
350
|
+
return `a ${typeof value}`;
|
|
351
|
+
}
|
|
352
|
+
function stringifyValue(value) {
|
|
353
|
+
if (typeof value === "number") return Object.is(value, -0) ? "-0" : String(value);
|
|
354
|
+
if (value === void 0) return "undefined";
|
|
355
|
+
return JSON.stringify(value);
|
|
356
|
+
}
|
|
357
|
+
function createResourceError(message, options) {
|
|
358
|
+
return new Error(`${message} in ${describeResource(options)}.`);
|
|
359
|
+
}
|
|
360
|
+
function createResourceParseError(err, options, columnOffset = 0) {
|
|
361
|
+
if (!(err instanceof Error)) return err;
|
|
362
|
+
const { lineNumber, column } = err;
|
|
363
|
+
const position = lineNumber != null ? ` (${lineNumber}:${column + columnOffset})` : "";
|
|
364
|
+
return new SyntaxError(`${err.message} in ${describeResource(options)}${position}`, {
|
|
365
|
+
cause: err
|
|
366
|
+
});
|
|
367
|
+
}
|
|
337
368
|
function excludeLocales({
|
|
338
369
|
messages,
|
|
339
370
|
onlyLocales
|
|
@@ -426,222 +457,121 @@ function scanAst(ast) {
|
|
|
426
457
|
return false;
|
|
427
458
|
}
|
|
428
459
|
function _generate$2(generator, node, options = {}) {
|
|
429
|
-
const propsCountStack = [];
|
|
430
|
-
const pathStack = [];
|
|
431
|
-
const itemsCountStack = [];
|
|
432
|
-
const skipStack = [];
|
|
433
460
|
const { forceStringify } = generator.context();
|
|
434
461
|
const codeMaps = /* @__PURE__ */ new Map();
|
|
435
462
|
const { type, sourceMap, isGlobal, locale, jit } = options;
|
|
463
|
+
const source = options.source ?? "";
|
|
436
464
|
const _codegenFn = jit ? generateResourceAst : generateMessageFunction;
|
|
437
|
-
function codegenFn(value) {
|
|
438
|
-
const { code, map } = _codegenFn(value, options,
|
|
465
|
+
function codegenFn(value, path) {
|
|
466
|
+
const { code, map } = _codegenFn(value, options, path);
|
|
439
467
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
440
468
|
return code;
|
|
441
469
|
}
|
|
442
470
|
const componentNamespace = "_Component";
|
|
443
471
|
const variableDeclarations = [];
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
break;
|
|
458
|
-
case "ImportDeclaration":
|
|
459
|
-
generator.push(options.source?.slice(node2.start, node2.end));
|
|
460
|
-
generator.newline();
|
|
461
|
-
break;
|
|
462
|
-
case "VariableDeclaration":
|
|
463
|
-
generator.push(options.source?.slice(node2.start, node2.end));
|
|
464
|
-
generator.newline();
|
|
465
|
-
variableDeclarations.push(
|
|
466
|
-
...node2.declarations.map((x) => `\`${x.id.name}\``)
|
|
467
|
-
);
|
|
468
|
-
break;
|
|
469
|
-
}
|
|
472
|
+
const { body } = node;
|
|
473
|
+
const exportDefault = body.find(
|
|
474
|
+
(statement) => statement.type === "ExportDefaultDeclaration"
|
|
475
|
+
);
|
|
476
|
+
const resource = exportDefault.declaration;
|
|
477
|
+
for (const statement of body) {
|
|
478
|
+
if (statement === exportDefault) continue;
|
|
479
|
+
generator.push(sourceOf(source, statement));
|
|
480
|
+
generator.newline();
|
|
481
|
+
if (statement.type === "VariableDeclaration") {
|
|
482
|
+
variableDeclarations.push(
|
|
483
|
+
...statement.declarations.map((x) => `\`${x.id.name}\``)
|
|
484
|
+
);
|
|
470
485
|
}
|
|
471
|
-
}
|
|
486
|
+
}
|
|
472
487
|
if (variableDeclarations.length > 0) {
|
|
473
488
|
options?.onWarn?.(
|
|
474
489
|
`
|
|
475
490
|
Variable declarations are not optimized - found ${variableDeclarations.join(", ")}`
|
|
476
491
|
);
|
|
477
492
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
*/
|
|
485
|
-
// @ts-ignore
|
|
486
|
-
enter(node2, parent) {
|
|
487
|
-
if (parent?.type === "Program") {
|
|
488
|
-
switch (node2.type) {
|
|
489
|
-
case "ImportDeclaration":
|
|
490
|
-
case "VariableDeclaration":
|
|
491
|
-
case "VariableDeclarator":
|
|
492
|
-
this.skip();
|
|
493
|
+
const messages = [];
|
|
494
|
+
function collect(value, path) {
|
|
495
|
+
if (value.type === "ObjectExpression") {
|
|
496
|
+
for (const property of value.properties) {
|
|
497
|
+
if (property.type === "Property" && property.kind === "init" && !property.method && !property.shorthand) {
|
|
498
|
+
collect(property.value, [...path, keyOf(source, property)]);
|
|
493
499
|
}
|
|
494
|
-
} else if (parent?.type === "ArrayExpression") {
|
|
495
|
-
const lastIndex = itemsCountStack.length - 1;
|
|
496
|
-
const currentCount = parent.elements.length - itemsCountStack[lastIndex];
|
|
497
|
-
pathStack.push(currentCount.toString());
|
|
498
|
-
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex];
|
|
499
|
-
} else if (parent?.type === "ObjectExpression") {
|
|
500
|
-
const lastIndex = propsCountStack.length - 1;
|
|
501
|
-
propsCountStack[lastIndex] = --propsCountStack[lastIndex];
|
|
502
|
-
}
|
|
503
|
-
switch (node2.type) {
|
|
504
|
-
case "Program":
|
|
505
|
-
if (type === "plain") {
|
|
506
|
-
generator.push(`const resource = `);
|
|
507
|
-
} else if (type === "sfc") {
|
|
508
|
-
const localeName = JSON.stringify(locale ?? '""');
|
|
509
|
-
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
510
|
-
generator.push(`export default function (Component) {`);
|
|
511
|
-
generator.indent();
|
|
512
|
-
generator.pushline(`const ${componentNamespace} = Component`);
|
|
513
|
-
generator.pushline(
|
|
514
|
-
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
515
|
-
);
|
|
516
|
-
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
517
|
-
generator.indent();
|
|
518
|
-
generator.pushline(`"locale": ${localeName},`);
|
|
519
|
-
generator.push(`"resource": `);
|
|
520
|
-
}
|
|
521
|
-
break;
|
|
522
|
-
case "ObjectExpression":
|
|
523
|
-
generator.push("{");
|
|
524
|
-
generator.indent();
|
|
525
|
-
propsCountStack.push(node2.properties.length);
|
|
526
|
-
break;
|
|
527
|
-
case "ArrayExpression":
|
|
528
|
-
generator.push("[");
|
|
529
|
-
generator.indent();
|
|
530
|
-
itemsCountStack.push(node2.elements.length);
|
|
531
|
-
break;
|
|
532
|
-
case "Property":
|
|
533
|
-
if (parent?.type !== "ObjectExpression") break;
|
|
534
|
-
if (node2.key.type !== "Literal" && node2.key.type !== "Identifier") break;
|
|
535
|
-
const name = node2.key.type === "Literal" ? String(node2.key.value) : node2.key.name;
|
|
536
|
-
const strName = JSON.stringify(name);
|
|
537
|
-
if (isJSONablePrimitiveLiteral(node2.value)) {
|
|
538
|
-
generator.push(`${strName}: `);
|
|
539
|
-
pathStack.push(name);
|
|
540
|
-
const value = getValue(node2.value);
|
|
541
|
-
const strValue = JSON.stringify(value);
|
|
542
|
-
if (node2.value.type === "Literal" && isString(node2.value.value) || node2.value.type === "TemplateLiteral") {
|
|
543
|
-
generator.push(codegenFn(value), node2.value, value);
|
|
544
|
-
} else if (forceStringify) {
|
|
545
|
-
generator.push(codegenFn(strValue), node2.value, strValue);
|
|
546
|
-
} else {
|
|
547
|
-
generator.push(strValue);
|
|
548
|
-
}
|
|
549
|
-
skipStack.push(false);
|
|
550
|
-
} else if (node2.value.type === "ArrayExpression" || node2.value.type === "ObjectExpression") {
|
|
551
|
-
generator.push(`${strName}: `);
|
|
552
|
-
pathStack.push(name);
|
|
553
|
-
skipStack.push(false);
|
|
554
|
-
} else if (node2.value.type === "FunctionExpression" || node2.value.type === "ArrowFunctionExpression") {
|
|
555
|
-
generator.push(`${strName}: `);
|
|
556
|
-
pathStack.push(name);
|
|
557
|
-
const code = generate$4(node2.value, {
|
|
558
|
-
format: { compact: true }
|
|
559
|
-
});
|
|
560
|
-
generator.push(code, node2.value, code);
|
|
561
|
-
skipStack.push(false);
|
|
562
|
-
} else {
|
|
563
|
-
const skipProperty = "regex" in node2.value;
|
|
564
|
-
if (!skipProperty && node2.type === "Property") {
|
|
565
|
-
const identifierName = node2.value.type === "Identifier" && String(node2.value.name) || node2.value.type === "Literal" && String(node2.value.value);
|
|
566
|
-
generator.push(`${strName}: ${identifierName || name}`);
|
|
567
|
-
skipStack.push(false);
|
|
568
|
-
} else {
|
|
569
|
-
skipStack.push(true);
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
break;
|
|
573
|
-
case "SpreadElement":
|
|
574
|
-
const spreadIdentifier = node2.argument.type === "Identifier" && String(node2.argument.name) || node2.argument.type === "Literal" && String(node2.argument.value);
|
|
575
|
-
generator.push(`...${spreadIdentifier}`);
|
|
576
|
-
break;
|
|
577
|
-
default:
|
|
578
|
-
if (parent?.type === "ArrayExpression") {
|
|
579
|
-
if (isJSONablePrimitiveLiteral(node2)) {
|
|
580
|
-
const value = getValue(node2);
|
|
581
|
-
const strValue = JSON.stringify(value);
|
|
582
|
-
if (node2.type === "Literal" && isString(node2.value) || node2.type === "TemplateLiteral") {
|
|
583
|
-
generator.push(codegenFn(value), node2, value);
|
|
584
|
-
} else if (forceStringify) {
|
|
585
|
-
generator.push(codegenFn(strValue), node2, strValue);
|
|
586
|
-
} else {
|
|
587
|
-
generator.push(strValue);
|
|
588
|
-
}
|
|
589
|
-
skipStack.push(false);
|
|
590
|
-
} else {
|
|
591
|
-
skipStack.push(true);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
break;
|
|
595
|
-
}
|
|
596
|
-
},
|
|
597
|
-
/**
|
|
598
|
-
* NOTE:
|
|
599
|
-
* force cast to Node of `estree-walker@3.x`,
|
|
600
|
-
* because `estree-walker@3.x` is not dual packages,
|
|
601
|
-
* so it's support only esm only ...
|
|
602
|
-
*/
|
|
603
|
-
// @ts-ignore
|
|
604
|
-
leave(node2, parent) {
|
|
605
|
-
switch (node2.type) {
|
|
606
|
-
case "Program":
|
|
607
|
-
if (type === "plain") {
|
|
608
|
-
generator.push("\n");
|
|
609
|
-
generator.push("export default resource");
|
|
610
|
-
} else if (type === "sfc") {
|
|
611
|
-
generator.deindent();
|
|
612
|
-
generator.push("})");
|
|
613
|
-
generator.deindent();
|
|
614
|
-
generator.pushline("}");
|
|
615
|
-
}
|
|
616
|
-
break;
|
|
617
|
-
case "ObjectExpression":
|
|
618
|
-
if (propsCountStack[propsCountStack.length - 1] === 0) {
|
|
619
|
-
pathStack.pop();
|
|
620
|
-
propsCountStack.pop();
|
|
621
|
-
}
|
|
622
|
-
generator.deindent();
|
|
623
|
-
generator.push("}");
|
|
624
|
-
break;
|
|
625
|
-
case "ArrayExpression":
|
|
626
|
-
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
|
|
627
|
-
pathStack.pop();
|
|
628
|
-
itemsCountStack.pop();
|
|
629
|
-
}
|
|
630
|
-
generator.deindent();
|
|
631
|
-
generator.push("]");
|
|
632
|
-
break;
|
|
633
500
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
if (
|
|
637
|
-
|
|
638
|
-
!skipStack.pop() && generator.pushline(",");
|
|
501
|
+
} else if (value.type === "ArrayExpression") {
|
|
502
|
+
value.elements.forEach((element, index) => {
|
|
503
|
+
if (element != null && element.type !== "SpreadElement") {
|
|
504
|
+
collect(element, [...path, String(index)]);
|
|
639
505
|
}
|
|
506
|
+
});
|
|
507
|
+
} else if (isMessageLiteral(value)) {
|
|
508
|
+
const message = getValue(value);
|
|
509
|
+
if (isString(message)) {
|
|
510
|
+
messages.push({ node: value, value: message, code: codegenFn(message, path) });
|
|
511
|
+
} else if (forceStringify) {
|
|
512
|
+
const strMessage = JSON.stringify(message);
|
|
513
|
+
messages.push({ node: value, value: strMessage, code: codegenFn(strMessage, path) });
|
|
640
514
|
}
|
|
641
515
|
}
|
|
642
|
-
}
|
|
516
|
+
}
|
|
517
|
+
collect(resource, []);
|
|
518
|
+
if (type === "plain") {
|
|
519
|
+
generator.push(`const resource = `);
|
|
520
|
+
} else if (type === "sfc") {
|
|
521
|
+
const localeName = JSON.stringify(locale ?? '""');
|
|
522
|
+
const variableName = !isGlobal ? "__i18n" : "__i18nGlobal";
|
|
523
|
+
generator.push(`export default function (Component) {`);
|
|
524
|
+
generator.indent();
|
|
525
|
+
generator.pushline(`const ${componentNamespace} = Component`);
|
|
526
|
+
generator.pushline(
|
|
527
|
+
`${componentNamespace}.${variableName} = ${componentNamespace}.${variableName} || []`
|
|
528
|
+
);
|
|
529
|
+
generator.push(`${componentNamespace}.${variableName}.push({`);
|
|
530
|
+
generator.indent();
|
|
531
|
+
generator.pushline(`"locale": ${localeName},`);
|
|
532
|
+
generator.push(`"resource": `);
|
|
533
|
+
}
|
|
534
|
+
let offset = rangeOf(resource).start;
|
|
535
|
+
for (const message of messages) {
|
|
536
|
+
generator.push(source.slice(offset, rangeOf(message.node).start));
|
|
537
|
+
generator.push(message.code, message.node, message.value);
|
|
538
|
+
offset = rangeOf(message.node).end;
|
|
539
|
+
}
|
|
540
|
+
generator.push(source.slice(offset, rangeOf(resource).end));
|
|
541
|
+
if (type === "plain") {
|
|
542
|
+
generator.push("\n");
|
|
543
|
+
generator.push("export default resource");
|
|
544
|
+
} else if (type === "sfc") {
|
|
545
|
+
generator.deindent();
|
|
546
|
+
generator.push("})");
|
|
547
|
+
generator.deindent();
|
|
548
|
+
generator.pushline("}");
|
|
549
|
+
}
|
|
643
550
|
return codeMaps;
|
|
644
551
|
}
|
|
552
|
+
function rangeOf(node) {
|
|
553
|
+
return node;
|
|
554
|
+
}
|
|
555
|
+
function sourceOf(source, node) {
|
|
556
|
+
const { start, end } = rangeOf(node);
|
|
557
|
+
return source.slice(start, end);
|
|
558
|
+
}
|
|
559
|
+
function keyOf(source, property) {
|
|
560
|
+
const { key } = property;
|
|
561
|
+
if (property.computed) {
|
|
562
|
+
return sourceOf(source, key);
|
|
563
|
+
}
|
|
564
|
+
return key.type === "Identifier" ? key.name : String(key.value);
|
|
565
|
+
}
|
|
566
|
+
function isMessageLiteral(node) {
|
|
567
|
+
if (node.type === "Literal" && "regex" in node) {
|
|
568
|
+
return false;
|
|
569
|
+
}
|
|
570
|
+
if (node.type === "TemplateLiteral" && node.expressions.length > 0) {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
573
|
+
return isJSONablePrimitiveLiteral(node);
|
|
574
|
+
}
|
|
645
575
|
function isJSONablePrimitiveLiteral(node) {
|
|
646
576
|
return node.type === "Literal" && (isString(node.value) || isNumber(node.value) || isBoolean(node.value) || node.value === null) || node.type === "TemplateLiteral";
|
|
647
577
|
}
|
|
@@ -681,7 +611,25 @@ function generate$2(targetSource, {
|
|
|
681
611
|
escapeHtml,
|
|
682
612
|
jit
|
|
683
613
|
};
|
|
684
|
-
let ast
|
|
614
|
+
let ast;
|
|
615
|
+
try {
|
|
616
|
+
ast = parseJSON(value, { filePath: filename });
|
|
617
|
+
} catch (err) {
|
|
618
|
+
throw createResourceParseError(err, options);
|
|
619
|
+
}
|
|
620
|
+
const root = ast.body[0].expression;
|
|
621
|
+
if (root.type !== "JSONObjectExpression") {
|
|
622
|
+
const rootValue = getStaticJSONValue(root);
|
|
623
|
+
if (rootValue !== null) {
|
|
624
|
+
throw createResourceError(
|
|
625
|
+
`You need to define an object as the locale messages at the top level, but got ${describeValue(rootValue)}`,
|
|
626
|
+
options
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
value = JSON.stringify({});
|
|
630
|
+
ast = parseJSON(value, { filePath: filename });
|
|
631
|
+
options.source = void 0;
|
|
632
|
+
}
|
|
685
633
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
686
634
|
const messages = getStaticJSONValue(ast);
|
|
687
635
|
value = JSON.stringify(
|
|
@@ -729,6 +677,19 @@ function _generate$1(generator, node, options = {}) {
|
|
|
729
677
|
sourceMap && map != null && codeMaps.set(value, map);
|
|
730
678
|
return code;
|
|
731
679
|
}
|
|
680
|
+
function pushExpressionValue(node2) {
|
|
681
|
+
const value = getStaticJSONValue(node2);
|
|
682
|
+
if (isString(value)) {
|
|
683
|
+
generator.push(codegenFn(value), node2, value);
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
const strValue = stringifyValue(value);
|
|
687
|
+
if (forceStringify) {
|
|
688
|
+
generator.push(codegenFn(strValue), node2, strValue);
|
|
689
|
+
} else {
|
|
690
|
+
generator.push(strValue);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
732
693
|
const componentNamespace = "_Component";
|
|
733
694
|
traverseNodes(node, {
|
|
734
695
|
enterNode(node2, parent) {
|
|
@@ -785,6 +746,8 @@ function _generate$1(generator, node, options = {}) {
|
|
|
785
746
|
} else {
|
|
786
747
|
generator.push(strValue2);
|
|
787
748
|
}
|
|
749
|
+
} else if (node2.value.type !== "JSONObjectExpression" && node2.value.type !== "JSONArrayExpression") {
|
|
750
|
+
pushExpressionValue(node2.value);
|
|
788
751
|
}
|
|
789
752
|
break;
|
|
790
753
|
}
|
|
@@ -800,6 +763,14 @@ function _generate$1(generator, node, options = {}) {
|
|
|
800
763
|
generator.push(strValue);
|
|
801
764
|
}
|
|
802
765
|
break;
|
|
766
|
+
case "JSONUnaryExpression":
|
|
767
|
+
case "JSONIdentifier":
|
|
768
|
+
case "JSONTemplateLiteral":
|
|
769
|
+
case "JSONBinaryExpression":
|
|
770
|
+
if (parent.type === "JSONArrayExpression") {
|
|
771
|
+
pushExpressionValue(node2);
|
|
772
|
+
}
|
|
773
|
+
break;
|
|
803
774
|
}
|
|
804
775
|
},
|
|
805
776
|
leaveNode(node2, parent) {
|
|
@@ -850,12 +821,10 @@ async function generate$1(targetSource, options) {
|
|
|
850
821
|
const _options = Object.assign({}, DEFAULT_OPTIONS, options, {
|
|
851
822
|
source: value
|
|
852
823
|
});
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
_options.source = transformed.code;
|
|
858
|
-
}
|
|
824
|
+
const transformed = await transform(value, { loader: "ts" });
|
|
825
|
+
if (transformed && transformed.code) {
|
|
826
|
+
value = transformed.code;
|
|
827
|
+
_options.source = transformed.code;
|
|
859
828
|
}
|
|
860
829
|
return generate$3(value, _options);
|
|
861
830
|
}
|
|
@@ -877,6 +846,9 @@ function generate(targetSource, {
|
|
|
877
846
|
usedKeyFilter = void 0
|
|
878
847
|
}) {
|
|
879
848
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
849
|
+
if (value.charCodeAt(0) === 65279) {
|
|
850
|
+
value = value.slice(1);
|
|
851
|
+
}
|
|
880
852
|
const options = {
|
|
881
853
|
type,
|
|
882
854
|
source: value,
|
|
@@ -892,9 +864,31 @@ function generate(targetSource, {
|
|
|
892
864
|
escapeHtml,
|
|
893
865
|
jit
|
|
894
866
|
};
|
|
895
|
-
let ast
|
|
867
|
+
let ast;
|
|
868
|
+
try {
|
|
869
|
+
ast = parseYAML(value, { filePath: filename });
|
|
870
|
+
} catch (err) {
|
|
871
|
+
throw createResourceParseError(err, options, 1);
|
|
872
|
+
}
|
|
873
|
+
const rootDocument = getRootDocument(ast, options);
|
|
874
|
+
const content = rootDocument?.content;
|
|
875
|
+
const root = content?.type === "YAMLWithMeta" ? content.value : content;
|
|
876
|
+
if (root?.type !== "YAMLMapping") {
|
|
877
|
+
const isAlias = root?.type === "YAMLAlias";
|
|
878
|
+
const rootValue = rootDocument == null || isAlias ? null : getStaticYAMLValue(rootDocument);
|
|
879
|
+
if (isAlias || rootValue !== null) {
|
|
880
|
+
const actual = isAlias ? "an alias" : describeValue(rootValue);
|
|
881
|
+
throw createResourceError(
|
|
882
|
+
`You need to define an object as the locale messages at the top level, but got ${actual}`,
|
|
883
|
+
options
|
|
884
|
+
);
|
|
885
|
+
}
|
|
886
|
+
value = JSON.stringify({});
|
|
887
|
+
ast = parseYAML(value, { filePath: filename });
|
|
888
|
+
options.source = void 0;
|
|
889
|
+
}
|
|
896
890
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
897
|
-
const messages = getStaticYAMLValue(ast);
|
|
891
|
+
const messages = getStaticYAMLValue(getRootDocument(ast, options));
|
|
898
892
|
value = JSON.stringify(
|
|
899
893
|
excludeLocales({
|
|
900
894
|
messages,
|
|
@@ -910,7 +904,7 @@ function generate(targetSource, {
|
|
|
910
904
|
options.source = void 0;
|
|
911
905
|
}
|
|
912
906
|
if (usedKeyFilter) {
|
|
913
|
-
const messages = getStaticYAMLValue(ast);
|
|
907
|
+
const messages = getStaticYAMLValue(getRootDocument(ast, options));
|
|
914
908
|
const isMultiLocale = !locale && type === "sfc";
|
|
915
909
|
const filtered = isMultiLocale ? filterMultiLocaleMessages(messages, usedKeyFilter) : filterMessageKeys(messages, usedKeyFilter);
|
|
916
910
|
value = JSON.stringify(filtered);
|
|
@@ -927,6 +921,16 @@ function generate(targetSource, {
|
|
|
927
921
|
map: newMap != null ? newMap : void 0
|
|
928
922
|
};
|
|
929
923
|
}
|
|
924
|
+
function getRootDocument(ast, options) {
|
|
925
|
+
const documents = ast.body.filter((doc) => doc.content != null);
|
|
926
|
+
if (documents.length > 1) {
|
|
927
|
+
throw createResourceError(
|
|
928
|
+
`You need to define the locale messages in a single YAML document, but got ${documents.length} documents`,
|
|
929
|
+
options
|
|
930
|
+
);
|
|
931
|
+
}
|
|
932
|
+
return documents[0] ?? null;
|
|
933
|
+
}
|
|
930
934
|
function _generate(generator, node, options = {}) {
|
|
931
935
|
const propsCountStack = [];
|
|
932
936
|
const pathStack = [];
|
|
@@ -987,7 +991,7 @@ function _generate(generator, node, options = {}) {
|
|
|
987
991
|
const name = node2.key.value;
|
|
988
992
|
const value = node2.value.value;
|
|
989
993
|
const strName = JSON.stringify(name);
|
|
990
|
-
const strValue =
|
|
994
|
+
const strValue = stringifyValue(value);
|
|
991
995
|
name && pathStack.push(name.toString());
|
|
992
996
|
generator.push(`${strName}: `);
|
|
993
997
|
if (isString(value)) {
|
|
@@ -1006,7 +1010,7 @@ function _generate(generator, node, options = {}) {
|
|
|
1006
1010
|
case "YAMLScalar":
|
|
1007
1011
|
if (parent.type === "YAMLSequence") {
|
|
1008
1012
|
const value = node2.value;
|
|
1009
|
-
const strValue =
|
|
1013
|
+
const strValue = stringifyValue(value);
|
|
1010
1014
|
if (isString(value)) {
|
|
1011
1015
|
generator.push(codegenFn(value), node2, value);
|
|
1012
1016
|
} else if (forceStringify) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlify/bundle-utils",
|
|
3
3
|
"description": "Bundle utilities for Intlify project",
|
|
4
|
-
"version": "11.2.
|
|
4
|
+
"version": "11.2.7",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -18,18 +18,15 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@intlify/message-compiler": "~11.4.
|
|
22
|
-
"@intlify/shared": "~11.4.
|
|
21
|
+
"@intlify/message-compiler": "~11.4.15",
|
|
22
|
+
"@intlify/shared": "~11.4.15",
|
|
23
23
|
"acorn": "^8.8.2",
|
|
24
24
|
"esbuild": "^0.25.4",
|
|
25
|
-
"escodegen": "^2.1.0",
|
|
26
|
-
"estree-walker": "^2.0.2",
|
|
27
25
|
"jsonc-eslint-parser": "^2.3.0",
|
|
28
26
|
"source-map-js": "^1.2.1",
|
|
29
27
|
"yaml-eslint-parser": "^1.2.2"
|
|
30
28
|
},
|
|
31
29
|
"devDependencies": {
|
|
32
|
-
"@types/escodegen": "^0.0.10",
|
|
33
30
|
"@types/estree": "^1.0.0",
|
|
34
31
|
"unbuild": "^3.6.1"
|
|
35
32
|
},
|