@monkeyplus/flow 5.0.0-rc.199 → 5.0.0-rc.2

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.
Files changed (81) hide show
  1. package/build.config.ts +25 -0
  2. package/dist/app/entry.d.ts +2 -2
  3. package/dist/app/entry.mjs +2 -3
  4. package/dist/app/flow.d.ts +3 -12
  5. package/dist/app/flow.mjs +0 -3
  6. package/dist/core/runtime/nitro/flow.d.ts +0 -1
  7. package/dist/core/runtime/nitro/flow.mjs +14 -9
  8. package/dist/core/runtime/nitro/renderer.mjs +18 -82
  9. package/dist/head/runtime/plugin.mjs +1 -0
  10. package/dist/index.mjs +772 -543
  11. package/dist/pages/runtime/helpers/index.d.ts +1 -1
  12. package/dist/pages/runtime/helpers/index.mjs +3 -14
  13. package/dist/pages/runtime/index.d.ts +3 -10
  14. package/dist/pages/runtime/index.mjs +4 -12
  15. package/dist/pages/runtime/plugin.mjs +53 -0
  16. package/dist/vite-client/runtime/injectManifest.d.ts +26 -0
  17. package/dist/vite-client/runtime/injectManifest.mjs +104 -0
  18. package/dist/vite-client/runtime/plugin.d.ts +2 -0
  19. package/dist/vite-client/runtime/plugin.mjs +27 -0
  20. package/package.json +40 -55
  21. package/src/app/composables/index.ts +20 -0
  22. package/src/app/entry.ts +36 -0
  23. package/src/app/flow.ts +157 -0
  24. package/src/app/index.ts +5 -0
  25. package/src/auto-imports/module.ts +143 -0
  26. package/src/auto-imports/presets.ts +49 -0
  27. package/src/auto-imports/transform.ts +48 -0
  28. package/src/core/app.ts +90 -0
  29. package/src/core/builder.ts +60 -0
  30. package/src/core/flow.ts +93 -0
  31. package/src/core/modules.ts +32 -0
  32. package/src/core/nitro.ts +206 -0
  33. package/src/core/plugins/import-protection.ts +49 -0
  34. package/src/core/plugins/unctx.ts +31 -0
  35. package/src/core/runtime/nitro/flow.ts +43 -0
  36. package/src/core/runtime/nitro/paths.ts +20 -0
  37. package/src/core/runtime/nitro/renderer.ts +74 -0
  38. package/src/core/templates.ts +119 -0
  39. package/src/core/vite/builder/css.ts +28 -0
  40. package/src/core/vite/builder/dev-bundler.ts +248 -0
  41. package/src/core/vite/builder/index.ts +96 -0
  42. package/src/core/vite/builder/manifest.ts +33 -0
  43. package/src/core/vite/builder/plugins/analyze.ts +32 -0
  44. package/src/core/vite/builder/plugins/cache-dir.ts +13 -0
  45. package/src/core/vite/builder/plugins/dynamic-base.ts +64 -0
  46. package/src/core/vite/builder/plugins/virtual.ts +45 -0
  47. package/src/core/vite/builder/server.ts +164 -0
  48. package/src/core/vite/builder/types/index.ts +13 -0
  49. package/src/core/vite/builder/utils/index.ts +53 -0
  50. package/src/core/vite/builder/utils/warmup.ts +27 -0
  51. package/src/core/vite/builder/utils/wpfs.ts +7 -0
  52. package/src/core/vite/builder/vite-node.ts +110 -0
  53. package/src/core/vite/client/index.ts +63 -0
  54. package/src/dirs.ts +8 -0
  55. package/src/head/module.ts +37 -0
  56. package/src/head/runtime/composables.ts +16 -0
  57. package/src/head/runtime/index.ts +1 -0
  58. package/src/head/runtime/plugin.ts +12 -0
  59. package/src/index.ts +2 -0
  60. package/src/pages/module.ts +55 -0
  61. package/src/pages/runtime/helpers/chunks.ts +0 -0
  62. package/src/pages/runtime/helpers/index.ts +33 -0
  63. package/src/pages/runtime/index.ts +9 -0
  64. package/src/pages/runtime/plugin.ts +65 -0
  65. package/src/pages/templates.ts +20 -0
  66. package/src/pages/utils.ts +49 -0
  67. package/src/vite-client/module.ts +84 -0
  68. package/src/vite-client/runtime/injectManifest.ts +188 -0
  69. package/src/vite-client/runtime/plugin.ts +33 -0
  70. package/dist/app/entry.async.d.ts +0 -3
  71. package/dist/app/entry.async.mjs +0 -1
  72. package/dist/chunks/dev-bundler.mjs +0 -277
  73. package/dist/core/runtime/client.manifest.d.mts +0 -2
  74. package/dist/core/runtime/client.manifest.mjs +0 -6
  75. package/dist/core/runtime/vite-node-shared.d.mts +0 -1
  76. package/dist/core/runtime/vite-node-shared.d.ts +0 -8
  77. package/dist/core/runtime/vite-node-shared.mjs +0 -3
  78. package/dist/core/runtime/vite-node.d.mts +0 -2
  79. package/dist/core/runtime/vite-node.mjs +0 -41
  80. package/dist/pages/runtime/pages.mjs +0 -123
  81. /package/dist/pages/runtime/{pages.d.ts → plugin.d.ts} +0 -0
