@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.
- 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 +5 -0
- package/build/plugin/esbuild.d.cts +6 -0
- package/build/plugin/esbuild.mjs +1 -1
- package/build/plugin/factory-B49Ym3ij.cjs +696 -0
- package/build/plugin/{factory-2tSt4Rte.mjs → factory-C_-OCKcW.mjs} +197 -56
- package/build/plugin/index-Dv4gZVjD.d.cts +23 -0
- package/build/plugin/index.cjs +12 -0
- package/build/plugin/index.d.cts +7 -0
- package/build/plugin/index.mjs +1 -1
- package/build/plugin/rollup.cjs +5 -0
- package/build/plugin/rollup.d.cts +5 -0
- package/build/plugin/rollup.mjs +1 -1
- package/build/plugin/rspack.cjs +5 -0
- package/build/plugin/rspack.d.cts +6 -0
- package/build/plugin/rspack.mjs +1 -1
- package/build/plugin/typings/client.d.ts +16 -4
- package/build/plugin/vite.cjs +5 -0
- package/build/plugin/vite.d.cts +5 -0
- package/build/plugin/vite.mjs +1 -1
- package/build/plugin/webpack.cjs +5 -0
- package/build/plugin/webpack.d.cts +6 -0
- package/build/plugin/webpack.mjs +1 -1
- package/package.json +13 -10
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
let node_fs = require("node:fs");
|
|
24
|
+
node_fs = __toESM(node_fs);
|
|
25
|
+
let magic_string = require("magic-string");
|
|
26
|
+
magic_string = __toESM(magic_string);
|
|
27
|
+
let oxc_parser = require("oxc-parser");
|
|
28
|
+
let node_path = require("node:path");
|
|
29
|
+
node_path = __toESM(node_path);
|
|
30
|
+
let oxc_transform = require("oxc-transform");
|
|
31
|
+
//#region src/plugin/core/utils.ts
|
|
32
|
+
const VIRTUAL_PREFIX = "\0dsl-plugin";
|
|
33
|
+
const COMPANION_TYPES = {
|
|
34
|
+
loading: "Loading",
|
|
35
|
+
error: "Error",
|
|
36
|
+
lazy: "Lazy"
|
|
37
|
+
};
|
|
38
|
+
const COMPANION_TYPE_BY_SUFFIX = {
|
|
39
|
+
Loading: "loading",
|
|
40
|
+
Error: "error",
|
|
41
|
+
Lazy: "lazy"
|
|
42
|
+
};
|
|
43
|
+
const COMPANION_TYPES_SET = new Set(Object.keys(COMPANION_TYPES));
|
|
44
|
+
const COMPANION_SUFFIXES_SET = new Set(Object.values(COMPANION_TYPES));
|
|
45
|
+
function isCompanionType(type) {
|
|
46
|
+
return COMPANION_TYPES_SET.has(type);
|
|
47
|
+
}
|
|
48
|
+
function isCompanionSuffix(suffix) {
|
|
49
|
+
return COMPANION_SUFFIXES_SET.has(suffix);
|
|
50
|
+
}
|
|
51
|
+
function makeVirtualId(type, sourceFile) {
|
|
52
|
+
return `${VIRTUAL_PREFIX}:${type}:${stripQuery(sourceFile)}`;
|
|
53
|
+
}
|
|
54
|
+
function parseVirtualId(id) {
|
|
55
|
+
const prefixIndex = id.indexOf(VIRTUAL_PREFIX);
|
|
56
|
+
if (prefixIndex === -1) return null;
|
|
57
|
+
const rest = id.slice(prefixIndex + 11 + 1);
|
|
58
|
+
const colonIndex = rest.indexOf(":");
|
|
59
|
+
if (colonIndex === -1) return null;
|
|
60
|
+
const type = rest.slice(0, colonIndex);
|
|
61
|
+
if (!isCompanionType(type)) return null;
|
|
62
|
+
return {
|
|
63
|
+
type,
|
|
64
|
+
sourceFile: rest.slice(colonIndex + 1)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function makeCompanionId(type, sourceFile) {
|
|
68
|
+
const suffix = COMPANION_TYPES[type];
|
|
69
|
+
const cleanSourceFile = stripQuery(sourceFile);
|
|
70
|
+
const ext = node_path.default.extname(cleanSourceFile);
|
|
71
|
+
return `${ext ? cleanSourceFile.slice(0, -ext.length) : cleanSourceFile}.${suffix}${ext}`;
|
|
72
|
+
}
|
|
73
|
+
function parseCompanionId(id) {
|
|
74
|
+
const suffixIndex = id.lastIndexOf(".");
|
|
75
|
+
if (suffixIndex === -1) return null;
|
|
76
|
+
const suffix = id.slice(suffixIndex + 1);
|
|
77
|
+
if (!isCompanionSuffix(suffix)) return null;
|
|
78
|
+
return {
|
|
79
|
+
type: COMPANION_TYPE_BY_SUFFIX[suffix],
|
|
80
|
+
sourceFile: id.slice(0, suffixIndex)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function isRelativeId(id) {
|
|
84
|
+
return id[0] === ".";
|
|
85
|
+
}
|
|
86
|
+
function toRelativeImportSource(fromFile, toFile) {
|
|
87
|
+
const fromDir = node_path.default.dirname(stripQuery(fromFile));
|
|
88
|
+
let rel = node_path.default.relative(fromDir, toFile);
|
|
89
|
+
if (node_path.default.sep !== "/") rel = rel.split(node_path.default.sep).join("/");
|
|
90
|
+
if (!rel.startsWith(".")) rel = `./${rel}`;
|
|
91
|
+
const ext = node_path.default.extname(rel);
|
|
92
|
+
return ext ? rel.slice(0, -ext.length) : rel;
|
|
93
|
+
}
|
|
94
|
+
function stripQuery(id) {
|
|
95
|
+
const queryIndex = id.indexOf("?");
|
|
96
|
+
return queryIndex === -1 ? id : id.slice(0, queryIndex);
|
|
97
|
+
}
|
|
98
|
+
function getHocString(from, name) {
|
|
99
|
+
return `${from}/${name}`;
|
|
100
|
+
}
|
|
101
|
+
function assertNever(value) {
|
|
102
|
+
throw new Error(`Unexpected value: ${value}`);
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/plugin/core/extract.ts
|
|
106
|
+
function extractHocInfo(hocsSet, filename, source) {
|
|
107
|
+
const program = parseProgram(filename, source);
|
|
108
|
+
if (!program) return null;
|
|
109
|
+
const importDecls = [];
|
|
110
|
+
const trackedHocs = /* @__PURE__ */ new Map();
|
|
111
|
+
for (const node of program.body) {
|
|
112
|
+
if (node.type !== "ImportDeclaration" || node.importKind === "type") continue;
|
|
113
|
+
importDecls.push(node);
|
|
114
|
+
for (const spec of node.specifiers) {
|
|
115
|
+
let importedName;
|
|
116
|
+
if (spec.type === "ImportSpecifier") {
|
|
117
|
+
if (spec.importKind === "type") continue;
|
|
118
|
+
importedName = spec.imported.type === "Literal" ? spec.imported.value : spec.imported.name;
|
|
119
|
+
} else if (spec.type === "ImportDefaultSpecifier") importedName = "default";
|
|
120
|
+
else continue;
|
|
121
|
+
if (hocsSet.has(getHocString(node.source.value, importedName))) trackedHocs.set(spec.local.name, {
|
|
122
|
+
from: node.source.value,
|
|
123
|
+
name: importedName
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (trackedHocs.size === 0) return null;
|
|
128
|
+
for (const node of program.body) {
|
|
129
|
+
if (node.type !== "ExportNamedDeclaration" || node.exportKind === "type" || node.declaration?.type !== "VariableDeclaration") continue;
|
|
130
|
+
for (const decl of node.declaration.declarations) {
|
|
131
|
+
if (decl.id.type !== "Identifier" || !decl.init || decl.init.type !== "CallExpression" || decl.init.callee.type !== "Identifier" || decl.init.arguments.length < 2) continue;
|
|
132
|
+
const hocEntry = trackedHocs.get(decl.init.callee.name);
|
|
133
|
+
if (!hocEntry) continue;
|
|
134
|
+
const [contentArg, loadingArg, errorArg] = decl.init.arguments;
|
|
135
|
+
const contentInfo = contentArg.type === "Identifier" ? {
|
|
136
|
+
kind: "identifier",
|
|
137
|
+
name: contentArg.name
|
|
138
|
+
} : {
|
|
139
|
+
kind: "inline",
|
|
140
|
+
argSource: source.slice(contentArg.start, contentArg.end),
|
|
141
|
+
argStart: contentArg.start,
|
|
142
|
+
argEnd: contentArg.end
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
hocImportSource: hocEntry.from,
|
|
146
|
+
hocImportedName: hocEntry.name,
|
|
147
|
+
hocLocalName: decl.init.callee.name,
|
|
148
|
+
exportedName: decl.id.name,
|
|
149
|
+
hocExportStart: node.start,
|
|
150
|
+
content: contentInfo,
|
|
151
|
+
loading: {
|
|
152
|
+
argSource: source.slice(loadingArg.start, loadingArg.end),
|
|
153
|
+
argStart: loadingArg.start,
|
|
154
|
+
argEnd: loadingArg.end,
|
|
155
|
+
imports: filterNeededImports(importDecls, collectIdentifiers(loadingArg))
|
|
156
|
+
},
|
|
157
|
+
error: errorArg ? {
|
|
158
|
+
argSource: source.slice(errorArg.start, errorArg.end),
|
|
159
|
+
argStart: errorArg.start,
|
|
160
|
+
argEnd: errorArg.end,
|
|
161
|
+
imports: filterNeededImports(importDecls, collectIdentifiers(errorArg))
|
|
162
|
+
} : null
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
function extractReExports(filename, source) {
|
|
169
|
+
const named = /* @__PURE__ */ new Map();
|
|
170
|
+
const stars = [];
|
|
171
|
+
const program = parseProgram(filename, source);
|
|
172
|
+
if (!program) return {
|
|
173
|
+
named,
|
|
174
|
+
stars
|
|
175
|
+
};
|
|
176
|
+
const localImports = /* @__PURE__ */ new Map();
|
|
177
|
+
for (const node of program.body) {
|
|
178
|
+
if (node.type !== "ImportDeclaration" || node.importKind === "type") continue;
|
|
179
|
+
for (const spec of node.specifiers) {
|
|
180
|
+
if (spec.type !== "ImportSpecifier" || spec.importKind === "type") continue;
|
|
181
|
+
const importedName = spec.imported.type === "Literal" ? spec.imported.value : spec.imported.name;
|
|
182
|
+
localImports.set(spec.local.name, {
|
|
183
|
+
source: node.source.value,
|
|
184
|
+
importedName
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
for (const node of program.body) {
|
|
189
|
+
if (node.type === "ExportAllDeclaration" && node.exportKind !== "type" && !node.exported) {
|
|
190
|
+
stars.push(node.source.value);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (node.type !== "ExportNamedDeclaration" || node.exportKind === "type") continue;
|
|
194
|
+
if (node.source) {
|
|
195
|
+
const src = node.source.value;
|
|
196
|
+
for (const spec of node.specifiers) {
|
|
197
|
+
if (spec.exportKind === "type") continue;
|
|
198
|
+
const localName = spec.local.type === "Literal" ? spec.local.value : spec.local.name;
|
|
199
|
+
const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
|
|
200
|
+
named.set(exportedName, {
|
|
201
|
+
source: src,
|
|
202
|
+
importedName: localName
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (node.declaration) continue;
|
|
208
|
+
for (const spec of node.specifiers) {
|
|
209
|
+
if (spec.exportKind === "type" || spec.local.type === "Literal") continue;
|
|
210
|
+
const target = localImports.get(spec.local.name);
|
|
211
|
+
if (!target) continue;
|
|
212
|
+
const exportedName = spec.exported.type === "Literal" ? spec.exported.value : spec.exported.name;
|
|
213
|
+
named.set(exportedName, target);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
named,
|
|
218
|
+
stars
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
const COMPANION_PROPS_RE = new RegExp(`\\.(${Object.values(COMPANION_TYPES).join("|")})\\b`);
|
|
222
|
+
function extractUsages(filename, source) {
|
|
223
|
+
const result = [];
|
|
224
|
+
if (!COMPANION_PROPS_RE.test(source)) return result;
|
|
225
|
+
const program = parseProgram(filename, source);
|
|
226
|
+
if (!program) return result;
|
|
227
|
+
const trackedImports = /* @__PURE__ */ new Map();
|
|
228
|
+
const importLocalStarts = /* @__PURE__ */ new Set();
|
|
229
|
+
for (const node of program.body) {
|
|
230
|
+
if (node.type !== "ImportDeclaration") continue;
|
|
231
|
+
const declMeta = {
|
|
232
|
+
source: node.source.value,
|
|
233
|
+
namedSpecifiers: [],
|
|
234
|
+
start: node.start,
|
|
235
|
+
end: node.end
|
|
236
|
+
};
|
|
237
|
+
for (const spec of node.specifiers) {
|
|
238
|
+
if (spec.type !== "ImportSpecifier") continue;
|
|
239
|
+
const specMeta = {
|
|
240
|
+
localName: spec.local.name,
|
|
241
|
+
importedName: spec.imported.type === "Literal" ? spec.imported.value : spec.imported.name,
|
|
242
|
+
start: spec.start,
|
|
243
|
+
end: spec.end
|
|
244
|
+
};
|
|
245
|
+
declMeta.namedSpecifiers.push(specMeta);
|
|
246
|
+
trackedImports.set(spec.local.name, {
|
|
247
|
+
decl: declMeta,
|
|
248
|
+
spec: specMeta
|
|
249
|
+
});
|
|
250
|
+
importLocalStarts.add(spec.local.start);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (trackedImports.size === 0) return result;
|
|
254
|
+
const usages = /* @__PURE__ */ new Map();
|
|
255
|
+
function ensureUsage(name) {
|
|
256
|
+
let usage = usages.get(name);
|
|
257
|
+
if (!usage) usages.set(name, usage = {
|
|
258
|
+
totalStarts: /* @__PURE__ */ new Set(),
|
|
259
|
+
accesses: []
|
|
260
|
+
});
|
|
261
|
+
return usage;
|
|
262
|
+
}
|
|
263
|
+
function trackIdentifier(name, start) {
|
|
264
|
+
if (trackedImports.has(name) && !importLocalStarts.has(start)) ensureUsage(name).totalStarts.add(start);
|
|
265
|
+
}
|
|
266
|
+
function trackCompanionAccess(localName, prop, exprStart, exprEnd) {
|
|
267
|
+
ensureUsage(localName).accesses.push({
|
|
268
|
+
prop,
|
|
269
|
+
start: exprStart,
|
|
270
|
+
end: exprEnd
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
new oxc_parser.Visitor({
|
|
274
|
+
Identifier(node) {
|
|
275
|
+
trackIdentifier(node.name, node.start);
|
|
276
|
+
},
|
|
277
|
+
JSXIdentifier(node) {
|
|
278
|
+
trackIdentifier(node.name, node.start);
|
|
279
|
+
},
|
|
280
|
+
MemberExpression(node) {
|
|
281
|
+
if (!node.computed && node.object.type === "Identifier" && trackedImports.has(node.object.name) && node.property.type === "Identifier" && isCompanionSuffix(node.property.name)) trackCompanionAccess(node.object.name, node.property.name, node.start, node.end);
|
|
282
|
+
},
|
|
283
|
+
JSXMemberExpression(node) {
|
|
284
|
+
if (node.object.type === "JSXIdentifier" && trackedImports.has(node.object.name) && isCompanionSuffix(node.property.name)) trackCompanionAccess(node.object.name, node.property.name, node.start, node.end);
|
|
285
|
+
}
|
|
286
|
+
}).visit(program);
|
|
287
|
+
for (const [localName, { accesses, totalStarts }] of usages) {
|
|
288
|
+
if (accesses.length === 0) continue;
|
|
289
|
+
const { decl, spec } = trackedImports.get(localName);
|
|
290
|
+
const hasOtherUsages = totalStarts.size > accesses.length;
|
|
291
|
+
result.push({
|
|
292
|
+
decl,
|
|
293
|
+
spec,
|
|
294
|
+
accesses,
|
|
295
|
+
hasOtherUsages
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
function collectIdentifiers(node) {
|
|
301
|
+
const result = /* @__PURE__ */ new Set();
|
|
302
|
+
walk(node, (current) => {
|
|
303
|
+
if (isOwnIdentifier(current) || isJSXIdentifier(current)) result.add(current.name);
|
|
304
|
+
});
|
|
305
|
+
return result;
|
|
306
|
+
}
|
|
307
|
+
function filterNeededImports(importDecls, identifiers) {
|
|
308
|
+
const result = [];
|
|
309
|
+
for (const decl of importDecls) {
|
|
310
|
+
if (decl.specifiers.length === 0) {
|
|
311
|
+
result.push(decl);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const specifiers = decl.specifiers.filter((spec) => identifiers.has(spec.local.name));
|
|
315
|
+
if (specifiers.length > 0) result.push({
|
|
316
|
+
...decl,
|
|
317
|
+
specifiers
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
function isOwnNode(node) {
|
|
323
|
+
return typeof node === "object" && node !== null && "type" in node && typeof node.type === "string";
|
|
324
|
+
}
|
|
325
|
+
function isOwnIdentifier(node) {
|
|
326
|
+
return node.type === "Identifier";
|
|
327
|
+
}
|
|
328
|
+
function isJSXIdentifier(node) {
|
|
329
|
+
return node.type === "JSXIdentifier";
|
|
330
|
+
}
|
|
331
|
+
function walk(node, visitor) {
|
|
332
|
+
if (!isOwnNode(node)) return;
|
|
333
|
+
visitor(node);
|
|
334
|
+
for (const value of Object.values(node)) if (Array.isArray(value)) for (const item of value) walk(item, visitor);
|
|
335
|
+
else walk(value, visitor);
|
|
336
|
+
}
|
|
337
|
+
function parseProgram(filename, source) {
|
|
338
|
+
const parsed = (0, oxc_parser.parseSync)(filename, source, {
|
|
339
|
+
sourceType: "module",
|
|
340
|
+
range: true
|
|
341
|
+
});
|
|
342
|
+
if (parsed.errors.some((error) => error.severity === "Error")) return null;
|
|
343
|
+
return parsed.program;
|
|
344
|
+
}
|
|
345
|
+
//#endregion
|
|
346
|
+
//#region src/plugin/core/generate.ts
|
|
347
|
+
function generateAuxModuleCode(exportedName, info, type) {
|
|
348
|
+
const suffix = COMPANION_TYPES[type];
|
|
349
|
+
return [
|
|
350
|
+
renderImports(info.imports),
|
|
351
|
+
"",
|
|
352
|
+
`export const ${exportedName}${suffix} = ${info.argSource};`,
|
|
353
|
+
""
|
|
354
|
+
].join("\n").trimStart();
|
|
355
|
+
}
|
|
356
|
+
function generateLazyModule(sourceFile, info, options = {}) {
|
|
357
|
+
const importSource = options.jsx?.importSource ?? "react";
|
|
358
|
+
const name = info.exportedName;
|
|
359
|
+
const cleanSourceFile = stripQuery(sourceFile);
|
|
360
|
+
const base = `./${node_path.default.basename(cleanSourceFile, node_path.default.extname(cleanSourceFile))}`;
|
|
361
|
+
const hocBinding = info.hocImportedName === "default" ? info.hocLocalName : info.hocImportedName;
|
|
362
|
+
const lines = [
|
|
363
|
+
`import {lazy} from '${importSource}';`,
|
|
364
|
+
"",
|
|
365
|
+
info.hocImportedName === "default" ? `import ${info.hocLocalName} from '${info.hocImportSource}';` : `import {${info.hocImportedName}} from '${info.hocImportSource}';`,
|
|
366
|
+
"",
|
|
367
|
+
`import {${name}Loading} from '${base}.Loading';`
|
|
368
|
+
];
|
|
369
|
+
if (info.error) lines.push(`import {${name}Error} from '${base}.Error';`);
|
|
370
|
+
lines.push("", `export const ${name}Lazy = ${hocBinding}(`, ` lazy(() => import('${base}').then((m) => ({default: m.${name}Content}))),`, ` ${name}Loading,`);
|
|
371
|
+
if (info.error) lines.push(` ${name}Error,`);
|
|
372
|
+
lines.push(`);`, ``);
|
|
373
|
+
const code = lines.join("\n");
|
|
374
|
+
const filename = makeCompanionId("lazy", cleanSourceFile);
|
|
375
|
+
return {
|
|
376
|
+
code,
|
|
377
|
+
map: new magic_string.default(code).generateMap({
|
|
378
|
+
source: filename,
|
|
379
|
+
includeContent: true,
|
|
380
|
+
hires: false
|
|
381
|
+
}).toString()
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
function transformJsx(filename, code, options = {}) {
|
|
385
|
+
const result = (0, oxc_transform.transformSync)(filename, code, {
|
|
386
|
+
lang: "tsx",
|
|
387
|
+
jsx: options.jsx,
|
|
388
|
+
target: options.target,
|
|
389
|
+
sourcemap: true
|
|
390
|
+
});
|
|
391
|
+
if (result.errors.length > 0) {
|
|
392
|
+
const error = result.errors[0];
|
|
393
|
+
throw new Error(error.codeframe ?? error.message);
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
code: result.code,
|
|
397
|
+
map: JSON.stringify(result.map)
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
function renderImport(decl) {
|
|
401
|
+
const parts = [];
|
|
402
|
+
const named = [];
|
|
403
|
+
for (const spec of decl.specifiers) if (spec.type === "ImportDefaultSpecifier") parts.push(spec.local.name);
|
|
404
|
+
else if (spec.type === "ImportNamespaceSpecifier") parts.push(`* as ${spec.local.name}`);
|
|
405
|
+
else {
|
|
406
|
+
const imported = spec.imported.type === "Literal" ? JSON.stringify(spec.imported.value) : spec.imported.name;
|
|
407
|
+
const renderedSpec = imported === spec.local.name ? spec.local.name : `${imported} as ${spec.local.name}`;
|
|
408
|
+
named.push(spec.importKind === "type" ? `type ${renderedSpec}` : renderedSpec);
|
|
409
|
+
}
|
|
410
|
+
if (named.length > 0) parts.push(`{${named.join(", ")}}`);
|
|
411
|
+
return `import ${decl.importKind === "type" ? "type " : ""}${parts.join(", ")} from '${decl.source.value}';`;
|
|
412
|
+
}
|
|
413
|
+
function renderImports(imports) {
|
|
414
|
+
return imports.map(renderImport).join("\n");
|
|
415
|
+
}
|
|
416
|
+
//#endregion
|
|
417
|
+
//#region src/plugin/core/resolve.ts
|
|
418
|
+
async function resolveSourceFile(ctx, sourceFile, importer) {
|
|
419
|
+
const nativeCtx = ctx.getNativeBuildContext?.();
|
|
420
|
+
const directory = node_path.default.dirname(importer);
|
|
421
|
+
if (nativeCtx?.framework === "webpack" || nativeCtx?.framework === "rspack") return resolveWithCompiler(nativeCtx.compiler, directory, sourceFile);
|
|
422
|
+
if (nativeCtx?.framework === "esbuild") {
|
|
423
|
+
const result = await nativeCtx.build.resolve(sourceFile, {
|
|
424
|
+
kind: "import-statement",
|
|
425
|
+
importer,
|
|
426
|
+
resolveDir: directory
|
|
427
|
+
});
|
|
428
|
+
return result.errors.length === 0 && result.path ? result.path : null;
|
|
429
|
+
}
|
|
430
|
+
if ("resolve" in ctx && typeof ctx.resolve === "function") {
|
|
431
|
+
const resolved = await ctx.resolve(sourceFile, importer, { skipSelf: true });
|
|
432
|
+
if (!resolved || resolved.external) return null;
|
|
433
|
+
return resolved.id;
|
|
434
|
+
}
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
function resolveWithCompiler(compiler, directory, request) {
|
|
438
|
+
return new Promise((resolve) => {
|
|
439
|
+
try {
|
|
440
|
+
compiler.resolverFactory.get("normal").resolve({}, directory, request, {}, (error, result) => {
|
|
441
|
+
resolve(!error && typeof result === "string" ? result : null);
|
|
442
|
+
});
|
|
443
|
+
} catch {
|
|
444
|
+
resolve(null);
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
//#endregion
|
|
449
|
+
//#region src/plugin/core/transform.ts
|
|
450
|
+
function transformDefinitionModule(s, filename, info) {
|
|
451
|
+
const name = info.exportedName;
|
|
452
|
+
const cleanFilename = stripQuery(filename);
|
|
453
|
+
const localSource = `./${node_path.default.basename(cleanFilename, node_path.default.extname(cleanFilename))}`;
|
|
454
|
+
const prependImports = [renderNamedImport(`${name}Loading`, `${name}Loading`, makeCompanionId("loading", localSource))];
|
|
455
|
+
if (info.error) prependImports.push(renderNamedImport(`${name}Error`, `${name}Error`, makeCompanionId("error", localSource)));
|
|
456
|
+
s.prepend(prependImports.join("\n") + "\n");
|
|
457
|
+
s.overwrite(info.loading.argStart, info.loading.argEnd, `${name}Loading`);
|
|
458
|
+
if (info.error) s.overwrite(info.error.argStart, info.error.argEnd, `${name}Error`);
|
|
459
|
+
if (info.content.kind === "identifier") s.append(`\nexport {${info.content.name} as ${name}Content};\n`);
|
|
460
|
+
else {
|
|
461
|
+
s.appendLeft(info.hocExportStart, `const ${name}Content = ${info.content.argSource};\n\n`);
|
|
462
|
+
s.overwrite(info.content.argStart, info.content.argEnd, `${name}Content`);
|
|
463
|
+
s.append(`\nexport {${name}Content};\n`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
function transformUsages(s, usages) {
|
|
467
|
+
for (const { usage } of usages) for (const access of usage.accesses) s.overwrite(access.start, access.end, `${usage.spec.localName}${access.prop}`);
|
|
468
|
+
const newImports = [];
|
|
469
|
+
for (const { usage, hocImportSource, hocExportedName } of usages) {
|
|
470
|
+
const propsUsed = new Set(usage.accesses.map((access) => access.prop));
|
|
471
|
+
for (const prop of propsUsed) newImports.push(renderNamedImport(`${usage.spec.localName}${prop}`, `${hocExportedName}${prop}`, makeCompanionId(COMPANION_TYPE_BY_SUFFIX[prop], hocImportSource)));
|
|
472
|
+
}
|
|
473
|
+
if (newImports.length > 0) s.prepend(newImports.join("\n") + "\n");
|
|
474
|
+
const localsByDecl = /* @__PURE__ */ new Map();
|
|
475
|
+
for (const { usage } of usages) {
|
|
476
|
+
if (usage.hasOtherUsages) continue;
|
|
477
|
+
let locals = localsByDecl.get(usage.decl);
|
|
478
|
+
if (!locals) localsByDecl.set(usage.decl, locals = /* @__PURE__ */ new Set());
|
|
479
|
+
locals.add(usage.spec.localName);
|
|
480
|
+
}
|
|
481
|
+
for (const [decl, locals] of localsByDecl) removeImportSpecifiers(s, decl, locals);
|
|
482
|
+
}
|
|
483
|
+
function removeImportSpecifiers(s, decl, localsToRemove) {
|
|
484
|
+
const kept = decl.namedSpecifiers.filter((spec) => !localsToRemove.has(spec.localName));
|
|
485
|
+
if (kept.length === 0) {
|
|
486
|
+
s.remove(decl.start, s.original[decl.end] === "\n" ? decl.end + 1 : decl.end);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const firstSpec = decl.namedSpecifiers[0];
|
|
490
|
+
const lastSpec = decl.namedSpecifiers[decl.namedSpecifiers.length - 1];
|
|
491
|
+
const braceOpen = s.original.lastIndexOf("{", firstSpec.start);
|
|
492
|
+
const braceClose = s.original.indexOf("}", lastSpec.end);
|
|
493
|
+
if (braceOpen === -1 || braceClose === -1) return;
|
|
494
|
+
const keptText = kept.map((spec) => s.original.slice(spec.start, spec.end)).join(", ");
|
|
495
|
+
s.overwrite(braceOpen + 1, braceClose, keptText);
|
|
496
|
+
}
|
|
497
|
+
function renderNamedImport(local, imported, source) {
|
|
498
|
+
return `import {${local === imported ? imported : `${imported} as ${local}`}} from '${source}';`;
|
|
499
|
+
}
|
|
500
|
+
//#endregion
|
|
501
|
+
//#region src/plugin/core/factory.ts
|
|
502
|
+
const DEFAULT_HOCS = [
|
|
503
|
+
{
|
|
504
|
+
from: "@gravity-ui/data-source",
|
|
505
|
+
name: "withAsync"
|
|
506
|
+
},
|
|
507
|
+
{
|
|
508
|
+
from: "@gravity-ui/data-source",
|
|
509
|
+
name: "withAsyncBoundary"
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
from: "@gravity-ui/data-source",
|
|
513
|
+
name: "withQueryAsyncBoundary"
|
|
514
|
+
}
|
|
515
|
+
];
|
|
516
|
+
const DEFAULT_INCLUDE = /\.(tsx?|jsx?)$/;
|
|
517
|
+
const VIRTUAL_EXCLUDE = new RegExp(`${VIRTUAL_PREFIX}|${encodeURIComponent(VIRTUAL_PREFIX)}`);
|
|
518
|
+
const dataSourceLazyUnpluginFactory = (options = {}) => {
|
|
519
|
+
const hocPatterns = options.hocs ?? DEFAULT_HOCS;
|
|
520
|
+
const include = options.include ?? DEFAULT_INCLUDE;
|
|
521
|
+
const exclude = [VIRTUAL_EXCLUDE];
|
|
522
|
+
if (Array.isArray(options.exclude)) exclude.push(...options.exclude);
|
|
523
|
+
else if (options.exclude) exclude.push(options.exclude);
|
|
524
|
+
const hocsSet = new Set(hocPatterns.map((pattern) => getHocString(pattern.from, pattern.name)));
|
|
525
|
+
const hocsRegexp = new RegExp(Array.from(new Set(hocPatterns.map((pattern) => pattern.name))).map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"));
|
|
526
|
+
const hocInfoCache = /* @__PURE__ */ new Map();
|
|
527
|
+
const reExportsCache = /* @__PURE__ */ new Map();
|
|
528
|
+
const getHocInfo = (sourceFile, source) => {
|
|
529
|
+
const key = stripQuery(sourceFile);
|
|
530
|
+
const cachedInfo = hocInfoCache.get(key);
|
|
531
|
+
if (cachedInfo !== void 0) return cachedInfo;
|
|
532
|
+
const resolvedSource = source ?? node_fs.default.readFileSync(key, "utf-8");
|
|
533
|
+
const info = hocsRegexp.test(resolvedSource) ? extractHocInfo(hocsSet, key, resolvedSource) : null;
|
|
534
|
+
hocInfoCache.set(key, info);
|
|
535
|
+
return info;
|
|
536
|
+
};
|
|
537
|
+
const getReExports = (sourceFile, source) => {
|
|
538
|
+
const key = stripQuery(sourceFile);
|
|
539
|
+
const cachedReExports = reExportsCache.get(key);
|
|
540
|
+
if (cachedReExports) return cachedReExports;
|
|
541
|
+
const reExports = extractReExports(key, source ?? node_fs.default.readFileSync(key, "utf-8"));
|
|
542
|
+
reExportsCache.set(key, reExports);
|
|
543
|
+
return reExports;
|
|
544
|
+
};
|
|
545
|
+
const resolveHocSourceFile = async (ctx, file, importedName, visited = /* @__PURE__ */ new Set()) => {
|
|
546
|
+
const visitKey = `${file}\0${importedName}`;
|
|
547
|
+
if (visited.has(visitKey)) return null;
|
|
548
|
+
visited.add(visitKey);
|
|
549
|
+
const directInfo = getHocInfo(file);
|
|
550
|
+
if (directInfo && directInfo.exportedName === importedName) return {
|
|
551
|
+
file,
|
|
552
|
+
info: directInfo
|
|
553
|
+
};
|
|
554
|
+
const reExports = getReExports(file);
|
|
555
|
+
const named = reExports.named.get(importedName);
|
|
556
|
+
if (named) {
|
|
557
|
+
const resolved = await resolveSourceFile(ctx, named.source, file);
|
|
558
|
+
if (resolved) {
|
|
559
|
+
const next = await resolveHocSourceFile(ctx, resolved, named.importedName, visited);
|
|
560
|
+
if (next) return next;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
for (const starSource of reExports.stars) {
|
|
564
|
+
const resolved = await resolveSourceFile(ctx, starSource, file);
|
|
565
|
+
if (!resolved) continue;
|
|
566
|
+
const next = await resolveHocSourceFile(ctx, resolved, importedName, visited);
|
|
567
|
+
if (next) return next;
|
|
568
|
+
}
|
|
569
|
+
return null;
|
|
570
|
+
};
|
|
571
|
+
const verifyUsages = async (ctx, usages, importerId) => {
|
|
572
|
+
return (await Promise.all(usages.map(async (usage) => {
|
|
573
|
+
const resolvedSourceFile = await resolveSourceFile(ctx, usage.decl.source, importerId);
|
|
574
|
+
if (!resolvedSourceFile) return null;
|
|
575
|
+
const target = await resolveHocSourceFile(ctx, resolvedSourceFile, usage.spec.importedName);
|
|
576
|
+
if (!target) return null;
|
|
577
|
+
return {
|
|
578
|
+
usage,
|
|
579
|
+
hocImportSource: toRelativeImportSource(importerId, target.file),
|
|
580
|
+
hocExportedName: target.info.exportedName
|
|
581
|
+
};
|
|
582
|
+
}))).filter((usage) => usage !== null);
|
|
583
|
+
};
|
|
584
|
+
return {
|
|
585
|
+
name: "data-source-lazy",
|
|
586
|
+
enforce: "pre",
|
|
587
|
+
async resolveId(id, importer) {
|
|
588
|
+
if (!importer) return null;
|
|
589
|
+
const companion = parseCompanionId(id);
|
|
590
|
+
const parsedImporter = parseVirtualId(importer);
|
|
591
|
+
const normalizedImporter = parsedImporter?.sourceFile ?? importer;
|
|
592
|
+
if (companion) {
|
|
593
|
+
if (await resolveSourceFile(this, id, normalizedImporter)) return null;
|
|
594
|
+
const resolvedSourceFile = await resolveSourceFile(this, companion.sourceFile, normalizedImporter);
|
|
595
|
+
return resolvedSourceFile && makeVirtualId(companion.type, resolvedSourceFile);
|
|
596
|
+
}
|
|
597
|
+
if (parsedImporter && isRelativeId(id)) return resolveSourceFile(this, id, parsedImporter.sourceFile);
|
|
598
|
+
return null;
|
|
599
|
+
},
|
|
600
|
+
load: {
|
|
601
|
+
filter: { id: new RegExp(VIRTUAL_PREFIX) },
|
|
602
|
+
async handler(id) {
|
|
603
|
+
const parsedId = parseVirtualId(id);
|
|
604
|
+
if (!parsedId) return null;
|
|
605
|
+
this.addWatchFile(parsedId.sourceFile);
|
|
606
|
+
const info = getHocInfo(parsedId.sourceFile);
|
|
607
|
+
if (!info) return null;
|
|
608
|
+
if (parsedId.type === "loading" || parsedId.type === "error") {
|
|
609
|
+
const auxInfo = info[parsedId.type];
|
|
610
|
+
if (!auxInfo) return null;
|
|
611
|
+
const filename = makeCompanionId(parsedId.type, parsedId.sourceFile);
|
|
612
|
+
const code = generateAuxModuleCode(info.exportedName, auxInfo, parsedId.type);
|
|
613
|
+
const usages = extractUsages(filename, code);
|
|
614
|
+
const verifiedUsages = await verifyUsages(this, usages, parsedId.sourceFile);
|
|
615
|
+
let resultCode = code;
|
|
616
|
+
if (verifiedUsages.length > 0) {
|
|
617
|
+
const s = new magic_string.default(code);
|
|
618
|
+
transformUsages(s, verifiedUsages);
|
|
619
|
+
resultCode = s.toString();
|
|
620
|
+
}
|
|
621
|
+
return transformJsx(filename, resultCode, options.generateOptions);
|
|
622
|
+
}
|
|
623
|
+
if (parsedId.type === "lazy") return generateLazyModule(parsedId.sourceFile, info, options.generateOptions);
|
|
624
|
+
return assertNever(parsedId.type);
|
|
625
|
+
}
|
|
626
|
+
},
|
|
627
|
+
transform: {
|
|
628
|
+
filter: { id: {
|
|
629
|
+
include,
|
|
630
|
+
exclude
|
|
631
|
+
} },
|
|
632
|
+
async handler(code, id) {
|
|
633
|
+
const info = getHocInfo(id, code);
|
|
634
|
+
const usages = extractUsages(id, code);
|
|
635
|
+
if (!info && usages.length === 0) return null;
|
|
636
|
+
const filteredUsages = info ? dropAccessesInsideHocArgs(info, usages) : usages;
|
|
637
|
+
const verifiedUsages = await verifyUsages(this, filteredUsages, id);
|
|
638
|
+
if (!info && verifiedUsages.length === 0) return null;
|
|
639
|
+
const s = new magic_string.default(code);
|
|
640
|
+
if (info) transformDefinitionModule(s, id, info);
|
|
641
|
+
if (verifiedUsages.length > 0) transformUsages(s, verifiedUsages);
|
|
642
|
+
if (!s.hasChanged()) return null;
|
|
643
|
+
return {
|
|
644
|
+
code: s.toString(),
|
|
645
|
+
map: s.generateMap({
|
|
646
|
+
source: id,
|
|
647
|
+
hires: true
|
|
648
|
+
}).toString()
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
},
|
|
652
|
+
watchChange(id) {
|
|
653
|
+
const key = stripQuery(id);
|
|
654
|
+
hocInfoCache.delete(key);
|
|
655
|
+
reExportsCache.delete(key);
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
};
|
|
659
|
+
function isInside(info, access) {
|
|
660
|
+
return info.argStart <= access.start && access.end <= info.argEnd;
|
|
661
|
+
}
|
|
662
|
+
function dropAccessesInsideHocArgs(info, usages) {
|
|
663
|
+
const result = [];
|
|
664
|
+
for (const usage of usages) {
|
|
665
|
+
const accesses = usage.accesses.filter((access) => !isInside(info.loading, access) && !(info.error && isInside(info.error, access)) && !(info.content.kind === "inline" && isInside(info.content, access)));
|
|
666
|
+
if (accesses.length === 0) continue;
|
|
667
|
+
if (accesses.length === usage.accesses.length) {
|
|
668
|
+
result.push(usage);
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
result.push({
|
|
672
|
+
...usage,
|
|
673
|
+
accesses
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
return result;
|
|
677
|
+
}
|
|
678
|
+
//#endregion
|
|
679
|
+
Object.defineProperty(exports, "DEFAULT_HOCS", {
|
|
680
|
+
enumerable: true,
|
|
681
|
+
get: function() {
|
|
682
|
+
return DEFAULT_HOCS;
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
Object.defineProperty(exports, "__toESM", {
|
|
686
|
+
enumerable: true,
|
|
687
|
+
get: function() {
|
|
688
|
+
return __toESM;
|
|
689
|
+
}
|
|
690
|
+
});
|
|
691
|
+
Object.defineProperty(exports, "dataSourceLazyUnpluginFactory", {
|
|
692
|
+
enumerable: true,
|
|
693
|
+
get: function() {
|
|
694
|
+
return dataSourceLazyUnpluginFactory;
|
|
695
|
+
}
|
|
696
|
+
});
|