@monkeyplus/flow 5.0.0-rc.3 → 5.0.0-rc.30
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/dist/app/flow.d.ts +2 -2
- package/dist/core/runtime/nitro/flow.mjs +3 -2
- package/dist/core/runtime/nitro/renderer.mjs +25 -11
- package/dist/index.mjs +99 -48
- package/dist/pages/runtime/index.d.ts +9 -3
- package/dist/pages/runtime/index.mjs +11 -4
- package/dist/pages/runtime/pages.mjs +90 -0
- package/dist/vite-client/runtime/plugin.mjs +3 -2
- package/package.json +5 -5
- package/dist/pages/runtime/plugin.mjs +0 -53
- /package/dist/pages/runtime/{plugin.d.ts → pages.d.ts} +0 -0
package/dist/app/flow.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { RadixRouter } from 'radix3';
|
|
2
2
|
import type { Hookable } from 'hookable';
|
|
3
|
-
import type { FlowPage, RuntimeConfig } from '@monkeyplus/flow-schema';
|
|
3
|
+
import type { FlowAppUtils, FlowPage, RuntimeConfig } from '@monkeyplus/flow-schema';
|
|
4
4
|
declare type HookResult = Promise<void> | void;
|
|
5
5
|
export interface FlowAppHooks {
|
|
6
6
|
'flow:pages': (page: FlowPage[]) => HookResult;
|
|
@@ -27,7 +27,7 @@ export interface FlowApp {
|
|
|
27
27
|
callHook: FlowApp['hooks']['callHook'];
|
|
28
28
|
eta: any;
|
|
29
29
|
app: {
|
|
30
|
-
utils:
|
|
30
|
+
utils: FlowAppUtils;
|
|
31
31
|
plugins: Record<string, any>;
|
|
32
32
|
globals: Record<string, any>;
|
|
33
33
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { defineEventHandler } from "h3";
|
|
2
1
|
import "node-fetch-native/polyfill";
|
|
3
|
-
import {
|
|
2
|
+
import { defineEventHandler } from "h3";
|
|
3
|
+
import { defineCachedFunction, useRuntimeConfig } from "#internal/nitro";
|
|
4
4
|
const getServerApp = cachedImport(() => import("#server"));
|
|
5
5
|
const getFlowRenderer = cachedResult(async () => {
|
|
6
6
|
const createFlowApp = await getServerApp();
|
|
@@ -12,6 +12,7 @@ export default defineEventHandler(async ({ context }) => {
|
|
|
12
12
|
const flow = await getFlowRenderer();
|
|
13
13
|
context.flow = flow;
|
|
14
14
|
context.render = flow.render;
|
|
15
|
+
context.defineCachedFunction = defineCachedFunction;
|
|
15
16
|
});
|
|
16
17
|
function _interopDefault(e) {
|
|
17
18
|
return e && typeof e === "object" && "default" in e ? e.default : e;
|
|
@@ -16,27 +16,32 @@ export default eventHandler(async (event) => {
|
|
|
16
16
|
if (view.bundle)
|
|
17
17
|
await flow.callHook("page:chunks", view.bundle, chunks);
|
|
18
18
|
const templateContext = {};
|
|
19
|
-
const
|
|
20
|
-
const contextPage = {
|
|
21
|
-
|
|
19
|
+
const dynamic = params?._;
|
|
20
|
+
const contextPage = {
|
|
21
|
+
chunks,
|
|
22
|
+
locale
|
|
23
|
+
};
|
|
24
|
+
const contextInject = {};
|
|
25
|
+
const utils = Object.assign({ getLocale: () => locale, injectContext: (key, value) => contextInject[key] = value, defineCachedFunction: event.context.defineCachedFunction }, flow.app.utils);
|
|
22
26
|
templateContext.url = url;
|
|
23
27
|
templateContext.locale = locale;
|
|
24
28
|
templateContext.view = view;
|
|
25
|
-
if (
|
|
29
|
+
if (dynamic && page.dynamic) {
|
|
26
30
|
const pages = await page.dynamic.method({ locale, utils });
|
|
27
|
-
const dynamicPage = pages.find((_page) => _page.url ===
|
|
31
|
+
const dynamicPage = pages.find((_page) => _page.url === dynamic);
|
|
28
32
|
if (!dynamicPage)
|
|
29
33
|
return;
|
|
30
34
|
if (page.dynamic.assign)
|
|
31
35
|
templateContext[page.dynamic.assign] = dynamicPage.context;
|
|
32
|
-
contextPage.
|
|
36
|
+
contextPage.dynamic = dynamicPage;
|
|
33
37
|
}
|
|
34
38
|
templateContext.seo = {};
|
|
35
39
|
templateContext.context = {};
|
|
36
|
-
|
|
40
|
+
const sharedContext = await utils.getSharedContext(utils);
|
|
41
|
+
templateContext.sharedContext = sharedContext || {};
|
|
37
42
|
templateContext.utils = utils;
|
|
38
|
-
templateContext.seo = await page.seo?.({ ...contextPage, utils }) || {};
|
|
39
|
-
templateContext.context = await page.context?.({ ...contextPage, utils }) || {};
|
|
43
|
+
templateContext.seo = await page.seo?.({ ...contextPage, utils, sharedContext }) || {};
|
|
44
|
+
templateContext.context = await page.context?.({ ...contextPage, utils, sharedContext }) || {};
|
|
40
45
|
templateContext.getHeadScripts = () => {
|
|
41
46
|
return scripts.head.join("\n");
|
|
42
47
|
};
|
|
@@ -46,11 +51,20 @@ export default eventHandler(async (event) => {
|
|
|
46
51
|
templateContext.getBodyChunks = () => {
|
|
47
52
|
return chunks.body.join("\n");
|
|
48
53
|
};
|
|
54
|
+
templateContext.getInjectContext = () => {
|
|
55
|
+
return `<script id="__FLOW_DATA__" type="application/json">${JSON.stringify(contextInject)}<\/script>`;
|
|
56
|
+
};
|
|
49
57
|
const query = useQuery(event);
|
|
50
58
|
if (query?.context)
|
|
51
59
|
return { ...templateContext, utils: Object.keys(utils) };
|
|
52
|
-
|
|
53
|
-
|
|
60
|
+
let html;
|
|
61
|
+
if (view.render)
|
|
62
|
+
html = await view.render(templateContext, event);
|
|
63
|
+
else
|
|
64
|
+
html = await flow.render(view, templateContext);
|
|
65
|
+
event.res.setHeader("Content-Type", view.contentType || "text/html;charset=UTF-8");
|
|
66
|
+
if (view.postRender)
|
|
67
|
+
html = await view.postRender(html, event);
|
|
54
68
|
if (flow.generate) {
|
|
55
69
|
await flow.callHook("page:generate", generate);
|
|
56
70
|
globalThis.generate = generate;
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { createHooks } from 'hookable';
|
|
2
2
|
import { dirname, resolve, normalize, join, isAbsolute, relative, extname } from 'pathe';
|
|
3
3
|
import { defineFlowModule, addPlugin, defineNuxtModule, logger, addTemplate, addPluginTemplate, addVitePlugin, useNuxt, resolveAlias, resolveFilesFlow, nuxtCtx, installModule, loadFlowConfig, templateUtils, normalizeTemplate, compileTemplate, normalizePlugin, isIgnoredFlow } from '@monkeyplus/flow-kit';
|
|
4
|
-
import { fileURLToPath
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
5
|
import { defineUnimportPreset, createUnimport, toImports, scanDirExports } from 'unimport';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
6
7
|
import { createUnplugin } from 'unplugin';
|
|
7
8
|
import { parseURL, parseQuery, joinURL, withoutTrailingSlash } from 'ufo';
|
|
9
|
+
import fs from 'fs';
|
|
8
10
|
import escapeRE from 'escape-string-regexp';
|
|
9
11
|
import { camelCase, pascalCase } from 'scule';
|
|
10
12
|
import { genImport, genDynamicImport, genArrayFromRaw, genString, genObjectFromRawEntries } from 'knitwork';
|
|
@@ -27,7 +29,7 @@ import { isExternal as isExternal$1, ExternalsDefaults } from 'externality';
|
|
|
27
29
|
import { createHash } from 'node:crypto';
|
|
28
30
|
import MagicString from 'magic-string';
|
|
29
31
|
|
|
30
|
-
const version = "5.0.0-rc.
|
|
32
|
+
const version = "5.0.0-rc.30";
|
|
31
33
|
|
|
32
34
|
let _distDir = dirname(fileURLToPath(import.meta.url));
|
|
33
35
|
if (_distDir.endsWith("chunks"))
|
|
@@ -87,6 +89,14 @@ const commonPresets = [
|
|
|
87
89
|
imports: [
|
|
88
90
|
"useHead"
|
|
89
91
|
]
|
|
92
|
+
}),
|
|
93
|
+
defineUnimportPreset({
|
|
94
|
+
from: "#_pages",
|
|
95
|
+
imports: [
|
|
96
|
+
"definePage",
|
|
97
|
+
"defineDinamycPage",
|
|
98
|
+
"defineSharedContext"
|
|
99
|
+
]
|
|
90
100
|
})
|
|
91
101
|
];
|
|
92
102
|
const appPreset = defineUnimportPreset({
|
|
@@ -221,14 +231,20 @@ ${ctx.generateTypeDecarations({ resolvePath: r })}`
|
|
|
221
231
|
});
|
|
222
232
|
}
|
|
223
233
|
|
|
224
|
-
async function
|
|
234
|
+
async function resolveFiles(dir) {
|
|
225
235
|
const nuxt = useNuxt();
|
|
226
|
-
const
|
|
227
|
-
const allRoutes = (await Promise.all(
|
|
228
|
-
const files = await resolveFilesFlow(
|
|
236
|
+
const dirs = [resolve(nuxt.options.srcDir, dir)];
|
|
237
|
+
const allRoutes = (await Promise.all(dirs.map(async (dir2) => {
|
|
238
|
+
const files = await resolveFilesFlow(dir2, `**/*{${nuxt.options.extensions.join(",")}}`);
|
|
229
239
|
files.sort();
|
|
230
|
-
return files.
|
|
231
|
-
|
|
240
|
+
return files.filter((file) => {
|
|
241
|
+
if (file.includes(" copy"))
|
|
242
|
+
return false;
|
|
243
|
+
if (!fs.readFileSync(file, "utf8").includes("export"))
|
|
244
|
+
return false;
|
|
245
|
+
return true;
|
|
246
|
+
}).map((file) => {
|
|
247
|
+
const segments = relative(dir2, file).replace(new RegExp(`${escapeRE(extname(file))}$`), "").split("/").join("_");
|
|
232
248
|
return {
|
|
233
249
|
file,
|
|
234
250
|
name: camelCase(segments)
|
|
@@ -237,21 +253,31 @@ async function resolvePagesRoutes() {
|
|
|
237
253
|
}))).flat();
|
|
238
254
|
return allRoutes;
|
|
239
255
|
}
|
|
240
|
-
function
|
|
241
|
-
const imports =
|
|
256
|
+
function normalizeExports(files) {
|
|
257
|
+
const imports = files.map((page) => genImport(page.file, page.name)).join("\n");
|
|
258
|
+
const exports = files.reduce((acc, curr) => {
|
|
259
|
+
const name = curr.name;
|
|
260
|
+
return `${name}:typeof ${name}==='function'?${name}():${name},${acc}`;
|
|
261
|
+
}, "");
|
|
242
262
|
return {
|
|
243
263
|
imports,
|
|
244
|
-
exports
|
|
264
|
+
exports
|
|
245
265
|
};
|
|
246
266
|
}
|
|
247
267
|
|
|
248
268
|
const pagesTypeTemplate = {
|
|
249
|
-
filename: "pages.d.ts",
|
|
269
|
+
filename: "types/pages.d.ts",
|
|
250
270
|
getContents: ({ options }) => `// Generated by pages discovery
|
|
251
271
|
export {}
|
|
252
272
|
declare global {
|
|
273
|
+
type ArrElement<ArrType> = ArrType extends readonly (infer ElementType)[]
|
|
274
|
+
? ElementType
|
|
275
|
+
: never;
|
|
253
276
|
|
|
254
|
-
${options.pages.map((c) =>
|
|
277
|
+
${options.pages.map((c) => {
|
|
278
|
+
const _type = `ArrElement<typeof ${genDynamicImport(isAbsolute(c.file) ? relative(join(options.buildDir, "types"), c.file) : c.file, { wrapper: false })}['default']>`;
|
|
279
|
+
return `export type ${pascalCase(c.name)}Context=Awaited<ReturnType<${_type}['locales']['es-ec']['context']>>`;
|
|
280
|
+
}).join("\n")}
|
|
255
281
|
}
|
|
256
282
|
export const pagesNames: string[]
|
|
257
283
|
`.replaceAll(".ts", "")
|
|
@@ -263,38 +289,54 @@ const pagesModule = defineNuxtModule({
|
|
|
263
289
|
},
|
|
264
290
|
async setup(_options, flow) {
|
|
265
291
|
const runtimeDir = resolve(distDir, "pages/runtime");
|
|
292
|
+
flow.options.alias["#_pages"] = runtimeDir;
|
|
266
293
|
const pages = [];
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
flow.options.dir.pages,
|
|
270
|
-
flow.options.dir.layouts,
|
|
271
|
-
flow.options.dir.middleware
|
|
272
|
-
].filter(Boolean);
|
|
273
|
-
const pathPattern = new RegExp(`^(${dirs.map(escapeRE).join("|")})/`);
|
|
274
|
-
if (event !== "change" && path.match(pathPattern))
|
|
275
|
-
await flow.callHook("builder:generateApp");
|
|
276
|
-
});
|
|
277
|
-
const options = { pages, buildDir: flow.options.buildDir };
|
|
294
|
+
const contexts = [];
|
|
295
|
+
const options = { pages, buildDir: flow.options.buildDir, contexts };
|
|
278
296
|
addTemplate({
|
|
279
297
|
...pagesTypeTemplate,
|
|
280
298
|
options
|
|
281
299
|
});
|
|
300
|
+
const pagesDirs = [{ path: resolve(flow.options.srcDir, flow.options.dir.pages) }];
|
|
282
301
|
flow.options.alias["#pages"] = resolve(flow.options.buildDir, "pages.mjs");
|
|
302
|
+
flow.options.alias["#pagesContexts"] = resolve(flow.options.buildDir, "pages.contexts.mjs");
|
|
283
303
|
addTemplate({
|
|
284
304
|
filename: "pages.mjs",
|
|
285
305
|
async getContents({ options: options2 }) {
|
|
286
|
-
const { exports, imports } =
|
|
287
|
-
|
|
306
|
+
const { exports, imports } = normalizeExports(options2.pages);
|
|
307
|
+
const module = [imports, `export default {${exports}}`].join("\n");
|
|
308
|
+
return module;
|
|
288
309
|
},
|
|
289
310
|
options
|
|
290
311
|
});
|
|
312
|
+
addTemplate({
|
|
313
|
+
filename: "pages.contexts.mjs",
|
|
314
|
+
async getContents({ options: options2 }) {
|
|
315
|
+
const { exports, imports } = normalizeExports(options2.contexts);
|
|
316
|
+
const module = [imports, `export default {${exports}}`].join("\n");
|
|
317
|
+
return module;
|
|
318
|
+
},
|
|
319
|
+
options
|
|
320
|
+
});
|
|
321
|
+
flow.hook("app:templates", async () => {
|
|
322
|
+
options.pages = await resolveFiles(flow.options.dir?.pages || "pages");
|
|
323
|
+
});
|
|
291
324
|
flow.hook("app:templates", async () => {
|
|
292
|
-
options.
|
|
325
|
+
options.contexts = await resolveFiles("shared/contexts");
|
|
293
326
|
});
|
|
294
327
|
flow.hook("prepare:types", ({ references }) => {
|
|
295
|
-
references.push({ path: resolve(flow.options.buildDir, "pages.d.ts") });
|
|
328
|
+
references.push({ path: resolve(flow.options.buildDir, "types/pages.d.ts") });
|
|
296
329
|
});
|
|
297
|
-
|
|
330
|
+
flow.hook("builder:watch", async (event, path) => {
|
|
331
|
+
if (!["add", "unlink", "change"].includes(event))
|
|
332
|
+
return;
|
|
333
|
+
if (path.includes(" copy"))
|
|
334
|
+
return;
|
|
335
|
+
const fPath = resolve(flow.options.rootDir, path);
|
|
336
|
+
if (pagesDirs.find((dir) => fPath.startsWith(dir.path)))
|
|
337
|
+
await flow.callHook("builder:generateApp");
|
|
338
|
+
});
|
|
339
|
+
addPlugin(resolve(runtimeDir, "pages"));
|
|
298
340
|
}
|
|
299
341
|
});
|
|
300
342
|
|
|
@@ -322,7 +364,7 @@ const createClient = async (flow) => {
|
|
|
322
364
|
if (vite)
|
|
323
365
|
vite?.ws?.send({ type: "full-reload" });
|
|
324
366
|
};
|
|
325
|
-
const doReload = debounce(_doReload,
|
|
367
|
+
const doReload = debounce(_doReload, 75);
|
|
326
368
|
flow.hook("bundler:change", () => {
|
|
327
369
|
doReload();
|
|
328
370
|
});
|
|
@@ -378,23 +420,24 @@ const viteModule = defineFlowModule({
|
|
|
378
420
|
}
|
|
379
421
|
});
|
|
380
422
|
} else {
|
|
381
|
-
flow.
|
|
423
|
+
const file = resolve(flow.options.rootDir, ".vite/manifest.json");
|
|
424
|
+
addTemplate({
|
|
425
|
+
filename: "viteManifest.mjs",
|
|
426
|
+
async getContents() {
|
|
427
|
+
return [
|
|
428
|
+
"import fs from 'fs';",
|
|
429
|
+
`export default ()=>{
|
|
430
|
+
const manifest =JSON.parse(fs.readFileSync("${file}", 'utf8'));
|
|
431
|
+
return {manifest,base:'${_options.dir}'}
|
|
432
|
+
}`
|
|
433
|
+
].join("\n");
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
flow.hook("build:before", async () => {
|
|
382
437
|
const start = Date.now();
|
|
383
438
|
logger$1.info("Building client...");
|
|
384
439
|
await builClient(flow);
|
|
385
|
-
const file = resolve(flow.options.rootDir, ".vite/manifest.json");
|
|
386
|
-
const manifest = await fse.readFile(file, "utf8");
|
|
387
440
|
logger$1.success(`Client build in ${Date.now() - start}ms`);
|
|
388
|
-
addTemplate({
|
|
389
|
-
filename: "viteManifest.mjs",
|
|
390
|
-
async getContents() {
|
|
391
|
-
return [
|
|
392
|
-
"export default ()=>(",
|
|
393
|
-
manifest,
|
|
394
|
-
")"
|
|
395
|
-
].join("\n");
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
441
|
});
|
|
399
442
|
flow.hook("generate:before", async () => {
|
|
400
443
|
const files = resolve(flow.options.rootDir, ".vite/scripts");
|
|
@@ -478,7 +521,8 @@ async function initNitro(flow) {
|
|
|
478
521
|
externals: {
|
|
479
522
|
inline: [
|
|
480
523
|
...flow.options.dev ? [] : ["eta", "@monkeyplus/", "@vue/", "@nuxt/", flow.options.buildDir],
|
|
481
|
-
"@monkeyplus/flow/dist"
|
|
524
|
+
"@monkeyplus/flow/dist",
|
|
525
|
+
"C:/Users/gnu/Documents/GitHub/flow/packages/flow/dist/app"
|
|
482
526
|
]
|
|
483
527
|
},
|
|
484
528
|
alias: {
|
|
@@ -489,7 +533,8 @@ async function initNitro(flow) {
|
|
|
489
533
|
...flow.options.alias
|
|
490
534
|
},
|
|
491
535
|
rollupConfig: {
|
|
492
|
-
plugins: []
|
|
536
|
+
plugins: [],
|
|
537
|
+
external: [""]
|
|
493
538
|
}
|
|
494
539
|
});
|
|
495
540
|
await flow.callHook("nitro:config", nitroConfig);
|
|
@@ -1081,6 +1126,8 @@ const buildServer = async (ctx) => {
|
|
|
1081
1126
|
"/__vue-jsx",
|
|
1082
1127
|
"#app",
|
|
1083
1128
|
/(nuxt|nuxt3)\/(dist|src|app)/,
|
|
1129
|
+
/flow\/(dist|src|app)/,
|
|
1130
|
+
/flow\/modules\/(content|icons|images|netlify|netlify-cms|seo|sitemap|vue)/,
|
|
1084
1131
|
/@monkeyplus\/flow\/(dist|src|app)/,
|
|
1085
1132
|
/@nuxt\/nitro\/(dist|src)/
|
|
1086
1133
|
]
|
|
@@ -1158,9 +1205,11 @@ const buildServer = async (ctx) => {
|
|
|
1158
1205
|
await onBuild();
|
|
1159
1206
|
ctx.flow.callHook("bundler:change", {});
|
|
1160
1207
|
};
|
|
1161
|
-
const doBuild = debounce(_doBuild);
|
|
1208
|
+
const doBuild = debounce(_doBuild, 50);
|
|
1162
1209
|
await _doBuild();
|
|
1163
1210
|
viteServer.watcher.on("all", (_event, file) => {
|
|
1211
|
+
if (file.includes("/pages/"))
|
|
1212
|
+
return;
|
|
1164
1213
|
file = normalize(file);
|
|
1165
1214
|
if (file.indexOf(ctx.flow.options.buildDir) === 0)
|
|
1166
1215
|
return;
|
|
@@ -1280,6 +1329,7 @@ async function bundleVite(flow) {
|
|
|
1280
1329
|
include: []
|
|
1281
1330
|
},
|
|
1282
1331
|
build: {
|
|
1332
|
+
ssr: true,
|
|
1283
1333
|
rollupOptions: {
|
|
1284
1334
|
output: { sanitizeFileName: sanitizeFilePath },
|
|
1285
1335
|
input: resolve(flow.options.appDir, "entry")
|
|
@@ -1316,6 +1366,8 @@ async function bundleVite(flow) {
|
|
|
1316
1366
|
flow.hook("vite:serverCreated", (server) => {
|
|
1317
1367
|
ctx.nuxt.hook("app:templatesGenerated", () => {
|
|
1318
1368
|
for (const [id, mod] of server.moduleGraph.idToModuleMap) {
|
|
1369
|
+
if (id.includes("pages.mjs"))
|
|
1370
|
+
server.moduleGraph.invalidateModule(mod);
|
|
1319
1371
|
if (id.startsWith("\0virtual:"))
|
|
1320
1372
|
server.moduleGraph.invalidateModule(mod);
|
|
1321
1373
|
}
|
|
@@ -1357,8 +1409,7 @@ function watch(flow) {
|
|
|
1357
1409
|
"node_modules"
|
|
1358
1410
|
]
|
|
1359
1411
|
});
|
|
1360
|
-
|
|
1361
|
-
watcher.on("all", watchHook);
|
|
1412
|
+
watcher.on("all", (event, path) => flow.callHook("builder:watch", event, normalize(path)));
|
|
1362
1413
|
flow.hook("close", () => watcher.close());
|
|
1363
1414
|
return watcher;
|
|
1364
1415
|
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
import type { DynamicPage, SimplePage } from '@monkeyplus/flow-schema';
|
|
2
|
-
export declare function definePage(
|
|
3
|
-
export declare function defineDinamycPage(
|
|
1
|
+
import type { DynamicPage, PageCtx, SimplePage } from '@monkeyplus/flow-schema';
|
|
2
|
+
export declare function definePage<T>(...pages: SimplePage<T>[]): SimplePage<T>[];
|
|
3
|
+
export declare function defineDinamycPage<T>(...pages: DynamicPage<T>[]): DynamicPage<T>[];
|
|
4
|
+
interface SharedContext {
|
|
5
|
+
assign?: 'global' | 'local';
|
|
6
|
+
setup: (cxt: PageCtx) => any;
|
|
7
|
+
}
|
|
8
|
+
export declare function defineSharedContext(shared: SharedContext): SharedContext;
|
|
9
|
+
export {};
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
export function definePage(
|
|
2
|
-
return
|
|
1
|
+
export function definePage(...pages) {
|
|
2
|
+
return pages;
|
|
3
3
|
}
|
|
4
|
-
export function defineDinamycPage(
|
|
5
|
-
return page
|
|
4
|
+
export function defineDinamycPage(...pages) {
|
|
5
|
+
return pages.map((page) => {
|
|
6
|
+
if (!page.name.endsWith("/**"))
|
|
7
|
+
page.name = `${page.name}/**`;
|
|
8
|
+
return page;
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export function defineSharedContext(shared) {
|
|
12
|
+
return shared;
|
|
6
13
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { joinURL } from "ufo";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { definePage } from "./helpers/index.mjs";
|
|
4
|
+
import { defineFlowPlugin, useRuntimeConfig } from "#app";
|
|
5
|
+
import pages from "#build/pages";
|
|
6
|
+
import contexts from "#build/pages.contexts";
|
|
7
|
+
export default defineFlowPlugin(async (flow) => {
|
|
8
|
+
const { app } = useRuntimeConfig();
|
|
9
|
+
const allPages = [];
|
|
10
|
+
const basePages = Object.entries(pages);
|
|
11
|
+
consola.success("Parsed %i pages files", basePages.length);
|
|
12
|
+
basePages.forEach(([name, _pages]) => {
|
|
13
|
+
const __pages = Array.isArray(_pages) ? _pages : [_pages];
|
|
14
|
+
__pages.forEach((page) => {
|
|
15
|
+
const { getPages } = definePage({
|
|
16
|
+
name,
|
|
17
|
+
...page
|
|
18
|
+
});
|
|
19
|
+
const _pages2 = getPages(app.locale.location, app.locale.language);
|
|
20
|
+
_pages2.forEach((page2) => {
|
|
21
|
+
flow.router.byUrl.insert(page2.url, page2.context);
|
|
22
|
+
flow.router.byName.insert(page2.name, page2.context);
|
|
23
|
+
allPages.push(page2.context);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
const cache = {};
|
|
28
|
+
async function getUrl(namePage, localeCode) {
|
|
29
|
+
const code = localeCode || this?.getLocale()?.code;
|
|
30
|
+
const [lang, loc] = code.split("-");
|
|
31
|
+
const name = joinURL("/", loc, lang, namePage);
|
|
32
|
+
const { path, params, page } = flow.router.byName.lookup(name) || {};
|
|
33
|
+
if (params?._ && page.dynamic) {
|
|
34
|
+
if (!cache[path]) {
|
|
35
|
+
cache[path] = this.defineCachedFunction(async (_ctx) => {
|
|
36
|
+
return await page.dynamic.method(_ctx);
|
|
37
|
+
}, { maxAge: 8, getKey: () => path, swr: false });
|
|
38
|
+
}
|
|
39
|
+
const fn = cache[path];
|
|
40
|
+
const list = await fn({ utils: this, locale: this.getLocale() });
|
|
41
|
+
const dPage = list.find((el) => el.name === params._);
|
|
42
|
+
return dPage ? path.replace("**", dPage.url) : "/404";
|
|
43
|
+
}
|
|
44
|
+
return path || "/404";
|
|
45
|
+
}
|
|
46
|
+
async function getUrls(withLocale = false) {
|
|
47
|
+
const urls = [];
|
|
48
|
+
for (const page of allPages) {
|
|
49
|
+
if (page.path.includes("/**") && page.page.dynamic) {
|
|
50
|
+
const dPages = await page.page.dynamic.method({
|
|
51
|
+
locale: page.locale,
|
|
52
|
+
utils: Object.assign({ getLocale: () => page.locale }, flow.app.utils),
|
|
53
|
+
chunks: {}
|
|
54
|
+
});
|
|
55
|
+
dPages.forEach((dPage) => {
|
|
56
|
+
const _path = joinURL(page.path.replace("/**", ""), dPage.url);
|
|
57
|
+
urls.push(withLocale ? { url: _path, locale: page.locale.code, name: joinURL(page.name, dPage.name) } : _path);
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
const url = page.path.replaceAll("*", "");
|
|
61
|
+
urls.push(withLocale ? { url, locale: page.locale.code, name: page.name } : url);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return urls.sort();
|
|
65
|
+
}
|
|
66
|
+
async function getSharedContext(utils) {
|
|
67
|
+
const entries = Object.entries(contexts);
|
|
68
|
+
if (!entries.length)
|
|
69
|
+
return {};
|
|
70
|
+
const _contexts = await Promise.all(entries.map(async ([key, method]) => {
|
|
71
|
+
const data = await method.setup(utils);
|
|
72
|
+
return { [key]: data };
|
|
73
|
+
}));
|
|
74
|
+
return _contexts.reduce((acc, curr) => {
|
|
75
|
+
return {
|
|
76
|
+
...acc,
|
|
77
|
+
...curr
|
|
78
|
+
};
|
|
79
|
+
}, {});
|
|
80
|
+
}
|
|
81
|
+
flow.setUtil("getUrl", getUrl);
|
|
82
|
+
flow.setUtil("getUrls", getUrls);
|
|
83
|
+
flow.setUtil("getPages", () => allPages);
|
|
84
|
+
flow.setUtil("getSharedContext", getSharedContext);
|
|
85
|
+
return {
|
|
86
|
+
provide: {
|
|
87
|
+
pages: { allPages }
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
});
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import logger from "consola";
|
|
2
|
+
import { joinURL } from "ufo";
|
|
2
3
|
import { generateBundle } from "./injectManifest.mjs";
|
|
3
4
|
import { defineFlowPlugin } from "#app";
|
|
4
5
|
import manifest from "#viteManifest";
|
|
5
6
|
export default defineFlowPlugin((flow) => {
|
|
6
7
|
if (typeof manifest === "function") {
|
|
7
|
-
const _manifest = manifest();
|
|
8
8
|
flow.hook("page:chunks", (bundle, chunks) => {
|
|
9
9
|
try {
|
|
10
|
-
const
|
|
10
|
+
const data = manifest();
|
|
11
|
+
const chunk = generateBundle(flow.$config.app || {}, data.manifest, joinURL("/", data.base, `${bundle}.ts`).replace("/", ""));
|
|
11
12
|
if (chunk) {
|
|
12
13
|
chunks.head.push(chunk.head);
|
|
13
14
|
chunks.body.push(chunk.body);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monkeyplus/flow",
|
|
3
|
-
"version": "5.0.0-rc.
|
|
3
|
+
"version": "5.0.0-rc.30",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@monkeyplus/flow-cli": "5.0.0-rc.
|
|
29
|
-
"@monkeyplus/flow-kit": "5.0.0-rc.
|
|
30
|
-
"@monkeyplus/flow-schema": "5.0.0-rc.
|
|
28
|
+
"@monkeyplus/flow-cli": "5.0.0-rc.30",
|
|
29
|
+
"@monkeyplus/flow-kit": "5.0.0-rc.30",
|
|
30
|
+
"@monkeyplus/flow-schema": "5.0.0-rc.30",
|
|
31
31
|
"@rollup/plugin-replace": "^4.0.0",
|
|
32
32
|
"@vueuse/head": "^0.7.6",
|
|
33
33
|
"c12": "^0.2.7",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"magic-string": "^0.26.2",
|
|
51
51
|
"mlly": "^0.5.2",
|
|
52
52
|
"mri": "^1.2.0",
|
|
53
|
-
"nitropack": "^0.4.
|
|
53
|
+
"nitropack": "^0.4.8",
|
|
54
54
|
"pathe": "^0.2.0",
|
|
55
55
|
"perfect-debounce": "^0.1.3",
|
|
56
56
|
"radix3": "^0.1.2",
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { joinURL } from "ufo";
|
|
2
|
-
import { definePage } from "./helpers/index.mjs";
|
|
3
|
-
import { defineFlowPlugin, useRuntimeConfig } from "#app";
|
|
4
|
-
import pages from "#pages";
|
|
5
|
-
export default defineFlowPlugin(async (flow) => {
|
|
6
|
-
const { app } = useRuntimeConfig();
|
|
7
|
-
const allPages = [];
|
|
8
|
-
Object.entries(pages).forEach(([name, page]) => {
|
|
9
|
-
const { getPages } = definePage({
|
|
10
|
-
name,
|
|
11
|
-
...page
|
|
12
|
-
});
|
|
13
|
-
const _pages = getPages(app.locale.location, app.locale.language);
|
|
14
|
-
_pages.forEach((page2) => {
|
|
15
|
-
flow.router.byUrl.insert(page2.url, page2.context);
|
|
16
|
-
flow.router.byName.insert(page2.name, page2.context);
|
|
17
|
-
allPages.push(page2.context);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
function getUrl(namePage, localeCode) {
|
|
21
|
-
const code = localeCode || this?.getLocale()?.code;
|
|
22
|
-
const [lang, loc] = code.split("-");
|
|
23
|
-
const name = joinURL("/", loc, lang, namePage);
|
|
24
|
-
const { path } = flow.router.byName.lookup(name) || {};
|
|
25
|
-
return path || "/404";
|
|
26
|
-
}
|
|
27
|
-
async function getUrls(withLocale = false) {
|
|
28
|
-
const urls = [];
|
|
29
|
-
for (const page of allPages) {
|
|
30
|
-
if (!page.path.includes("/**")) {
|
|
31
|
-
urls.push(withLocale ? { url: page.path, locale: page.locale.code, name: page.name } : page.path);
|
|
32
|
-
} else {
|
|
33
|
-
const dPages = await page.page.dynamic.method({
|
|
34
|
-
locale: page.locale,
|
|
35
|
-
utils: Object.assign({ getLocale: () => page.locale }, flow.app.utils)
|
|
36
|
-
});
|
|
37
|
-
dPages.forEach((dPage) => {
|
|
38
|
-
const _path = joinURL(page.path.replace("/**", ""), dPage.url);
|
|
39
|
-
urls.push(withLocale ? { url: _path, locale: page.locale.code, name: joinURL(page.name, dPage.name) } : _path);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return urls.sort();
|
|
44
|
-
}
|
|
45
|
-
flow.setUtil("getUrl", getUrl);
|
|
46
|
-
flow.setUtil("getUrls", getUrls);
|
|
47
|
-
flow.setUtil("getPages", () => allPages);
|
|
48
|
-
return {
|
|
49
|
-
provide: {
|
|
50
|
-
pages: { allPages }
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
});
|
|
File without changes
|