@intlify/bundle-utils 11.2.5 → 11.2.6
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 +76 -4
- package/lib/index.mjs +76 -4
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -341,6 +341,25 @@ function filterMultiLocaleMessages(messages, shouldKeep) {
|
|
|
341
341
|
}
|
|
342
342
|
return result;
|
|
343
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 createResourceError(message, options) {
|
|
353
|
+
return new Error(`${message} in ${describeResource(options)}.`);
|
|
354
|
+
}
|
|
355
|
+
function createResourceParseError(err, options, columnOffset = 0) {
|
|
356
|
+
if (!(err instanceof Error)) return err;
|
|
357
|
+
const { lineNumber, column } = err;
|
|
358
|
+
const position = lineNumber != null ? ` (${lineNumber}:${column + columnOffset})` : "";
|
|
359
|
+
return new SyntaxError(`${err.message} in ${describeResource(options)}${position}`, {
|
|
360
|
+
cause: err
|
|
361
|
+
});
|
|
362
|
+
}
|
|
344
363
|
function excludeLocales({
|
|
345
364
|
messages,
|
|
346
365
|
onlyLocales
|
|
@@ -688,7 +707,25 @@ function generate$2(targetSource, {
|
|
|
688
707
|
escapeHtml,
|
|
689
708
|
jit
|
|
690
709
|
};
|
|
691
|
-
let ast
|
|
710
|
+
let ast;
|
|
711
|
+
try {
|
|
712
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
713
|
+
} catch (err) {
|
|
714
|
+
throw createResourceParseError(err, options);
|
|
715
|
+
}
|
|
716
|
+
const root = ast.body[0].expression;
|
|
717
|
+
if (root.type !== "JSONObjectExpression") {
|
|
718
|
+
const rootValue = jsoncEslintParser.getStaticJSONValue(root);
|
|
719
|
+
if (rootValue !== null) {
|
|
720
|
+
throw createResourceError(
|
|
721
|
+
`You need to define an object as the locale messages at the top level, but got ${describeValue(rootValue)}`,
|
|
722
|
+
options
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
value = JSON.stringify({});
|
|
726
|
+
ast = jsoncEslintParser.parseJSON(value, { filePath: filename });
|
|
727
|
+
options.source = void 0;
|
|
728
|
+
}
|
|
692
729
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
693
730
|
const messages = jsoncEslintParser.getStaticJSONValue(ast);
|
|
694
731
|
value = JSON.stringify(
|
|
@@ -884,6 +921,9 @@ function generate(targetSource, {
|
|
|
884
921
|
usedKeyFilter = void 0
|
|
885
922
|
}) {
|
|
886
923
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
924
|
+
if (value.charCodeAt(0) === 65279) {
|
|
925
|
+
value = value.slice(1);
|
|
926
|
+
}
|
|
887
927
|
const options = {
|
|
888
928
|
type,
|
|
889
929
|
source: value,
|
|
@@ -899,9 +939,31 @@ function generate(targetSource, {
|
|
|
899
939
|
escapeHtml,
|
|
900
940
|
jit
|
|
901
941
|
};
|
|
902
|
-
let ast
|
|
942
|
+
let ast;
|
|
943
|
+
try {
|
|
944
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
945
|
+
} catch (err) {
|
|
946
|
+
throw createResourceParseError(err, options, 1);
|
|
947
|
+
}
|
|
948
|
+
const rootDocument = getRootDocument(ast, options);
|
|
949
|
+
const content = rootDocument?.content;
|
|
950
|
+
const root = content?.type === "YAMLWithMeta" ? content.value : content;
|
|
951
|
+
if (root?.type !== "YAMLMapping") {
|
|
952
|
+
const isAlias = root?.type === "YAMLAlias";
|
|
953
|
+
const rootValue = rootDocument == null || isAlias ? null : yamlEslintParser.getStaticYAMLValue(rootDocument);
|
|
954
|
+
if (isAlias || rootValue !== null) {
|
|
955
|
+
const actual = isAlias ? "an alias" : describeValue(rootValue);
|
|
956
|
+
throw createResourceError(
|
|
957
|
+
`You need to define an object as the locale messages at the top level, but got ${actual}`,
|
|
958
|
+
options
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
value = JSON.stringify({});
|
|
962
|
+
ast = yamlEslintParser.parseYAML(value, { filePath: filename });
|
|
963
|
+
options.source = void 0;
|
|
964
|
+
}
|
|
903
965
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
904
|
-
const messages = yamlEslintParser.getStaticYAMLValue(ast);
|
|
966
|
+
const messages = yamlEslintParser.getStaticYAMLValue(getRootDocument(ast, options));
|
|
905
967
|
value = JSON.stringify(
|
|
906
968
|
excludeLocales({
|
|
907
969
|
messages,
|
|
@@ -917,7 +979,7 @@ function generate(targetSource, {
|
|
|
917
979
|
options.source = void 0;
|
|
918
980
|
}
|
|
919
981
|
if (usedKeyFilter) {
|
|
920
|
-
const messages = yamlEslintParser.getStaticYAMLValue(ast);
|
|
982
|
+
const messages = yamlEslintParser.getStaticYAMLValue(getRootDocument(ast, options));
|
|
921
983
|
const isMultiLocale = !locale && type === "sfc";
|
|
922
984
|
const filtered = isMultiLocale ? filterMultiLocaleMessages(messages, usedKeyFilter) : filterMessageKeys(messages, usedKeyFilter);
|
|
923
985
|
value = JSON.stringify(filtered);
|
|
@@ -934,6 +996,16 @@ function generate(targetSource, {
|
|
|
934
996
|
map: newMap != null ? newMap : void 0
|
|
935
997
|
};
|
|
936
998
|
}
|
|
999
|
+
function getRootDocument(ast, options) {
|
|
1000
|
+
const documents = ast.body.filter((doc) => doc.content != null);
|
|
1001
|
+
if (documents.length > 1) {
|
|
1002
|
+
throw createResourceError(
|
|
1003
|
+
`You need to define the locale messages in a single YAML document, but got ${documents.length} documents`,
|
|
1004
|
+
options
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
return documents[0] ?? null;
|
|
1008
|
+
}
|
|
937
1009
|
function _generate(generator, node, options = {}) {
|
|
938
1010
|
const propsCountStack = [];
|
|
939
1011
|
const pathStack = [];
|
package/lib/index.mjs
CHANGED
|
@@ -334,6 +334,25 @@ function filterMultiLocaleMessages(messages, shouldKeep) {
|
|
|
334
334
|
}
|
|
335
335
|
return result;
|
|
336
336
|
}
|
|
337
|
+
function describeResource({ type, filename }) {
|
|
338
|
+
return type === "sfc" ? `the i18n custom block of ${filename}` : `${filename}`;
|
|
339
|
+
}
|
|
340
|
+
function describeValue(value) {
|
|
341
|
+
if (Array.isArray(value)) return "an array";
|
|
342
|
+
if (value === void 0) return "undefined";
|
|
343
|
+
return `a ${typeof value}`;
|
|
344
|
+
}
|
|
345
|
+
function createResourceError(message, options) {
|
|
346
|
+
return new Error(`${message} in ${describeResource(options)}.`);
|
|
347
|
+
}
|
|
348
|
+
function createResourceParseError(err, options, columnOffset = 0) {
|
|
349
|
+
if (!(err instanceof Error)) return err;
|
|
350
|
+
const { lineNumber, column } = err;
|
|
351
|
+
const position = lineNumber != null ? ` (${lineNumber}:${column + columnOffset})` : "";
|
|
352
|
+
return new SyntaxError(`${err.message} in ${describeResource(options)}${position}`, {
|
|
353
|
+
cause: err
|
|
354
|
+
});
|
|
355
|
+
}
|
|
337
356
|
function excludeLocales({
|
|
338
357
|
messages,
|
|
339
358
|
onlyLocales
|
|
@@ -681,7 +700,25 @@ function generate$2(targetSource, {
|
|
|
681
700
|
escapeHtml,
|
|
682
701
|
jit
|
|
683
702
|
};
|
|
684
|
-
let ast
|
|
703
|
+
let ast;
|
|
704
|
+
try {
|
|
705
|
+
ast = parseJSON(value, { filePath: filename });
|
|
706
|
+
} catch (err) {
|
|
707
|
+
throw createResourceParseError(err, options);
|
|
708
|
+
}
|
|
709
|
+
const root = ast.body[0].expression;
|
|
710
|
+
if (root.type !== "JSONObjectExpression") {
|
|
711
|
+
const rootValue = getStaticJSONValue(root);
|
|
712
|
+
if (rootValue !== null) {
|
|
713
|
+
throw createResourceError(
|
|
714
|
+
`You need to define an object as the locale messages at the top level, but got ${describeValue(rootValue)}`,
|
|
715
|
+
options
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
value = JSON.stringify({});
|
|
719
|
+
ast = parseJSON(value, { filePath: filename });
|
|
720
|
+
options.source = void 0;
|
|
721
|
+
}
|
|
685
722
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
686
723
|
const messages = getStaticJSONValue(ast);
|
|
687
724
|
value = JSON.stringify(
|
|
@@ -877,6 +914,9 @@ function generate(targetSource, {
|
|
|
877
914
|
usedKeyFilter = void 0
|
|
878
915
|
}) {
|
|
879
916
|
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource;
|
|
917
|
+
if (value.charCodeAt(0) === 65279) {
|
|
918
|
+
value = value.slice(1);
|
|
919
|
+
}
|
|
880
920
|
const options = {
|
|
881
921
|
type,
|
|
882
922
|
source: value,
|
|
@@ -892,9 +932,31 @@ function generate(targetSource, {
|
|
|
892
932
|
escapeHtml,
|
|
893
933
|
jit
|
|
894
934
|
};
|
|
895
|
-
let ast
|
|
935
|
+
let ast;
|
|
936
|
+
try {
|
|
937
|
+
ast = parseYAML(value, { filePath: filename });
|
|
938
|
+
} catch (err) {
|
|
939
|
+
throw createResourceParseError(err, options, 1);
|
|
940
|
+
}
|
|
941
|
+
const rootDocument = getRootDocument(ast, options);
|
|
942
|
+
const content = rootDocument?.content;
|
|
943
|
+
const root = content?.type === "YAMLWithMeta" ? content.value : content;
|
|
944
|
+
if (root?.type !== "YAMLMapping") {
|
|
945
|
+
const isAlias = root?.type === "YAMLAlias";
|
|
946
|
+
const rootValue = rootDocument == null || isAlias ? null : getStaticYAMLValue(rootDocument);
|
|
947
|
+
if (isAlias || rootValue !== null) {
|
|
948
|
+
const actual = isAlias ? "an alias" : describeValue(rootValue);
|
|
949
|
+
throw createResourceError(
|
|
950
|
+
`You need to define an object as the locale messages at the top level, but got ${actual}`,
|
|
951
|
+
options
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
value = JSON.stringify({});
|
|
955
|
+
ast = parseYAML(value, { filePath: filename });
|
|
956
|
+
options.source = void 0;
|
|
957
|
+
}
|
|
896
958
|
if (!locale && type === "sfc" && onlyLocales?.length) {
|
|
897
|
-
const messages = getStaticYAMLValue(ast);
|
|
959
|
+
const messages = getStaticYAMLValue(getRootDocument(ast, options));
|
|
898
960
|
value = JSON.stringify(
|
|
899
961
|
excludeLocales({
|
|
900
962
|
messages,
|
|
@@ -910,7 +972,7 @@ function generate(targetSource, {
|
|
|
910
972
|
options.source = void 0;
|
|
911
973
|
}
|
|
912
974
|
if (usedKeyFilter) {
|
|
913
|
-
const messages = getStaticYAMLValue(ast);
|
|
975
|
+
const messages = getStaticYAMLValue(getRootDocument(ast, options));
|
|
914
976
|
const isMultiLocale = !locale && type === "sfc";
|
|
915
977
|
const filtered = isMultiLocale ? filterMultiLocaleMessages(messages, usedKeyFilter) : filterMessageKeys(messages, usedKeyFilter);
|
|
916
978
|
value = JSON.stringify(filtered);
|
|
@@ -927,6 +989,16 @@ function generate(targetSource, {
|
|
|
927
989
|
map: newMap != null ? newMap : void 0
|
|
928
990
|
};
|
|
929
991
|
}
|
|
992
|
+
function getRootDocument(ast, options) {
|
|
993
|
+
const documents = ast.body.filter((doc) => doc.content != null);
|
|
994
|
+
if (documents.length > 1) {
|
|
995
|
+
throw createResourceError(
|
|
996
|
+
`You need to define the locale messages in a single YAML document, but got ${documents.length} documents`,
|
|
997
|
+
options
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
return documents[0] ?? null;
|
|
1001
|
+
}
|
|
930
1002
|
function _generate(generator, node, options = {}) {
|
|
931
1003
|
const propsCountStack = [];
|
|
932
1004
|
const pathStack = [];
|