@intlify/core-base 9.3.0-beta.26 → 9.3.0-beta.27

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.
@@ -1,9 +1,9 @@
1
1
  /*!
2
- * core-base v9.3.0-beta.26
2
+ * core-base v9.3.0-beta.27
3
3
  * (c) 2023 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
6
- import { getGlobalThis, isObject, isString, isFunction, isNumber, isPlainObject, join, toDisplayString, isArray, format as format$1, isBoolean, assign, isRegExp, warn, escapeHtml, inBrowser, mark, measure, isEmptyObject, generateCodeFrame, generateFormatCacheKey, isDate } from '@intlify/shared';
6
+ import { getGlobalThis, isObject, isString, isFunction, isNumber, isPlainObject, assign, join, toDisplayString, isArray, format as format$1, isBoolean, warn, isRegExp, warnOnce, incrementer, escapeHtml, inBrowser, mark, measure, isEmptyObject, generateCodeFrame, generateFormatCacheKey, isDate } from '@intlify/shared';
7
7
  import { CompileErrorCodes, createCompileError, detectHtmlTag, defaultOnError, baseCompile as baseCompile$1 } from '@intlify/message-compiler';
8
8
  export { CompileErrorCodes, createCompileError } from '@intlify/message-compiler';
9
9
  import { IntlifyDevToolsHooks } from '@intlify/devtools-if';
@@ -19,6 +19,9 @@ function initFeatureFlags() {
19
19
  if (typeof __INTLIFY_JIT_COMPILATION__ !== 'boolean') {
20
20
  getGlobalThis().__INTLIFY_JIT_COMPILATION__ = false;
21
21
  }
22
+ if (typeof __INTLIFY_DROP_MESSAGE_COMPILER__ !== 'boolean') {
23
+ getGlobalThis().__INTLIFY_DROP_MESSAGE_COMPILER__ = false;
24
+ }
22
25
  }
23
26
 
24
27
  const pathStateMachine = [];
@@ -410,7 +413,8 @@ function createMessageContext(options = {}) {
410
413
  ["message" /* HelperNameMap.MESSAGE */]: message,
411
414
  ["type" /* HelperNameMap.TYPE */]: type,
412
415
  ["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
413
- ["normalize" /* HelperNameMap.NORMALIZE */]: normalize
416
+ ["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
417
+ ["values" /* HelperNameMap.VALUES */]: assign({}, _list, _named)
414
418
  };
415
419
  return ctx;
416
420
  }
@@ -444,7 +448,8 @@ const CoreWarnCodes = {
444
448
  FALLBACK_TO_NUMBER_FORMAT: 4,
445
449
  CANNOT_FORMAT_DATE: 5,
446
450
  FALLBACK_TO_DATE_FORMAT: 6,
447
- __EXTEND_POINT__: 7
451
+ EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
452
+ __EXTEND_POINT__: 8
448
453
  };
449
454
  /** @internal */
