@monkeyplus/flow 5.0.0-rc.2 → 5.0.0-rc.20
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/core/runtime/nitro/flow.mjs +1 -1
- package/dist/core/runtime/nitro/renderer.mjs +18 -7
- package/dist/index.mjs +38 -29
- package/dist/pages/runtime/index.d.ts +2 -2
- package/dist/pages/runtime/index.mjs +8 -4
- package/dist/pages/runtime/plugin.mjs +30 -12
- package/dist/vite-client/runtime/plugin.mjs +1 -2
- package/package.json +19 -8
- package/build.config.ts +0 -25
- package/src/app/composables/index.ts +0 -20
- package/src/app/entry.ts +0 -36
- package/src/app/flow.ts +0 -157
- package/src/app/index.ts +0 -5
- package/src/auto-imports/module.ts +0 -143
- package/src/auto-imports/presets.ts +0 -49
- package/src/auto-imports/transform.ts +0 -48
- package/src/core/app.ts +0 -90
- package/src/core/builder.ts +0 -60
- package/src/core/flow.ts +0 -93
- package/src/core/modules.ts +0 -32
- package/src/core/nitro.ts +0 -206
- package/src/core/plugins/import-protection.ts +0 -49
- package/src/core/plugins/unctx.ts +0 -31
- package/src/core/runtime/nitro/flow.ts +0 -43
- package/src/core/runtime/nitro/paths.ts +0 -20
- package/src/core/runtime/nitro/renderer.ts +0 -74
- package/src/core/templates.ts +0 -119
- package/src/core/vite/builder/css.ts +0 -28
- package/src/core/vite/builder/dev-bundler.ts +0 -248
- package/src/core/vite/builder/index.ts +0 -96
- package/src/core/vite/builder/manifest.ts +0 -33
- package/src/core/vite/builder/plugins/analyze.ts +0 -32
- package/src/core/vite/builder/plugins/cache-dir.ts +0 -13
- package/src/core/vite/builder/plugins/dynamic-base.ts +0 -64
- package/src/core/vite/builder/plugins/virtual.ts +0 -45
- package/src/core/vite/builder/server.ts +0 -164
- package/src/core/vite/builder/types/index.ts +0 -13
- package/src/core/vite/builder/utils/index.ts +0 -53
- package/src/core/vite/builder/utils/warmup.ts +0 -27
- package/src/core/vite/builder/utils/wpfs.ts +0 -7
- package/src/core/vite/builder/vite-node.ts +0 -110
- package/src/core/vite/client/index.ts +0 -63
- package/src/dirs.ts +0 -8
- package/src/head/module.ts +0 -37
- package/src/head/runtime/composables.ts +0 -16
- package/src/head/runtime/index.ts +0 -1
- package/src/head/runtime/plugin.ts +0 -12
- package/src/index.ts +0 -2
- package/src/pages/module.ts +0 -55
- package/src/pages/runtime/helpers/chunks.ts +0 -0
- package/src/pages/runtime/helpers/index.ts +0 -33
- package/src/pages/runtime/index.ts +0 -9
- package/src/pages/runtime/plugin.ts +0 -65
- package/src/pages/templates.ts +0 -20
- package/src/pages/utils.ts +0 -49
- package/src/vite-client/module.ts +0 -84
- package/src/vite-client/runtime/injectManifest.ts +0 -188
- package/src/vite-client/runtime/plugin.ts +0 -33
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { defineEventHandler } from "h3";
|
|
2
1
|
import "node-fetch-native/polyfill";
|
|
2
|
+
import { defineEventHandler } from "h3";
|
|
3
3
|
import { useRuntimeConfig } from "#internal/nitro";
|
|
4
4
|
const getServerApp = cachedImport(() => import("#server"));
|
|
5
5
|
const getFlowRenderer = cachedResult(async () => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { eventHandler, useQuery } from "h3";
|
|
2
|
+
import { defineCachedFunction } from "#internal/nitro";
|
|
2
3
|
export default eventHandler(async (event) => {
|
|
3
4
|
const url = event.req.url?.split("?")[0];
|
|
4
5
|
const flow = event.context.flow;
|
|
@@ -16,20 +17,21 @@ export default eventHandler(async (event) => {
|
|
|
16
17
|
if (view.bundle)
|
|
17
18
|
await flow.callHook("page:chunks", view.bundle, chunks);
|
|
18
19
|
const templateContext = {};
|
|
19
|
-
const
|
|
20
|
+
const dynamic = params?._;
|
|
20
21
|
const contextPage = {};
|
|
21
|
-
const
|
|
22
|
+
const contextInject = {};
|
|
23
|
+
const utils = Object.assign({ getLocale: () => locale, injectContext: (key, value) => contextInject[key] = value, defineCachedFunction }, flow.app.utils);
|
|
22
24
|
templateContext.url = url;
|
|
23
25
|
templateContext.locale = locale;
|
|
24
26
|
templateContext.view = view;
|
|
25
|
-
if (
|
|
27
|
+
if (dynamic && page.dynamic) {
|
|
26
28
|
const pages = await page.dynamic.method({ locale, utils });
|
|
27
|
-
const dynamicPage = pages.find((_page) => _page.url ===
|
|
29
|
+
const dynamicPage = pages.find((_page) => _page.url === dynamic);
|
|
28
30
|
if (!dynamicPage)
|
|
29
31
|
return;
|
|
30
32
|
if (page.dynamic.assign)
|
|
31
33
|
templateContext[page.dynamic.assign] = dynamicPage.context;
|
|
32
|
-
contextPage.
|
|
34
|
+
contextPage.dynamic = dynamicPage;
|
|
33
35
|
}
|
|
34
36
|
templateContext.seo = {};
|
|
35
37
|
templateContext.context = {};
|
|
@@ -46,11 +48,20 @@ export default eventHandler(async (event) => {
|
|
|
46
48
|
templateContext.getBodyChunks = () => {
|
|
47
49
|
return chunks.body.join("\n");
|
|
48
50
|
};
|
|
51
|
+
templateContext.getInjectContext = () => {
|
|
52
|
+
return `<script id="__FLOW_DATA__" type="application/json">${JSON.stringify(contextInject)}<\/script>`;
|
|
53
|
+
};
|
|
49
54
|
const query = useQuery(event);
|
|
50
55
|
if (query?.context)
|
|
51
56
|
return { ...templateContext, utils: Object.keys(utils) };
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
let html;
|
|
58
|
+
if (view.render)
|
|
59
|
+
html = await view.render(templateContext, event);
|
|
60
|
+
else
|
|
61
|
+
html = await flow.render(view, templateContext);
|
|
62
|
+
event.res.setHeader("Content-Type", view.contentType || "text/html;charset=UTF-8");
|
|
63
|
+
if (view.postRender)
|
|
64
|
+
html = await view.postRender(html, event);
|
|
54
65
|
if (flow.generate) {
|
|
55
66
|
await flow.callHook("page:generate", generate);
|
|
56
67
|
globalThis.generate = generate;
|
package/dist/index.mjs
CHANGED
|
@@ -27,7 +27,7 @@ import { isExternal as isExternal$1, ExternalsDefaults } from 'externality';
|
|
|
27
27
|
import { createHash } from 'node:crypto';
|
|
28
28
|
import MagicString from 'magic-string';
|
|
29
29
|
|
|
30
|
-
const version = "5.0.0-rc.
|
|
30
|
+
const version = "5.0.0-rc.20";
|
|
31
31
|
|
|
32
32
|
let _distDir = dirname(fileURLToPath(import.meta.url));
|
|
33
33
|
if (_distDir.endsWith("chunks"))
|
|
@@ -87,6 +87,13 @@ const commonPresets = [
|
|
|
87
87
|
imports: [
|
|
88
88
|
"useHead"
|
|
89
89
|
]
|
|
90
|
+
}),
|
|
91
|
+
defineUnimportPreset({
|
|
92
|
+
from: "#_pages",
|
|
93
|
+
imports: [
|
|
94
|
+
"definePage",
|
|
95
|
+
"defineDinamycPage"
|
|
96
|
+
]
|
|
90
97
|
})
|
|
91
98
|
];
|
|
92
99
|
const appPreset = defineUnimportPreset({
|
|
@@ -223,11 +230,11 @@ ${ctx.generateTypeDecarations({ resolvePath: r })}`
|
|
|
223
230
|
|
|
224
231
|
async function resolvePagesRoutes() {
|
|
225
232
|
const nuxt = useNuxt();
|
|
226
|
-
const pagesDirs = [
|
|
233
|
+
const pagesDirs = [resolve(nuxt.options.srcDir, nuxt.options.dir?.pages || "pages")];
|
|
227
234
|
const allRoutes = (await Promise.all(pagesDirs.map(async (dir) => {
|
|
228
235
|
const files = await resolveFilesFlow(dir, `**/*{${nuxt.options.extensions.join(",")}}`);
|
|
229
236
|
files.sort();
|
|
230
|
-
return files.map((file) => {
|
|
237
|
+
return files.filter((file) => !file.includes(" copy")).map((file) => {
|
|
231
238
|
const segments = relative(dir, file).replace(new RegExp(`${escapeRE(extname(file))}$`), "").split("/").join("_");
|
|
232
239
|
return {
|
|
233
240
|
file,
|
|
@@ -241,7 +248,10 @@ function normalizePages(pages) {
|
|
|
241
248
|
const imports = pages.map((page) => genImport(page.file, [{ name: "pages", as: page.name }])).join("\n");
|
|
242
249
|
return {
|
|
243
250
|
imports,
|
|
244
|
-
exports: pages.reduce((acc, curr) =>
|
|
251
|
+
exports: pages.reduce((acc, curr) => {
|
|
252
|
+
const name = curr.name;
|
|
253
|
+
return `${name}:typeof ${name}==='function'?${name}():${name},${acc}`;
|
|
254
|
+
}, "")
|
|
245
255
|
};
|
|
246
256
|
}
|
|
247
257
|
|
|
@@ -263,22 +273,14 @@ const pagesModule = defineNuxtModule({
|
|
|
263
273
|
},
|
|
264
274
|
async setup(_options, flow) {
|
|
265
275
|
const runtimeDir = resolve(distDir, "pages/runtime");
|
|
276
|
+
flow.options.alias["#_pages"] = runtimeDir;
|
|
266
277
|
const pages = [];
|
|
267
|
-
flow.hook("builder:watch", async (event, path) => {
|
|
268
|
-
const dirs = [
|
|
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
278
|
const options = { pages, buildDir: flow.options.buildDir };
|
|
278
279
|
addTemplate({
|
|
279
280
|
...pagesTypeTemplate,
|
|
280
281
|
options
|
|
281
282
|
});
|
|
283
|
+
const pagesDirs = [{ path: resolve(flow.options.srcDir, flow.options.dir.pages) }];
|
|
282
284
|
flow.options.alias["#pages"] = resolve(flow.options.buildDir, "pages.mjs");
|
|
283
285
|
addTemplate({
|
|
284
286
|
filename: "pages.mjs",
|
|
@@ -294,6 +296,15 @@ const pagesModule = defineNuxtModule({
|
|
|
294
296
|
flow.hook("prepare:types", ({ references }) => {
|
|
295
297
|
references.push({ path: resolve(flow.options.buildDir, "pages.d.ts") });
|
|
296
298
|
});
|
|
299
|
+
flow.hook("builder:watch", async (event, path) => {
|
|
300
|
+
if (!["add", "unlink"].includes(event))
|
|
301
|
+
return;
|
|
302
|
+
if (path.includes(" copy"))
|
|
303
|
+
return;
|
|
304
|
+
const fPath = resolve(flow.options.rootDir, path);
|
|
305
|
+
if (pagesDirs.find((dir) => fPath.startsWith(dir.path)))
|
|
306
|
+
await flow.callHook("builder:generateApp");
|
|
307
|
+
});
|
|
297
308
|
addPlugin({ src: resolve(runtimeDir, "plugin") });
|
|
298
309
|
}
|
|
299
310
|
});
|
|
@@ -378,23 +389,21 @@ const viteModule = defineFlowModule({
|
|
|
378
389
|
}
|
|
379
390
|
});
|
|
380
391
|
} else {
|
|
381
|
-
flow.
|
|
392
|
+
const file = resolve(flow.options.rootDir, ".vite/manifest.json");
|
|
393
|
+
addTemplate({
|
|
394
|
+
filename: "viteManifest.mjs",
|
|
395
|
+
async getContents() {
|
|
396
|
+
return [
|
|
397
|
+
"import fs from 'fs';",
|
|
398
|
+
`export default ()=>JSON.parse(fs.readFileSync("${file}", 'utf8'))`
|
|
399
|
+
].join("\n");
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
flow.hook("build:before", async () => {
|
|
382
403
|
const start = Date.now();
|
|
383
404
|
logger$1.info("Building client...");
|
|
384
405
|
await builClient(flow);
|
|
385
|
-
const file = resolve(flow.options.rootDir, ".vite/manifest.json");
|
|
386
|
-
const manifest = await fse.readFile(file, "utf8");
|
|
387
406
|
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
407
|
});
|
|
399
408
|
flow.hook("generate:before", async () => {
|
|
400
409
|
const files = resolve(flow.options.rootDir, ".vite/scripts");
|
|
@@ -486,7 +495,6 @@ async function initNitro(flow) {
|
|
|
486
495
|
"@babel/parser": "unenv/runtime/mock/proxy",
|
|
487
496
|
"#paths": resolve(distDir, "core/runtime/nitro/paths"),
|
|
488
497
|
"#server": "#build/dist/server/server.mjs",
|
|
489
|
-
"#app": flow.options.appDir,
|
|
490
498
|
...flow.options.alias
|
|
491
499
|
},
|
|
492
500
|
rollupConfig: {
|
|
@@ -630,6 +638,7 @@ async function initFlow(flow) {
|
|
|
630
638
|
async function loadFlow(opts) {
|
|
631
639
|
const options = await loadFlowConfig(opts);
|
|
632
640
|
options.appDir = resolve(distDir, "app");
|
|
641
|
+
options.alias["#app"] = resolve(distDir, "app/index");
|
|
633
642
|
options._majorVersion = 3;
|
|
634
643
|
options._modules.push(pagesModule, metaModule, autoImportsModule, viteModule);
|
|
635
644
|
options.modulesDir.push(resolve(pkgDir, "node_modules"));
|
|
@@ -1158,7 +1167,7 @@ const buildServer = async (ctx) => {
|
|
|
1158
1167
|
await onBuild();
|
|
1159
1168
|
ctx.flow.callHook("bundler:change", {});
|
|
1160
1169
|
};
|
|
1161
|
-
const doBuild = debounce(_doBuild);
|
|
1170
|
+
const doBuild = debounce(_doBuild, 50);
|
|
1162
1171
|
await _doBuild();
|
|
1163
1172
|
viteServer.watcher.on("all", (_event, file) => {
|
|
1164
1173
|
file = normalize(file);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { DynamicPage, SimplePage } from '@monkeyplus/flow-schema';
|
|
2
|
-
export declare function definePage(
|
|
3
|
-
export declare function defineDinamycPage(
|
|
2
|
+
export declare function definePage(...pages: SimplePage[]): SimplePage[];
|
|
3
|
+
export declare function defineDinamycPage(...pages: DynamicPage[]): DynamicPage[];
|
|
@@ -1,6 +1,10 @@
|
|
|
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
|
+
});
|
|
6
10
|
}
|
|
@@ -1,27 +1,45 @@
|
|
|
1
1
|
import { joinURL } from "ufo";
|
|
2
|
+
import consola from "consola";
|
|
2
3
|
import { definePage } from "./helpers/index.mjs";
|
|
3
4
|
import { defineFlowPlugin, useRuntimeConfig } from "#app";
|
|
4
5
|
import pages from "#pages";
|
|
5
6
|
export default defineFlowPlugin(async (flow) => {
|
|
6
7
|
const { app } = useRuntimeConfig();
|
|
7
8
|
const allPages = [];
|
|
8
|
-
Object.entries(pages)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
const basePages = Object.entries(pages);
|
|
10
|
+
consola.success("Parsed %i pages files", basePages.length);
|
|
11
|
+
basePages.forEach(([name, _pages]) => {
|
|
12
|
+
const __pages = Array.isArray(_pages) ? _pages : [_pages];
|
|
13
|
+
__pages.forEach((page) => {
|
|
14
|
+
const { getPages } = definePage({
|
|
15
|
+
name,
|
|
16
|
+
...page
|
|
17
|
+
});
|
|
18
|
+
const _pages2 = getPages(app.locale.location, app.locale.language);
|
|
19
|
+
_pages2.forEach((page2) => {
|
|
20
|
+
flow.router.byUrl.insert(page2.url, page2.context);
|
|
21
|
+
flow.router.byName.insert(page2.name, page2.context);
|
|
22
|
+
allPages.push(page2.context);
|
|
23
|
+
});
|
|
18
24
|
});
|
|
19
25
|
});
|
|
20
|
-
|
|
26
|
+
const cache = {};
|
|
27
|
+
async function getUrl(namePage, localeCode) {
|
|
21
28
|
const code = localeCode || this?.getLocale()?.code;
|
|
22
29
|
const [lang, loc] = code.split("-");
|
|
23
30
|
const name = joinURL("/", loc, lang, namePage);
|
|
24
|
-
const { path } = flow.router.byName.lookup(name) || {};
|
|
31
|
+
const { path, params, page } = flow.router.byName.lookup(name) || {};
|
|
32
|
+
if (params?._ && page.dynamic) {
|
|
33
|
+
if (!cache[path]) {
|
|
34
|
+
cache[path] = this.defineCachedFunction(async (_ctx) => {
|
|
35
|
+
return await page.dynamic.method(_ctx);
|
|
36
|
+
}, { maxAge: 8, getKey: () => path, swr: false });
|
|
37
|
+
}
|
|
38
|
+
const fn = cache[path];
|
|
39
|
+
const list = await fn({ utils: this, locale: this.getLocale() });
|
|
40
|
+
const dPage = list.find((el) => el.name === params._);
|
|
41
|
+
return dPage ? path.replace("**", dPage.url) : "/404";
|
|
42
|
+
}
|
|
25
43
|
return path || "/404";
|
|
26
44
|
}
|
|
27
45
|
async function getUrls(withLocale = false) {
|
|
@@ -4,10 +4,9 @@ import { defineFlowPlugin } from "#app";
|
|
|
4
4
|
import manifest from "#viteManifest";
|
|
5
5
|
export default defineFlowPlugin((flow) => {
|
|
6
6
|
if (typeof manifest === "function") {
|
|
7
|
-
const _manifest = manifest();
|
|
8
7
|
flow.hook("page:chunks", (bundle, chunks) => {
|
|
9
8
|
try {
|
|
10
|
-
const chunk = generateBundle(flow.$config.app || {},
|
|
9
|
+
const chunk = generateBundle(flow.$config.app || {}, manifest(), `client/pages/${bundle}.ts`);
|
|
11
10
|
if (chunk) {
|
|
12
11
|
chunks.head.push(chunk.head);
|
|
13
12
|
chunks.body.push(chunk.body);
|
package/package.json
CHANGED
|
@@ -1,22 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monkeyplus/flow",
|
|
3
|
-
"version": "5.0.0-rc.
|
|
3
|
+
"version": "5.0.0-rc.20",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"main": "./dist/index.mjs",
|
|
7
|
+
"types": "./types.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"flow": "./bin/flow.mjs"
|
|
10
|
+
},
|
|
6
11
|
"exports": {
|
|
7
12
|
".": "./dist/index.mjs",
|
|
8
13
|
"./app": "./dist/app/index.mjs",
|
|
9
14
|
"./package.json": "./package.json"
|
|
10
15
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
16
|
+
"imports": {
|
|
17
|
+
"#app": "./dist/app/index.mjs",
|
|
18
|
+
"#head": "./dist/head/runtime/index.mjs",
|
|
19
|
+
"#pages": "./dist/pages/runtime/index.mjs"
|
|
15
20
|
},
|
|
21
|
+
"files": [
|
|
22
|
+
"app.d.ts",
|
|
23
|
+
"bin",
|
|
24
|
+
"types.d.ts",
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
16
27
|
"dependencies": {
|
|
17
|
-
"@monkeyplus/flow-cli": "5.0.0-rc.
|
|
18
|
-
"@monkeyplus/flow-kit": "5.0.0-rc.
|
|
19
|
-
"@monkeyplus/flow-schema": "5.0.0-rc.
|
|
28
|
+
"@monkeyplus/flow-cli": "5.0.0-rc.20",
|
|
29
|
+
"@monkeyplus/flow-kit": "5.0.0-rc.20",
|
|
30
|
+
"@monkeyplus/flow-schema": "5.0.0-rc.20",
|
|
20
31
|
"@rollup/plugin-replace": "^4.0.0",
|
|
21
32
|
"@vueuse/head": "^0.7.6",
|
|
22
33
|
"c12": "^0.2.7",
|
package/build.config.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { BuildEntry } from 'unbuild';
|
|
2
|
-
import { defineBuildConfig } from 'unbuild';
|
|
3
|
-
|
|
4
|
-
export default defineBuildConfig({
|
|
5
|
-
declaration: true,
|
|
6
|
-
entries: [
|
|
7
|
-
'src/index',
|
|
8
|
-
{ input: 'src/app/', outDir: 'dist/app/' },
|
|
9
|
-
...[
|
|
10
|
-
'core',
|
|
11
|
-
'head',
|
|
12
|
-
'pages',
|
|
13
|
-
'vite-client',
|
|
14
|
-
].map((name) => ({ input: `src/${name}/runtime/`, outDir: `dist/${name}/runtime`, format: 'esm' } as BuildEntry)),
|
|
15
|
-
// { input: 'src/runtime/', outDir: 'dist/runtime', format: 'esm' },
|
|
16
|
-
],
|
|
17
|
-
dependencies: [
|
|
18
|
-
'@monkeyplus/flow-cli',
|
|
19
|
-
'ohmyfetch',
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
externals: [
|
|
23
|
-
|
|
24
|
-
],
|
|
25
|
-
});
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
//* fake to nuxt Compatibilities
|
|
3
|
-
import type { RouteLocationNormalizedLoaded } from 'vue-router';
|
|
4
|
-
export function useCookie(name: string, _opts?: any) {
|
|
5
|
-
// console.log('Cookie', name, _opts);
|
|
6
|
-
|
|
7
|
-
return {};
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function refreshNuxtData(keys?: string | string[]): Promise<void> {
|
|
11
|
-
return Promise.resolve();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function useAsyncData(key: string, handlers: () => Promise<any>) {
|
|
15
|
-
return handlers();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function useRoute(): RouteLocationNormalizedLoaded {
|
|
19
|
-
return {} as any;
|
|
20
|
-
}
|
package/src/app/entry.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import type { RuntimeConfig } from '@monkeyplus/flow-schema';
|
|
3
|
-
import { config, configure, render } from 'eta';
|
|
4
|
-
import { applyPlugins, createFlowApp } from './flow';
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
import plugins from '#build/plugins';
|
|
7
|
-
|
|
8
|
-
export default async(runtimeConfig: RuntimeConfig) => {
|
|
9
|
-
const flow = createFlowApp({ runtimeConfig });
|
|
10
|
-
try {
|
|
11
|
-
await applyPlugins(flow, plugins);
|
|
12
|
-
// console.log(flow.eta);
|
|
13
|
-
|
|
14
|
-
configure(flow.eta);
|
|
15
|
-
flow.render = (view: Record<string, string>, data: any) => {
|
|
16
|
-
// eslint-disable-next-line no-template-curly-in-string
|
|
17
|
-
const layout = '<%layout(`layouts/${it.view?.layout||\'default\'}`)%>';
|
|
18
|
-
const getTemplate = flow.engines[view.engine || flow.engine];
|
|
19
|
-
const base = `${layout}
|
|
20
|
-
${getTemplate(view.template)}`;
|
|
21
|
-
return render(base, data || {}, {
|
|
22
|
-
...config,
|
|
23
|
-
async: true,
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
// await flow.hooks.callHook('app:created', vueApp);
|
|
28
|
-
}
|
|
29
|
-
catch (err) {
|
|
30
|
-
console.log(err);
|
|
31
|
-
// await flow.callHook('app:error', err);
|
|
32
|
-
// ssrContext.error = ssrContext.error || err;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return flow;
|
|
36
|
-
};
|
package/src/app/flow.ts
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import type { RadixRouter } from 'radix3';
|
|
2
|
-
import { createRouter } from 'radix3';
|
|
3
|
-
import type { Hookable } from 'hookable';
|
|
4
|
-
import { createHooks } from 'hookable';
|
|
5
|
-
import type { FlowPage, RuntimeConfig } from '@monkeyplus/flow-schema';
|
|
6
|
-
import { getContext } from 'unctx';
|
|
7
|
-
import { globby } from 'globby';
|
|
8
|
-
import { join, resolve } from 'pathe';
|
|
9
|
-
|
|
10
|
-
const flowAppCtx = getContext<FlowApp>('flow-app');
|
|
11
|
-
|
|
12
|
-
type HookResult = Promise<void> | void;
|
|
13
|
-
export interface FlowAppHooks{
|
|
14
|
-
'flow:pages': (page: FlowPage[]) => HookResult
|
|
15
|
-
'eta:plugin': (options: any) => HookResult
|
|
16
|
-
'page:scripts': (scripts: { head: string[]; bodyEnd: string[]; bodyStart: string[] }) => HookResult
|
|
17
|
-
'page:links': (links: string[]) => HookResult
|
|
18
|
-
'page:chunks': (bundle: string, scripts: { head: string[]; body: string[] }) => HookResult
|
|
19
|
-
'page:generate': (generate: any) => HookResult
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
export interface FlowApp {
|
|
23
|
-
router: { byUrl: RadixRouter<FlowPage['context']>; byName: RadixRouter<FlowPage['context']> }
|
|
24
|
-
hooks: Hookable<FlowAppHooks>
|
|
25
|
-
hook: FlowApp['hooks']['hook']
|
|
26
|
-
callHook: FlowApp['hooks']['callHook']
|
|
27
|
-
eta: any
|
|
28
|
-
app: {
|
|
29
|
-
utils: Record<string, any>
|
|
30
|
-
plugins: Record<string, any>
|
|
31
|
-
globals: Record<string, any>
|
|
32
|
-
}
|
|
33
|
-
engine: string
|
|
34
|
-
|
|
35
|
-
engines: Record<string, (template: string) => string>
|
|
36
|
-
generate: boolean
|
|
37
|
-
render: (view: Record<string, string>, data: any) => Promise<string>|void|string
|
|
38
|
-
provide: (name: string, value: any) => void
|
|
39
|
-
setUtil: (name: string, value: any) => void
|
|
40
|
-
|
|
41
|
-
[key: string]: any
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const FlowPluginIndicator = '__flow_plugin';
|
|
46
|
-
export const NuxtPluginIndicator = '__nuxt_plugin';
|
|
47
|
-
|
|
48
|
-
export interface Plugin<Injections extends Record<string, any> = Record<string, any>> {
|
|
49
|
-
(flow: FlowApp): Promise<void> | Promise<{ provide?: Injections }> | void | { provide?: Injections }
|
|
50
|
-
[FlowPluginIndicator]?: true
|
|
51
|
-
}
|
|
52
|
-
interface OptionsApp {
|
|
53
|
-
runtimeConfig: RuntimeConfig
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function createFlowApp({ runtimeConfig }: OptionsApp): FlowApp {
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
-
const viewsDir = join(runtimeConfig.app.rootDir, 'views');
|
|
60
|
-
|
|
61
|
-
const flowApp: FlowApp = {
|
|
62
|
-
router: { byUrl: createRouter(), byName: createRouter() },
|
|
63
|
-
app: {
|
|
64
|
-
utils: {},
|
|
65
|
-
globals: {},
|
|
66
|
-
},
|
|
67
|
-
eta: {
|
|
68
|
-
views: viewsDir,
|
|
69
|
-
plugins: [],
|
|
70
|
-
},
|
|
71
|
-
engine: 'eta',
|
|
72
|
-
engines: { },
|
|
73
|
-
generate: runtimeConfig.generate,
|
|
74
|
-
} as FlowApp;
|
|
75
|
-
|
|
76
|
-
flowApp.engines.eta = (template: string) => {
|
|
77
|
-
return `<%~ await includeFile('templates/${template}',it) %>`;
|
|
78
|
-
};
|
|
79
|
-
flowApp.hooks = createHooks<FlowAppHooks>();
|
|
80
|
-
flowApp.hook = flowApp.hooks.hook;
|
|
81
|
-
flowApp.callHook = flowApp.hooks.callHook;
|
|
82
|
-
|
|
83
|
-
flowApp.provide = (name: string, value: any) => {
|
|
84
|
-
const $name = `$${name}`;
|
|
85
|
-
defineGetter(flowApp, $name, value);
|
|
86
|
-
// defineGetter(nuxtApp.vueApp.config.globalProperties, $name, value);
|
|
87
|
-
};
|
|
88
|
-
flowApp.setUtil = (name: string, method: any) => {
|
|
89
|
-
flowApp.app.utils[name] = method;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
flowApp.hook('eta:plugin', (_plugin) => {
|
|
93
|
-
flowApp.eta.plugins.push(_plugin);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
flowApp.provide('config', runtimeConfig);
|
|
97
|
-
|
|
98
|
-
return flowApp;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function defineFlowPlugin<T>(plugin: Plugin<T>) {
|
|
102
|
-
plugin[FlowPluginIndicator] = true;
|
|
103
|
-
return plugin;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function defineNuxtPlugin<T>(plugin: Plugin<T>) {
|
|
107
|
-
plugin[NuxtPluginIndicator] = true;
|
|
108
|
-
return plugin;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export async function applyPlugin(flowApp: FlowApp, plugin: Plugin) {
|
|
112
|
-
if (typeof plugin !== 'function') return;
|
|
113
|
-
const { provide } = await callWithFlow(flowApp, plugin, [flowApp]) || {};
|
|
114
|
-
if (provide && typeof provide === 'object') {
|
|
115
|
-
for (const key in provide)
|
|
116
|
-
flowApp.provide(key, provide[key]);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
export async function applyPlugins(flowApp: FlowApp, plugins: Plugin[]) {
|
|
120
|
-
for (const plugin of plugins)
|
|
121
|
-
await applyPlugin(flowApp, plugin);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Ensures that the setup function passed in has access to the Nuxt instance via `useNuxt`.
|
|
126
|
-
*
|
|
127
|
-
* @param flow A Flow instance
|
|
128
|
-
* @param setup The function to call
|
|
129
|
-
*/
|
|
130
|
-
export function callWithFlow<T extends(...args: any[]) => any> (flow: FlowApp, setup: T, args?: Parameters<T>) {
|
|
131
|
-
const fn = () => args ? setup(...args as Parameters<T>) : setup();
|
|
132
|
-
// TODO: posible error
|
|
133
|
-
flowAppCtx.set(flow);
|
|
134
|
-
return flowAppCtx.callAsync<ReturnType<T>>(flow, fn);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Returns the current Nuxt instance.
|
|
138
|
-
*/
|
|
139
|
-
export function useFlowApp() {
|
|
140
|
-
const flowAppInstance = flowAppCtx.use();
|
|
141
|
-
if (!flowAppInstance)
|
|
142
|
-
throw new Error('flow instance unavailable');
|
|
143
|
-
|
|
144
|
-
return flowAppInstance;
|
|
145
|
-
}
|
|
146
|
-
export function useRuntimeConfig(): RuntimeConfig {
|
|
147
|
-
return useFlowApp().$config;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function defineGetter<K extends string | number | symbol, V>(obj: Record<K, V>, key: K, val: V) {
|
|
151
|
-
Object.defineProperty(obj, key, { get: () => val });
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export async function resolveFiles(path: string, pattern: string | string[]) {
|
|
155
|
-
const files = await globby(pattern, { cwd: path, followSymbolicLinks: true });
|
|
156
|
-
return files.filter((file) => !file.includes('copy.ts')).map((p) => resolve(path, p));
|
|
157
|
-
}
|