@lingui/cli 4.0.0-next.5 → 4.0.0-next.6

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.
Files changed (66) hide show
  1. package/dist/api/catalog/extractFromFiles.d.ts +5 -0
  2. package/dist/api/catalog/extractFromFiles.js +27 -44
  3. package/dist/api/catalog/getCatalogs.d.ts +14 -0
  4. package/dist/api/catalog/getCatalogs.js +70 -78
  5. package/dist/api/catalog/getTranslationsForCatalog.d.ts +14 -0
  6. package/dist/api/catalog/getTranslationsForCatalog.js +36 -32
  7. package/dist/api/catalog/mergeCatalog.d.ts +3 -0
  8. package/dist/api/catalog/mergeCatalog.js +43 -74
  9. package/dist/api/catalog.d.ts +66 -0
  10. package/dist/api/catalog.js +144 -169
  11. package/dist/api/compile.d.ts +19 -0
  12. package/dist/api/compile.js +60 -87
  13. package/dist/api/extractors/babel.d.ts +3 -0
  14. package/dist/api/extractors/babel.js +43 -100
  15. package/dist/api/extractors/index.d.ts +6 -0
  16. package/dist/api/extractors/index.js +17 -23
  17. package/dist/api/extractors/typescript.d.ts +3 -0
  18. package/dist/api/extractors/typescript.js +7 -11
  19. package/dist/api/formats/formatterWrapper.d.ts +10 -0
  20. package/dist/api/formats/formatterWrapper.js +14 -19
  21. package/dist/api/formats/index.d.ts +5 -0
  22. package/dist/api/formats/index.js +15 -63
  23. package/dist/api/help.d.ts +1 -0
  24. package/dist/api/help.js +12 -13
  25. package/dist/api/index.d.ts +4 -0
  26. package/dist/api/index.js +24 -30
  27. package/dist/api/pseudoLocalize.d.ts +1 -0
  28. package/dist/api/pseudoLocalize.js +23 -27
  29. package/dist/api/rethrownError.d.ts +4 -0
  30. package/dist/api/rethrownError.js +4 -9
  31. package/dist/api/stats.d.ts +6 -0
  32. package/dist/api/stats.js +23 -55
  33. package/dist/api/types.d.ts +5 -0
  34. package/dist/api/types.js +1 -3
  35. package/dist/api/utils.d.ts +22 -0
  36. package/dist/api/utils.js +61 -59
  37. package/dist/extract-experimental/buildExternalizeFilter.d.ts +13 -0
  38. package/dist/extract-experimental/buildExternalizeFilter.js +19 -64
  39. package/dist/extract-experimental/bundleSource.d.ts +2 -0
  40. package/dist/extract-experimental/bundleSource.js +25 -74
  41. package/dist/extract-experimental/constants.d.ts +2 -0
  42. package/dist/extract-experimental/constants.js +4 -15
  43. package/dist/extract-experimental/getEntryPoints.d.ts +1 -0
  44. package/dist/extract-experimental/getEntryPoints.js +8 -16
  45. package/dist/extract-experimental/getExperimentalCatalogs.d.ts +3 -0
  46. package/dist/extract-experimental/getExperimentalCatalogs.js +15 -19
  47. package/dist/extract-experimental/resolveCatalogPath.d.ts +2 -0
  48. package/dist/extract-experimental/resolveCatalogPath.js +13 -24
  49. package/dist/extract-experimental/resolveTemplatePath.d.ts +1 -0
  50. package/dist/extract-experimental/resolveTemplatePath.js +11 -14
  51. package/dist/extract-experimental/writeCatalogs.d.ts +21 -0
  52. package/dist/extract-experimental/writeCatalogs.js +23 -36
  53. package/dist/lingui-compile.d.ts +9 -0
  54. package/dist/lingui-compile.js +94 -82
  55. package/dist/lingui-extract-experimental.d.ts +10 -0
  56. package/dist/lingui-extract-experimental.js +58 -67
  57. package/dist/lingui-extract-template.d.ts +6 -0
  58. package/dist/lingui-extract-template.js +29 -35
  59. package/dist/lingui-extract.d.ts +11 -0
  60. package/dist/lingui-extract.js +81 -117
  61. package/dist/lingui.d.ts +2 -0
  62. package/dist/lingui.js +11 -6
  63. package/dist/services/translationIO.d.ts +3 -0
  64. package/dist/services/translationIO.js +92 -89
  65. package/package.json +23 -14
  66. package/dist/tests.js +0 -137
