@monkeyplus/flow 5.0.0-rc.233 → 5.0.0-rc.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,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,9 @@
1
1
  import type { DynamicPage, PageCtx, SimplePage } from '@monkeyplus/flow-schema';
2
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 {
3
+ export declare function defineDinamycPage<T>(...pages: DynamicPage<T>[]): DynamicPage<T>[];
4
+ interface SharedContext {
5
5
  assign?: 'global' | 'local';
6
- merge?: boolean;
7
6
  setup: (cxt: PageCtx) => any;
8
- locales?: Record<string, (ctx: PageCtx) => any>;
9
7
  }
10
8
  export declare function defineSharedContext(shared: SharedContext): SharedContext;
9
+ export {};
@@ -1,9 +1,8 @@
1
1
  export function definePage(...pages) {
2
2
  return pages;
3
3
  }
4
- export function defineDynamicPage(...pages) {
4
+ export function defineDinamycPage(...pages) {
5
5
  return pages.map((page) => {
6
- page.originalName = page.name;
7
6
  if (!page.name.endsWith("/**"))
8
7
  page.name = `${page.name}/**`;
9
8
  return page;
@@ -16,104 +16,58 @@ export default defineFlowPlugin(async (flow) => {
16
16
  name,
17
17
  ...page
18
18
  });
19
- const _pages2 = getPages(
20
- app.locale.location,
21
- app.locale.language,
22
- app.locale.locales
23
- );
19
+ const _pages2 = getPages(app.locale.location, app.locale.language);
24
20
  _pages2.forEach((page2) => {
25
21
  flow.router.byUrl.insert(page2.url, page2.context);
26
- if (page2.url.includes("**") && !page2.context.page.dynamic) {
27
- const _url = page2.url.replaceAll("*", "");
28
- flow.router.byUrl.insert(_url, {
29
- ...page2.context,
30
- path: _url
31
- });
32
- }
33
22
  flow.router.byName.insert(page2.name, page2.context);
34
23
  allPages.push(page2.context);
35
24
  });
36
25
  });
37
26
  });
38
27
  const cache = {};
39
- const cacheData = {};
40
- function replacePath(_path, url) {
41
- if (typeof url === "string") {
42
- _path = joinURL(_path.replace("/**", ""), url);
43
- } else {
44
- Object.entries(url).forEach(([key, value]) => {
45
- _path = _path.replace(`:${key}`, value);
46
- });
47
- if (url._ && _path.endsWith("**"))
48
- _path = joinURL(_path.replace("/**", ""), url._);
49
- }
50
- return _path;
51
- }
52
28
  async function getUrl(namePage, localeCode) {
53
29
  const code = localeCode || this?.getLocale()?.code;
54
30
  const [lang, loc] = code.split("-");
55
31
  const name = joinURL("/", loc, lang, namePage);
56
- const baseHome = joinURL("/", loc, lang, "home");
57
32
  const { path, params, page } = flow.router.byName.lookup(name) || {};
58
- const h = flow.router.byName.lookup(baseHome);
59
- const base404 = h?.path || "/";
60
33
  if (params?._ && page.dynamic) {
61
- if (!cache[path + code]) {
62
- if (flow.generate) {
63
- cache[path + code] = async (_ctx, _path) => {
64
- if (cacheData[_path])
65
- return cacheData[_path];
66
- const r = await page.dynamic.method(_ctx);
67
- cacheData[_path] = r;
68
- return r;
69
- };
70
- } else {
71
- cache[path + code] = this.defineCachedFunction(async (_ctx) => {
72
- return await page.dynamic.method(_ctx);
73
- }, { maxAge: 20, getKey: (_ctx, _path) => _path, swr: false });
74
- }
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 });
75
38
  }
76
- const fn = cache[path + code];
77
- const list = await fn({ utils: this, locale: { ...this.getLocale(), code } }, path);
39
+ const fn = cache[path];
40
+ const list = await fn({ utils: this, locale: this.getLocale() });
78
41
  const dPage = list.find((el) => el.name === params._);
79
- return dPage ? replacePath(path, dPage.url) : `${base404}404`;
42
+ return dPage ? path.replace("**", dPage.url) : "/404";
80
43
  }
