@discord/intl-loader-core 0.0.5 → 0.0.6-rc.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl-loader-core",
3
- "version": "0.0.5",
3
+ "version": "0.0.6-rc.0",
4
4
  "license": "MIT",
5
5
  "description": "Core utilities for writing loaders and transformers using @discord/intl",
6
6
  "author": "Jon Egeland",
@@ -19,7 +19,7 @@
19
19
  "chokidar": "^3.6.0",
20
20
  "debug": "^4.3.6",
21
21
  "fast-glob": "^3.3.2",
22
- "@discord/intl-message-database": "0.0.5"
22
+ "@discord/intl-message-database": "0.0.6-rc.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/debug": "^4.1.12"
package/src/processing.js CHANGED
@@ -1,10 +1,21 @@
1
1
  const path = require('node:path');
2
2
 
3
+ const debug = require('debug')('intl:loader-core');
3
4
  const { IntlCompiledMessageFormat } = require('@discord/intl-message-database');
4
5
 
5
6
  const { database } = require('./database');
6
7
  const { findAllTranslationFiles, getLocaleFromTranslationsFileName } = require('./util');
7
8
 
9
+ /**
10
+ * @param {string} sourcePath
11
+ * @param {import('@discord/intl-message-database').IntlSourceFile} sourceFile
12
+ */
13
+ function debugSourceFile(sourcePath, sourceFile) {
14
+ debug(
15
+ `[${sourcePath}] Parsed messages file: type=${sourceFile.type}, locale=${sourceFile.locale}, messageCount=${sourceFile.messageKeys.length}, meta=${JSON.stringify(sourceFile.meta)}`,
16
+ );
17
+ }
18
+
8
19
  /**
9
20
  * @param {string} sourcePath
10
21
  * @param {string=} sourceContent
@@ -20,6 +31,8 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
20
31
  // TODO: Make this more configurable/automatically determined.
21
32
  locale = 'en-US',
22
33
  } = options;
34
+ debug(`[${sourcePath}] Processing definitions with locale "${locale}"`);
35
+
23
36
  if (sourceContent != null) {
24
37
  database.processDefinitionsFileContent(sourcePath, sourceContent);
25
38
  } else {
@@ -27,15 +40,22 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
27
40
  }
28
41
 
29
42
  const sourceFile = database.getSourceFile(sourcePath);
43
+ debugSourceFile(sourcePath, sourceFile);
30
44
  if (sourceFile.type !== 'definition') {
31
45
  throw new Error(
32
46
  `Expected ${sourcePath} to be a message definitions file, but it resulted in ${sourceFile.type} instead.`,
33
47
  );
34
48
  }
35
49
 
36
- const hashedMessageKeys = database.getSourceFileHashedKeys(sourcePath);
50
+ const messageKeys = database.getSourceFileKeyMap(sourcePath);
37
51
  const translationsPath = path.resolve(path.dirname(sourcePath), sourceFile.meta.translationsPath);
38
- const translationsLocaleMap = findAllTranslationFiles(translationsPath);
52
+ let translationsLocaleMap = findAllTranslationFiles(translationsPath);
53
+ if (translationsLocaleMap instanceof Error) {
54
+ debug(
55
+ `[${sourcePath}] Failed to build translations locale map: [${translationsLocaleMap.name}] ${translationsLocaleMap.message}`,
56
+ );
57
+ translationsLocaleMap = {};
58
+ }
39
59
 
40
60
  if (processTranslations) {
41
61
  database.processAllTranslationFiles(translationsLocaleMap);
@@ -44,7 +64,7 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
44
64
  return {
45
65
  sourceFile,
46
66
  locale,
47
- hashedMessageKeys,
67
+ messageKeys,
48
68
  translationsPath,
49
69
  translationsLocaleMap,
50
70
  };
@@ -56,12 +76,11 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
56
76
  * @param {string=} sourceContent
57
77
  * @param {{
58
78
  * locale?: string,
59
- * outputFile?: string
60
79
  * }=} options
61
80
  * @returns {import('./types').ProcessTranslationsResult}
62
81
  */