@@ -1,172 +1,129 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- Catalog: ()=>Catalog,
13
- cleanObsolete: ()=>cleanObsolete,
14
- order: ()=>order,
15
- orderByMessage: ()=>orderByMessage
16
- });
17
- const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
18
- const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
19
- const _ramda = /*#__PURE__*/ _interopRequireWildcard(require("ramda"));
20
- const _glob = /*#__PURE__*/ _interopRequireDefault(require("glob"));
21
- const _normalizePath = /*#__PURE__*/ _interopRequireDefault(require("normalize-path"));
22
- const _getTranslationsForCatalog = require("./catalog/getTranslationsForCatalog");
23
- const _mergeCatalog = require("./catalog/mergeCatalog");
24
- const _extractFromFiles = require("./catalog/extractFromFiles");
25
- const _utils = require("./utils");
26
- function _interopRequireDefault(obj) {
27
- return obj && obj.__esModule ? obj : {
28
- default: obj
29
- };
30
- }
31
- function _getRequireWildcardCache(nodeInterop) {
32
- if (typeof WeakMap !== "function") return null;
33
- var cacheBabelInterop = new WeakMap();
34
- var cacheNodeInterop = new WeakMap();
35
- return (_getRequireWildcardCache = function(nodeInterop) {
36
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
37
- })(nodeInterop);
38
- }
39
- function _interopRequireWildcard(obj, nodeInterop) {
40
- if (!nodeInterop && obj && obj.__esModule) {
41
- return obj;
42
- }
43
- if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
44
- return {
45
- default: obj
46
- };
47
- }
48
- var cache = _getRequireWildcardCache(nodeInterop);
49
- if (cache && cache.has(obj)) {
50
- return cache.get(obj);
51
- }
52
- var newObj = {};
53
- var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
54
- for(var key in obj){
55
- if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
56
- var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
57
- if (desc && (desc.get || desc.set)) {
58
- Object.defineProperty(newObj, key, desc);
59
- } else {
60
- newObj[key] = obj[key];
61
- }
62
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
63
7
  }
64
- newObj.default = obj;
65
- if (cache) {
66
- cache.set(obj, newObj);
67
- }
68
- return newObj;
69
- }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.orderByMessage = exports.order = exports.cleanObsolete = exports.Catalog = void 0;
30
+ const fs_1 = __importDefault(require("fs"));
31
+ const path_1 = __importDefault(require("path"));
32
+ const R = __importStar(require("ramda"));
33
+ const glob_1 = __importDefault(require("glob"));
34
+ const normalize_path_1 = __importDefault(require("normalize-path"));
35
+ const getTranslationsForCatalog_1 = require("./catalog/getTranslationsForCatalog");
36
+ const mergeCatalog_1 = require("./catalog/mergeCatalog");
37
+ const extractFromFiles_1 = require("./catalog/extractFromFiles");
38
+ const utils_1 = require("./utils");
70
39
  const LOCALE = "{locale}";
71
40
  const LOCALE_SUFFIX_RE = /\{locale\}.*$/;
