@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.
- package/build.config.ts +25 -0
- package/dist/app/entry.d.ts +2 -2
- package/dist/app/entry.mjs +2 -3
- package/dist/app/flow.d.ts +3 -12
- package/dist/app/flow.mjs +0 -3
- package/dist/core/runtime/nitro/flow.d.ts +0 -1
- package/dist/core/runtime/nitro/flow.mjs +14 -9
- package/dist/core/runtime/nitro/renderer.mjs +18 -82
- package/dist/head/runtime/plugin.mjs +1 -0
- package/dist/index.mjs +772 -543
- package/dist/pages/runtime/helpers/index.d.ts +1 -1
- package/dist/pages/runtime/helpers/index.mjs +3 -14
- package/dist/pages/runtime/index.d.ts +3 -10
- package/dist/pages/runtime/index.mjs +4 -12
- package/dist/pages/runtime/plugin.mjs +53 -0
- package/dist/vite-client/runtime/injectManifest.d.ts +26 -0
- package/dist/vite-client/runtime/injectManifest.mjs +104 -0
- package/dist/vite-client/runtime/plugin.d.ts +2 -0
- package/dist/vite-client/runtime/plugin.mjs +27 -0
- package/package.json +40 -55
- package/src/app/composables/index.ts +20 -0
- package/src/app/entry.ts +36 -0
- package/src/app/flow.ts +157 -0
- package/src/app/index.ts +5 -0
- package/src/auto-imports/module.ts +143 -0
- package/src/auto-imports/presets.ts +49 -0
- package/src/auto-imports/transform.ts +48 -0
- package/src/core/app.ts +90 -0
- package/src/core/builder.ts +60 -0
- package/src/core/flow.ts +93 -0
- package/src/core/modules.ts +32 -0
- package/src/core/nitro.ts +206 -0
- package/src/core/plugins/import-protection.ts +49 -0
- package/src/core/plugins/unctx.ts +31 -0
- package/src/core/runtime/nitro/flow.ts +43 -0
- package/src/core/runtime/nitro/paths.ts +20 -0
- package/src/core/runtime/nitro/renderer.ts +74 -0
- package/src/core/templates.ts +119 -0
- package/src/core/vite/builder/css.ts +28 -0
- package/src/core/vite/builder/dev-bundler.ts +248 -0
- package/src/core/vite/builder/index.ts +96 -0
- package/src/core/vite/builder/manifest.ts +33 -0
- package/src/core/vite/builder/plugins/analyze.ts +32 -0
- package/src/core/vite/builder/plugins/cache-dir.ts +13 -0
- package/src/core/vite/builder/plugins/dynamic-base.ts +64 -0
- package/src/core/vite/builder/plugins/virtual.ts +45 -0
- package/src/core/vite/builder/server.ts +164 -0
- package/src/core/vite/builder/types/index.ts +13 -0
- package/src/core/vite/builder/utils/index.ts +53 -0
- package/src/core/vite/builder/utils/warmup.ts +27 -0
- package/src/core/vite/builder/utils/wpfs.ts +7 -0
- package/src/core/vite/builder/vite-node.ts +110 -0
- package/src/core/vite/client/index.ts +63 -0
- package/src/dirs.ts +8 -0
- package/src/head/module.ts +37 -0
- package/src/head/runtime/composables.ts +16 -0
- package/src/head/runtime/index.ts +1 -0
- package/src/head/runtime/plugin.ts +12 -0
- package/src/index.ts +2 -0
- package/src/pages/module.ts +55 -0
- package/src/pages/runtime/helpers/chunks.ts +0 -0
- package/src/pages/runtime/helpers/index.ts +33 -0
- package/src/pages/runtime/index.ts +9 -0
- package/src/pages/runtime/plugin.ts +65 -0
- package/src/pages/templates.ts +20 -0
- package/src/pages/utils.ts +49 -0
- package/src/vite-client/module.ts +84 -0
- package/src/vite-client/runtime/injectManifest.ts +188 -0
- package/src/vite-client/runtime/plugin.ts +33 -0
- package/dist/app/entry.async.d.ts +0 -3
- package/dist/app/entry.async.mjs +0 -1
- package/dist/chunks/dev-bundler.mjs +0 -277
- package/dist/core/runtime/client.manifest.d.mts +0 -2
- package/dist/core/runtime/client.manifest.mjs +0 -6
- package/dist/core/runtime/vite-node-shared.d.mts +0 -1
- package/dist/core/runtime/vite-node-shared.d.ts +0 -8
- package/dist/core/runtime/vite-node-shared.mjs +0 -3
- package/dist/core/runtime/vite-node.d.mts +0 -2
- package/dist/core/runtime/vite-node.mjs +0 -41
- package/dist/pages/runtime/pages.mjs +0 -123
- /package/dist/pages/runtime/{pages.d.ts → plugin.d.ts} +0 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { existsSync, promises as fsp } from 'node:fs';
|
|
2
|
+
import {
|
|
3
|
+
build,
|
|
4
|
+
copyPublicAssets,
|
|
5
|
+
createDevServer,
|
|
6
|
+
createNitro,
|
|
7
|
+
prepare,
|
|
8
|
+
prerender,
|
|
9
|
+
scanHandlers,
|
|
10
|
+
writeTypes,
|
|
11
|
+
|
|
12
|
+
} from 'nitropack';
|
|
13
|
+
import fsExtra from 'fs-extra';
|
|
14
|
+
import type { NitroConfig, NitroDevEventHandler, NitroEventHandler } from 'nitropack';
|
|
15
|
+
import defu from 'defu';
|
|
16
|
+
import { join, resolve } from 'pathe';
|
|
17
|
+
import type { Flow } from '@monkeyplus/flow-schema';
|
|
18
|
+
import { dynamicEventHandler, toEventHandler } from 'h3';
|
|
19
|
+
// import { debounce } from 'perfect-debounce';
|
|
20
|
+
import { distDir } from '../dirs';
|
|
21
|
+
import { ImportProtectionPlugin } from './plugins/import-protection';
|
|
22
|
+
|
|
23
|
+
export async function initNitro(flow: Flow): Promise<void> {
|
|
24
|
+
const { handlers, devHandlers } = await resolveHandlers(flow);
|
|
25
|
+
|
|
26
|
+
const _nitroConfig = ((flow.options as any).nitro || {}) as NitroConfig;
|
|
27
|
+
// const vite = await createVite(flow);
|
|
28
|
+
globalThis.generate = {};
|
|
29
|
+
const nitroConfig: NitroConfig = defu(_nitroConfig, <NitroConfig>{
|
|
30
|
+
rootDir: flow.options.rootDir,
|
|
31
|
+
srcDir: join(flow.options.srcDir, 'server'),
|
|
32
|
+
dev: flow.options.dev,
|
|
33
|
+
preset: flow.options.dev ? 'nitro-dev' : undefined,
|
|
34
|
+
buildDir: flow.options.buildDir,
|
|
35
|
+
scanDirs: flow.options._layers.map((layer) => join(layer.config.srcDir, 'server')),
|
|
36
|
+
renderer: resolve(distDir, 'core/runtime/nitro/renderer'),
|
|
37
|
+
// errorHandler: resolve(distDir, 'core/runtime/nitro/error'),
|
|
38
|
+
nodeModulesDirs: flow.options.modulesDir,
|
|
39
|
+
handlers,
|
|
40
|
+
devHandlers: [
|
|
41
|
+
|
|
42
|
+
],
|
|
43
|
+
baseURL: flow.options.app.baseURL,
|
|
44
|
+
runtimeConfig: {
|
|
45
|
+
...flow.options.runtimeConfig,
|
|
46
|
+
app: {
|
|
47
|
+
...flow.options.runtimeConfig.app,
|
|
48
|
+
rootDir: flow.options.rootDir,
|
|
49
|
+
locale: flow.options.locale,
|
|
50
|
+
},
|
|
51
|
+
nitro: {
|
|
52
|
+
envPrefix: 'FLOW_',
|
|
53
|
+
...flow.options.runtimeConfig.nitro,
|
|
54
|
+
},
|
|
55
|
+
generate: flow.options._generate,
|
|
56
|
+
},
|
|
57
|
+
typescript: {
|
|
58
|
+
generateTsConfig: false,
|
|
59
|
+
},
|
|
60
|
+
publicAssets: [
|
|
61
|
+
{
|
|
62
|
+
baseURL: flow.options.app.buildAssetsDir,
|
|
63
|
+
dir: resolve(flow.options.buildDir, 'dist/client'),
|
|
64
|
+
},
|
|
65
|
+
...flow.options._layers
|
|
66
|
+
.map((layer) => join(layer.config.srcDir, layer.config.dir?.public || 'public'))
|
|
67
|
+
.filter((dir) => existsSync(dir))
|
|
68
|
+
.map((dir) => ({ dir })),
|
|
69
|
+
],
|
|
70
|
+
prerender: {
|
|
71
|
+
crawlLinks: flow.options._generate ? flow.options.generate.crawler : false,
|
|
72
|
+
routes: []
|
|
73
|
+
.concat(flow.options._generate ? ['/_urls', ...flow.options.generate.routes] : []),
|
|
74
|
+
},
|
|
75
|
+
sourcemap: flow.options.sourcemap,
|
|
76
|
+
externals: {
|
|
77
|
+
inline: [
|
|
78
|
+
...(flow.options.dev ? [] : ['eta', '@monkeyplus/', '@vue/', '@nuxt/', flow.options.buildDir]),
|
|
79
|
+
'@monkeyplus/flow/dist',
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
alias: {
|
|
83
|
+
|
|
84
|
+
// Vue 3 mocks
|
|
85
|
+
'estree-walker': 'unenv/runtime/mock/proxy',
|
|
86
|
+
'@babel/parser': 'unenv/runtime/mock/proxy',
|
|
87
|
+
// Paths
|
|
88
|
+
'#paths': resolve(distDir, 'core/runtime/nitro/paths'),
|
|
89
|
+
// Shortcut to server main file
|
|
90
|
+
'#server': '#build/dist/server/server.mjs',
|
|
91
|
+
//* App dir
|
|
92
|
+
'#app': flow.options.appDir,
|
|
93
|
+
|
|
94
|
+
// Nuxt aliases
|
|
95
|
+
...flow.options.alias,
|
|
96
|
+
},
|
|
97
|
+
rollupConfig: {
|
|
98
|
+
plugins: [],
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
});
|
|
102
|
+
await flow.callHook('nitro:config', nitroConfig);
|
|
103
|
+
|
|
104
|
+
// Base middleware
|
|
105
|
+
nitroConfig.handlers.unshift({
|
|
106
|
+
middleware: true,
|
|
107
|
+
handler: resolve(distDir, 'core/runtime/nitro/flow'),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Extend nitro config with hook
|
|
111
|
+
|
|
112
|
+
const nitro = await createNitro(nitroConfig);
|
|
113
|
+
|
|
114
|
+
// Expose nitro to modules
|
|
115
|
+
await flow.callHook('nitro:init', nitro);
|
|
116
|
+
|
|
117
|
+
// Connect vfs storages
|
|
118
|
+
nitro.vfs = flow.vfs = nitro.vfs || flow.vfs || {};
|
|
119
|
+
|
|
120
|
+
// Connect hooks
|
|
121
|
+
flow.hook('close', () => nitro.hooks.callHook('close'));
|
|
122
|
+
|
|
123
|
+
// Register flow protection patterns
|
|
124
|
+
nitro.hooks.hook('rollup:before', (nitro) => {
|
|
125
|
+
const plugin = ImportProtectionPlugin.rollup({
|
|
126
|
+
rootDir: flow.options.rootDir,
|
|
127
|
+
patterns: [
|
|
128
|
+
...['#app', /^#build(\/|$)/]
|
|
129
|
+
.map((p) => [p, 'Flow app aliases are not allowed in server routes.']) as [RegExp | string, string][],
|
|
130
|
+
],
|
|
131
|
+
});
|
|
132
|
+
nitro.options.rollupConfig.plugins.push(plugin);
|
|
133
|
+
});
|
|
134
|
+
// Setup handlers
|
|
135
|
+
const devMidlewareHandler = dynamicEventHandler();
|
|
136
|
+
nitro.options.devHandlers.unshift({ handler: devMidlewareHandler });
|
|
137
|
+
nitro.options.devHandlers.push(...devHandlers);
|
|
138
|
+
nitro.options.handlers.unshift({
|
|
139
|
+
route: '/__flow_error',
|
|
140
|
+
lazy: true,
|
|
141
|
+
handler: resolve(distDir, 'core/runtime/nitro/renderer'),
|
|
142
|
+
});
|
|
143
|
+
// Add typed route responses
|
|
144
|
+
flow.hook('prepare:types', async(opts) => {
|
|
145
|
+
if (flow.options._prepare) {
|
|
146
|
+
await scanHandlers(nitro);
|
|
147
|
+
await writeTypes(nitro);
|
|
148
|
+
}
|
|
149
|
+
opts.references.push({ path: resolve(flow.options.buildDir, 'types/nitro.d.ts') });
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
flow.hook('build:done', async() => {
|
|
153
|
+
await flow.callHook('nitro:build:before', nitro);
|
|
154
|
+
// console.log(nitro.options._config, nitro.vfs['#build/dist/server/server.mjs']);
|
|
155
|
+
|
|
156
|
+
if (flow.options.dev) {
|
|
157
|
+
await build(nitro);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
await prepare(nitro);
|
|
161
|
+
|
|
162
|
+
await copyPublicAssets(nitro);
|
|
163
|
+
|
|
164
|
+
await prerender(nitro);
|
|
165
|
+
if (!flow.options._generate) {
|
|
166
|
+
await build(nitro);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
// TODO: refactor to nuxt
|
|
170
|
+
const nitroDev = await createNitro({
|
|
171
|
+
...nitro.options._config,
|
|
172
|
+
rootDir: nitro.options.rootDir,
|
|
173
|
+
logLevel: 0,
|
|
174
|
+
preset: 'nitro-prerender',
|
|
175
|
+
});
|
|
176
|
+
flow.server = nitroDev;
|
|
177
|
+
|
|
178
|
+
const distDir = resolve(flow.options.rootDir, 'dist');
|
|
179
|
+
if (!existsSync(distDir))
|
|
180
|
+
await fsp.symlink(nitro.options.output.publicDir, distDir, 'junction').catch(() => {});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
if (flow.options.dev) {
|
|
185
|
+
flow.hook('build:compile', ({ compiler }) => {
|
|
186
|
+
compiler.outputFileSystem = { ...fsExtra, join } as any;
|
|
187
|
+
});
|
|
188
|
+
flow.hook('server:devMiddleware', (m) => { devMidlewareHandler.set(toEventHandler(m)); });
|
|
189
|
+
flow.server = createDevServer(nitro);
|
|
190
|
+
flow.hook('build:resources', () => {
|
|
191
|
+
flow.server.reload();
|
|
192
|
+
});
|
|
193
|
+
const waitUntilCompile = new Promise<void>((resolve) => nitro.hooks.hook('compiled', () => resolve()));
|
|
194
|
+
flow.hook('build:done', () => waitUntilCompile);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function resolveHandlers(flow: Flow) {
|
|
199
|
+
const handlers: NitroEventHandler[] = [...flow.options.serverHandlers];
|
|
200
|
+
const devHandlers: NitroDevEventHandler[] = [...flow.options.devServerHandlers];
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
handlers,
|
|
204
|
+
devHandlers,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { createUnplugin } from 'unplugin';
|
|
3
|
+
import { logger } from '@monkeyplus/flow-kit';
|
|
4
|
+
import { isAbsolute, relative } from 'pathe';
|
|
5
|
+
|
|
6
|
+
const _require = createRequire(import.meta.url);
|
|
7
|
+
|
|
8
|
+
interface ImportProtectionOptions {
|
|
9
|
+
rootDir: string
|
|
10
|
+
patterns: [importPattern: string | RegExp, warning?: string][]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// export const vueAppPatterns = (flow: Flow) => [
|
|
14
|
+
// [/^(nuxt3|nuxt)$/, '`nuxt3`/`nuxt` cannot be imported directly. Instead, import runtime Nuxt composables from `#app` or `#imports`.'],
|
|
15
|
+
// [/nuxt\.config/, 'Importing directly from a `nuxt.config` file is not allowed. Instead, use runtime config or a module.'],
|
|
16
|
+
// [/(^|node_modules\/)@vue\/composition-api/],
|
|
17
|
+
// ...nuxt.options.modules.filter((m) => typeof m === 'string').map((m: string) =>
|
|
18
|
+
// [new RegExp(`^${escapeRE(m)}$`), 'Importing directly from module entry points is not allowed.']),
|
|
19
|
+
// ...[/(^|node_modules\/)@nuxt\/kit/, /^nitropack/]
|
|
20
|
+
// .map((i) => [i, 'This module cannot be imported in the Vue part of your app.']),
|
|
21
|
+
// [new RegExp(escapeRE(resolve(nuxt.options.srcDir, (nuxt.options.dir as any).server || 'server'))), 'Importing from server middleware is not allowed in the Vue part of your app.'],
|
|
22
|
+
// ] as ImportProtectionOptions['patterns'];
|
|
23
|
+
|
|
24
|
+
export const ImportProtectionPlugin = createUnplugin((options: ImportProtectionOptions) => {
|
|
25
|
+
const cache: Record<string, Map<string | RegExp, boolean>> = {};
|
|
26
|
+
return {
|
|
27
|
+
name: 'flow:import-protection',
|
|
28
|
+
enforce: 'pre',
|
|
29
|
+
resolveId(id, importer) {
|
|
30
|
+
const invalidImports = options.patterns.filter(([pattern]) => pattern instanceof RegExp ? pattern.test(id) : pattern === id);
|
|
31
|
+
let matched: boolean;
|
|
32
|
+
for (const match of invalidImports) {
|
|
33
|
+
cache[id] = cache[id] || new Map();
|
|
34
|
+
const [pattern, warning] = match;
|
|
35
|
+
// Skip if already warned
|
|
36
|
+
if (cache[id].has(pattern)) continue;
|
|
37
|
+
|
|
38
|
+
const relativeImporter = isAbsolute(importer) ? relative(options.rootDir, importer) : importer;
|
|
39
|
+
logger.error(warning || 'Invalid import', `[importing \`${id}\` from \`${relativeImporter}\`]`);
|
|
40
|
+
cache[id].set(pattern, true);
|
|
41
|
+
matched = true;
|
|
42
|
+
}
|
|
43
|
+
if (matched)
|
|
44
|
+
return _require.resolve('unenv/runtime/mock/proxy');
|
|
45
|
+
|
|
46
|
+
return null;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Nuxt, NuxtApp, NuxtMiddleware } from '@monkeyplus/flow-schema';
|
|
2
|
+
import { createTransformer } from 'unctx/transform';
|
|
3
|
+
import { createUnplugin } from 'unplugin';
|
|
4
|
+
|
|
5
|
+
export const UnctxTransformPlugin = (nuxt: Nuxt) => {
|
|
6
|
+
const transformer = createTransformer({
|
|
7
|
+
asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'],
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
let app: NuxtApp | undefined;
|
|
11
|
+
let middleware: NuxtMiddleware[] = [];
|
|
12
|
+
nuxt.hook('app:resolve', (_app) => { app = _app; });
|
|
13
|
+
nuxt.hook('pages:middleware:extend', (_middlewares) => { middleware = _middlewares; });
|
|
14
|
+
|
|
15
|
+
return createUnplugin((options: { sourcemap?: boolean } = {}) => ({
|
|
16
|
+
name: 'unctx:transfrom',
|
|
17
|
+
enforce: 'post',
|
|
18
|
+
transformInclude(id) {
|
|
19
|
+
return Boolean(app?.plugins.find((i) => i.src === id) || middleware.find((m) => m.path === id));
|
|
20
|
+
},
|
|
21
|
+
transform(code, id) {
|
|
22
|
+
const result = transformer.transform(code);
|
|
23
|
+
if (result) {
|
|
24
|
+
return {
|
|
25
|
+
code: result.code,
|
|
26
|
+
map: options.sourcemap && result.magicString.generateMap({ source: id, includeContent: true }),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { defineEventHandler } from 'h3';
|
|
2
|
+
// TODO: remove in node 18
|
|
3
|
+
import 'node-fetch-native/polyfill';
|
|
4
|
+
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import { useRuntimeConfig } from '#internal/nitro';
|
|
7
|
+
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
const getServerApp = cachedImport(() => import('#server'));
|
|
10
|
+
|
|
11
|
+
const getFlowRenderer = cachedResult(async() => {
|
|
12
|
+
// Load server bundle
|
|
13
|
+
const createFlowApp = await getServerApp();
|
|
14
|
+
|
|
15
|
+
if (!createFlowApp) throw new Error('Server bundle is not available');
|
|
16
|
+
// Create renderer
|
|
17
|
+
return createFlowApp(useRuntimeConfig());
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export default defineEventHandler(async({ context }) => {
|
|
21
|
+
const flow = await getFlowRenderer(); // const flow = module.default(_config);
|
|
22
|
+
context.flow = flow;
|
|
23
|
+
context.render = flow.render;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
function _interopDefault(e: any) {
|
|
27
|
+
return e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function cachedImport <M>(importer: () => Promise<M>) {
|
|
31
|
+
return cachedResult(() => importer().then(_interopDefault).then(_interopDefault)) as () => Promise<M>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function cachedResult <T>(fn: () => Promise<T>): () => Promise<T> {
|
|
35
|
+
let res: Promise<T> | null = null;
|
|
36
|
+
// console.log(res);
|
|
37
|
+
return () => {
|
|
38
|
+
if (res === null)
|
|
39
|
+
res = fn().catch((err) => { res = null; throw err; });
|
|
40
|
+
|
|
41
|
+
return res;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { joinURL } from 'ufo';
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import { useRuntimeConfig } from '#internal/nitro';
|
|
4
|
+
|
|
5
|
+
export function baseURL(): string {
|
|
6
|
+
return useRuntimeConfig().app.baseURL;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function buildAssetsDir(): string {
|
|
10
|
+
return useRuntimeConfig().app.buildAssetsDir;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function buildAssetsURL(...path: string[]): string {
|
|
14
|
+
return joinURL(publicAssetsURL(), useRuntimeConfig().app.buildAssetsDir, ...path);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function publicAssetsURL(...path: string[]): string {
|
|
18
|
+
const publicBase = useRuntimeConfig().app.cdnURL || useRuntimeConfig().app.baseURL;
|
|
19
|
+
return path.length ? joinURL(publicBase, ...path) : publicBase;
|
|
20
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { eventHandler, useQuery } from 'h3';
|
|
2
|
+
import type{ FlowApp } from '#app/flow';
|
|
3
|
+
|
|
4
|
+
export default eventHandler(async(event) => {
|
|
5
|
+
const url = event.req.url?.split('?')[0];
|
|
6
|
+
|
|
7
|
+
// console.debug(url);
|
|
8
|
+
|
|
9
|
+
const flow = event.context.flow as FlowApp;
|
|
10
|
+
if (url.includes('_urls')) {
|
|
11
|
+
const urls = await flow.app.utils.getUrls();
|
|
12
|
+
return urls;
|
|
13
|
+
}
|
|
14
|
+
const scripts = { head: [], bodyStart: [], bodyEnd: [] };
|
|
15
|
+
const chunks = { head: [], body: [] };
|
|
16
|
+
const generate = {};
|
|
17
|
+
|
|
18
|
+
// console.log(flow);
|
|
19
|
+
const { page, locale, params, view } = flow.router.byUrl.lookup(url!) || {};
|
|
20
|
+
// Truncate page
|
|
21
|
+
if (!page) return;
|
|
22
|
+
|
|
23
|
+
await flow.callHook('page:scripts', scripts);
|
|
24
|
+
if (view.bundle)
|
|
25
|
+
await flow.callHook('page:chunks', view.bundle, chunks);
|
|
26
|
+
|
|
27
|
+
const templateContext = {} as any;
|
|
28
|
+
|
|
29
|
+
const dynamyc = params?._;
|
|
30
|
+
const contextPage = {} as any;
|
|
31
|
+
const utils = Object.assign({ getLocale: () => locale }, flow.app.utils);
|
|
32
|
+
templateContext.url = url;
|
|
33
|
+
templateContext.locale = locale;
|
|
34
|
+
templateContext.view = view;
|
|
35
|
+
|
|
36
|
+
if (dynamyc && page.dynamic) {
|
|
37
|
+
const pages = await page.dynamic.method({ locale, utils });
|
|
38
|
+
const dynamicPage = pages.find((_page) => _page.url === dynamyc);
|
|
39
|
+
if (!dynamicPage) return;
|
|
40
|
+
if (page.dynamic.assign)
|
|
41
|
+
templateContext[page.dynamic.assign] = dynamicPage.context;
|
|
42
|
+
|
|
43
|
+
contextPage.dynamyc = dynamicPage;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
templateContext.seo = {};
|
|
47
|
+
templateContext.context = {};
|
|
48
|
+
templateContext.sharedContext = {};
|
|
49
|
+
templateContext.utils = utils;
|
|
50
|
+
templateContext.seo = await page.seo?.({ ...contextPage, utils }) || {};
|
|
51
|
+
templateContext.context = await page.context?.({ ...contextPage, utils }) || {};
|
|
52
|
+
templateContext.getHeadScripts = () => {
|
|
53
|
+
return scripts.head.join('\n');
|
|
54
|
+
};
|
|
55
|
+
templateContext.getHeadChunks = () => {
|
|
56
|
+
return chunks.head.join('\n');
|
|
57
|
+
};
|
|
58
|
+
templateContext.getBodyChunks = () => {
|
|
59
|
+
return chunks.body.join('\n');
|
|
60
|
+
};
|
|
61
|
+
const query = useQuery(event);
|
|
62
|
+
if (query?.context)
|
|
63
|
+
return { ...templateContext, utils: Object.keys(utils) };
|
|
64
|
+
|
|
65
|
+
const html = await flow.render(view as any, templateContext);
|
|
66
|
+
event.res.setHeader('Content-Type', 'text/html;charset=UTF-8');
|
|
67
|
+
|
|
68
|
+
if (flow.generate) {
|
|
69
|
+
await flow.callHook('page:generate', generate);
|
|
70
|
+
globalThis.generate = generate;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return html;
|
|
74
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { templateUtils } from '@monkeyplus/flow-kit';
|
|
2
|
+
import type { Nuxt, NuxtApp, NuxtTemplate } from '@monkeyplus/flow-schema';
|
|
3
|
+
import { genArrayFromRaw, genDynamicImport, genString } from 'knitwork';
|
|
4
|
+
import { isAbsolute, join, relative } from 'pathe';
|
|
5
|
+
import { generateTypes, resolveSchema } from 'untyped';
|
|
6
|
+
|
|
7
|
+
import escapeRE from 'escape-string-regexp';
|
|
8
|
+
|
|
9
|
+
export interface TemplateContext {
|
|
10
|
+
nuxt: Nuxt
|
|
11
|
+
app: NuxtApp
|
|
12
|
+
}
|
|
13
|
+
// export const cssTemplate = {
|
|
14
|
+
// filename: 'css.mjs',
|
|
15
|
+
// getContents: (ctx: TemplateContext) => ctx.flow.options.css.map((i) => genImport(i)).join('\n'),
|
|
16
|
+
// };
|
|
17
|
+
|
|
18
|
+
export const serverPluginTemplate = {
|
|
19
|
+
filename: 'plugins/server.mjs',
|
|
20
|
+
getContents(ctx: TemplateContext) {
|
|
21
|
+
const serverPlugins = ctx.app.plugins;
|
|
22
|
+
return [
|
|
23
|
+
templateUtils.importSources(serverPlugins.map((p) => p.src)),
|
|
24
|
+
`export default ${genArrayFromRaw([
|
|
25
|
+
...serverPlugins.map((p) => templateUtils.importName(p.src)),
|
|
26
|
+
])}`,
|
|
27
|
+
].join('\n');
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const pluginsDeclaration = {
|
|
32
|
+
filename: 'types/plugins.d.ts',
|
|
33
|
+
getContents: (ctx: TemplateContext) => {
|
|
34
|
+
// console.log(ctx);
|
|
35
|
+
|
|
36
|
+
const EXTENSION_RE = new RegExp(`(?<=\\w)(${ctx.nuxt.options.extensions.map((e) => escapeRE(e)).join('|')})$`, 'g');
|
|
37
|
+
const tsImports = ctx.app.plugins.map((p) => (isAbsolute(p.src) ? relative(join(ctx.nuxt.options.buildDir, 'types'), p.src) : p.src).replace(EXTENSION_RE, ''));
|
|
38
|
+
|
|
39
|
+
return `// Generated by Flow'
|
|
40
|
+
import type { Plugin } from '#app'
|
|
41
|
+
|
|
42
|
+
type Decorate<T extends Record<string, any>> = { [K in keyof T as K extends string ? \`$\${K}\` : never]: T[K] }
|
|
43
|
+
|
|
44
|
+
type InjectionType<A extends Plugin> = A extends Plugin<infer T> ? Decorate<T> : unknown
|
|
45
|
+
|
|
46
|
+
type FlowAppInjections = \n ${tsImports.map((p) => `InjectionType<typeof ${genDynamicImport(p, { wrapper: false })}.default>`).join(' &\n ')}
|
|
47
|
+
|
|
48
|
+
declare module '#app' {
|
|
49
|
+
interface FlowApp extends FlowAppInjections { }
|
|
50
|
+
}
|
|
51
|
+
// TODO: Insert extend types
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
export { }
|
|
55
|
+
`;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const adHocModules = ['auto-imports', 'meta', 'pages', 'vite-client'];
|
|
60
|
+
export const schemaTemplate = {
|
|
61
|
+
filename: 'types/schema.d.ts',
|
|
62
|
+
getContents: ({ nuxt }: TemplateContext) => {
|
|
63
|
+
const moduleInfo = nuxt.options._installedModules.map((m) => ({
|
|
64
|
+
...m.meta || {},
|
|
65
|
+
importName: m.entryPath || m.meta?.name,
|
|
66
|
+
})).filter((m) => m.configKey && m.name && !adHocModules.includes(m.name));
|
|
67
|
+
|
|
68
|
+
return [
|
|
69
|
+
'import { FlowModule } from \'@monkeyplus/flow-schema\'',
|
|
70
|
+
'declare module \'@monkeyplus/flow-schema\' {',
|
|
71
|
+
' interface FlowConfig {',
|
|
72
|
+
...moduleInfo.filter(Boolean).map((meta) =>
|
|
73
|
+
` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends FlowModule<infer O> ? Partial<O> : Record<string, any>`,
|
|
74
|
+
),
|
|
75
|
+
' }',
|
|
76
|
+
generateTypes(resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))),
|
|
77
|
+
{
|
|
78
|
+
interfaceName: 'RuntimeConfig',
|
|
79
|
+
addExport: false,
|
|
80
|
+
addDefaults: false,
|
|
81
|
+
allowExtraKeys: false,
|
|
82
|
+
indentation: 2,
|
|
83
|
+
}),
|
|
84
|
+
generateTypes(resolveSchema(nuxt.options.runtimeConfig.public),
|
|
85
|
+
{
|
|
86
|
+
interfaceName: 'PublicRuntimeConfig',
|
|
87
|
+
addExport: false,
|
|
88
|
+
addDefaults: false,
|
|
89
|
+
allowExtraKeys: false,
|
|
90
|
+
indentation: 2,
|
|
91
|
+
}),
|
|
92
|
+
'}',
|
|
93
|
+
].join('\n');
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const publicPathTemplate: NuxtTemplate = {
|
|
98
|
+
filename: 'paths.mjs',
|
|
99
|
+
getContents({ nuxt }) {
|
|
100
|
+
return [
|
|
101
|
+
'import { joinURL } from \'ufo\'',
|
|
102
|
+
!nuxt.options.dev && 'import { useRuntimeConfig } from \'#internal/nitro\'',
|
|
103
|
+
|
|
104
|
+
nuxt.options.dev
|
|
105
|
+
? `const appConfig = ${JSON.stringify(nuxt.options.app)}`
|
|
106
|
+
: 'const appConfig = useRuntimeConfig().app',
|
|
107
|
+
|
|
108
|
+
'export const baseURL = () => appConfig.baseURL',
|
|
109
|
+
'export const buildAssetsDir = () => appConfig.buildAssetsDir',
|
|
110
|
+
|
|
111
|
+
'export const buildAssetsURL = (...path) => joinURL(publicAssetsURL(), buildAssetsDir(), ...path)',
|
|
112
|
+
|
|
113
|
+
'export const publicAssetsURL = (...path) => {',
|
|
114
|
+
' const publicBase = appConfig.cdnURL || appConfig.baseURL',
|
|
115
|
+
' return path.length ? joinURL(publicBase, ...path) : publicBase',
|
|
116
|
+
'}',
|
|
117
|
+
].filter(Boolean).join('\n');
|
|
118
|
+
},
|
|
119
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { requireModule } from '@monkeyplus/flow-kit';
|
|
2
|
+
import type { Flow } from '@monkeyplus/flow-schema';
|
|
3
|
+
import { distDir } from '../../../dirs';
|
|
4
|
+
import type { ViteOptions } from './types';
|
|
5
|
+
|
|
6
|
+
export function resolveCSSOptions(flow: Flow): ViteOptions['css'] {
|
|
7
|
+
const css: ViteOptions['css'] & { postcss: Exclude<ViteOptions['css']['postcss'], string> } = {
|
|
8
|
+
postcss: {
|
|
9
|
+
plugins: [],
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const lastPlugins = ['autoprefixer', 'cssnano'];
|
|
14
|
+
css.postcss.plugins = Object.entries(flow.options.postcss?.plugins || {})
|
|
15
|
+
.sort((a, b) => lastPlugins.indexOf(a[0]) - lastPlugins.indexOf(b[0]))
|
|
16
|
+
.filter(([, opts]) => opts)
|
|
17
|
+
.map(([name, opts]) => {
|
|
18
|
+
const plugin = requireModule(name, {
|
|
19
|
+
paths: [
|
|
20
|
+
...flow.options.modulesDir,
|
|
21
|
+
distDir,
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
return plugin(opts);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return css;
|
|
28
|
+
}
|