@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.
- package/app.d.ts +1 -0
- package/bin/flow.mjs +2 -0
- package/build.config.ts +25 -0
- package/dist/app/composables/index.d.ts +4 -0
- package/dist/app/composables/index.mjs +12 -0
- package/dist/app/entry.d.ts +3 -0
- package/dist/app/entry.mjs +23 -0
- package/dist/app/flow.d.ts +73 -0
- package/dist/app/flow.mjs +85 -0
- package/dist/app/index.d.ts +3 -0
- package/dist/app/index.mjs +3 -0
- package/dist/core/runtime/nitro/flow.d.ts +3 -0
- package/dist/core/runtime/nitro/flow.mjs +32 -0
- package/dist/core/runtime/nitro/paths.d.ts +4 -0
- package/dist/core/runtime/nitro/paths.mjs +15 -0
- package/dist/core/runtime/nitro/renderer.d.ts +2 -0
- package/dist/core/runtime/nitro/renderer.mjs +59 -0
- package/dist/head/runtime/composables.d.ts +9 -0
- package/dist/head/runtime/composables.mjs +2 -0
- package/dist/head/runtime/index.d.ts +1 -0
- package/dist/head/runtime/index.mjs +1 -0
- package/dist/head/runtime/plugin.d.ts +2 -0
- package/dist/head/runtime/plugin.mjs +6 -0
- package/dist/index.d.ts +8 -61
- package/dist/index.mjs +1286 -954
- package/dist/pages/runtime/helpers/chunks.d.ts +0 -0
- package/dist/pages/runtime/helpers/chunks.mjs +0 -0
- package/dist/pages/runtime/helpers/index.d.ts +5 -0
- package/dist/pages/runtime/helpers/index.mjs +28 -0
- package/dist/pages/runtime/plugin.d.ts +2 -0
- package/dist/pages/runtime/plugin.mjs +51 -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 +54 -36
- 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 +55 -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/plugin.ts +58 -0
- package/src/pages/templates.ts +20 -0
- package/src/pages/utils.ts +49 -0
- package/src/vite-client/module.ts +81 -0
- package/src/vite-client/runtime/injectManifest.ts +188 -0
- package/src/vite-client/runtime/plugin.ts +33 -0
- package/types.d.ts +2 -0
- package/dist/index.cjs +0 -1061
- package/types/core.d.ts +0 -143
- package/types/flow.d.ts +0 -239
- package/types/index.d.ts +0 -38
- package/types/interfaces.d.ts +0 -61
package/dist/index.mjs
CHANGED
|
@@ -1,1034 +1,1366 @@
|
|
|
1
|
-
import path, { join } from 'path';
|
|
2
|
-
import { applyToDefaults } from '@hapi/hoek';
|
|
3
|
-
import consola from 'consola';
|
|
4
|
-
import * as R from 'ramda';
|
|
5
|
-
import { flatten, dissoc, groupBy, mapObjIndexed } from 'ramda';
|
|
6
|
-
import { notFound } from '@hapi/boom';
|
|
7
|
-
import os from 'os';
|
|
8
|
-
import chalk from 'chalk';
|
|
9
|
-
import fs from 'fs-extra';
|
|
10
|
-
import fs$1 from 'fs';
|
|
11
1
|
import { createHooks } from 'hookable';
|
|
2
|
+
import { dirname, resolve, normalize, join, isAbsolute, relative, extname } from 'pathe';
|
|
3
|
+
import { defineFlowModule, addPlugin, defineNuxtModule, logger, addTemplate, addPluginTemplate, addVitePlugin, useNuxt, resolveAlias, resolveFiles, nuxtCtx, installModule, loadFlowConfig, templateUtils, normalizeTemplate, compileTemplate, normalizePlugin, resolveFilesFlow, isIgnoredFlow } from '@monkeyplus/flow-kit';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
5
|
+
import { defineUnimportPreset, createUnimport, toImports, scanDirExports } from 'unimport';
|
|
6
|
+
import { createUnplugin } from 'unplugin';
|
|
7
|
+
import { parseURL, parseQuery, joinURL, withoutTrailingSlash } from 'ufo';
|
|
8
|
+
import escapeRE from 'escape-string-regexp';
|
|
9
|
+
import { camelCase, pascalCase } from 'scule';
|
|
10
|
+
import { genImport, genDynamicImport, genArrayFromRaw, genString, genObjectFromRawEntries } from 'knitwork';
|
|
11
|
+
import fse from 'fs-extra';
|
|
12
|
+
import logger$1 from 'consola';
|
|
13
|
+
import * as vite from 'vite';
|
|
14
|
+
import { createServer, build as build$1 } from 'vite';
|
|
15
|
+
import { debounce } from 'perfect-debounce';
|
|
16
|
+
import { existsSync, promises, readdirSync, statSync } from 'node:fs';
|
|
17
|
+
import { createNitro, scanHandlers, writeTypes, build as build$2, prepare, copyPublicAssets, prerender, createDevServer } from 'nitropack';
|
|
18
|
+
import defu from 'defu';
|
|
19
|
+
import { dynamicEventHandler, toEventHandler } from 'h3';
|
|
20
|
+
import { createRequire, builtinModules } from 'node:module';
|
|
12
21
|
import chokidar from 'chokidar';
|
|
22
|
+
import { generateTypes, resolveSchema } from 'untyped';
|
|
23
|
+
import { getPort } from 'get-port-please';
|
|
24
|
+
import { sanitizeFilePath } from 'mlly';
|
|
25
|
+
import replace from '@rollup/plugin-replace';
|
|
26
|
+
import { isExternal as isExternal$1, ExternalsDefaults } from 'externality';
|
|
27
|
+
import { createHash } from 'node:crypto';
|
|
28
|
+
import MagicString from 'magic-string';
|
|
13
29
|
|
|
14
|
-
const
|
|
15
|
-
const version = "4.0.0-beta.9";
|
|
16
|
-
const description = "Utils hapi";
|
|
17
|
-
const author = "Andres Navarrete";
|
|
18
|
-
const license = "MIT";
|
|
19
|
-
const main = "./dist/index.cjs";
|
|
20
|
-
const module = "./dist/index.mjs";
|
|
21
|
-
const types = "./dist/index.d.ts";
|
|
22
|
-
const files = [
|
|
23
|
-
"dist/",
|
|
24
|
-
"types/"
|
|
25
|
-
];
|
|
26
|
-
const exports = {
|
|
27
|
-
".": {
|
|
28
|
-
"import": "./dist/index.mjs",
|
|
29
|
-
require: "./dist/index.cjs"
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const scripts = {
|
|
33
|
-
build: "unbuild",
|
|
34
|
-
dev: "pnpm stub && pnpm types",
|
|
35
|
-
stub: "unbuild --stub",
|
|
36
|
-
types: "tsup ./src/index.ts --dts-only --external types && esno ../../scripts/replace",
|
|
37
|
-
lint: "eslint --ext .js,.ts .",
|
|
38
|
-
fix: "eslint --fix --ext .ts .",
|
|
39
|
-
prepublishOnly: "pnpm run build",
|
|
40
|
-
start: "esno src/index.ts",
|
|
41
|
-
test: "vitest"
|
|
42
|
-
};
|
|
43
|
-
const dependencies$1 = {
|
|
44
|
-
"@hapi/boom": "9.x.x",
|
|
45
|
-
"@hapi/hoek": "9.x.x",
|
|
46
|
-
consola: "^2.15.3",
|
|
47
|
-
chalk: "^4.1.2",
|
|
48
|
-
chokidar: "^3.5.3",
|
|
49
|
-
hookable: "^5.1.1",
|
|
50
|
-
"fs-extra": "^10.0.0",
|
|
51
|
-
ramda: "^0.28.0"
|
|
52
|
-
};
|
|
53
|
-
const devDependencies = {
|
|
54
|
-
"@types/fs-extra": "^9.0.13",
|
|
55
|
-
"@types/hapi__hapi": "^20.0.10",
|
|
56
|
-
"@types/hapi__nes": "^11.0.5",
|
|
57
|
-
"@types/hapi__vision": "^5.5.3",
|
|
58
|
-
"@types/ramda": "^0.27.64"
|
|
59
|
-
};
|
|
60
|
-
const peerDependencies = {
|
|
61
|
-
"@hapi/hapi": "^20.x"
|
|
62
|
-
};
|
|
63
|
-
const gitHead = "1d5387b9c77909019f36d360a0ac53ddaa40e4af";
|
|
64
|
-
const pkg = {
|
|
65
|
-
name: name,
|
|
66
|
-
version: version,
|
|
67
|
-
description: description,
|
|
68
|
-
author: author,
|
|
69
|
-
license: license,
|
|
70
|
-
main: main,
|
|
71
|
-
module: module,
|
|
72
|
-
types: types,
|
|
73
|
-
files: files,
|
|
74
|
-
exports: exports,
|
|
75
|
-
scripts: scripts,
|
|
76
|
-
dependencies: dependencies$1,
|
|
77
|
-
devDependencies: devDependencies,
|
|
78
|
-
peerDependencies: peerDependencies,
|
|
79
|
-
gitHead: gitHead
|
|
80
|
-
};
|
|
30
|
+
const version = "5.0.0-beta.10";
|
|
81
31
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
};
|
|
89
|
-
const isProduction = process.env.NODE_ENV === "production";
|
|
90
|
-
const isGenerate = process.env.GENERATE;
|
|
32
|
+
let _distDir = dirname(fileURLToPath(import.meta.url));
|
|
33
|
+
if (_distDir.endsWith("chunks"))
|
|
34
|
+
_distDir = dirname(_distDir);
|
|
35
|
+
const distDir = _distDir;
|
|
36
|
+
const pkgDir = resolve(distDir, "..");
|
|
37
|
+
resolve(distDir, "runtime");
|
|
91
38
|
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
39
|
+
const metaModule = defineFlowModule({
|
|
40
|
+
meta: {
|
|
41
|
+
name: "meta"
|
|
42
|
+
},
|
|
43
|
+
defaults: {
|
|
44
|
+
charset: "utf-8",
|
|
45
|
+
viewport: "width=device-width, initial-scale=1"
|
|
46
|
+
},
|
|
47
|
+
setup(options, flow) {
|
|
48
|
+
const runtimeDir = flow.options.alias["#head"] || resolve(distDir, "head/runtime");
|
|
49
|
+
flow.options.alias["#head"] = runtimeDir;
|
|
50
|
+
addPlugin({ src: resolve(runtimeDir, "plugin") });
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }) => {
|
|
55
|
+
return {
|
|
56
|
+
name: "flow:auto-imports-transform",
|
|
57
|
+
enforce: "post",
|
|
58
|
+
transformInclude(id) {
|
|
59
|
+
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href));
|
|
60
|
+
const { type, macro } = parseQuery(search);
|
|
61
|
+
const exclude = options.transform?.exclude || [/[\\/]node_modules[\\/]/];
|
|
62
|
+
const include = options.transform?.include || [];
|
|
63
|
+
if (exclude.some((pattern) => id.match(pattern)))
|
|
64
|
+
return false;
|
|
65
|
+
if (include.some((pattern) => id.match(pattern)))
|
|
66
|
+
return true;
|
|
67
|
+
if (pathname.endsWith(".vue") && (type === "template" || type === "script" || macro || !search))
|
|
68
|
+
return true;
|
|
69
|
+
if (pathname.match(/\.((c|m)?j|t)sx?$/g))
|
|
70
|
+
return true;
|
|
71
|
+
},
|
|
72
|
+
async transform(_code, id) {
|
|
73
|
+
const { code, s } = await ctx.injectImports(_code);
|
|
74
|
+
if (code === _code)
|
|
75
|
+
return;
|
|
76
|
+
return {
|
|
77
|
+
code,
|
|
78
|
+
map: sourcemap && s.generateMap({ source: id, includeContent: true })
|
|
105
79
|
};
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const commonPresets = [
|
|
85
|
+
defineUnimportPreset({
|
|
86
|
+
from: "#head",
|
|
87
|
+
imports: [
|
|
88
|
+
"useHead"
|
|
89
|
+
]
|
|
90
|
+
})
|
|
91
|
+
];
|
|
92
|
+
const appPreset = defineUnimportPreset({
|
|
93
|
+
from: "#app",
|
|
94
|
+
imports: [
|
|
95
|
+
"useRuntimeConfig",
|
|
96
|
+
"defineFlowPlugin",
|
|
97
|
+
"defineNuxtPlugin",
|
|
98
|
+
"useCookie",
|
|
99
|
+
"refreshNuxtData",
|
|
100
|
+
"useAsyncData",
|
|
101
|
+
"useRoute"
|
|
102
|
+
]
|
|
103
|
+
});
|
|
104
|
+
const vuePreset = defineUnimportPreset({
|
|
105
|
+
from: "vue",
|
|
106
|
+
imports: [
|
|
107
|
+
"defineComponent",
|
|
108
|
+
"getCurrentInstance",
|
|
109
|
+
"useSlots",
|
|
110
|
+
"h",
|
|
111
|
+
"computed"
|
|
112
|
+
]
|
|
113
|
+
});
|
|
114
|
+
const defaultPresets = [
|
|
115
|
+
...commonPresets,
|
|
116
|
+
appPreset,
|
|
117
|
+
vuePreset
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
const autoImportsModule = defineNuxtModule({
|
|
121
|
+
meta: {
|
|
122
|
+
name: "auto-imports",
|
|
123
|
+
configKey: "autoImports"
|
|
124
|
+
},
|
|
125
|
+
defaults: {
|
|
126
|
+
presets: defaultPresets,
|
|
127
|
+
global: false,
|
|
128
|
+
imports: [],
|
|
129
|
+
dirs: [],
|
|
130
|
+
transform: {
|
|
131
|
+
exclude: void 0
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
async setup(options, flow) {
|
|
135
|
+
await flow.callHook("autoImports:sources", options.presets);
|
|
136
|
+
options.presets.forEach((i) => {
|
|
137
|
+
if (typeof i !== "string" && i.names && !i.imports) {
|
|
138
|
+
i.imports = i.names;
|
|
139
|
+
logger.warn("auto-imports: presets.names is deprecated, use presets.imports instead");
|
|
140
|
+
}
|
|
114
141
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (!dynamic)
|
|
119
|
-
return h.continue;
|
|
120
|
-
const pages = await dynamic.pages({
|
|
121
|
-
server,
|
|
122
|
-
route: req.route,
|
|
123
|
-
locale: flow?.locale
|
|
124
|
-
});
|
|
125
|
-
if (!pages || !pages.length)
|
|
126
|
-
return h.continue;
|
|
127
|
-
const page = pages.find((_page) => _page.url === req.params.url);
|
|
128
|
-
if (!page)
|
|
129
|
-
return notFound();
|
|
130
|
-
Object.assign(req.plugins.flow.local, {
|
|
131
|
-
[dynamic.assign || "dynamic"]: page.context ?? { a: "" }
|
|
132
|
-
});
|
|
133
|
-
req.app.dynamic = R.omit(["context"], page);
|
|
134
|
-
return h.continue;
|
|
142
|
+
const ctx = createUnimport({
|
|
143
|
+
presets: options.presets,
|
|
144
|
+
imports: options.imports
|
|
135
145
|
});
|
|
136
|
-
|
|
137
|
-
|
|
146
|
+
let composablesDirs = [];
|
|
147
|
+
for (const layer of flow.options._layers) {
|
|
148
|
+
composablesDirs.push(resolve(layer.config.srcDir, "composables"));
|
|
149
|
+
for (const dir of layer.config.autoImports?.dirs ?? [])
|
|
150
|
+
composablesDirs.push(resolve(layer.config.srcDir, dir));
|
|
151
|
+
}
|
|
152
|
+
await flow.callHook("autoImports:dirs", composablesDirs);
|
|
153
|
+
composablesDirs = composablesDirs.map((dir) => normalize(dir));
|
|
154
|
+
addTemplate({
|
|
155
|
+
filename: "imports.mjs",
|
|
156
|
+
getContents: () => ctx.toExports()
|
|
157
|
+
});
|
|
158
|
+
flow.options.alias["#imports"] = join(flow.options.buildDir, "imports");
|
|
159
|
+
if (flow.options.dev && options.global) {
|
|
160
|
+
addPluginTemplate({
|
|
161
|
+
filename: "auto-imports.mjs",
|
|
162
|
+
getContents: () => {
|
|
163
|
+
const imports = ctx.getImports();
|
|
164
|
+
const importStatement = toImports(imports);
|
|
165
|
+
const globalThisSet = imports.map((i) => `globalThis.${i.as} = ${i.as};`).join("\n");
|
|
166
|
+
return `${importStatement}
|
|
138
167
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}).forEach(({ type, host }) => {
|
|
154
|
-
const url = `${protocol}://${host}:${chalk.bold(server.info.port)}`;
|
|
155
|
-
console.log(` > ${type} ${chalk.cyan(url)}`);
|
|
156
|
-
}));
|
|
168
|
+
${globalThisSet}
|
|
169
|
+
|
|
170
|
+
export default () => {};`;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
} else {
|
|
174
|
+
addVitePlugin(TransformPlugin.vite({ ctx, options, sourcemap: flow.options.sourcemap }));
|
|
175
|
+
}
|
|
176
|
+
const regenerateAutoImports = async () => {
|
|
177
|
+
ctx.clearDynamicImports();
|
|
178
|
+
await ctx.modifyDynamicImports(async (imports) => {
|
|
179
|
+
imports.push(...await scanDirExports(composablesDirs));
|
|
180
|
+
await flow.callHook("autoImports:extend", imports);
|
|
181
|
+
});
|
|
157
182
|
};
|
|
158
|
-
|
|
183
|
+
await regenerateAutoImports();
|
|
184
|
+
addDeclarationTemplates(ctx);
|
|
185
|
+
flow.hook("prepare:types", ({ references }) => {
|
|
186
|
+
references.push({ path: resolve(flow.options.buildDir, "types/auto-imports.d.ts") });
|
|
187
|
+
references.push({ path: resolve(flow.options.buildDir, "imports.d.ts") });
|
|
188
|
+
});
|
|
189
|
+
flow.hook("builder:watch", async (_, path) => {
|
|
190
|
+
const _resolved = resolve(flow.options.srcDir, path);
|
|
191
|
+
if (composablesDirs.find((dir) => _resolved.startsWith(dir)))
|
|
192
|
+
await flow.callHook("builder:generateApp");
|
|
193
|
+
});
|
|
194
|
+
flow.hook("builder:generateApp", async () => {
|
|
195
|
+
await regenerateAutoImports();
|
|
196
|
+
});
|
|
159
197
|
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
198
|
+
});
|
|
199
|
+
function addDeclarationTemplates(ctx) {
|
|
200
|
+
const nuxt = useNuxt();
|
|
201
|
+
const stripExtension = (path) => path.replace(/\.[a-z]+$/, "");
|
|
202
|
+
const resolved = {};
|
|
203
|
+
const r = ({ from }) => {
|
|
204
|
+
if (resolved[from])
|
|
205
|
+
return resolved[from];
|
|
206
|
+
let path = resolveAlias(from);
|
|
207
|
+
if (isAbsolute(path))
|
|
208
|
+
path = relative(join(nuxt.options.buildDir, "types"), path);
|
|
209
|
+
path = stripExtension(path);
|
|
210
|
+
resolved[from] = path;
|
|
211
|
+
return path;
|
|
174
212
|
};
|
|
213
|
+
addTemplate({
|
|
214
|
+
filename: "imports.d.ts",
|
|
215
|
+
getContents: () => ctx.toExports()
|
|
216
|
+
});
|
|
217
|
+
addTemplate({
|
|
218
|
+
filename: "types/auto-imports.d.ts",
|
|
219
|
+
getContents: () => `// Generated by auto imports
|
|
220
|
+
${ctx.generateTypeDecarations({ resolvePath: r })}`
|
|
221
|
+
});
|
|
175
222
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
223
|
+
|
|
224
|
+
async function resolvePagesRoutes() {
|
|
225
|
+
const nuxt = useNuxt();
|
|
226
|
+
const pagesDirs = [...new Set(nuxt.options._layers.map((layer) => resolve(layer.config.srcDir, layer.config.dir?.pages || "pages")))];
|
|
227
|
+
const allRoutes = (await Promise.all(pagesDirs.map(async (dir) => {
|
|
228
|
+
const files = await resolveFiles(dir, `**/*{${nuxt.options.extensions.join(",")}}`);
|
|
229
|
+
files.sort();
|
|
230
|
+
return files.map((file) => {
|
|
231
|
+
const segments = relative(dir, file).replace(new RegExp(`${escapeRE(extname(file))}$`), "").split("/").join("_");
|
|
232
|
+
return {
|
|
233
|
+
file,
|
|
234
|
+
name: camelCase(segments)
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
}))).flat();
|
|
238
|
+
return allRoutes;
|
|
179
239
|
}
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
const pages = (locales) => locales.map(({ locale, url, seo }) => {
|
|
183
|
-
const page = getLocalDefaults(locale, options);
|
|
184
|
-
const levelBase = getLevel(locale, levelOptions.level);
|
|
185
|
-
const level = getLevel(locale, options.level);
|
|
186
|
-
const [language, ubication] = locale.split("-");
|
|
187
|
-
const prefixLang = language === defaultLocale.language ? "" : language;
|
|
188
|
-
const prefixUbication = ubication === defaultLocale.ubication ? "" : ubication;
|
|
189
|
-
const prefixLevel = path.posix.join("/", prefixLang, prefixUbication, levelBase, level);
|
|
190
|
-
const publicPath = defaultLocale.publicPath || "/";
|
|
191
|
-
const fullPath = path.posix.join("/", publicPath, prefixLevel, url);
|
|
192
|
-
const redirect = R.last(fullPath) === "/" ? path.posix.join(fullPath, "index.html") : void 0;
|
|
193
|
-
const app = {
|
|
194
|
-
localeName: path.posix.join(prefixName, locale, options.name),
|
|
195
|
-
name: options.name,
|
|
196
|
-
locale,
|
|
197
|
-
language,
|
|
198
|
-
level,
|
|
199
|
-
redirect,
|
|
200
|
-
seo: R.mergeDeepLeft(seo || {}, page.seo),
|
|
201
|
-
blogInfo: page.blogInfo,
|
|
202
|
-
urlObject: {
|
|
203
|
-
publicPath,
|
|
204
|
-
level: prefixLevel,
|
|
205
|
-
url,
|
|
206
|
-
path: fullPath,
|
|
207
|
-
static: R.last(fullPath) === "/" ? `${fullPath}index.html` : `${fullPath}.html`
|
|
208
|
-
},
|
|
209
|
-
view: { ...options.view, ...page.view },
|
|
210
|
-
runSharedContext: options.context,
|
|
211
|
-
runLocalContext: page.runContext
|
|
212
|
-
};
|
|
213
|
-
return app;
|
|
214
|
-
});
|
|
240
|
+
function normalizePages(pages) {
|
|
241
|
+
const imports = pages.map((page) => genImport(page.file, [{ name: "pages", as: page.name }])).join("\n");
|
|
215
242
|
return {
|
|
216
|
-
|
|
217
|
-
pages
|
|
243
|
+
imports,
|
|
244
|
+
exports: pages.reduce((acc, curr) => `${curr.name || ""},${acc}`, "")
|
|
218
245
|
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const pagesTypeTemplate = {
|
|
249
|
+
filename: "pages.d.ts",
|
|
250
|
+
getContents: ({ options }) => `// Generated by pages discovery
|
|
251
|
+
export {}
|
|
252
|
+
declare global {
|
|
253
|
+
|
|
254
|
+
${options.pages.map((c) => `export type ${pascalCase(c.name)}Context=Awaited<ReturnType<typeof ${genDynamicImport(isAbsolute(c.file) ? relative(options.buildDir, c.file) : c.file, { wrapper: false })}['pages']['locales']['es-ec']['context']>>`).join("\n")}
|
|
255
|
+
}
|
|
256
|
+
export const pagesNames: string[]
|
|
257
|
+
`.replaceAll(".ts", "")
|
|
219
258
|
};
|
|
220
259
|
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
"
|
|
235
|
-
"
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
260
|
+
const pagesModule = defineNuxtModule({
|
|
261
|
+
meta: {
|
|
262
|
+
name: "pages"
|
|
263
|
+
},
|
|
264
|
+
async setup(_options, flow) {
|
|
265
|
+
const runtimeDir = resolve(distDir, "pages/runtime");
|
|
266
|
+
const pages = [];
|
|
267
|
+
flow.hook("builder:watch", async (event, path) => {
|
|
268
|
+
const dirs = [
|
|
269
|
+
flow.options.dir.pages,
|
|
270
|
+
flow.options.dir.layouts,
|
|
271
|
+
flow.options.dir.middleware
|
|
272
|
+
].filter(Boolean);
|
|
273
|
+
const pathPattern = new RegExp(`^(${dirs.map(escapeRE).join("|")})/`);
|
|
274
|
+
if (event !== "change" && path.match(pathPattern))
|
|
275
|
+
await flow.callHook("builder:generateApp");
|
|
276
|
+
});
|
|
277
|
+
const options = { pages, buildDir: flow.options.buildDir };
|
|
278
|
+
addTemplate({
|
|
279
|
+
...pagesTypeTemplate,
|
|
280
|
+
options
|
|
281
|
+
});
|
|
282
|
+
flow.options.alias["#pages"] = resolve(flow.options.buildDir, "pages.mjs");
|
|
283
|
+
addTemplate({
|
|
284
|
+
filename: "pages.mjs",
|
|
285
|
+
async getContents({ options: options2 }) {
|
|
286
|
+
const { exports, imports } = normalizePages(options2.pages);
|
|
287
|
+
return [imports, `export default {${exports}}`].join("\n");
|
|
288
|
+
},
|
|
289
|
+
options
|
|
290
|
+
});
|
|
291
|
+
flow.hook("app:templates", async () => {
|
|
292
|
+
options.pages = await resolvePagesRoutes();
|
|
293
|
+
});
|
|
294
|
+
flow.hook("prepare:types", ({ references }) => {
|
|
295
|
+
references.push({ path: resolve(flow.options.buildDir, "pages.d.ts") });
|
|
243
296
|
});
|
|
297
|
+
addPlugin({ src: resolve(runtimeDir, "plugin") });
|
|
244
298
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const createClient = async (flow) => {
|
|
302
|
+
const vite = await createServer({
|
|
303
|
+
root: resolve(flow.options.rootDir),
|
|
304
|
+
base: "/_vite/",
|
|
305
|
+
build: {
|
|
306
|
+
manifest: true
|
|
249
307
|
},
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
req,
|
|
261
|
-
h,
|
|
262
|
-
page,
|
|
263
|
-
extensions: req.plugins.flow.extensions,
|
|
264
|
-
global: req.plugins.flow.global,
|
|
265
|
-
utils
|
|
308
|
+
server: {
|
|
309
|
+
watch: {
|
|
310
|
+
ignored: ["**/.env/**", "**/.env*"]
|
|
311
|
+
},
|
|
312
|
+
middlewareMode: "ssr"
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
const _doReload = () => {
|
|
316
|
+
if (vite)
|
|
317
|
+
vite?.ws?.send({ type: "full-reload" });
|
|
266
318
|
};
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
319
|
+
const doReload = debounce(_doReload, 50);
|
|
320
|
+
flow.hook("bundler:change", () => {
|
|
321
|
+
doReload();
|
|
322
|
+
});
|
|
323
|
+
flow.hook("close", async () => {
|
|
324
|
+
await vite.close();
|
|
325
|
+
});
|
|
326
|
+
return vite;
|
|
327
|
+
};
|
|
328
|
+
const builClient = async (flow) => {
|
|
329
|
+
return await build$1({
|
|
330
|
+
root: flow.options.rootDir,
|
|
331
|
+
mode: "production",
|
|
332
|
+
build: {
|
|
333
|
+
assetsDir: "scripts",
|
|
334
|
+
target: "es2017",
|
|
335
|
+
outDir: ".vite",
|
|
336
|
+
manifest: true
|
|
279
337
|
}
|
|
338
|
+
});
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const viteModule = defineFlowModule({
|
|
342
|
+
meta: {
|
|
343
|
+
name: "viteClient"
|
|
344
|
+
},
|
|
345
|
+
defaults: {
|
|
346
|
+
route: "/_vite/"
|
|
347
|
+
},
|
|
348
|
+
async setup(_options, flow) {
|
|
349
|
+
const runtimeDir = resolve(distDir, "vite-client/runtime");
|
|
350
|
+
flow.options.alias["#viteManifest"] = resolve(flow.options.buildDir, "viteManifest.mjs");
|
|
351
|
+
let vite;
|
|
352
|
+
if (flow.options.dev) {
|
|
353
|
+
flow.hook("nitro:init", async (nitro) => {
|
|
354
|
+
vite = await createClient(flow);
|
|
355
|
+
nitro.options.devHandlers.push({
|
|
356
|
+
handler: vite.middlewares,
|
|
357
|
+
route: _options.route
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
addTemplate({
|
|
361
|
+
filename: "viteManifest.mjs",
|
|
362
|
+
async getContents() {
|
|
363
|
+
return [
|
|
364
|
+
"export default {",
|
|
365
|
+
'head:()=>`<script type="module" src="/_vite/@vite/client"><\/script>`',
|
|
366
|
+
",",
|
|
367
|
+
'body: (bundle)=>`<script type="module" src="/_vite/client/pages/${bundle}.ts"><\/script>`',
|
|
368
|
+
"}"
|
|
369
|
+
].join("\n");
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
} else {
|
|
373
|
+
flow.hook("modules:done", async () => {
|
|
374
|
+
const start = Date.now();
|
|
375
|
+
logger$1.info("Building client...");
|
|
376
|
+
await builClient(flow);
|
|
377
|
+
const file = resolve(flow.options.rootDir, ".vite/manifest.json");
|
|
378
|
+
const manifest = await fse.readFile(file, "utf8");
|
|
379
|
+
logger$1.success(`Client build in ${Date.now() - start}ms`);
|
|
380
|
+
addTemplate({
|
|
381
|
+
filename: "viteManifest.mjs",
|
|
382
|
+
async getContents() {
|
|
383
|
+
return [
|
|
384
|
+
"export default ()=>(",
|
|
385
|
+
manifest,
|
|
386
|
+
")"
|
|
387
|
+
].join("\n");
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
flow.hook("generate:before", async () => {
|
|
392
|
+
const files = resolve(flow.options.rootDir, ".vite/scripts");
|
|
393
|
+
await fse.copy(files, resolve(flow.options.generate.dir, "assets"));
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
addPlugin({ src: resolve(runtimeDir, "plugin") });
|
|
280
397
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
const _require = createRequire(import.meta.url);
|
|
401
|
+
const ImportProtectionPlugin = createUnplugin((options) => {
|
|
402
|
+
const cache = {};
|
|
403
|
+
return {
|
|
404
|
+
name: "flow:import-protection",
|
|
405
|
+
enforce: "pre",
|
|
406
|
+
resolveId(id, importer) {
|
|
407
|
+
const invalidImports = options.patterns.filter(([pattern]) => pattern instanceof RegExp ? pattern.test(id) : pattern === id);
|
|
408
|
+
let matched;
|
|
409
|
+
for (const match of invalidImports) {
|
|
410
|
+
cache[id] = cache[id] || /* @__PURE__ */ new Map();
|
|
411
|
+
const [pattern, warning] = match;
|
|
412
|
+
if (cache[id].has(pattern))
|
|
413
|
+
continue;
|
|
414
|
+
const relativeImporter = isAbsolute(importer) ? relative(options.rootDir, importer) : importer;
|
|
415
|
+
logger.error(warning || "Invalid import", `[importing \`${id}\` from \`${relativeImporter}\`]`);
|
|
416
|
+
cache[id].set(pattern, true);
|
|
417
|
+
matched = true;
|
|
418
|
+
}
|
|
419
|
+
if (matched)
|
|
420
|
+
return _require.resolve("unenv/runtime/mock/proxy");
|
|
421
|
+
return null;
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
async function initNitro(flow) {
|
|
427
|
+
const { handlers, devHandlers } = await resolveHandlers(flow);
|
|
428
|
+
const _nitroConfig = flow.options.nitro || {};
|
|
429
|
+
globalThis.generate = {};
|
|
430
|
+
const nitroConfig = defu(_nitroConfig, {
|
|
431
|
+
rootDir: flow.options.rootDir,
|
|
432
|
+
srcDir: join(flow.options.srcDir, "server"),
|
|
433
|
+
dev: flow.options.dev,
|
|
434
|
+
preset: flow.options.dev ? "nitro-dev" : void 0,
|
|
435
|
+
buildDir: flow.options.buildDir,
|
|
436
|
+
scanDirs: flow.options._layers.map((layer) => join(layer.config.srcDir, "server")),
|
|
437
|
+
renderer: resolve(distDir, "core/runtime/nitro/renderer"),
|
|
438
|
+
nodeModulesDirs: flow.options.modulesDir,
|
|
439
|
+
handlers,
|
|
440
|
+
devHandlers: [],
|
|
441
|
+
baseURL: flow.options.app.baseURL,
|
|
442
|
+
runtimeConfig: {
|
|
443
|
+
...flow.options.runtimeConfig,
|
|
444
|
+
app: {
|
|
445
|
+
...flow.options.runtimeConfig.app,
|
|
446
|
+
rootDir: flow.options.rootDir,
|
|
447
|
+
locale: flow.options.locale
|
|
448
|
+
},
|
|
449
|
+
nitro: {
|
|
450
|
+
envPrefix: "FLOW_",
|
|
451
|
+
...flow.options.runtimeConfig.nitro
|
|
452
|
+
},
|
|
453
|
+
generate: flow.options._generate
|
|
454
|
+
},
|
|
455
|
+
typescript: {
|
|
456
|
+
generateTsConfig: false
|
|
457
|
+
},
|
|
458
|
+
publicAssets: [
|
|
459
|
+
{
|
|
460
|
+
baseURL: flow.options.app.buildAssetsDir,
|
|
461
|
+
dir: resolve(flow.options.buildDir, "dist/client")
|
|
462
|
+
},
|
|
463
|
+
...flow.options._layers.map((layer) => join(layer.config.srcDir, layer.config.dir?.public || "public")).filter((dir) => existsSync(dir)).map((dir) => ({ dir }))
|
|
464
|
+
],
|
|
465
|
+
prerender: {
|
|
466
|
+
crawlLinks: flow.options._generate ? flow.options.generate.crawler : false,
|
|
467
|
+
routes: [].concat(flow.options._generate ? ["/_urls", ...flow.options.generate.routes] : [])
|
|
468
|
+
},
|
|
469
|
+
sourcemap: flow.options.sourcemap,
|
|
470
|
+
externals: {
|
|
471
|
+
inline: [
|
|
472
|
+
...flow.options.dev ? [] : ["eta", "@monkeyplus/", "@vue/", "@nuxt/", flow.options.buildDir],
|
|
473
|
+
"@monkeyplus/flow/dist"
|
|
474
|
+
]
|
|
475
|
+
},
|
|
476
|
+
alias: {
|
|
477
|
+
"estree-walker": "unenv/runtime/mock/proxy",
|
|
478
|
+
"@babel/parser": "unenv/runtime/mock/proxy",
|
|
479
|
+
"#paths": resolve(distDir, "core/runtime/nitro/paths"),
|
|
480
|
+
"#server": "#build/dist/server/server.mjs",
|
|
481
|
+
"#app": flow.options.appDir,
|
|
482
|
+
...flow.options.alias
|
|
483
|
+
},
|
|
484
|
+
rollupConfig: {
|
|
485
|
+
plugins: []
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
await flow.callHook("nitro:config", nitroConfig);
|
|
489
|
+
nitroConfig.handlers.unshift({
|
|
490
|
+
middleware: true,
|
|
491
|
+
handler: resolve(distDir, "core/runtime/nitro/flow")
|
|
492
|
+
});
|
|
493
|
+
const nitro = await createNitro(nitroConfig);
|
|
494
|
+
await flow.callHook("nitro:init", nitro);
|
|
495
|
+
nitro.vfs = flow.vfs = nitro.vfs || flow.vfs || {};
|
|
496
|
+
flow.hook("close", () => nitro.hooks.callHook("close"));
|
|
497
|
+
nitro.hooks.hook("rollup:before", (nitro2) => {
|
|
498
|
+
const plugin = ImportProtectionPlugin.rollup({
|
|
499
|
+
rootDir: flow.options.rootDir,
|
|
500
|
+
patterns: [
|
|
501
|
+
...["#app", /^#build(\/|$)/].map((p) => [p, "Flow app aliases are not allowed in server routes."])
|
|
502
|
+
]
|
|
503
|
+
});
|
|
504
|
+
nitro2.options.rollupConfig.plugins.push(plugin);
|
|
505
|
+
});
|
|
506
|
+
const devMidlewareHandler = dynamicEventHandler();
|
|
507
|
+
nitro.options.devHandlers.unshift({ handler: devMidlewareHandler });
|
|
508
|
+
nitro.options.devHandlers.push(...devHandlers);
|
|
509
|
+
nitro.options.handlers.unshift({
|
|
510
|
+
route: "/__flow_error",
|
|
511
|
+
lazy: true,
|
|
512
|
+
handler: resolve(distDir, "core/runtime/nitro/renderer")
|
|
513
|
+
});
|
|
514
|
+
flow.hook("prepare:types", async (opts) => {
|
|
515
|
+
if (flow.options._prepare) {
|
|
516
|
+
await scanHandlers(nitro);
|
|
517
|
+
await writeTypes(nitro);
|
|
518
|
+
}
|
|
519
|
+
opts.references.push({ path: resolve(flow.options.buildDir, "types/nitro.d.ts") });
|
|
520
|
+
});
|
|
521
|
+
flow.hook("build:done", async () => {
|
|
522
|
+
await flow.callHook("nitro:build:before", nitro);
|
|
523
|
+
if (flow.options.dev) {
|
|
524
|
+
await build$2(nitro);
|
|
525
|
+
} else {
|
|
526
|
+
await prepare(nitro);
|
|
527
|
+
await copyPublicAssets(nitro);
|
|
528
|
+
await prerender(nitro);
|
|
529
|
+
if (!flow.options._generate) {
|
|
530
|
+
await build$2(nitro);
|
|
531
|
+
} else {
|
|
532
|
+
const nitroDev = await createNitro({
|
|
533
|
+
...nitro.options._config,
|
|
534
|
+
rootDir: nitro.options.rootDir,
|
|
535
|
+
logLevel: 0,
|
|
536
|
+
preset: "nitro-prerender"
|
|
537
|
+
});
|
|
538
|
+
flow.server = nitroDev;
|
|
539
|
+
const distDir2 = resolve(flow.options.rootDir, "dist");
|
|
540
|
+
if (!existsSync(distDir2))
|
|
541
|
+
await promises.symlink(nitro.options.output.publicDir, distDir2, "junction").catch(() => {
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
if (flow.options.dev) {
|
|
547
|
+
flow.hook("build:compile", ({ compiler }) => {
|
|
548
|
+
compiler.outputFileSystem = { ...fse, join };
|
|
549
|
+
});
|
|
550
|
+
flow.hook("server:devMiddleware", (m) => {
|
|
551
|
+
devMidlewareHandler.set(toEventHandler(m));
|
|
552
|
+
});
|
|
553
|
+
flow.server = createDevServer(nitro);
|
|
554
|
+
flow.hook("build:resources", () => {
|
|
555
|
+
flow.server.reload();
|
|
556
|
+
});
|
|
557
|
+
const waitUntilCompile = new Promise((resolve2) => nitro.hooks.hook("compiled", () => resolve2()));
|
|
558
|
+
flow.hook("build:done", () => waitUntilCompile);
|
|
288
559
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
global: Object.freeze({ ...global, ...req.plugins.flow.global })
|
|
560
|
+
}
|
|
561
|
+
async function resolveHandlers(flow) {
|
|
562
|
+
const handlers = [...flow.options.serverHandlers];
|
|
563
|
+
const devHandlers = [...flow.options.devServerHandlers];
|
|
564
|
+
return {
|
|
565
|
+
handlers,
|
|
566
|
+
devHandlers
|
|
297
567
|
};
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const addModuleTranspiles = (opts = {}) => {
|
|
571
|
+
const flow = useNuxt();
|
|
572
|
+
const modules = [
|
|
573
|
+
...opts.additionalModules || [],
|
|
574
|
+
...flow.options.modules,
|
|
575
|
+
...flow.options._modules
|
|
576
|
+
].map((m) => typeof m === "string" ? m : Array.isArray(m) ? m[0] : m.src).filter((m) => typeof m === "string").map((m) => m.split("node_modules/").pop());
|
|
577
|
+
flow.options.build.transpile = flow.options.build.transpile.map((m) => typeof m === "string" ? m.split("node_modules/").pop() : m);
|
|
578
|
+
function isTranspilePresent(mod) {
|
|
579
|
+
return flow.options.build.transpile.some((t) => !(t instanceof Function) && (t instanceof RegExp ? t.test(mod) : new RegExp(t).test(mod)));
|
|
580
|
+
}
|
|
581
|
+
for (const module of modules) {
|
|
582
|
+
if (!isTranspilePresent(module))
|
|
583
|
+
flow.options.build.transpile.push(module);
|
|
302
584
|
}
|
|
303
|
-
context.utils = utils;
|
|
304
|
-
return h.view(`${configs.dirTemplates}${flow.view.layout || "default"}`, context);
|
|
305
585
|
};
|
|
306
586
|
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
outputDir: "build",
|
|
321
|
-
url: process.env.CUSTOM_URL || process.env.URL,
|
|
322
|
-
engines: ["eta"],
|
|
323
|
-
plugins: {}
|
|
324
|
-
};
|
|
325
|
-
function getConfigs(server, options) {
|
|
326
|
-
const relativeTo = path.resolve(options.relativeTo);
|
|
327
|
-
const getObject = (object) => {
|
|
328
|
-
const config = applyToDefaults(defaults, { ...object, ...options });
|
|
329
|
-
return {
|
|
330
|
-
...config,
|
|
331
|
-
relativeTo,
|
|
332
|
-
locale: `${config.defaultLanguage}-${config.defaultUbication}`
|
|
333
|
-
};
|
|
587
|
+
function createFlow(options) {
|
|
588
|
+
const hooks = createHooks();
|
|
589
|
+
const flow = {
|
|
590
|
+
_version: "3.0.0-rc.3",
|
|
591
|
+
version,
|
|
592
|
+
options,
|
|
593
|
+
hooks,
|
|
594
|
+
callHook: hooks.callHook,
|
|
595
|
+
addHooks: hooks.addHooks,
|
|
596
|
+
hook: hooks.hook,
|
|
597
|
+
ready: () => initFlow(flow),
|
|
598
|
+
close: () => Promise.resolve(hooks.callHook("close", flow)),
|
|
599
|
+
vfs: {}
|
|
334
600
|
};
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
601
|
+
return flow;
|
|
602
|
+
}
|
|
603
|
+
async function initFlow(flow) {
|
|
604
|
+
nuxtCtx.set(flow);
|
|
605
|
+
flow.hook("close", () => nuxtCtx.unset());
|
|
606
|
+
await flow.callHook("modules:before", { nuxt: flow });
|
|
607
|
+
const modulesToInstall = [
|
|
608
|
+
...flow.options.modules,
|
|
609
|
+
...flow.options._modules
|
|
610
|
+
];
|
|
611
|
+
for (const m of modulesToInstall) {
|
|
612
|
+
if (Array.isArray(m))
|
|
613
|
+
await installModule(m[0], m[1]);
|
|
614
|
+
else
|
|
615
|
+
await installModule(m, {});
|
|
344
616
|
}
|
|
617
|
+
await flow.callHook("modules:done", { nuxt: flow });
|
|
618
|
+
await addModuleTranspiles();
|
|
619
|
+
await initNitro(flow);
|
|
620
|
+
await flow.callHook("ready", flow);
|
|
621
|
+
}
|
|
622
|
+
async function loadFlow(opts) {
|
|
623
|
+
const options = await loadFlowConfig(opts);
|
|
624
|
+
options.appDir = resolve(distDir, "app");
|
|
625
|
+
options._majorVersion = 3;
|
|
626
|
+
options._modules.push(pagesModule, metaModule, autoImportsModule, viteModule);
|
|
627
|
+
options.modulesDir.push(resolve(pkgDir, "node_modules"));
|
|
628
|
+
const flow = createFlow(options);
|
|
629
|
+
if (opts.ready !== false)
|
|
630
|
+
await flow.ready();
|
|
631
|
+
return flow;
|
|
632
|
+
}
|
|
633
|
+
function defineFlowConfig(config) {
|
|
634
|
+
return config;
|
|
345
635
|
}
|
|
346
|
-
const isDinamyc = (url) => /\{url?(.+)\}/.test(url);
|
|
347
636
|
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
url: route.path,
|
|
359
|
-
locale: pageInfo.locale,
|
|
360
|
-
localeName: pageInfo.localeName,
|
|
361
|
-
name: pageInfo.name
|
|
362
|
-
});
|
|
363
|
-
if (pageInfo.redirect)
|
|
364
|
-
helpers.addRedirect(pageInfo.redirect, route.path);
|
|
365
|
-
};
|
|
366
|
-
if (flow) {
|
|
367
|
-
runCommons(flow);
|
|
368
|
-
} else {
|
|
369
|
-
const routes = definePages({
|
|
370
|
-
name: options.name,
|
|
371
|
-
view: options.view,
|
|
372
|
-
locales: {
|
|
373
|
-
[options.locale || configs.locale]: {}
|
|
374
|
-
},
|
|
375
|
-
context: options.context
|
|
376
|
-
})({
|
|
377
|
-
language: configs.defaultLanguage,
|
|
378
|
-
ubication: configs.defaultUbication,
|
|
379
|
-
publicPath: configs.publicPath
|
|
380
|
-
})().pages([
|
|
381
|
-
{ locale: options.locale || configs.locale, url: route.path }
|
|
382
|
-
]);
|
|
383
|
-
const pageInfo = routes[0];
|
|
384
|
-
runCommons(pageInfo);
|
|
385
|
-
Object.assign(app, { flow: pageInfo, content: options.content });
|
|
386
|
-
}
|
|
387
|
-
const extensions = plugins?.flow?.extensions || {};
|
|
388
|
-
if (isDinamyc(route.path)) {
|
|
389
|
-
if (!route.settings.plugins?.flow?.dynamic)
|
|
390
|
-
throw new Error("Dynamic pages require a method");
|
|
391
|
-
helpers.addDynamicPages(flow.localeName, ((options2) => async () => {
|
|
392
|
-
const _pages = await route.settings.plugins?.flow?.dynamic?.pages(options2);
|
|
393
|
-
const obj = {};
|
|
394
|
-
for (const sPage of _pages || []) {
|
|
395
|
-
const localeName = `${app?.flow.localeName}/${sPage.name}`;
|
|
396
|
-
obj[localeName] = {
|
|
397
|
-
locale: app?.flow.locale,
|
|
398
|
-
localeName,
|
|
399
|
-
name: localeName.replace(`${app?.flow.locale}/`, ""),
|
|
400
|
-
url: route.path.replace("{url*}", sPage.url),
|
|
401
|
-
context: sPage.context,
|
|
402
|
-
dynamicSlug: sPage.dynamicSlug
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
return obj;
|
|
406
|
-
})({
|
|
407
|
-
server,
|
|
408
|
-
route,
|
|
409
|
-
locale: app?.flow.locale
|
|
410
|
-
}));
|
|
411
|
-
}
|
|
412
|
-
for (const extension in extensions) {
|
|
413
|
-
const optionExtension = plugins?.flow?.extensions[extension];
|
|
414
|
-
const routeMethod = state.extensions[extension]?.routeMethod;
|
|
415
|
-
if (routeMethod)
|
|
416
|
-
routeMethod({ pageInfo: app.flow, route }, optionExtension);
|
|
417
|
-
}
|
|
418
|
-
return handlerPage;
|
|
419
|
-
};
|
|
420
|
-
const getUrl = (_locale, _name) => {
|
|
421
|
-
try {
|
|
422
|
-
const pageName = path.posix.join(_locale, _name);
|
|
423
|
-
const page = state.pages.all[pageName];
|
|
424
|
-
if (page) {
|
|
425
|
-
return page.url;
|
|
426
|
-
} else {
|
|
427
|
-
logger$1.warn("Not found link with name: %s", pageName);
|
|
428
|
-
return "/404";
|
|
429
|
-
}
|
|
430
|
-
} catch (error) {
|
|
431
|
-
logger$1.error(error);
|
|
432
|
-
return "/";
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
server.method("flow.getUrl", getUrl, {});
|
|
436
|
-
server.decorate("handler", "flow", flowDecorate);
|
|
437
|
-
server.decorate("toolkit", "getUrl", server.methods.flow.getUrl);
|
|
637
|
+
const serverPluginTemplate = {
|
|
638
|
+
filename: "plugins/server.mjs",
|
|
639
|
+
getContents(ctx) {
|
|
640
|
+
const serverPlugins = ctx.app.plugins;
|
|
641
|
+
return [
|
|
642
|
+
templateUtils.importSources(serverPlugins.map((p) => p.src)),
|
|
643
|
+
`export default ${genArrayFromRaw([
|
|
644
|
+
...serverPlugins.map((p) => templateUtils.importName(p.src))
|
|
645
|
+
])}`
|
|
646
|
+
].join("\n");
|
|
438
647
|
}
|
|
439
648
|
};
|
|
649
|
+
const pluginsDeclaration = {
|
|
650
|
+
filename: "types/plugins.d.ts",
|
|
651
|
+
getContents: (ctx) => {
|
|
652
|
+
const EXTENSION_RE = new RegExp(`(?<=\\w)(${ctx.nuxt.options.extensions.map((e) => escapeRE(e)).join("|")})$`, "g");
|
|
653
|
+
const tsImports = ctx.app.plugins.map((p) => (isAbsolute(p.src) ? relative(join(ctx.nuxt.options.buildDir, "types"), p.src) : p.src).replace(EXTENSION_RE, ""));
|
|
654
|
+
return `// Generated by Flow'
|
|
655
|
+
import type { Plugin } from '#app'
|
|
440
656
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
if (!files[route.path])
|
|
457
|
-
files[route.path] = generate;
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
return files;
|
|
461
|
-
};
|
|
462
|
-
const writeFile = async (file, payload) => {
|
|
463
|
-
await fs.ensureFile(file);
|
|
464
|
-
await fs.writeFile(file, payload, "utf8");
|
|
465
|
-
};
|
|
466
|
-
const useCreateFile = (isVirtual = false) => async (dir, url, ext) => {
|
|
467
|
-
if (typeof ext === "object") {
|
|
468
|
-
if (!isVirtual) {
|
|
469
|
-
const dirFile = path.dirname(url);
|
|
470
|
-
await fs.ensureDir(path.join(dir, dirFile));
|
|
471
|
-
const file = await fs.readFile(ext.file);
|
|
472
|
-
await fs.writeFile(path.join(dir, url), file);
|
|
473
|
-
}
|
|
474
|
-
return { type: ext.type || "assets", url };
|
|
475
|
-
} else {
|
|
476
|
-
const r = await server.inject({
|
|
477
|
-
url,
|
|
478
|
-
method: "get"
|
|
479
|
-
});
|
|
480
|
-
let file = "";
|
|
481
|
-
if (typeof ext === "boolean") {
|
|
482
|
-
if (!isVirtual) {
|
|
483
|
-
file = path.join(dir, url);
|
|
484
|
-
await writeFile(file, r.payload);
|
|
485
|
-
}
|
|
486
|
-
return { type: "assets", url };
|
|
487
|
-
} else if (typeof ext === "string") {
|
|
488
|
-
if (ext === ".html") {
|
|
489
|
-
if (!isVirtual) {
|
|
490
|
-
const isIndex = R.last(url) === "/";
|
|
491
|
-
const nameFile = isIndex ? `${url}index.html` : `${url}.html`;
|
|
492
|
-
file = path.join(dir, nameFile);
|
|
493
|
-
await writeFile(file, r.payload);
|
|
494
|
-
}
|
|
495
|
-
return { type: "page", url };
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
return { type: "asset", url };
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
const injectAssets = (assets) => {
|
|
502
|
-
for (const asset of assets) {
|
|
503
|
-
const dirs = asset.dirs;
|
|
504
|
-
for (const dir of dirs) {
|
|
505
|
-
const dirFiles = fs.readdirSync(path.join(asset.relativeTo, dir));
|
|
506
|
-
for (const file of dirFiles) {
|
|
507
|
-
const pathFile = path.join(asset.relativeTo, dir, file);
|
|
508
|
-
const ext = path.extname(pathFile);
|
|
509
|
-
if (!!ext) {
|
|
510
|
-
const pathRoute = path.posix.join("/", asset.prefix, dir, file);
|
|
511
|
-
const pathRouteOverride = asset.override?.[pathRoute];
|
|
512
|
-
server.route({
|
|
513
|
-
path: pathRouteOverride || pathRoute,
|
|
514
|
-
method: "get",
|
|
515
|
-
options: {
|
|
516
|
-
plugins: {
|
|
517
|
-
generate: {
|
|
518
|
-
isAsset: true,
|
|
519
|
-
file: pathFile,
|
|
520
|
-
type: asset.type,
|
|
521
|
-
isRemane: !!pathRouteOverride
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
},
|
|
525
|
-
handler: {
|
|
526
|
-
file: {
|
|
527
|
-
path: pathFile,
|
|
528
|
-
confine: false
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
});
|
|
532
|
-
} else {
|
|
533
|
-
injectAssets([
|
|
534
|
-
{
|
|
535
|
-
dirs: [path.posix.join(dir, file)],
|
|
536
|
-
prefix: asset.prefix,
|
|
537
|
-
relativeTo: asset.relativeTo,
|
|
538
|
-
override: asset.override,
|
|
539
|
-
type: asset.type || file
|
|
540
|
-
}
|
|
541
|
-
]);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
|
-
const runGenerate = async ({
|
|
548
|
-
assets = [],
|
|
549
|
-
ommitAssets = [],
|
|
550
|
-
virtualGenerate = false
|
|
551
|
-
}) => {
|
|
552
|
-
await server.methods.flow.pages();
|
|
553
|
-
const _assetsApp = [...Object.values(server.app.flow.generate.folders)];
|
|
554
|
-
const _assets = [...assets, ..._assetsApp];
|
|
555
|
-
if (!virtualGenerate)
|
|
556
|
-
injectAssets(_assets);
|
|
557
|
-
const dirOutut = helpers.getPath(configs.outputDir ?? "build");
|
|
558
|
-
const staticDirs = Object.keys(server.app.flow.generate.staticFolders).map((dir) => helpers.getPath(dir));
|
|
559
|
-
const files = await getFlowPages();
|
|
560
|
-
const pairFiles = R.toPairs(files);
|
|
561
|
-
const ommit = ommitAssets;
|
|
562
|
-
try {
|
|
563
|
-
await fs.remove(dirOutut);
|
|
564
|
-
for (const dirStatic of staticDirs)
|
|
565
|
-
await fs.copy(dirStatic, dirOutut);
|
|
566
|
-
for (const omitFile of ommit)
|
|
567
|
-
await fs.remove(path.join(dirOutut, omitFile));
|
|
568
|
-
const createFile = useCreateFile(virtualGenerate);
|
|
569
|
-
const files2 = await Promise.all(pairFiles.map(async (v) => {
|
|
570
|
-
return await createFile(dirOutut, ...v);
|
|
571
|
-
}));
|
|
572
|
-
const groups = R.groupBy((file) => {
|
|
573
|
-
if (file.url.includes("images/"))
|
|
574
|
-
return "image";
|
|
575
|
-
else
|
|
576
|
-
return (path.extname(file.url) || file.type).toLowerCase();
|
|
577
|
-
}, files2);
|
|
578
|
-
const _groups = {
|
|
579
|
-
pages: 0,
|
|
580
|
-
js: 0,
|
|
581
|
-
css: 0,
|
|
582
|
-
images: 0,
|
|
583
|
-
fonts: 0,
|
|
584
|
-
others: 0
|
|
585
|
-
};
|
|
586
|
-
for (const type in groups) {
|
|
587
|
-
const cp = (ext) => ext === type;
|
|
588
|
-
const qty = groups[type].length;
|
|
589
|
-
if (type === "page")
|
|
590
|
-
_groups.pages += qty;
|
|
591
|
-
else if (type === ".js")
|
|
592
|
-
_groups.js += qty;
|
|
593
|
-
else if (type === ".css")
|
|
594
|
-
_groups.css += qty;
|
|
595
|
-
else if ([".gif", ".png", ".jpg", ".jpeg", "image"].find(cp))
|
|
596
|
-
_groups.images += qty;
|
|
597
|
-
else if ([".woff", ".eot", ".ttf"].find(cp))
|
|
598
|
-
_groups.fonts += qty;
|
|
599
|
-
else
|
|
600
|
-
_groups.others += qty;
|
|
601
|
-
}
|
|
602
|
-
for (const type in _groups)
|
|
603
|
-
logger.success("%i %s generated", _groups[type], type);
|
|
604
|
-
const bf = server.app.flow.generate.postGenerate;
|
|
605
|
-
if (Object.keys(bf).length) {
|
|
606
|
-
logger.info("Post generate");
|
|
607
|
-
}
|
|
608
|
-
for (const key in bf)
|
|
609
|
-
await bf[key]();
|
|
610
|
-
} catch (error) {
|
|
611
|
-
logger.error(error);
|
|
612
|
-
await fs.remove(dirOutut);
|
|
613
|
-
throw new Error(error);
|
|
614
|
-
}
|
|
615
|
-
};
|
|
616
|
-
return { runGenerate };
|
|
657
|
+
type Decorate<T extends Record<string, any>> = { [K in keyof T as K extends string ? \`$\${K}\` : never]: T[K] }
|
|
658
|
+
|
|
659
|
+
type InjectionType<A extends Plugin> = A extends Plugin<infer T> ? Decorate<T> : unknown
|
|
660
|
+
|
|
661
|
+
type FlowAppInjections =
|
|
662
|
+
${tsImports.map((p) => `InjectionType<typeof ${genDynamicImport(p, { wrapper: false })}.default>`).join(" &\n ")}
|
|
663
|
+
|
|
664
|
+
declare module '#app' {
|
|
665
|
+
interface FlowApp extends FlowAppInjections { }
|
|
666
|
+
}
|
|
667
|
+
// TODO: Insert extend types
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
export { }
|
|
671
|
+
`;
|
|
617
672
|
}
|
|
618
673
|
};
|
|
619
|
-
|
|
620
|
-
const
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
}
|
|
674
|
+
const adHocModules = ["auto-imports", "meta", "pages", "vite-client"];
|
|
675
|
+
const schemaTemplate = {
|
|
676
|
+
filename: "types/schema.d.ts",
|
|
677
|
+
getContents: ({ nuxt }) => {
|
|
678
|
+
const moduleInfo = nuxt.options._installedModules.map((m) => ({
|
|
679
|
+
...m.meta || {},
|
|
680
|
+
importName: m.entryPath || m.meta?.name
|
|
681
|
+
})).filter((m) => m.configKey && m.name && !adHocModules.includes(m.name));
|
|
682
|
+
return [
|
|
683
|
+
"import { FlowModule } from '@monkeyplus/flow-schema'",
|
|
684
|
+
"declare module '@monkeyplus/flow-schema' {",
|
|
685
|
+
" interface FlowConfig {",
|
|
686
|
+
...moduleInfo.filter(Boolean).map((meta) => ` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends FlowModule<infer O> ? Partial<O> : Record<string, any>`),
|
|
687
|
+
" }",
|
|
688
|
+
generateTypes(resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== "public"))), {
|
|
689
|
+
interfaceName: "RuntimeConfig",
|
|
690
|
+
addExport: false,
|
|
691
|
+
addDefaults: false,
|
|
692
|
+
allowExtraKeys: false,
|
|
693
|
+
indentation: 2
|
|
694
|
+
}),
|
|
695
|
+
generateTypes(resolveSchema(nuxt.options.runtimeConfig.public), {
|
|
696
|
+
interfaceName: "PublicRuntimeConfig",
|
|
697
|
+
addExport: false,
|
|
698
|
+
addDefaults: false,
|
|
699
|
+
allowExtraKeys: false,
|
|
700
|
+
indentation: 2
|
|
701
|
+
}),
|
|
702
|
+
"}"
|
|
703
|
+
].join("\n");
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
const publicPathTemplate = {
|
|
707
|
+
filename: "paths.mjs",
|
|
708
|
+
getContents({ nuxt }) {
|
|
709
|
+
return [
|
|
710
|
+
"import { joinURL } from 'ufo'",
|
|
711
|
+
!nuxt.options.dev && "import { useRuntimeConfig } from '#internal/nitro'",
|
|
712
|
+
nuxt.options.dev ? `const appConfig = ${JSON.stringify(nuxt.options.app)}` : "const appConfig = useRuntimeConfig().app",
|
|
713
|
+
"export const baseURL = () => appConfig.baseURL",
|
|
714
|
+
"export const buildAssetsDir = () => appConfig.buildAssetsDir",
|
|
715
|
+
"export const buildAssetsURL = (...path) => joinURL(publicAssetsURL(), buildAssetsDir(), ...path)",
|
|
716
|
+
"export const publicAssetsURL = (...path) => {",
|
|
717
|
+
" const publicBase = appConfig.cdnURL || appConfig.baseURL",
|
|
718
|
+
" return path.length ? joinURL(publicBase, ...path) : publicBase",
|
|
719
|
+
"}"
|
|
720
|
+
].filter(Boolean).join("\n");
|
|
627
721
|
}
|
|
628
|
-
const infoPages = definePages({
|
|
629
|
-
name: pageOptions.name,
|
|
630
|
-
view: pageOptions.view,
|
|
631
|
-
context: pageOptions.context,
|
|
632
|
-
level: pageOptions.level,
|
|
633
|
-
locales: pageOptions.locales
|
|
634
|
-
})({
|
|
635
|
-
language: configs.defaultLanguage,
|
|
636
|
-
ubication: configs.defaultUbication,
|
|
637
|
-
publicPath: configs.publicPath
|
|
638
|
-
})(levelOptions).pages(pages);
|
|
639
|
-
const {
|
|
640
|
-
options: sharedOptions,
|
|
641
|
-
rules: sharedRules,
|
|
642
|
-
vhost: sharedVHost
|
|
643
|
-
} = pageOptions;
|
|
644
|
-
const sharedPre = sharedOptions?.pre || [];
|
|
645
|
-
const routes = infoPages.map((page) => {
|
|
646
|
-
const localPage = pageOptions.locales[page.locale];
|
|
647
|
-
const route = {
|
|
648
|
-
path: page.urlObject.path,
|
|
649
|
-
method: "get",
|
|
650
|
-
options: {
|
|
651
|
-
app: {
|
|
652
|
-
flow: page
|
|
653
|
-
},
|
|
654
|
-
plugins: {
|
|
655
|
-
flow: {
|
|
656
|
-
extensions: pageOptions.extensions || {},
|
|
657
|
-
dynamic: localPage.dynamic
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
},
|
|
661
|
-
handler: {
|
|
662
|
-
flow: {
|
|
663
|
-
name: page.localeName,
|
|
664
|
-
locale: page.locale,
|
|
665
|
-
view: page.view
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
};
|
|
669
|
-
if (sharedRules || localPage.rules)
|
|
670
|
-
route.rules = localPage.rules || sharedRules;
|
|
671
|
-
if (sharedVHost || localPage.vhost)
|
|
672
|
-
route.vhost = localPage.vhost || sharedVHost;
|
|
673
|
-
if (sharedOptions || localPage.options) {
|
|
674
|
-
const localOptions = localPage?.options || {};
|
|
675
|
-
const localPre = localPage?.options?.pre || [];
|
|
676
|
-
const pre = [...sharedPre, ...localPre];
|
|
677
|
-
const options = R.mergeDeepRight(sharedOptions || {}, localOptions);
|
|
678
|
-
route.options = R.mergeDeepRight(options, route.options);
|
|
679
|
-
if (route.options && pre.length)
|
|
680
|
-
route.options.pre = pre;
|
|
681
|
-
}
|
|
682
|
-
return route;
|
|
683
|
-
});
|
|
684
|
-
return routes;
|
|
685
722
|
};
|
|
686
723
|
|
|
687
|
-
const
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
724
|
+
const defaultTemplates = {
|
|
725
|
+
__proto__: null,
|
|
726
|
+
serverPluginTemplate: serverPluginTemplate,
|
|
727
|
+
pluginsDeclaration: pluginsDeclaration,
|
|
728
|
+
schemaTemplate: schemaTemplate,
|
|
729
|
+
publicPathTemplate: publicPathTemplate
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
function createApp(flow, options = {}) {
|
|
733
|
+
return defu(options, {
|
|
734
|
+
dir: flow.options.srcDir,
|
|
735
|
+
extensions: flow.options.extensions,
|
|
736
|
+
plugins: [],
|
|
737
|
+
templates: []
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
async function generateApp(flow, app) {
|
|
741
|
+
await resolveApp(flow, app);
|
|
742
|
+
app.templates = Object.values(defaultTemplates).concat(flow.options.build.templates);
|
|
743
|
+
await flow.callHook("app:templates", app);
|
|
744
|
+
app.templates = app.templates.map((tmpl) => normalizeTemplate(tmpl));
|
|
745
|
+
const templateContext = { utils: templateUtils, nuxt: flow, app };
|
|
746
|
+
await Promise.all(app.templates.map(async (template) => {
|
|
747
|
+
const contents = await compileTemplate(template, templateContext);
|
|
748
|
+
const fullPath = template.dst || resolve(flow.options.buildDir, template.filename);
|
|
749
|
+
flow.vfs[fullPath] = contents;
|
|
750
|
+
const aliasPath = `#build/${template.filename.replace(/\.\w+$/, "")}`;
|
|
751
|
+
flow.vfs[aliasPath] = contents;
|
|
752
|
+
if (process.platform === "win32")
|
|
753
|
+
flow.vfs[fullPath.replace(/\//g, "\\")] = contents;
|
|
754
|
+
if (template.write) {
|
|
755
|
+
await promises.mkdir(dirname(fullPath), { recursive: true });
|
|
756
|
+
await promises.writeFile(fullPath, contents, "utf8");
|
|
697
757
|
}
|
|
698
758
|
}));
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
const
|
|
704
|
-
|
|
759
|
+
await flow.callHook("app:templatesGenerated", app);
|
|
760
|
+
}
|
|
761
|
+
async function resolveApp(flow, app) {
|
|
762
|
+
app.plugins = [...flow.options.plugins.map(normalizePlugin)];
|
|
763
|
+
for (const config of flow.options._layers.map((layer) => layer.config)) {
|
|
764
|
+
app.plugins.push(...[
|
|
765
|
+
...config.plugins || [],
|
|
766
|
+
...await resolveFilesFlow(config.srcDir, [
|
|
767
|
+
"plugins/*.{ts,js,mjs,cjs,mts,cts}",
|
|
768
|
+
"plugins/*/index.*{ts,js,mjs,cjs,mts,cts}"
|
|
769
|
+
])
|
|
770
|
+
].map((plugin) => normalizePlugin(plugin)));
|
|
771
|
+
}
|
|
772
|
+
app.plugins = uniqueBy(app.plugins, "src");
|
|
773
|
+
await flow.callHook("app:resolve", app);
|
|
774
|
+
}
|
|
775
|
+
function uniqueBy(arr, key) {
|
|
776
|
+
const res = [];
|
|
777
|
+
const seen = /* @__PURE__ */ new Set();
|
|
778
|
+
for (const item of arr) {
|
|
779
|
+
if (seen.has(item[key]))
|
|
780
|
+
continue;
|
|
781
|
+
seen.add(item[key]);
|
|
782
|
+
res.push(item);
|
|
783
|
+
}
|
|
784
|
+
return res;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
async function warmupViteServer(server, entries) {
|
|
788
|
+
const warmedUrls = /* @__PURE__ */ new Set();
|
|
789
|
+
const warmup = async (url) => {
|
|
790
|
+
if (warmedUrls.has(url))
|
|
791
|
+
return;
|
|
792
|
+
warmedUrls.add(url);
|
|
705
793
|
try {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
} else if (typeof module === "object") {
|
|
710
|
-
if (Array.isArray(module)) {
|
|
711
|
-
module.forEach((el) => listPrePages.push(el()));
|
|
712
|
-
} else {
|
|
713
|
-
const _pages = await module.pages({ server });
|
|
714
|
-
if (Array.isArray(_pages))
|
|
715
|
-
_pages.forEach((page) => listPrePages.push(definePage(page)()));
|
|
716
|
-
else
|
|
717
|
-
listPrePages.push(definePage(_pages)());
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
} catch (details) {
|
|
721
|
-
logger$1.error(details);
|
|
794
|
+
await server.transformRequest(url);
|
|
795
|
+
} catch (e) {
|
|
796
|
+
logger.debug("Warmup for %s failed with: %s", url, e);
|
|
722
797
|
}
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
await
|
|
798
|
+
const mod = await server.moduleGraph.getModuleByUrl(url);
|
|
799
|
+
const deps = Array.from(mod?.importedModules || []);
|
|
800
|
+
await Promise.all(deps.map((m) => warmup(m.url.replace("/@id/__x00__", "\0"))));
|
|
801
|
+
};
|
|
802
|
+
await Promise.all(entries.map((entry) => warmup(entry)));
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
function cacheDirPlugin(rootDir, name) {
|
|
806
|
+
const optimizeCacheDir = resolve(rootDir, "node_modules/.cache/vite", name);
|
|
807
|
+
return {
|
|
808
|
+
name: "flow:cache-dir",
|
|
809
|
+
configResolved(resolvedConfig) {
|
|
810
|
+
resolvedConfig.optimizeCacheDir = optimizeCacheDir;
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
const wpfs = {
|
|
816
|
+
...fse,
|
|
817
|
+
join
|
|
726
818
|
};
|
|
727
819
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
820
|
+
function uniq(arr) {
|
|
821
|
+
return Array.from(new Set(arr));
|
|
822
|
+
}
|
|
823
|
+
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|less|stylus|styl)(\?[^.]+)?$/;
|
|
824
|
+
function isCSS(file) {
|
|
825
|
+
return IS_CSS_RE.test(file);
|
|
826
|
+
}
|
|
827
|
+
function hashId(id) {
|
|
828
|
+
return `$id_${hash(id)}`;
|
|
829
|
+
}
|
|
830
|
+
function hash(input, length = 8) {
|
|
831
|
+
return createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
832
|
+
}
|
|
833
|
+
function readDirRecursively(dir) {
|
|
834
|
+
return readdirSync(dir).reduce((files, file) => {
|
|
835
|
+
const name = join(dir, file);
|
|
836
|
+
const isDirectory2 = statSync(name).isDirectory();
|
|
837
|
+
return isDirectory2 ? [...files, ...readDirRecursively(name)] : [...files, name];
|
|
838
|
+
}, []);
|
|
839
|
+
}
|
|
840
|
+
async function isDirectory(path) {
|
|
841
|
+
try {
|
|
842
|
+
return (await promises.stat(path)).isDirectory();
|
|
843
|
+
} catch (_err) {
|
|
844
|
+
return false;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
function isExternal(opts, id) {
|
|
849
|
+
const ssrConfig = opts.viteServer.config.ssr;
|
|
850
|
+
const externalOpts = {
|
|
851
|
+
inline: [
|
|
852
|
+
/virtual:/,
|
|
853
|
+
/\.ts$/,
|
|
854
|
+
...ExternalsDefaults.inline,
|
|
855
|
+
...ssrConfig.noExternal
|
|
856
|
+
],
|
|
857
|
+
external: [
|
|
858
|
+
/node_modules/
|
|
859
|
+
],
|
|
860
|
+
resolve: {
|
|
861
|
+
type: "module",
|
|
862
|
+
extensions: [".ts", ".js", ".json", ".vue", ".mjs", ".jsx", ".tsx", ".wasm"]
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
return isExternal$1(id, opts.viteServer.config.root, externalOpts);
|
|
866
|
+
}
|
|
867
|
+
async function transformRequest(opts, id) {
|
|
868
|
+
if (id && id.startsWith("/@id/__x00__"))
|
|
869
|
+
id = `\0${id.slice("/@id/__x00__".length)}`;
|
|
870
|
+
if (id && id.startsWith("/@id/"))
|
|
871
|
+
id = id.slice("/@id/".length);
|
|
872
|
+
if (id && id.startsWith("/@fs/")) {
|
|
873
|
+
id = id.slice("/@fs".length);
|
|
874
|
+
if (id.match(/^\/\w:/))
|
|
875
|
+
id = id.slice(1);
|
|
876
|
+
} else if (!id.includes("entry") && id.startsWith("/")) {
|
|
877
|
+
const resolvedPath = resolve(opts.viteServer.config.root, `.${id}`);
|
|
878
|
+
if (existsSync(resolvedPath))
|
|
879
|
+
id = resolvedPath;
|
|
880
|
+
}
|
|
881
|
+
const withoutVersionQuery = id.replace(/\?v=\w+$/, "");
|
|
882
|
+
if (await isExternal(opts, withoutVersionQuery)) {
|
|
883
|
+
const path = builtinModules.includes(withoutVersionQuery.split("node:").pop()) ? withoutVersionQuery : pathToFileURL(withoutVersionQuery).href;
|
|
884
|
+
return {
|
|
885
|
+
code: `(global, module, _, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) =>
|
|
886
|
+
${genDynamicImport(path, { wrapper: false })}
|
|
887
|
+
.then(r => {
|
|
888
|
+
if (r.default && r.default.__esModule)
|
|
889
|
+
r = r.default
|
|
890
|
+
exports.default = r.default
|
|
891
|
+
ssrExportAll(r)
|
|
892
|
+
})
|
|
893
|
+
.catch(e => {
|
|
894
|
+
console.error(e)
|
|
895
|
+
throw new Error(${JSON.stringify(`[vite dev] Error loading external "${id}".`)})
|
|
896
|
+
})`,
|
|
897
|
+
deps: [],
|
|
898
|
+
dynamicDeps: []
|
|
739
899
|
};
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
900
|
+
}
|
|
901
|
+
const res = await opts.viteServer.transformRequest(id, { ssr: true }).catch((err) => {
|
|
902
|
+
console.warn(`[SSR] Error transforming ${id}:`, err);
|
|
903
|
+
}) || { code: "", map: {}, deps: [], dynamicDeps: [] };
|
|
904
|
+
const code = `async function (global, module, exports, __vite_ssr_exports__, __vite_ssr_import_meta__, __vite_ssr_import__, __vite_ssr_dynamic_import__, __vite_ssr_exportAll__) {
|
|
905
|
+
${res.code || "/* empty */"};
|
|
906
|
+
}`;
|
|
907
|
+
return { code, deps: res.deps || [], dynamicDeps: res.dynamicDeps || [] };
|
|
908
|
+
}
|
|
909
|
+
async function transformRequestRecursive(opts, id, parent = "<entry>", chunks = {}) {
|
|
910
|
+
if (chunks[id]) {
|
|
911
|
+
chunks[id].parents.push(parent);
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
const res = await transformRequest(opts, id);
|
|
915
|
+
const deps = uniq([...res.deps, ...res.dynamicDeps]);
|
|
916
|
+
chunks[id] = {
|
|
917
|
+
id,
|
|
918
|
+
code: res.code,
|
|
919
|
+
deps,
|
|
920
|
+
parents: [parent]
|
|
921
|
+
};
|
|
922
|
+
for (const dep of deps)
|
|
923
|
+
await transformRequestRecursive(opts, dep, id, chunks);
|
|
924
|
+
return Object.values(chunks);
|
|
925
|
+
}
|
|
926
|
+
async function bundleRequest(opts, entryURL) {
|
|
927
|
+
const chunks = await transformRequestRecursive(opts, entryURL);
|
|
928
|
+
const listIds = (ids) => ids.map((id) => `// - ${id} (${hashId(id)})`).join("\n");
|
|
929
|
+
const chunksCode = chunks.map((chunk) => `
|
|
930
|
+
// --------------------
|
|
931
|
+
// Request: ${chunk.id}
|
|
932
|
+
// Parents:
|
|
933
|
+
${listIds(chunk.parents)}
|
|
934
|
+
// Dependencies:
|
|
935
|
+
${listIds(chunk.deps)}
|
|
936
|
+
// --------------------
|
|
937
|
+
const ${hashId(chunk.id)} = ${chunk.code}
|
|
938
|
+
`).join("\n");
|
|
939
|
+
const manifestCode = `const __modules__ = ${genObjectFromRawEntries(chunks.map((chunk) => [chunk.id, hashId(chunk.id)]))}`;
|
|
940
|
+
const ssrModuleLoader = `
|
|
941
|
+
const __pendingModules__ = new Map()
|
|
942
|
+
const __pendingImports__ = new Map()
|
|
943
|
+
const __ssrContext__ = { global: globalThis }
|
|
944
|
+
|
|
945
|
+
function __ssrLoadModule__(url, urlStack = []) {
|
|
946
|
+
const pendingModule = __pendingModules__.get(url)
|
|
947
|
+
if (pendingModule) { return pendingModule }
|
|
948
|
+
const modulePromise = __instantiateModule__(url, urlStack)
|
|
949
|
+
__pendingModules__.set(url, modulePromise)
|
|
950
|
+
modulePromise.catch(() => { __pendingModules__.delete(url) })
|
|
951
|
+
.finally(() => { __pendingModules__.delete(url) })
|
|
952
|
+
return modulePromise
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
async function __instantiateModule__(url, urlStack) {
|
|
956
|
+
const mod = __modules__[url]
|
|
957
|
+
if (mod.stubModule) { return mod.stubModule }
|
|
958
|
+
const stubModule = { [Symbol.toStringTag]: 'Module' }
|
|
959
|
+
Object.defineProperty(stubModule, '__esModule', { value: true })
|
|
960
|
+
mod.stubModule = stubModule
|
|
961
|
+
// https://vitejs.dev/guide/api-hmr.html
|
|
962
|
+
const importMeta = { url, hot: { accept() {}, prune() {}, dispose() {}, invalidate() {}, decline() {}, on() {} } }
|
|
963
|
+
urlStack = urlStack.concat(url)
|
|
964
|
+
const isCircular = url => urlStack.includes(url)
|
|
965
|
+
const pendingDeps = []
|
|
966
|
+
const ssrImport = async (dep) => {
|
|
967
|
+
// TODO: Handle externals if dep[0] !== '.' | '/'
|
|
968
|
+
if (!isCircular(dep) && !__pendingImports__.get(dep)?.some(isCircular)) {
|
|
969
|
+
pendingDeps.push(dep)
|
|
970
|
+
if (pendingDeps.length === 1) {
|
|
971
|
+
__pendingImports__.set(url, pendingDeps)
|
|
756
972
|
}
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
const page = pages[key];
|
|
763
|
-
registerRoute(page);
|
|
973
|
+
await __ssrLoadModule__(dep, urlStack)
|
|
974
|
+
if (pendingDeps.length === 1) {
|
|
975
|
+
__pendingImports__.delete(url)
|
|
976
|
+
} else {
|
|
977
|
+
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
|
|
764
978
|
}
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
979
|
+
}
|
|
980
|
+
return __modules__[dep].stubModule
|
|
981
|
+
}
|
|
982
|
+
function ssrDynamicImport (dep) {
|
|
983
|
+
// TODO: Handle dynamic import starting with . relative to url
|
|
984
|
+
return ssrImport(dep)
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
function ssrExportAll(sourceModule) {
|
|
988
|
+
for (const key in sourceModule) {
|
|
989
|
+
if (key !== 'default') {
|
|
990
|
+
try {
|
|
991
|
+
Object.defineProperty(stubModule, key, {
|
|
992
|
+
enumerable: true,
|
|
993
|
+
configurable: true,
|
|
994
|
+
get() { return sourceModule[key] }
|
|
995
|
+
})
|
|
996
|
+
} catch (_err) { }
|
|
769
997
|
}
|
|
770
|
-
|
|
771
|
-
};
|
|
772
|
-
return { init, addPage };
|
|
998
|
+
}
|
|
773
999
|
}
|
|
774
|
-
};
|
|
775
1000
|
|
|
776
|
-
const
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
1001
|
+
const cjsModule = {
|
|
1002
|
+
get exports () {
|
|
1003
|
+
return stubModule.default
|
|
1004
|
+
},
|
|
1005
|
+
set exports (v) {
|
|
1006
|
+
stubModule.default = v
|
|
1007
|
+
},
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
await mod(
|
|
1011
|
+
__ssrContext__.global,
|
|
1012
|
+
cjsModule,
|
|
1013
|
+
stubModule.default,
|
|
1014
|
+
stubModule,
|
|
1015
|
+
importMeta,
|
|
1016
|
+
ssrImport,
|
|
1017
|
+
ssrDynamicImport,
|
|
1018
|
+
ssrExportAll
|
|
1019
|
+
)
|
|
1020
|
+
|
|
1021
|
+
return stubModule
|
|
1022
|
+
}
|
|
1023
|
+
`;
|
|
1024
|
+
const code = [
|
|
1025
|
+
chunksCode,
|
|
1026
|
+
manifestCode,
|
|
1027
|
+
ssrModuleLoader,
|
|
1028
|
+
`export default await __ssrLoadModule__(${JSON.stringify(entryURL)})`
|
|
1029
|
+
].join("\n\n");
|
|
1030
|
+
return {
|
|
1031
|
+
code,
|
|
1032
|
+
ids: chunks.map((i) => i.id)
|
|
781
1033
|
};
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
async function writeManifest(ctx, extraEntries = []) {
|
|
1037
|
+
const clientDist = resolve(ctx.flow.options.buildDir, "dist/client");
|
|
1038
|
+
const serverDist = resolve(ctx.flow.options.buildDir, "dist/server");
|
|
1039
|
+
const entries = [
|
|
1040
|
+
"@vite/client",
|
|
1041
|
+
"entry.mjs",
|
|
1042
|
+
...extraEntries
|
|
1043
|
+
];
|
|
1044
|
+
const devClientManifest = {
|
|
1045
|
+
publicPath: joinURL(ctx.flow.options.app.baseURL, ctx.flow.options.app.buildAssetsDir),
|
|
1046
|
+
all: entries,
|
|
1047
|
+
initial: entries,
|
|
1048
|
+
async: [],
|
|
1049
|
+
modules: {}
|
|
793
1050
|
};
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
1051
|
+
const clientManifest = ctx.flow.options.dev ? devClientManifest : await fse.readJSON(resolve(clientDist, "manifest.json"));
|
|
1052
|
+
await fse.mkdirp(serverDist);
|
|
1053
|
+
await fse.writeFile(resolve(serverDist, "client.manifest.json"), JSON.stringify(clientManifest, null, 2), "utf8");
|
|
1054
|
+
await fse.writeFile(resolve(serverDist, "client.manifest.mjs"), `export default ${JSON.stringify(clientManifest, null, 2)}`, "utf8");
|
|
1055
|
+
}
|
|
797
1056
|
|
|
798
|
-
const
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
1057
|
+
const buildServer = async (ctx) => {
|
|
1058
|
+
const serverConfig = vite.mergeConfig(ctx.config, {
|
|
1059
|
+
configFile: false,
|
|
1060
|
+
define: {
|
|
1061
|
+
"process.server": true,
|
|
1062
|
+
"typeof window": '"undefined"',
|
|
1063
|
+
"typeof document": '"undefined"',
|
|
1064
|
+
"typeof navigator": '"undefined"',
|
|
1065
|
+
"typeof location": '"undefined"',
|
|
1066
|
+
"typeof XMLHttpRequest": '"undefined"'
|
|
1067
|
+
},
|
|
1068
|
+
ssr: {
|
|
1069
|
+
noExternal: [
|
|
1070
|
+
...ctx.flow.options.build.transpile,
|
|
1071
|
+
/\/esm\/.*\.js$/,
|
|
1072
|
+
/\.(es|esm|esm-browser|esm-bundler).js$/,
|
|
1073
|
+
"/__vue-jsx",
|
|
1074
|
+
"#app",
|
|
1075
|
+
/(nuxt|nuxt3)\/(dist|src|app)/,
|
|
1076
|
+
/@monkeyplus\/flow\/(dist|src|app)/,
|
|
1077
|
+
/@nuxt\/nitro\/(dist|src)/
|
|
1078
|
+
]
|
|
1079
|
+
},
|
|
1080
|
+
build: {
|
|
1081
|
+
outDir: resolve(ctx.flow.options.buildDir, "dist/server"),
|
|
1082
|
+
manifest: true,
|
|
1083
|
+
rollupOptions: {
|
|
1084
|
+
external: ["#internal/nitro"],
|
|
1085
|
+
output: {
|
|
1086
|
+
entryFileNames: "server.mjs",
|
|
1087
|
+
preferConst: true,
|
|
1088
|
+
format: "module"
|
|
1089
|
+
},
|
|
1090
|
+
onwarn(warning, rollupWarn) {
|
|
1091
|
+
if (!["UNUSED_EXTERNAL_IMPORT"].includes(warning.code))
|
|
1092
|
+
rollupWarn(warning);
|
|
808
1093
|
}
|
|
809
1094
|
}
|
|
810
1095
|
},
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
1096
|
+
server: {
|
|
1097
|
+
preTransformRequests: false,
|
|
1098
|
+
cors: true
|
|
1099
|
+
},
|
|
1100
|
+
plugins: [
|
|
1101
|
+
cacheDirPlugin(ctx.flow.options.rootDir, "server")
|
|
1102
|
+
]
|
|
1103
|
+
});
|
|
1104
|
+
await ctx.flow.callHook("vite:extendConfig", serverConfig, { isClient: false, isServer: true });
|
|
1105
|
+
ctx.flow.hook("nitro:build:before", async () => {
|
|
1106
|
+
if (ctx.flow.options.dev)
|
|
1107
|
+
return;
|
|
1108
|
+
const clientDist = resolve(ctx.flow.options.buildDir, "dist/client");
|
|
1109
|
+
const publicDir = join(ctx.flow.options.srcDir, ctx.flow.options.dir.public);
|
|
1110
|
+
let publicFiles = [];
|
|
1111
|
+
if (await isDirectory(publicDir)) {
|
|
1112
|
+
publicFiles = readDirRecursively(publicDir).map((r) => r.replace(publicDir, ""));
|
|
1113
|
+
for (const file of publicFiles) {
|
|
1114
|
+
try {
|
|
1115
|
+
fse.rmSync(join(clientDist, file));
|
|
1116
|
+
} catch {
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
if (await isDirectory(clientDist)) {
|
|
1121
|
+
const nestedAssetsPath = withoutTrailingSlash(join(clientDist, ctx.flow.options.app.buildAssetsDir));
|
|
1122
|
+
if (await isDirectory(nestedAssetsPath)) {
|
|
1123
|
+
await fse.copy(nestedAssetsPath, clientDist, { recursive: true });
|
|
1124
|
+
await fse.remove(nestedAssetsPath);
|
|
815
1125
|
}
|
|
816
1126
|
}
|
|
817
1127
|
});
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
1128
|
+
const onBuild = () => ctx.flow.callHook("build:resources", wpfs);
|
|
1129
|
+
if (!ctx.flow.options.dev) {
|
|
1130
|
+
const start = Date.now();
|
|
1131
|
+
logger.info("Building server...");
|
|
1132
|
+
await vite.build(serverConfig);
|
|
1133
|
+
await onBuild();
|
|
1134
|
+
logger.success(`Server built in ${Date.now() - start}ms`);
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
const viteServer = await vite.createServer(serverConfig);
|
|
1138
|
+
ctx.ssrServer = viteServer;
|
|
1139
|
+
await ctx.flow.callHook("vite:serverCreated", viteServer, { isClient: false, isServer: true });
|
|
1140
|
+
ctx.flow.hook("close", () => viteServer.close());
|
|
1141
|
+
await viteServer.pluginContainer.buildStart({});
|
|
1142
|
+
const _doBuild = async () => {
|
|
1143
|
+
const start = Date.now();
|
|
1144
|
+
const { code, ids } = await bundleRequest({ viteServer }, resolve(ctx.flow.options.appDir, "entry"));
|
|
1145
|
+
await fse.ensureFile(resolve(ctx.flow.options.buildDir, "dist/server/server.mjs"));
|
|
1146
|
+
await fse.writeFile(resolve(ctx.flow.options.buildDir, "dist/server/server.mjs"), code, "utf-8");
|
|
1147
|
+
await writeManifest(ctx, ids.filter(isCSS).map((i) => i.slice(1)));
|
|
1148
|
+
const time = Date.now() - start;
|
|
1149
|
+
logger.success(`Vite server built in ${time}ms`);
|
|
1150
|
+
await onBuild();
|
|
1151
|
+
ctx.flow.callHook("bundler:change", {});
|
|
1152
|
+
};
|
|
1153
|
+
const doBuild = debounce(_doBuild);
|
|
1154
|
+
await _doBuild();
|
|
1155
|
+
viteServer.watcher.on("all", (_event, file) => {
|
|
1156
|
+
file = normalize(file);
|
|
1157
|
+
if (file.indexOf(ctx.flow.options.buildDir) === 0)
|
|
1158
|
+
return;
|
|
1159
|
+
doBuild();
|
|
1160
|
+
});
|
|
1161
|
+
ctx.flow.hook("app:templatesGenerated", () => doBuild());
|
|
1162
|
+
};
|
|
1163
|
+
|
|
1164
|
+
const PREFIX = "virtual:";
|
|
1165
|
+
function virtual(vfs) {
|
|
1166
|
+
const extensions = ["", ".ts", ".vue", ".mjs", ".cjs", ".js", ".json"];
|
|
1167
|
+
const resolveWithExt = (id) => {
|
|
1168
|
+
for (const ext of extensions) {
|
|
1169
|
+
const rId = id + ext;
|
|
1170
|
+
if (rId in vfs)
|
|
1171
|
+
return rId;
|
|
1172
|
+
}
|
|
1173
|
+
return null;
|
|
1174
|
+
};
|
|
1175
|
+
return {
|
|
1176
|
+
name: "virtual",
|
|
1177
|
+
resolveId(id, importer) {
|
|
1178
|
+
if (process.platform === "win32" && isAbsolute(id)) {
|
|
1179
|
+
id = resolve(id);
|
|
838
1180
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
1181
|
+
const resolvedId = resolveWithExt(id);
|
|
1182
|
+
if (resolvedId)
|
|
1183
|
+
return PREFIX + resolvedId;
|
|
1184
|
+
if (importer && !isAbsolute(id)) {
|
|
1185
|
+
const importerNoPrefix = importer.startsWith(PREFIX) ? importer.slice(PREFIX.length) : importer;
|
|
1186
|
+
const importedDir = dirname(importerNoPrefix);
|
|
1187
|
+
const resolved = resolveWithExt(join(importedDir, id));
|
|
1188
|
+
if (resolved)
|
|
1189
|
+
return PREFIX + resolved;
|
|
848
1190
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
1191
|
+
return null;
|
|
1192
|
+
},
|
|
1193
|
+
load(id) {
|
|
1194
|
+
if (!id.startsWith(PREFIX))
|
|
1195
|
+
return null;
|
|
1196
|
+
const idNoPrefix = id.slice(PREFIX.length);
|
|
1197
|
+
return {
|
|
1198
|
+
code: vfs[idNoPrefix],
|
|
1199
|
+
map: null
|
|
1200
|
+
};
|
|
1201
|
+
}
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
const VITE_ASSET_RE = /^export default ["'](__VITE_ASSET.*)["']$/;
|
|
1206
|
+
const DynamicBasePlugin = createUnplugin((options = {}) => {
|
|
1207
|
+
return {
|
|
1208
|
+
name: "nuxt:dynamic-base-path",
|
|
1209
|
+
resolveId(id) {
|
|
1210
|
+
if (id.startsWith("/__NUXT_BASE__"))
|
|
1211
|
+
return id.replace("/__NUXT_BASE__", "");
|
|
1212
|
+
if (id === "#internal/nitro")
|
|
1213
|
+
return "#internal/nitro";
|
|
1214
|
+
return null;
|
|
1215
|
+
},
|
|
1216
|
+
enforce: "post",
|
|
1217
|
+
transform(code, id) {
|
|
1218
|
+
const s = new MagicString(code);
|
|
1219
|
+
if (options.globalPublicPath && id.includes("paths.mjs") && code.includes("const appConfig = "))
|
|
1220
|
+
s.append(`${options.globalPublicPath} = buildAssetsURL();
|
|
1221
|
+
`);
|
|
1222
|
+
const assetId = code.match(VITE_ASSET_RE);
|
|
1223
|
+
if (assetId) {
|
|
1224
|
+
s.overwrite(0, code.length, [
|
|
1225
|
+
"import { buildAssetsURL } from '#build/paths.mjs';",
|
|
1226
|
+
`export default buildAssetsURL("${assetId[1]}".replace("/__NUXT_BASE__", ""));`
|
|
1227
|
+
].join("\n"));
|
|
862
1228
|
}
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
1229
|
+
if (!id.includes("paths.mjs") && code.includes("NUXT_BASE") && !code.includes("import { publicAssetsURL as __publicAssetsURL }"))
|
|
1230
|
+
s.prepend("import { publicAssetsURL as __publicAssetsURL } from '#build/paths.mjs';\n");
|
|
1231
|
+
if (id === "vite/preload-helper") {
|
|
1232
|
+
s.prepend("import { buildAssetsDir } from '#build/paths.mjs';\n");
|
|
1233
|
+
s.replace(/const base = ['"]\/__NUXT_BASE__\/['"]/, "const base = buildAssetsDir()");
|
|
1234
|
+
}
|
|
1235
|
+
s.replace(/from *['"]\/__NUXT_BASE__(\/[^'"]*)['"]/g, 'from "$1"');
|
|
1236
|
+
const delimiterRE = /(?<!(const base = |from *))(`([^`]*)\/__NUXT_BASE__\/([^`]*)`|'([^']*)\/__NUXT_BASE__\/([^']*)'|"([^"]*)\/__NUXT_BASE__\/([^"]*)")/g;
|
|
1237
|
+
s.replace(delimiterRE, (r) => `\`${r.replace(/\/__NUXT_BASE__\//g, "${__publicAssetsURL()}").slice(1, -1)}\``);
|
|
1238
|
+
if (s.hasChanged()) {
|
|
872
1239
|
return {
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
return {
|
|
876
|
-
total: l.length,
|
|
877
|
-
pages: l
|
|
878
|
-
};
|
|
879
|
-
}, locales)
|
|
1240
|
+
code: s.toString(),
|
|
1241
|
+
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
|
|
880
1242
|
};
|
|
881
1243
|
}
|
|
882
|
-
});
|
|
883
|
-
server.route({
|
|
884
|
-
path: "/_flow/configs",
|
|
885
|
-
method: "get",
|
|
886
|
-
handler: (req) => {
|
|
887
|
-
return req.server.plugins.flow.configs;
|
|
888
|
-
}
|
|
889
|
-
});
|
|
890
|
-
}
|
|
891
|
-
const HMR = {
|
|
892
|
-
head: ' <script src="/_flow/hmr/client.js"><\/script>',
|
|
893
|
-
body: ' <script src="/_flow/hmr/connect.js"><\/script>'
|
|
894
|
-
};
|
|
895
|
-
server.ext("onPreHandler", async (req, h) => {
|
|
896
|
-
if (req.route.settings.plugins?.generate === ".html") {
|
|
897
|
-
const { global } = req.plugins.flow;
|
|
898
|
-
Object.assign(global, {
|
|
899
|
-
hmr: isProduction || isGenerate ? { head: "", body: "" } : HMR
|
|
900
|
-
});
|
|
901
1244
|
}
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
};
|
|
1245
|
+
};
|
|
1246
|
+
});
|
|
905
1247
|
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1248
|
+
async function bundleVite(flow) {
|
|
1249
|
+
const hmrPortDefault = 24678;
|
|
1250
|
+
const hmrPort = await getPort({
|
|
1251
|
+
port: hmrPortDefault,
|
|
1252
|
+
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
|
|
1253
|
+
});
|
|
1254
|
+
const ctx = {
|
|
1255
|
+
nuxt: flow,
|
|
1256
|
+
flow,
|
|
1257
|
+
config: vite.mergeConfig({
|
|
1258
|
+
mode: flow.options.dev ? "development" : void 0,
|
|
1259
|
+
resolve: {
|
|
1260
|
+
alias: {
|
|
1261
|
+
...flow.options.alias,
|
|
1262
|
+
"#app": flow.options.appDir,
|
|
1263
|
+
"#build/plugins": resolve(flow.options.buildDir, "plugins/server"),
|
|
1264
|
+
"#build": flow.options.buildDir,
|
|
1265
|
+
"/entry.mjs": resolve(flow.options.appDir, "entry")
|
|
1266
|
+
}
|
|
921
1267
|
},
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
const { serverInfo } = ServerInfo.register(server);
|
|
928
|
-
const serverDecorate = {
|
|
929
|
-
serverInfo
|
|
930
|
-
};
|
|
931
|
-
const helpers = {
|
|
932
|
-
getPath: (...paths) => path.join(config.relativeTo, ...paths),
|
|
933
|
-
addRedirect: (key, urlPath) => {
|
|
934
|
-
state.pages.redirects[key] = urlPath;
|
|
1268
|
+
optimizeDeps: {
|
|
1269
|
+
entries: [
|
|
1270
|
+
resolve(flow.options.appDir, "entry.ts")
|
|
1271
|
+
],
|
|
1272
|
+
include: []
|
|
935
1273
|
},
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
logger$1.warn("The namePage %s already exist", state.pages.statics[key]);
|
|
941
|
-
} else {
|
|
942
|
-
state.pages.statics[key] = url;
|
|
1274
|
+
build: {
|
|
1275
|
+
rollupOptions: {
|
|
1276
|
+
output: { sanitizeFileName: sanitizeFilePath },
|
|
1277
|
+
input: resolve(flow.options.appDir, "entry")
|
|
943
1278
|
}
|
|
944
1279
|
},
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
else
|
|
967
|
-
logger$1.warn("Duplicate page %s", keyPage);
|
|
968
|
-
}
|
|
969
|
-
}
|
|
1280
|
+
plugins: [
|
|
1281
|
+
replace({
|
|
1282
|
+
...Object.fromEntries([";", "(", "{", "}", " ", " ", "\n"].map((d) => [`${d}global.`, `${d}globalThis.`])),
|
|
1283
|
+
preventAssignment: true
|
|
1284
|
+
}),
|
|
1285
|
+
virtual(flow.vfs),
|
|
1286
|
+
DynamicBasePlugin.vite({ sourcemap: flow.options.sourcemap })
|
|
1287
|
+
],
|
|
1288
|
+
server: {
|
|
1289
|
+
watch: {
|
|
1290
|
+
ignored: isIgnoredFlow
|
|
1291
|
+
},
|
|
1292
|
+
hmr: {
|
|
1293
|
+
protocol: "ws",
|
|
1294
|
+
clientPort: hmrPort,
|
|
1295
|
+
port: hmrPort
|
|
1296
|
+
},
|
|
1297
|
+
fs: {
|
|
1298
|
+
allow: [
|
|
1299
|
+
flow.options.appDir
|
|
1300
|
+
]
|
|
970
1301
|
}
|
|
971
1302
|
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
1303
|
+
}, flow.options.vite)
|
|
1304
|
+
};
|
|
1305
|
+
if (flow.options.dev)
|
|
1306
|
+
ctx.config.mode = process.env.NODE_ENV || "development";
|
|
1307
|
+
await flow.callHook("vite:extend", ctx);
|
|
1308
|
+
flow.hook("vite:serverCreated", (server) => {
|
|
1309
|
+
ctx.nuxt.hook("app:templatesGenerated", () => {
|
|
1310
|
+
for (const [id, mod] of server.moduleGraph.idToModuleMap) {
|
|
1311
|
+
if (id.startsWith("\0virtual:"))
|
|
1312
|
+
server.moduleGraph.invalidateModule(mod);
|
|
1313
|
+
}
|
|
976
1314
|
});
|
|
977
|
-
|
|
978
|
-
|
|
1315
|
+
const start = Date.now();
|
|
1316
|
+
warmupViteServer(server, ["/entry.mjs"]).then(() => logger.info(`Vite server warmed up in ${Date.now() - start}ms`)).catch(logger.error);
|
|
1317
|
+
});
|
|
1318
|
+
await buildServer(ctx);
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
async function build(flow) {
|
|
1322
|
+
const app = createApp(flow);
|
|
1323
|
+
const generateApp$1 = debounce(() => generateApp(flow, app), void 0, { leading: true });
|
|
1324
|
+
await generateApp$1();
|
|
1325
|
+
if (flow.options.dev) {
|
|
1326
|
+
watch(flow);
|
|
1327
|
+
flow.hook("builder:watch", async (event, path) => {
|
|
1328
|
+
if (event !== "change" && /app|error|plugins/i.test(path))
|
|
1329
|
+
await generateApp$1();
|
|
979
1330
|
});
|
|
980
|
-
|
|
981
|
-
Decorators.register(server);
|
|
982
|
-
LifeCircle.register(server);
|
|
983
|
-
const { runGenerate } = useGenerator.register(server);
|
|
984
|
-
serverDecorate.runGenerate = runGenerate;
|
|
985
|
-
const { addPage, init } = RunMethods.register(server);
|
|
986
|
-
serverDecorate.addPage = addPage;
|
|
987
|
-
serverDecorate.init = init;
|
|
988
|
-
serverDecorate.prepagePackage = async () => {
|
|
989
|
-
for (const key in state.generate.beforePackage)
|
|
990
|
-
await state.generate.beforePackage[key]();
|
|
991
|
-
};
|
|
992
|
-
serverDecorate.assignPluginOptions = (realm, options = {}, d = {}, _context) => {
|
|
993
|
-
const key = realm.plugin.replace("@monkeyplus/", "").replace("flow-", "");
|
|
994
|
-
const baseOptions = applyToDefaults(d, options);
|
|
995
|
-
const { plugins } = config;
|
|
996
|
-
if (plugins[key] && Object.values(plugins[key])) {
|
|
997
|
-
if (typeof plugins[key] === "boolean") {
|
|
998
|
-
try {
|
|
999
|
-
const module = require(path.resolve("./extensions", key))?.default;
|
|
1000
|
-
if (typeof module === "function") {
|
|
1001
|
-
const promise = module({ server, ..._context });
|
|
1002
|
-
const isPromise = !!promise && typeof promise.then === "function";
|
|
1003
|
-
if (isPromise) {
|
|
1004
|
-
return promise.then((opts2) => {
|
|
1005
|
-
const _opts = applyToDefaults(baseOptions, opts2 || {});
|
|
1006
|
-
Object.assign(plugins, { [key]: _opts });
|
|
1007
|
-
return _opts;
|
|
1008
|
-
});
|
|
1009
|
-
}
|
|
1010
|
-
Object.assign(plugins, { [key]: promise });
|
|
1011
|
-
return promise;
|
|
1012
|
-
}
|
|
1013
|
-
const opts = applyToDefaults(baseOptions, module || {});
|
|
1014
|
-
Object.assign(plugins, { [key]: opts });
|
|
1015
|
-
return opts;
|
|
1016
|
-
} catch (error) {
|
|
1017
|
-
logger$1.error("Error in load extension: %s", key);
|
|
1018
|
-
return baseOptions;
|
|
1019
|
-
}
|
|
1020
|
-
} else {
|
|
1021
|
-
const opts = applyToDefaults(baseOptions, plugins[key]);
|
|
1022
|
-
Object.assign(plugins, { [key]: opts });
|
|
1023
|
-
return opts;
|
|
1024
|
-
}
|
|
1025
|
-
} else {
|
|
1026
|
-
Object.assign(plugins, { [key]: baseOptions });
|
|
1027
|
-
return baseOptions;
|
|
1028
|
-
}
|
|
1029
|
-
};
|
|
1030
|
-
await RegisterCommon(server, config);
|
|
1331
|
+
flow.hook("builder:generateApp", generateApp$1);
|
|
1031
1332
|
}
|
|
1032
|
-
};
|
|
1333
|
+
await flow.callHook("build:before", { flow }, flow.options.build);
|
|
1334
|
+
if (!flow.options._prepare) {
|
|
1335
|
+
await bundle(flow);
|
|
1336
|
+
await flow.callHook("build:done", { flow });
|
|
1337
|
+
}
|
|
1338
|
+
if (!flow.options.dev)
|
|
1339
|
+
await flow.callHook("close", flow);
|
|
1340
|
+
}
|
|
1341
|
+
function watch(flow) {
|
|
1342
|
+
const watcher = chokidar.watch(flow.options.srcDir, {
|
|
1343
|
+
...flow.options.watchers.chokidar,
|
|
1344
|
+
cwd: flow.options.srcDir,
|
|
1345
|
+
ignoreInitial: true,
|
|
1346
|
+
ignored: [
|
|
1347
|
+
isIgnoredFlow,
|
|
1348
|
+
".flow",
|
|
1349
|
+
"node_modules"
|
|
1350
|
+
]
|
|
1351
|
+
});
|
|
1352
|
+
const watchHook = debounce((event, path) => flow.callHook("builder:watch", event, normalize(path)));
|
|
1353
|
+
watcher.on("all", watchHook);
|
|
1354
|
+
flow.hook("close", () => watcher.close());
|
|
1355
|
+
return watcher;
|
|
1356
|
+
}
|
|
1357
|
+
async function bundle(nuxt) {
|
|
1358
|
+
try {
|
|
1359
|
+
return bundleVite(nuxt);
|
|
1360
|
+
} catch (error) {
|
|
1361
|
+
await nuxt.callHook("build:error", error);
|
|
1362
|
+
throw error;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1033
1365
|
|
|
1034
|
-
export {
|
|
1366
|
+
export { build, createFlow, defineFlowConfig, loadFlow };
|