450
455
  const warnMessages = {
@@ -453,7 +458,8 @@ const warnMessages = {
453
458
  [CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
454
459
  [CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
455
460
  [CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
456
- [CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`
461
+ [CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
462
+ [CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER]: `This project is using Custom Message Compiler, which is an experimental feature. It may receive breaking changes or be removed in the future.`
457
463
  };
458
464
  function getWarnMessage(code, ...args) {
459
465
  return format$1(warnMessages[code], ...args);
@@ -580,7 +586,7 @@ function appendItemToChain(chain, target, blocks) {
580
586
  * Intlify core-base version
581
587
  * @internal
582
588
  */
583
- const VERSION = '9.3.0-beta.26';
589
+ const VERSION = '9.3.0-beta.27';
584
590
  const NOT_REOSLVED = -1;
585
591
  const DEFAULT_LOCALE = 'en-US';
586
592
  const MISSING_RESOLVE_VALUE = '';
@@ -641,10 +647,10 @@ function registerLocaleFallbacker(fallbacker) {
641
647
  }
642
648
  // Additional Meta for Intlify DevTools
643
649
  let _additionalMeta = null;
644
- const setAdditionalMeta = (meta) => {
650
+ const setAdditionalMeta = /* #__PURE__*/ (meta) => {
645
651
  _additionalMeta = meta;
646
652
  };
647
- const getAdditionalMeta = () => _additionalMeta;
653
+ const getAdditionalMeta = /* #__PURE__*/ () => _additionalMeta;
648
654
  let _fallbackContext = null;
649
655
  const setFallbackContext = (context) => {
650
656
  _fallbackContext = context;
@@ -654,6 +660,7 @@ const getFallbackContext = () => _fallbackContext;
654
660
  let _cid = 0;
655
661
  function createCoreContext(options = {}) {
656
662
  // setup options
663
+ const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
657
664
  const version = isString(options.version) ? options.version : VERSION;
658
665
  const locale = isString(options.locale) ? options.locale : DEFAULT_LOCALE;
659
666
  const fallbackLocale = isArray(options.fallbackLocale) ||
@@ -695,6 +702,12 @@ function createCoreContext(options = {}) {
695
702
  const messageCompiler = isFunction(options.messageCompiler)
696
703
  ? options.messageCompiler
697
704
  : _compiler;
705
+ if ((process.env.NODE_ENV !== 'production') &&
706
+ !false &&
707
+ !false &&
708
+ isFunction(options.messageCompiler)) {
709
+ warnOnce(getWarnMessage(CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER));
710
+ }
698
711
  const messageResolver = isFunction(options.messageResolver)
699
712
  ? options.messageResolver
700
713
  : _resolver || resolveWithKeyValue;
@@ -704,7 +717,6 @@ function createCoreContext(options = {}) {
704
717
  const fallbackContext = isObject(options.fallbackContext)
705
718
  ? options.fallbackContext
706
719
  : undefined;
707
- const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
708
720
  // setup internal options
709
721
  const internalOptions = options;
710
722
  const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters)
@@ -862,14 +874,14 @@ function formatMessagePart(ctx, node) {
862
874
  }
863
875
  }
864
876
 
865
- let code = CompileErrorCodes.__EXTEND_POINT__;
866
- const inc = () => ++code;
877
+ const code = CompileErrorCodes.__EXTEND_POINT__;
878
+ const inc = incrementer(code);
867
879
  const CoreErrorCodes = {
868
880
  INVALID_ARGUMENT: code,
869
881
  INVALID_DATE_ARGUMENT: inc(),
870
882
  INVALID_ISO_DATE_ARGUMENT: inc(),
871
- NOT_SUPPORT_AST: inc(),
872
- __EXTEND_POINT__: inc() // 19
883
+ NOT_SUPPORT_NON_STRING_MESSAGE: inc(),
884
+ __EXTEND_POINT__: inc() // 22
873
885
  };
874
886
  function createCoreError(code) {
875
887
  return createCompileError(code, null, (process.env.NODE_ENV !== 'production') ? { messages: errorMessages } : undefined);
@@ -880,7 +892,7 @@ const errorMessages = {
880
892
  [CoreErrorCodes.INVALID_DATE_ARGUMENT]: 'The date provided is an invalid Date object.' +
881
893
  'Make sure your Date represents a valid date.',
882
894
  [CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: 'The argument provided is not a valid ISO date string',
883
- [CoreErrorCodes.NOT_SUPPORT_AST]: 'Not support AST'
895
+ [CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: 'Not support non-string message'
884
896
  };
885
897
 
886
898
  const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
@@ -894,6 +906,9 @@ let compileCache = Object.create(null);
894
906
  function clearCompileCache() {
895
907
  compileCache = Object.create(null);
896
908
  }
909
+ const isMessageAST = (val) => isObject(val) &&
910
+ (val.t === 0 || val.type === 0) &&
911
+ ('b' in val || 'body' in val);
897
912
  function baseCompile(message, options = {}) {
898
913
  // error detecting on compile
899
914
  let detectError = false;
@@ -905,27 +920,25 @@ function baseCompile(message, options = {}) {
905
920
  // compile with mesasge-compiler
906
921
  return { ...baseCompile$1(message, options), detectError };
907
922
  }
908
- function compileToFunction(message, options = {}) {
923
+ function compileToFunction(message, context) {
909
924
  if (!isString(message)) {
910
- throw createCoreError(CoreErrorCodes.NOT_SUPPORT_AST);
925
+ throw createCoreError(CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE);
911
926
  }
912
927
  {
913
928
  // check HTML message
914
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
915
- const warnHtmlMessage = isBoolean(options.warnHtmlMessage)
916
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
917
- options.warnHtmlMessage
929
+ const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
930
+ ? context.warnHtmlMessage
918
931
  : true;
919
932
  (process.env.NODE_ENV !== 'production') && checkHtmlMessage(message, warnHtmlMessage);
920
933
  // check caches
921
- const onCacheKey = options.onCacheKey || defaultOnCacheKey;
934
+ const onCacheKey = context.onCacheKey || defaultOnCacheKey;
922
935
  const cacheKey = onCacheKey(message);
923
936
  const cached = compileCache[cacheKey];
924
937
  if (cached) {
925
938
  return cached;
926
939
  }
927
940
  // compile
928
- const { code, detectError } = baseCompile(message, options);
941
+ const { code, detectError } = baseCompile(message, context);
929
942
  // evaluate function
930
943
  const msg = new Function(`return ${code}`)();
931
944
  // if occurred compile error, don't cache
@@ -934,17 +947,17 @@ function compileToFunction(message, options = {}) {
934
947
  : msg;
935
948
  }
936
949
  }
937
- function compile(message, options = {}) {
938
- if (isString(message)) {
950
+ function compile(message, context) {
951
+ if (__INTLIFY_JIT_COMPILATION__ &&
952
+ !__INTLIFY_DROP_MESSAGE_COMPILER__ &&
953
+ isString(message)) {
939
954
  // check HTML message
940
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
941
- const warnHtmlMessage = isBoolean(options.warnHtmlMessage)
942
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
943
- options.warnHtmlMessage
955
+ const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
956
+ ? context.warnHtmlMessage
944
957
  : true;
945
958
  (process.env.NODE_ENV !== 'production') && checkHtmlMessage(message, warnHtmlMessage);
946
959
  // check caches
947
- const onCacheKey = options.onCacheKey || defaultOnCacheKey;
960
+ const onCacheKey = context.onCacheKey || defaultOnCacheKey;
948
961
  const cacheKey = onCacheKey(message);
949
962
  const cached = compileCache[cacheKey];
950
963
  if (cached) {
@@ -952,7 +965,7 @@ function compile(message, options = {}) {
952
965
  }
953
966
  // compile with JIT mode
954
967
  const { ast, detectError } = baseCompile(message, {
955
- ...options,
968
+ ...context,
956
969
  location: (process.env.NODE_ENV !== 'production'),
957
970
  jit: true
958
971
  });
@@ -964,6 +977,10 @@ function compile(message, options = {}) {
964
977
  : msg;
965
978
  }
966
979
  else {
980
+ if ((process.env.NODE_ENV !== 'production') && !isMessageAST(message)) {
981
+ warn(`the message that is resolve with key '${context.key}') is not supported for jit compilation`);
982
+ return (() => message);
983
+ }
967
984
  // AST case (passed from bundler)
968
985
  const cacheKey = message.cacheKey;
969
986
  if (cacheKey) {
@@ -983,7 +1000,6 @@ function compile(message, options = {}) {
983
1000
 
984
1001
  const NOOP_MESSAGE_FUNCTION = () => '';
985
1002
  const isMessageFunction = (val) => isFunction(val);
986
- const isMessageAST = (val) => isObject(val) && val.type === 0 && 'body' in val;
987
1003
  // implementation of `translate` function
988
1004
  function translate(context, ...args) {
989
1005
  const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
@@ -1054,12 +1070,12 @@ function translate(context, ...args) {
1054
1070
  }
1055
1071
  // setup compile error detecting
1056
1072
  let occurred = false;
1057
- const errorDetector = () => {
1073
+ const onError = () => {
1058
1074
  occurred = true;
1059
1075
  };
1060
1076
  // compile message format
1061
1077
  const msg = !isMessageFunction(format)
1062
- ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, errorDetector)
1078
+ ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
1063
1079
  : format;
1064
1080
  // if occurred compile error, return the message format
1065
1081
  if (occurred) {
@@ -1188,7 +1204,7 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
1188
1204
  }
1189
1205
  return [format, targetLocale, message];
1190
1206
  }
1191
- function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, errorDetector) {
1207
+ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
1192
1208
  const { messageCompiler, warnHtmlMessage } = context;
1193
1209
  if (isMessageFunction(format)) {
1194
1210
  const msg = format;
@@ -1212,7 +1228,7 @@ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
1212
1228
  endTag = 'intlify-message-compilation-end';
1213
1229
  mark && mark(startTag);
1214
1230
  }
1215
- const msg = messageCompiler(format, getCompileOptions(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, errorDetector));
1231
+ const msg = messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, onError));
1216
1232
  // for vue-devtools timeline event
1217
1233
  if ((process.env.NODE_ENV !== 'production') && inBrowser) {
1218
1234
  const end = window.performance.now();
@@ -1305,11 +1321,13 @@ function parseTranslateArgs(...args) {
1305
1321
  }
1306
1322
  return [key, options];
1307
1323
  }
1308
- function getCompileOptions(context, locale, key, source, warnHtmlMessage, errorDetector) {
1324
+ function getCompileContext(context, locale, key, source, warnHtmlMessage, onError) {
1309
1325
  return {
1326
+ locale,
1327
+ key,
1310
1328
  warnHtmlMessage,
1311
1329
  onError: (err) => {
1312
- errorDetector && errorDetector(err);
1330
+ onError && onError(err);
1313
1331
  if ((process.env.NODE_ENV !== 'production')) {
1314
1332
  const _source = getSourceForCodeFrame(source);
1315
1333
  const message = `Message compilation error: ${err.message}`;
@@ -1354,10 +1372,10 @@ function getMessageContextOptions(context, locale, message, options) {
1354
1372
  }
1355
1373
  if (isString(val) || isMessageAST(val)) {
1356
1374
  let occurred = false;
1357
- const errorDetector = () => {
1375
+ const onError = () => {
1358
1376
  occurred = true;
1359
1377
  };
1360
- const msg = compileMessageFormat(context, key, locale, val, key, errorDetector);
1378
+ const msg = compileMessageFormat(context, key, locale, val, key, onError);
1361
1379
  return !occurred
1362
1380
  ? msg
1363
1381
  : NOOP_MESSAGE_FUNCTION;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core-base v9.3.0-beta.26
2
+ * core-base v9.3.0-beta.27
3
3
  * (c) 2023 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -398,7 +398,8 @@ function createMessageContext(options = {}) {
398
398
  ["message" /* HelperNameMap.MESSAGE */]: message,
399
399
  ["type" /* HelperNameMap.TYPE */]: type,
400
400
  ["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
401
- ["normalize" /* HelperNameMap.NORMALIZE */]: normalize
401
+ ["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
402
+ ["values" /* HelperNameMap.VALUES */]: shared.assign({}, _list, _named)
402
403
  };
403
404
  return ctx;
404
405
  }
@@ -432,7 +433,8 @@ const CoreWarnCodes = {
432
433
  FALLBACK_TO_NUMBER_FORMAT: 4,
433
434
  CANNOT_FORMAT_DATE: 5,
434
435
  FALLBACK_TO_DATE_FORMAT: 6,
435
- __EXTEND_POINT__: 7
436
+ EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
437
+ __EXTEND_POINT__: 8
436
438
  };
437
439
  /** @internal */
438
440
  const warnMessages = {
@@ -441,7 +443,8 @@ const warnMessages = {
441
443
  [CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
442
444
  [CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
443
445
  [CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
444
- [CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`
446
+ [CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
447
+ [CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER]: `This project is using Custom Message Compiler, which is an experimental feature. It may receive breaking changes or be removed in the future.`
445
448
  };
446
449
  function getWarnMessage(code, ...args) {
447
450
  return shared.format(warnMessages[code], ...args);
@@ -568,7 +571,7 @@ function appendItemToChain(chain, target, blocks) {
568
571
  * Intlify core-base version
569
572
  * @internal
570
573
  */
571
- const VERSION = '9.3.0-beta.26';
574
+ const VERSION = '9.3.0-beta.27';
572
575
  const NOT_REOSLVED = -1;
573
576
  const DEFAULT_LOCALE = 'en-US';
574
577
  const MISSING_RESOLVE_VALUE = '';
@@ -629,10 +632,10 @@ function registerLocaleFallbacker(fallbacker) {
629
632
  }
630
633
  // Additional Meta for Intlify DevTools
631
634
  let _additionalMeta = null;
632
- const setAdditionalMeta = (meta) => {
635
+ const setAdditionalMeta = /* #__PURE__*/ (meta) => {
633
636
  _additionalMeta = meta;
634
637
  };
635
- const getAdditionalMeta = () => _additionalMeta;
638
+ const getAdditionalMeta = /* #__PURE__*/ () => _additionalMeta;
636
639
  let _fallbackContext = null;
637
640
  const setFallbackContext = (context) => {
638
641
  _fallbackContext = context;
@@ -642,6 +645,7 @@ const getFallbackContext = () => _fallbackContext;
642
645
  let _cid = 0;
643
646
  function createCoreContext(options = {}) {
644
647
  // setup options
648
+ const onWarn = shared.isFunction(options.onWarn) ? options.onWarn : shared.warn;
645
649
  const version = shared.isString(options.version) ? options.version : VERSION;
646
650
  const locale = shared.isString(options.locale) ? options.locale : DEFAULT_LOCALE;
647
651
  const fallbackLocale = shared.isArray(options.fallbackLocale) ||
@@ -692,7 +696,6 @@ function createCoreContext(options = {}) {
692
696
  const fallbackContext = shared.isObject(options.fallbackContext)
693
697
  ? options.fallbackContext
694
698
  : undefined;
695
- const onWarn = shared.isFunction(options.onWarn) ? options.onWarn : shared.warn;
696
699
  // setup internal options
697
700
  const internalOptions = options;
698
701
  const __datetimeFormatters = shared.isObject(internalOptions.__datetimeFormatters)
@@ -824,14 +827,14 @@ function formatMessagePart(ctx, node) {
824
827
  }
825
828
  }
826
829
 
827
- let code = messageCompiler.CompileErrorCodes.__EXTEND_POINT__;
828
- const inc = () => ++code;
830
+ const code = messageCompiler.CompileErrorCodes.__EXTEND_POINT__;
831
+ const inc = shared.incrementer(code);
829
832
  const CoreErrorCodes = {
830
833
  INVALID_ARGUMENT: code,
831
834
  INVALID_DATE_ARGUMENT: inc(),
832
835
  INVALID_ISO_DATE_ARGUMENT: inc(),
833
- NOT_SUPPORT_AST: inc(),
834
- __EXTEND_POINT__: inc() // 19
836
+ NOT_SUPPORT_NON_STRING_MESSAGE: inc(),
837
+ __EXTEND_POINT__: inc() // 22
835
838
  };
836
839
  function createCoreError(code) {
837
840
  return messageCompiler.createCompileError(code, null, undefined);
@@ -842,7 +845,7 @@ function createCoreError(code) {
842
845
  [CoreErrorCodes.INVALID_DATE_ARGUMENT]: 'The date provided is an invalid Date object.' +
843
846
  'Make sure your Date represents a valid date.',
844
847
  [CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: 'The argument provided is not a valid ISO date string',
845
- [CoreErrorCodes.NOT_SUPPORT_AST]: 'Not support AST'
848
+ [CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: 'Not support non-string message'
846
849
  });
847
850
 
848
851
  const defaultOnCacheKey = (message) => message;
@@ -850,6 +853,9 @@ let compileCache = Object.create(null);
850
853
  function clearCompileCache() {
851
854
  compileCache = Object.create(null);
852
855
  }
856
+ const isMessageAST = (val) => shared.isObject(val) &&
857
+ (val.t === 0 || val.type === 0) &&
858
+ ('b' in val || 'body' in val);
853
859
  function baseCompile(message, options = {}) {
854
860
  // error detecting on compile
855
861
  let detectError = false;
@@ -861,26 +867,24 @@ function baseCompile(message, options = {}) {
861
867
  // compile with mesasge-compiler
862
868
  return { ...messageCompiler.baseCompile(message, options), detectError };
863
869
  }
864
- function compileToFunction(message, options = {}) {
870
+ function compileToFunction(message, context) {
865
871
  if (!shared.isString(message)) {
866
- throw createCoreError(CoreErrorCodes.NOT_SUPPORT_AST);
872
+ throw createCoreError(CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE);
867
873
  }
868
874
  {
869
875
  // check HTML message
870
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
871
- shared.isBoolean(options.warnHtmlMessage)
872
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
873
- options.warnHtmlMessage
876
+ shared.isBoolean(context.warnHtmlMessage)
877
+ ? context.warnHtmlMessage
874
878
  : true;
875
879
  // check caches
876
- const onCacheKey = options.onCacheKey || defaultOnCacheKey;
880
+ const onCacheKey = context.onCacheKey || defaultOnCacheKey;
877
881
  const cacheKey = onCacheKey(message);
878
882
  const cached = compileCache[cacheKey];
879
883
  if (cached) {
880
884
  return cached;
881
885
  }
882
886
  // compile
883
- const { code, detectError } = baseCompile(message, options);
887
+ const { code, detectError } = baseCompile(message, context);
884
888
  // evaluate function
885
889
  const msg = new Function(`return ${code}`)();
886
890
  // if occurred compile error, don't cache
@@ -889,35 +893,8 @@ function compileToFunction(message, options = {}) {
889
893
  : msg;
890
894
  }
891
895
  }
892
- function compile(message, options = {}) {
893
- if (shared.isString(message)) {
894
- // check HTML message
895
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
896
- shared.isBoolean(options.warnHtmlMessage)
897
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
898
- options.warnHtmlMessage
899
- : true;
900
- // check caches
901
- const onCacheKey = options.onCacheKey || defaultOnCacheKey;
902
- const cacheKey = onCacheKey(message);
903
- const cached = compileCache[cacheKey];
904
- if (cached) {
905
- return cached;
906
- }
907
- // compile with JIT mode
908
- const { ast, detectError } = baseCompile(message, {
909
- ...options,
910
- location: false,
911
- jit: true
912
- });
913
- // compose message function from AST
914
- const msg = format(ast);
915
- // if occurred compile error, don't cache
916
- return !detectError
917
- ? (compileCache[cacheKey] = msg)
918
- : msg;
919
- }
920
- else {
896
+ function compile(message, context) {
897
+ {
921
898
  // AST case (passed from bundler)
922
899
  const cacheKey = message.cacheKey;
923
900
  if (cacheKey) {
@@ -937,7 +914,6 @@ function compile(message, options = {}) {
937
914
 
938
915
  const NOOP_MESSAGE_FUNCTION = () => '';
939
916
  const isMessageFunction = (val) => shared.isFunction(val);
940
- const isMessageAST = (val) => shared.isObject(val) && val.type === 0 && 'body' in val;
941
917
  // implementation of `translate` function
942
918
  function translate(context, ...args) {
943
919
  const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
@@ -1000,12 +976,12 @@ function translate(context, ...args) {
1000
976
  }
1001
977
  // setup compile error detecting
1002
978
  let occurred = false;
1003
- const errorDetector = () => {
979
+ const onError = () => {
1004
980
  occurred = true;
1005
981
  };
1006
982
  // compile message format
1007
983
  const msg = !isMessageFunction(format)
1008
- ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, errorDetector)
984
+ ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
1009
985
  : format;
1010
986
  // if occurred compile error, return the message format
1011
987
  if (occurred) {
@@ -1059,7 +1035,7 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
1059
1035
  }
1060
1036
  return [format, targetLocale, message];
1061
1037
  }
1062
- function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, errorDetector) {
1038
+ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
1063
1039
  const { messageCompiler, warnHtmlMessage } = context;
1064
1040
  if (isMessageFunction(format)) {
1065
1041
  const msg = format;
@@ -1073,7 +1049,7 @@ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
1073
1049
  msg.key = key;
1074
1050
  return msg;
1075
1051
  }
1076
- const msg = messageCompiler(format, getCompileOptions(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, errorDetector));
1052
+ const msg = messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, onError));
1077
1053
  msg.locale = targetLocale;
1078
1054
  msg.key = key;
1079
1055
  msg.source = format;
@@ -1122,11 +1098,13 @@ function parseTranslateArgs(...args) {
1122
1098
  }
1123
1099
  return [key, options];
1124
1100
  }
1125
- function getCompileOptions(context, locale, key, source, warnHtmlMessage, errorDetector) {
1101
+ function getCompileContext(context, locale, key, source, warnHtmlMessage, onError) {
1126
1102
  return {
1103
+ locale,
1104
+ key,
1127
1105
  warnHtmlMessage,
1128
1106
  onError: (err) => {
1129
- errorDetector && errorDetector(err);
1107
+ onError && onError(err);
1130
1108
  {
1131
1109
  throw err;
1132
1110
  }
@@ -1145,10 +1123,10 @@ function getMessageContextOptions(context, locale, message, options) {
1145
1123
  }
1146
1124
  if (shared.isString(val) || isMessageAST(val)) {
1147
1125
  let occurred = false;
1148
- const errorDetector = () => {
1126
+ const onError = () => {
1149
1127
  occurred = true;
1150
1128
  };
1151
- const msg = compileMessageFormat(context, key, locale, val, key, errorDetector);
1129
+ const msg = compileMessageFormat(context, key, locale, val, key, onError);
1152
1130
  return !occurred
1153
1131
  ? msg
1154
1132
  : NOOP_MESSAGE_FUNCTION;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/core-base",
3
- "version": "9.3.0-beta.26",
3
+ "version": "9.3.0-beta.27",
4
4
  "description": "@intlify/core-base",
5
5
  "keywords": [
6
6
  "core",
@@ -33,10 +33,10 @@
33
33
  "jsdelivr": "dist/core-base.global.js",
34
34
  "types": "dist/core-base.d.ts",
35
35
  "dependencies": {
36
- "@intlify/message-compiler": "9.3.0-beta.26",
37
- "@intlify/shared": "9.3.0-beta.26",
38
- "@intlify/devtools-if": "9.3.0-beta.26",
39
- "@intlify/vue-devtools": "9.3.0-beta.26"
36
+ "@intlify/shared": "9.3.0-beta.27",
37
+ "@intlify/message-compiler": "9.3.0-beta.27",
38
+ "@intlify/vue-devtools": "9.3.0-beta.27",
39
+ "@intlify/devtools-if": "9.3.0-beta.27"
40
40
  },
41
41
  "engines": {
42
42
  "node": ">= 16"