@gravity-ui/data-source 0.10.0-alpha.0 → 0.10.0-alpha.10

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 (77) hide show
  1. package/build/cjs/react/components/Async/Async.d.ts +3 -0
  2. package/build/cjs/react/components/Async/Async.js +18 -0
  3. package/build/cjs/react/components/Async/Async.js.map +1 -0
  4. package/build/cjs/react/components/Async/index.d.ts +3 -0
  5. package/build/cjs/react/components/Async/index.js +20 -0
  6. package/build/cjs/react/components/Async/index.js.map +1 -0
  7. package/build/cjs/react/components/Async/types.d.ts +9 -0
  8. package/build/cjs/react/components/Async/types.js +6 -0
  9. package/build/cjs/react/components/Async/types.js.map +1 -0
  10. package/build/cjs/react/components/Async/withAsync.d.ts +3 -0
  11. package/build/cjs/react/components/Async/withAsync.js +26 -0
  12. package/build/cjs/react/components/Async/withAsync.js.map +1 -0
  13. package/build/cjs/react/components/AsyncBoundary/AsyncBoundary.js +3 -2
  14. package/build/cjs/react/components/AsyncBoundary/AsyncBoundary.js.map +1 -1
  15. package/build/cjs/react/components/AsyncBoundary/types.d.ts +3 -3
  16. package/build/cjs/react/components/AsyncBoundary/types.js.map +1 -1
  17. package/build/cjs/react/components/AsyncBoundary/withAsyncBoundary.d.ts +3 -2
  18. package/build/cjs/react/components/AsyncBoundary/withAsyncBoundary.js +9 -5
  19. package/build/cjs/react/components/AsyncBoundary/withAsyncBoundary.js.map +1 -1
  20. package/build/cjs/react/index.d.ts +1 -0
  21. package/build/cjs/react/index.js +12 -0
  22. package/build/cjs/react/index.js.map +1 -1
  23. package/build/cjs/react-query/components/QueryAsyncBoundary/types.d.ts +5 -4
  24. package/build/cjs/react-query/components/QueryAsyncBoundary/types.js.map +1 -1
  25. package/build/cjs/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.d.ts +3 -2
  26. package/build/cjs/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js +9 -5
  27. package/build/cjs/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js.map +1 -1
  28. package/build/esm/react/components/Async/Async.d.ts +3 -0
  29. package/build/esm/react/components/Async/Async.js +11 -0
  30. package/build/esm/react/components/Async/Async.js.map +1 -0
  31. package/build/esm/react/components/Async/index.d.ts +3 -0
  32. package/build/esm/react/components/Async/index.js +3 -0
  33. package/build/esm/react/components/Async/index.js.map +1 -0
  34. package/build/esm/react/components/Async/types.d.ts +9 -0
  35. package/build/esm/react/components/Async/types.js +2 -0
  36. package/build/esm/react/components/Async/types.js.map +1 -0
  37. package/build/esm/react/components/Async/withAsync.d.ts +3 -0
  38. package/build/esm/react/components/Async/withAsync.js +19 -0
  39. package/build/esm/react/components/Async/withAsync.js.map +1 -0
  40. package/build/esm/react/components/AsyncBoundary/AsyncBoundary.js +3 -2
  41. package/build/esm/react/components/AsyncBoundary/AsyncBoundary.js.map +1 -1
  42. package/build/esm/react/components/AsyncBoundary/types.d.ts +3 -3
  43. package/build/esm/react/components/AsyncBoundary/types.js.map +1 -1
  44. package/build/esm/react/components/AsyncBoundary/withAsyncBoundary.d.ts +3 -2
  45. package/build/esm/react/components/AsyncBoundary/withAsyncBoundary.js +9 -5
  46. package/build/esm/react/components/AsyncBoundary/withAsyncBoundary.js.map +1 -1
  47. package/build/esm/react/index.d.ts +1 -0
  48. package/build/esm/react/index.js +1 -0
  49. package/build/esm/react/index.js.map +1 -1
  50. package/build/esm/react-query/components/QueryAsyncBoundary/types.d.ts +5 -4
  51. package/build/esm/react-query/components/QueryAsyncBoundary/types.js.map +1 -1
  52. package/build/esm/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.d.ts +3 -2
  53. package/build/esm/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js +9 -5
  54. package/build/esm/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js.map +1 -1
  55. package/build/plugin/esbuild.cjs +5 -0
  56. package/build/plugin/esbuild.d.cts +6 -0
  57. package/build/plugin/esbuild.mjs +1 -1
  58. package/build/plugin/factory-B49Ym3ij.cjs +696 -0
  59. package/build/plugin/{factory-2tSt4Rte.mjs → factory-C_-OCKcW.mjs} +197 -56
  60. package/build/plugin/index-Dv4gZVjD.d.cts +23 -0
  61. package/build/plugin/index.cjs +12 -0
  62. package/build/plugin/index.d.cts +7 -0
  63. package/build/plugin/index.mjs +1 -1
  64. package/build/plugin/rollup.cjs +5 -0
  65. package/build/plugin/rollup.d.cts +5 -0
  66. package/build/plugin/rollup.mjs +1 -1
  67. package/build/plugin/rspack.cjs +5 -0
  68. package/build/plugin/rspack.d.cts +6 -0
  69. package/build/plugin/rspack.mjs +1 -1
  70. package/build/plugin/typings/client.d.ts +16 -4
  71. package/build/plugin/vite.cjs +5 -0
  72. package/build/plugin/vite.d.cts +5 -0
  73. package/build/plugin/vite.mjs +1 -1
  74. package/build/plugin/webpack.cjs +5 -0
  75. package/build/plugin/webpack.d.cts +6 -0
  76. package/build/plugin/webpack.mjs +1 -1
  77. package/package.json +13 -10
