@lingui/conf 3.14.0 → 3.16.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/build/index.js ADDED
@@ -0,0 +1,484 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getConfig = getConfig;
7
+ exports.replaceRootDir = replaceRootDir;
8
+ exports.fallbackLanguageMigration = fallbackLanguageMigration;
9
+ exports.catalogMigration = catalogMigration;
10
+ exports.configValidation = exports.defaultConfig = void 0;
11
+
12
+ var _path = _interopRequireDefault(require("path"));
13
+
14
+ var _fs = _interopRequireDefault(require("fs"));
15
+
16
+ var _chalk = _interopRequireDefault(require("chalk"));
17
+
18
+ var _cosmiconfig = require("cosmiconfig");
19
+
20
+ var _jestValidate = require("jest-validate");
21
+
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+
24
+ // Enforce posix path delimiters internally
25
+ const pathJoinPosix = (...values) => _path.default // normalize double slashes
26
+ .join(...values) // convert platform specific path.sep to posix
27
+ .split(_path.default.sep).join("/");
28
+
29
+ const defaultConfig = {
30
+ catalogs: [{
31
+ path: pathJoinPosix("<rootDir>", "locale", "{locale}", "messages"),
32
+ include: ["<rootDir>"],
33
+ exclude: ["*/node_modules/*"]
34
+ }],
35
+ catalogsMergePath: "",
36
+ compileNamespace: "cjs",
37
+ compilerBabelOptions: {
38
+ minified: true,
39
+ jsescOption: {
40
+ minimal: true
41
+ }
42
+ },
43
+ extractBabelOptions: {
44
+ plugins: [],
45
+ presets: []
46
+ },
47
+ fallbackLocales: {},
48
+ format: "po",
49
+ formatOptions: {
50
+ origins: true,
51
+ lineNumbers: true
52
+ },
53
+ locales: [],
54
+ orderBy: "messageId",
55
+ pseudoLocale: "",
56
+ rootDir: ".",
57
+ runtimeConfigModule: ["@lingui/core", "i18n"],
58
+ sourceLocale: "",
59
+ service: {
60
+ name: "",
61
+ apiKey: ""
62
+ }
63
+ };
64
+ exports.defaultConfig = defaultConfig;
65
+
66
+ function configExists(path) {
67
+ return path && _fs.default.existsSync(path);
68
+ }
69
+
70
+ function getConfig({
71
+ cwd,
72
+ configPath,
73
+ skipValidation = false
74
+ } = {}) {
75
+ const defaultRootDir = cwd || process.cwd();
76
+ const moduleName = "lingui";
77
+ const configExplorer = (0, _cosmiconfig.cosmiconfigSync)(moduleName, {
78
+ searchPlaces: ["package.json", `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.ts`, `.${moduleName}rc.js`, `${moduleName}.config.ts`, `${moduleName}.config.js`],
79
+ loaders: {
80
+ ".ts": TypeScriptLoader
81
+ }
82
+ });
83
+ const result = configExists(configPath) ? configExplorer.load(configPath) : configExplorer.search(defaultRootDir);
84
+ const userConfig = result ? result.config : {};
85
+ const config = { ...defaultConfig,
86
+ rootDir: result ? _path.default.dirname(result.filepath) : defaultRootDir,
87
+ ...userConfig
88
+ };
89
+
90
+ if (!skipValidation) {
91
+ (0, _jestValidate.validate)(config, configValidation);
92
+ return pipe( // List config migrations from oldest to newest
93
+ fallbackLanguageMigration, catalogMigration, // Custom validation
94
+ validateLocales, // `replaceRootDir` should always be the last
95
+ config => replaceRootDir(config, config.rootDir))(config);
96
+ } else {
97
+ return replaceRootDir(config, config.rootDir);
98
+ }
99
+ }
100
+
101
+ const exampleConfig = { ...defaultConfig,
102
+ extractors: (0, _jestValidate.multipleValidOptions)([], ["babel"], [{
103
+ match: fileName => false,
104
+ extract: (filename, targetDir, options) => {}
105
+ }]),
106
+ runtimeConfigModule: (0, _jestValidate.multipleValidOptions)({
107
+ i18n: ["@lingui/core", "i18n"],
108
+ Trans: ["@lingui/react", "Trans"]
109
+ }, ["@lingui/core", "i18n"]),
110
+ fallbackLocales: (0, _jestValidate.multipleValidOptions)({}, {
111
+ "en-US": "en"
112
+ }, {
113
+ "en-US": ["en"]
114
+ }, {
115
+ default: "en"
116
+ }, false),
117
+ extractBabelOptions: {
118
+ extends: "babelconfig.js",
119
+ rootMode: "rootmode",
120
+ plugins: ["plugin"],
121
+ presets: ["preset"],
122
+ targets: (0, _jestValidate.multipleValidOptions)({}, '> 0.5%', ['> 0.5%', 'not dead'], undefined),
123
+ assumptions: (0, _jestValidate.multipleValidOptions)({}, undefined),
124
+ browserslistConfigFile: (0, _jestValidate.multipleValidOptions)(true, undefined),
125
+ browserslistEnv: (0, _jestValidate.multipleValidOptions)('.browserslistrc', undefined)
126
+ }
127
+ };
128
+ const deprecatedConfig = {
129
+ fallbackLocale: config => ` Option ${_chalk.default.bold("fallbackLocale")} was replaced by ${_chalk.default.bold("fallbackLocales")}
130
+
131
+ You can find more information here: https://github.com/lingui/js-lingui/issues/791
132
+
133
+ @lingui/cli now treats your current configuration as:
134
+ {
135
+ ${_chalk.default.bold('"fallbackLocales"')}: {
136
+ default: ${_chalk.default.bold(`"${config.fallbackLocale}"`)}
137
+ }
138
+ }
139
+
140
+ Please update your configuration.
141
+ `,
142
+ localeDir: config => ` Option ${_chalk.default.bold("localeDir")} is deprecated. Configure source paths using ${_chalk.default.bold("catalogs")} instead.
143
+
144
+ @lingui/cli now treats your current configuration as:
145
+
146
+ {
147
+ ${_chalk.default.bold('"catalogs"')}: ${JSON.stringify(catalogMigration(config).catalogs, null, 2)}
148
+ }
149
+
150
+ Please update your configuration.
151
+ `,
152
+ srcPathDirs: config => ` Option ${_chalk.default.bold("srcPathDirs")} is deprecated. Configure source paths using ${_chalk.default.bold("catalogs")} instead.
153
+
154
+ @lingui/cli now treats your current configuration as:
155
+
156
+ {
157
+ ${_chalk.default.bold('"catalogs"')}: ${JSON.stringify(catalogMigration(config).catalogs, null, 2)}
158
+ }
159
+
160
+ Please update your configuration.
161
+ `,
162
+ srcPathIgnorePatterns: config => ` Option ${_chalk.default.bold("srcPathIgnorePatterns")} is deprecated. Configure excluded source paths using ${_chalk.default.bold("catalogs")} instead.
163
+
164
+ @lingui/cli now treats your current configuration as:
165
+
166
+ {
167
+ ${_chalk.default.bold('"catalogs"')}: ${JSON.stringify(catalogMigration(config).catalogs, null, 2)}
168
+ }
169
+
170
+ Please update your configuration.
171
+ `
172
+ };
173
+ const configValidation = {
174
+ exampleConfig,
175
+ deprecatedConfig,
176
+ comment: "Documentation: https://lingui.js.org/ref/conf.html"
177
+ };
178
+ exports.configValidation = configValidation;
179
+
180
+ function validateLocales(config) {
181
+ if (!Array.isArray(config.locales) || !config.locales.length) {
182
+ console.error("No locales defined!\n");
183
+ console.error(`Add ${_chalk.default.yellow("'locales'")} to your configuration. See ${_chalk.default.underline("https://lingui.js.org/ref/conf.html#locales")}`);
184
+ }
185
+
186
+ return config;
187
+ }
188
+
189
+ function replaceRootDir(config, rootDir) {
190
+ return function replaceDeep(value, rootDir) {
191
+ const replace = s => s.replace("<rootDir>", rootDir);
192
+
193
+ if (value == null) {
194
+ return value;
195
+ } else if (typeof value === "string") {
196
+ return replace(value);
197
+ } else if (Array.isArray(value)) {
198
+ return value.map(item => replaceDeep(item, rootDir));
199
+ } else if (typeof value === "object") {
200
+ Object.keys(value).forEach(key => {
201
+ const newKey = replaceDeep(key, rootDir);
202
+ value[newKey] = replaceDeep(value[key], rootDir);
203
+ if (key !== newKey) delete value[key];
204
+ });
205
+ }
206
+
207
+ return value;
208
+ }(config, rootDir);
209
+ }
210
+ /**
211
+ * Replace fallbackLocale, by the new standard fallbackLocales
212
+ * - https://github.com/lingui/js-lingui/issues/791
213
+ * - Remove anytime after 4.x
214
+ */
215
+
216
+
217
+ function fallbackLanguageMigration(config) {
218
+ const {
219
+ fallbackLocale,
220
+ fallbackLocales
221
+ } = config;
222
+ if (fallbackLocales === false) return { ...config,
223
+ fallbackLocales: null
224
+ };
225
+ const DEFAULT_FALLBACK = fallbackLocales?.default || fallbackLocale;
226
+
227
+ if (DEFAULT_FALLBACK) {
228
+ if (!config.fallbackLocales) config.fallbackLocales = {};
229
+ config.fallbackLocales.default = DEFAULT_FALLBACK;
230
+ }
231
+
232
+ if (config.fallbackLocales !== false && !config.fallbackLocales.default) {
233
+ config.locales.forEach(locale => {
234
+ const fl = getCldrParentLocale(locale.toLowerCase());
235
+
236
+ if (fl && !config.fallbackLocales[locale]) {
237
+ config.fallbackLocales = { ...config.fallbackLocales,
238
+ [locale]: fl
239
+ };
240
+ }
241
+ });
242
+ }
243
+
244
+ return config;
245
+ }
246
+
247
+ function getCldrParentLocale(sourceLocale) {
248
+ return {
249
+ "en-ag": "en",
250
+ "en-ai": "en",
251
+ "en-au": "en",
252
+ "en-bb": "en",
253
+ "en-bm": "en",
254
+ "en-bs": "en",
255
+ "en-bw": "en",
256
+ "en-bz": "en",
257
+ "en-ca": "en",
258
+ "en-cc": "en",
259
+ "en-ck": "en",
260
+ "en-cm": "en",
261
+ "en-cx": "en",
262
+ "en-cy": "en",
263
+ "en-dg": "en",
264
+ "en-dm": "en",
265
+ "en-er": "en",
266
+ "en-fj": "en",
267
+ "en-fk": "en",
268
+ "en-fm": "en",
269
+ "en-gb": "en",
270
+ "en-gd": "en",
271
+ "en-gg": "en",
272
+ "en-gh": "en",
273
+ "en-gi": "en",
274
+ "en-gm": "en",
275
+ "en-gy": "en",
276
+ "en-hk": "en",
277
+ "en-ie": "en",
278
+ "en-il": "en",
279
+ "en-im": "en",
280
+ "en-in": "en",
281
+ "en-io": "en",
282
+ "en-je": "en",
283
+ "en-jm": "en",
284
+ "en-ke": "en",
285
+ "en-ki": "en",
286
+ "en-kn": "en",
287
+ "en-ky": "en",
288
+ "en-lc": "en",
289
+ "en-lr": "en",
290
+ "en-ls": "en",
291
+ "en-mg": "en",
292
+ "en-mo": "en",
293
+ "en-ms": "en",
294
+ "en-mt": "en",
295
+ "en-mu": "en",
296
+ "en-mw": "en",
297
+ "en-my": "en",
298
+ "en-na": "en",
299
+ "en-nf": "en",
300
+ "en-ng": "en",
301
+ "en-nr": "en",
302
+ "en-nu": "en",
303
+ "en-nz": "en",
304
+ "en-pg": "en",
305
+ "en-ph": "en",
306
+ "en-pk": "en",
307
+ "en-pn": "en",
308
+ "en-pw": "en",
309
+ "en-rw": "en",
310
+ "en-sb": "en",
311
+ "en-sc": "en",
312
+ "en-sd": "en",
313
+ "en-sg": "en",
314
+ "en-sh": "en",
315
+ "en-sl": "en",
316
+ "en-ss": "en",
317
+ "en-sx": "en",
318
+ "en-sz": "en",
319
+ "en-tc": "en",
320
+ "en-tk": "en",
321
+ "en-to": "en",
322
+ "en-tt": "en",
323
+ "en-tv": "en",
324
+ "en-tz": "en",
325
+ "en-ug": "en",
326
+ "en-us": "en",
327
+ "en-vc": "en",
328
+ "en-vg": "en",
329
+ "en-vu": "en",
330
+ "en-ws": "en",
331
+ "en-za": "en",
332
+ "en-zm": "en",
333
+ "en-zw": "en",
334
+ "en-at": "en",
335
+ "en-be": "en",
336
+ "en-ch": "en",
337
+ "en-de": "en",
338
+ "en-dk": "en",
339
+ "en-fi": "en",
340
+ "en-nl": "en",
341
+ "en-se": "en",
342
+ "en-si": "en",
343
+ "es-ar": "es",
344
+ "es-bo": "es",
345
+ "es-br": "es",
346
+ "es-bz": "es",
347
+ "es-cl": "es",
348
+ "es-co": "es",
349
+ "es-cr": "es",
350
+ "es-cu": "es",
351
+ "es-do": "es",
352
+ "es-ec": "es",
353
+ "es-es": "es",
354
+ "es-gt": "es",
355
+ "es-hn": "es",
356
+ "es-mx": "es",
357
+ "es-ni": "es",
358
+ "es-pa": "es",
359
+ "es-pe": "es",
360
+ "es-pr": "es",
361
+ "es-py": "es",
362
+ "es-sv": "es",
363
+ "es-us": "es",
364
+ "es-uy": "es",
365
+ "es-ve": "es",
366
+ "pt-ao": "pt",
367
+ "pt-ch": "pt",
368
+ "pt-cv": "pt",
369
+ "pt-fr": "pt",
370
+ "pt-gq": "pt",
371
+ "pt-gw": "pt",
372
+ "pt-lu": "pt",
373
+ "pt-mo": "pt",
374
+ "pt-mz": "pt",
375
+ "pt-pt": "pt",
376
+ "pt-st": "pt",
377
+ "pt-tl": "pt",
378
+ "az-arab": "az",
379
+ "az-cyrl": "az",
380
+ "blt-latn": "blt",
381
+ "bm-nkoo": "bm",
382
+ "bs-cyrl": "bs",
383
+ "byn-latn": "byn",
384
+ "cu-glag": "cu",
385
+ "dje-arab": "dje",
386
+ "dyo-arab": "dyo",
387
+ "en-dsrt": "en",
388
+ "en-shaw": "en",
389
+ "ff-adlm": "ff",
390
+ "ff-arab": "ff",
391
+ "ha-arab": "ha",
392
+ "hi-latn": "hi",
393
+ "iu-latn": "iu",
394
+ "kk-arab": "kk",
395
+ "ks-deva": "ks",
396
+ "ku-arab": "ku",
397
+ "ky-arab": "ky",
398
+ "ky-latn": "ky",
399
+ "ml-arab": "ml",
400
+ "mn-mong": "mn",
401
+ "mni-mtei": "mni",
402
+ "ms-arab": "ms",
403
+ "pa-arab": "pa",
404
+ "sat-deva": "sat",
405
+ "sd-deva": "sd",
406
+ "sd-khoj": "sd",
407
+ "sd-sind": "sd",
408
+ "shi-latn": "shi",
409
+ "so-arab": "so",
410
+ "sr-latn": "sr",
411
+ "sw-arab": "sw",
412
+ "tg-arab": "tg",
413
+ "ug-cyrl": "ug",
414
+ "uz-arab": "uz",
415
+ "uz-cyrl": "uz",
416
+ "vai-latn": "vai",
417
+ "wo-arab": "wo",
418
+ "yo-arab": "yo",
419
+ "yue-hans": "yue",
420
+ "zh-hant": "zh",
421
+ "zh-hant-hk": "zh",
422
+ "zh-hant-mo": "zh-hant-hk"
423
+ }[sourceLocale];
424
+ }
425
+ /**
426
+ * Replace localeDir, srcPathDirs and srcPathIgnorePatterns with catalogs
427
+ *
428
+ * Released in @lingui/conf 3.0
429
+ * Remove anytime after 4.x
430
+ */
431
+
432
+
433
+ function catalogMigration(config) {
434
+ let {
435
+ localeDir,
436
+ srcPathDirs,
437
+ srcPathIgnorePatterns,
438
+ ...newConfig
439
+ } = config;
440
+
441
+ if (localeDir || srcPathDirs || srcPathIgnorePatterns) {
442
+ // Replace missing values with default ones
443
+ if (localeDir === undefined) localeDir = pathJoinPosix("<rootDir>", "locale", "{locale}", "messages");
444
+ if (srcPathDirs === undefined) srcPathDirs = ["<rootDir>"];
445
+ if (srcPathIgnorePatterns === undefined) srcPathIgnorePatterns = ["**/node_modules/**"];
446
+ let newLocaleDir = localeDir.split(_path.default.sep).join("/");
447
+
448
+ if (newLocaleDir.slice(-1) !== _path.default.sep) {
449
+ newLocaleDir += "/";
450
+ }
451
+
452
+ if (!Array.isArray(newConfig.catalogs)) {
453
+ newConfig.catalogs = [];
454
+ }
455
+
456
+ newConfig.catalogs.push({
457
+ path: pathJoinPosix(newLocaleDir, "{locale}", "messages"),
458
+ include: srcPathDirs,
459
+ exclude: srcPathIgnorePatterns
460
+ });
461
+ }
462
+
463
+ return newConfig;
464
+ }
465
+
466
+ const pipe = (...functions) => args => functions.reduce((arg, fn) => fn(arg), args);
467
+ /** Typescript loader using just typescript API and eval(), instead of using ts-node/register which is slower */
468
+
469
+
470
+ function TypeScriptLoader(filePath) {
471
+ const tsc = require("typescript");
472
+
473
+ const fileContent = _fs.default.readFileSync(filePath, "utf-8");
474
+
475
+ const {
476
+ outputText
477
+ } = tsc.transpileModule(fileContent, {
478
+ compilerOptions: {
479
+ module: tsc.ModuleKind.CommonJS
480
+ }
481
+ });
482
+ const configFileParsed = eval(outputText);
483
+ return configFileParsed;
484
+ }
package/index.d.ts CHANGED
@@ -49,7 +49,10 @@ export declare type LinguiConfig = {
49
49
  orderBy: OrderBy;
50
50
  pseudoLocale: string;
51
51
  rootDir: string;
52
- runtimeConfigModule: [string, string?];
52
+ runtimeConfigModule: [source: string, identifier?: string] | {
53
+ i18n?: [source: string, identifier?: string]
54
+ Trans?: [source: string, identifier?: string]
55
+ };
53
56
  sourceLocale: string;
54
57
  service: CatalogService;
55
58
  };
@@ -77,7 +80,7 @@ export declare const configValidation: {
77
80
  orderBy: OrderBy;
78
81
  pseudoLocale: string;
79
82
  rootDir: string;
80
- runtimeConfigModule: [string, string?];
83
+ runtimeConfigModule: LinguiConfig['runtimeConfigModule'];
81
84
  sourceLocale: string;
82
85
  service: CatalogService;
83
86
  };