@monkeyplus/flow 4.0.0-beta.9 → 5.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/app.d.ts +1 -0
  2. package/bin/flow.mjs +2 -0
  3. package/build.config.ts +25 -0
  4. package/dist/app/composables/index.d.ts +4 -0
  5. package/dist/app/composables/index.mjs +12 -0
  6. package/dist/app/entry.d.ts +3 -0
  7. package/dist/app/entry.mjs +23 -0
  8. package/dist/app/flow.d.ts +73 -0
  9. package/dist/app/flow.mjs +85 -0
  10. package/dist/app/index.d.ts +3 -0
  11. package/dist/app/index.mjs +3 -0
  12. package/dist/core/runtime/nitro/flow.d.ts +3 -0
  13. package/dist/core/runtime/nitro/flow.mjs +32 -0
  14. package/dist/core/runtime/nitro/paths.d.ts +4 -0
  15. package/dist/core/runtime/nitro/paths.mjs +15 -0
  16. package/dist/core/runtime/nitro/renderer.d.ts +2 -0
  17. package/dist/core/runtime/nitro/renderer.mjs +59 -0
  18. package/dist/head/runtime/composables.d.ts +9 -0
  19. package/dist/head/runtime/composables.mjs +2 -0
  20. package/dist/head/runtime/index.d.ts +1 -0
  21. package/dist/head/runtime/index.mjs +1 -0
  22. package/dist/head/runtime/plugin.d.ts +2 -0
  23. package/dist/head/runtime/plugin.mjs +6 -0
  24. package/dist/index.d.ts +8 -61
  25. package/dist/index.mjs +1286 -954
  26. package/dist/pages/runtime/helpers/chunks.d.ts +0 -0
  27. package/dist/pages/runtime/helpers/chunks.mjs +0 -0
  28. package/dist/pages/runtime/helpers/index.d.ts +5 -0
  29. package/dist/pages/runtime/helpers/index.mjs +28 -0
  30. package/dist/pages/runtime/plugin.d.ts +2 -0
  31. package/dist/pages/runtime/plugin.mjs +51 -0
  32. package/dist/vite-client/runtime/injectManifest.d.ts +26 -0
  33. package/dist/vite-client/runtime/injectManifest.mjs +104 -0
  34. package/dist/vite-client/runtime/plugin.d.ts +2 -0
  35. package/dist/vite-client/runtime/plugin.mjs +27 -0
  36. package/package.json +54 -36
  37. package/src/app/composables/index.ts +20 -0
  38. package/src/app/entry.ts +36 -0
  39. package/src/app/flow.ts +157 -0
  40. package/src/app/index.ts +5 -0
  41. package/src/auto-imports/module.ts +143 -0
  42. package/src/auto-imports/presets.ts +49 -0
  43. package/src/auto-imports/transform.ts +48 -0
  44. package/src/core/app.ts +90 -0
  45. package/src/core/builder.ts +60 -0
  46. package/src/core/flow.ts +93 -0
  47. package/src/core/modules.ts +32 -0
  48. package/src/core/nitro.ts +206 -0
  49. package/src/core/plugins/import-protection.ts +49 -0
  50. package/src/core/plugins/unctx.ts +31 -0
  51. package/src/core/runtime/nitro/flow.ts +43 -0
  52. package/src/core/runtime/nitro/paths.ts +20 -0
  53. package/src/core/runtime/nitro/renderer.ts +74 -0
  54. package/src/core/templates.ts +119 -0
  55. package/src/core/vite/builder/css.ts +28 -0
  56. package/src/core/vite/builder/dev-bundler.ts +248 -0
  57. package/src/core/vite/builder/index.ts +96 -0
  58. package/src/core/vite/builder/manifest.ts +33 -0
  59. package/src/core/vite/builder/plugins/analyze.ts +32 -0
  60. package/src/core/vite/builder/plugins/cache-dir.ts +13 -0
  61. package/src/core/vite/builder/plugins/dynamic-base.ts +64 -0
  62. package/src/core/vite/builder/plugins/virtual.ts +45 -0
  63. package/src/core/vite/builder/server.ts +164 -0
  64. package/src/core/vite/builder/types/index.ts +13 -0
  65. package/src/core/vite/builder/utils/index.ts +53 -0
  66. package/src/core/vite/builder/utils/warmup.ts +27 -0
  67. package/src/core/vite/builder/utils/wpfs.ts +7 -0
  68. package/src/core/vite/builder/vite-node.ts +110 -0
  69. package/src/core/vite/client/index.ts +55 -0
  70. package/src/dirs.ts +8 -0
  71. package/src/head/module.ts +37 -0
  72. package/src/head/runtime/composables.ts +16 -0
  73. package/src/head/runtime/index.ts +1 -0
  74. package/src/head/runtime/plugin.ts +12 -0
  75. package/src/index.ts +2 -0
  76. package/src/pages/module.ts +55 -0
  77. package/src/pages/runtime/helpers/chunks.ts +0 -0
  78. package/src/pages/runtime/helpers/index.ts +33 -0
  79. package/src/pages/runtime/plugin.ts +58 -0
  80. package/src/pages/templates.ts +20 -0
  81. package/src/pages/utils.ts +49 -0
  82. package/src/vite-client/module.ts +81 -0
  83. package/src/vite-client/runtime/injectManifest.ts +188 -0
  84. package/src/vite-client/runtime/plugin.ts +33 -0
  85. package/types.d.ts +2 -0
  86. package/dist/index.cjs +0 -1061
  87. package/types/core.d.ts +0 -143
  88. package/types/flow.d.ts +0 -239
  89. package/types/index.d.ts +0 -38
  90. package/types/interfaces.d.ts +0 -61