81
- return path?.replaceAll("*", "") || `${base404}404`;
44
+ return path || "/404";
82
45
  }
83
- async function getUrls(withLocale = false, omitNoPublish = false) {
46
+ async function getUrls(withLocale = false) {
84
47
  const urls = [];
85
- const _allPages = allPages.filter((p) => omitNoPublish ? !p.noPublish : true);
86
- for (const page of _allPages) {
87
- if ((page.path.includes("/:") || page.path.includes("/**")) && page.page.dynamic) {
48
+ for (const page of allPages) {
49
+ if (!page.path.includes("/**")) {
50
+ urls.push(withLocale ? { url: page.path, locale: page.locale.code, name: page.name } : page.path);
51
+ } else {
88
52
  const dPages = await page.page.dynamic.method({
89
53
  locale: page.locale,
90
- utils: Object.assign({ getLocale: () => page.locale }, flow.app.utils),
91
- chunks: {},
92
- page: {}
54
+ utils: Object.assign({ getLocale: () => page.locale }, flow.app.utils)
93
55
  });
94
56
  dPages.forEach((dPage) => {
95
- const _path = replacePath(page.path, dPage.url);
96
- urls.push(withLocale ? { url: _path, locale: page.locale.code, name: joinURL(page.name?.replaceAll("*", ""), dPage.name) } : _path);
57
+ const _path = joinURL(page.path.replace("/**", ""), dPage.url);
58
+ urls.push(withLocale ? { url: _path, locale: page.locale.code, name: joinURL(page.name, dPage.name) } : _path);
97
59
  });
98
- } else {
99
- const url = page.path.replaceAll("*", "");
100
- urls.push(withLocale ? { url, locale: page.locale.code, name: page.name } : url);
101
60
  }
102
61
  }
103
62
  return urls.sort();
104
63
  }
105
- async function getSharedContext(ctx) {
64
+ async function getSharedContext(utils) {
106
65
  const entries = Object.entries(contexts);
107
66
  if (!entries.length)
108
67
  return {};
109
68
  const _contexts = await Promise.all(entries.map(async ([key, method]) => {
110
- const data = await method.setup(ctx);
111
- const setupLocal = method?.locales?.[ctx.locale.code];
112
- let dataLocal = {};
113
- if (setupLocal)
114
- dataLocal = await setupLocal(ctx);
115
- const obj = method.merge ? { ...data, ...dataLocal } : { [key]: { ...data, ...dataLocal } };
116
- return obj;
69
+ const data = await method.setup(utils);
70
+ return { [key]: data };
117
71
  }));
118
72
  return _contexts.reduce((acc, curr) => {
119
73
  return {
@@ -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,26 @@
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
+ flow.hook("page:chunks", (bundle, chunks) => {
8
+ try {
9
+ const chunk = generateBundle(flow.$config.app || {}, manifest(), `client/pages/${bundle}.ts`);
10
+ if (chunk) {
11
+ chunks.head.push(chunk.head);
12
+ chunks.body.push(chunk.body);
13
+ } else {
14
+ logger.warn('Entry "%s" not found ', bundle);
15
+ }
16
+ } catch (error) {
17
+ logger.error("Error in inject %s ", bundle);
18
+ }
19
+ });
20
+ } else {
21
+ flow.hook("page:chunks", (bundle, chunks) => {
22
+ chunks.head.push(manifest.head());
23
+ chunks.body.push(manifest.body(bundle));
24
+ });
25
+ }
26
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monkeyplus/flow",
3
- "version": "5.0.0-rc.233",
3
+ "version": "5.0.0-rc.24",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -25,56 +25,52 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
+ "@monkeyplus/flow-cli": "5.0.0-rc.24",
29
+ "@monkeyplus/flow-kit": "5.0.0-rc.24",
30
+ "@monkeyplus/flow-schema": "5.0.0-rc.24",
28
31
  "@rollup/plugin-replace": "^4.0.0",
29
- "@vueuse/head": "^1.0.16",
30
- "c12": "^1.0.1",
32
+ "@vueuse/head": "^0.7.6",
33
+ "c12": "^0.2.7",
31
34
  "chokidar": "^3.5.3",
32
35
  "consola": "^2.15.3",
33
- "defu": "^6.1.0",
34
- "esbuild": "^0.15.13",
36
+ "defu": "^6.0.0",
37
+ "esbuild": "^0.14.38",
35
38
  "escape-string-regexp": "^5.0.0",
36
- "esno": "^0.16.3",
39
+ "esno": "^0.14.1",
37
40
  "eta": "^1.12.3",
38
- "externality": "^1.0.0",
41
+ "externality": "^0.2.1",
39
42
  "fs-extra": "^10.1.0",
40
- "globby": "^13.1.2",
41
- "h3": "^1.8.0",
42
- "hookable": "^5.4.2",
43
- "jiti": "^1.19.0",
44
- "ohash": "^1.0.0",
45
- "knitwork": "^1.0.0",
46
- "listhen": "^1.0.0",
47
- "magic-string": "^0.26.7",
48
- "mlly": "^1.0.0",
43
+ "get-port-please": "^2.5.0",
44
+ "globby": "^13.1.1",
45
+ "h3": "^0.7.4",
46
+ "hookable": "^5.1.1",
47
+ "jiti": "^1.13.0",
48
+ "knitwork": "^0.1.1",
49
+ "listhen": "^0.2.10",
50
+ "magic-string": "^0.26.2",
51
+ "mlly": "^0.5.2",
49
52
  "mri": "^1.2.0",
50
- "nitropack": "1.0.0",
51
- "pathe": "^1.0.0",
53
+ "nitropack": "^0.4.4",
54
+ "pathe": "^0.2.0",
52
55
  "perfect-debounce": "^0.1.3",
53
- "radix3": "^1.0.0",
54
- "unenv": "^1.0.0",
55
- "ohmyfetch": "^0.4.21",
56
- "node-fetch-native": "^1.0.1",
57
- "rollup": "^3.10.1",
58
- "rollup-plugin-visualizer": "^5.8.3",
59
- "scule": "^1.0.0",
60
- "ufo": "^1.0.0",
61
- "unctx": "^2.1.0",
62
- "unimport": "^1.0.1",
63
- "unplugin": "^1.0.0",
64
- "untyped": "^1.0.0",
65
- "pkg-types": "^1.0.1",
66
- "vite": "~4.0.4",
67
- "vite-node": "^0.24.5",
68
- "vite-plugin-checker": "^0.5.1",
69
- "vue-bundle-renderer": "^1.0.0",
70
- "vue": "^3.2.41",
71
- "@monkeyplus/flow-cli": "5.0.0-rc.233",
72
- "@monkeyplus/flow-kit": "5.0.0-rc.233",
73
- "@monkeyplus/flow-schema": "5.0.0-rc.233"
56
+ "radix3": "^0.1.2",
57
+ "unenv": "^0.5.2",
58
+ "ohmyfetch": "^0.4.18",
59
+ "node-fetch-native": "^0.1.3",
60
+ "rollup": "^2.72.1",
61
+ "rollup-plugin-visualizer": "^5.6.0",
62
+ "scule": "^0.2.1",
63
+ "ufo": "^0.8.3",
64
+ "unctx": "^1.1.4",
65
+ "unimport": "^0.2.1",
66
+ "unplugin": "^0.6.3",
67
+ "untyped": "^0.4.4",
68
+ "vite": "^2.9.9",
69
+ "vue": "^3.2.33"
74
70
  },
75
71
  "devDependencies": {
76
72
  "@types/fs-extra": "^9.0.13",
77
- "vue-router": "^4.1.6"
73
+ "vue-router": "^4.0.15"
78
74
  },
79
75
  "engines": {
80
76
  "node": "^16.11.0 || ^17.0.0 || ^18.0.0"
@@ -1,3 +0,0 @@
1
- import type { RuntimeConfig } from '@monkeyplus/flow-schema';
2
- declare const _default: (ctx?: RuntimeConfig) => Promise<any>;
3
- export default _default;
@@ -1 +0,0 @@
1
- export default (ctx) => import("#app/entry").then((m) => m.default(ctx));