@gravity-ui/data-source 0.10.0-alpha.5 → 0.10.0-alpha.7

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.
@@ -1,4 +1,4 @@
1
- const require_factory = require("./factory-CmOH3Kh6.cjs");
1
+ const require_factory = require("./factory-Dq6kUGl-.cjs");
2
2
  //#region src/plugin/esbuild.ts
3
3
  var esbuild_default = (0, require("unplugin").createEsbuildPlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
4
  //#endregion
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory--H-amlua.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-Ds3_-8kv.mjs";
2
2
  import { createEsbuildPlugin } from "unplugin";
3
3
  //#region src/plugin/esbuild.ts
4
4
  var esbuild_default = createEsbuildPlugin(dataSourceLazyUnpluginFactory);
@@ -82,6 +82,14 @@ function parseCompanionId(id) {
82
82
  function isRelativeId(id) {
83
83
  return id[0] === ".";
84
84
  }
85
+ function toRelativeImportSource(fromFile, toFile) {
86
+ const fromDir = node_path.default.dirname(stripQuery(fromFile));
87
+ let rel = node_path.default.relative(fromDir, toFile);
88
+ if (node_path.default.sep !== "/") rel = rel.split(node_path.default.sep).join("/");
89
+ if (!rel.startsWith(".")) rel = `./${rel}`;
90
+ const ext = node_path.default.extname(rel);
91
+ return ext ? rel.slice(0, -ext.length) : rel;
92
+ }
85
93
  function stripQuery(id) {
86
94
  const queryIndex = id.indexOf("?");
87
95
  return queryIndex === -1 ? id : id.slice(0, queryIndex);
@@ -156,6 +164,59 @@ function extractHocInfo(hocsSet, filename, source) {
156
164
  }
157
165
  return null;
158
166
  }
167
+ function extractReExports(filename, source) {
168
+ const named = /* @__PURE__ */ new Map();
169
+ const stars = [];
170
+ const program = parseProgram(filename, source);
171
+ if (!program) return {
172
+ named,
173
+ stars
174
+ };
175
+ const localImports = /* @__PURE__ */ new Map();
176
+ for (const node of program.body) {
177
+ if (node.type !== "ImportDeclaration" || node.importKind === "type") continue;
178
+ for (const spec of node.specifiers) {
179
+ if (spec.type !== "ImportSpecifier" || spec.importKind === "type") continue;
180
+ const importedName = spec.imported.type === "Literal" ? spec.imported.value : spec.imported.name;
181
+ localImports.set(spec.local.name, {
182
+ source: node.source.value,
183
+ importedName
184
+ });
185
+ }
186
+ }
187
+ for (const node of program.body) {
188
+ if (node.type === "ExportAllDeclaration" && node.exportKind !== "type" && !node.exported) {
189
+ stars.push(node.source.value);
190
+ continue;
191
+ }
192
+ if (node.type !== "ExportNamedDeclaration" || node.exportKind === "type") continue;
193
+ if (node.source) {
194
+ const src = node.source.value;
195
+ for (const spec of node.specifiers) {
196
+ if (spec.exportKind === "type") continue;
197
+ const localName = spec.local.type === "Literal" ? spec.local.value : spec.local.name;
198
+ const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
199
+ named.set(exportedName, {
200
+ source: src,
201
+ importedName: localName
202
+ });
203
+ }
204
+ continue;
205
+ }
206
+ if (node.declaration) continue;
207
+ for (const spec of node.specifiers) {
208
+ if (spec.exportKind === "type" || spec.local.type === "Literal") continue;
209
+ const target = localImports.get(spec.local.name);
210
+ if (!target) continue;
211
+ const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
212
+ named.set(exportedName, target);
213
+ }
214
+ }
215
+ return {
216
+ named,
217
+ stars
218
+ };
219
+ }
159
220
  const COMPANION_PROPS_RE = new RegExp(`\\.(${Object.values(COMPANION_TYPES).join("|")})\\b`);
160
221
  function extractUsages(filename, source) {
161
222
  const result = [];
@@ -406,15 +467,15 @@ function transformDefinitionModule(s, filename, info) {
406
467
  }
407
468
  }
408
469
  function transformUsages(s, usages) {
409
- for (const { spec, accesses } of usages) for (const access of accesses) s.overwrite(access.start, access.end, `${spec.localName}${access.prop}`);
470
+ for (const { usage } of usages) for (const access of usage.accesses) s.overwrite(access.start, access.end, `${usage.spec.localName}${access.prop}`);
410
471
  const newImports = [];
411
- for (const { decl, spec, accesses } of usages) {
412
- const propsUsed = new Set(accesses.map((access) => access.prop));
413
- for (const prop of propsUsed) newImports.push(renderNamedImport(`${spec.localName}${prop}`, `${spec.importedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], decl.source)));
472
+ for (const { usage, hocImportSource, hocExportedName } of usages) {
473
+ const propsUsed = new Set(usage.accesses.map((access) => access.prop));
474
+ for (const prop of propsUsed) newImports.push(renderNamedImport(`${usage.spec.localName}${prop}`, `${hocExportedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], hocImportSource)));
414
475
  }
415
476
  if (newImports.length > 0) s.prepend(newImports.join("\n") + "\n");
416
477
  const localsByDecl = /* @__PURE__ */ new Map();
417
- for (const usage of usages) {
478
+ for (const { usage } of usages) {
418
479
  if (usage.hasOtherUsages) continue;
419
480
  let locals = localsByDecl.get(usage.decl);
420
481
  if (!locals) localsByDecl.set(usage.decl, locals = /* @__PURE__ */ new Set());
@@ -459,6 +520,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
459
520
  const hocsSet = new Set(hocPatterns.map((pattern) => getHocString(pattern.from, pattern.name)));
460
521
  const hocsRegexp = new RegExp(Array.from(new Set(hocPatterns.map((pattern) => pattern.name))).map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"));
461
522
  const hocInfoCache = /* @__PURE__ */ new Map();
523
+ const reExportsCache = /* @__PURE__ */ new Map();
462
524
  const getHocInfo = (sourceFile, source) => {
463
525
  const key = stripQuery(sourceFile);
464
526
  const cachedInfo = hocInfoCache.get(key);
@@ -468,6 +530,14 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
468
530
  hocInfoCache.set(key, info);
469
531
  return info;
470
532
  };
533
+ const getReExports = (sourceFile, source) => {
534
+ const key = stripQuery(sourceFile);
535
+ const cachedReExports = reExportsCache.get(key);
536
+ if (cachedReExports) return cachedReExports;
537
+ const reExports = extractReExports(key, source ?? node_fs.default.readFileSync(key, "utf-8"));
538
+ reExportsCache.set(key, reExports);
539
+ return reExports;
540
+ };
471
541
  return {
472
542
  name: "data-source-lazy",
473
543
  enforce: "pre",
@@ -510,12 +580,42 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
510
580
  const usages = extractUsages(id, code);
511
581
  if (!info && usages.length === 0) return null;
512
582
  const filteredUsages = info ? dropAccessesInsideHocArgs(info, usages) : usages;
583
+ const resolveHocSourceFile = async (file, importedName, visited = /* @__PURE__ */ new Set()) => {
584
+ const visitKey = `${file}\0${importedName}`;
585
+ if (visited.has(visitKey)) return null;
586
+ visited.add(visitKey);
587
+ const directInfo = getHocInfo(file);
588
+ if (directInfo && directInfo.exportedName === importedName) return {
589
+ file,
590
+ info: directInfo
591
+ };
592
+ const reExports = getReExports(file);
593
+ const named = reExports.named.get(importedName);
594
+ if (named) {
595
+ const resolved = await resolveSourceFile(this, named.source, file);
596
+ if (resolved) {
597
+ const next = await resolveHocSourceFile(resolved, named.importedName, visited);
598
+ if (next) return next;
599
+ }
600
+ }
601
+ for (const starSource of reExports.stars) {
602
+ const resolved = await resolveSourceFile(this, starSource, file);
603
+ if (!resolved) continue;
604
+ const next = await resolveHocSourceFile(resolved, importedName, visited);
605
+ if (next) return next;
606
+ }
607
+ return null;
608
+ };
513
609
  const verifiedUsages = (await Promise.all(filteredUsages.map(async (usage) => {
514
610
  const resolvedSourceFile = await resolveSourceFile(this, usage.decl.source, id);
515
611
  if (!resolvedSourceFile) return null;
516
- const usageInfo = getHocInfo(resolvedSourceFile);
517
- if (!usageInfo || usageInfo.exportedName !== usage.spec.importedName) return null;
518
- return usage;
612
+ const target = await resolveHocSourceFile(resolvedSourceFile, usage.spec.importedName);
613
+ if (!target) return null;
614
+ return {
615
+ usage,
616
+ hocImportSource: toRelativeImportSource(id, target.file),
617
+ hocExportedName: target.info.exportedName
618
+ };
519
619
  }))).filter((usage) => usage !== null);
520
620
  if (!info && verifiedUsages.length === 0) return null;
521
621
  const s = new magic_string.default(code);
@@ -532,7 +632,9 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
532
632
  }
533
633
  },
534
634
  watchChange(id) {
535
- hocInfoCache.delete(stripQuery(id));
635
+ const key = stripQuery(id);
636
+ hocInfoCache.delete(key);
637
+ reExportsCache.delete(key);
536
638
  }
537
639
  };
538
640
  };
@@ -57,6 +57,14 @@ function parseCompanionId(id) {
57
57
  function isRelativeId(id) {
58
58
  return id[0] === ".";
59
59
  }
60
+ function toRelativeImportSource(fromFile, toFile) {
61
+ const fromDir = path.dirname(stripQuery(fromFile));
62
+ let rel = path.relative(fromDir, toFile);
63
+ if (path.sep !== "/") rel = rel.split(path.sep).join("/");
64
+ if (!rel.startsWith(".")) rel = `./${rel}`;
65
+ const ext = path.extname(rel);
66
+ return ext ? rel.slice(0, -ext.length) : rel;
67
+ }
60
68
  function stripQuery(id) {
61
69
  const queryIndex = id.indexOf("?");
62
70
  return queryIndex === -1 ? id : id.slice(0, queryIndex);
@@ -131,6 +139,59 @@ function extractHocInfo(hocsSet, filename, source) {
131
139
  }
132
140
  return null;
133
141
  }
142
+ function extractReExports(filename, source) {
143
+ const named = /* @__PURE__ */ new Map();
144
+ const stars = [];
145
+ const program = parseProgram(filename, source);
146
+ if (!program) return {
147
+ named,
148
+ stars
149
+ };
150
+ const localImports = /* @__PURE__ */ new Map();
151
+ for (const node of program.body) {
152
+ if (node.type !== "ImportDeclaration" || node.importKind === "type") continue;
153
+ for (const spec of node.specifiers) {
154
+ if (spec.type !== "ImportSpecifier" || spec.importKind === "type") continue;
155
+ const importedName = spec.imported.type === "Literal" ? spec.imported.value : spec.imported.name;
156
+ localImports.set(spec.local.name, {
157
+ source: node.source.value,
158
+ importedName
159
+ });
160
+ }
161
+ }
162
+ for (const node of program.body) {
163
+ if (node.type === "ExportAllDeclaration" && node.exportKind !== "type" && !node.exported) {
164
+ stars.push(node.source.value);
165
+ continue;
166
+ }
167
+ if (node.type !== "ExportNamedDeclaration" || node.exportKind === "type") continue;
168
+ if (node.source) {
169
+ const src = node.source.value;
170
+ for (const spec of node.specifiers) {
171
+ if (spec.exportKind === "type") continue;
172
+ const localName = spec.local.type === "Literal" ? spec.local.value : spec.local.name;
173
+ const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
174
+ named.set(exportedName, {
175
+ source: src,
176
+ importedName: localName
177
+ });
178
+ }
179
+ continue;
180
+ }
181
+ if (node.declaration) continue;
182
+ for (const spec of node.specifiers) {
183
+ if (spec.exportKind === "type" || spec.local.type === "Literal") continue;
184
+ const target = localImports.get(spec.local.name);
185
+ if (!target) continue;
186
+ const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
187
+ named.set(exportedName, target);
188
+ }
189
+ }
190
+ return {
191
+ named,
192
+ stars
193
+ };
194
+ }
134
195
  const COMPANION_PROPS_RE = new RegExp(`\\.(${Object.values(COMPANION_TYPES).join("|")})\\b`);
135
196
  function extractUsages(filename, source) {
136
197
  const result = [];
@@ -381,15 +442,15 @@ function transformDefinitionModule(s, filename, info) {
381
442
  }
382
443
  }
383
444
  function transformUsages(s, usages) {
384
- for (const { spec, accesses } of usages) for (const access of accesses) s.overwrite(access.start, access.end, `${spec.localName}${access.prop}`);
445
+ for (const { usage } of usages) for (const access of usage.accesses) s.overwrite(access.start, access.end, `${usage.spec.localName}${access.prop}`);
385
446
  const newImports = [];
386
- for (const { decl, spec, accesses } of usages) {
387
- const propsUsed = new Set(accesses.map((access) => access.prop));
388
- for (const prop of propsUsed) newImports.push(renderNamedImport(`${spec.localName}${prop}`, `${spec.importedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], decl.source)));
447
+ for (const { usage, hocImportSource, hocExportedName } of usages) {
448
+ const propsUsed = new Set(usage.accesses.map((access) => access.prop));
449
+ for (const prop of propsUsed) newImports.push(renderNamedImport(`${usage.spec.localName}${prop}`, `${hocExportedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], hocImportSource)));
389
450
  }
390
451
  if (newImports.length > 0) s.prepend(newImports.join("\n") + "\n");
391
452
  const localsByDecl = /* @__PURE__ */ new Map();
392
- for (const usage of usages) {
453
+ for (const { usage } of usages) {
393
454
  if (usage.hasOtherUsages) continue;
394
455
  let locals = localsByDecl.get(usage.decl);
395
456
  if (!locals) localsByDecl.set(usage.decl, locals = /* @__PURE__ */ new Set());
@@ -434,6 +495,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
434
495
  const hocsSet = new Set(hocPatterns.map((pattern) => getHocString(pattern.from, pattern.name)));
435
496
  const hocsRegexp = new RegExp(Array.from(new Set(hocPatterns.map((pattern) => pattern.name))).map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"));
436
497
  const hocInfoCache = /* @__PURE__ */ new Map();
498
+ const reExportsCache = /* @__PURE__ */ new Map();
437
499
  const getHocInfo = (sourceFile, source) => {
438
500
  const key = stripQuery(sourceFile);
439
501
  const cachedInfo = hocInfoCache.get(key);
@@ -443,6 +505,14 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
443
505
  hocInfoCache.set(key, info);
444
506
  return info;
445
507
  };
508
+ const getReExports = (sourceFile, source) => {
509
+ const key = stripQuery(sourceFile);
510
+ const cachedReExports = reExportsCache.get(key);
511
+ if (cachedReExports) return cachedReExports;
512
+ const reExports = extractReExports(key, source ?? fs.readFileSync(key, "utf-8"));
513
+ reExportsCache.set(key, reExports);
514
+ return reExports;
515
+ };
446
516
  return {
447
517
  name: "data-source-lazy",
448
518
  enforce: "pre",
@@ -485,12 +555,42 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
485
555
  const usages = extractUsages(id, code);
486
556
  if (!info && usages.length === 0) return null;
487
557
  const filteredUsages = info ? dropAccessesInsideHocArgs(info, usages) : usages;
558
+ const resolveHocSourceFile = async (file, importedName, visited = /* @__PURE__ */ new Set()) => {
559
+ const visitKey = `${file}\0${importedName}`;
560
+ if (visited.has(visitKey)) return null;
561
+ visited.add(visitKey);
562
+ const directInfo = getHocInfo(file);
563
+ if (directInfo && directInfo.exportedName === importedName) return {
564
+ file,
565
+ info: directInfo
566
+ };
567
+ const reExports = getReExports(file);
568
+ const named = reExports.named.get(importedName);
569
+ if (named) {
570
+ const resolved = await resolveSourceFile(this, named.source, file);
571
+ if (resolved) {
572
+ const next = await resolveHocSourceFile(resolved, named.importedName, visited);
573
+ if (next) return next;
574
+ }
575
+ }
576
+ for (const starSource of reExports.stars) {
577
+ const resolved = await resolveSourceFile(this, starSource, file);
578
+ if (!resolved) continue;
579
+ const next = await resolveHocSourceFile(resolved, importedName, visited);
580
+ if (next) return next;
581
+ }
582
+ return null;
583
+ };
488
584
  const verifiedUsages = (await Promise.all(filteredUsages.map(async (usage) => {
489
585
  const resolvedSourceFile = await resolveSourceFile(this, usage.decl.source, id);
490
586
  if (!resolvedSourceFile) return null;
491
- const usageInfo = getHocInfo(resolvedSourceFile);
492
- if (!usageInfo || usageInfo.exportedName !== usage.spec.importedName) return null;
493
- return usage;
587
+ const target = await resolveHocSourceFile(resolvedSourceFile, usage.spec.importedName);
588
+ if (!target) return null;
589
+ return {
590
+ usage,
591
+ hocImportSource: toRelativeImportSource(id, target.file),
592
+ hocExportedName: target.info.exportedName
593
+ };
494
594
  }))).filter((usage) => usage !== null);
495
595
  if (!info && verifiedUsages.length === 0) return null;
496
596
  const s = new MagicString(code);
@@ -507,7 +607,9 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
507
607
  }
508
608
  },
509
609
  watchChange(id) {
510
- hocInfoCache.delete(stripQuery(id));
610
+ const key = stripQuery(id);
611
+ hocInfoCache.delete(key);
612
+ reExportsCache.delete(key);
511
613
  }
512
614
  };
513
615
  };
@@ -2,7 +2,7 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
- const require_factory = require("./factory-CmOH3Kh6.cjs");
5
+ const require_factory = require("./factory-Dq6kUGl-.cjs");
6
6
  //#region src/plugin/index.ts
7
7
  const DataSourceLazyPlugin = /* @__PURE__ */ (0, require("unplugin").createUnplugin)(require_factory.dataSourceLazyUnpluginFactory);
8
8
  //#endregion
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory, t as DEFAULT_HOCS } from "./factory--H-amlua.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory, t as DEFAULT_HOCS } from "./factory-Ds3_-8kv.mjs";
2
2
  import { createUnplugin } from "unplugin";
3
3
  //#region src/plugin/index.ts
4
4
  const DataSourceLazyPlugin = /* @__PURE__ */ createUnplugin(dataSourceLazyUnpluginFactory);
@@ -1,4 +1,4 @@
1
- const require_factory = require("./factory-CmOH3Kh6.cjs");
1
+ const require_factory = require("./factory-Dq6kUGl-.cjs");
2
2
  //#region src/plugin/rollup.ts
3
3
  var rollup_default = (0, require("unplugin").createRollupPlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
4
  //#endregion
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory--H-amlua.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-Ds3_-8kv.mjs";
2
2
  import { createRollupPlugin } from "unplugin";
3
3
  //#region src/plugin/rollup.ts
4
4
  var rollup_default = createRollupPlugin(dataSourceLazyUnpluginFactory);
@@ -1,4 +1,4 @@
1
- const require_factory = require("./factory-CmOH3Kh6.cjs");
1
+ const require_factory = require("./factory-Dq6kUGl-.cjs");
2
2
  //#region src/plugin/rspack.ts
3
3
  var rspack_default = (0, require("unplugin").createRspackPlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
4
  //#endregion
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory--H-amlua.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-Ds3_-8kv.mjs";
2
2
  import { createRspackPlugin } from "unplugin";
3
3
  //#region src/plugin/rspack.ts
4
4
  var rspack_default = createRspackPlugin(dataSourceLazyUnpluginFactory);
@@ -1,4 +1,4 @@
1
- const require_factory = require("./factory-CmOH3Kh6.cjs");
1
+ const require_factory = require("./factory-Dq6kUGl-.cjs");
2
2
  //#region src/plugin/vite.ts
3
3
  var vite_default = (0, require("unplugin").createVitePlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
4
  //#endregion
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory--H-amlua.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-Ds3_-8kv.mjs";
2
2
  import { createVitePlugin } from "unplugin";
3
3
  //#region src/plugin/vite.ts
4
4
  var vite_default = createVitePlugin(dataSourceLazyUnpluginFactory);
@@ -1,4 +1,4 @@
1
- const require_factory = require("./factory-CmOH3Kh6.cjs");
1
+ const require_factory = require("./factory-Dq6kUGl-.cjs");
2
2
  //#region src/plugin/webpack.ts
3
3
  var webpack_default = (0, require("unplugin").createWebpackPlugin)(require_factory.dataSourceLazyUnpluginFactory);
4
4
  //#endregion
@@ -1,4 +1,4 @@
1
- import { n as dataSourceLazyUnpluginFactory } from "./factory--H-amlua.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-Ds3_-8kv.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.5",
3
+ "version": "0.10.0-alpha.7",
4
4
  "description": "A wrapper around data fetching",
5
5
  "keywords": [
6
6
  "data-fetching",