@gravity-ui/data-source 0.10.0-alpha.1 → 0.10.0-alpha.11
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/build/cjs/react/components/Async/Async.d.ts +3 -0
- package/build/cjs/react/components/Async/Async.js +18 -0
- package/build/cjs/react/components/Async/Async.js.map +1 -0
- package/build/cjs/react/components/Async/index.d.ts +3 -0
- package/build/cjs/react/components/Async/index.js +20 -0
- package/build/cjs/react/components/Async/index.js.map +1 -0
- package/build/cjs/react/components/Async/types.d.ts +9 -0
- package/build/cjs/react/components/Async/types.js +6 -0
- package/build/cjs/react/components/Async/types.js.map +1 -0
- package/build/cjs/react/components/Async/withAsync.d.ts +3 -0
- package/build/cjs/react/components/Async/withAsync.js +26 -0
- package/build/cjs/react/components/Async/withAsync.js.map +1 -0
- package/build/cjs/react/components/AsyncBoundary/AsyncBoundary.js +3 -2
- package/build/cjs/react/components/AsyncBoundary/AsyncBoundary.js.map +1 -1
- package/build/cjs/react/components/AsyncBoundary/types.d.ts +3 -3
- package/build/cjs/react/components/AsyncBoundary/types.js.map +1 -1
- package/build/cjs/react/components/AsyncBoundary/withAsyncBoundary.d.ts +3 -2
- package/build/cjs/react/components/AsyncBoundary/withAsyncBoundary.js +9 -5
- package/build/cjs/react/components/AsyncBoundary/withAsyncBoundary.js.map +1 -1
- package/build/cjs/react/index.d.ts +1 -0
- package/build/cjs/react/index.js +12 -0
- package/build/cjs/react/index.js.map +1 -1
- package/build/cjs/react-query/components/QueryAsyncBoundary/types.d.ts +5 -4
- package/build/cjs/react-query/components/QueryAsyncBoundary/types.js.map +1 -1
- package/build/cjs/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.d.ts +3 -2
- package/build/cjs/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js +9 -5
- package/build/cjs/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js.map +1 -1
- package/build/esm/react/components/Async/Async.d.ts +3 -0
- package/build/esm/react/components/Async/Async.js +11 -0
- package/build/esm/react/components/Async/Async.js.map +1 -0
- package/build/esm/react/components/Async/index.d.ts +3 -0
- package/build/esm/react/components/Async/index.js +3 -0
- package/build/esm/react/components/Async/index.js.map +1 -0
- package/build/esm/react/components/Async/types.d.ts +9 -0
- package/build/esm/react/components/Async/types.js +2 -0
- package/build/esm/react/components/Async/types.js.map +1 -0
- package/build/esm/react/components/Async/withAsync.d.ts +3 -0
- package/build/esm/react/components/Async/withAsync.js +19 -0
- package/build/esm/react/components/Async/withAsync.js.map +1 -0
- package/build/esm/react/components/AsyncBoundary/AsyncBoundary.js +3 -2
- package/build/esm/react/components/AsyncBoundary/AsyncBoundary.js.map +1 -1
- package/build/esm/react/components/AsyncBoundary/types.d.ts +3 -3
- package/build/esm/react/components/AsyncBoundary/types.js.map +1 -1
- package/build/esm/react/components/AsyncBoundary/withAsyncBoundary.d.ts +3 -2
- package/build/esm/react/components/AsyncBoundary/withAsyncBoundary.js +9 -5
- package/build/esm/react/components/AsyncBoundary/withAsyncBoundary.js.map +1 -1
- package/build/esm/react/index.d.ts +1 -0
- package/build/esm/react/index.js +1 -0
- package/build/esm/react/index.js.map +1 -1
- package/build/esm/react-query/components/QueryAsyncBoundary/types.d.ts +5 -4
- package/build/esm/react-query/components/QueryAsyncBoundary/types.js.map +1 -1
- package/build/esm/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.d.ts +3 -2
- package/build/esm/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js +9 -5
- package/build/esm/react-query/components/QueryAsyncBoundary/withQueryAsyncBoundary.js.map +1 -1
- package/build/plugin/esbuild.cjs +1 -1
- package/build/plugin/esbuild.mjs +1 -1
- package/build/plugin/{factory-2tSt4Rte.mjs → factory-BdwLvQEp.mjs} +212 -61
- package/build/plugin/{factory-Cdmba_F_.cjs → factory-TMKD3PiJ.cjs} +212 -61
- package/build/plugin/index.cjs +1 -1
- package/build/plugin/index.mjs +1 -1
- package/build/plugin/rollup.cjs +1 -1
- package/build/plugin/rollup.mjs +1 -1
- package/build/plugin/rspack.cjs +1 -1
- package/build/plugin/rspack.mjs +1 -1
- package/build/plugin/typings/client.d.ts +16 -4
- package/build/plugin/vite.cjs +1 -1
- package/build/plugin/vite.mjs +1 -1
- package/build/plugin/webpack.cjs +1 -1
- package/build/plugin/webpack.mjs +1 -1
- package/package.json +1 -4
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ComponentType } from 'react';
|
|
2
|
+
import type { AsyncBoundaryProps, ErrorViewProps } from '../../../react';
|
|
2
3
|
export interface QueryAsyncBoundaryProps extends AsyncBoundaryProps {
|
|
3
4
|
}
|
|
4
|
-
export interface QueryAsyncBoundaryComponent<TProps extends object> extends React.FC<TProps
|
|
5
|
+
export interface QueryAsyncBoundaryComponent<TProps extends object = {}, TLoadingProps extends object = {}, TErrorProps extends object = {}> extends React.FC<TProps & TLoadingProps & Omit<TErrorProps, keyof ErrorViewProps>> {
|
|
5
6
|
Content: React.ComponentType<TProps>;
|
|
6
|
-
Loading:
|
|
7
|
-
Error:
|
|
7
|
+
Loading: ComponentType<TLoadingProps & Partial<TProps>>;
|
|
8
|
+
Error: ComponentType<ErrorViewProps & TErrorProps & Partial<TProps>>;
|
|
8
9
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type {AsyncBoundaryProps} from '../../../react';\n\nexport interface QueryAsyncBoundaryProps extends AsyncBoundaryProps {}\n\nexport interface QueryAsyncBoundaryComponent
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type {ComponentType} from 'react';\n\nimport type {AsyncBoundaryProps, ErrorViewProps} from '../../../react';\n\nexport interface QueryAsyncBoundaryProps extends AsyncBoundaryProps {}\n\nexport interface QueryAsyncBoundaryComponent<\n TProps extends object = {},\n TLoadingProps extends object = {},\n TErrorProps extends object = {},\n> extends React.FC<TProps & TLoadingProps & Omit<TErrorProps, keyof ErrorViewProps>> {\n Content: React.ComponentType<TProps>;\n Loading: ComponentType<TLoadingProps & Partial<TProps>>;\n Error: ComponentType<ErrorViewProps & TErrorProps & Partial<TProps>>;\n}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
2
|
+
import type { ErrorViewProps } from '../../../react';
|
|
3
|
+
import type { QueryAsyncBoundaryComponent } from './types';
|
|
4
|
+
export declare const withQueryAsyncBoundary: <TProps extends object = {}, TLoadingProps extends object = {}, TErrorProps extends object = {}>(Component: React.ComponentType<TProps>, LoadingView: React.ComponentType<TLoadingProps & Partial<TProps>>, ErrorView: React.ComponentType<ErrorViewProps & TErrorProps & Partial<TProps>>) => QueryAsyncBoundaryComponent<TProps, TLoadingProps, TErrorProps>;
|
|
@@ -2,18 +2,22 @@ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import { QueryAsyncBoundary } from './QueryAsyncBoundary';
|
|
4
4
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
|
-
export var withQueryAsyncBoundary = function withQueryAsyncBoundary(Component,
|
|
5
|
+
export var withQueryAsyncBoundary = function withQueryAsyncBoundary(Component, _LoadingView, _ErrorView) {
|
|
6
6
|
var WrappedComponent = function WrappedComponent(props) {
|
|
7
7
|
return /*#__PURE__*/_jsx(QueryAsyncBoundary, {
|
|
8
|
-
LoadingView: LoadingView
|
|
9
|
-
|
|
8
|
+
LoadingView: function LoadingView() {
|
|
9
|
+
return /*#__PURE__*/_jsx(_LoadingView, _objectSpread({}, props));
|
|
10
|
+
},
|
|
11
|
+
ErrorView: function ErrorView(errorProps) {
|
|
12
|
+
return /*#__PURE__*/_jsx(_ErrorView, _objectSpread(_objectSpread({}, props), errorProps));
|
|
13
|
+
},
|
|
10
14
|
children: /*#__PURE__*/_jsx(Component, _objectSpread({}, props))
|
|
11
15
|
});
|
|
12
16
|
};
|
|
13
17
|
WrappedComponent.displayName = "WithQueryAsyncBoundary(".concat(Component.displayName || Component.name, ")");
|
|
14
18
|
WrappedComponent.Content = Component;
|
|
15
|
-
WrappedComponent.Loading =
|
|
16
|
-
WrappedComponent.Error =
|
|
19
|
+
WrappedComponent.Loading = _LoadingView;
|
|
20
|
+
WrappedComponent.Error = _ErrorView;
|
|
17
21
|
return WrappedComponent;
|
|
18
22
|
};
|
|
19
23
|
// #sourceMappingURL=withQueryAsyncBoundary.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["React","QueryAsyncBoundary","jsx","_jsx","withQueryAsyncBoundary","Component","LoadingView","ErrorView","WrappedComponent","props","
|
|
1
|
+
{"version":3,"names":["React","QueryAsyncBoundary","jsx","_jsx","withQueryAsyncBoundary","Component","LoadingView","ErrorView","WrappedComponent","props","_LoadingView","_objectSpread","errorProps","_ErrorView","children","displayName","concat","name","Content","Loading","Error"],"sources":["withQueryAsyncBoundary.tsx"],"sourcesContent":["import React from 'react';\n\nimport type {ErrorViewProps} from '../../../react';\n\nimport {QueryAsyncBoundary} from './QueryAsyncBoundary';\nimport type {QueryAsyncBoundaryComponent} from './types';\n\nexport const withQueryAsyncBoundary = <\n TProps extends object = {},\n TLoadingProps extends object = {},\n TErrorProps extends object = {},\n>(\n Component: React.ComponentType<TProps>,\n LoadingView: React.ComponentType<TLoadingProps & Partial<TProps>>,\n ErrorView: React.ComponentType<ErrorViewProps & TErrorProps & Partial<TProps>>,\n) => {\n const WrappedComponent = ((props: TProps & TLoadingProps & TErrorProps) => (\n <QueryAsyncBoundary\n LoadingView={() => <LoadingView {...props} />}\n ErrorView={(errorProps) => <ErrorView {...props} {...errorProps} />}\n >\n <Component {...props} />\n </QueryAsyncBoundary>\n )) as QueryAsyncBoundaryComponent<TProps, TLoadingProps, TErrorProps>;\n\n WrappedComponent.displayName = `WithQueryAsyncBoundary(${\n Component.displayName || Component.name\n })`;\n\n WrappedComponent.Content = Component;\n WrappedComponent.Loading = LoadingView;\n WrappedComponent.Error = ErrorView;\n\n return WrappedComponent;\n};\n"],"mappings":";AAAA,OAAOA,KAAK,MAAM,OAAO;AAIzB,SAAQC,kBAAkB,QAAO,sBAAsB;AAAC,SAAAC,GAAA,IAAAC,IAAA;AAGxD,OAAO,IAAMC,sBAAsB,GAAG,SAAzBA,sBAAsBA,CAK/BC,SAAsC,EACtCC,YAAiE,EACjEC,UAA8E,EAC7E;EACD,IAAMC,gBAAgB,GAAI,SAApBA,gBAAgBA,CAAKC,KAA2C;IAAA,oBAClEN,IAAA,CAACF,kBAAkB;MACfK,WAAW,EAAE,SAAbA,WAAWA,CAAA;QAAA,oBAAQH,IAAA,CAACO,YAAW,EAAAC,aAAA,KAAKF,KAAK,CAAG,CAAC;MAAA,CAAC;MAC9CF,SAAS,EAAE,SAAXA,SAASA,CAAGK,UAAU;QAAA,oBAAKT,IAAA,CAACU,UAAS,EAAAF,aAAA,CAAAA,aAAA,KAAKF,KAAK,GAAMG,UAAU,CAAG,CAAC;MAAA,CAAC;MAAAE,QAAA,eAEpEX,IAAA,CAACE,SAAS,EAAAM,aAAA,KAAKF,KAAK,CAAG;IAAC,CACR,CAAC;EAAA,CAC4C;EAErED,gBAAgB,CAACO,WAAW,6BAAAC,MAAA,CACxBX,SAAS,CAACU,WAAW,IAAIV,SAAS,CAACY,IAAI,MACxC;EAEHT,gBAAgB,CAACU,OAAO,GAAGb,SAAS;EACpCG,gBAAgB,CAACW,OAAO,GAAGb,YAAW;EACtCE,gBAAgB,CAACY,KAAK,GAAGb,UAAS;EAElC,OAAOC,gBAAgB;AAC3B,CAAC","ignoreList":[]}
|
package/build/plugin/esbuild.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_factory = require("./factory-
|
|
1
|
+
const require_factory = require("./factory-TMKD3PiJ.cjs");
|
|
2
2
|
//#region src/plugin/esbuild.ts
|
|
3
3
|
var esbuild_default = (0, require("unplugin").createEsbuildPlugin)(require_factory.dataSourceLazyUnpluginFactory);
|
|
4
4
|
//#endregion
|
package/build/plugin/esbuild.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as dataSourceLazyUnpluginFactory } from "./factory-
|
|
1
|
+
import { n as dataSourceLazyUnpluginFactory } from "./factory-BdwLvQEp.mjs";
|
|
2
2
|
import { createEsbuildPlugin } from "unplugin";
|
|
3
3
|
//#region src/plugin/esbuild.ts
|
|
4
4
|
var esbuild_default = createEsbuildPlugin(dataSourceLazyUnpluginFactory);
|
|
@@ -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
|
-
|
|
31
|
-
|
|
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
|
|
44
|
-
|
|
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
|
|
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 = [];
|
|
@@ -209,6 +272,9 @@ function extractUsages(filename, source) {
|
|
|
209
272
|
}
|
|
210
273
|
return result;
|
|
211
274
|
}
|
|
275
|
+
function isAccessInsideArg(arg, access) {
|
|
276
|
+
return arg.argStart <= access.start && access.end <= arg.argEnd;
|
|
277
|
+
}
|
|
212
278
|
function collectIdentifiers(node) {
|
|
213
279
|
const result = /* @__PURE__ */ new Set();
|
|
214
280
|
walk(node, (current) => {
|
|
@@ -256,39 +322,33 @@ function parseProgram(filename, source) {
|
|
|
256
322
|
}
|
|
257
323
|
//#endregion
|
|
258
324
|
//#region src/plugin/core/generate.ts
|
|
259
|
-
function
|
|
260
|
-
const auxInfo = info[type];
|
|
261
|
-
const name = info.exportedName;
|
|
325
|
+
function generateAuxModuleCode(exportedName, info, type) {
|
|
262
326
|
const suffix = COMPANION_TYPES[type];
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
renderImports(auxInfo.imports),
|
|
327
|
+
return [
|
|
328
|
+
renderImports(info.imports),
|
|
266
329
|
"",
|
|
267
|
-
`export const ${
|
|
330
|
+
`export const ${exportedName}${suffix} = ${info.argSource};`,
|
|
268
331
|
""
|
|
269
332
|
].join("\n").trimStart();
|
|
270
|
-
return transformJsx(makeCompanionId(type, cleanSourceFile), code, options);
|
|
271
333
|
}
|
|
272
334
|
function generateLazyModule(sourceFile, info, options = {}) {
|
|
273
335
|
const importSource = options.jsx?.importSource ?? "react";
|
|
274
336
|
const name = info.exportedName;
|
|
275
337
|
const cleanSourceFile = stripQuery(sourceFile);
|
|
276
338
|
const base = `./${path.basename(cleanSourceFile, path.extname(cleanSourceFile))}`;
|
|
277
|
-
const
|
|
339
|
+
const hocBinding = info.hocImportedName === "default" ? info.hocLocalName : info.hocImportedName;
|
|
340
|
+
const lines = [
|
|
278
341
|
`import {lazy} from '${importSource}';`,
|
|
279
342
|
"",
|
|
280
343
|
info.hocImportedName === "default" ? `import ${info.hocLocalName} from '${info.hocImportSource}';` : `import {${info.hocImportedName}} from '${info.hocImportSource}';`,
|
|
281
344
|
"",
|
|
282
|
-
`import {${name}Loading} from '${base}.Loading'
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
`);`,
|
|
290
|
-
``
|
|
291
|
-
].join("\n");
|
|
345
|
+
`import {${name}Loading} from '${base}.Loading';`
|
|
346
|
+
];
|
|
347
|
+
if (info.error) lines.push(`import {${name}Error} from '${base}.Error';`);
|
|
348
|
+
lines.push("", `export const ${name}Lazy = ${hocBinding}(`, ` lazy(() => import('${base}').then((m) => ({default: m.${name}Content}))),`, ` ${name}Loading,`);
|
|
349
|
+
if (info.error) lines.push(` ${name}Error,`);
|
|
350
|
+
lines.push(`);`, ``);
|
|
351
|
+
const code = lines.join("\n");
|
|
292
352
|
const filename = makeCompanionId("lazy", cleanSourceFile);
|
|
293
353
|
return {
|
|
294
354
|
code,
|
|
@@ -299,7 +359,7 @@ function generateLazyModule(sourceFile, info, options = {}) {
|
|
|
299
359
|
}).toString()
|
|
300
360
|
};
|
|
301
361
|
}
|
|
302
|
-
function transformJsx(filename, code, options) {
|
|
362
|
+
function transformJsx(filename, code, options = {}) {
|
|
303
363
|
const result = transformSync(filename, code, {
|
|
304
364
|
lang: "tsx",
|
|
305
365
|
jsx: options.jsx,
|
|
@@ -365,30 +425,42 @@ function resolveWithCompiler(compiler, directory, request) {
|
|
|
365
425
|
}
|
|
366
426
|
//#endregion
|
|
367
427
|
//#region src/plugin/core/transform.ts
|
|
368
|
-
function transformDefinitionModule(s, filename, info) {
|
|
428
|
+
function transformDefinitionModule(s, filename, info, verifiedUsages) {
|
|
369
429
|
const name = info.exportedName;
|
|
370
430
|
const cleanFilename = stripQuery(filename);
|
|
371
431
|
const localSource = `./${path.basename(cleanFilename, path.extname(cleanFilename))}`;
|
|
372
|
-
|
|
432
|
+
const prependImports = [renderNamedImport(`${name}Loading`, `${name}Loading`, makeCompanionId("loading", localSource))];
|
|
433
|
+
if (info.error) prependImports.push(renderNamedImport(`${name}Error`, `${name}Error`, makeCompanionId("error", localSource)));
|
|
434
|
+
s.prepend(prependImports.join("\n") + "\n");
|
|
373
435
|
s.overwrite(info.loading.argStart, info.loading.argEnd, `${name}Loading`);
|
|
374
|
-
s.overwrite(info.error.argStart, info.error.argEnd, `${name}Error`);
|
|
436
|
+
if (info.error) s.overwrite(info.error.argStart, info.error.argEnd, `${name}Error`);
|
|
375
437
|
if (info.content.kind === "identifier") s.append(`\nexport {${info.content.name} as ${name}Content};\n`);
|
|
376
438
|
else {
|
|
377
|
-
|
|
439
|
+
const contentSource = verifiedUsages.length > 0 ? renderInlineContent(info.content, verifiedUsages) : info.content.argSource;
|
|
440
|
+
s.appendLeft(info.hocExportStart, `const ${name}Content = ${contentSource};\n\n`);
|
|
378
441
|
s.overwrite(info.content.argStart, info.content.argEnd, `${name}Content`);
|
|
379
442
|
s.append(`\nexport {${name}Content};\n`);
|
|
380
443
|
}
|
|
381
444
|
}
|
|
382
|
-
function
|
|
383
|
-
|
|
445
|
+
function renderInlineContent(content, usages) {
|
|
446
|
+
const s = new MagicString(content.argSource);
|
|
447
|
+
for (const { usage } of usages) for (const access of usage.accesses) if (isAccessInsideArg(content, access)) s.overwrite(access.start - content.argStart, access.end - content.argStart, `${usage.spec.localName}${access.prop}`);
|
|
448
|
+
return s.toString();
|
|
449
|
+
}
|
|
450
|
+
function transformUsages(s, usages, info) {
|
|
451
|
+
const inlineContent = info?.content.kind === "inline" ? info.content : null;
|
|
452
|
+
for (const { usage } of usages) for (const access of usage.accesses) {
|
|
453
|
+
if (inlineContent && isAccessInsideArg(inlineContent, access)) continue;
|
|
454
|
+
s.overwrite(access.start, access.end, `${usage.spec.localName}${access.prop}`);
|
|
455
|
+
}
|
|
384
456
|
const newImports = [];
|
|
385
|
-
for (const {
|
|
386
|
-
const propsUsed = new Set(accesses.map((access) => access.prop));
|
|
387
|
-
for (const prop of propsUsed) newImports.push(renderNamedImport(`${spec.localName}${prop}`, `${
|
|
457
|
+
for (const { usage, hocImportSource, hocExportedName } of usages) {
|
|
458
|
+
const propsUsed = new Set(usage.accesses.map((access) => access.prop));
|
|
459
|
+
for (const prop of propsUsed) newImports.push(renderNamedImport(`${usage.spec.localName}${prop}`, `${hocExportedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], hocImportSource)));
|
|
388
460
|
}
|
|
389
461
|
if (newImports.length > 0) s.prepend(newImports.join("\n") + "\n");
|
|
390
462
|
const localsByDecl = /* @__PURE__ */ new Map();
|
|
391
|
-
for (const usage of usages) {
|
|
463
|
+
for (const { usage } of usages) {
|
|
392
464
|
if (usage.hasOtherUsages) continue;
|
|
393
465
|
let locals = localsByDecl.get(usage.decl);
|
|
394
466
|
if (!locals) localsByDecl.set(usage.decl, locals = /* @__PURE__ */ new Set());
|
|
@@ -415,13 +487,20 @@ function renderNamedImport(local, imported, source) {
|
|
|
415
487
|
}
|
|
416
488
|
//#endregion
|
|
417
489
|
//#region src/plugin/core/factory.ts
|
|
418
|
-
const DEFAULT_HOCS = [
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
490
|
+
const DEFAULT_HOCS = [
|
|
491
|
+
{
|
|
492
|
+
from: "@gravity-ui/data-source",
|
|
493
|
+
name: "withAsync"
|
|
494
|
+
},
|
|
495
|
+
{
|
|
496
|
+
from: "@gravity-ui/data-source",
|
|
497
|
+
name: "withAsyncBoundary"
|
|
498
|
+
},
|
|
499
|
+
{
|
|
500
|
+
from: "@gravity-ui/data-source",
|
|
501
|
+
name: "withQueryAsyncBoundary"
|
|
502
|
+
}
|
|
503
|
+
];
|
|
425
504
|
const DEFAULT_INCLUDE = /\.(tsx?|jsx?)$/;
|
|
426
505
|
const VIRTUAL_EXCLUDE = new RegExp(`${VIRTUAL_PREFIX}|${encodeURIComponent(VIRTUAL_PREFIX)}`);
|
|
427
506
|
const dataSourceLazyUnpluginFactory = (options = {}) => {
|
|
@@ -433,6 +512,7 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
|
|
|
433
512
|
const hocsSet = new Set(hocPatterns.map((pattern) => getHocString(pattern.from, pattern.name)));
|
|
434
513
|
const hocsRegexp = new RegExp(Array.from(new Set(hocPatterns.map((pattern) => pattern.name))).map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"));
|
|
435
514
|
const hocInfoCache = /* @__PURE__ */ new Map();
|
|
515
|
+
const reExportsCache = /* @__PURE__ */ new Map();
|
|
436
516
|
const getHocInfo = (sourceFile, source) => {
|
|
437
517
|
const key = stripQuery(sourceFile);
|
|
438
518
|
const cachedInfo = hocInfoCache.get(key);
|
|
@@ -442,6 +522,53 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
|
|
|
442
522
|
hocInfoCache.set(key, info);
|
|
443
523
|
return info;
|
|
444
524
|
};
|
|
525
|
+
const getReExports = (sourceFile, source) => {
|
|
526
|
+
const key = stripQuery(sourceFile);
|
|
527
|
+
const cachedReExports = reExportsCache.get(key);
|
|
528
|
+
if (cachedReExports) return cachedReExports;
|
|
529
|
+
const reExports = extractReExports(key, source ?? fs.readFileSync(key, "utf-8"));
|
|
530
|
+
reExportsCache.set(key, reExports);
|
|
531
|
+
return reExports;
|
|
532
|
+
};
|
|
533
|
+
const resolveHocSourceFile = async (ctx, file, importedName, visited = /* @__PURE__ */ new Set()) => {
|
|
534
|
+
const visitKey = `${file}\0${importedName}`;
|
|
535
|
+
if (visited.has(visitKey)) return null;
|
|
536
|
+
visited.add(visitKey);
|
|
537
|
+
const directInfo = getHocInfo(file);
|
|
538
|
+
if (directInfo && directInfo.exportedName === importedName) return {
|
|
539
|
+
file,
|
|
540
|
+
info: directInfo
|
|
541
|
+
};
|
|
542
|
+
const reExports = getReExports(file);
|
|
543
|
+
const named = reExports.named.get(importedName);
|
|
544
|
+
if (named) {
|
|
545
|
+
const resolved = await resolveSourceFile(ctx, named.source, file);
|
|
546
|
+
if (resolved) {
|
|
547
|
+
const next = await resolveHocSourceFile(ctx, resolved, named.importedName, visited);
|
|
548
|
+
if (next) return next;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
for (const starSource of reExports.stars) {
|
|
552
|
+
const resolved = await resolveSourceFile(ctx, starSource, file);
|
|
553
|
+
if (!resolved) continue;
|
|
554
|
+
const next = await resolveHocSourceFile(ctx, resolved, importedName, visited);
|
|
555
|
+
if (next) return next;
|
|
556
|
+
}
|
|
557
|
+
return null;
|
|
558
|
+
};
|
|
559
|
+
const verifyUsages = async (ctx, usages, importerId) => {
|
|
560
|
+
return (await Promise.all(usages.map(async (usage) => {
|
|
561
|
+
const resolvedSourceFile = await resolveSourceFile(ctx, usage.decl.source, importerId);
|
|
562
|
+
if (!resolvedSourceFile) return null;
|
|
563
|
+
const target = await resolveHocSourceFile(ctx, resolvedSourceFile, usage.spec.importedName);
|
|
564
|
+
if (!target) return null;
|
|
565
|
+
return {
|
|
566
|
+
usage,
|
|
567
|
+
hocImportSource: toRelativeImportSource(importerId, target.file),
|
|
568
|
+
hocExportedName: target.info.exportedName
|
|
569
|
+
};
|
|
570
|
+
}))).filter((usage) => usage !== null);
|
|
571
|
+
};
|
|
445
572
|
return {
|
|
446
573
|
name: "data-source-lazy",
|
|
447
574
|
enforce: "pre",
|
|
@@ -460,18 +587,29 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
|
|
|
460
587
|
},
|
|
461
588
|
load: {
|
|
462
589
|
filter: { id: new RegExp(VIRTUAL_PREFIX) },
|
|
463
|
-
handler(id) {
|
|
590
|
+
async handler(id) {
|
|
464
591
|
const parsedId = parseVirtualId(id);
|
|
465
592
|
if (!parsedId) return null;
|
|
466
593
|
this.addWatchFile(parsedId.sourceFile);
|
|
467
594
|
const info = getHocInfo(parsedId.sourceFile);
|
|
468
595
|
if (!info) return null;
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
596
|
+
if (parsedId.type === "loading" || parsedId.type === "error") {
|
|
597
|
+
const auxInfo = info[parsedId.type];
|
|
598
|
+
if (!auxInfo) return null;
|
|
599
|
+
const filename = makeCompanionId(parsedId.type, parsedId.sourceFile);
|
|
600
|
+
const code = generateAuxModuleCode(info.exportedName, auxInfo, parsedId.type);
|
|
601
|
+
const usages = extractUsages(filename, code);
|
|
602
|
+
const verifiedUsages = await verifyUsages(this, usages, parsedId.sourceFile);
|
|
603
|
+
let resultCode = code;
|
|
604
|
+
if (verifiedUsages.length > 0) {
|
|
605
|
+
const s = new MagicString(code);
|
|
606
|
+
transformUsages(s, verifiedUsages);
|
|
607
|
+
resultCode = s.toString();
|
|
608
|
+
}
|
|
609
|
+
return transformJsx(filename, resultCode, options.generateOptions);
|
|
474
610
|
}
|
|
611
|
+
if (parsedId.type === "lazy") return generateLazyModule(parsedId.sourceFile, info, options.generateOptions);
|
|
612
|
+
return assertNever(parsedId.type);
|
|
475
613
|
}
|
|
476
614
|
},
|
|
477
615
|
transform: {
|
|
@@ -483,17 +621,12 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
|
|
|
483
621
|
const info = getHocInfo(id, code);
|
|
484
622
|
const usages = extractUsages(id, code);
|
|
485
623
|
if (!info && usages.length === 0) return null;
|
|
486
|
-
const
|
|
487
|
-
|
|
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);
|
|
624
|
+
const filteredUsages = info ? dropAccessesInsideHocArgs(info, usages) : usages;
|
|
625
|
+
const verifiedUsages = await verifyUsages(this, filteredUsages, id);
|
|
493
626
|
if (!info && verifiedUsages.length === 0) return null;
|
|
494
627
|
const s = new MagicString(code);
|
|
495
|
-
if (info) transformDefinitionModule(s, id, info);
|
|
496
|
-
if (verifiedUsages.length > 0) transformUsages(s, verifiedUsages);
|
|
628
|
+
if (info) transformDefinitionModule(s, id, info, verifiedUsages);
|
|
629
|
+
if (verifiedUsages.length > 0) transformUsages(s, verifiedUsages, info);
|
|
497
630
|
if (!s.hasChanged()) return null;
|
|
498
631
|
return {
|
|
499
632
|
code: s.toString(),
|
|
@@ -505,9 +638,27 @@ const dataSourceLazyUnpluginFactory = (options = {}) => {
|
|
|
505
638
|
}
|
|
506
639
|
},
|
|
507
640
|
watchChange(id) {
|
|
508
|
-
|
|
641
|
+
const key = stripQuery(id);
|
|
642
|
+
hocInfoCache.delete(key);
|
|
643
|
+
reExportsCache.delete(key);
|
|
509
644
|
}
|
|
510
645
|
};
|
|
511
646
|
};
|
|
647
|
+
function dropAccessesInsideHocArgs(info, usages) {
|
|
648
|
+
const result = [];
|
|
649
|
+
for (const usage of usages) {
|
|
650
|
+
const accesses = usage.accesses.filter((access) => !isAccessInsideArg(info.loading, access) && !(info.error && isAccessInsideArg(info.error, access)));
|
|
651
|
+
if (accesses.length === 0) continue;
|
|
652
|
+
if (accesses.length === usage.accesses.length) {
|
|
653
|
+
result.push(usage);
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
result.push({
|
|
657
|
+
...usage,
|
|
658
|
+
accesses
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
return result;
|
|
662
|
+
}
|
|
512
663
|
//#endregion
|
|
513
664
|
export { dataSourceLazyUnpluginFactory as n, DEFAULT_HOCS as t };
|