72
41
  class Catalog {
73
- config;
74
- name;
75
- path;
76
- include;
77
- exclude;
78
- format;
79
- templateFile;
80
- constructor({ name , path , include , templatePath , format , exclude =[] }, config){
42
+ constructor({ name, path, include, templatePath, format, exclude = [] }, config) {
81
43
  this.config = config;
82
44
  this.name = name;
83
- this.path = (0, _utils.normalizeRelativePath)(path);
84
- this.include = include.map(_utils.normalizeRelativePath);
85
- this.exclude = [
86
- this.localeDir,
87
- ...exclude.map(_utils.normalizeRelativePath)
88
- ];
45
+ this.path = (0, utils_1.normalizeRelativePath)(path);
46
+ this.include = include.map(utils_1.normalizeRelativePath);
47
+ this.exclude = [this.localeDir, ...exclude.map(utils_1.normalizeRelativePath)];
89
48
  this.format = format;
90
- this.templateFile = templatePath || getTemplatePath(this.format.getTemplateExtension(), this.path);
49
+ this.templateFile =
50
+ templatePath ||
51
+ getTemplatePath(this.format.getTemplateExtension(), this.path);
91
52
  }
92
53
  async make(options) {
93
- const nextCatalog = await this.collect({
94
- files: options.files
95
- });
96
- if (!nextCatalog) return false;
54
+ const nextCatalog = await this.collect({ files: options.files });
55
+ if (!nextCatalog)
56
+ return false;
97
57
  const prevCatalogs = await this.readAll();
98
58
  const catalogs = this.merge(prevCatalogs, nextCatalog, {
99
59
  overwrite: options.overwrite,
100
- files: options.files
60
+ files: options.files,
101
61
  });
102
62
  // Map over all locales and post-process each catalog
103
- const cleanAndSort = _ramda.map(_ramda.pipe(// Clean obsolete messages
104
- options.clean ? cleanObsolete : _ramda.identity, // Sort messages
63
+ const cleanAndSort = R.map(R.pipe(
64
+ // Clean obsolete messages
65
+ (options.clean ? exports.cleanObsolete : R.identity),
66
+ // Sort messages
105
67
  order(options.orderBy)));
106
68
  const sortedCatalogs = cleanAndSort(catalogs);
107
- const locales = options.locale ? [
108
- options.locale
109
- ] : this.locales;
110
- await Promise.all(locales.map((locale)=>this.write(locale, sortedCatalogs[locale])));
69
+ const locales = options.locale ? [options.locale] : this.locales;
70
+ await Promise.all(locales.map((locale) => this.write(locale, sortedCatalogs[locale])));
111
71
  return sortedCatalogs;
112
72
  }
113
73
  async makeTemplate(options) {
114
- const catalog = await this.collect({
115
- files: options.files
116
- });
117
- if (!catalog) return false;
74
+ const catalog = await this.collect({ files: options.files });
75
+ if (!catalog)
76
+ return false;
118
77
  const sorted = order(options.orderBy)(catalog);
119
78
  await this.writeTemplate(sorted);
120
79
  return sorted;
121
80
  }
122
81
  /**
123
- * Collect messages from source paths. Return a raw message catalog as JSON.
124
- */ async collect(options = {}) {
82
+ * Collect messages from source paths. Return a raw message catalog as JSON.
83
+ */
84
+ async collect(options = {}) {
125
85
  let paths = this.sourcePaths;
126
86
  if (options.files) {
127
- options.files = options.files.map((p)=>(0, _normalizePath.default)(p, false));
87
+ options.files = options.files.map((p) => (0, normalize_path_1.default)(p, false));
128
88
  const regex = new RegExp(options.files.join("|"), "i");
129
- paths = paths.filter((path)=>regex.test(path));
89
+ paths = paths.filter((path) => regex.test(path));
130
90
  }
131
- return await (0, _extractFromFiles.extractFromFiles)(paths, this.config);
91
+ return await (0, extractFromFiles_1.extractFromFiles)(paths, this.config);
132
92
  }
133
93
  /*
134
- *
135
- * prevCatalogs - map of message catalogs in all available languages with translations
136
- * nextCatalog - language-agnostic catalog with collected messages
137
- *
138
- * Note: if a catalog in prevCatalogs is null it means the language is available, but
139
- * no previous catalog was generated (usually first run).
140
- *
141
- * Orthogonal use-cases
142
- * --------------------
143
- *
144
- * Message IDs:
145
- * - auto-generated IDs: message is used as a key, `defaults` is not set
146
- * - custom IDs: message is used as `defaults`, custom ID as a key
147
- *
148
- * Source locale (defined by `sourceLocale` in config):
149
- * - catalog for `sourceLocale`: initially, `translation` is prefilled with `defaults`
150
- * (for custom IDs) or `key` (for auto-generated IDs)
151
- * - all other languages: translation is kept empty
152
- */ merge(prevCatalogs, nextCatalog, options) {
153
- return _ramda.mapObjIndexed((prevCatalog, locale)=>{
154
- return (0, _mergeCatalog.mergeCatalog)(prevCatalog, nextCatalog, this.config.sourceLocale === locale, options);
94
+ *
95
+ * prevCatalogs - map of message catalogs in all available languages with translations
96
+ * nextCatalog - language-agnostic catalog with collected messages
97
+ *
98
+ * Note: if a catalog in prevCatalogs is null it means the language is available, but
99
+ * no previous catalog was generated (usually first run).
100
+ *
101
+ * Orthogonal use-cases
102
+ * --------------------
103
+ *
104
+ * Message IDs:
105
+ * - auto-generated IDs: message is used as a key, `defaults` is not set
106
+ * - custom IDs: message is used as `defaults`, custom ID as a key
107
+ *
108
+ * Source locale (defined by `sourceLocale` in config):
109
+ * - catalog for `sourceLocale`: initially, `translation` is prefilled with `defaults`
110
+ * (for custom IDs) or `key` (for auto-generated IDs)
111
+ * - all other languages: translation is kept empty
112
+ */
113
+ merge(prevCatalogs, nextCatalog, options) {
114
+ return R.mapObjIndexed((prevCatalog, locale) => {
115
+ return (0, mergeCatalog_1.mergeCatalog)(prevCatalog, nextCatalog, this.config.sourceLocale === locale, options);
155
116
  }, prevCatalogs);
156
117
  }
157
118
  async getTranslations(locale, options) {
158
- return await (0, _getTranslationsForCatalog.getTranslationsForCatalog)(this, locale, options);
119
+ return await (0, getTranslationsForCatalog_1.getTranslationsForCatalog)(this, locale, options);
159
120
  }
160
121
  async write(locale, messages) {
161
- const filename = (0, _utils.replacePlaceholders)(this.path, {
162
- locale
163
- }) + this.format.getCatalogExtension();
164
- const created = !_fs.default.existsSync(filename);
122
+ const filename = (0, utils_1.replacePlaceholders)(this.path, { locale }) +
123
+ this.format.getCatalogExtension();
124
+ const created = !fs_1.default.existsSync(filename);
165
125
  await this.format.write(filename, messages, locale);
166
- return [
167
- created,
168
- filename
169
- ];
126
+ return [created, filename];
170
127
  }
171
128
  async writeTemplate(messages) {
172
129
  const filename = this.templateFile;
@@ -176,29 +133,28 @@ class Catalog {
176
133
  let ext;
177
134
  if (namespace === "es") {
178
135
  ext = "mjs";
179
- } else if (namespace === "ts") {
136
+ }
137
+ else if (namespace === "ts") {
180
138
  ext = "ts";
181
- } else {
139
+ }
140
+ else {
182
141
  ext = "js";
183
142
  }
184
- const filename = `${(0, _utils.replacePlaceholders)(this.path, {
185
- locale
186
- })}.${ext}`;
187
- await (0, _utils.writeFile)(filename, compiledCatalog);
143
+ const filename = `${(0, utils_1.replacePlaceholders)(this.path, { locale })}.${ext}`;
144
+ await (0, utils_1.writeFile)(filename, compiledCatalog);
188
145
  return filename;
189
146
  }
190
147
  async read(locale) {
191
- const filename = (0, _utils.replacePlaceholders)(this.path, {
192
- locale
193
- }) + this.format.getCatalogExtension();
148
+ const filename = (0, utils_1.replacePlaceholders)(this.path, { locale }) +
149
+ this.format.getCatalogExtension();
194
150
  return await this.format.read(filename, locale);
195
151
  }
196
152
  async readAll() {
197
153
  const res = {};
198
- await Promise.all(this.locales.map(async (locale)=>res[locale] = await this.read(locale)));
154
+ await Promise.all(this.locales.map(async (locale) => (res[locale] = await this.read(locale))));
199
155
  // statement above will save locales in object in undetermined order
200
156
  // resort here to have keys order the same as in locales definition
201
- return this.locales.reduce((acc, locale)=>{
157
+ return this.locales.reduce((acc, locale) => {
202
158
  acc[locale] = res[locale];
203
159
  return acc;
204
160
  }, {});
@@ -208,18 +164,18 @@ class Catalog {
208
164
  return await this.format.read(filename, undefined);
209
165
  }
210
166
  get sourcePaths() {
211
- const includeGlobs = this.include.map((includePath)=>{
212
- const isDir = (0, _utils.isDirectory)(includePath);
167
+ const includeGlobs = this.include.map((includePath) => {
168
+ const isDir = (0, utils_1.isDirectory)(includePath);
213
169
  /**
214
- * glob library results from absolute patterns such as /foo/* are mounted onto the root setting using path.join.
215
- * On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
216
- */ return isDir ? (0, _normalizePath.default)(_path.default.resolve(process.cwd(), includePath === "/" ? "" : includePath, "**/*.*")) : includePath;
170
+ * glob library results from absolute patterns such as /foo/* are mounted onto the root setting using path.join.
171
+ * On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
172
+ */
173
+ return isDir
174
+ ? (0, normalize_path_1.default)(path_1.default.resolve(process.cwd(), includePath === "/" ? "" : includePath, "**/*.*"))
175
+ : includePath;
217
176
  });
218
177
  const patterns = includeGlobs.length > 1 ? `{${includeGlobs.join(",")}}` : includeGlobs[0];
219
- return _glob.default.sync(patterns, {
220
- ignore: this.exclude,
221
- mark: true
222
- });
178
+ return glob_1.default.sync(patterns, { ignore: this.exclude, mark: true });
223
179
  }
224
180
  get localeDir() {
225
181
  const localePatternIndex = this.path.indexOf(LOCALE);
@@ -232,55 +188,74 @@ class Catalog {
232
188
  return this.config.locales;
233
189
  }
234
190
  }
191
+ exports.Catalog = Catalog;
235
192
  function getTemplatePath(ext, path) {
236
193
  return path.replace(LOCALE_SUFFIX_RE, "messages" + ext);
237
194
  }
238
- const cleanObsolete = _ramda.filter((message)=>!message.obsolete);
195
+ exports.cleanObsolete = R.filter((message) => !message.obsolete);
239
196
  function order(by) {
240
- return ({
197
+ return {
241
198
  messageId: orderByMessageId,
242
199
  message: orderByMessage,
243
- origin: orderByOrigin
244
- })[by];
200
+ origin: orderByOrigin,
201
+ }[by];
245
202
  }
203
+ exports.order = order;
246
204
  /**
247
205
  * Object keys are in the same order as they were created
248
206
  * https://stackoverflow.com/a/31102605/1535540
249
- */ function orderByMessageId(messages) {
250
- return Object.keys(messages).sort().reduce((acc, key)=>{
207
+ */
208
+ function orderByMessageId(messages) {
209
+ return Object.keys(messages)
210
+ .sort()
211
+ .reduce((acc, key) => {
212
+ ;
251
213
  acc[key] = messages[key];
252
214
  return acc;
253
215
  }, {});
254
216
  }
255
217
  function orderByOrigin(messages) {
256
218
  function getFirstOrigin(messageKey) {
257
- const sortedOrigins = messages[messageKey].origin.sort((a, b)=>{
258
- if (a[0] < b[0]) return -1;
259
- if (a[0] > b[0]) return 1;
219
+ const sortedOrigins = messages[messageKey].origin.sort((a, b) => {
220
+ if (a[0] < b[0])
221
+ return -1;
222
+ if (a[0] > b[0])
223
+ return 1;
260
224
  return 0;
261
225
  });
262
226
  return sortedOrigins[0];
263
227
  }
264
- return Object.keys(messages).sort((a, b)=>{
228
+ return Object.keys(messages)
229
+ .sort((a, b) => {
265
230
  const [aFile, aLineNumber] = getFirstOrigin(a);
266
231
  const [bFile, bLineNumber] = getFirstOrigin(b);
267
- if (aFile < bFile) return -1;
268
- if (aFile > bFile) return 1;
269
- if (aLineNumber < bLineNumber) return -1;
270
- if (aLineNumber > bLineNumber) return 1;
232
+ if (aFile < bFile)
233
+ return -1;
234
+ if (aFile > bFile)
235
+ return 1;
236
+ if (aLineNumber < bLineNumber)
237
+ return -1;
238
+ if (aLineNumber > bLineNumber)
239
+ return 1;
271
240
  return 0;
272
- }).reduce((acc, key)=>{
241
+ })
242
+ .reduce((acc, key) => {
243
+ ;
273
244
  acc[key] = messages[key];
274
245
  return acc;
275
246
  }, {});
276
247
  }
277
248
  function orderByMessage(messages) {
278
- return Object.keys(messages).sort((a, b)=>{
249
+ return Object.keys(messages)
250
+ .sort((a, b) => {
279
251
  const aMsg = messages[a].message || "";
280
252
  const bMsg = messages[b].message || "";
281
253
  return aMsg.localeCompare(bMsg);
282
- }).reduce((acc, key)=>{
254
+ })
255
+ .reduce((acc, key) => {
256
+ ;
283
257
  acc[key] = messages[key];
284
258
  return acc;
285
259
  }, {});
286
260
  }
261
+ exports.orderByMessage = orderByMessage;
@@ -0,0 +1,19 @@
1
+ import { GeneratorOptions } from "@babel/generator";
2
+ import { CompiledMessage } from "@lingui/message-utils/compileMessage";
3
+ export type CompiledCatalogNamespace = "cjs" | "es" | "ts" | "json" | string;
4
+ type CompiledCatalogType = {
5
+ [msgId: string]: string;
6
+ };
7
+ export type CreateCompileCatalogOptions = {
8
+ strict?: boolean;
9
+ namespace?: CompiledCatalogNamespace;
10
+ pseudoLocale?: string;
11
+ compilerBabelOptions?: GeneratorOptions;
12
+ };
13
+ export declare function createCompiledCatalog(locale: string, messages: CompiledCatalogType, options: CreateCompileCatalogOptions): string;
14
+ /**
15
+ * Compile string message into AST tree. Message format is parsed/compiled into
16
+ * JS arrays, which are handled in client.
17
+ */
18
+ export declare function compile(message: string, shouldPseudolocalize?: boolean): CompiledMessage;
19
+ export {};
@@ -1,115 +1,88 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- createCompiledCatalog: ()=>createCompiledCatalog,
13
- compile: ()=>compile
14
- });
15
- const _types = /*#__PURE__*/ _interopRequireWildcard(require("@babel/types"));
16
- const _generator = /*#__PURE__*/ _interopRequireDefault(require("@babel/generator"));
17
- const _compileMessage = require("@lingui/message-utils/compileMessage");
18
- const _pseudoLocalize = /*#__PURE__*/ _interopRequireDefault(require("./pseudoLocalize"));
19
- function _interopRequireDefault(obj) {
20
- return obj && obj.__esModule ? obj : {
21
- default: obj
22
- };
23
- }
24
- function _getRequireWildcardCache(nodeInterop) {
25
- if (typeof WeakMap !== "function") return null;
26
- var cacheBabelInterop = new WeakMap();
27
- var cacheNodeInterop = new WeakMap();
28
- return (_getRequireWildcardCache = function(nodeInterop) {
29
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
30
- })(nodeInterop);
31
- }
32
- function _interopRequireWildcard(obj, nodeInterop) {
33
- if (!nodeInterop && obj && obj.__esModule) {
34
- return obj;
35
- }
36
- if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
37
- return {
38
- default: obj
39
- };
40
- }
41
- var cache = _getRequireWildcardCache(nodeInterop);
42
- if (cache && cache.has(obj)) {
43
- return cache.get(obj);
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
44
7
  }
45
- var newObj = {};
46
- var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
47
- for(var key in obj){
48
- if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
49
- var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
50
- if (desc && (desc.get || desc.set)) {
51
- Object.defineProperty(newObj, key, desc);
52
- } else {
53
- newObj[key] = obj[key];
54
- }
55
- }
56
- }
57
- newObj.default = obj;
58
- if (cache) {
59
- cache.set(obj, newObj);
60
- }
61
- return newObj;
62
- }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.compile = exports.createCompiledCatalog = void 0;
30
+ const t = __importStar(require("@babel/types"));
31
+ const generator_1 = __importDefault(require("@babel/generator"));
32
+ const compileMessage_1 = require("@lingui/message-utils/compileMessage");
33
+ const pseudoLocalize_1 = __importDefault(require("./pseudoLocalize"));
63
34
  function createCompiledCatalog(locale, messages, options) {
64
- const { strict =false , namespace ="cjs" , pseudoLocale , compilerBabelOptions ={} } = options;
35
+ const { strict = false, namespace = "cjs", pseudoLocale, compilerBabelOptions = {}, } = options;
65
36
  const shouldPseudolocalize = locale === pseudoLocale;
66
- const compiledMessages = Object.keys(messages).reduce((obj, key)=>{
37
+ const compiledMessages = Object.keys(messages).reduce((obj, key) => {
67
38
  // Don't use `key` as a fallback translation in strict mode.
68
- const translation = messages[key] || (!strict ? key : "");
39
+ const translation = (messages[key] || (!strict ? key : ""));
69
40
  obj[key] = compile(translation, shouldPseudolocalize);
70
41
  return obj;
71
42
  }, {});
72
43
  if (namespace === "json") {
73
- return JSON.stringify({
74
- messages: compiledMessages
75
- });
44
+ return JSON.stringify({ messages: compiledMessages });
76
45
  }
77
- const ast = buildExportStatement(//build JSON.parse(<compiledMessages>) statement
78
- _types.callExpression(_types.memberExpression(_types.identifier("JSON"), _types.identifier("parse")), [
79
- _types.stringLiteral(JSON.stringify(compiledMessages))
80
- ]), namespace);
81
- const code = (0, _generator.default)(ast, {
82
- minified: true,
83
- jsescOption: {
84
- minimal: true
85
- },
86
- ...compilerBabelOptions
87
- }).code;
46
+ const ast = buildExportStatement(
47
+ //build JSON.parse(<compiledMessages>) statement
48
+ t.callExpression(t.memberExpression(t.identifier("JSON"), t.identifier("parse")), [t.stringLiteral(JSON.stringify(compiledMessages))]), namespace);
49
+ const code = (0, generator_1.default)(ast, Object.assign({ minified: true, jsescOption: {
50
+ minimal: true,
51
+ } }, compilerBabelOptions)).code;
88
52
  return "/*eslint-disable*/" + code;
89
53
  }
54
+ exports.createCompiledCatalog = createCompiledCatalog;
90
55
  function buildExportStatement(expression, namespace) {
91
56
  if (namespace === "es" || namespace === "ts") {
92
57
  // export const messages = { message: "Translation" }
93
- return _types.exportNamedDeclaration(_types.variableDeclaration("const", [
94
- _types.variableDeclarator(_types.identifier("messages"), expression)
58
+ return t.exportNamedDeclaration(t.variableDeclaration("const", [
59
+ t.variableDeclarator(t.identifier("messages"), expression),
95
60
  ]));
96
- } else {
61
+ }
62
+ else {
97
63
  let exportExpression = null;
98
64
  const matches = namespace.match(/^(window|global)\.([^.\s]+)$/);
99
65
  if (namespace === "cjs") {
100
66
  // module.exports.messages = { message: "Translation" }
101
- exportExpression = _types.memberExpression(_types.identifier("module"), _types.identifier("exports"));
102
- } else if (matches) {
67
+ exportExpression = t.memberExpression(t.identifier("module"), t.identifier("exports"));
68
+ }
69
+ else if (matches) {
103
70
  // window.i18nMessages = { messages: { message: "Translation" }}
104
- exportExpression = _types.memberExpression(_types.identifier(matches[1]), _types.identifier(matches[2]));
105
- } else {
71
+ exportExpression = t.memberExpression(t.identifier(matches[1]), t.identifier(matches[2]));
72
+ }
73
+ else {
106
74
  throw new Error(`Invalid namespace param: "${namespace}"`);
107
75
  }
108
- return _types.expressionStatement(_types.assignmentExpression("=", exportExpression, _types.objectExpression([
109
- _types.objectProperty(_types.identifier("messages"), expression)
76
+ return t.expressionStatement(t.assignmentExpression("=", exportExpression, t.objectExpression([
77
+ t.objectProperty(t.identifier("messages"), expression),
110
78
  ])));
111
79
  }
112
80
  }
81
+ /**
82
+ * Compile string message into AST tree. Message format is parsed/compiled into
83
+ * JS arrays, which are handled in client.
84
+ */
113
85
  function compile(message, shouldPseudolocalize = false) {
114
- return (0, _compileMessage.compileMessage)(message, (value)=>shouldPseudolocalize ? (0, _pseudoLocalize.default)(value) : value);
86
+ return (0, compileMessage_1.compileMessage)(message, (value) => shouldPseudolocalize ? (0, pseudoLocalize_1.default)(value) : value);
115
87
  }
88
+ exports.compile = compile;
@@ -0,0 +1,3 @@
1
+ import type { ExtractorType } from "@lingui/conf";
2
+ declare const extractor: ExtractorType;
3
+ export default extractor;