@blocklet/pages-kit-inner-components 0.4.23 → 0.4.25

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,454 +0,0 @@
1
- import { getComponentMountPoint as v } from "@blocklet/pages-kit/builtin/utils";
2
- import { PreloadComponentScriptModule as b } from "@blocklet/pages-kit/types";
3
- import { componentUMDName as L, RenderNestedComponent as M, mergeComponent as U } from "@blocklet/pages-kit/utils/property";
4
- import { memoize as O, Sandbox as _, BuiltinModules as D } from "@blocklet/quickjs";
5
- import T from "@blocklet/sdk/lib/config";
6
- import { LRUCache as F } from "lru-cache";
7
- import B, { Headers as J } from "node-fetch";
8
- import { joinURL as y, getQuery as G, withQuery as K, parseURL as j, withHttps as Q } from "ufo";
9
- import W from "@blocklet/logger";
10
- import A from "vite-plugin-node-polyfills/shims/buffer";
11
- import { BuiltinModules as V } from "@blocklet/pages-kit/utils/builtin";
12
- import { createBuiltinModuleTransformer as H, isRelativeModule as I } from "@blocklet/pages-kit/utils/typescript/builtin-module-transformer";
13
- import { call as z } from "@blocklet/sdk/lib/component";
14
- import X from "autoprefixer";
15
- import * as Y from "esbuild";
16
- import Z from "postcss";
17
- import q from "tailwindcss";
18
- import d from "typescript";
19
- const h = W("pages-kit"), ee = async (t, { componentId: e }) => {
20
- const o = `@tailwind components;
21
- @tailwind utilities;
22
- `, n = `.CustomComponent_${e}`;
23
- return (await Z([
24
- q({ content: [{ raw: t, extension: "tsx" }] }),
25
- X({
26
- overrideBrowserslist: ["> 1%", "last 2 versions"],
27
- stats: {}
28
- }),
29
- (a) => {
30
- a.walkRules((r) => {
31
- r.selectors = r.selectors.map((c) => c.replace(/\.(.+)/g, `${n}.$1,${n} .$1`));
32
- });
33
- }
34
- // FIXME: cssnano use browserslist, it is not working with the fs isolation
35
- // cssnano({ preset: 'default' }),
36
- ]).process(o)).css;
37
- }, te = async (t, { componentId: e }) => {
38
- const o = await ee(t, { componentId: e });
39
- return `export const __PagesKit_CSS__ = ${JSON.stringify(o)};
40
-
41
- ${t}
42
- `;
43
- }, $ = O(
44
- async (t, e) => {
45
- let o = d.transpileModule(t, {
46
- compilerOptions: {
47
- jsx: d.JsxEmit.React,
48
- target: d.ScriptTarget.ES2016,
49
- module: d.ModuleKind.ESNext
50
- },
51
- transformers: {
52
- before: [H(d)]
53
- }
54
- }).outputText;
55
- if (e.tailwind && (o = await te(o, { componentId: e.componentId })), e.module === b.ESM) return o;
56
- const n = d.transpileModule(o, {
57
- compilerOptions: {
58
- jsx: d.JsxEmit.React,
59
- target: d.ScriptTarget.ES2016,
60
- module: d.ModuleKind.CommonJS,
61
- moduleResolution: d.ModuleResolutionKind.Node16
62
- }
63
- }).outputText;
64
- return e.module === b.CJS ? n : ne(e.moduleName, n);
65
- },
66
- { keyGenerator: (t, e) => JSON.stringify(["transpileModule", t, e]) }
67
- ), oe = async (t, e) => {
68
- var r, c;
69
- const n = (c = (r = (await Y.build({
70
- entryPoints: ["index.tsx"],
71
- external: Object.keys(V),
72
- format: "esm",
73
- target: "esnext",
74
- bundle: !0,
75
- write: !1,
76
- plugins: [
77
- {
78
- name: "vfs",
79
- setup(u) {
80
- u.onResolve({ filter: /.*/ }, (s) => s.path === "index.tsx" ? { path: "index.tsx", namespace: "vfs" } : s.path === "./component" ? { path: "component.tsx", namespace: "vfs" } : I(s.path) ? { path: s.path, namespace: "vfs" } : null), u.onLoad({ filter: /.*/, namespace: "vfs" }, async (s) => {
81
- if (s.path === "index.tsx")
82
- return { contents: `export { ${e} } from './component'`, loader: "tsx" };
83
- if (s.path === "component.tsx")
84
- return { contents: t, loader: "tsx" };
85
- if (I(s.path)) {
86
- const l = s.path.split("/").pop(), f = y("chunks", l), { data: g } = await z({
87
- name: "pages-kit",
88
- method: "GET",
89
- path: f
90
- });
91
- return { contents: g, loader: "js" };
92
- }
93
- return null;
94
- });
95
- }
96
- }
97
- ]
98
- })).outputFiles) == null ? void 0 : r[0]) == null ? void 0 : c.contents;
99
- if (!n) throw new Error("Failed to build server code");
100
- const i = A.from(n).toString();
101
- return d.transpileModule(i, {
102
- compilerOptions: { module: d.ModuleKind.ESNext, target: d.ScriptTarget.ES2020 }
103
- }).outputText;
104
- }, k = O(
105
- async (t, e) => {
106
- const o = await oe(t, e);
107
- return new RegExp(`export\\s+\\{\\s+${e}\\s+\\}`, "m").test(o) ? o : void 0;
108
- },
109
- { keyGenerator: (t, e) => JSON.stringify(["extractExportValueSchema", t, e]) }
110
- ), ne = (t, e) => {
111
- const o = /\bawait\b/.test(e);
112
- return `// GENERATED FILE. DO NOT EDIT.
113
- var ${t} = ${o ? "async" : ""} function () {
114
-
115
- const exports = {};
116
- let moduleExports = null;
117
-
118
- // add commonjs module compatibility layer
119
- const module = { exports: moduleExports };
120
-
121
- // execute component code
122
- ${e}
123
-
124
- // handle possible module.exports
125
- if (module.exports && module.exports !== moduleExports) {
126
- // if module.exports is used, use it first
127
- return typeof module.exports === 'object' ? module.exports : { default: module.exports };
128
- }
129
-
130
- // ensure a default export
131
- if (!('default' in exports) && Object.keys(exports).length === 0) {
132
- // module has no exports, return null to indicate invalid
133
- return null;
134
- }
135
-
136
- return exports;
137
- };
138
- `;
139
- }, w = new F({
140
- max: 100
141
- }), re = 60 * 60, N = 60;
142
- function se(t) {
143
- h.info("clear preload components cache", { cacheKey: t }), w.delete(t);
144
- }
145
- function je(t) {
146
- for (const e of w.keys())
147
- e.includes(t) && (h.info("clear preload components cache", { cacheKey: e }), se(e));
148
- }
149
- function ce({
150
- mode: t,
151
- instanceId: e,
152
- componentId: o,
153
- locale: n
154
- }) {
155
- return ["getPreloadComponents", t, e, o, n].join("-");
156
- }
157
- async function Ie({
158
- mode: t,
159
- req: e,
160
- state: o,
161
- locale: n,
162
- instances: i,
163
- module: a
164
- }) {
165
- const {
166
- supportedLocales: r,
167
- config: { defaultLocale: c }
168
- } = o;
169
- if (!c) return null;
170
- const u = (await Promise.all(
171
- i.map(async (p) => {
172
- try {
173
- const m = ce({
174
- mode: t,
175
- instanceId: p.id,
176
- componentId: p.componentId,
177
- locale: n
178
- });
179
- if (t !== "draft" && p.useCache && w.has(m))
180
- return h.info(`get preload component from cache: ${m}`), w.get(m);
181
- const x = C({ state: o, componentId: p.componentId });
182
- if (!x) return null;
183
- const S = await ie({
184
- req: e,
185
- state: o,
186
- componentId: x.id,
187
- locale: n,
188
- defaultLocale: c,
189
- properties: p.properties
190
- });
191
- if (!S) return null;
192
- const P = { instanceId: p.id, preload: S };
193
- if (t !== "draft" && p.useCache) {
194
- let E = re;
195
- p.cacheDuration && (E = p.cacheDuration), h.info(`set preload component to cache(${E}s): ${m}`), w.set(m, P, {
196
- ttl: E * 1e3
197
- });
198
- }
199
- return P;
200
- } catch (m) {
201
- return h.error("get preload component error", { instanceId: p.id, componentId: p.componentId }, { error: m }), null;
202
- }
203
- })
204
- )).filter((p) => !!p), s = Object.values(
205
- u.reduce((p, m) => ({ ...p, ...m.preload.components }), {})
206
- );
207
- async function l() {
208
- const p = await Promise.all(
209
- s.map(async (m) => {
210
- const x = L({ componentId: m.component.id }), S = a === b.ESM ? {
211
- module: a,
212
- script: await $(m.script, {
213
- componentId: m.component.id,
214
- module: a,
215
- tailwind: t !== "draft"
216
- })
217
- } : {
218
- module: a,
219
- script: await $(m.script, {
220
- componentId: m.component.id,
221
- module: a,
222
- moduleName: x,
223
- tailwind: t !== "draft"
224
- }),
225
- moduleName: x
226
- };
227
- return [m.component.id, { component: m.component, script: S }];
228
- })
229
- );
230
- return Object.fromEntries(p);
231
- }
232
- const f = await l();
233
- return {
234
- config: { defaultLocale: c, supportedLocales: r },
235
- components: f,
236
- instances: u.map((p) => ({
237
- id: p.instanceId,
238
- componentId: p.preload.component.id,
239
- locales: { [p.preload.locale]: { props: p.preload.props } }
240
- }))
241
- };
242
- }
243
- async function ie({
244
- req: t,
245
- state: e,
246
- componentId: o,
247
- locale: n,
248
- defaultLocale: i,
249
- properties: a
250
- }) {
251
- const { supportedLocales: r } = e, c = C({ state: e, componentId: o });
252
- if (!c) return null;
253
- const u = r.some((l) => l.locale === n) ? n : i;
254
- if (!u) return null;
255
- const s = await R({ req: t, state: e, componentId: o, locale: u, defaultLocale: i, properties: a });
256
- return s ? {
257
- component: c,
258
- ...s
259
- } : null;
260
- }
261
- const ae = 20;
262
- async function R({
263
- req: t,
264
- depth: e = 0,
265
- state: o,
266
- componentId: n,
267
- locale: i,
268
- defaultLocale: a,
269
- properties: r
270
- }) {
271
- if (e > ae) throw new RangeError("max component depth exceeded");
272
- const c = pe({ state: o, componentId: n, properties: r, locale: i });
273
- if (!c) return null;
274
- const { props: u } = c, s = {
275
- locale: i || a,
276
- components: { [c.component.id]: { component: c.component, script: c.script } },
277
- props: { ...u }
278
- };
279
- try {
280
- const l = await le({ ...c, req: t });
281
- l != null && l.props && Object.assign(s.props, l.props);
282
- } catch (l) {
283
- h.error("preload data at server side error", { componentId: n, name: c.component.name }, { error: l });
284
- }
285
- return await Promise.all(
286
- Object.entries(u).map(async ([l, f]) => {
287
- if ((f == null ? void 0 : f.type) === M) {
288
- const g = await R({
289
- req: t,
290
- depth: e + 1,
291
- state: o,
292
- componentId: f.componentId,
293
- locale: i,
294
- defaultLocale: a,
295
- properties: f.properties
296
- });
297
- g && (Object.assign(s.components, g.components), Object.assign(s.props, {
298
- [l]: {
299
- type: M,
300
- componentId: f.componentId,
301
- props: g.props
302
- }
303
- }));
304
- }
305
- })
306
- ), s;
307
- }
308
- function pe({
309
- state: t,
310
- componentId: e,
311
- locale: o,
312
- properties: n
313
- }) {
314
- const i = C({ state: t, componentId: e });
315
- if (!i) return null;
316
- const a = U({
317
- componentId: e,
318
- getComponent: (r) => C({ state: t, componentId: r }),
319
- locale: o,
320
- defaultLocale: t.config.defaultLocale,
321
- properties: n
322
- });
323
- return a ? { component: i, script: a.script, props: { locale: o, ...a.properties } } : null;
324
- }
325
- function C({ state: t, componentId: e }) {
326
- var o, n, i;
327
- return ((o = t.components[e]) == null ? void 0 : o.data) ?? ((i = (n = t.resources.components) == null ? void 0 : n[e]) == null ? void 0 : i.component);
328
- }
329
- function $e({ state: t, name: e }) {
330
- var n, i;
331
- const o = e.toLowerCase();
332
- return ((n = Object.values(t.components).find((a) => {
333
- var r;
334
- return ((r = a.data.name) == null ? void 0 : r.toLowerCase()) === o;
335
- })) == null ? void 0 : n.data) ?? ((i = t.resources.components && Object.values(t.resources.components).find((a) => {
336
- var r;
337
- return ((r = a.component.name) == null ? void 0 : r.toLowerCase()) === o;
338
- })) == null ? void 0 : i.component);
339
- }
340
- async function le({
341
- component: t,
342
- script: e,
343
- props: o,
344
- req: n
345
- }) {
346
- const i = await k(e, "getServerSideProps");
347
- if (!i) return null;
348
- const a = new Promise((r, c) => {
349
- setTimeout(() => c(new Error("getServerSideProps timeout in promise race")), N * 1e3);
350
- });
351
- return Promise.race([
352
- a,
353
- _.callFunction({
354
- code: `${i}
355
-
356
- export async function getServerSidePropsWrapper({ location, fetch, props}) {
357
- globalThis.location = location
358
- globalThis.fetch = fetch
359
-
360
- return getServerSideProps(props);
361
- }
362
- `,
363
- filename: `${t.name || t.id}.js`,
364
- moduleLoader: (r) => {
365
- if (r === "@blocklet/pages-kit/builtin/utils")
366
- return `const { joinURL, withQuery, getQuery, getComponentMountPoint } = globalThis
367
- export { joinURL, withQuery, getQuery, getComponentMountPoint }
368
- `;
369
- if (r === "@blocklet/pages-kit/builtin/dayjs")
370
- return D.dayjs;
371
- },
372
- global: {
373
- console: {
374
- // NOTE: do not return logger.xxx result, it will cause memory leak
375
- // eslint-disable-next-line prettier/prettier
376
- log: (...r) => {
377
- h.info(...r);
378
- },
379
- warn: (...r) => {
380
- h.warn(...r);
381
- },
382
- error: (...r) => {
383
- h.error(...r);
384
- }
385
- },
386
- getComponentMountPoint: v,
387
- joinURL: y,
388
- withQuery: K,
389
- getQuery: G
390
- },
391
- functionName: "getServerSidePropsWrapper",
392
- args: [
393
- {
394
- // NOTE: IMPORTANT! place location and fetch (has side effect) at the args not global
395
- // because the global is shared between all runtime and just init once
396
- location: { href: y(Q(n.hostname), n.originalUrl) },
397
- fetch: (r, { ...c } = {}) => {
398
- const u = typeof r == "string" && r.startsWith("/") ? y(T.env.appUrl, r) : r;
399
- if (typeof u == "string" && j(u).host === j(T.env.appUrl).host) {
400
- const s = n.get("cookie");
401
- if (s) {
402
- const l = new J(c.headers);
403
- l.set("cookie", s), c.headers = l;
404
- }
405
- }
406
- return B(u, {
407
- ...c,
408
- timeout: N * 1e3
409
- }).then((s) => ({
410
- ok: s.ok,
411
- status: s.status,
412
- statusText: s.statusText,
413
- headers: Object.fromEntries(s.headers.entries()),
414
- json: () => s.json()
415
- }));
416
- },
417
- props: o
418
- }
419
- ]
420
- })
421
- ]);
422
- }
423
- const Ne = O(
424
- async (t, e, o) => {
425
- const n = await k(t, e);
426
- return n ? await _.callFunction({
427
- code: `
428
- ${n}
429
-
430
- export function get${e}SchemaWrapper() {
431
- return ${e};
432
- }
433
- `,
434
- filename: `${o}.js`,
435
- functionName: `get${e}SchemaWrapper`
436
- }) : null;
437
- },
438
- {
439
- keyGenerator: (t, e, o) => JSON.stringify(["getExportSchemaValueFromCode", t, e, o])
440
- }
441
- );
442
- export {
443
- re as C,
444
- N as G,
445
- Ie as a,
446
- se as b,
447
- je as c,
448
- ce as d,
449
- ie as e,
450
- C as f,
451
- Ne as g,
452
- $e as h,
453
- h as l
454
- };