@gravity-ui/data-source 0.10.0-alpha.8 → 0.10.0-alpha.9

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-GDKkqVwg.cjs");
1
+ const require_factory = require("./factory-amr4dgO8.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-Dwr-yuOT.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-BxKix--T.mjs";
2
2
  import { createEsbuildPlugin } from "unplugin";
3
3
  //#region src/plugin/esbuild.ts
4
4
  var esbuild_default = createEsbuildPlugin(dataSourceLazyUnpluginFactory);
@@ -318,16 +318,30 @@ function parseProgram(filename, source) {
318
318
  }
319
319
  //#endregion
320
320
  //#region src/plugin/core/generate.ts
321
- function generateAuxModule(sourceFile, exportedName, info, type, options = {}) {
321
+ function assembleAuxModuleSource(exportedName, info, type) {
322
322
  const suffix = COMPANION_TYPES[type];
323
- const cleanSourceFile = stripQuery(sourceFile);
324
- const code = [
323
+ return [
325
324
  renderImports(info.imports),
326
325
  "",
327
326
  `export const ${exportedName}${suffix} = ${info.argSource};`,
328
327
  ""
329
328
  ].join("\n").trimStart();
330
- return transformJsx(makeCompanionId(type, cleanSourceFile), code, options);
329
+ }
330
+ function compileAuxJsx(filename, code, options = {}) {
331
+ const result = transformSync(filename, code, {
332
+ lang: "tsx",
333
+ jsx: options.jsx,
334
+ target: options.target,
335
+ sourcemap: true
336
+ });
337
+ if (result.errors.length > 0) {
338
+ const error = result.errors[0];
339
+ throw new Error(error.codeframe ?? error.message);
340
+ }
341
+ return {
342
+ code: result.code,
343
+ map: JSON.stringify(result.map)
344
+ };
331
345
  }
332
346
  function generateLazyModule(sourceFile, info, options = {}) {
333
347
  const importSource = options.jsx?.importSource ?? "react";
@@ -357,22 +371,6 @@ function generateLazyModule(sourceFile, info, options = {}) {
357
371
  }).toString()
358
372
  };
359
373
  }
360
- function transformJsx(filename, code, options) {
361
- const result = transformSync(filename, code, {
362
- lang: "tsx",
363
- jsx: options.jsx,
364
- target: options.target,
365
- sourcemap: true
366
- });
367
- if (result.errors.length > 0) {
368
- const error = result.errors[0];
369
- throw new Error(error.codeframe ?? error.message);
370
- }
371
- return {
372
- code: result.code,
373
- map: JSON.stringify(result.map)
374
- };
375
- }
376
374
  function renderImport(decl) {
377
375
  const parts = [];
378
376
  const named = [];
@@ -518,6 +516,45 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
518
516
  reExportsCache.set(key, reExports);
519
517
  return reExports;
520
518
  };
519
+ const resolveHocSourceFile = async (ctx, file, importedName, visited = /* @__PURE__ */ new Set()) => {
520
+ const visitKey = `${file}\0${importedName}`;
521
+ if (visited.has(visitKey)) return null;
522
+ visited.add(visitKey);
523
+ const directInfo = getHocInfo(file);
524
+ if (directInfo && directInfo.exportedName === importedName) return {
525
+ file,
526
+ info: directInfo
527
+ };
528
+ const reExports = getReExports(file);
529
+ const named = reExports.named.get(importedName);
530
+ if (named) {
531
+ const resolved = await resolveSourceFile(ctx, named.source, file);
532
+ if (resolved) {
533
+ const next = await resolveHocSourceFile(ctx, resolved, named.importedName, visited);
534
+ if (next) return next;
535
+ }
536
+ }
537
+ for (const starSource of reExports.stars) {
538
+ const resolved = await resolveSourceFile(ctx, starSource, file);
539
+ if (!resolved) continue;
540
+ const next = await resolveHocSourceFile(ctx, resolved, importedName, visited);
541
+ if (next) return next;
542
+ }
543
+ return null;
544
+ };
545
+ const validateUsages = async (ctx, usages, importerId) => {
546
+ return (await Promise.all(usages.map(async (usage) => {
547
+ const resolvedSourceFile = await resolveSourceFile(ctx, usage.decl.source, importerId);
548
+ if (!resolvedSourceFile) return null;
549
+ const target = await resolveHocSourceFile(ctx, resolvedSourceFile, usage.spec.importedName);
550
+ if (!target) return null;
551
+ return {
552
+ usage,
553
+ hocImportSource: toRelativeImportSource(importerId, target.file),
554
+ hocExportedName: target.info.exportedName
555
+ };
556
+ }))).filter((usage) => usage !== null);
557
+ };
521
558
  return {
522
559
  name: "data-source-lazy",
523
560
  enforce: "pre",
@@ -536,7 +573,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
536
573
  },
537
574
  load: {
538
575
  filter: { id: new RegExp(VIRTUAL_PREFIX) },
539
- handler(id) {
576
+ async handler(id) {
540
577
  const parsedId = parseVirtualId(id);
541
578
  if (!parsedId) return null;
542
579
  this.addWatchFile(parsedId.sourceFile);
@@ -545,7 +582,18 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
545
582
  if (parsedId.type === "loading" || parsedId.type === "error") {
546
583
  const auxInfo = info[parsedId.type];
547
584
  if (!auxInfo) return null;
548
- return generateAuxModule(parsedId.sourceFile, info.exportedName, auxInfo, parsedId.type, options.generateOptions);
585
+ const cleanSourceFile = stripQuery(parsedId.sourceFile);
586
+ const auxFilename = makeCompanionId(parsedId.type, cleanSourceFile);
587
+ const assembled = assembleAuxModuleSource(info.exportedName, auxInfo, parsedId.type);
588
+ const usages = extractUsages(auxFilename, assembled);
589
+ const verifiedUsages = await validateUsages(this, usages, parsedId.sourceFile);
590
+ let auxCode = assembled;
591
+ if (verifiedUsages.length > 0) {
592
+ const s = new MagicString(assembled);
593
+ transformUsages(s, verifiedUsages);
594
+ auxCode = s.toString();
595
+ }
596
+ return compileAuxJsx(auxFilename, auxCode, options.generateOptions);
549
597
  }
550
598
  if (parsedId.type === "lazy") return generateLazyModule(parsedId.sourceFile, info, options.generateOptions);
551
599
  return assertNever(parsedId.type);
@@ -561,43 +609,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
561
609
  const usages = extractUsages(id, code);
562
610
  if (!info && usages.length === 0) return null;
563
611
  const filteredUsages = info ? dropAccessesInsideHocArgs(info, usages) : usages;
564
- const resolveHocSourceFile = async (file, importedName, visited = /* @__PURE__ */ new Set()) => {
565
- const visitKey = `${file}\0${importedName}`;
566
- if (visited.has(visitKey)) return null;
567
- visited.add(visitKey);
568
- const directInfo = getHocInfo(file);
569
- if (directInfo && directInfo.exportedName === importedName) return {
570
- file,
571
- info: directInfo
572
- };
573
- const reExports = getReExports(file);
574
- const named = reExports.named.get(importedName);
575
- if (named) {
576
- const resolved = await resolveSourceFile(this, named.source, file);
577
- if (resolved) {
578
- const next = await resolveHocSourceFile(resolved, named.importedName, visited);
579
- if (next) return next;
580
- }
581
- }
582
- for (const starSource of reExports.stars) {
583
- const resolved = await resolveSourceFile(this, starSource, file);
584
- if (!resolved) continue;
585
- const next = await resolveHocSourceFile(resolved, importedName, visited);
586
- if (next) return next;
587
- }
588
- return null;
589
- };
590
- const verifiedUsages = (await Promise.all(filteredUsages.map(async (usage) => {
591
- const resolvedSourceFile = await resolveSourceFile(this, usage.decl.source, id);
592
- if (!resolvedSourceFile) return null;
593
- const target = await resolveHocSourceFile(resolvedSourceFile, usage.spec.importedName);
594
- if (!target) return null;
595
- return {
596
- usage,
597
- hocImportSource: toRelativeImportSource(id, target.file),
598
- hocExportedName: target.info.exportedName
599
- };
600
- }))).filter((usage) => usage !== null);
612
+ const verifiedUsages = await validateUsages(this, filteredUsages, id);
601
613
  if (!info && verifiedUsages.length === 0) return null;
602
614
  const s = new MagicString(code);
603
615
  if (info) transformDefinitionModule(s, id, info);
@@ -343,16 +343,30 @@ function parseProgram(filename, source) {
343
343
  }
344
344
  //#endregion
345
345
  //#region src/plugin/core/generate.ts
346
- function generateAuxModule(sourceFile, exportedName, info, type, options = {}) {
346
+ function assembleAuxModuleSource(exportedName, info, type) {
347
347
  const suffix = COMPANION_TYPES[type];
348
- const cleanSourceFile = stripQuery(sourceFile);
349
- const code = [
348
+ return [
350
349
  renderImports(info.imports),
351
350
  "",
352
351
  `export const ${exportedName}${suffix} = ${info.argSource};`,
353
352
  ""
354
353
  ].join("\n").trimStart();
355
- return transformJsx(makeCompanionId(type, cleanSourceFile), code, options);
354
+ }
355
+ function compileAuxJsx(filename, code, options = {}) {
356
+ const result = (0, oxc_transform.transformSync)(filename, code, {
357
+ lang: "tsx",
358
+ jsx: options.jsx,
359
+ target: options.target,
360
+ sourcemap: true
361
+ });
362
+ if (result.errors.length > 0) {
363
+ const error = result.errors[0];
364
+ throw new Error(error.codeframe ?? error.message);
365
+ }
366
+ return {
367
+ code: result.code,
368
+ map: JSON.stringify(result.map)
369
+ };
356
370
  }
357
371
  function generateLazyModule(sourceFile, info, options = {}) {
358
372
  const importSource = options.jsx?.importSource ?? "react";
@@ -382,22 +396,6 @@ function generateLazyModule(sourceFile, info, options = {}) {
382
396
  }).toString()
383
397
  };
384
398
  }
385
- function transformJsx(filename, code, options) {
386
- const result = (0, oxc_transform.transformSync)(filename, code, {
387
- lang: "tsx",
388
- jsx: options.jsx,
389
- target: options.target,
390
- sourcemap: true
391
- });
392
- if (result.errors.length > 0) {
393
- const error = result.errors[0];
394
- throw new Error(error.codeframe ?? error.message);
395
- }
396
- return {
397
- code: result.code,
398
- map: JSON.stringify(result.map)
399
- };
400
- }
401
399
  function renderImport(decl) {
402
400
  const parts = [];
403
401
  const named = [];
@@ -543,6 +541,45 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
543
541
  reExportsCache.set(key, reExports);
544
542
  return reExports;
545
543
  };
544
+ const resolveHocSourceFile = async (ctx, file, importedName, visited = /* @__PURE__ */ new Set()) => {
545
+ const visitKey = `${file}\0${importedName}`;
546
+ if (visited.has(visitKey)) return null;
547
+ visited.add(visitKey);
548
+ const directInfo = getHocInfo(file);
549
+ if (directInfo && directInfo.exportedName === importedName) return {
550
+ file,
551
+ info: directInfo
552
+ };
553
+ const reExports = getReExports(file);
554
+ const named = reExports.named.get(importedName);
555
+ if (named) {
556
+ const resolved = await resolveSourceFile(ctx, named.source, file);
557
+ if (resolved) {
558
+ const next = await resolveHocSourceFile(ctx, resolved, named.importedName, visited);
559
+ if (next) return next;
560
+ }
561
+ }
562
+ for (const starSource of reExports.stars) {
563
+ const resolved = await resolveSourceFile(ctx, starSource, file);
564
+ if (!resolved) continue;
565
+ const next = await resolveHocSourceFile(ctx, resolved, importedName, visited);
566
+ if (next) return next;
567
+ }
568
+ return null;
569
+ };
570
+ const validateUsages = async (ctx, usages, importerId) => {
571
+ return (await Promise.all(usages.map(async (usage) => {
572
+ const resolvedSourceFile = await resolveSourceFile(ctx, usage.decl.source, importerId);
573
+ if (!resolvedSourceFile) return null;
574
+ const target = await resolveHocSourceFile(ctx, resolvedSourceFile, usage.spec.importedName);
575
+ if (!target) return null;
576
+ return {
577
+ usage,
578
+ hocImportSource: toRelativeImportSource(importerId, target.file),
579
+ hocExportedName: target.info.exportedName
580
+ };
581
+ }))).filter((usage) => usage !== null);
582
+ };
546
583
  return {
547
584
  name: "data-source-lazy",
548
585
  enforce: "pre",
@@ -561,7 +598,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
561
598
  },
562
599
  load: {
563
600
  filter: { id: new RegExp(VIRTUAL_PREFIX) },
564
- handler(id) {
601
+ async handler(id) {
565
602
  const parsedId = parseVirtualId(id);
566
603
  if (!parsedId) return null;
567
604
  this.addWatchFile(parsedId.sourceFile);
@@ -570,7 +607,18 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
570
607
  if (parsedId.type === "loading" || parsedId.type === "error") {
571
608
  const auxInfo = info[parsedId.type];
572
609
  if (!auxInfo) return null;
573
- return generateAuxModule(parsedId.sourceFile, info.exportedName, auxInfo, parsedId.type, options.generateOptions);
610
+ const cleanSourceFile = stripQuery(parsedId.sourceFile);
611
+ const auxFilename = makeCompanionId(parsedId.type, cleanSourceFile);
612
+ const assembled = assembleAuxModuleSource(info.exportedName, auxInfo, parsedId.type);
613
+ const usages = extractUsages(auxFilename, assembled);
614
+ const verifiedUsages = await validateUsages(this, usages, parsedId.sourceFile);
615
+ let auxCode = assembled;
616
+ if (verifiedUsages.length > 0) {
617
+ const s = new magic_string.default(assembled);
618
+ transformUsages(s, verifiedUsages);
619
+ auxCode = s.toString();
620
+ }
621
+ return compileAuxJsx(auxFilename, auxCode, options.generateOptions);
574
622
  }
575
623
  if (parsedId.type === "lazy") return generateLazyModule(parsedId.sourceFile, info, options.generateOptions);
576
624
  return assertNever(parsedId.type);
@@ -586,43 +634,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
586
634
  const usages = extractUsages(id, code);
587
635
  if (!info && usages.length === 0) return null;
588
636
  const filteredUsages = info ? dropAccessesInsideHocArgs(info, usages) : usages;
589
- const resolveHocSourceFile = async (file, importedName, visited = /* @__PURE__ */ new Set()) => {
590
- const visitKey = `${file}\0${importedName}`;
591
- if (visited.has(visitKey)) return null;
592
- visited.add(visitKey);
593
- const directInfo = getHocInfo(file);
594
- if (directInfo && directInfo.exportedName === importedName) return {
595
- file,
596
- info: directInfo
597
- };
598
- const reExports = getReExports(file);
599
- const named = reExports.named.get(importedName);
600
- if (named) {
601
- const resolved = await resolveSourceFile(this, named.source, file);
602
- if (resolved) {
603
- const next = await resolveHocSourceFile(resolved, named.importedName, visited);
604
- if (next) return next;
605
- }
606
- }
607
- for (const starSource of reExports.stars) {
608
- const resolved = await resolveSourceFile(this, starSource, file);
609
- if (!resolved) continue;
610
- const next = await resolveHocSourceFile(resolved, importedName, visited);
611
- if (next) return next;
612
- }
613
- return null;
614
- };
615
- const verifiedUsages = (await Promise.all(filteredUsages.map(async (usage) => {
616
- const resolvedSourceFile = await resolveSourceFile(this, usage.decl.source, id);
617
- if (!resolvedSourceFile) return null;
618
- const target = await resolveHocSourceFile(resolvedSourceFile, usage.spec.importedName);
619
- if (!target) return null;
620
- return {
621
- usage,
622
- hocImportSource: toRelativeImportSource(id, target.file),
623
- hocExportedName: target.info.exportedName
624
- };
625
- }))).filter((usage) => usage !== null);
637
+ const verifiedUsages = await validateUsages(this, filteredUsages, id);
626
638
  if (!info && verifiedUsages.length === 0) return null;
627
639
  const s = new magic_string.default(code);
628
640
  if (info) transformDefinitionModule(s, id, info);
@@ -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-GDKkqVwg.cjs");
5
+ const require_factory = require("./factory-amr4dgO8.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-Dwr-yuOT.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory, t as DEFAULT_HOCS } from "./factory-BxKix--T.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-GDKkqVwg.cjs");
1
+ const require_factory = require("./factory-amr4dgO8.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-Dwr-yuOT.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-BxKix--T.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-GDKkqVwg.cjs");
1
+ const require_factory = require("./factory-amr4dgO8.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-Dwr-yuOT.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-BxKix--T.mjs";
2
2
  import { createRspackPlugin } from "unplugin";
3
3
  //#region src/plugin/rspack.ts
4
4
  var rspack_default = createRspackPlugin(dataSourceLazyUnpluginFactory);
@@ -1,6 +1,10 @@
1
1
  import type {ComponentType} from 'react';
2
2
 
3
3
  declare module '@gravity-ui/data-source' {
4
+ export interface AsyncComponent<TProps extends object = {}, TLoadingProps extends object = {}> {
5
+ Lazy: ComponentType<TProps & TLoadingProps>;
6
+ }
7
+
4
8
  interface AsyncBoundaryComponent<
5
9
  TProps extends object = {},
6
10
  TLoadingProps extends object = {},
@@ -1,4 +1,4 @@
1
- const require_factory = require("./factory-GDKkqVwg.cjs");
1
+ const require_factory = require("./factory-amr4dgO8.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-Dwr-yuOT.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-BxKix--T.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-GDKkqVwg.cjs");
1
+ const require_factory = require("./factory-amr4dgO8.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-Dwr-yuOT.mjs";
1
+ import { n as dataSourceLazyUnpluginFactory } from "./factory-BxKix--T.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.8",
3
+ "version": "0.10.0-alpha.9",
4
4
  "description": "A wrapper around data fetching",
5
5
  "keywords": [
6
6
  "data-fetching",