@monkeyplus/flow 4.0.0-beta.8 → 5.0.0-beta.1

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 +1275 -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 +55 -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 +59 -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 +72 -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 +247 -0
  57. package/src/core/vite/builder/index.ts +92 -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 +163 -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 +48 -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 +70 -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
@@ -0,0 +1,188 @@
1
+ import { joinURL } from 'ufo';
2
+ export type Manifest = Record<string, ManifestChunk>;
3
+
4
+ export interface ManifestChunk {
5
+ src?: string
6
+ file: string
7
+ css?: string[]
8
+ assets?: string[]
9
+ isEntry?: boolean
10
+ isDynamicEntry?: boolean
11
+ imports?: string[]
12
+ dynamicImports?: string[]
13
+ }
14
+
15
+ export interface HtmlTagDescriptor {
16
+ tag: string
17
+ attrs?: Record<string, string | boolean | undefined>
18
+ children?: string | HtmlTagDescriptor[]
19
+ /**
20
+ * default: 'head-prepend'
21
+ */
22
+ injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend'
23
+ }
24
+ export const generateBundle = (config: any, bundle: Manifest, _id: string) => {
25
+ const analyzedChunk: Map<ManifestChunk, number> = new Map();
26
+
27
+ const getImportedChunks = (
28
+ chunk: ManifestChunk,
29
+ seen: Set<string> = new Set(),
30
+ ): ManifestChunk[] => {
31
+ const chunks: ManifestChunk[] = [];
32
+ chunk.imports?.forEach((file) => {
33
+ const importee = bundle[file];
34
+ if (file.startsWith('_') && !seen.has(file)) {
35
+ seen.add(file);
36
+
37
+ // post-order traversal
38
+ chunks.push(...getImportedChunks(importee, seen));
39
+ chunks.push(importee);
40
+ }
41
+ });
42
+ // TODO: evaluate if is nesesary
43
+ // chunk.dynamicImports?.forEach((file) => {
44
+ // const importee = bundle[file];
45
+ // if (!seen.has(file)) {
46
+ // seen.add(file);
47
+ // // post-order traversal
48
+ // chunks.push(...getImportedChunks(importee, seen));
49
+ // chunks.push(importee);
50
+ // }
51
+ // });
52
+ return chunks;
53
+ };
54
+ const toScriptTag = (
55
+ chunk: ManifestChunk,
56
+ publicBase: string,
57
+ isAsync: boolean,
58
+ ): HtmlTagDescriptor => ({
59
+ tag: 'script',
60
+ attrs: {
61
+ ...(isAsync ? { async: true } : {}),
62
+ type: 'module',
63
+ crossorigin: true,
64
+ src: toPublicPath(chunk.file, publicBase),
65
+ },
66
+ });
67
+
68
+ const toPreloadTag = (
69
+ chunk: ManifestChunk,
70
+ publicBase: string,
71
+ ): HtmlTagDescriptor => ({
72
+ tag: 'link',
73
+ attrs: {
74
+ rel: 'modulepreload',
75
+ crossorigin: true,
76
+ href: toPublicPath(chunk.file, publicBase),
77
+ },
78
+ });
79
+
80
+ const getCssTagsForChunk = (
81
+ chunk: ManifestChunk,
82
+ publicBase: string,
83
+ seen: Set<string> = new Set(),
84
+ ): HtmlTagDescriptor[] => {
85
+ const tags: HtmlTagDescriptor[] = [];
86
+ if (!analyzedChunk.has(chunk)) {
87
+ analyzedChunk.set(chunk, 1);
88
+ chunk.imports?.forEach((file) => {
89
+ const importee = bundle[file];
90
+ if (file.startsWith('_'))
91
+ tags.push(...getCssTagsForChunk(importee, publicBase, seen));
92
+ });
93
+ }
94
+
95
+ chunk.css?.forEach((file) => {
96
+ if (!seen.has(file)) {
97
+ seen.add(file);
98
+ tags.push({
99
+ tag: 'link',
100
+ attrs: {
101
+ rel: 'stylesheet',
102
+ href: toPublicPath(file, publicBase),
103
+ },
104
+ });
105
+ }
106
+ });
107
+
108
+ return tags;
109
+ };
110
+
111
+ // const relativeUrlPath = path.posix.relative(config.root, id);
112
+ const publicBase = config.baseURL || '/'; // getPublicBase(relativeUrlPath, config);
113
+
114
+ const isAsync = false;// isAsyncScriptMap.get(config)!.get(id)!;
115
+
116
+ // const result = html;
117
+ const chunk = Object.values(bundle).find(
118
+ (chunk) =>
119
+ chunk.isEntry && chunk.src === _id,
120
+ );
121
+
122
+ if (chunk) {
123
+ // when not inlined, inject <script> for entry and modulepreload its dependencies
124
+ // when inlined, discard entry chunk and inject <script> for everything in post-order
125
+ const imports = getImportedChunks(chunk);
126
+ const assetTags = [
127
+ toScriptTag(chunk, publicBase, isAsync),
128
+ ...imports.map((i) => toPreloadTag(i, publicBase)),
129
+ ];
130
+
131
+ assetTags.push(...getCssTagsForChunk(chunk, publicBase));
132
+ const tags = serializeTags(assetTags);
133
+ return { head: tags, body: '' };
134
+ // result = injectToHead(result, assetTags);
135
+ }
136
+ };
137
+
138
+ export const externalRE = /^(https?:)?\/\//;
139
+ export const isExternalUrl = (url: string): boolean => externalRE.test(url);
140
+
141
+ function toPublicPath(filename: string, publicBase: string) {
142
+ return isExternalUrl(filename) ? filename : joinURL(publicBase, filename.replace('scripts', 'assets'));
143
+ }
144
+
145
+ const unaryTags = new Set(['link', 'meta', 'base']);
146
+
147
+ function serializeTag(
148
+ { tag, attrs, children }: HtmlTagDescriptor,
149
+ indent = '',
150
+ ): string {
151
+ if (unaryTags.has(tag)) {
152
+ return `<${tag}${serializeAttrs(attrs)}>`;
153
+ }
154
+ else {
155
+ return `<${tag}${serializeAttrs(attrs)}>${serializeTags(
156
+ children,
157
+ incrementIndent(indent),
158
+ )}</${tag}>`;
159
+ }
160
+ }
161
+
162
+ function serializeTags(
163
+ tags: HtmlTagDescriptor['children'],
164
+ indent = '',
165
+ ): string {
166
+ if (typeof tags === 'string')
167
+ return tags;
168
+
169
+ else if (tags && tags.length)
170
+ return tags.map((tag) => `${indent}${serializeTag(tag, indent)}\n`).join('');
171
+
172
+ return '';
173
+ }
174
+
175
+ function serializeAttrs(attrs: HtmlTagDescriptor['attrs']): string {
176
+ let res = '';
177
+ for (const key in attrs) {
178
+ if (typeof attrs[key] === 'boolean')
179
+ res += attrs[key] ? ` ${key}` : '';
180
+
181
+ else
182
+ res += ` ${key}=${JSON.stringify(attrs[key])}`;
183
+ }
184
+ return res;
185
+ }
186
+ function incrementIndent(indent = '') {
187
+ return `${indent}${indent[0] === '\t' ? '\t' : ' '}`;
188
+ }
@@ -0,0 +1,33 @@
1
+
2
+ import logger from 'consola';
3
+ import { generateBundle } from './injectManifest';
4
+ import { defineFlowPlugin } from '#app';
5
+ // @ts-ignore
6
+ import manifest from '#viteManifest';
7
+ export default defineFlowPlugin((flow) => {
8
+ if (typeof manifest === 'function') {
9
+ const _manifest = manifest();
10
+ flow.hook('page:chunks', (bundle, chunks) => {
11
+ try {
12
+ const chunk = generateBundle(flow.$config.app || {}, _manifest, `client/pages/${bundle}.ts`);
13
+
14
+ if (chunk) {
15
+ chunks.head.push(chunk.head);
16
+ chunks.body.push(chunk.body);
17
+ }
18
+ else {
19
+ logger.warn('Entry "%s" not found ', bundle);
20
+ }
21
+ }
22
+ catch (error) {
23
+ logger.error('Error in inject %s ', bundle);
24
+ }
25
+ });
26
+ }
27
+ else {
28
+ flow.hook('page:chunks', (bundle, chunks) => {
29
+ chunks.head.push(manifest.head());
30
+ chunks.body.push(manifest.body(bundle));
31
+ });
32
+ }
33
+ });
package/types.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ /// <reference types="nitropack" />
2
+ export * from './dist/index';