@lingui/conf 4.0.0-next.2 → 4.0.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,422 @@
1
+ 'use strict';
2
+
3
+ const chalk = require('chalk');
4
+ const jestValidate = require('jest-validate');
5
+ const path = require('path');
6
+ const fs = require('fs');
7
+ const cosmiconfig = require('cosmiconfig');
8
+
9
+ function replaceRootDir(config, rootDir) {
10
+ return function replaceDeep(value, rootDir2) {
11
+ const replace = (s) => s.replace("<rootDir>", rootDir2);
12
+ if (value == null) {
13
+ return value;
14
+ } else if (typeof value === "string") {
15
+ return replace(value);
16
+ } else if (Array.isArray(value)) {
17
+ return value.map((item) => replaceDeep(item, rootDir2));
18
+ } else if (typeof value === "object") {
19
+ Object.keys(value).forEach((key) => {
20
+ const newKey = replaceDeep(key, rootDir2);
21
+ value[newKey] = replaceDeep(value[key], rootDir2);
22
+ if (key !== newKey)
23
+ delete value[key];
24
+ });
25
+ }
26
+ return value;
27
+ }(config, rootDir);
28
+ }
29
+
30
+ function setCldrParentLocales(config) {
31
+ if (config.fallbackLocales === false) {
32
+ return {
33
+ ...config,
34
+ fallbackLocales: {}
35
+ };
36
+ }
37
+ if (!config.fallbackLocales.default) {
38
+ config.locales.forEach((locale) => {
39
+ const fl = getCldrParentLocale(locale.toLowerCase());
40
+ if (fl && !config.fallbackLocales[locale]) {
41
+ config.fallbackLocales = {
42
+ ...config.fallbackLocales,
43
+ [locale]: fl
44
+ };
45
+ }
46
+ });
47
+ }
48
+ return config;
49
+ }
50
+ function getCldrParentLocale(sourceLocale) {
51
+ return {
52
+ "en-ag": "en",
53
+ "en-ai": "en",
54
+ "en-au": "en",
55
+ "en-bb": "en",
56
+ "en-bm": "en",
57
+ "en-bs": "en",
58
+ "en-bw": "en",
59
+ "en-bz": "en",
60
+ "en-ca": "en",
61
+ "en-cc": "en",
62
+ "en-ck": "en",
63
+ "en-cm": "en",
64
+ "en-cx": "en",
65
+ "en-cy": "en",
66
+ "en-dg": "en",
67
+ "en-dm": "en",
68
+ "en-er": "en",
69
+ "en-fj": "en",
70
+ "en-fk": "en",
71
+ "en-fm": "en",
72
+ "en-gb": "en",
73
+ "en-gd": "en",
74
+ "en-gg": "en",
75
+ "en-gh": "en",
76
+ "en-gi": "en",
77
+ "en-gm": "en",
78
+ "en-gy": "en",
79
+ "en-hk": "en",
80
+ "en-ie": "en",
81
+ "en-il": "en",
82
+ "en-im": "en",
83
+ "en-in": "en",
84
+ "en-io": "en",
85
+ "en-je": "en",
86
+ "en-jm": "en",
87
+ "en-ke": "en",
88
+ "en-ki": "en",
89
+ "en-kn": "en",
90
+ "en-ky": "en",
91
+ "en-lc": "en",
92
+ "en-lr": "en",
93
+ "en-ls": "en",
94
+ "en-mg": "en",
95
+ "en-mo": "en",
96
+ "en-ms": "en",
97
+ "en-mt": "en",
98
+ "en-mu": "en",
99
+ "en-mw": "en",
100
+ "en-my": "en",
101
+ "en-na": "en",
102
+ "en-nf": "en",
103
+ "en-ng": "en",
104
+ "en-nr": "en",
105
+ "en-nu": "en",
106
+ "en-nz": "en",
107
+ "en-pg": "en",
108
+ "en-ph": "en",
109
+ "en-pk": "en",
110
+ "en-pn": "en",
111
+ "en-pw": "en",
112
+ "en-rw": "en",
113
+ "en-sb": "en",
114
+ "en-sc": "en",
115
+ "en-sd": "en",
116
+ "en-sg": "en",
117
+ "en-sh": "en",
118
+ "en-sl": "en",
119
+ "en-ss": "en",
120
+ "en-sx": "en",
121
+ "en-sz": "en",
122
+ "en-tc": "en",
123
+ "en-tk": "en",
124
+ "en-to": "en",
125
+ "en-tt": "en",
126
+ "en-tv": "en",
127
+ "en-tz": "en",
128
+ "en-ug": "en",
129
+ "en-us": "en",
130
+ "en-vc": "en",
131
+ "en-vg": "en",
132
+ "en-vu": "en",
133
+ "en-ws": "en",
134
+ "en-za": "en",
135
+ "en-zm": "en",
136
+ "en-zw": "en",
137
+ "en-at": "en",
138
+ "en-be": "en",
139
+ "en-ch": "en",
140
+ "en-de": "en",
141
+ "en-dk": "en",
142
+ "en-fi": "en",
143
+ "en-nl": "en",
144
+ "en-se": "en",
145
+ "en-si": "en",
146
+ "es-ar": "es",
147
+ "es-bo": "es",
148
+ "es-br": "es",
149
+ "es-bz": "es",
150
+ "es-cl": "es",
151
+ "es-co": "es",
152
+ "es-cr": "es",
153
+ "es-cu": "es",
154
+ "es-do": "es",
155
+ "es-ec": "es",
156
+ "es-es": "es",
157
+ "es-gt": "es",
158
+ "es-hn": "es",
159
+ "es-mx": "es",
160
+ "es-ni": "es",
161
+ "es-pa": "es",
162
+ "es-pe": "es",
163
+ "es-pr": "es",
164
+ "es-py": "es",
165
+ "es-sv": "es",
166
+ "es-us": "es",
167
+ "es-uy": "es",
168
+ "es-ve": "es",
169
+ "pt-ao": "pt",
170
+ "pt-ch": "pt",
171
+ "pt-cv": "pt",
172
+ "pt-fr": "pt",
173
+ "pt-gq": "pt",
174
+ "pt-gw": "pt",
175
+ "pt-lu": "pt",
176
+ "pt-mo": "pt",
177
+ "pt-mz": "pt",
178
+ "pt-pt": "pt",
179
+ "pt-st": "pt",
180
+ "pt-tl": "pt",
181
+ "az-arab": "az",
182
+ "az-cyrl": "az",
183
+ "blt-latn": "blt",
184
+ "bm-nkoo": "bm",
185
+ "bs-cyrl": "bs",
186
+ "byn-latn": "byn",
187
+ "cu-glag": "cu",
188
+ "dje-arab": "dje",
189
+ "dyo-arab": "dyo",
190
+ "en-dsrt": "en",
191
+ "en-shaw": "en",
192
+ "ff-adlm": "ff",
193
+ "ff-arab": "ff",
194
+ "ha-arab": "ha",
195
+ "hi-latn": "hi",
196
+ "iu-latn": "iu",
197
+ "kk-arab": "kk",
198
+ "ks-deva": "ks",
199
+ "ku-arab": "ku",
200
+ "ky-arab": "ky",
201
+ "ky-latn": "ky",
202
+ "ml-arab": "ml",
203
+ "mn-mong": "mn",
204
+ "mni-mtei": "mni",
205
+ "ms-arab": "ms",
206
+ "pa-arab": "pa",
207
+ "sat-deva": "sat",
208
+ "sd-deva": "sd",
209
+ "sd-khoj": "sd",
210
+ "sd-sind": "sd",
211
+ "shi-latn": "shi",
212
+ "so-arab": "so",
213
+ "sr-latn": "sr",
214
+ "sw-arab": "sw",
215
+ "tg-arab": "tg",
216
+ "ug-cyrl": "ug",
217
+ "uz-arab": "uz",
218
+ "uz-cyrl": "uz",
219
+ "vai-latn": "vai",
220
+ "wo-arab": "wo",
221
+ "yo-arab": "yo",
222
+ "yue-hans": "yue",
223
+ "zh-hant": "zh",
224
+ "zh-hant-hk": "zh",
225
+ "zh-hant-mo": "zh-hant-hk"
226
+ }[sourceLocale];
227
+ }
228
+
229
+ const pathJoinPosix = (...values) => path.join(...values).split(path.sep).join("/");
230
+
231
+ const getSymbolSource = (defaults, config) => {
232
+ const name = defaults[1];
233
+ if (Array.isArray(config)) {
234
+ if (name === "i18n") {
235
+ return config;
236
+ }
237
+ return defaults;
238
+ }
239
+ return config[name] || defaults;
240
+ };
241
+ function normalizeRuntimeConfigModule(config) {
242
+ const [i18nImportModule, i18nImportName] = getSymbolSource(
243
+ ["@lingui/core", "i18n"],
244
+ config.runtimeConfigModule
245
+ );
246
+ const [TransImportModule, TransImportName] = getSymbolSource(
247
+ ["@lingui/react", "Trans"],
248
+ config.runtimeConfigModule
249
+ );
250
+ return {
251
+ ...config,
252
+ runtimeConfigModule: {
253
+ i18nImportModule,
254
+ i18nImportName,
255
+ TransImportModule,
256
+ TransImportName
257
+ }
258
+ };
259
+ }
260
+
261
+ function makeConfig(userConfig, opts = {}) {
262
+ let config = {
263
+ ...defaultConfig,
264
+ ...userConfig
265
+ };
266
+ if (!opts.skipValidation) {
267
+ jestValidate.validate(config, configValidation);
268
+ validateLocales(config);
269
+ }
270
+ config = pipe(
271
+ // List config migrations from oldest to newest
272
+ setCldrParentLocales,
273
+ normalizeRuntimeConfigModule
274
+ )(config);
275
+ return replaceRootDir(
276
+ config,
277
+ config.rootDir
278
+ );
279
+ }
280
+ const defaultConfig = {
281
+ catalogs: [
282
+ {
283
+ path: pathJoinPosix("<rootDir>", "locale", "{locale}", "messages"),
284
+ include: ["<rootDir>"],
285
+ exclude: ["*/node_modules/*"]
286
+ }
287
+ ],
288
+ catalogsMergePath: "",
289
+ compileNamespace: "cjs",
290
+ compilerBabelOptions: {
291
+ minified: true,
292
+ jsescOption: {
293
+ minimal: true
294
+ }
295
+ },
296
+ extractorParserOptions: {
297
+ flow: false,
298
+ tsExperimentalDecorators: false
299
+ },
300
+ fallbackLocales: {},
301
+ format: "po",
302
+ formatOptions: { origins: true, lineNumbers: true },
303
+ locales: [],
304
+ orderBy: "message",
305
+ pseudoLocale: "",
306
+ rootDir: ".",
307
+ runtimeConfigModule: ["@lingui/core", "i18n"],
308
+ sourceLocale: "",
309
+ service: { name: "", apiKey: "" }
310
+ };
311
+ const exampleConfig = {
312
+ ...defaultConfig,
313
+ format: jestValidate.multipleValidOptions({}, "po"),
314
+ extractors: jestValidate.multipleValidOptions([], ["babel"], [Object]),
315
+ runtimeConfigModule: jestValidate.multipleValidOptions(
316
+ { i18n: ["@lingui/core", "i18n"], Trans: ["@lingui/react", "Trans"] },
317
+ ["@lingui/core", "i18n"]
318
+ ),
319
+ fallbackLocales: jestValidate.multipleValidOptions(
320
+ {},
321
+ { "en-US": "en" },
322
+ { "en-US": ["en"] },
323
+ { default: "en" },
324
+ false
325
+ ),
326
+ extractorParserOptions: {
327
+ flow: false,
328
+ tsExperimentalDecorators: false
329
+ },
330
+ experimental: {
331
+ extractor: {
332
+ entries: [],
333
+ includeDeps: [],
334
+ excludeDeps: [],
335
+ excludeExtensions: [],
336
+ output: "",
337
+ resolveEsbuildOptions: Function
338
+ }
339
+ }
340
+ };
341
+ const extractBabelOptionsDeprecations = {
342
+ extractBabelOptions: () => ` Option ${chalk.bold("extractBabelOptions")} was removed.
343
+
344
+ Please remove it from your config file.
345
+
346
+ You can find more information here: https://lingui.dev/releases/migration-4
347
+ `
348
+ };
349
+ const configValidation = {
350
+ exampleConfig,
351
+ deprecatedConfig: {
352
+ ...extractBabelOptionsDeprecations
353
+ },
354
+ comment: "Documentation: https://lingui.dev/ref/conf"
355
+ };
356
+ function validateLocales(config) {
357
+ if (!Array.isArray(config.locales) || !config.locales.length) {
358
+ console.error("No locales defined!\n");
359
+ console.error(
360
+ `Add ${chalk.yellow(
361
+ "'locales'"
362
+ )} to your configuration. See ${chalk.underline(
363
+ "https://lingui.dev/ref/conf#locales"
364
+ )}`
365
+ );
366
+ }
367
+ }
368
+ const pipe = (...functions) => (args) => functions.reduce((arg, fn) => fn(arg), args);
369
+
370
+ function configExists(path2) {
371
+ return path2 && fs.existsSync(path2);
372
+ }
373
+ function JitiLoader() {
374
+ return (filepath, content) => {
375
+ const opts = {
376
+ interopDefault: true
377
+ };
378
+ const jiti = require("jiti")(__filename, opts);
379
+ return jiti(filepath);
380
+ };
381
+ }
382
+ function getConfig({
383
+ cwd,
384
+ configPath,
385
+ skipValidation = false
386
+ } = {}) {
387
+ const defaultRootDir = cwd || process.cwd();
388
+ const moduleName = "lingui";
389
+ const configExplorer = cosmiconfig.cosmiconfigSync(moduleName, {
390
+ searchPlaces: [
391
+ `${moduleName}.config.js`,
392
+ `${moduleName}.config.cjs`,
393
+ `${moduleName}.config.ts`,
394
+ `${moduleName}.config.mjs`,
395
+ "package.json",
396
+ `.${moduleName}rc`,
397
+ `.${moduleName}rc.json`,
398
+ `.${moduleName}rc.yaml`,
399
+ `.${moduleName}rc.yml`,
400
+ `.${moduleName}rc.ts`,
401
+ `.${moduleName}rc.js`
402
+ ],
403
+ loaders: {
404
+ ".js": JitiLoader(),
405
+ ".ts": JitiLoader(),
406
+ ".mjs": JitiLoader()
407
+ }
408
+ });
409
+ configPath = configPath || process.env.LINGUI_CONFIG;
410
+ const result = configExists(configPath) ? configExplorer.load(configPath) : configExplorer.search(defaultRootDir);
411
+ const userConfig = result ? result.config : {};
412
+ return makeConfig(
413
+ {
414
+ rootDir: result ? path.dirname(result.filepath) : defaultRootDir,
415
+ ...userConfig
416
+ },
417
+ { skipValidation }
418
+ );
419
+ }
420
+
421
+ exports.getConfig = getConfig;
422
+ exports.makeConfig = makeConfig;
@@ -1,6 +1,19 @@
1
- import type { GeneratorOptions } from "@babel/core";
2
- export type CatalogFormat = "lingui" | "minimal" | "po" | "csv" | "po-gettext";
3
- export type ExtractorCtx = {
1
+ /**
2
+ * @deprecated please pass formatter directly to `format`
3
+ *
4
+ * @example
5
+ * ```js
6
+ * // lingui.config.{js,ts}
7
+ * import {formatter} from "@lingui/format-po"
8
+ *
9
+ * export default {
10
+ * [...]
11
+ * format: formatter({lineNumbers: false}),
12
+ * }
13
+ * ```
14
+ */
15
+ type CatalogFormat = "lingui" | "minimal" | "po" | "csv" | "po-gettext";
16
+ type ExtractorCtx = {
4
17
  /**
5
18
  * Raw Sourcemaps object to mapping from.
6
19
  * Check the https://github.com/mozilla/source-map#new-sourcemapconsumerrawsourcemap
@@ -8,30 +21,34 @@ export type ExtractorCtx = {
8
21
  sourceMaps?: any;
9
22
  linguiConfig: LinguiConfigNormalized;
10
23
  };
11
- export type MessageOrigin = [filename: string, line?: number];
12
- export type ExtractedMessageType = {
24
+ type CatalogExtra = Record<string, unknown>;
25
+ type MessageOrigin = [filename: string, line?: number];
26
+ type ExtractedMessageType<Extra = CatalogExtra> = {
13
27
  message?: string;
14
28
  origin?: MessageOrigin[];
15
29
  comments?: string[];
16
- extractedComments?: string[];
17
30
  obsolete?: boolean;
18
- flags?: string[];
19
31
  context?: string;
32
+ /**
33
+ * the generic field where
34
+ * formatters can store additional data
35
+ */
36
+ extra?: Extra;
20
37
  };
21
- export type MessageType = ExtractedMessageType & {
38
+ type MessageType<Extra = CatalogExtra> = ExtractedMessageType<Extra> & {
22
39
  translation: string;
23
40
  };
24
- export type ExtractedCatalogType = {
25
- [msgId: string]: ExtractedMessageType;
41
+ type ExtractedCatalogType<Extra = CatalogExtra> = {
42
+ [msgId: string]: ExtractedMessageType<Extra>;
26
43
  };
27
- export type CatalogType = {
28
- [msgId: string]: MessageType;
44
+ type CatalogType<Extra = CatalogExtra> = {
45
+ [msgId: string]: MessageType<Extra>;
29
46
  };
30
- export type ExtractorType = {
47
+ type ExtractorType = {
31
48
  match(filename: string): boolean;
32
49
  extract(filename: string, code: string, onMessageExtracted: (msg: ExtractedMessage) => void, ctx?: ExtractorCtx): Promise<void> | void;
33
50
  };
34
- export type CatalogFormatter = {
51
+ type CatalogFormatter = {
35
52
  catalogExtension: string;
36
53
  /**
37
54
  * Set extension used when extract to template
@@ -40,28 +57,30 @@ export type CatalogFormatter = {
40
57
  templateExtension?: string;
41
58
  parse(content: string, ctx: {
42
59
  locale: string | null;
60
+ sourceLocale: string;
43
61
  filename: string;
44
62
  }): Promise<CatalogType> | CatalogType;
45
63
  serialize(catalog: CatalogType, ctx: {
46
64
  locale: string | null;
65
+ sourceLocale: string;
47
66
  filename: string;
48
67
  existing: string | null;
49
68
  }): Promise<string> | string;
50
69
  };
51
- export type ExtractedMessage = {
70
+ type ExtractedMessage = {
52
71
  id: string;
53
72
  message?: string;
54
73
  context?: string;
55
74
  origin?: [filename: string, line: number, column?: number];
56
75
  comment?: string;
57
76
  };
58
- export type CatalogFormatOptions = {
77
+ type CatalogFormatOptions = {
59
78
  origins?: boolean;
60
79
  lineNumbers?: boolean;
61
80
  disableSelectWarning?: boolean;
62
81
  };
63
- export type OrderBy = "messageId" | "message" | "origin";
64
- export type CatalogConfig = {
82
+ type OrderBy = "messageId" | "message" | "origin";
83
+ type CatalogConfig = {
65
84
  name?: string;
66
85
  path: string;
67
86
  include: string[];
@@ -71,13 +90,13 @@ type LocaleObject = {
71
90
  [locale: string]: string[] | string;
72
91
  default?: string;
73
92
  };
74
- export type FallbackLocales = LocaleObject;
93
+ type FallbackLocales = LocaleObject;
75
94
  type ModuleSource = [string, string?];
76
95
  type CatalogService = {
77
96
  name: string;
78
97
  apiKey: string;
79
98
  };
80
- export type ExperimentalExtractorOptions = {
99
+ type ExperimentalExtractorOptions = {
81
100
  /**
82
101
  * Entries to start extracting from.
83
102
  * Each separate resolved entry would create a separate catalog.
@@ -132,7 +151,7 @@ export type ExperimentalExtractorOptions = {
132
151
  output: string;
133
152
  resolveEsbuildOptions?: (options: any) => any;
134
153
  };
135
- export type LinguiConfig = {
154
+ type LinguiConfig = {
136
155
  catalogs?: CatalogConfig[];
137
156
  compileNamespace?: "es" | "ts" | "cjs" | string;
138
157
  extractorParserOptions?: {
@@ -148,7 +167,7 @@ export type LinguiConfig = {
148
167
  */
149
168
  flow?: boolean;
150
169
  };
151
- compilerBabelOptions?: GeneratorOptions;
170
+ compilerBabelOptions?: any;
152
171
  fallbackLocales?: FallbackLocales | false;
153
172
  extractors?: (string | ExtractorType)[];
154
173
  prevFormat?: CatalogFormat;
@@ -169,7 +188,7 @@ export type LinguiConfig = {
169
188
  extractor?: ExperimentalExtractorOptions;
170
189
  };
171
190
  };
172
- export type LinguiConfigNormalized = Omit<LinguiConfig, "runtimeConfigModule"> & {
191
+ type LinguiConfigNormalized = Omit<LinguiConfig, "runtimeConfigModule"> & {
173
192
  fallbackLocales?: FallbackLocales;
174
193
  runtimeConfigModule: {
175
194
  i18nImportModule: string;
@@ -178,4 +197,15 @@ export type LinguiConfigNormalized = Omit<LinguiConfig, "runtimeConfigModule"> &
178
197
  TransImportName: string;
179
198
  };
180
199
  };
181
- export {};
200
+
201
+ declare function makeConfig(userConfig: Partial<LinguiConfig>, opts?: {
202
+ skipValidation?: boolean;
203
+ }): LinguiConfigNormalized;
204
+
205
+ declare function getConfig({ cwd, configPath, skipValidation, }?: {
206
+ cwd?: string;
207
+ configPath?: string;
208
+ skipValidation?: boolean;
209
+ }): LinguiConfigNormalized;
210
+
211
+ export { CatalogConfig, CatalogFormat, CatalogFormatOptions, CatalogFormatter, CatalogType, ExperimentalExtractorOptions, ExtractedCatalogType, ExtractedMessage, ExtractedMessageType, ExtractorCtx, ExtractorType, FallbackLocales, LinguiConfig, LinguiConfigNormalized, MessageOrigin, MessageType, OrderBy, getConfig, makeConfig };