File without changes
File without changes
@@ -0,0 +1,5 @@
1
+ import type { GetPages, PageDefinition } from '@monkeyplus/flow-schema';
2
+ export declare function definePage(page: PageDefinition): {
3
+ pages: any[];
4
+ getPages: GetPages;
5
+ };
@@ -0,0 +1,28 @@
1
+ import { joinURL, withTrailingSlash } from "ufo";
2
+ const buildPages = (page) => (location, language) => {
3
+ const locales = Object.entries(page.locales);
4
+ return locales.map(([locale, localePage]) => {
5
+ const [_language, _location] = locale.split("-");
6
+ const name = joinURL("/", _location, _language, page.name);
7
+ const defLocation = _location === location ? "" : _location;
8
+ const defLanguage = _language === language ? "" : _language;
9
+ const url = joinURL("/", defLocation, defLanguage, localePage.url);
10
+ const _locale = {
11
+ lang: _language,
12
+ loc: _location,
13
+ code: locale
14
+ };
15
+ const path = localePage.url.endsWith("/") ? withTrailingSlash(url) : url;
16
+ return {
17
+ name,
18
+ url: path,
19
+ context: { page: localePage, locale: _locale, path, view: page.view, name }
20
+ };
21
+ });
22
+ };
23
+ export function definePage(page) {
24
+ return {
25
+ pages: [],
26
+ getPages: buildPages(page)
27
+ };
28
+ }
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,51 @@
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() {
28
+ const urls = [];
29
+ for (const page of allPages) {
30
+ if (!page.path.includes("/**")) {
31
+ urls.push(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
+ urls.push(joinURL(page.path.replace("/**", ""), dPage.url));
39
+ });
40
+ }
41
+ }
42
+ return urls.sort();
43
+ }
44
+ flow.setUtil("getUrl", getUrl);
45
+ flow.setUtil("getUrls", getUrls);
46
+ return {
47
+ provide: {
48
+ pages: { allPages }
49
+ }
50
+ };
51
+ });
@@ -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,51 +1,69 @@
1
1
  {
2
2
  "name": "@monkeyplus/flow",
3
- "version": "4.0.0-beta.9",
4
- "description": "Utils hapi",
5
- "author": "Andres Navarrete",
3
+ "version": "5.0.0-beta.10",
6
4
  "license": "MIT",
7
- "main": "./dist/index.cjs",
8
- "module": "./dist/index.mjs",
9
- "types": "./dist/index.d.ts",
10
- "files": [
11
- "dist/",
12
- "types/"
13
- ],
5
+ "type": "module",
14
6
  "exports": {
15
- ".": {
16
- "import": "./dist/index.mjs",
17
- "require": "./dist/index.cjs"
18
- }
7
+ ".": "./dist/index.mjs",
8
+ "./app": "./dist/app/index.mjs",
9
+ "./package.json": "./package.json"
10
+ },
11
+ "main": "./dist/index.mjs",
12
+ "types": "./types.d.ts",
13
+ "bin": {
14
+ "flow": "./bin/flow.mjs"
19
15
  },
20
16
  "dependencies": {
21
- "@hapi/boom": "9.x.x",
22
- "@hapi/hoek": "9.x.x",
23
- "consola": "^2.15.3",
24
- "chalk": "^4.1.2",
17
+ "@monkeyplus/flow-cli": "5.0.0-beta.10",
18
+ "@monkeyplus/flow-kit": "5.0.0-beta.10",
19
+ "@monkeyplus/flow-schema": "5.0.0-beta.10",
20
+ "@rollup/plugin-replace": "^4.0.0",
21
+ "@vueuse/head": "^0.7.6",
22
+ "c12": "^0.2.7",
25
23
  "chokidar": "^3.5.3",
24
+ "consola": "^2.15.3",
25
+ "defu": "^6.0.0",
26
+ "esbuild": "^0.14.38",
27
+ "escape-string-regexp": "^5.0.0",
28
+ "esno": "^0.14.1",
29
+ "eta": "^1.12.3",
30
+ "externality": "^0.2.1",
31
+ "fs-extra": "^10.1.0",
32
+ "get-port-please": "^2.5.0",
33
+ "globby": "^13.1.1",
34
+ "h3": "^0.7.4",
26
35
  "hookable": "^5.1.1",
27
- "fs-extra": "^10.0.0",
28
- "ramda": "^0.28.0"
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",
41
+ "mri": "^1.2.0",
42
+ "nitropack": "^0.4.4",
43
+ "pathe": "^0.2.0",
44
+ "perfect-debounce": "^0.1.3",
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"
29
59
  },
30
60
  "devDependencies": {
31
- "@types/fs-extra": "^9.0.13",
32
- "@types/hapi__hapi": "^20.0.10",
33
- "@types/hapi__nes": "^11.0.5",
34
- "@types/hapi__vision": "^5.5.3",
35
- "@types/ramda": "^0.27.64"
61
+ "@types/fs-extra": "^9.0.13"
36
62
  },
37
- "peerDependencies": {
38
- "@hapi/hapi": "^20.x"
63
+ "engines": {
64
+ "node": "^16.11.0 || ^17.0.0 || ^18.0.0"
39
65
  },
40
- "gitHead": "1d5387b9c77909019f36d360a0ac53ddaa40e4af",
41
66
  "scripts": {
42
- "build": "unbuild",
43
- "dev": "pnpm stub && pnpm types",
44
- "stub": "unbuild --stub",
45
- "types": "tsup ./src/index.ts --dts-only --external types && esno ../../scripts/replace",
46
- "lint": "eslint --ext .js,.ts .",
47
- "fix": "eslint --fix --ext .ts .",
48
- "start": "esno src/index.ts",
49
- "test": "vitest"
67
+ "stub": "unbuild --stub"
50
68
  }
51
69
  }
@@ -0,0 +1,20 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ //* fake to nuxt Compatibilities
3
+
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() {
19
+ return {};
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';