@monkeyplus/flow 5.0.0-rc.105 → 5.0.0-rc.107
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/dist/app/entry.mjs +1 -1
- package/dist/app/flow.d.ts +1 -0
- package/dist/chunks/dev-bundler.mjs +33 -3
- package/dist/core/runtime/nitro/flow.d.ts +1 -0
- package/dist/core/runtime/nitro/flow.mjs +6 -6
- package/dist/core/runtime/nitro/renderer.mjs +2 -2
- package/dist/index.mjs +1106 -26
- package/package.json +35 -35
- package/dist/chunks/external.mjs +0 -37
- package/dist/chunks/index.mjs +0 -1031
- package/dist/chunks/vite-node.mjs +0 -154
package/dist/index.mjs
CHANGED
|
@@ -1,26 +1,1106 @@
|
|
|
1
|
-
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
5
|
-
import 'url';
|
|
6
|
-
import 'unimport';
|
|
7
|
-
import '
|
|
8
|
-
import '
|
|
9
|
-
import '
|
|
10
|
-
import '
|
|
11
|
-
import '
|
|
12
|
-
import 'scule';
|
|
13
|
-
import 'knitwork';
|
|
14
|
-
import 'node:fs';
|
|
15
|
-
import 'nitropack';
|
|
16
|
-
import '
|
|
17
|
-
import '
|
|
18
|
-
import '
|
|
19
|
-
import '
|
|
20
|
-
import '
|
|
21
|
-
import '
|
|
22
|
-
import '
|
|
23
|
-
import 'vite';
|
|
24
|
-
import '@rollup/plugin-replace';
|
|
25
|
-
import '
|
|
26
|
-
import '
|
|
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, addWebpackPlugin, updateTemplates, useNuxt, resolveAlias, resolveFilesFlow, nuxtCtx, installModule, loadFlowConfig, normalizeTemplate, compileTemplate, normalizePlugin, findPath, templateUtils, isIgnoredFlow } from '@monkeyplus/flow-kit';
|
|
4
|
+
import escapeRE from 'escape-string-regexp';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { defineUnimportPreset, createUnimport, toImports, scanDirExports } from 'unimport';
|
|
7
|
+
import defu from 'defu';
|
|
8
|
+
import { pathToFileURL } from 'node:url';
|
|
9
|
+
import { createUnplugin } from 'unplugin';
|
|
10
|
+
import { parseURL, parseQuery, joinURL } from 'ufo';
|
|
11
|
+
import fs from 'fs';
|
|
12
|
+
import { camelCase, pascalCase } from 'scule';
|
|
13
|
+
import { genImport, genDynamicImport, genSafeVariableName, genArrayFromRaw, genString } from 'knitwork';
|
|
14
|
+
import { existsSync, promises } from 'node:fs';
|
|
15
|
+
import { createNitro, scanHandlers, writeTypes, build as build$1, prepare, copyPublicAssets, prerender, createDevServer } from 'nitropack';
|
|
16
|
+
import { dynamicEventHandler } from 'h3';
|
|
17
|
+
import { createRequire } from 'node:module';
|
|
18
|
+
import chokidar from 'chokidar';
|
|
19
|
+
import { debounce } from 'perfect-debounce';
|
|
20
|
+
import { generateTypes, resolveSchema } from 'untyped';
|
|
21
|
+
import { hash } from 'ohash';
|
|
22
|
+
import { resolvePath, sanitizeFilePath } from 'mlly';
|
|
23
|
+
import * as vite from 'vite';
|
|
24
|
+
import replace from '@rollup/plugin-replace';
|
|
25
|
+
import { resolveTSConfig } from 'pkg-types';
|
|
26
|
+
import fse from 'fs-extra';
|
|
27
|
+
|
|
28
|
+
const version = "5.0.0-rc.107";
|
|
29
|
+
|
|
30
|
+
let _distDir = dirname(fileURLToPath(import.meta.url));
|
|
31
|
+
if (_distDir.endsWith("chunks"))
|
|
32
|
+
_distDir = dirname(_distDir);
|
|
33
|
+
const distDir = _distDir;
|
|
34
|
+
const pkgDir = resolve(distDir, "..");
|
|
35
|
+
resolve(distDir, "runtime");
|
|
36
|
+
|
|
37
|
+
const metaModule = defineFlowModule({
|
|
38
|
+
meta: {
|
|
39
|
+
name: "meta"
|
|
40
|
+
},
|
|
41
|
+
defaults: {
|
|
42
|
+
charset: "utf-8",
|
|
43
|
+
viewport: "width=device-width, initial-scale=1"
|
|
44
|
+
},
|
|
45
|
+
setup(options, flow) {
|
|
46
|
+
const runtimeDir = flow.options.alias["#head"] || resolve(distDir, "head/runtime");
|
|
47
|
+
flow.options.alias["#head"] = runtimeDir;
|
|
48
|
+
addPlugin({ src: resolve(runtimeDir, "plugin") });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }) => {
|
|
53
|
+
return {
|
|
54
|
+
name: "nuxt:imports-transform",
|
|
55
|
+
enforce: "post",
|
|
56
|
+
transformInclude(id) {
|
|
57
|
+
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href));
|
|
58
|
+
const query = parseQuery(search);
|
|
59
|
+
if (options.transform?.include?.some((pattern) => id.match(pattern)))
|
|
60
|
+
return true;
|
|
61
|
+
if (options.transform?.exclude?.some((pattern) => id.match(pattern)))
|
|
62
|
+
return false;
|
|
63
|
+
if (id.endsWith(".vue") || "macro" in query || "vue" in query && (query.type === "template" || query.type === "script" || "setup" in query))
|
|
64
|
+
return true;
|
|
65
|
+
if (pathname.match(/\.((c|m)?j|t)sx?$/g))
|
|
66
|
+
return true;
|
|
67
|
+
},
|
|
68
|
+
async transform(code, id) {
|
|
69
|
+
id = normalize(id);
|
|
70
|
+
const isNodeModule = id.match(/[\\/]node_modules[\\/]/) && !options.transform?.include?.some((pattern) => id.match(pattern));
|
|
71
|
+
if (isNodeModule && !code.match(/(['"])#imports\1/))
|
|
72
|
+
return;
|
|
73
|
+
const { s } = await ctx.injectImports(code, id, { autoImport: options.autoImport && !isNodeModule });
|
|
74
|
+
if (s.hasChanged()) {
|
|
75
|
+
return {
|
|
76
|
+
code: s.toString(),
|
|
77
|
+
map: sourcemap ? s.generateMap({ source: id, includeContent: true }) : void 0
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const commonPresets = [
|
|
85
|
+
defineUnimportPreset({
|
|
86
|
+
from: "#head",
|
|
87
|
+
imports: [
|
|
88
|
+
"useHead"
|
|
89
|
+
]
|
|
90
|
+
}),
|
|
91
|
+
defineUnimportPreset({
|
|
92
|
+
from: "#_pages",
|
|
93
|
+
imports: [
|
|
94
|
+
"definePage",
|
|
95
|
+
"defineDynamicPage",
|
|
96
|
+
"defineSharedContext"
|
|
97
|
+
]
|
|
98
|
+
})
|
|
99
|
+
];
|
|
100
|
+
const appPreset = defineUnimportPreset({
|
|
101
|
+
from: "#app",
|
|
102
|
+
imports: [
|
|
103
|
+
"useRuntimeConfig",
|
|
104
|
+
"defineFlowPlugin",
|
|
105
|
+
"defineNuxtPlugin",
|
|
106
|
+
"useCookie",
|
|
107
|
+
"refreshNuxtData",
|
|
108
|
+
"useAsyncData",
|
|
109
|
+
"useRoute"
|
|
110
|
+
]
|
|
111
|
+
});
|
|
112
|
+
const vuePreset = defineUnimportPreset({
|
|
113
|
+
from: "vue",
|
|
114
|
+
imports: [
|
|
115
|
+
"defineComponent",
|
|
116
|
+
"getCurrentInstance",
|
|
117
|
+
"useSlots",
|
|
118
|
+
"h",
|
|
119
|
+
"computed"
|
|
120
|
+
]
|
|
121
|
+
});
|
|
122
|
+
const defaultPresets = [
|
|
123
|
+
...commonPresets,
|
|
124
|
+
appPreset,
|
|
125
|
+
vuePreset
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
const importsModule = defineNuxtModule({
|
|
129
|
+
meta: {
|
|
130
|
+
name: "imports",
|
|
131
|
+
configKey: "imports"
|
|
132
|
+
},
|
|
133
|
+
defaults: {
|
|
134
|
+
autoImport: true,
|
|
135
|
+
presets: defaultPresets,
|
|
136
|
+
global: false,
|
|
137
|
+
imports: [],
|
|
138
|
+
dirs: [],
|
|
139
|
+
transform: {
|
|
140
|
+
include: [],
|
|
141
|
+
exclude: void 0
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
async setup(options, nuxt) {
|
|
145
|
+
if (nuxt.options.autoImports) {
|
|
146
|
+
logger.warn("`autoImports` config is deprecated, use `imports` instead.");
|
|
147
|
+
options = defu(nuxt.options.autoImports, options);
|
|
148
|
+
}
|
|
149
|
+
await nuxt.callHook("imports:sources", options.presets);
|
|
150
|
+
options.presets?.forEach((_i) => {
|
|
151
|
+
const i = _i;
|
|
152
|
+
if (typeof i !== "string" && i.names && !i.imports) {
|
|
153
|
+
i.imports = i.names;
|
|
154
|
+
logger.warn("imports: presets.names is deprecated, use presets.imports instead");
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
const ctx = createUnimport({
|
|
158
|
+
presets: options.presets,
|
|
159
|
+
imports: options.imports,
|
|
160
|
+
virtualImports: ["#imports"],
|
|
161
|
+
addons: {
|
|
162
|
+
vueTemplate: options.autoImport
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
let composablesDirs = [];
|
|
166
|
+
for (const layer of nuxt.options._layers) {
|
|
167
|
+
composablesDirs.push(resolve(layer.config.srcDir, "composables"));
|
|
168
|
+
for (const dir of layer.config.imports?.dirs ?? []) {
|
|
169
|
+
if (!dir)
|
|
170
|
+
continue;
|
|
171
|
+
composablesDirs.push(resolve(layer.config.srcDir, dir));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
await nuxt.callHook("imports:dirs", composablesDirs);
|
|
175
|
+
composablesDirs = composablesDirs.map((dir) => normalize(dir));
|
|
176
|
+
addTemplate({
|
|
177
|
+
filename: "imports.mjs",
|
|
178
|
+
getContents: async () => `${await ctx.toExports()}
|
|
179
|
+
if (process.dev) { console.warn("[nuxt] \`#imports\` should be transformed with real imports. There seems to be something wrong with the imports plugin.") }`
|
|
180
|
+
});
|
|
181
|
+
nuxt.options.alias["#imports"] = join(nuxt.options.buildDir, "imports");
|
|
182
|
+
if (nuxt.options.dev && options.global) {
|
|
183
|
+
addPluginTemplate({
|
|
184
|
+
filename: "imports.mjs",
|
|
185
|
+
getContents: async () => {
|
|
186
|
+
const imports = await ctx.getImports();
|
|
187
|
+
const importStatement = toImports(imports);
|
|
188
|
+
const globalThisSet = imports.map((i) => `globalThis.${i.as} = ${i.as};`).join("\n");
|
|
189
|
+
return `${importStatement}
|
|
190
|
+
|
|
191
|
+
${globalThisSet}
|
|
192
|
+
|
|
193
|
+
export default () => {};`;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
} else {
|
|
197
|
+
addVitePlugin(TransformPlugin.vite({ ctx, options, sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client }));
|
|
198
|
+
addWebpackPlugin(TransformPlugin.webpack({ ctx, options, sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client }));
|
|
199
|
+
}
|
|
200
|
+
const regenerateImports = async () => {
|
|
201
|
+
ctx.clearDynamicImports();
|
|
202
|
+
await ctx.modifyDynamicImports(async (imports) => {
|
|
203
|
+
imports.push(...await scanDirExports(composablesDirs));
|
|
204
|
+
await nuxt.callHook("imports:extend", imports);
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
await regenerateImports();
|
|
208
|
+
addDeclarationTemplates(ctx, options);
|
|
209
|
+
nuxt.hook("prepare:types", ({ references }) => {
|
|
210
|
+
references.push({ path: resolve(nuxt.options.buildDir, "types/imports.d.ts") });
|
|
211
|
+
references.push({ path: resolve(nuxt.options.buildDir, "imports.d.ts") });
|
|
212
|
+
});
|
|
213
|
+
const templates = [
|
|
214
|
+
"types/imports.d.ts",
|
|
215
|
+
"imports.d.ts",
|
|
216
|
+
"imports.mjs"
|
|
217
|
+
];
|
|
218
|
+
nuxt.hook("builder:watch", async (_, path) => {
|
|
219
|
+
const _resolved = resolve(nuxt.options.srcDir, path);
|
|
220
|
+
if (composablesDirs.find((dir) => _resolved.startsWith(dir))) {
|
|
221
|
+
await updateTemplates({
|
|
222
|
+
filter: (template) => templates.includes(template.filename)
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
nuxt.hook("builder:generateApp", async () => {
|
|
227
|
+
await regenerateImports();
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
function addDeclarationTemplates(ctx, options) {
|
|
232
|
+
const nuxt = useNuxt();
|
|
233
|
+
const stripExtension = (path) => path.replace(/\.[a-z]+$/, "");
|
|
234
|
+
const resolved = {};
|
|
235
|
+
const r = ({ from }) => {
|
|
236
|
+
if (resolved[from])
|
|
237
|
+
return resolved[from];
|
|
238
|
+
let path = resolveAlias(from);
|
|
239
|
+
if (isAbsolute(path))
|
|
240
|
+
path = relative(join(nuxt.options.buildDir, "types"), path);
|
|
241
|
+
path = stripExtension(path);
|
|
242
|
+
resolved[from] = path;
|
|
243
|
+
return path;
|
|
244
|
+
};
|
|
245
|
+
addTemplate({
|
|
246
|
+
filename: "imports.d.ts",
|
|
247
|
+
getContents: () => ctx.toExports(nuxt.options.buildDir)
|
|
248
|
+
});
|
|
249
|
+
addTemplate({
|
|
250
|
+
filename: "types/imports.d.ts",
|
|
251
|
+
getContents: async () => `// Generated by auto imports
|
|
252
|
+
${options.autoImport ? await ctx.generateTypeDeclarations({ resolvePath: r }) : "// Implicit auto importing is disabled, you can use explicitly import from `#imports` instead."}`
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async function resolveFiles(dir) {
|
|
257
|
+
const nuxt = useNuxt();
|
|
258
|
+
const dirs = [resolve(nuxt.options.srcDir, dir)];
|
|
259
|
+
const allRoutes = (await Promise.all(
|
|
260
|
+
dirs.map(async (dir2) => {
|
|
261
|
+
const files = await resolveFilesFlow(dir2, `**/*{${nuxt.options.extensions.join(",")}}`);
|
|
262
|
+
files.sort();
|
|
263
|
+
return files.filter((file) => {
|
|
264
|
+
if (file.includes(" copy"))
|
|
265
|
+
return false;
|
|
266
|
+
if (!fs.readFileSync(file, "utf8").includes("export"))
|
|
267
|
+
return false;
|
|
268
|
+
return true;
|
|
269
|
+
}).map((file) => {
|
|
270
|
+
const segments = relative(dir2, file).replace(new RegExp(`${escapeRE(extname(file))}$`), "").split("/").join("_");
|
|
271
|
+
return {
|
|
272
|
+
file,
|
|
273
|
+
name: camelCase(segments)
|
|
274
|
+
};
|
|
275
|
+
});
|
|
276
|
+
})
|
|
277
|
+
)).flat();
|
|
278
|
+
return allRoutes;
|
|
279
|
+
}
|
|
280
|
+
function normalizeExports(files) {
|
|
281
|
+
const imports = files.map((page) => genImport(page.file, page.name)).join("\n");
|
|
282
|
+
const exports = files.reduce((acc, curr) => {
|
|
283
|
+
const name = curr.name;
|
|
284
|
+
return `${name}:typeof ${name}==='function'?${name}():${name},${acc}`;
|
|
285
|
+
}, "");
|
|
286
|
+
return {
|
|
287
|
+
imports,
|
|
288
|
+
exports
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const pagesTypeTemplate = {
|
|
293
|
+
filename: "types/pages.d.ts",
|
|
294
|
+
getContents: ({ options }) => `// Generated by pages discovery
|
|
295
|
+
export {}
|
|
296
|
+
declare global {
|
|
297
|
+
type ArrElement<ArrType> = ArrType extends readonly (infer ElementType)[]
|
|
298
|
+
? ElementType
|
|
299
|
+
: never;
|
|
300
|
+
|
|
301
|
+
${options.pages.map((c) => {
|
|
302
|
+
const _type = `ArrElement<typeof ${genDynamicImport(isAbsolute(c.file) ? relative(join(options.buildDir, "types"), c.file) : c.file, { wrapper: false })}['default']>`;
|
|
303
|
+
return `export type ${pascalCase(c.name)}Context=Awaited<ReturnType<${_type}['locales']['es-ec']['context']>>`;
|
|
304
|
+
}).join("\n")}
|
|
305
|
+
}
|
|
306
|
+
export const pagesNames: string[]
|
|
307
|
+
`.replaceAll(".ts", "")
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const pagesModule = defineNuxtModule({
|
|
311
|
+
meta: {
|
|
312
|
+
name: "pages"
|
|
313
|
+
},
|
|
314
|
+
async setup(_options, flow) {
|
|
315
|
+
const runtimeDir = resolve(distDir, "pages/runtime");
|
|
316
|
+
flow.options.alias["#_pages"] = runtimeDir;
|
|
317
|
+
const pages = [];
|
|
318
|
+
const contexts = [];
|
|
319
|
+
const options = { pages, buildDir: flow.options.buildDir, contexts };
|
|
320
|
+
addTemplate({
|
|
321
|
+
...pagesTypeTemplate,
|
|
322
|
+
options
|
|
323
|
+
});
|
|
324
|
+
const pagesDirs = [{ path: resolve(flow.options.srcDir, flow.options.dir.pages) }];
|
|
325
|
+
flow.options.alias["#pages"] = resolve(flow.options.buildDir, "pages.mjs");
|
|
326
|
+
flow.options.alias["#pagesContexts"] = resolve(flow.options.buildDir, "pages.contexts.mjs");
|
|
327
|
+
addTemplate({
|
|
328
|
+
filename: "pages.mjs",
|
|
329
|
+
async getContents({ options: options2 }) {
|
|
330
|
+
const { exports, imports } = normalizeExports(options2.pages);
|
|
331
|
+
const module = [imports, `export default {${exports}}`].join("\n");
|
|
332
|
+
return module;
|
|
333
|
+
},
|
|
334
|
+
options
|
|
335
|
+
});
|
|
336
|
+
addTemplate({
|
|
337
|
+
filename: "pages.contexts.mjs",
|
|
338
|
+
async getContents({ options: options2 }) {
|
|
339
|
+
const { exports, imports } = normalizeExports(options2.contexts);
|
|
340
|
+
const module = [imports, `export default {${exports}}`].join("\n");
|
|
341
|
+
return module;
|
|
342
|
+
},
|
|
343
|
+
options
|
|
344
|
+
});
|
|
345
|
+
flow.hook("app:templates", async () => {
|
|
346
|
+
options.pages = await resolveFiles(flow.options.dir?.pages || "pages");
|
|
347
|
+
});
|
|
348
|
+
flow.hook("app:templates", async () => {
|
|
349
|
+
options.contexts = await resolveFiles("shared/contexts");
|
|
350
|
+
});
|
|
351
|
+
flow.hook("prepare:types", ({ references }) => {
|
|
352
|
+
references.push({ path: resolve(flow.options.buildDir, "types/pages.d.ts") });
|
|
353
|
+
});
|
|
354
|
+
flow.hook("builder:watch", async (event, path) => {
|
|
355
|
+
if (!["add", "unlink", "change"].includes(event))
|
|
356
|
+
return;
|
|
357
|
+
if (path.includes(" copy"))
|
|
358
|
+
return;
|
|
359
|
+
const fPath = resolve(flow.options.rootDir, path);
|
|
360
|
+
if (pagesDirs.find((dir) => fPath.startsWith(dir.path)))
|
|
361
|
+
await flow.callHook("builder:generateApp");
|
|
362
|
+
});
|
|
363
|
+
addPlugin(resolve(runtimeDir, "pages"));
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
const _require = createRequire(import.meta.url);
|
|
368
|
+
const ImportProtectionPlugin = createUnplugin((options) => {
|
|
369
|
+
const cache = {};
|
|
370
|
+
const importersToExclude = options?.exclude || [];
|
|
371
|
+
return {
|
|
372
|
+
name: "nuxt:import-protection",
|
|
373
|
+
enforce: "pre",
|
|
374
|
+
resolveId(id, importer) {
|
|
375
|
+
if (!importer)
|
|
376
|
+
return;
|
|
377
|
+
if (id.startsWith("."))
|
|
378
|
+
id = join(importer, "..", id);
|
|
379
|
+
if (isAbsolute(id))
|
|
380
|
+
id = relative(options.rootDir, id);
|
|
381
|
+
if (importersToExclude.some((p) => typeof p === "string" ? importer === p : p.test(importer)))
|
|
382
|
+
return;
|
|
383
|
+
const invalidImports = options.patterns.filter(([pattern]) => pattern instanceof RegExp ? pattern.test(id) : pattern === id);
|
|
384
|
+
let matched = false;
|
|
385
|
+
for (const match of invalidImports) {
|
|
386
|
+
cache[id] = cache[id] || /* @__PURE__ */ new Map();
|
|
387
|
+
const [pattern, warning] = match;
|
|
388
|
+
if (cache[id].has(pattern))
|
|
389
|
+
continue;
|
|
390
|
+
const relativeImporter = isAbsolute(importer) ? relative(options.rootDir, importer) : importer;
|
|
391
|
+
logger.error(warning || "Invalid import", `[importing \`${id}\` from \`${relativeImporter}\`]`);
|
|
392
|
+
cache[id].set(pattern, true);
|
|
393
|
+
matched = true;
|
|
394
|
+
}
|
|
395
|
+
if (matched)
|
|
396
|
+
return _require.resolve("unenv/runtime/mock/proxy");
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
async function initNitro(flow) {
|
|
403
|
+
const _nitroConfig = flow.options.nitro || {};
|
|
404
|
+
globalThis.generate = {};
|
|
405
|
+
const nitroConfig = defu(_nitroConfig, {
|
|
406
|
+
rootDir: flow.options.rootDir,
|
|
407
|
+
workspaceDir: flow.options.workspaceDir,
|
|
408
|
+
srcDir: flow.options.serverDir,
|
|
409
|
+
dev: flow.options.dev,
|
|
410
|
+
buildDir: flow.options.buildDir,
|
|
411
|
+
scanDirs: flow.options._layers.map((layer) => (layer.config.serverDir || layer.config.srcDir) && resolve(layer.cwd, layer.config.serverDir || resolve(layer.config.srcDir, "server"))).filter(Boolean),
|
|
412
|
+
renderer: resolve(distDir, "core/runtime/nitro/renderer"),
|
|
413
|
+
nodeModulesDirs: flow.options.modulesDir,
|
|
414
|
+
handlers: [
|
|
415
|
+
{
|
|
416
|
+
middleware: true,
|
|
417
|
+
handler: resolve(distDir, "core/runtime/nitro/flow")
|
|
418
|
+
},
|
|
419
|
+
...flow.options.serverHandlers
|
|
420
|
+
],
|
|
421
|
+
output: {
|
|
422
|
+
dir: resolve(flow.options.buildDir, "output")
|
|
423
|
+
},
|
|
424
|
+
devHandlers: [],
|
|
425
|
+
baseURL: flow.options.app.baseURL,
|
|
426
|
+
virtual: {},
|
|
427
|
+
runtimeConfig: {
|
|
428
|
+
...flow.options.runtimeConfig,
|
|
429
|
+
app: {
|
|
430
|
+
...flow.options.runtimeConfig.app,
|
|
431
|
+
baseURL: flow.options.dev ? "/" : flow.options.app.baseURL,
|
|
432
|
+
rootDir: flow.options.rootDir,
|
|
433
|
+
locale: flow.options.locale
|
|
434
|
+
},
|
|
435
|
+
nitro: {
|
|
436
|
+
envPrefix: "FLOW_",
|
|
437
|
+
...flow.options.runtimeConfig.nitro
|
|
438
|
+
},
|
|
439
|
+
generate: flow.options._generate
|
|
440
|
+
},
|
|
441
|
+
typescript: {
|
|
442
|
+
generateTsConfig: false
|
|
443
|
+
},
|
|
444
|
+
publicAssets: [
|
|
445
|
+
{
|
|
446
|
+
dir: resolve(flow.options.buildDir, "dist/client")
|
|
447
|
+
},
|
|
448
|
+
...flow.options._layers.map((layer) => join(layer.config.srcDir, layer.config.dir?.public || "public")).filter((dir) => existsSync(dir)).map((dir) => ({ dir }))
|
|
449
|
+
],
|
|
450
|
+
prerender: {
|
|
451
|
+
crawlLinks: flow.options._generate ?? void 0,
|
|
452
|
+
routes: [].concat(flow.options.generate.routes).concat(flow.options._generate ? ["/_urls", ...flow.options.generate.routes] : [])
|
|
453
|
+
},
|
|
454
|
+
sourceMap: flow.options.sourcemap.server,
|
|
455
|
+
externals: {
|
|
456
|
+
inline: [
|
|
457
|
+
...flow.options.dev ? [] : ["eta", "@monkeyplus/", "@vue/", "@nuxt/", flow.options.buildDir],
|
|
458
|
+
"@monkeyplus/flow/dist",
|
|
459
|
+
"C:/Users/gnu/Documents/GitHub/flow/packages/flow/dist/app"
|
|
460
|
+
]
|
|
461
|
+
},
|
|
462
|
+
alias: {
|
|
463
|
+
"estree-walker": "unenv/runtime/mock/proxy",
|
|
464
|
+
"@babel/parser": "unenv/runtime/mock/proxy",
|
|
465
|
+
"#paths": resolve(distDir, "core/runtime/nitro/paths"),
|
|
466
|
+
"#server": "#build/dist/server/server.mjs",
|
|
467
|
+
...flow.options.alias
|
|
468
|
+
},
|
|
469
|
+
rollupConfig: {
|
|
470
|
+
plugins: [],
|
|
471
|
+
external: [""]
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
nitroConfig.rollupConfig.plugins.push(
|
|
475
|
+
ImportProtectionPlugin.rollup({
|
|
476
|
+
rootDir: flow.options.rootDir,
|
|
477
|
+
patterns: [
|
|
478
|
+
...["#app", /^#build(\/|$)/].map((p) => [p, "Vue app aliases are not allowed in server routes."])
|
|
479
|
+
],
|
|
480
|
+
exclude: [/core[\\/]runtime[\\/]nitro[\\/]renderer/]
|
|
481
|
+
})
|
|
482
|
+
);
|
|
483
|
+
await flow.callHook("nitro:config", nitroConfig);
|
|
484
|
+
const nitro = await createNitro(nitroConfig);
|
|
485
|
+
flow._nitro = nitro;
|
|
486
|
+
flow.nitro = nitro;
|
|
487
|
+
await flow.callHook("nitro:init", nitro);
|
|
488
|
+
nitro.vfs = flow.vfs = nitro.vfs || flow.vfs || {};
|
|
489
|
+
flow.hook("close", () => nitro.hooks.callHook("close"));
|
|
490
|
+
nitro.hooks.hook("prerender:routes", (routes) => {
|
|
491
|
+
flow.callHook("prerender:routes", { routes });
|
|
492
|
+
});
|
|
493
|
+
const devMiddlewareHandler = dynamicEventHandler();
|
|
494
|
+
nitro.options.devHandlers.unshift({ handler: devMiddlewareHandler });
|
|
495
|
+
nitro.options.devHandlers.push(...flow.options.devServerHandlers);
|
|
496
|
+
nitro.options.handlers.unshift({
|
|
497
|
+
route: "/__nuxt_error",
|
|
498
|
+
lazy: true,
|
|
499
|
+
handler: resolve(distDir, "core/runtime/nitro/renderer")
|
|
500
|
+
});
|
|
501
|
+
flow.hook("prepare:types", async (opts) => {
|
|
502
|
+
if (!flow.options.dev) {
|
|
503
|
+
await scanHandlers(nitro);
|
|
504
|
+
await writeTypes(nitro);
|
|
505
|
+
}
|
|
506
|
+
opts.references.push({ path: resolve(flow.options.buildDir, "types/nitro.d.ts") });
|
|
507
|
+
});
|
|
508
|
+
flow.hook("build:done", async () => {
|
|
509
|
+
await flow.callHook("nitro:build:before", nitro);
|
|
510
|
+
if (flow.options.dev) {
|
|
511
|
+
await build$1(nitro);
|
|
512
|
+
} else {
|
|
513
|
+
await prepare(nitro);
|
|
514
|
+
await copyPublicAssets(nitro);
|
|
515
|
+
await prerender(nitro);
|
|
516
|
+
if (!flow.options._generate) {
|
|
517
|
+
await build$1(nitro);
|
|
518
|
+
} else {
|
|
519
|
+
const distDir2 = resolve(flow.options.rootDir, "dist");
|
|
520
|
+
if (!existsSync(distDir2))
|
|
521
|
+
await promises.symlink(nitro.options.output.publicDir, distDir2, "junction").catch(() => {
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
if (flow.options.dev) {
|
|
527
|
+
flow.hook("vite:compiled", () => {
|
|
528
|
+
flow.server.reload();
|
|
529
|
+
});
|
|
530
|
+
flow.hook("server:devHandler", (h) => {
|
|
531
|
+
devMiddlewareHandler.set(h);
|
|
532
|
+
});
|
|
533
|
+
flow.server = createDevServer(nitro);
|
|
534
|
+
const waitUntilCompile = new Promise((resolve2) => nitro.hooks.hook("compiled", () => resolve2()));
|
|
535
|
+
flow.hook("build:done", () => waitUntilCompile);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const addModuleTranspiles = (opts = {}) => {
|
|
540
|
+
const flow = useNuxt();
|
|
541
|
+
const modules = [
|
|
542
|
+
...opts.additionalModules || [],
|
|
543
|
+
...flow.options.buildModules,
|
|
544
|
+
...flow.options.modules,
|
|
545
|
+
...flow.options._modules
|
|
546
|
+
].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());
|
|
547
|
+
flow.options.build.transpile = flow.options.build.transpile.map((m) => typeof m === "string" ? m.split("node_modules/").pop() : m).filter((x) => !!x);
|
|
548
|
+
function isTranspilePresent(mod) {
|
|
549
|
+
return flow.options.build.transpile.some((t) => !(t instanceof Function) && (t instanceof RegExp ? t.test(mod) : new RegExp(t).test(mod)));
|
|
550
|
+
}
|
|
551
|
+
for (const module of modules) {
|
|
552
|
+
if (!isTranspilePresent(module))
|
|
553
|
+
flow.options.build.transpile.push(module);
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
function createFlow(options) {
|
|
558
|
+
const hooks = createHooks();
|
|
559
|
+
const flow = {
|
|
560
|
+
_version: "3.0.0-rc.106",
|
|
561
|
+
version,
|
|
562
|
+
options,
|
|
563
|
+
hooks,
|
|
564
|
+
callHook: hooks.callHook,
|
|
565
|
+
addHooks: hooks.addHooks,
|
|
566
|
+
hook: hooks.hook,
|
|
567
|
+
ready: () => initFlow(flow),
|
|
568
|
+
close: () => Promise.resolve(hooks.callHook("close", flow)),
|
|
569
|
+
vfs: {}
|
|
570
|
+
};
|
|
571
|
+
return flow;
|
|
572
|
+
}
|
|
573
|
+
async function initFlow(flow) {
|
|
574
|
+
flow.hooks.addHooks(flow.options.hooks);
|
|
575
|
+
nuxtCtx.set(flow);
|
|
576
|
+
flow.hook("close", () => nuxtCtx.unset());
|
|
577
|
+
await flow.callHook("modules:before");
|
|
578
|
+
const modulesToInstall = [
|
|
579
|
+
...flow.options.buildModules,
|
|
580
|
+
...flow.options.modules,
|
|
581
|
+
...flow.options._modules
|
|
582
|
+
];
|
|
583
|
+
flow.hooks.deprecateHooks({
|
|
584
|
+
"autoImports:sources": {
|
|
585
|
+
to: "imports:sources",
|
|
586
|
+
message: "`autoImports:sources` hook is deprecated. Use `addImportsSources()` from `@nuxt/kit` or `imports:dirs` with `nuxt>=3.0.0-rc.10`."
|
|
587
|
+
},
|
|
588
|
+
"autoImports:dirs": {
|
|
589
|
+
to: "imports:dirs",
|
|
590
|
+
message: "`autoImports:dirs` hook is deprecated. Use `addImportsDir()` from `@nuxt/kit` or `imports:dirs` with `nuxt>=3.0.0-rc.9`."
|
|
591
|
+
},
|
|
592
|
+
"autoImports:extend": {
|
|
593
|
+
to: "imports:extend",
|
|
594
|
+
message: "`autoImports:extend` hook is deprecated. Use `addImports()` from `@nuxt/kit` or `imports:extend` with `nuxt>=3.0.0-rc.9`."
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
for (const m of modulesToInstall) {
|
|
598
|
+
if (Array.isArray(m))
|
|
599
|
+
await installModule(m[0], m[1]);
|
|
600
|
+
else
|
|
601
|
+
await installModule(m, {});
|
|
602
|
+
}
|
|
603
|
+
await flow.callHook("modules:done");
|
|
604
|
+
flow.options.build.transpile = flow.options.build.transpile.map((t) => typeof t === "string" ? normalize(t) : t);
|
|
605
|
+
addModuleTranspiles();
|
|
606
|
+
await initNitro(flow);
|
|
607
|
+
await flow.callHook("ready", flow);
|
|
608
|
+
}
|
|
609
|
+
async function loadFlow(opts) {
|
|
610
|
+
const start1 = Date.now();
|
|
611
|
+
const options = await loadFlowConfig(opts);
|
|
612
|
+
logger.info(`Config loading in ${Date.now() - start1}ms`);
|
|
613
|
+
options.appDir = resolve(distDir, "app");
|
|
614
|
+
options.alias["#app"] = resolve(distDir, "app/index");
|
|
615
|
+
options._majorVersion = 3;
|
|
616
|
+
options._modules.push(pagesModule, metaModule);
|
|
617
|
+
options._modules.push([importsModule, {
|
|
618
|
+
transform: {
|
|
619
|
+
include: options._layers.filter((i) => i.cwd && i.cwd.includes("node_modules")).map((i) => new RegExp(`(^|\\/)${escapeRE(i.cwd.split("node_modules/").pop())}(\\/|$)(?!node_modules\\/)`))
|
|
620
|
+
}
|
|
621
|
+
}]);
|
|
622
|
+
options.modulesDir.push(resolve(options.workspaceDir, "node_modules"));
|
|
623
|
+
options.modulesDir.push(resolve(pkgDir, "node_modules"));
|
|
624
|
+
const flow = createFlow(options);
|
|
625
|
+
if (opts.ready !== false)
|
|
626
|
+
await flow.ready();
|
|
627
|
+
return flow;
|
|
628
|
+
}
|
|
629
|
+
function defineFlowConfig(config) {
|
|
630
|
+
return config;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const serverPluginTemplate = {
|
|
634
|
+
filename: "plugins/server.mjs",
|
|
635
|
+
getContents(ctx) {
|
|
636
|
+
const serverPlugins = ctx.app.plugins;
|
|
637
|
+
const exports = [];
|
|
638
|
+
const imports = [];
|
|
639
|
+
for (const plugin of serverPlugins) {
|
|
640
|
+
const path = relative(ctx.nuxt.options.rootDir, plugin.src);
|
|
641
|
+
const variable = `${genSafeVariableName(path).replace(/_(45|46|47)/g, "_")}_${hash(path)}`;
|
|
642
|
+
exports.push(variable);
|
|
643
|
+
imports.push(genImport(plugin.src, variable));
|
|
644
|
+
}
|
|
645
|
+
return [
|
|
646
|
+
...imports,
|
|
647
|
+
`export default ${genArrayFromRaw(exports)}`
|
|
648
|
+
].join("\n");
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
const pluginsDeclaration = {
|
|
652
|
+
filename: "types/plugins.d.ts",
|
|
653
|
+
getContents: (ctx) => {
|
|
654
|
+
const EXTENSION_RE = new RegExp(`(?<=\\w)(${ctx.nuxt.options.extensions.map((e) => escapeRE(e)).join("|")})$`, "g");
|
|
655
|
+
const tsImports = ctx.app.plugins.map((p) => (isAbsolute(p.src) ? relative(join(ctx.nuxt.options.buildDir, "types"), p.src) : p.src).replace(EXTENSION_RE, ""));
|
|
656
|
+
return `// Generated by Flow'
|
|
657
|
+
import type { Plugin } from '#app'
|
|
658
|
+
|
|
659
|
+
type Decorate<T extends Record<string, any>> = { [K in keyof T as K extends string ? \`$\${K}\` : never]: T[K] }
|
|
660
|
+
|
|
661
|
+
type InjectionType<A extends Plugin> = A extends Plugin<infer T> ? Decorate<T> : unknown
|
|
662
|
+
|
|
663
|
+
type FlowAppInjections =
|
|
664
|
+
${tsImports.map((p) => `InjectionType<typeof ${genDynamicImport(p, { wrapper: false })}.default>`).join(" &\n ")}
|
|
665
|
+
|
|
666
|
+
declare module '#app' {
|
|
667
|
+
interface FlowApp extends FlowAppInjections { }
|
|
668
|
+
}
|
|
669
|
+
// TODO: Insert extend types
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
export { }
|
|
673
|
+
`;
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
const adHocModules = ["imports", "meta", "pages"];
|
|
677
|
+
const schemaTemplate = {
|
|
678
|
+
filename: "types/schema.d.ts",
|
|
679
|
+
getContents: async ({ nuxt }) => {
|
|
680
|
+
const moduleInfo = nuxt.options._installedModules.map((m) => ({
|
|
681
|
+
...m.meta || {},
|
|
682
|
+
importName: m.entryPath || m.meta?.name
|
|
683
|
+
})).filter((m) => m.configKey && m.name && !adHocModules.includes(m.name));
|
|
684
|
+
const relativeRoot = relative(resolve(nuxt.options.buildDir, "types"), nuxt.options.rootDir);
|
|
685
|
+
return [
|
|
686
|
+
"import { FlowModule } from '@monkeyplus/flow-schema'",
|
|
687
|
+
"declare module '@monkeyplus/flow-schema' {",
|
|
688
|
+
" interface FlowConfig {",
|
|
689
|
+
...moduleInfo.filter(Boolean).map(
|
|
690
|
+
(meta) => ` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName.startsWith(".") ? `./${join(relativeRoot, meta.importName)}` : meta.importName, { wrapper: false })}.default extends FlowModule<infer O> ? Partial<O> : Record<string, any>`
|
|
691
|
+
),
|
|
692
|
+
" }",
|
|
693
|
+
generateTypes(
|
|
694
|
+
await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== "public"))),
|
|
695
|
+
{
|
|
696
|
+
interfaceName: "RuntimeConfig",
|
|
697
|
+
addExport: false,
|
|
698
|
+
addDefaults: false,
|
|
699
|
+
allowExtraKeys: false,
|
|
700
|
+
indentation: 2
|
|
701
|
+
}
|
|
702
|
+
),
|
|
703
|
+
generateTypes(
|
|
704
|
+
await resolveSchema(nuxt.options.runtimeConfig.public),
|
|
705
|
+
{
|
|
706
|
+
interfaceName: "PublicRuntimeConfig",
|
|
707
|
+
addExport: false,
|
|
708
|
+
addDefaults: false,
|
|
709
|
+
allowExtraKeys: false,
|
|
710
|
+
indentation: 2
|
|
711
|
+
}
|
|
712
|
+
),
|
|
713
|
+
"}"
|
|
714
|
+
].join("\n");
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
const publicPathTemplate = {
|
|
718
|
+
filename: "paths.mjs",
|
|
719
|
+
async getContents({ nuxt }) {
|
|
720
|
+
return [
|
|
721
|
+
`import { joinURL } from '${await _resolveId("ufo")}'`,
|
|
722
|
+
!nuxt.options.dev && "import { useRuntimeConfig } from '#internal/nitro'",
|
|
723
|
+
nuxt.options.dev ? `const appConfig = ${JSON.stringify(nuxt.options.app)}` : "const appConfig = useRuntimeConfig().app",
|
|
724
|
+
"export const baseURL = () => appConfig.baseURL",
|
|
725
|
+
"export const buildAssetsDir = () => appConfig.buildAssetsDir",
|
|
726
|
+
"export const buildAssetsURL = (...path) => joinURL(publicAssetsURL(), buildAssetsDir(), ...path)",
|
|
727
|
+
"export const publicAssetsURL = (...path) => {",
|
|
728
|
+
" const publicBase = appConfig.cdnURL || appConfig.baseURL",
|
|
729
|
+
" return path.length ? joinURL(publicBase, ...path) : publicBase",
|
|
730
|
+
"}"
|
|
731
|
+
].filter(Boolean).join("\n");
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
function _resolveId(id) {
|
|
735
|
+
return resolvePath(id, {
|
|
736
|
+
url: [
|
|
737
|
+
global.__NUXT_PREPATHS__,
|
|
738
|
+
import.meta.url,
|
|
739
|
+
process.cwd(),
|
|
740
|
+
global.__NUXT_PATHS__
|
|
741
|
+
]
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
const defaultTemplates = {
|
|
746
|
+
__proto__: null,
|
|
747
|
+
serverPluginTemplate: serverPluginTemplate,
|
|
748
|
+
pluginsDeclaration: pluginsDeclaration,
|
|
749
|
+
schemaTemplate: schemaTemplate,
|
|
750
|
+
publicPathTemplate: publicPathTemplate
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
function uniqueBy(arr, key) {
|
|
754
|
+
const res = [];
|
|
755
|
+
const seen = /* @__PURE__ */ new Set();
|
|
756
|
+
for (const item of arr) {
|
|
757
|
+
if (seen.has(item[key]))
|
|
758
|
+
continue;
|
|
759
|
+
seen.add(item[key]);
|
|
760
|
+
res.push(item);
|
|
761
|
+
}
|
|
762
|
+
return res;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function createApp(flow, options = {}) {
|
|
766
|
+
return defu(options, {
|
|
767
|
+
dir: flow.options.srcDir,
|
|
768
|
+
extensions: flow.options.extensions,
|
|
769
|
+
plugins: [],
|
|
770
|
+
templates: []
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
async function generateApp(flow, app, options) {
|
|
774
|
+
await resolveApp(flow, app);
|
|
775
|
+
app.templates = Object.values(defaultTemplates).concat(
|
|
776
|
+
flow.options.build.templates
|
|
777
|
+
);
|
|
778
|
+
await flow.callHook("app:templates", app);
|
|
779
|
+
app.templates = app.templates.map((tmpl) => normalizeTemplate(tmpl));
|
|
780
|
+
const templateContext = { utils: templateUtils, nuxt: flow, app };
|
|
781
|
+
await Promise.all(
|
|
782
|
+
app.templates.map(async (template) => {
|
|
783
|
+
const contents = await compileTemplate(template, templateContext);
|
|
784
|
+
const fullPath = template.dst || resolve(flow.options.buildDir, template.filename);
|
|
785
|
+
flow.vfs[fullPath] = contents;
|
|
786
|
+
const aliasPath = `#build/${template.filename.replace(/\.\w+$/, "")}`;
|
|
787
|
+
flow.vfs[aliasPath] = contents;
|
|
788
|
+
if (process.platform === "win32")
|
|
789
|
+
flow.vfs[fullPath.replace(/\//g, "\\")] = contents;
|
|
790
|
+
if (template.write) {
|
|
791
|
+
await promises.mkdir(dirname(fullPath), { recursive: true });
|
|
792
|
+
await promises.writeFile(fullPath, contents, "utf8");
|
|
793
|
+
}
|
|
794
|
+
})
|
|
795
|
+
);
|
|
796
|
+
await flow.callHook("app:templatesGenerated", app);
|
|
797
|
+
}
|
|
798
|
+
async function resolveApp(flow, app) {
|
|
799
|
+
app.plugins = [
|
|
800
|
+
...flow.options.plugins.map(normalizePlugin)
|
|
801
|
+
];
|
|
802
|
+
for (const config of flow.options._layers.map((layer) => layer.config)) {
|
|
803
|
+
app.plugins.push(...[
|
|
804
|
+
...config.plugins || [],
|
|
805
|
+
...config.srcDir ? await resolveFilesFlow(config.srcDir, [
|
|
806
|
+
`${config.dir?.plugins || "plugins"}/*.{ts,js,mjs,cjs,mts,cts}`,
|
|
807
|
+
`${config.dir?.plugins || "plugins"}/*/index.*{ts,js,mjs,cjs,mts,cts}`
|
|
808
|
+
]) : []
|
|
809
|
+
].map((plugin) => normalizePlugin(plugin)));
|
|
810
|
+
}
|
|
811
|
+
app.configs = [];
|
|
812
|
+
for (const config of flow.options._layers.map((layer) => layer.config)) {
|
|
813
|
+
const appConfigPath = await findPath(resolve(config.srcDir, "app.config"));
|
|
814
|
+
if (appConfigPath)
|
|
815
|
+
app.configs.push(appConfigPath);
|
|
816
|
+
}
|
|
817
|
+
await flow.callHook("app:resolve", app);
|
|
818
|
+
app.plugins = uniqueBy(app.plugins, "src");
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const PREFIX = "virtual:nuxt:";
|
|
822
|
+
function virtual(vfs) {
|
|
823
|
+
const extensions = ["", ".ts", ".vue", ".mjs", ".cjs", ".js", ".json"];
|
|
824
|
+
const resolveWithExt = (id) => {
|
|
825
|
+
for (const ext of extensions) {
|
|
826
|
+
const rId = id + ext;
|
|
827
|
+
if (rId in vfs)
|
|
828
|
+
return rId;
|
|
829
|
+
}
|
|
830
|
+
return null;
|
|
831
|
+
};
|
|
832
|
+
return {
|
|
833
|
+
name: "virtual",
|
|
834
|
+
resolveId(id, importer) {
|
|
835
|
+
if (process.platform === "win32" && isAbsolute(id)) {
|
|
836
|
+
id = resolve(id);
|
|
837
|
+
}
|
|
838
|
+
const resolvedId = resolveWithExt(id);
|
|
839
|
+
if (resolvedId)
|
|
840
|
+
return PREFIX + resolvedId;
|
|
841
|
+
if (importer && !isAbsolute(id)) {
|
|
842
|
+
const importerNoPrefix = importer.startsWith(PREFIX) ? importer.slice(PREFIX.length) : importer;
|
|
843
|
+
const importedDir = dirname(importerNoPrefix);
|
|
844
|
+
const resolved = resolveWithExt(join(importedDir, id));
|
|
845
|
+
if (resolved)
|
|
846
|
+
return PREFIX + resolved;
|
|
847
|
+
}
|
|
848
|
+
return null;
|
|
849
|
+
},
|
|
850
|
+
load(id) {
|
|
851
|
+
if (!id.startsWith(PREFIX))
|
|
852
|
+
return null;
|
|
853
|
+
const idNoPrefix = id.slice(PREFIX.length);
|
|
854
|
+
if (idNoPrefix in vfs) {
|
|
855
|
+
return {
|
|
856
|
+
code: vfs[idNoPrefix],
|
|
857
|
+
map: null
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
async function warmupViteServer(server, entries) {
|
|
865
|
+
const warmedUrls = /* @__PURE__ */ new Set();
|
|
866
|
+
const warmup = async (url) => {
|
|
867
|
+
if (warmedUrls.has(url))
|
|
868
|
+
return;
|
|
869
|
+
warmedUrls.add(url);
|
|
870
|
+
try {
|
|
871
|
+
await server.transformRequest(url);
|
|
872
|
+
} catch (e) {
|
|
873
|
+
logger.debug("Warmup for %s failed with: %s", url, e);
|
|
874
|
+
}
|
|
875
|
+
const mod = await server.moduleGraph.getModuleByUrl(url);
|
|
876
|
+
const deps = Array.from(mod?.importedModules || []);
|
|
877
|
+
await Promise.all(deps.map((m) => warmup(m.url.replace("/@id/__x00__", "\0"))));
|
|
878
|
+
};
|
|
879
|
+
await Promise.all(entries.map((entry) => warmup(entry)));
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const wpfs = {
|
|
883
|
+
...fse,
|
|
884
|
+
join
|
|
885
|
+
};
|
|
886
|
+
|
|
887
|
+
function cacheDirPlugin(rootDir, name) {
|
|
888
|
+
const optimizeCacheDir = resolve(rootDir, "node_modules/.cache/vite", name);
|
|
889
|
+
return {
|
|
890
|
+
name: "flow:cache-dir",
|
|
891
|
+
configResolved(resolvedConfig) {
|
|
892
|
+
resolvedConfig.optimizeCacheDir = optimizeCacheDir;
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
async function buildServer(ctx) {
|
|
898
|
+
const serverConfig = vite.mergeConfig(ctx.config, {
|
|
899
|
+
base: ctx.nuxt.options.dev ? joinURL(ctx.nuxt.options.app.baseURL.replace(/^\.\//, "/") || "/", ctx.nuxt.options.app.buildAssetsDir) : void 0,
|
|
900
|
+
define: {
|
|
901
|
+
"process.server": true,
|
|
902
|
+
"process.client": false,
|
|
903
|
+
"typeof window": '"undefined"',
|
|
904
|
+
"typeof document": '"undefined"',
|
|
905
|
+
"typeof navigator": '"undefined"',
|
|
906
|
+
"typeof location": '"undefined"',
|
|
907
|
+
"typeof XMLHttpRequest": '"undefined"'
|
|
908
|
+
},
|
|
909
|
+
resolve: {
|
|
910
|
+
alias: {
|
|
911
|
+
"#build/plugins": resolve(ctx.nuxt.options.buildDir, "plugins/server")
|
|
912
|
+
}
|
|
913
|
+
},
|
|
914
|
+
ssr: {
|
|
915
|
+
external: ["#internal/nitro", "#internal/nitro/utils"],
|
|
916
|
+
noExternal: [
|
|
917
|
+
...ctx.nuxt.options.build.transpile,
|
|
918
|
+
/\/esm\/.*\.js$/,
|
|
919
|
+
/\.(es|esm|esm-browser|esm-bundler).js$/,
|
|
920
|
+
"/__vue-jsx",
|
|
921
|
+
"#app",
|
|
922
|
+
/(nuxt|nuxt3)\/(dist|src|app)/,
|
|
923
|
+
/flow\/(dist|src|app)/,
|
|
924
|
+
/flow\/modules\/(content|icons|images|netlify|netlify-cms|seo|sitemap|vue)/,
|
|
925
|
+
/@monkeyplus\/flow\/(dist|src|app)/,
|
|
926
|
+
/@nuxt\/nitro\/(dist|src)/
|
|
927
|
+
]
|
|
928
|
+
},
|
|
929
|
+
build: {
|
|
930
|
+
outDir: resolve(ctx.nuxt.options.buildDir, "dist/server"),
|
|
931
|
+
ssr: true,
|
|
932
|
+
rollupOptions: {
|
|
933
|
+
external: ["#internal/nitro"],
|
|
934
|
+
output: {
|
|
935
|
+
entryFileNames: "server.mjs",
|
|
936
|
+
preferConst: true,
|
|
937
|
+
inlineDynamicImports: !ctx.nuxt.options.experimental.viteServerDynamicImports,
|
|
938
|
+
format: "module"
|
|
939
|
+
},
|
|
940
|
+
onwarn(warning, rollupWarn) {
|
|
941
|
+
if (!["UNUSED_EXTERNAL_IMPORT"].includes(warning.code))
|
|
942
|
+
rollupWarn(warning);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
},
|
|
946
|
+
server: {
|
|
947
|
+
preTransformRequests: false,
|
|
948
|
+
hmr: false
|
|
949
|
+
},
|
|
950
|
+
plugins: [
|
|
951
|
+
cacheDirPlugin(ctx.nuxt.options.rootDir, "server"),
|
|
952
|
+
replace({
|
|
953
|
+
...Object.fromEntries([";", "(", "{", "}", " ", " ", "\n"].map((d) => [`${d}global.`, `${d}globalThis.`])),
|
|
954
|
+
preventAssignment: true
|
|
955
|
+
})
|
|
956
|
+
]
|
|
957
|
+
});
|
|
958
|
+
if (ctx.nuxt.options.typescript.typeCheck === true || ctx.nuxt.options.typescript.typeCheck === "build" && !ctx.nuxt.options.dev) {
|
|
959
|
+
const checker = await import('vite-plugin-checker').then((r) => r.default);
|
|
960
|
+
serverConfig.plugins.push(checker({
|
|
961
|
+
vueTsc: {
|
|
962
|
+
tsconfigPath: await resolveTSConfig(ctx.nuxt.options.rootDir)
|
|
963
|
+
}
|
|
964
|
+
}));
|
|
965
|
+
}
|
|
966
|
+
await ctx.nuxt.callHook("vite:extendConfig", serverConfig, { isClient: false, isServer: true });
|
|
967
|
+
const onBuild = () => ctx.nuxt.callHook("build:resources", wpfs);
|
|
968
|
+
if (!ctx.nuxt.options.dev) {
|
|
969
|
+
const start = Date.now();
|
|
970
|
+
logger.info("Building server...");
|
|
971
|
+
await vite.build(serverConfig);
|
|
972
|
+
await onBuild();
|
|
973
|
+
logger.success(`Server built in ${Date.now() - start}ms`);
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
const viteServer = await vite.createServer(serverConfig);
|
|
977
|
+
ctx.ssrServer = viteServer;
|
|
978
|
+
await ctx.nuxt.callHook("vite:serverCreated", viteServer, { isClient: false, isServer: true });
|
|
979
|
+
ctx.nuxt.hook("close", () => viteServer.close());
|
|
980
|
+
await viteServer.pluginContainer.buildStart({});
|
|
981
|
+
await import('./chunks/dev-bundler.mjs').then((r) => r.initViteDevBundler(ctx, onBuild));
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
async function bundle$1(nuxt) {
|
|
985
|
+
const entry = resolve(nuxt.options.appDir, nuxt.options.experimental.asyncEntry ? "entry.async" : "entry");
|
|
986
|
+
const ctx = {
|
|
987
|
+
nuxt,
|
|
988
|
+
entry,
|
|
989
|
+
config: vite.mergeConfig(
|
|
990
|
+
{
|
|
991
|
+
configFile: false,
|
|
992
|
+
resolve: {
|
|
993
|
+
alias: {
|
|
994
|
+
...nuxt.options.alias,
|
|
995
|
+
"#app": nuxt.options.appDir,
|
|
996
|
+
"#build/plugins": "",
|
|
997
|
+
"#build": nuxt.options.buildDir,
|
|
998
|
+
"web-streams-polyfill/ponyfill/es2018": "unenv/runtime/mock/empty",
|
|
999
|
+
"abort-controller": "unenv/runtime/mock/empty"
|
|
1000
|
+
}
|
|
1001
|
+
},
|
|
1002
|
+
optimizeDeps: {
|
|
1003
|
+
entries: [entry],
|
|
1004
|
+
include: []
|
|
1005
|
+
},
|
|
1006
|
+
build: {
|
|
1007
|
+
rollupOptions: {
|
|
1008
|
+
output: { sanitizeFileName: sanitizeFilePath },
|
|
1009
|
+
input: resolve(nuxt.options.appDir, "entry")
|
|
1010
|
+
},
|
|
1011
|
+
watch: {
|
|
1012
|
+
exclude: nuxt.options.ignore
|
|
1013
|
+
}
|
|
1014
|
+
},
|
|
1015
|
+
plugins: [
|
|
1016
|
+
replace({
|
|
1017
|
+
...Object.fromEntries([";", "(", "{", "}", " ", " ", "\n"].map((d) => [`${d}global.`, `${d}globalThis.`])),
|
|
1018
|
+
preventAssignment: true
|
|
1019
|
+
}),
|
|
1020
|
+
virtual(nuxt.vfs)
|
|
1021
|
+
],
|
|
1022
|
+
vue: {
|
|
1023
|
+
reactivityTransform: nuxt.options.experimental.reactivityTransform
|
|
1024
|
+
},
|
|
1025
|
+
server: {
|
|
1026
|
+
watch: { ignored: isIgnoredFlow },
|
|
1027
|
+
fs: {
|
|
1028
|
+
allow: [
|
|
1029
|
+
nuxt.options.appDir
|
|
1030
|
+
]
|
|
1031
|
+
},
|
|
1032
|
+
hmr: false
|
|
1033
|
+
}
|
|
1034
|
+
},
|
|
1035
|
+
nuxt.options.vite
|
|
1036
|
+
)
|
|
1037
|
+
};
|
|
1038
|
+
if (!nuxt.options.dev) {
|
|
1039
|
+
ctx.config.server.watch = void 0;
|
|
1040
|
+
ctx.config.build.watch = void 0;
|
|
1041
|
+
}
|
|
1042
|
+
await nuxt.callHook("vite:extend", ctx);
|
|
1043
|
+
nuxt.hook("vite:serverCreated", (server, env) => {
|
|
1044
|
+
ctx.nuxt.hook("app:templatesGenerated", () => {
|
|
1045
|
+
for (const [id, mod] of server.moduleGraph.idToModuleMap) {
|
|
1046
|
+
if (id.includes("pages."))
|
|
1047
|
+
server.moduleGraph.invalidateModule(mod);
|
|
1048
|
+
if (id.startsWith("\virtual:"))
|
|
1049
|
+
server.moduleGraph.invalidateModule(mod);
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
const start = Date.now();
|
|
1053
|
+
warmupViteServer(server, [join("/@fs/", ctx.entry)]).then(() => logger.info(`Vite ${env.isClient ? "client" : "server"} warmed up in ${Date.now() - start}ms`)).catch(logger.error);
|
|
1054
|
+
});
|
|
1055
|
+
await buildServer(ctx);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
async function build(flow) {
|
|
1059
|
+
const app = createApp(flow);
|
|
1060
|
+
const generateApp$1 = debounce(() => generateApp(flow, app), void 0, { leading: true });
|
|
1061
|
+
await generateApp$1();
|
|
1062
|
+
if (flow.options.dev) {
|
|
1063
|
+
watch(flow);
|
|
1064
|
+
flow.hook("builder:watch", async (event, path) => {
|
|
1065
|
+
if (event !== "change" && /app|error|plugins/i.test(path))
|
|
1066
|
+
await generateApp$1();
|
|
1067
|
+
});
|
|
1068
|
+
flow.hook("builder:generateApp", (options) => {
|
|
1069
|
+
if (options)
|
|
1070
|
+
return generateApp(flow, app);
|
|
1071
|
+
return generateApp$1();
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
await flow.callHook("build:before");
|
|
1075
|
+
if (!flow.options._prepare) {
|
|
1076
|
+
await bundle(flow);
|
|
1077
|
+
await flow.callHook("build:done");
|
|
1078
|
+
}
|
|
1079
|
+
if (!flow.options.dev)
|
|
1080
|
+
await flow.callHook("close", flow);
|
|
1081
|
+
}
|
|
1082
|
+
function watch(flow) {
|
|
1083
|
+
const watcher = chokidar.watch(flow.options.srcDir, {
|
|
1084
|
+
...flow.options.watchers.chokidar,
|
|
1085
|
+
cwd: flow.options.srcDir,
|
|
1086
|
+
ignoreInitial: true,
|
|
1087
|
+
ignored: [
|
|
1088
|
+
isIgnoredFlow,
|
|
1089
|
+
".flow",
|
|
1090
|
+
"node_modules"
|
|
1091
|
+
]
|
|
1092
|
+
});
|
|
1093
|
+
watcher.on("all", (event, path) => flow.callHook("builder:watch", event, normalize(path)));
|
|
1094
|
+
flow.hook("close", () => watcher.close());
|
|
1095
|
+
return watcher;
|
|
1096
|
+
}
|
|
1097
|
+
async function bundle(nuxt) {
|
|
1098
|
+
try {
|
|
1099
|
+
return bundle$1(nuxt);
|
|
1100
|
+
} catch (error) {
|
|
1101
|
+
await nuxt.callHook("build:error", error);
|
|
1102
|
+
throw error;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
export { build, createFlow, defineFlowConfig, loadFlow };
|