@@ -24,11 +24,12 @@ function isCompanionSuffix(suffix) {
24
24
  return COMPANION_SUFFIXES_SET.has(suffix);
25
25
  }
26
26
  function makeVirtualId(type, sourceFile) {
27
- return `${VIRTUAL_PREFIX}:${type}:${sourceFile}`;
27
+ return `${VIRTUAL_PREFIX}:${type}:${stripQuery(sourceFile)}`;
28
28
  }
29
29
  function parseVirtualId(id) {
30
- if (id.indexOf("\0dsl-plugin") !== 0) return null;
31
- const rest = id.slice(12);
30
+ const prefixIndex = id.indexOf(VIRTUAL_PREFIX);
31
+ if (prefixIndex === -1) return null;
32
+ const rest = id.slice(prefixIndex + 11 + 1);
32
33
  const colonIndex = rest.indexOf(":");
33
34
  if (colonIndex === -1) return null;
34
35
  const type = rest.slice(0, colonIndex);
@@ -40,8 +41,9 @@ function parseVirtualId(id) {
40
41
  }
41
42
  function makeCompanionId(type, sourceFile) {
42
43
  const suffix = COMPANION_TYPES[type];
43
- const ext = path.extname(sourceFile);
44
- return `${ext ? sourceFile.slice(0, -ext.length) : sourceFile}.${suffix}${ext}`;
44
+ const cleanSourceFile = stripQuery(sourceFile);
45
+ const ext = path.extname(cleanSourceFile);
46
+ return `${ext ? cleanSourceFile.slice(0, -ext.length) : cleanSourceFile}.${suffix}${ext}`;
45
47
  }
46
48
  function parseCompanionId(id) {
47
49
  const suffixIndex = id.lastIndexOf(".");
@@ -56,6 +58,14 @@ function parseCompanionId(id) {
56
58
  function isRelativeId(id) {
57
59
  return id[0] === ".";
58
60
  }
61
+ function toRelativeImportSource(fromFile, toFile) {
62
+ const fromDir = path.dirname(stripQuery(fromFile));
63
+ let rel = path.relative(fromDir, toFile);
64
+ if (path.sep !== "/") rel = rel.split(path.sep).join("/");
65
+ if (!rel.startsWith(".")) rel = `./${rel}`;
66
+ const ext = path.extname(rel);
67
+ return ext ? rel.slice(0, -ext.length) : rel;
68
+ }
59
69
  function stripQuery(id) {
60
70
  const queryIndex = id.indexOf("?");
61
71
  return queryIndex === -1 ? id : id.slice(0, queryIndex);
@@ -93,7 +103,7 @@ function extractHocInfo(hocsSet, filename, source) {
93
103
  for (const node of program.body) {
94
104
  if (node.type !== "ExportNamedDeclaration" || node.exportKind === "type" || node.declaration?.type !== "VariableDeclaration") continue;
95
105
  for (const decl of node.declaration.declarations) {
96
- if (decl.id.type !== "Identifier" || !decl.init || decl.init.type !== "CallExpression" || decl.init.callee.type !== "Identifier" || decl.init.arguments.length !== 3) continue;
106
+ if (decl.id.type !== "Identifier" || !decl.init || decl.init.type !== "CallExpression" || decl.init.callee.type !== "Identifier" || decl.init.arguments.length < 2) continue;
97
107
  const hocEntry = trackedHocs.get(decl.init.callee.name);
98
108
  if (!hocEntry) continue;
99
109
  const [contentArg, loadingArg, errorArg] = decl.init.arguments;
@@ -119,17 +129,70 @@ function extractHocInfo(hocsSet, filename, source) {
119
129
  argEnd: loadingArg.end,
120
130
  imports: filterNeededImports(importDecls, collectIdentifiers(loadingArg))
121
131
  },
122
- error: {
132
+ error: errorArg ? {
123
133
  argSource: source.slice(errorArg.start, errorArg.end),
124
134
  argStart: errorArg.start,
125
135
  argEnd: errorArg.end,
126
136
  imports: filterNeededImports(importDecls, collectIdentifiers(errorArg))
127
- }
137
+ } : null
128
138
  };
129
139
  }
130
140
  }
131
141
  return null;
132
142
  }
143
+ function extractReExports(filename, source) {
144
+ const named = /* @__PURE__ */ new Map();
145
+ const stars = [];
146
+ const program = parseProgram(filename, source);
147
+ if (!program) return {
148
+ named,
149
+ stars
150
+ };
151
+ const localImports = /* @__PURE__ */ new Map();
152
+ for (const node of program.body) {
153
+ if (node.type !== "ImportDeclaration" || node.importKind === "type") continue;
154
+ for (const spec of node.specifiers) {
155
+ if (spec.type !== "ImportSpecifier" || spec.importKind === "type") continue;
156
+ const importedName = spec.imported.type === "Literal" ? spec.imported.value : spec.imported.name;
157
+ localImports.set(spec.local.name, {
158
+ source: node.source.value,
159
+ importedName
160
+ });
161
+ }
162
+ }
163
+ for (const node of program.body) {
164
+ if (node.type === "ExportAllDeclaration" && node.exportKind !== "type" && !node.exported) {
165
+ stars.push(node.source.value);
166
+ continue;
167
+ }
168
+ if (node.type !== "ExportNamedDeclaration" || node.exportKind === "type") continue;
169
+ if (node.source) {
170
+ const src = node.source.value;
171
+ for (const spec of node.specifiers) {
172
+ if (spec.exportKind === "type") continue;
173
+ const localName = spec.local.type === "Literal" ? spec.local.value : spec.local.name;
174
+ const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
175
+ named.set(exportedName, {
176
+ source: src,
177
+ importedName: localName
178
+ });
179
+ }
180
+ continue;
181
+ }
182
+ if (node.declaration) continue;
183
+ for (const spec of node.specifiers) {
184
+ if (spec.exportKind === "type" || spec.local.type === "Literal") continue;
185
+ const target = localImports.get(spec.local.name);
186
+ if (!target) continue;
187
+ const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
188
+ named.set(exportedName, target);
189
+ }
190
+ }
191
+ return {
192
+ named,
193
+ stars
194
+ };
195
+ }
133
196
  const COMPANION_PROPS_RE = new RegExp(`\\.(${Object.values(COMPANION_TYPES).join("|")})\\b`);
134
197
  function extractUsages(filename, source) {
135
198
  const result = [];
@@ -256,39 +319,33 @@ function parseProgram(filename, source) {
256
319
  }
257
320
  //#endregion
258
321
  //#region src/plugin/core/generate.ts
259
- function generateAuxModule(sourceFile, info, type, options = {}) {
260
- const auxInfo = info[type];
261
- const name = info.exportedName;
322
+ function generateAuxModuleCode(exportedName, info, type) {
262
323
  const suffix = COMPANION_TYPES[type];
263
- const cleanSourceFile = stripQuery(sourceFile);
264
- const code = [
265
- renderImports(auxInfo.imports),
324
+ return [
325
+ renderImports(info.imports),
266
326
  "",
267
- `export const ${name}${suffix} = ${auxInfo.argSource};`,
327
+ `export const ${exportedName}${suffix} = ${info.argSource};`,
268
328
  ""
269
329
  ].join("\n").trimStart();
270
- return transformJsx(makeCompanionId(type, cleanSourceFile), code, options);
271
330
  }
272
331
  function generateLazyModule(sourceFile, info, options = {}) {
273
332
  const importSource = options.jsx?.importSource ?? "react";
274
333
  const name = info.exportedName;
275
334
  const cleanSourceFile = stripQuery(sourceFile);
276
335
  const base = `./${path.basename(cleanSourceFile, path.extname(cleanSourceFile))}`;
277
- const code = [
336
+ const hocBinding = info.hocImportedName === "default" ? info.hocLocalName : info.hocImportedName;
337
+ const lines = [
278
338
  `import {lazy} from '${importSource}';`,
279
339
  "",
280
340
  info.hocImportedName === "default" ? `import ${info.hocLocalName} from '${info.hocImportSource}';` : `import {${info.hocImportedName}} from '${info.hocImportSource}';`,
281
341
  "",
282
- `import {${name}Loading} from '${base}.Loading';`,
283
- `import {${name}Error} from '${base}.Error';`,
284
- ``,
285
- `export const ${name}Lazy = ${info.hocImportedName === "default" ? info.hocLocalName : info.hocImportedName}(`,
286
- ` lazy(() => import('${base}').then((m) => ({default: m.${name}Content}))),`,
287
- ` ${name}Loading,`,
288
- ` ${name}Error,`,
289
- `);`,
290
- ``
291
- ].join("\n");
342
+ `import {${name}Loading} from '${base}.Loading';`
343
+ ];
344
+ if (info.error) lines.push(`import {${name}Error} from '${base}.Error';`);
345
+ lines.push("", `export const ${name}Lazy = ${hocBinding}(`, ` lazy(() => import('${base}').then((m) => ({default: m.${name}Content}))),`, ` ${name}Loading,`);
346
+ if (info.error) lines.push(` ${name}Error,`);
347
+ lines.push(`);`, ``);
348
+ const code = lines.join("\n");
292
349
  const filename = makeCompanionId("lazy", cleanSourceFile);
293
350
  return {
294
351
  code,
@@ -299,7 +356,7 @@ function generateLazyModule(sourceFile, info, options = {}) {
299
356
  }).toString()
300
357
  };
301
358
  }
302
- function transformJsx(filename, code, options) {
359
+ function transformJsx(filename, code, options = {}) {
303
360
  const result = transformSync(filename, code, {
304
361
  lang: "tsx",
305
362
  jsx: options.jsx,
@@ -369,9 +426,11 @@ function transformDefinitionModule(s, filename, info) {
369
426
  const name = info.exportedName;
370
427
  const cleanFilename = stripQuery(filename);
371
428
  const localSource = `./${path.basename(cleanFilename, path.extname(cleanFilename))}`;
372
- s.prepend(`${renderNamedImport(`${name}Loading`, `${name}Loading`, makeCompanionId("loading", localSource))}\n${renderNamedImport(`${name}Error`, `${name}Error`, makeCompanionId("error", localSource))}\n`);
429
+ const prependImports = [renderNamedImport(`${name}Loading`, `${name}Loading`, makeCompanionId("loading", localSource))];
430
+ if (info.error) prependImports.push(renderNamedImport(`${name}Error`, `${name}Error`, makeCompanionId("error", localSource)));
431
+ s.prepend(prependImports.join("\n") + "\n");
373
432
  s.overwrite(info.loading.argStart, info.loading.argEnd, `${name}Loading`);
374
- s.overwrite(info.error.argStart, info.error.argEnd, `${name}Error`);
433
+ if (info.error) s.overwrite(info.error.argStart, info.error.argEnd, `${name}Error`);
375
434
  if (info.content.kind === "identifier") s.append(`\nexport {${info.content.name} as ${name}Content};\n`);
376
435
  else {
377
436
  s.appendLeft(info.hocExportStart, `const ${name}Content = ${info.content.argSource};\n\n`);
@@ -380,15 +439,15 @@ function transformDefinitionModule(s, filename, info) {
380
439
  }
381
440
  }
382
441
  function transformUsages(s, usages) {
383
- for (const { spec, accesses } of usages) for (const access of accesses) s.overwrite(access.start, access.end, `${spec.localName}${access.prop}`);
442
+ for (const { usage } of usages) for (const access of usage.accesses) s.overwrite(access.start, access.end, `${usage.spec.localName}${access.prop}`);
384
443
  const newImports = [];
385
- for (const { decl, spec, accesses } of usages) {
386
- const propsUsed = new Set(accesses.map((access) => access.prop));
387
- for (const prop of propsUsed) newImports.push(renderNamedImport(`${spec.localName}${prop}`, `${spec.importedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], decl.source)));
444
+ for (const { usage, hocImportSource, hocExportedName } of usages) {
445
+ const propsUsed = new Set(usage.accesses.map((access) => access.prop));
446
+ for (const prop of propsUsed) newImports.push(renderNamedImport(`${usage.spec.localName}${prop}`, `${hocExportedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], hocImportSource)));
388
447
  }
389
448
  if (newImports.length > 0) s.prepend(newImports.join("\n") + "\n");
390
449
  const localsByDecl = /* @__PURE__ */ new Map();
391
- for (const usage of usages) {
450
+ for (const { usage } of usages) {
392
451
  if (usage.hasOtherUsages) continue;
393
452
  let locals = localsByDecl.get(usage.decl);
394
453
  if (!locals) localsByDecl.set(usage.decl, locals = /* @__PURE__ */ new Set());
@@ -415,13 +474,20 @@ function renderNamedImport(local, imported, source) {
415
474
  }
416
475
  //#endregion
417
476
  //#region src/plugin/core/factory.ts
418
- const DEFAULT_HOCS = [{
419
- from: "@gravity-ui/data-source",
420
- name: "withAsyncBoundary"
421
- }, {
422
- from: "@gravity-ui/data-source",
423
- name: "withQueryAsyncBoundary"
424
- }];
477
+ const DEFAULT_HOCS = [
478
+ {
479
+ from: "@gravity-ui/data-source",
480
+ name: "withAsync"
481
+ },
482
+ {
483
+ from: "@gravity-ui/data-source",
484
+ name: "withAsyncBoundary"
485
+ },
486
+ {
487
+ from: "@gravity-ui/data-source",
488
+ name: "withQueryAsyncBoundary"
489
+ }
490
+ ];
425
491
  const DEFAULT_INCLUDE = /\.(tsx?|jsx?)$/;
426
492
  const VIRTUAL_EXCLUDE = new RegExp(`${VIRTUAL_PREFIX}|${encodeURIComponent(VIRTUAL_PREFIX)}`);
427
493
  const dataSourceLazyUnpluginFactory = (options = {}) => {
@@ -433,6 +499,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
433
499
  const hocsSet = new Set(hocPatterns.map((pattern) => getHocString(pattern.from, pattern.name)));
434
500
  const hocsRegexp = new RegExp(Array.from(new Set(hocPatterns.map((pattern) => pattern.name))).map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"));
435
501
  const hocInfoCache = /* @__PURE__ */ new Map();
502
+ const reExportsCache = /* @__PURE__ */ new Map();
436
503
  const getHocInfo = (sourceFile, source) => {
437
504
  const key = stripQuery(sourceFile);
438
505
  const cachedInfo = hocInfoCache.get(key);
@@ -442,6 +509,53 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
442
509
  hocInfoCache.set(key, info);
443
510
  return info;
444
511
  };
512
+ const getReExports = (sourceFile, source) => {
513
+ const key = stripQuery(sourceFile);
514
+ const cachedReExports = reExportsCache.get(key);
515
+ if (cachedReExports) return cachedReExports;
516
+ const reExports = extractReExports(key, source ?? fs.readFileSync(key, "utf-8"));
517
+ reExportsCache.set(key, reExports);
518
+ return reExports;
519
+ };
520
+ const resolveHocSourceFile = async (ctx, file, importedName, visited = /* @__PURE__ */ new Set()) => {
521
+ const visitKey = `${file}\0${importedName}`;
522
+ if (visited.has(visitKey)) return null;
523
+ visited.add(visitKey);
524
+ const directInfo = getHocInfo(file);
525
+ if (directInfo && directInfo.exportedName === importedName) return {
526
+ file,
527
+ info: directInfo
528
+ };
529
+ const reExports = getReExports(file);
530
+ const named = reExports.named.get(importedName);
531
+ if (named) {
532
+ const resolved = await resolveSourceFile(ctx, named.source, file);
533
+ if (resolved) {
534
+ const next = await resolveHocSourceFile(ctx, resolved, named.importedName, visited);
535
+ if (next) return next;
536
+ }
537
+ }
538
+ for (const starSource of reExports.stars) {
539
+ const resolved = await resolveSourceFile(ctx, starSource, file);
540
+ if (!resolved) continue;
541
+ const next = await resolveHocSourceFile(ctx, resolved, importedName, visited);
542
+ if (next) return next;
543
+ }
544
+ return null;
545
+ };
546
+ const verifyUsages = async (ctx, usages, importerId) => {
547
+ return (await Promise.all(usages.map(async (usage) => {
548
+ const resolvedSourceFile = await resolveSourceFile(ctx, usage.decl.source, importerId);
549
+ if (!resolvedSourceFile) return null;
550
+ const target = await resolveHocSourceFile(ctx, resolvedSourceFile, usage.spec.importedName);
551
+ if (!target) return null;
552
+ return {
553
+ usage,
554
+ hocImportSource: toRelativeImportSource(importerId, target.file),
555
+ hocExportedName: target.info.exportedName
556
+ };
557
+ }))).filter((usage) => usage !== null);
558
+ };
445
559
  return {
446
560
  name: "data-source-lazy",
447
561
  enforce: "pre",
@@ -460,18 +574,29 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
460
574
  },
461
575
  load: {
462
576
  filter: { id: new RegExp(VIRTUAL_PREFIX) },
463
- handler(id) {
577
+ async handler(id) {
464
578
  const parsedId = parseVirtualId(id);
465
579
  if (!parsedId) return null;
466
580
  this.addWatchFile(parsedId.sourceFile);
467
581
  const info = getHocInfo(parsedId.sourceFile);
468
582
  if (!info) return null;
469
- switch (parsedId.type) {
470
- case "loading":
471
- case "error": return generateAuxModule(parsedId.sourceFile, info, parsedId.type, options.generateOptions);
472
- case "lazy": return generateLazyModule(parsedId.sourceFile, info, options.generateOptions);
473
- default: return assertNever(parsedId.type);
583
+ if (parsedId.type === "loading" || parsedId.type === "error") {
584
+ const auxInfo = info[parsedId.type];
585
+ if (!auxInfo) return null;
586
+ const filename = makeCompanionId(parsedId.type, parsedId.sourceFile);
587
+ const code = generateAuxModuleCode(info.exportedName, auxInfo, parsedId.type);
588
+ const usages = extractUsages(filename, code);
589
+ const verifiedUsages = await verifyUsages(this, usages, parsedId.sourceFile);
590
+ let resultCode = code;
591
+ if (verifiedUsages.length > 0) {
592
+ const s = new MagicString(code);
593
+ transformUsages(s, verifiedUsages);
594
+ resultCode = s.toString();
595
+ }
596
+ return transformJsx(filename, resultCode, options.generateOptions);
474
597
  }
598
+ if (parsedId.type === "lazy") return generateLazyModule(parsedId.sourceFile, info, options.generateOptions);
599
+ return assertNever(parsedId.type);
475
600
  }
476
601
  },
477
602
  transform: {
@@ -483,13 +608,8 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
483
608
  const info = getHocInfo(id, code);
484
609
  const usages = extractUsages(id, code);
485
610
  if (!info && usages.length === 0) return null;
486
- const verifiedUsages = (await Promise.all(usages.map(async (usage) => {
487
- const resolvedSourceFile = await resolveSourceFile(this, usage.decl.source, id);
488
- if (!resolvedSourceFile) return null;
489
- const usageInfo = getHocInfo(resolvedSourceFile);
490
- if (!usageInfo || usageInfo.exportedName !== usage.spec.importedName) return null;
491
- return usage;
492
- }))).filter((usage) => usage !== null);
611
+ const filteredUsages = info ? dropAccessesInsideHocArgs(info, usages) : usages;
612
+ const verifiedUsages = await verifyUsages(this, filteredUsages, id);
493
613
  if (!info && verifiedUsages.length === 0) return null;
494
614
  const s = new MagicString(code);
495
615
  if (info) transformDefinitionModule(s, id, info);
@@ -505,9 +625,30 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
505
625
  }
506
626
  },
507
627
  watchChange(id) {
508
- hocInfoCache.delete(stripQuery(id));
628
+ const key = stripQuery(id);
629
+ hocInfoCache.delete(key);
630
+ reExportsCache.delete(key);
509
631
  }
510
632
  };
511
633
  };
634
+ function isInside(info, access) {
635
+ return info.argStart <= access.start && access.end <= info.argEnd;
636
+ }
637
+ function dropAccessesInsideHocArgs(info, usages) {
638
+ const result = [];
639
+ for (const usage of usages) {
640
+ const accesses = usage.accesses.filter((access) => !isInside(info.loading, access) && !(info.error && isInside(info.error, access)) && !(info.content.kind === "inline" && isInside(info.content, access)));
641
+ if (accesses.length === 0) continue;
642
+ if (accesses.length === usage.accesses.length) {
643
+ result.push(usage);
644
+ continue;
645
+ }
646
+ result.push({
647
+ ...usage,
648
+ accesses
649
+ });
650
+ }
651
+ return result;
652
+ }
512
653
  //#endregion
513
654
  export { dataSourceLazyUnpluginFactory as n, DEFAULT_HOCS as t };
@@ -0,0 +1,23 @@
1
+ import { UnpluginFactory } from "unplugin";
2
+ import { JsxOptions } from "oxc-transform";
3
+ //#region src/plugin/core/generate.d.ts
4
+ interface GenerateOptions {
5
+ jsx?: JsxOptions;
6
+ target?: string | string[];
7
+ }
8
+ //#endregion
9
+ //#region src/plugin/core/factory.d.ts
10
+ interface DataSourceLazyHocPattern {
11
+ from: string;
12
+ name: string;
13
+ }
14
+ interface DataSourceLazyPluginOptions {
15
+ hocs?: DataSourceLazyHocPattern[];
16
+ include?: RegExp | RegExp[];
17
+ exclude?: RegExp | RegExp[];
18
+ generateOptions?: GenerateOptions;
19
+ }
20
+ declare const DEFAULT_HOCS: DataSourceLazyPluginOptions['hocs'];
21
+ declare const dataSourceLazyUnpluginFactory: UnpluginFactory<DataSourceLazyPluginOptions | undefined>;
22
+ //#endregion
23
+ export { dataSourceLazyUnpluginFactory as i, DataSourceLazyHocPattern as n, DataSourceLazyPluginOptions as r, DEFAULT_HOCS as t };
@@ -0,0 +1,12 @@
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
5
+ const require_factory = require("./factory-B49Ym3ij.cjs");
6
+ //#region src/plugin/index.ts
7
+ const DataSourceLazyPlugin = /* @__PURE__ */ (0, require("unplugin").createUnplugin)(require_factory.dataSourceLazyUnpluginFactory);
8
+ //#endregion
9
+ exports.DEFAULT_HOCS = require_factory.DEFAULT_HOCS;
10
+ exports.DataSourceLazyPlugin = DataSourceLazyPlugin;
11
+ exports.default = DataSourceLazyPlugin;
12
+ exports.dataSourceLazyUnpluginFactory = require_factory.dataSourceLazyUnpluginFactory;
@@ -0,0 +1,7 @@
1
+ import { i as dataSourceLazyUnpluginFactory, n as DataSourceLazyHocPattern, r as DataSourceLazyPluginOptions, t as DEFAULT_HOCS } from "./index-Dv4gZVjD.cjs";
2
+ import * as _$unplugin from "unplugin";
3
+
4
+ //#region src/plugin/index.d.ts
5
+ declare const DataSourceLazyPlugin: _$unplugin.UnpluginInstance<DataSourceLazyPluginOptions | undefined, boolean>;
6
+ //#endregion
7
+ export { DEFAULT_HOCS, DataSourceLazyHocPattern, DataSourceLazyPlugin, DataSourceLazyPlugin as default, DataSourceLazyPluginOptions, dataSourceLazyUnpluginFactory };
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory, t as DEFAULT_HOCS } from "./factory-2tSt4Rte.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory, t as DEFAULT_HOCS } from "./factory-C_-OCKcW.mjs";
2
2
  import { createUnplugin } from "unplugin";
3
3
  //#region src/plugin/index.ts
4
4
  const DataSourceLazyPlugin = /* @__PURE__ */ createUnplugin(dataSourceLazyUnpluginFactory);
@@ -0,0 +1,5 @@
1
+ const require_factory = require("./factory-B49Ym3ij.cjs");
2
+ //#region src/plugin/rollup.ts
3
+ var rollup_default = (0, require("unplugin").createRollupPlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
+ //#endregion
5
+ module.exports = rollup_default;
@@ -0,0 +1,5 @@
1
+ import { r as DataSourceLazyPluginOptions } from "./index-Dv4gZVjD.cjs";
2
+
3
+ //#region src/plugin/rollup.d.ts
4
+ declare const _default: (options?: DataSourceLazyPluginOptions | undefined) => any;
5
+ export = _default;
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory-2tSt4Rte.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-C_-OCKcW.mjs";
2
2
  import { createRollupPlugin } from "unplugin";
3
3
  //#region src/plugin/rollup.ts
4
4
  var rollup_default = createRollupPlugin(dataSourceLazyUnpluginFactory);
@@ -0,0 +1,5 @@
1
+ const require_factory = require("./factory-B49Ym3ij.cjs");
2
+ //#region src/plugin/rspack.ts
3
+ var rspack_default = (0, require("unplugin").createRspackPlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
+ //#endregion
5
+ module.exports = rspack_default;
@@ -0,0 +1,6 @@
1
+ import { r as DataSourceLazyPluginOptions } from "./index-Dv4gZVjD.cjs";
2
+ import * as _$unplugin from "unplugin";
3
+
4
+ //#region src/plugin/rspack.d.ts
5
+ declare const _default: (options?: DataSourceLazyPluginOptions | undefined) => _$unplugin.RspackPluginInstance;
6
+ export = _default;
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory-2tSt4Rte.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-C_-OCKcW.mjs";
2
2
  import { createRspackPlugin } from "unplugin";
3
3
  //#region src/plugin/rspack.ts
4
4
  var rspack_default = createRspackPlugin(dataSourceLazyUnpluginFactory);
@@ -1,11 +1,23 @@
1
1
  import type {ComponentType} from 'react';
2
2
 
3
3
  declare module '@gravity-ui/data-source' {
4
- interface AsyncBoundaryComponent<TProps extends object> {
5
- Lazy: ComponentType<TProps>;
4
+ interface AsyncComponent<TProps extends object = {}, TLoadingProps extends object = {}> {
5
+ Lazy: ComponentType<TProps & TLoadingProps>;
6
6
  }
7
7
 
8
- interface QueryAsyncBoundaryComponent<TProps extends object> {
9
- Lazy: ComponentType<TProps>;
8
+ interface AsyncBoundaryComponent<
9
+ TProps extends object = {},
10
+ TLoadingProps extends object = {},
11
+ TErrorProps extends object = {},
12
+ > {
13
+ Lazy: ComponentType<TProps & TLoadingProps & Omit<TErrorProps, keyof ErrorViewProps>>;
14
+ }
15
+
16
+ interface QueryAsyncBoundaryComponent<
17
+ TProps extends object = {},
18
+ TLoadingProps extends object = {},
19
+ TErrorProps extends object = {},
20
+ > {
21
+ Lazy: ComponentType<TProps & TLoadingProps & Omit<TErrorProps, keyof ErrorViewProps>>;
10
22
  }
11
23
  }
@@ -0,0 +1,5 @@
1
+ const require_factory = require("./factory-B49Ym3ij.cjs");
2
+ //#region src/plugin/vite.ts
3
+ var vite_default = (0, require("unplugin").createVitePlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
+ //#endregion
5
+ module.exports = vite_default;
@@ -0,0 +1,5 @@
1
+ import { r as DataSourceLazyPluginOptions } from "./index-Dv4gZVjD.cjs";
2
+
3
+ //#region src/plugin/vite.d.ts
4
+ declare const _default: (options?: DataSourceLazyPluginOptions | undefined) => any;
5
+ export = _default;
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory-2tSt4Rte.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-C_-OCKcW.mjs";
2
2
  import { createVitePlugin } from "unplugin";
3
3
  //#region src/plugin/vite.ts
4
4
  var vite_default = createVitePlugin(dataSourceLazyUnpluginFactory);
@@ -0,0 +1,5 @@
1
+ const require_factory = require("./factory-B49Ym3ij.cjs");
2
+ //#region src/plugin/webpack.ts
3
+ var webpack_default = (0, require("unplugin").createWebpackPlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
+ //#endregion
5
+ module.exports = webpack_default;
@@ -0,0 +1,6 @@
1
+ import { r as DataSourceLazyPluginOptions } from "./index-Dv4gZVjD.cjs";
2
+ import * as _$webpack from "webpack";
3
+
4
+ //#region src/plugin/webpack.d.ts
5
+ declare const _default: (options?: DataSourceLazyPluginOptions | undefined) => _$webpack.WebpackPluginInstance;
6
+ export = _default;
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory-2tSt4Rte.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-C_-OCKcW.mjs";
2
2
  import { createWebpackPlugin } from "unplugin";
3
3
  //#region src/plugin/webpack.ts
4
4
  var webpack_default = createWebpackPlugin(dataSourceLazyUnpluginFactory);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/data-source",
3
- "version": "0.10.0-alpha.0",
3
+ "version": "0.10.0-alpha.10",
4
4
  "description": "A wrapper around data fetching",
5
5
  "keywords": [
6
6
  "data-fetching",
@@ -24,9 +24,6 @@
24
24
  "yarn": "Please use npm instead of yarn to install dependencies",
25
25
  "pnpm": "Please use npm instead of pnpm to install dependencies"
26
26
  },
27
- "main": "build/cjs/index.js",
28
- "module": "build/esm/index.js",
29
- "types": "build/esm/index.d.ts",
30
27
  "exports": {
31
28
  ".": {
32
29
  "types": "./build/esm/index.d.ts",
@@ -35,27 +32,33 @@
35
32
  },
36
33
  "./plugin": {
37
34
  "types": "./build/plugin/index.d.mts",
38
- "default": "./build/plugin/index.mjs"
35
+ "import": "./build/plugin/index.mjs",
36
+ "require": "./build/plugin/index.cjs"
39
37
  },
40
38
  "./plugin-webpack": {
41
39
  "types": "./build/plugin/webpack.d.mts",
42
- "default": "./build/plugin/webpack.mjs"
40
+ "import": "./build/plugin/webpack.mjs",
41
+ "require": "./build/plugin/webpack.cjs"
43
42
  },
44
43
  "./plugin-rspack": {
45
44
  "types": "./build/plugin/rspack.d.mts",
46
- "default": "./build/plugin/rspack.mjs"
45
+ "import": "./build/plugin/rspack.mjs",
46
+ "require": "./build/plugin/rspack.cjs"
47
47
  },
48
48
  "./plugin-vite": {
49
49
  "types": "./build/plugin/vite.d.mts",
50
- "default": "./build/plugin/vite.mjs"
50
+ "import": "./build/plugin/vite.mjs",
51
+ "require": "./build/plugin/vite.cjs"
51
52
  },
52
53
  "./plugin-rollup": {
53
54
  "types": "./build/plugin/rollup.d.mts",
54
- "default": "./build/plugin/rollup.mjs"
55
+ "import": "./build/plugin/rollup.mjs",
56
+ "require": "./build/plugin/rollup.cjs"
55
57
  },
56
58
  "./plugin-esbuild": {
57
59
  "types": "./build/plugin/esbuild.d.mts",
58
- "default": "./build/plugin/esbuild.mjs"
60
+ "import": "./build/plugin/esbuild.mjs",
61
+ "require": "./build/plugin/esbuild.cjs"
59
62
  },
60
63
  "./plugin-client": {
61
64
  "types": "./build/plugin/typings/client.d.ts"