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

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,420 @@
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.ts`,
393
+ `${moduleName}.config.mjs`,
394
+ "package.json",
395
+ `.${moduleName}rc`,
396
+ `.${moduleName}rc.json`,
397
+ `.${moduleName}rc.yaml`,
398
+ `.${moduleName}rc.yml`,
399
+ `.${moduleName}rc.ts`,
400
+ `.${moduleName}rc.js`
401
+ ],
402
+ loaders: {
403
+ ".ts": JitiLoader(),
404
+ ".mjs": JitiLoader()
405
+ }
406
+ });
407
+ configPath = configPath || process.env.LINGUI_CONFIG;
408
+ const result = configExists(configPath) ? configExplorer.load(configPath) : configExplorer.search(defaultRootDir);
409
+ const userConfig = result ? result.config : {};
410
+ return makeConfig(
411
+ {
412
+ rootDir: result ? path.dirname(result.filepath) : defaultRootDir,
413
+ ...userConfig
414
+ },
415
+ { skipValidation }
416
+ );
417
+ }
418
+
419
+ exports.getConfig = getConfig;
420
+ 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,8 +21,8 @@ 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 MessageOrigin = [filename: string, line?: number];
25
+ type ExtractedMessageType = {
13
26
  message?: string;
14
27
  origin?: MessageOrigin[];
15
28
  comments?: string[];
@@ -18,20 +31,20 @@ export type ExtractedMessageType = {
18
31
  flags?: string[];
19
32
  context?: string;
20
33
  };
21
- export type MessageType = ExtractedMessageType & {
34
+ type MessageType = ExtractedMessageType & {
22
35
  translation: string;
23
36
  };
24
- export type ExtractedCatalogType = {
37
+ type ExtractedCatalogType = {
25
38
  [msgId: string]: ExtractedMessageType;
26
39
  };
27
- export type CatalogType = {
40
+ type CatalogType = {
28
41
  [msgId: string]: MessageType;
29
42
  };
30
- export type ExtractorType = {
43
+ type ExtractorType = {
31
44
  match(filename: string): boolean;
32
45
  extract(filename: string, code: string, onMessageExtracted: (msg: ExtractedMessage) => void, ctx?: ExtractorCtx): Promise<void> | void;
33
46
  };
34
- export type CatalogFormatter = {
47
+ type CatalogFormatter = {
35
48
  catalogExtension: string;
36
49
  /**
37
50
  * Set extension used when extract to template
@@ -40,28 +53,30 @@ export type CatalogFormatter = {
40
53
  templateExtension?: string;
41
54
  parse(content: string, ctx: {
42
55
  locale: string | null;
56
+ sourceLocale: string;
43
57
  filename: string;
44
58
  }): Promise<CatalogType> | CatalogType;
45
59
  serialize(catalog: CatalogType, ctx: {
46
60
  locale: string | null;
61
+ sourceLocale: string;
47
62
  filename: string;
48
63
  existing: string | null;
49
64
  }): Promise<string> | string;
50
65
  };
51
- export type ExtractedMessage = {
66
+ type ExtractedMessage = {
52
67
  id: string;
53
68
  message?: string;
54
69
  context?: string;
55
70
  origin?: [filename: string, line: number, column?: number];
56
71
  comment?: string;
57
72
  };
58
- export type CatalogFormatOptions = {
73
+ type CatalogFormatOptions = {
59
74
  origins?: boolean;
60
75
  lineNumbers?: boolean;
61
76
  disableSelectWarning?: boolean;
62
77
  };
63
- export type OrderBy = "messageId" | "message" | "origin";
64
- export type CatalogConfig = {
78
+ type OrderBy = "messageId" | "message" | "origin";
79
+ type CatalogConfig = {
65
80
  name?: string;
66
81
  path: string;
67
82
  include: string[];
@@ -71,13 +86,13 @@ type LocaleObject = {
71
86
  [locale: string]: string[] | string;
72
87
  default?: string;
73
88
  };
74
- export type FallbackLocales = LocaleObject;
89
+ type FallbackLocales = LocaleObject;
75
90
  type ModuleSource = [string, string?];
76
91
  type CatalogService = {
77
92
  name: string;
78
93
  apiKey: string;
79
94
  };
80
- export type ExperimentalExtractorOptions = {
95
+ type ExperimentalExtractorOptions = {
81
96
  /**
82
97
  * Entries to start extracting from.
83
98
  * Each separate resolved entry would create a separate catalog.
@@ -132,7 +147,7 @@ export type ExperimentalExtractorOptions = {
132
147
  output: string;
133
148
  resolveEsbuildOptions?: (options: any) => any;
134
149
  };
135
- export type LinguiConfig = {
150
+ type LinguiConfig = {
136
151
  catalogs?: CatalogConfig[];
137
152
  compileNamespace?: "es" | "ts" | "cjs" | string;
138
153
  extractorParserOptions?: {
@@ -148,7 +163,7 @@ export type LinguiConfig = {
148
163
  */
149
164
  flow?: boolean;
150
165
  };
151
- compilerBabelOptions?: GeneratorOptions;
166
+ compilerBabelOptions?: any;
152
167
  fallbackLocales?: FallbackLocales | false;
153
168
  extractors?: (string | ExtractorType)[];
154
169
  prevFormat?: CatalogFormat;
@@ -169,7 +184,7 @@ export type LinguiConfig = {
169
184
  extractor?: ExperimentalExtractorOptions;
170
185
  };
171
186
  };
172
- export type LinguiConfigNormalized = Omit<LinguiConfig, "runtimeConfigModule"> & {
187
+ type LinguiConfigNormalized = Omit<LinguiConfig, "runtimeConfigModule"> & {
173
188
  fallbackLocales?: FallbackLocales;
174
189
  runtimeConfigModule: {
175
190
  i18nImportModule: string;
@@ -178,4 +193,15 @@ export type LinguiConfigNormalized = Omit<LinguiConfig, "runtimeConfigModule"> &
178
193
  TransImportName: string;
179
194
  };
180
195
  };
181
- export {};
196
+
197
+ declare function makeConfig(userConfig: Partial<LinguiConfig>, opts?: {
198
+ skipValidation?: boolean;
199
+ }): LinguiConfigNormalized;
200
+
201
+ declare function getConfig({ cwd, configPath, skipValidation, }?: {
202
+ cwd?: string;
203
+ configPath?: string;
204
+ skipValidation?: boolean;
205
+ }): LinguiConfigNormalized;
206
+
207
+ export { CatalogConfig, CatalogFormat, CatalogFormatOptions, CatalogFormatter, CatalogType, ExperimentalExtractorOptions, ExtractedCatalogType, ExtractedMessage, ExtractedMessageType, ExtractorCtx, ExtractorType, FallbackLocales, LinguiConfig, LinguiConfigNormalized, MessageOrigin, MessageType, OrderBy, getConfig, makeConfig };