@@ -1,5 +1,5 @@
1
1
  import type { GetPages, PageDefinition } from '@monkeyplus/flow-schema';
2
2
  export declare function definePage(page: PageDefinition): {
3
- pages: never[];
3
+ pages: any[];
4
4
  getPages: GetPages;
5
5
  };
@@ -1,9 +1,6 @@
1
1
  import { joinURL, withTrailingSlash } from "ufo";
2
- const buildPages = (page) => (location, language, _locales) => {
3
- const locales = _locales.map((key) => {
4
- const _page = page.locales[key];
5
- return [key, _page];
6
- }).filter((el) => !!el[1]);
2
+ const buildPages = (page) => (location, language) => {
3
+ const locales = Object.entries(page.locales);
7
4
  return locales.map(([locale, localePage]) => {
8
5
  const [_language, _location] = locale.split("-");
9
6
  const name = joinURL("/", _location, _language, page.name);
@@ -19,15 +16,7 @@ const buildPages = (page) => (location, language, _locales) => {
19
16
  return {
20
17
  name,
21
18
  url: path,
22
- context: {
23
- page: localePage,
24
- locale: _locale,
25
- path,
26
- view: page.view,
27
- name: page.name,
28
- noPublish: page.noPublish
29
- }
30
- // locale: _locale,
19
+ context: { page: localePage, locale: _locale, path, view: page.view, name: page.name }
31
20
  };
32
21
  });
33
22
  };
@@ -1,10 +1,3 @@
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 defineDynamicPage<T>(...pages: DynamicPage<T>[]): DynamicPage<T>[];
4
- export interface SharedContext {
5
- assign?: 'global' | 'local';
6
- merge?: boolean;
7
- setup: (cxt: PageCtx) => any;
8
- locales?: Record<string, (ctx: PageCtx) => any>;
9
- }
10
- export declare function defineSharedContext(shared: SharedContext): SharedContext;
1
+ import type { DynamicPage, SimplePage } from '@monkeyplus/flow-schema';
2
+ export declare function definePage(page: SimplePage): SimplePage;
3
+ export declare function defineDinamycPage(page: DynamicPage): DynamicPage;
@@ -1,14 +1,6 @@
1
- export function definePage(...pages) {
2
- return pages;
1
+ export function definePage(page) {
2
+ return page;
3
3
  }
4
- export function defineDynamicPage(...pages) {
5
- return pages.map((page) => {
6
- page.originalName = page.name;
7
- if (!page.name.endsWith("/**"))
8
- page.name = `${page.name}/**`;
9
- return page;
10
- });
11
- }
12
- export function defineSharedContext(shared) {
13
- return shared;
4
+ export function defineDinamycPage(page) {
5
+ return page;
14
6
  }
@@ -0,0 +1,53 @@
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
+ });
@@ -0,0 +1,26 @@
1
+ export declare type Manifest = Record<string, ManifestChunk>;
2
+ export interface ManifestChunk {
3
+ src?: string;
4
+ file: string;
5
+ css?: string[];
6
+ assets?: string[];
7
+ isEntry?: boolean;
8
+ isDynamicEntry?: boolean;
9
+ imports?: string[];
10
+ dynamicImports?: string[];
11
+ }
12
+ export interface HtmlTagDescriptor {
13
+ tag: string;
14
+ attrs?: Record<string, string | boolean | undefined>;
15
+ children?: string | HtmlTagDescriptor[];
16
+ /**
17
+ * default: 'head-prepend'
18
+ */
19
+ injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend';
20
+ }
21
+ export declare const generateBundle: (config: any, bundle: Manifest, _id: string) => {
22
+ head: string;
23
+ body: string;
24
+ };
25
+ export declare const externalRE: RegExp;
26
+ export declare const isExternalUrl: (url: string) => boolean;
@@ -0,0 +1,104 @@
1
+ import { joinURL } from "ufo";
2
+ export const generateBundle = (config, bundle, _id) => {
3
+ const analyzedChunk = new Map();
4
+ const getImportedChunks = (chunk2, seen = new Set()) => {
5
+ const chunks = [];
6
+ chunk2.imports?.forEach((file) => {
7
+ const importee = bundle[file];
8
+ if (file.startsWith("_") && !seen.has(file)) {
9
+ seen.add(file);
10
+ chunks.push(...getImportedChunks(importee, seen));
11
+ chunks.push(importee);
12
+ }
13
+ });
14
+ return chunks;
15
+ };
16
+ const toScriptTag = (chunk2, publicBase2, isAsync2) => ({
17
+ tag: "script",
18
+ attrs: {
19
+ ...isAsync2 ? { async: true } : {},
20
+ type: "module",
21
+ crossorigin: true,
22
+ src: toPublicPath(chunk2.file, publicBase2)
23
+ }
24
+ });
25
+ const toPreloadTag = (chunk2, publicBase2) => ({
26
+ tag: "link",
27
+ attrs: {
28
+ rel: "modulepreload",
29
+ crossorigin: true,
30
+ href: toPublicPath(chunk2.file, publicBase2)
31
+ }
32
+ });
33
+ const getCssTagsForChunk = (chunk2, publicBase2, seen = new Set()) => {
34
+ const tags = [];
35
+ if (!analyzedChunk.has(chunk2)) {
36
+ analyzedChunk.set(chunk2, 1);
37
+ chunk2.imports?.forEach((file) => {
38
+ const importee = bundle[file];
39
+ if (file.startsWith("_"))
40
+ tags.push(...getCssTagsForChunk(importee, publicBase2, seen));
41
+ });
42
+ }
43
+ chunk2.css?.forEach((file) => {
44
+ if (!seen.has(file)) {
45
+ seen.add(file);
46
+ tags.push({
47
+ tag: "link",
48
+ attrs: {
49
+ rel: "stylesheet",
50
+ href: toPublicPath(file, publicBase2)
51
+ }
52
+ });
53
+ }
54
+ });
55
+ return tags;
56
+ };
57
+ const publicBase = config.baseURL || "/";
58
+ const isAsync = false;
59
+ const chunk = Object.values(bundle).find((chunk2) => chunk2.isEntry && chunk2.src === _id);
60
+ if (chunk) {
61
+ const imports = getImportedChunks(chunk);
62
+ const assetTags = [
63
+ toScriptTag(chunk, publicBase, isAsync),
64
+ ...imports.map((i) => toPreloadTag(i, publicBase))
65
+ ];
66
+ assetTags.push(...getCssTagsForChunk(chunk, publicBase));
67
+ const tags = serializeTags(assetTags);
68
+ return { head: tags, body: "" };
69
+ }
70
+ };
71
+ export const externalRE = /^(https?:)?\/\//;
72
+ export const isExternalUrl = (url) => externalRE.test(url);
73
+ function toPublicPath(filename, publicBase) {
74
+ return isExternalUrl(filename) ? filename : joinURL(publicBase, filename.replace("scripts", "assets"));
75
+ }
76
+ const unaryTags = new Set(["link", "meta", "base"]);
77
+ function serializeTag({ tag, attrs, children }, indent = "") {
78
+ if (unaryTags.has(tag)) {
79
+ return `<${tag}${serializeAttrs(attrs)}>`;
80
+ } else {
81
+ return `<${tag}${serializeAttrs(attrs)}>${serializeTags(children, incrementIndent(indent))}</${tag}>`;
82
+ }
83
+ }
84
+ function serializeTags(tags, indent = "") {
85
+ if (typeof tags === "string")
86
+ return tags;
87
+ else if (tags && tags.length)
88
+ return tags.map((tag) => `${indent}${serializeTag(tag, indent)}
89
+ `).join("");
90
+ return "";
91
+ }
92
+ function serializeAttrs(attrs) {
93
+ let res = "";
94
+ for (const key in attrs) {
95
+ if (typeof attrs[key] === "boolean")
96
+ res += attrs[key] ? ` ${key}` : "";
97
+ else
98
+ res += ` ${key}=${JSON.stringify(attrs[key])}`;
99
+ }
100
+ return res;
101
+ }
102
+ function incrementIndent(indent = "") {
103
+ return `${indent}${indent[0] === " " ? " " : " "}`;
104
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,27 @@
1
+ import logger from "consola";
2
+ import { generateBundle } from "./injectManifest.mjs";
3
+ import { defineFlowPlugin } from "#app";
4
+ import manifest from "#viteManifest";
5
+ export default defineFlowPlugin((flow) => {
6
+ if (typeof manifest === "function") {
7
+ const _manifest = manifest();
8
+ flow.hook("page:chunks", (bundle, chunks) => {
9
+ try {
10
+ const chunk = generateBundle(flow.$config.app || {}, _manifest, `client/pages/${bundle}.ts`);
11
+ if (chunk) {
12
+ chunks.head.push(chunk.head);
13
+ chunks.body.push(chunk.body);
14
+ } else {
15
+ logger.warn('Entry "%s" not found ', bundle);
16
+ }
17
+ } catch (error) {
18
+ logger.error("Error in inject %s ", bundle);
19
+ }
20
+ });
21
+ } else {
22
+ flow.hook("page:chunks", (bundle, chunks) => {
23
+ chunks.head.push(manifest.head());
24
+ chunks.body.push(manifest.body(bundle));
25
+ });
26
+ }
27
+ });
package/package.json CHANGED
@@ -1,80 +1,65 @@
1
1
  {
2
2
  "name": "@monkeyplus/flow",
3
- "version": "5.0.0-rc.199",
3
+ "version": "5.0.0-rc.2",
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
- },
11
6
  "exports": {
12
7
  ".": "./dist/index.mjs",
13
8
  "./app": "./dist/app/index.mjs",
14
9
  "./package.json": "./package.json"
15
10
  },
16
- "imports": {
17
- "#app": "./dist/app/index.mjs",
18
- "#head": "./dist/head/runtime/index.mjs",
19
- "#pages": "./dist/pages/runtime/index.mjs"
11
+ "main": "./dist/index.mjs",
12
+ "types": "./types.d.ts",
13
+ "bin": {
14
+ "flow": "./bin/flow.mjs"
20
15
  },
21
- "files": [
22
- "app.d.ts",
23
- "bin",
24
- "types.d.ts",
25
- "dist"
26
- ],
27
16
  "dependencies": {
28
- "@monkeyplus/flow-cli": "5.0.0-rc.199",
29
- "@monkeyplus/flow-kit": "5.0.0-rc.199",
30
- "@monkeyplus/flow-schema": "5.0.0-rc.199",
17
+ "@monkeyplus/flow-cli": "5.0.0-rc.2",
18
+ "@monkeyplus/flow-kit": "5.0.0-rc.2",
19
+ "@monkeyplus/flow-schema": "5.0.0-rc.2",
31
20
  "@rollup/plugin-replace": "^4.0.0",
32
- "@vueuse/head": "^1.0.16",
33
- "c12": "^1.0.1",
21
+ "@vueuse/head": "^0.7.6",
22
+ "c12": "^0.2.7",
34
23
  "chokidar": "^3.5.3",
35
24
  "consola": "^2.15.3",
36
- "defu": "^6.1.0",
37
- "esbuild": "^0.15.13",
25
+ "defu": "^6.0.0",
26
+ "esbuild": "^0.14.38",
38
27
  "escape-string-regexp": "^5.0.0",
39
- "esno": "^0.16.3",
28
+ "esno": "^0.14.1",
40
29
  "eta": "^1.12.3",
41
- "externality": "^1.0.0",
30
+ "externality": "^0.2.1",
42
31
  "fs-extra": "^10.1.0",
43
- "globby": "^13.1.2",
44
- "h3": "^1.5.0",
45
- "hookable": "^5.4.2",
46
- "jiti": "^1.16.0",
47
- "ohash": "^1.0.0",
48
- "knitwork": "^1.0.0",
49
- "listhen": "^1.0.0",
50
- "magic-string": "^0.26.7",
51
- "mlly": "^1.0.0",
32
+ "get-port-please": "^2.5.0",
33
+ "globby": "^13.1.1",
34
+ "h3": "^0.7.4",
35
+ "hookable": "^5.1.1",
36
+ "jiti": "^1.13.0",
37
+ "knitwork": "^0.1.1",
38
+ "listhen": "^0.2.10",
39
+ "magic-string": "^0.26.2",
40
+ "mlly": "^0.5.2",
52
41
  "mri": "^1.2.0",
53
- "nitropack": "^1.0.0",
54
- "pathe": "^1.0.0",
42
+ "nitropack": "^0.4.4",
43
+ "pathe": "^0.2.0",
55
44
  "perfect-debounce": "^0.1.3",
56
- "radix3": "^1.0.0",
57
- "unenv": "^1.0.0",
58
- "ohmyfetch": "^0.4.21",
59
- "node-fetch-native": "^1.0.1",
60
- "rollup": "^3.10.1",
61
- "rollup-plugin-visualizer": "^5.8.3",
62
- "scule": "^1.0.0",
63
- "ufo": "^1.0.0",
64
- "unctx": "^2.1.0",
65
- "unimport": "^1.0.1",
66
- "unplugin": "^1.0.0",
67
- "untyped": "^1.0.0",
68
- "pkg-types": "^1.0.1",
69
- "vite": "~4.0.4",
70
- "vite-node": "^0.24.5",
71
- "vite-plugin-checker": "^0.5.1",
72
- "vue-bundle-renderer": "^1.0.0",
73
- "vue": "^3.2.41"
45
+ "radix3": "^0.1.2",
46
+ "unenv": "^0.5.2",
47
+ "ohmyfetch": "^0.4.18",
48
+ "node-fetch-native": "^0.1.3",
49
+ "rollup": "^2.72.1",
50
+ "rollup-plugin-visualizer": "^5.6.0",
51
+ "scule": "^0.2.1",
52
+ "ufo": "^0.8.3",
53
+ "unctx": "^1.1.4",
54
+ "unimport": "^0.2.1",
55
+ "unplugin": "^0.6.3",
56
+ "untyped": "^0.4.4",
57
+ "vite": "^2.9.9",
58
+ "vue": "^3.2.33"
74
59
  },
75
60
  "devDependencies": {
76
61
  "@types/fs-extra": "^9.0.13",
77
- "vue-router": "^4.1.6"
62
+ "vue-router": "^4.0.15"
78
63
  },
79
64
  "engines": {
80
65
  "node": "^16.11.0 || ^17.0.0 || ^18.0.0"
@@ -0,0 +1,20 @@
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
+ }
@@ -0,0 +1,36 @@
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
+ };
@@ -0,0 +1,157 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ export * from './flow';
2
+ export * from './composables';
3
+
4
+ // @ts-ignore
5
+ export { useHead } from '#head';