@let-value/translate-extract 1.0.12 → 1.0.14

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/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { run } from "../run-Dl-tr2kf.js";
1
+ import { run } from "../run-C_k3ujlk.js";
2
2
  import path, { basename, dirname, extname, join, relative, resolve } from "node:path";
3
3
  import fs, { readFile } from "node:fs/promises";
4
4
  import * as gettextParser from "gettext-parser";
@@ -10,43 +10,52 @@ import { ResolverFactory } from "oxc-resolver";
10
10
  import { getFormula, getNPlurals } from "plural-forms";
11
11
 
12
12
  //#region src/plugins/cleanup/cleanup.ts
13
+ const namespace$2 = "cleanup";
13
14
  function cleanup() {
14
15
  return {
15
16
  name: "cleanup",
16
17
  setup(build) {
17
18
  build.context.logger?.debug("cleanup plugin initialized");
18
- const processedDirs = /* @__PURE__ */ new Set();
19
+ const processed = /* @__PURE__ */ new Set();
19
20
  const generated = /* @__PURE__ */ new Set();
21
+ const dirs = /* @__PURE__ */ new Set();
22
+ let dispatched = false;
20
23
  build.onResolve({
21
- namespace: "cleanup",
24
+ namespace: namespace$2,
22
25
  filter: /.*/
23
- }, (args) => {
24
- generated.add(args.path);
25
- return args;
26
+ }, ({ path: path$1 }) => {
27
+ generated.add(path$1);
28
+ dirs.add(dirname(path$1));
29
+ Promise.all([build.defer("source"), build.defer("translate")]).then(() => {
30
+ if (dispatched) return;
31
+ dispatched = true;
32
+ for (const path$2 of dirs.values()) build.process({
33
+ entrypoint: path$2,
34
+ path: path$2,
35
+ namespace: namespace$2,
36
+ data: void 0
37
+ });
38
+ });
26
39
  });
27
40
  build.onProcess({
28
- namespace: "cleanup",
41
+ namespace: namespace$2,
29
42
  filter: /.*/
30
43
  }, async ({ path: path$1 }) => {
31
- await build.defer("translate");
32
- const dir = dirname(path$1);
33
- if (processedDirs.has(dir)) return void 0;
34
- processedDirs.add(dir);
35
- const files = await fs.readdir(dir).catch(() => []);
44
+ if (processed.has(path$1)) return;
45
+ processed.add(path$1);
46
+ const files = await fs.readdir(path$1).catch(() => []);
36
47
  for (const f of files.filter((p) => p.endsWith(".po"))) {
37
- const full = join(dir, f);
48
+ const full = join(path$1, f);
38
49
  if (generated.has(full)) continue;
39
50
  const contents = await fs.readFile(full).catch(() => void 0);
40
51
  if (!contents) continue;
41
52
  const parsed = gettextParser.po.parse(contents);
42
- const hasTranslations = Object.entries(parsed.translations || {}).some(([ctx, msgs]) => Object.keys(msgs).some((id) => !(ctx === "" && id === "")));
43
- if (hasTranslations) build.context.logger?.warn({ path: full }, "stray translation file");
53
+ if (Object.entries(parsed.translations || {}).some(([ctx, msgs]) => Object.keys(msgs).some((id) => !(ctx === "" && id === "")))) build.context.logger?.warn({ path: full }, "stray translation file");
44
54
  else {
45
55
  await fs.unlink(full);
46
56
  build.context.logger?.info({ path: full }, "removed empty translation file");
47
57
  }
48
58
  }
49
- return void 0;
50
59
  });
51
60
  }
52
61
  };
@@ -56,8 +65,7 @@ function cleanup() {
56
65
  //#region src/plugins/core/queries/comment.ts
57
66
  function getReference(node, { path: path$1 }) {
58
67
  const line = node.startPosition.row + 1;
59
- const rel = relative(process.cwd(), path$1).replace(/\\+/g, "/");
60
- return `${rel}:${line}`;
68
+ return `${relative(process.cwd(), path$1).replace(/\\+/g, "/")}:${line}`;
61
69
  }
62
70
  function getComment(node) {
63
71
  const text = node.text;
@@ -109,8 +117,7 @@ const importQuery = {
109
117
  ]
110
118
  `,
111
119
  extract(match) {
112
- const node = match.captures.find((c) => c.name === "import")?.node;
113
- return node?.text;
120
+ return (match.captures.find((c) => c.name === "import")?.node)?.text;
114
121
  }
115
122
  };
116
123
 
@@ -152,7 +159,7 @@ const notInPlural = (query) => ({
152
159
  "ngettext",
153
160
  "pgettext",
154
161
  "npgettext"
155
- ].includes(fn.childForFieldName("property")?.text ?? "")) return void 0;
162
+ ].includes(fn.childForFieldName("property")?.text ?? "")) return;
156
163
  }
157
164
  }
158
165
  return result;
@@ -180,7 +187,7 @@ const messageArg = `[
180
187
  const messageArgs = `[ (arguments ${messageArg}) (template_string) @tpl ]`;
181
188
  const extractMessage = (name) => (match) => {
182
189
  const node = match.captures.find((c) => c.name === "call")?.node;
183
- if (!node) return void 0;
190
+ if (!node) return;
184
191
  const msgid = match.captures.find((c) => c.name === "msgid")?.node.text;
185
192
  if (msgid) return {
186
193
  node,
@@ -211,13 +218,12 @@ const extractMessage = (name) => (match) => {
211
218
  const id = match.captures.find((c) => c.name === "id")?.node.text;
212
219
  const message = match.captures.find((c) => c.name === "message")?.node.text;
213
220
  const msgId = id ?? message;
214
- if (!msgId) return void 0;
215
- const msgstr = message ?? id ?? "";
221
+ if (!msgId) return;
216
222
  return {
217
223
  node,
218
224
  translation: {
219
225
  id: msgId,
220
- message: [msgstr]
226
+ message: [message ?? id ?? ""]
221
227
  }
222
228
  };
223
229
  };
@@ -235,8 +241,8 @@ const messageInvalidQuery = notInPlural({
235
241
  extract(match) {
236
242
  const call = match.captures.find((c) => c.name === "call")?.node;
237
243
  const node = match.captures.find((c) => c.name === "arg")?.node;
238
- if (!call || !node) return void 0;
239
- if (allowed$1.has(node.type)) return void 0;
244
+ if (!call || !node) return;
245
+ if (allowed$1.has(node.type)) return;
240
246
  return {
241
247
  node,
242
248
  error: "message() argument must be a string literal, object literal, or template literal"
@@ -249,7 +255,7 @@ const messageInvalidQuery = notInPlural({
249
255
  const extractPluralForms = (name) => (match) => {
250
256
  const call = match.captures.find((c) => c.name === "call")?.node;
251
257
  const n = match.captures.find((c) => c.name === "n")?.node;
252
- if (!call || !n || n.nextNamedSibling) return void 0;
258
+ if (!call || !n || n.nextNamedSibling) return;
253
259
  const msgctxt = match.captures.find((c) => c.name === "msgctxt")?.node?.text;
254
260
  const msgNodes = match.captures.filter((c) => c.name === "msg").map((c) => c.node);
255
261
  const ids = [];
@@ -279,7 +285,7 @@ const extractPluralForms = (name) => (match) => {
279
285
  strs.push(result.translation.message[0] ?? "");
280
286
  }
281
287
  }
282
- if (ids.length === 0) return void 0;
288
+ if (ids.length === 0) return;
283
289
  const translation = {
284
290
  id: ids[0],
285
291
  plural: ids[1],
@@ -340,12 +346,12 @@ const contextInvalidQuery = withComment({
340
346
  pattern: ctxCall,
341
347
  extract(match) {
342
348
  const call = match.captures.find((c) => c.name === "ctx")?.node;
343
- if (!call) return void 0;
349
+ if (!call) return;
344
350
  const parent = call.parent;
345
351
  if (parent && parent.type === "member_expression" && parent.childForFieldName("object")?.id === call.id) {
346
352
  const property = parent.childForFieldName("property")?.text;
347
353
  const grandparent = parent.parent;
348
- if (grandparent && grandparent.type === "call_expression" && grandparent.childForFieldName("function")?.id === parent.id && (property === "message" || property === "plural")) return void 0;
354
+ if (grandparent && grandparent.type === "call_expression" && grandparent.childForFieldName("function")?.id === parent.id && (property === "message" || property === "plural")) return;
349
355
  }
350
356
  return {
351
357
  node: call,
@@ -372,8 +378,8 @@ const gettextInvalidQuery = {
372
378
  extract(match) {
373
379
  const call = match.captures.find((c) => c.name === "call")?.node;
374
380
  const node = match.captures.find((c) => c.name === "arg")?.node;
375
- if (!call || !node) return void 0;
376
- if (allowed.has(node.type)) return void 0;
381
+ if (!call || !node) return;
382
+ if (allowed.has(node.type)) return;
377
383
  return {
378
384
  node,
379
385
  error: "gettext() argument must be a string literal, object literal, or template literal"
@@ -541,7 +547,7 @@ function findTsconfig(dir) {
541
547
  const config = path.join(current, "tsconfig.json");
542
548
  if (fs$1.existsSync(config)) return config;
543
549
  const parent = path.dirname(current);
544
- if (parent === current) return void 0;
550
+ if (parent === current) return;
545
551
  current = parent;
546
552
  }
547
553
  }
@@ -634,7 +640,6 @@ function core() {
634
640
  namespace: "translate",
635
641
  data: translations
636
642
  });
637
- return void 0;
638
643
  });
639
644
  }
640
645
  };
@@ -780,7 +785,7 @@ function po() {
780
785
  filter: /.*/,
781
786
  namespace
782
787
  }, async ({ entrypoint, path: path$1, data }) => {
783
- if (!data || !Array.isArray(data)) return void 0;
788
+ if (!data || !Array.isArray(data)) return;
784
789
  for (const locale of build.context.config.locales) {
785
790
  const destination = build.context.config.destination({
786
791
  entrypoint,
@@ -802,7 +807,6 @@ function po() {
802
807
  namespace
803
808
  });
804
809
  });
805
- return void 0;
806
810
  });
807
811
  build.onLoad({
808
812
  filter: /.*\.po$/,
@@ -823,7 +827,7 @@ function po() {
823
827
  const collected = collections.get(path$1);
824
828
  if (!collected) {
825
829
  build.context.logger?.warn({ path: path$1 }, "no translations collected for this path");
826
- return void 0;
830
+ return;
827
831
  }
828
832
  const { locale, translations } = collected;
829
833
  const record = collect(translations, locale);
@@ -880,8 +884,7 @@ function resolvePlugins(user) {
880
884
  function defineConfig(config) {
881
885
  const defaultLocale = config.defaultLocale ?? "en";
882
886
  const plugins = resolvePlugins(config.plugins);
883
- const raw = Array.isArray(config.entrypoints) ? config.entrypoints : [config.entrypoints];
884
- const entrypoints = raw.map(resolveEntrypoint);
887
+ const entrypoints = (Array.isArray(config.entrypoints) ? config.entrypoints : [config.entrypoints]).map(resolveEntrypoint);
885
888
  return {
886
889
  plugins,
887
890
  entrypoints,
@@ -936,8 +939,7 @@ function buildTemplate(node) {
936
939
  raw: false
937
940
  });
938
941
  else if (expr.type === "template_string") {
939
- const hasSubstitutions = expr.children.some((c) => c.type === "template_substitution");
940
- if (hasSubstitutions) return {
942
+ if (expr.children.some((c) => c.type === "template_substitution")) return {
941
943
  text: "",
942
944
  error: "JSX expressions with template substitutions are not supported"
943
945
  };
@@ -959,8 +961,7 @@ function buildTemplate(node) {
959
961
  raw: false
960
962
  });
961
963
  segmentStart = child.endIndex;
962
- } else if (child.type === "jsx_text" || child.type === "html_character_reference" || child.isError) continue;
963
- else return {
964
+ } else if (child.type === "jsx_text" || child.type === "html_character_reference" || child.isError) {} else return {
964
965
  text: "",
965
966
  error: "Unsupported JSX child"
966
967
  };
@@ -1008,13 +1009,11 @@ function buildAttrValue(node) {
1008
1009
  if (expr.type === "identifier") return { text: `\${${expr.text}}` };
1009
1010
  else if (expr.type === "string") return { text: expr.text.slice(1, -1) };
1010
1011
  else if (expr.type === "template_string") {
1011
- const hasSubstitutions = expr.children.some((c) => c.type === "template_substitution");
1012
- if (hasSubstitutions) return {
1012
+ if (expr.children.some((c) => c.type === "template_substitution")) return {
1013
1013
  text: "",
1014
1014
  error: "JSX expressions with template substitutions are not supported"
1015
1015
  };
1016
- const content = expr.text.slice(1, -1);
1017
- return { text: content };
1016
+ return { text: expr.text.slice(1, -1) };
1018
1017
  } else return {
1019
1018
  text: "",
1020
1019
  error: "JSX expressions must be simple identifiers, strings, or template literals"
@@ -1204,22 +1203,22 @@ function react() {
1204
1203
  build.onResolve({
1205
1204
  filter: /.*/,
1206
1205
  namespace: "source"
1207
- }, ({ entrypoint, path: path$1, namespace: namespace$2 }) => {
1206
+ }, ({ entrypoint, path: path$1, namespace: namespace$3 }) => {
1208
1207
  return {
1209
1208
  entrypoint,
1210
- namespace: namespace$2,
1209
+ namespace: namespace$3,
1211
1210
  path: resolve(path$1)
1212
1211
  };
1213
1212
  });
1214
1213
  build.onLoad({
1215
1214
  filter,
1216
1215
  namespace: "source"
1217
- }, async ({ entrypoint, path: path$1, namespace: namespace$2 }) => {
1216
+ }, async ({ entrypoint, path: path$1, namespace: namespace$3 }) => {
1218
1217
  const data = await readFile(path$1, "utf8");
1219
1218
  return {
1220
1219
  entrypoint,
1221
1220
  path: path$1,
1222
- namespace: namespace$2,
1221
+ namespace: namespace$3,
1223
1222
  data
1224
1223
  };
1225
1224
  });
@@ -1235,7 +1234,6 @@ function react() {
1235
1234
  namespace: "translate",
1236
1235
  data: translations
1237
1236
  });
1238
- return void 0;
1239
1237
  });
1240
1238
  }
1241
1239
  };