63
82
  function processTranslationsFile(sourcePath, sourceContent, options = {}) {
64
- const { locale = getLocaleFromTranslationsFileName(sourcePath), outputFile } = options;
83
+ const { locale = getLocaleFromTranslationsFileName(sourcePath) } = options;
65
84
  if (sourceContent) {
66
85
  database.processTranslationFileContent(sourcePath, locale, sourceContent);
67
86
  } else {
@@ -69,6 +88,7 @@ function processTranslationsFile(sourcePath, sourceContent, options = {}) {
69
88
  }
70
89
 
71
90
  const sourceFile = database.getSourceFile(sourcePath);
91
+ debugSourceFile(sourcePath, sourceFile);
72
92
  if (sourceFile.type !== 'translation') {
73
93
  throw new Error(
74
94
  `Expected ${sourcePath} to be a message translations file, but it resulted in ${sourceFile.type} instead.`,
@@ -78,7 +98,7 @@ function processTranslationsFile(sourcePath, sourceContent, options = {}) {
78
98
  return {
79
99
  sourceFile,
80
100
  locale,
81
- hashedMessageKeys: database.getSourceFileHashedKeys(sourcePath),
101
+ messageKeys: database.getSourceFileKeyMap(sourcePath),
82
102
  };
83
103
  }
84
104
 
@@ -77,6 +77,20 @@ class MessageDefinitionsTransformer {
77
77
  return `{${localeProperties.join(',')}}`;
78
78
  }
79
79
 
80
+ /**
81
+ * Return a map of key hashes to their original values, as well as a plain-text map of locales
82
+ * to the file names that they import from.
83
+ *
84
+ * @returns {string[]}
85
+ */
86
+ debugModeSetup() {
87
+ if (!this.options.debug) return [];
88
+
89
+ return [
90
+ `loader.withDebugValues(${JSON.stringify(this.options.messageKeys)}, ${JSON.stringify(this.options.localeMap)})`,
91
+ ];
92
+ }
93
+
80
94
  /**
81
95
  * Returns the reduced, transformed output for this file. Currently not
82
96
  * configurable, but could be told to include default messages or preserve
@@ -90,7 +104,9 @@ class MessageDefinitionsTransformer {
90
104
  `const {createLoader} = require('@discord/intl');`,
91
105
  `const _keys = ${JSON.stringify(this.options.messageKeys)};`,
92
106
  `const _locales = ${this.getLocaleRequireMap()};`,
93
- 'const loader = createLoader(_keys, _locales);',
107
+ `const _defaultLocale = ${JSON.stringify(this.options.defaultLocale)};`,
108
+ 'const loader = createLoader(_keys, _locales, _defaultLocale);',
109
+ ...this.debugModeSetup(),
94
110
  'export default loader.getBinds();',
95
111
  ].join('\n');
96
112
  }
package/src/types.d.ts CHANGED
@@ -2,13 +2,20 @@ import { IntlSourceFile } from '@discord/intl-message-database';
2
2
 
3
3
  export interface MessageDefinitionsTransformerOptions {
4
4
  /**
5
- * The list of hashed message keys that this file manages.
5
+ * The map of message keys that this file manages to their original values. By default, only the
6
+ * keys of this map are used (the hashed names), but in debug mode the values will also be
7
+ * included in the transformed file to provide context in errors and warnings.
6
8
  */
7
- messageKeys: string[];
9
+ messageKeys: Record<string, string>;
8
10
  /**
9
11
  * Map of locale names to import paths used for loading translations.
10
12
  */
11
13
  localeMap: Record<string, string>;
14
+ /**
15
+ * Default locale to use for the runtime loader. This is almost always the locale of the source
16
+ * file being transformed, but can be set explicitly to something else for special cases.
17
+ */
18
+ defaultLocale: string;
12
19
  /**
13
20
  * Function to create a prelude that gets injected at the start of the transformed file to set up
14
21
  * anything needed for other injections later on.
@@ -27,6 +34,12 @@ export interface MessageDefinitionsTransformerOptions {
27
34
  * may need to wrap the result with the `default` key, like `.then((data) => ({default: data}))`.
28
35
  */
29
36
  getTranslationImport(importPath: string): string;
37
+
38
+ /**
39
+ * Whether to include additional information about keys and source files in the transformed loader
40
+ * code to provide context for debugging in errors and warning messages.
41
+ */
42
+ debug?: boolean;
30
43
  }
31
44
 
32
45
  /**
@@ -44,11 +57,11 @@ export interface ProcessDefinitionsResult {
44
57
  */
45
58
  locale: string;
46
59
  /**
47
- * The full list of message keys contained by the processed source file. While `sourceFile`
48
- * contains a list of key _symbols_, this list contains all of the resolved strings for the
49
- * hashed message keys.
60
+ * The full map of message keys contained by the processed source file to their original values.
61
+ * While `sourceFile` contains a list of key _symbols_, this list contains all of the
62
+ * resolved strings for the hashed message keys.
50
63
  */
51
- hashedMessageKeys: string[];
64
+ messageKeys: Record<string, string>;
52
65
  /**
53
66
  * Fully-resolved path to the translations directory that was scanned for entries for the
54
67
  * source file.
@@ -73,15 +86,15 @@ export interface ProcessTranslationsResult {
73
86
  * Direct source file from the database that was created or updated by this process.
74
87
  */
75
88
  sourceFile: IntlSourceFile;
89
+ /**
90
+ * The full map of message keys contained by the processed source file to their original values.
91
+ * While `sourceFile` contains a list of key _symbols_, this list contains all of the
92
+ * resolved strings for the hashed message keys.
93
+ */
94
+ messageKeys: Record<string, string>;
76
95
  /**
77
96
  * The locale that was either determined from the sourceFile name or overridden by the options
78
97
  * provided to this call.
79
98
  */
80
99
  locale: string;
81
- /**
82
- * The full list of message keys contained by the processed source file. While `sourceFile`
83
- * contains a list of key _symbols_, this list contains all of the resolved strings for the
84
- * hashed message keys.
85
- */
86
- hashedMessageKeys: string[];
87
100
  }
package/src/util.js CHANGED
@@ -21,7 +21,7 @@ function getLocaleFromTranslationsFileName(fileName) {
21
21
  * as a map from locale name to the path for importing.
22
22
  *
23
23
  * @param {string} translationsPath
24
- * @returns {Record<string, string>}
24
+ * @returns {Record<string, string> | Error}
25
25
  */
26
26
  function findAllTranslationFiles(translationsPath) {
27
27
  /** @type {Record<string, string>} */
@@ -40,7 +40,7 @@ function findAllTranslationFiles(translationsPath) {
40
40
  localeMap[locale] = filePath;
41
41
  }
42
42
  } catch (e) {
43
- throw new Error(
43
+ return new Error(
44
44
  `The translations directory ${translationsPath} was not found. No translations will be loaded for these messages`,
45
45
  );
46
46
  }