@monkeyplus/flow 5.0.0-rc.233 → 5.0.0-rc.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/entry.d.ts +2 -2
- package/dist/app/entry.mjs +2 -3
- package/dist/app/flow.d.ts +1 -10
- package/dist/app/flow.mjs +0 -3
- package/dist/core/runtime/nitro/flow.d.ts +1 -2
- package/dist/core/runtime/nitro/flow.mjs +14 -8
- package/dist/core/runtime/nitro/renderer.d.ts +1 -1
- package/dist/core/runtime/nitro/renderer.mjs +14 -65
- package/dist/head/runtime/plugin.mjs +1 -0
- package/dist/index.mjs +759 -486
- package/dist/pages/runtime/helpers/index.d.ts +1 -1
- package/dist/pages/runtime/helpers/index.mjs +3 -14
- package/dist/pages/runtime/index.d.ts +3 -4
- package/dist/pages/runtime/index.mjs +1 -2
- package/dist/pages/runtime/pages.mjs +20 -66
- 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 +26 -0
- package/package.json +36 -40
- package/dist/app/entry.async.d.ts +0 -3
- package/dist/app/entry.async.mjs +0 -1
- package/dist/chunks/dev-bundler.mjs +0 -277
- package/dist/core/runtime/client.manifest.d.mts +0 -2
- package/dist/core/runtime/client.manifest.mjs +0 -6
- package/dist/core/runtime/vite-node-shared.d.mts +0 -1
- package/dist/core/runtime/vite-node-shared.d.ts +0 -8
- package/dist/core/runtime/vite-node-shared.mjs +0 -3
- package/dist/core/runtime/vite-node.d.mts +0 -2
- package/dist/core/runtime/vite-node.mjs +0 -41
package/dist/index.mjs
CHANGED
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
import { createHooks } from 'hookable';
|
|
2
2
|
import { dirname, resolve, normalize, join, isAbsolute, relative, extname } from 'pathe';
|
|
3
|
-
import { defineFlowModule, addPlugin, defineNuxtModule, logger, addTemplate, addPluginTemplate, addVitePlugin,
|
|
4
|
-
import
|
|
5
|
-
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
3
|
+
import { defineFlowModule, addPlugin, defineNuxtModule, logger, addTemplate, addPluginTemplate, addVitePlugin, useNuxt, resolveAlias, resolveFilesFlow, nuxtCtx, installModule, loadFlowConfig, templateUtils, normalizeTemplate, compileTemplate, normalizePlugin, isIgnoredFlow } from '@monkeyplus/flow-kit';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
6
5
|
import { defineUnimportPreset, createUnimport, toImports, scanDirExports } from 'unimport';
|
|
7
|
-
import
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
8
7
|
import { createUnplugin } from 'unplugin';
|
|
9
|
-
import { parseURL, parseQuery, joinURL } from 'ufo';
|
|
8
|
+
import { parseURL, parseQuery, joinURL, withoutTrailingSlash } from 'ufo';
|
|
10
9
|
import fs from 'fs';
|
|
10
|
+
import escapeRE from 'escape-string-regexp';
|
|
11
11
|
import { camelCase, pascalCase } from 'scule';
|
|
12
|
-
import { genImport, genDynamicImport,
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import
|
|
16
|
-
import {
|
|
17
|
-
import chokidar from 'chokidar';
|
|
12
|
+
import { genImport, genDynamicImport, genArrayFromRaw, genString, genObjectFromRawEntries } from 'knitwork';
|
|
13
|
+
import fse from 'fs-extra';
|
|
14
|
+
import logger$1 from 'consola';
|
|
15
|
+
import * as vite from 'vite';
|
|
16
|
+
import { createServer, build as build$1 } from 'vite';
|
|
18
17
|
import { debounce } from 'perfect-debounce';
|
|
18
|
+
import { existsSync, promises, readdirSync, statSync } from 'node:fs';
|
|
19
|
+
import { createNitro, scanHandlers, writeTypes, build as build$2, prepare, copyPublicAssets, prerender, createDevServer } from 'nitropack';
|
|
20
|
+
import defu from 'defu';
|
|
21
|
+
import { dynamicEventHandler, toEventHandler } from 'h3';
|
|
22
|
+
import { createRequire, builtinModules } from 'node:module';
|
|
23
|
+
import chokidar from 'chokidar';
|
|
19
24
|
import { generateTypes, resolveSchema } from 'untyped';
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import * as vite from 'vite';
|
|
25
|
+
import { getPort } from 'get-port-please';
|
|
26
|
+
import { sanitizeFilePath } from 'mlly';
|
|
23
27
|
import replace from '@rollup/plugin-replace';
|
|
24
|
-
import {
|
|
25
|
-
import
|
|
28
|
+
import { isExternal as isExternal$1, ExternalsDefaults } from 'externality';
|
|
29
|
+
import { createHash } from 'node:crypto';
|
|
30
|
+
import MagicString from 'magic-string';
|
|
26
31
|
|
|
27
|
-
const version = "5.0.0-rc.
|
|
32
|
+
const version = "5.0.0-rc.24";
|
|
28
33
|
|
|
29
34
|
let _distDir = dirname(fileURLToPath(import.meta.url));
|
|
30
|
-
if (_distDir.
|
|
35
|
+
if (_distDir.endsWith("chunks"))
|
|
31
36
|
_distDir = dirname(_distDir);
|
|
32
37
|
const distDir = _distDir;
|
|
33
38
|
const pkgDir = resolve(distDir, "..");
|
|
@@ -50,38 +55,35 @@ const metaModule = defineFlowModule({
|
|
|
50
55
|
|
|
51
56
|
const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }) => {
|
|
52
57
|
return {
|
|
53
|
-
name: "
|
|
58
|
+
name: "flow:auto-imports-transform",
|
|
54
59
|
enforce: "post",
|
|
55
60
|
transformInclude(id) {
|
|
56
61
|
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href));
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
62
|
+
const { type, macro } = parseQuery(search);
|
|
63
|
+
const exclude = options.transform?.exclude || [/[\\/]node_modules[\\/]/];
|
|
64
|
+
const include = options.transform?.include || [];
|
|
65
|
+
if (exclude.some((pattern) => id.match(pattern)))
|
|
61
66
|
return false;
|
|
62
|
-
if (
|
|
67
|
+
if (include.some((pattern) => id.match(pattern)))
|
|
68
|
+
return true;
|
|
69
|
+
if (pathname.endsWith(".vue") && (type === "template" || type === "script" || macro || !search))
|
|
63
70
|
return true;
|
|
64
71
|
if (pathname.match(/\.((c|m)?j|t)sx?$/g))
|
|
65
72
|
return true;
|
|
66
73
|
},
|
|
67
|
-
async transform(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (isNodeModule && !code.match(/(['"])#imports\1/))
|
|
74
|
+
async transform(_code, id) {
|
|
75
|
+
const { code, s } = await ctx.injectImports(_code);
|
|
76
|
+
if (code === _code)
|
|
71
77
|
return;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
map: sourcemap ? s.generateMap({ source: id, includeContent: true }) : void 0
|
|
77
|
-
};
|
|
78
|
-
}
|
|
78
|
+
return {
|
|
79
|
+
code,
|
|
80
|
+
map: sourcemap && s.generateMap({ source: id, includeContent: true })
|
|
81
|
+
};
|
|
79
82
|
}
|
|
80
83
|
};
|
|
81
84
|
});
|
|
82
85
|
|
|
83
86
|
const commonPresets = [
|
|
84
|
-
// #head
|
|
85
87
|
defineUnimportPreset({
|
|
86
88
|
from: "#head",
|
|
87
89
|
imports: [
|
|
@@ -92,7 +94,7 @@ const commonPresets = [
|
|
|
92
94
|
from: "#_pages",
|
|
93
95
|
imports: [
|
|
94
96
|
"definePage",
|
|
95
|
-
"
|
|
97
|
+
"defineDinamycPage",
|
|
96
98
|
"defineSharedContext"
|
|
97
99
|
]
|
|
98
100
|
})
|
|
@@ -112,12 +114,10 @@ const appPreset = defineUnimportPreset({
|
|
|
112
114
|
const vuePreset = defineUnimportPreset({
|
|
113
115
|
from: "vue",
|
|
114
116
|
imports: [
|
|
115
|
-
// Components
|
|
116
117
|
"defineComponent",
|
|
117
118
|
"getCurrentInstance",
|
|
118
119
|
"useSlots",
|
|
119
120
|
"h",
|
|
120
|
-
// Reactivity
|
|
121
121
|
"computed"
|
|
122
122
|
]
|
|
123
123
|
});
|
|
@@ -127,65 +127,50 @@ const defaultPresets = [
|
|
|
127
127
|
vuePreset
|
|
128
128
|
];
|
|
129
129
|
|
|
130
|
-
const
|
|
130
|
+
const autoImportsModule = defineNuxtModule({
|
|
131
131
|
meta: {
|
|
132
|
-
name: "imports",
|
|
133
|
-
configKey: "
|
|
132
|
+
name: "auto-imports",
|
|
133
|
+
configKey: "autoImports"
|
|
134
134
|
},
|
|
135
135
|
defaults: {
|
|
136
|
-
autoImport: true,
|
|
137
136
|
presets: defaultPresets,
|
|
138
137
|
global: false,
|
|
139
138
|
imports: [],
|
|
140
139
|
dirs: [],
|
|
141
140
|
transform: {
|
|
142
|
-
include: [],
|
|
143
141
|
exclude: void 0
|
|
144
142
|
}
|
|
145
143
|
},
|
|
146
|
-
async setup(options,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
options = defu(nuxt.options.autoImports, options);
|
|
150
|
-
}
|
|
151
|
-
await nuxt.callHook("imports:sources", options.presets);
|
|
152
|
-
options.presets?.forEach((_i) => {
|
|
153
|
-
const i = _i;
|
|
144
|
+
async setup(options, flow) {
|
|
145
|
+
await flow.callHook("autoImports:sources", options.presets);
|
|
146
|
+
options.presets.forEach((i) => {
|
|
154
147
|
if (typeof i !== "string" && i.names && !i.imports) {
|
|
155
148
|
i.imports = i.names;
|
|
156
|
-
logger.warn("imports: presets.names is deprecated, use presets.imports instead");
|
|
149
|
+
logger.warn("auto-imports: presets.names is deprecated, use presets.imports instead");
|
|
157
150
|
}
|
|
158
151
|
});
|
|
159
152
|
const ctx = createUnimport({
|
|
160
153
|
presets: options.presets,
|
|
161
|
-
imports: options.imports
|
|
162
|
-
virtualImports: ["#imports"],
|
|
163
|
-
addons: {
|
|
164
|
-
vueTemplate: options.autoImport
|
|
165
|
-
}
|
|
154
|
+
imports: options.imports
|
|
166
155
|
});
|
|
167
156
|
let composablesDirs = [];
|
|
168
|
-
for (const layer of
|
|
157
|
+
for (const layer of flow.options._layers) {
|
|
169
158
|
composablesDirs.push(resolve(layer.config.srcDir, "composables"));
|
|
170
|
-
for (const dir of layer.config.
|
|
171
|
-
if (!dir)
|
|
172
|
-
continue;
|
|
159
|
+
for (const dir of layer.config.autoImports?.dirs ?? [])
|
|
173
160
|
composablesDirs.push(resolve(layer.config.srcDir, dir));
|
|
174
|
-
}
|
|
175
161
|
}
|
|
176
|
-
await
|
|
162
|
+
await flow.callHook("autoImports:dirs", composablesDirs);
|
|
177
163
|
composablesDirs = composablesDirs.map((dir) => normalize(dir));
|
|
178
164
|
addTemplate({
|
|
179
165
|
filename: "imports.mjs",
|
|
180
|
-
getContents:
|
|
181
|
-
if (process.dev) { console.warn("[nuxt] \`#imports\` should be transformed with real imports. There seems to be something wrong with the imports plugin.") }`
|
|
166
|
+
getContents: () => ctx.toExports()
|
|
182
167
|
});
|
|
183
|
-
|
|
184
|
-
if (
|
|
168
|
+
flow.options.alias["#imports"] = join(flow.options.buildDir, "imports");
|
|
169
|
+
if (flow.options.dev && options.global) {
|
|
185
170
|
addPluginTemplate({
|
|
186
|
-
filename: "imports.mjs",
|
|
187
|
-
getContents:
|
|
188
|
-
const imports =
|
|
171
|
+
filename: "auto-imports.mjs",
|
|
172
|
+
getContents: () => {
|
|
173
|
+
const imports = ctx.getImports();
|
|
189
174
|
const importStatement = toImports(imports);
|
|
190
175
|
const globalThisSet = imports.map((i) => `globalThis.${i.as} = ${i.as};`).join("\n");
|
|
191
176
|
return `${importStatement}
|
|
@@ -196,41 +181,32 @@ export default () => {};`;
|
|
|
196
181
|
}
|
|
197
182
|
});
|
|
198
183
|
} else {
|
|
199
|
-
addVitePlugin(TransformPlugin.vite({ ctx, options, sourcemap:
|
|
200
|
-
addWebpackPlugin(TransformPlugin.webpack({ ctx, options, sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client }));
|
|
184
|
+
addVitePlugin(TransformPlugin.vite({ ctx, options, sourcemap: flow.options.sourcemap }));
|
|
201
185
|
}
|
|
202
|
-
const
|
|
186
|
+
const regenerateAutoImports = async () => {
|
|
203
187
|
ctx.clearDynamicImports();
|
|
204
188
|
await ctx.modifyDynamicImports(async (imports) => {
|
|
205
189
|
imports.push(...await scanDirExports(composablesDirs));
|
|
206
|
-
await
|
|
190
|
+
await flow.callHook("autoImports:extend", imports);
|
|
207
191
|
});
|
|
208
192
|
};
|
|
209
|
-
await
|
|
210
|
-
addDeclarationTemplates(ctx
|
|
211
|
-
|
|
212
|
-
references.push({ path: resolve(
|
|
213
|
-
references.push({ path: resolve(
|
|
193
|
+
await regenerateAutoImports();
|
|
194
|
+
addDeclarationTemplates(ctx);
|
|
195
|
+
flow.hook("prepare:types", ({ references }) => {
|
|
196
|
+
references.push({ path: resolve(flow.options.buildDir, "types/auto-imports.d.ts") });
|
|
197
|
+
references.push({ path: resolve(flow.options.buildDir, "imports.d.ts") });
|
|
214
198
|
});
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
];
|
|
220
|
-
nuxt.hook("builder:watch", async (_, path) => {
|
|
221
|
-
const _resolved = resolve(nuxt.options.srcDir, path);
|
|
222
|
-
if (composablesDirs.find((dir) => _resolved.startsWith(dir))) {
|
|
223
|
-
await updateTemplates({
|
|
224
|
-
filter: (template) => templates.includes(template.filename)
|
|
225
|
-
});
|
|
226
|
-
}
|
|
199
|
+
flow.hook("builder:watch", async (_, path) => {
|
|
200
|
+
const _resolved = resolve(flow.options.srcDir, path);
|
|
201
|
+
if (composablesDirs.find((dir) => _resolved.startsWith(dir)))
|
|
202
|
+
await flow.callHook("builder:generateApp");
|
|
227
203
|
});
|
|
228
|
-
|
|
229
|
-
await
|
|
204
|
+
flow.hook("builder:generateApp", async () => {
|
|
205
|
+
await regenerateAutoImports();
|
|
230
206
|
});
|
|
231
207
|
}
|
|
232
208
|
});
|
|
233
|
-
function addDeclarationTemplates(ctx
|
|
209
|
+
function addDeclarationTemplates(ctx) {
|
|
234
210
|
const nuxt = useNuxt();
|
|
235
211
|
const stripExtension = (path) => path.replace(/\.[a-z]+$/, "");
|
|
236
212
|
const resolved = {};
|
|
@@ -246,37 +222,35 @@ function addDeclarationTemplates(ctx, options) {
|
|
|
246
222
|
};
|
|
247
223
|
addTemplate({
|
|
248
224
|
filename: "imports.d.ts",
|
|
249
|
-
getContents: () => ctx.toExports(
|
|
225
|
+
getContents: () => ctx.toExports()
|
|
250
226
|
});
|
|
251
227
|
addTemplate({
|
|
252
|
-
filename: "types/imports.d.ts",
|
|
253
|
-
getContents:
|
|
254
|
-
${
|
|
228
|
+
filename: "types/auto-imports.d.ts",
|
|
229
|
+
getContents: () => `// Generated by auto imports
|
|
230
|
+
${ctx.generateTypeDecarations({ resolvePath: r })}`
|
|
255
231
|
});
|
|
256
232
|
}
|
|
257
233
|
|
|
258
234
|
async function resolveFiles(dir) {
|
|
259
235
|
const nuxt = useNuxt();
|
|
260
236
|
const dirs = [resolve(nuxt.options.srcDir, dir)];
|
|
261
|
-
const allRoutes = (await Promise.all(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
})
|
|
279
|
-
)).flat();
|
|
237
|
+
const allRoutes = (await Promise.all(dirs.map(async (dir2) => {
|
|
238
|
+
const files = await resolveFilesFlow(dir2, `**/*{${nuxt.options.extensions.join(",")}}`);
|
|
239
|
+
files.sort();
|
|
240
|
+
return files.filter((file) => {
|
|
241
|
+
if (file.includes(" copy"))
|
|
242
|
+
return false;
|
|
243
|
+
if (!fs.readFileSync(file, "utf8").includes("export"))
|
|
244
|
+
return false;
|
|
245
|
+
return true;
|
|
246
|
+
}).map((file) => {
|
|
247
|
+
const segments = relative(dir2, file).replace(new RegExp(`${escapeRE(extname(file))}$`), "").split("/").join("_");
|
|
248
|
+
return {
|
|
249
|
+
file,
|
|
250
|
+
name: camelCase(segments)
|
|
251
|
+
};
|
|
252
|
+
});
|
|
253
|
+
}))).flat();
|
|
280
254
|
return allRoutes;
|
|
281
255
|
}
|
|
282
256
|
function normalizeExports(files) {
|
|
@@ -366,24 +340,120 @@ const pagesModule = defineNuxtModule({
|
|
|
366
340
|
}
|
|
367
341
|
});
|
|
368
342
|
|
|
343
|
+
const createClient = async (flow) => {
|
|
344
|
+
let vite;
|
|
345
|
+
if (globalThis.viteClient) {
|
|
346
|
+
vite = globalThis.viteClient;
|
|
347
|
+
} else {
|
|
348
|
+
vite = await createServer({
|
|
349
|
+
root: resolve(flow.options.rootDir),
|
|
350
|
+
base: "/_vite/",
|
|
351
|
+
build: {
|
|
352
|
+
manifest: true
|
|
353
|
+
},
|
|
354
|
+
server: {
|
|
355
|
+
watch: {
|
|
356
|
+
ignored: ["**/.env/**", "**/.env*"]
|
|
357
|
+
},
|
|
358
|
+
middlewareMode: "ssr"
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
globalThis.viteClient = vite;
|
|
362
|
+
}
|
|
363
|
+
const _doReload = () => {
|
|
364
|
+
if (vite)
|
|
365
|
+
vite?.ws?.send({ type: "full-reload" });
|
|
366
|
+
};
|
|
367
|
+
const doReload = debounce(_doReload, 75);
|
|
368
|
+
flow.hook("bundler:change", () => {
|
|
369
|
+
doReload();
|
|
370
|
+
});
|
|
371
|
+
flow.hook("close", async () => {
|
|
372
|
+
vite.restart();
|
|
373
|
+
});
|
|
374
|
+
return vite;
|
|
375
|
+
};
|
|
376
|
+
const builClient = async (flow) => {
|
|
377
|
+
return await build$1({
|
|
378
|
+
root: flow.options.rootDir,
|
|
379
|
+
mode: "production",
|
|
380
|
+
build: {
|
|
381
|
+
assetsDir: "scripts",
|
|
382
|
+
target: "es2017",
|
|
383
|
+
outDir: ".vite",
|
|
384
|
+
manifest: true
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const viteModule = defineFlowModule({
|
|
390
|
+
meta: {
|
|
391
|
+
name: "vite-client",
|
|
392
|
+
configKey: "bundle"
|
|
393
|
+
},
|
|
394
|
+
defaults: {
|
|
395
|
+
route: "/_vite/",
|
|
396
|
+
dir: "/client/pages"
|
|
397
|
+
},
|
|
398
|
+
async setup(_options, flow) {
|
|
399
|
+
const runtimeDir = resolve(distDir, "vite-client/runtime");
|
|
400
|
+
flow.options.alias["#viteManifest"] = resolve(flow.options.buildDir, "viteManifest.mjs");
|
|
401
|
+
let vite;
|
|
402
|
+
if (flow.options.dev) {
|
|
403
|
+
flow.hook("nitro:init", async (nitro) => {
|
|
404
|
+
vite = await createClient(flow);
|
|
405
|
+
nitro.options.devHandlers.push({
|
|
406
|
+
handler: vite.middlewares,
|
|
407
|
+
route: _options.route
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
addTemplate({
|
|
411
|
+
filename: "viteManifest.mjs",
|
|
412
|
+
async getContents() {
|
|
413
|
+
return [
|
|
414
|
+
"export default {",
|
|
415
|
+
`head:()=>'<script type="module" src="${joinURL("/", _options.route, "/@vite/client")}"><\/script>'`,
|
|
416
|
+
",",
|
|
417
|
+
`body: (bundle)=>\`<script type="module" src="${joinURL("/", _options.route, _options.dir)}/\${bundle}.ts"><\/script>\``,
|
|
418
|
+
"}"
|
|
419
|
+
].join("\n");
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
} else {
|
|
423
|
+
const file = resolve(flow.options.rootDir, ".vite/manifest.json");
|
|
424
|
+
addTemplate({
|
|
425
|
+
filename: "viteManifest.mjs",
|
|
426
|
+
async getContents() {
|
|
427
|
+
return [
|
|
428
|
+
"import fs from 'fs';",
|
|
429
|
+
`export default ()=>JSON.parse(fs.readFileSync("${file}", 'utf8'))`
|
|
430
|
+
].join("\n");
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
flow.hook("build:before", async () => {
|
|
434
|
+
const start = Date.now();
|
|
435
|
+
logger$1.info("Building client...");
|
|
436
|
+
await builClient(flow);
|
|
437
|
+
logger$1.success(`Client build in ${Date.now() - start}ms`);
|
|
438
|
+
});
|
|
439
|
+
flow.hook("generate:before", async () => {
|
|
440
|
+
const files = resolve(flow.options.rootDir, ".vite/scripts");
|
|
441
|
+
await fse.copy(files, resolve(flow.options.generate.dir, "assets"));
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
addPlugin({ src: resolve(runtimeDir, "plugin") });
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
|
|
369
448
|
const _require = createRequire(import.meta.url);
|
|
370
449
|
const ImportProtectionPlugin = createUnplugin((options) => {
|
|
371
450
|
const cache = {};
|
|
372
|
-
const importersToExclude = options?.exclude || [];
|
|
373
451
|
return {
|
|
374
|
-
name: "
|
|
452
|
+
name: "flow:import-protection",
|
|
375
453
|
enforce: "pre",
|
|
376
454
|
resolveId(id, importer) {
|
|
377
|
-
if (!importer)
|
|
378
|
-
return;
|
|
379
|
-
if (id.startsWith("."))
|
|
380
|
-
id = join(importer, "..", id);
|
|
381
|
-
if (isAbsolute(id))
|
|
382
|
-
id = relative(options.rootDir, id);
|
|
383
|
-
if (importersToExclude.some((p) => typeof p === "string" ? importer === p : p.test(importer)))
|
|
384
|
-
return;
|
|
385
455
|
const invalidImports = options.patterns.filter(([pattern]) => pattern instanceof RegExp ? pattern.test(id) : pattern === id);
|
|
386
|
-
let matched
|
|
456
|
+
let matched;
|
|
387
457
|
for (const match of invalidImports) {
|
|
388
458
|
cache[id] = cache[id] || /* @__PURE__ */ new Map();
|
|
389
459
|
const [pattern, warning] = match;
|
|
@@ -402,38 +472,25 @@ const ImportProtectionPlugin = createUnplugin((options) => {
|
|
|
402
472
|
});
|
|
403
473
|
|
|
404
474
|
async function initNitro(flow) {
|
|
475
|
+
const { handlers, devHandlers } = await resolveHandlers(flow);
|
|
405
476
|
const _nitroConfig = flow.options.nitro || {};
|
|
406
477
|
globalThis.generate = {};
|
|
407
|
-
const 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);
|
|
408
478
|
const nitroConfig = defu(_nitroConfig, {
|
|
409
479
|
rootDir: flow.options.rootDir,
|
|
410
|
-
|
|
411
|
-
srcDir: flow.options.serverDir,
|
|
480
|
+
srcDir: join(flow.options.srcDir, "server"),
|
|
412
481
|
dev: flow.options.dev,
|
|
482
|
+
preset: flow.options.dev ? "nitro-dev" : void 0,
|
|
413
483
|
buildDir: flow.options.buildDir,
|
|
414
|
-
scanDirs,
|
|
484
|
+
scanDirs: flow.options._layers.map((layer) => join(layer.config.srcDir, "server")),
|
|
415
485
|
renderer: resolve(distDir, "core/runtime/nitro/renderer"),
|
|
416
|
-
// errorHandler: resolve(distDir, 'core/runtime/nitro/error'),
|
|
417
486
|
nodeModulesDirs: flow.options.modulesDir,
|
|
418
|
-
handlers
|
|
419
|
-
//* Important middleware
|
|
420
|
-
{
|
|
421
|
-
middleware: true,
|
|
422
|
-
handler: resolve(distDir, "core/runtime/nitro/flow")
|
|
423
|
-
},
|
|
424
|
-
...flow.options.serverHandlers
|
|
425
|
-
],
|
|
426
|
-
output: {
|
|
427
|
-
dir: resolve(flow.options.buildDir, "output")
|
|
428
|
-
},
|
|
487
|
+
handlers,
|
|
429
488
|
devHandlers: [],
|
|
430
489
|
baseURL: flow.options.app.baseURL,
|
|
431
|
-
virtual: {},
|
|
432
490
|
runtimeConfig: {
|
|
433
491
|
...flow.options.runtimeConfig,
|
|
434
492
|
app: {
|
|
435
493
|
...flow.options.runtimeConfig.app,
|
|
436
|
-
baseURL: flow.options.dev ? "/" : flow.options.app.baseURL,
|
|
437
494
|
rootDir: flow.options.rootDir,
|
|
438
495
|
locale: flow.options.locale
|
|
439
496
|
},
|
|
@@ -448,81 +505,62 @@ async function initNitro(flow) {
|
|
|
448
505
|
},
|
|
449
506
|
publicAssets: [
|
|
450
507
|
{
|
|
451
|
-
|
|
508
|
+
baseURL: flow.options.app.buildAssetsDir,
|
|
452
509
|
dir: resolve(flow.options.buildDir, "dist/client")
|
|
453
510
|
},
|
|
454
511
|
...flow.options._layers.map((layer) => join(layer.config.srcDir, layer.config.dir?.public || "public")).filter((dir) => existsSync(dir)).map((dir) => ({ dir }))
|
|
455
512
|
],
|
|
456
513
|
prerender: {
|
|
457
|
-
crawlLinks: flow.options._generate
|
|
458
|
-
routes: [].concat(flow.options.
|
|
514
|
+
crawlLinks: flow.options._generate ? flow.options.generate.crawler : false,
|
|
515
|
+
routes: [].concat(flow.options._generate ? ["/_urls", ...flow.options.generate.routes] : [])
|
|
459
516
|
},
|
|
460
|
-
|
|
517
|
+
sourcemap: flow.options.sourcemap,
|
|
461
518
|
externals: {
|
|
462
519
|
inline: [
|
|
463
520
|
...flow.options.dev ? [] : ["eta", "@monkeyplus/", "@vue/", "@nuxt/", flow.options.buildDir],
|
|
464
|
-
"@monkeyplus/flow/dist"
|
|
465
|
-
// Dev windows
|
|
466
|
-
"C:/Users/gnu/Documents/GitHub/flow/packages/flow/dist/app",
|
|
467
|
-
distDir
|
|
521
|
+
"@monkeyplus/flow/dist"
|
|
468
522
|
]
|
|
469
523
|
},
|
|
470
|
-
// preset: flow.options.dev ? 'nitro-dev' : undefined,
|
|
471
|
-
// output: {
|
|
472
|
-
// dir: resolve(flow.options.buildDir, 'output'),
|
|
473
|
-
// },
|
|
474
|
-
replace: {
|
|
475
|
-
"process.dev": flow.options.dev
|
|
476
|
-
},
|
|
477
524
|
alias: {
|
|
478
|
-
// Vue 3 mocks
|
|
479
525
|
"estree-walker": "unenv/runtime/mock/proxy",
|
|
480
526
|
"@babel/parser": "unenv/runtime/mock/proxy",
|
|
481
|
-
// Paths
|
|
482
527
|
"#paths": resolve(distDir, "core/runtime/nitro/paths"),
|
|
483
|
-
// Shortcut to server main file
|
|
484
528
|
"#server": "#build/dist/server/server.mjs",
|
|
485
|
-
// Nuxt aliases
|
|
486
529
|
...flow.options.alias
|
|
487
530
|
},
|
|
488
531
|
rollupConfig: {
|
|
489
|
-
plugins: []
|
|
490
|
-
|
|
491
|
-
// watchOptions:{}
|
|
492
|
-
//* Include to refresh server
|
|
493
|
-
devServer: {
|
|
494
|
-
watch: [resolve(flow.options.buildDir, "dist/server/server.mjs")]
|
|
532
|
+
plugins: [],
|
|
533
|
+
external: [""]
|
|
495
534
|
}
|
|
496
535
|
});
|
|
497
|
-
nitroConfig.rollupConfig.plugins.push(
|
|
498
|
-
ImportProtectionPlugin.rollup({
|
|
499
|
-
rootDir: flow.options.rootDir,
|
|
500
|
-
patterns: [
|
|
501
|
-
...["#app", /^#build(\/|$)/].map((p) => [p, "Vue app aliases are not allowed in server routes."])
|
|
502
|
-
],
|
|
503
|
-
exclude: [/core[\\/]runtime[\\/]nitro[\\/]renderer/]
|
|
504
|
-
})
|
|
505
|
-
);
|
|
506
536
|
await flow.callHook("nitro:config", nitroConfig);
|
|
537
|
+
nitroConfig.handlers.unshift({
|
|
538
|
+
middleware: true,
|
|
539
|
+
handler: resolve(distDir, "core/runtime/nitro/flow")
|
|
540
|
+
});
|
|
507
541
|
const nitro = await createNitro(nitroConfig);
|
|
508
|
-
flow._nitro = nitro;
|
|
509
|
-
flow.nitro = nitro;
|
|
510
542
|
await flow.callHook("nitro:init", nitro);
|
|
511
543
|
nitro.vfs = flow.vfs = nitro.vfs || flow.vfs || {};
|
|
512
544
|
flow.hook("close", () => nitro.hooks.callHook("close"));
|
|
513
|
-
nitro.hooks.hook("
|
|
514
|
-
|
|
545
|
+
nitro.hooks.hook("rollup:before", (nitro2) => {
|
|
546
|
+
const plugin = ImportProtectionPlugin.rollup({
|
|
547
|
+
rootDir: flow.options.rootDir,
|
|
548
|
+
patterns: [
|
|
549
|
+
...["#app", /^#build(\/|$)/].map((p) => [p, "Flow app aliases are not allowed in server routes."])
|
|
550
|
+
]
|
|
551
|
+
});
|
|
552
|
+
nitro2.options.rollupConfig.plugins.push(plugin);
|
|
515
553
|
});
|
|
516
|
-
const
|
|
517
|
-
nitro.options.devHandlers.unshift({ handler:
|
|
518
|
-
nitro.options.devHandlers.push(...
|
|
554
|
+
const devMidlewareHandler = dynamicEventHandler();
|
|
555
|
+
nitro.options.devHandlers.unshift({ handler: devMidlewareHandler });
|
|
556
|
+
nitro.options.devHandlers.push(...devHandlers);
|
|
519
557
|
nitro.options.handlers.unshift({
|
|
520
|
-
route: "/
|
|
558
|
+
route: "/__flow_error",
|
|
521
559
|
lazy: true,
|
|
522
560
|
handler: resolve(distDir, "core/runtime/nitro/renderer")
|
|
523
561
|
});
|
|
524
562
|
flow.hook("prepare:types", async (opts) => {
|
|
525
|
-
if (
|
|
563
|
+
if (flow.options._prepare) {
|
|
526
564
|
await scanHandlers(nitro);
|
|
527
565
|
await writeTypes(nitro);
|
|
528
566
|
}
|
|
@@ -531,15 +569,21 @@ async function initNitro(flow) {
|
|
|
531
569
|
flow.hook("build:done", async () => {
|
|
532
570
|
await flow.callHook("nitro:build:before", nitro);
|
|
533
571
|
if (flow.options.dev) {
|
|
534
|
-
await build$
|
|
572
|
+
await build$2(nitro);
|
|
535
573
|
} else {
|
|
536
|
-
console.log("build", flow.options._generate);
|
|
537
574
|
await prepare(nitro);
|
|
538
575
|
await copyPublicAssets(nitro);
|
|
539
576
|
await prerender(nitro);
|
|
540
577
|
if (!flow.options._generate) {
|
|
541
|
-
await build$
|
|
578
|
+
await build$2(nitro);
|
|
542
579
|
} else {
|
|
580
|
+
const nitroDev = await createNitro({
|
|
581
|
+
...nitro.options._config,
|
|
582
|
+
rootDir: nitro.options.rootDir,
|
|
583
|
+
logLevel: 0,
|
|
584
|
+
preset: "nitro-prerender"
|
|
585
|
+
});
|
|
586
|
+
flow.server = nitroDev;
|
|
543
587
|
const distDir2 = resolve(flow.options.rootDir, "dist");
|
|
544
588
|
if (!existsSync(distDir2))
|
|
545
589
|
await promises.symlink(nitro.options.output.publicDir, distDir2, "junction").catch(() => {
|
|
@@ -548,27 +592,37 @@ async function initNitro(flow) {
|
|
|
548
592
|
}
|
|
549
593
|
});
|
|
550
594
|
if (flow.options.dev) {
|
|
551
|
-
flow.hook("
|
|
552
|
-
|
|
595
|
+
flow.hook("build:compile", ({ compiler }) => {
|
|
596
|
+
compiler.outputFileSystem = { ...fse, join };
|
|
553
597
|
});
|
|
554
|
-
flow.hook("server:
|
|
555
|
-
|
|
598
|
+
flow.hook("server:devMiddleware", (m) => {
|
|
599
|
+
devMidlewareHandler.set(toEventHandler(m));
|
|
556
600
|
});
|
|
557
601
|
flow.server = createDevServer(nitro);
|
|
602
|
+
flow.hook("build:resources", () => {
|
|
603
|
+
flow.server.reload();
|
|
604
|
+
});
|
|
558
605
|
const waitUntilCompile = new Promise((resolve2) => nitro.hooks.hook("compiled", () => resolve2()));
|
|
559
606
|
flow.hook("build:done", () => waitUntilCompile);
|
|
560
607
|
}
|
|
561
608
|
}
|
|
609
|
+
async function resolveHandlers(flow) {
|
|
610
|
+
const handlers = [...flow.options.serverHandlers];
|
|
611
|
+
const devHandlers = [...flow.options.devServerHandlers];
|
|
612
|
+
return {
|
|
613
|
+
handlers,
|
|
614
|
+
devHandlers
|
|
615
|
+
};
|
|
616
|
+
}
|
|
562
617
|
|
|
563
618
|
const addModuleTranspiles = (opts = {}) => {
|
|
564
619
|
const flow = useNuxt();
|
|
565
620
|
const modules = [
|
|
566
621
|
...opts.additionalModules || [],
|
|
567
|
-
// ...flow.options.buildModules,
|
|
568
622
|
...flow.options.modules,
|
|
569
623
|
...flow.options._modules
|
|
570
624
|
].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());
|
|
571
|
-
flow.options.build.transpile = flow.options.build.transpile.map((m) => typeof m === "string" ? m.split("node_modules/").pop() : m)
|
|
625
|
+
flow.options.build.transpile = flow.options.build.transpile.map((m) => typeof m === "string" ? m.split("node_modules/").pop() : m);
|
|
572
626
|
function isTranspilePresent(mod) {
|
|
573
627
|
return flow.options.build.transpile.some((t) => !(t instanceof Function) && (t instanceof RegExp ? t.test(mod) : new RegExp(t).test(mod)));
|
|
574
628
|
}
|
|
@@ -581,10 +635,7 @@ const addModuleTranspiles = (opts = {}) => {
|
|
|
581
635
|
function createFlow(options) {
|
|
582
636
|
const hooks = createHooks();
|
|
583
637
|
const flow = {
|
|
584
|
-
|
|
585
|
-
_version: "3.0.0",
|
|
586
|
-
//* Include version
|
|
587
|
-
// @ts-ignore
|
|
638
|
+
_version: "3.0.0-rc.3",
|
|
588
639
|
version,
|
|
589
640
|
options,
|
|
590
641
|
hooks,
|
|
@@ -598,12 +649,10 @@ function createFlow(options) {
|
|
|
598
649
|
return flow;
|
|
599
650
|
}
|
|
600
651
|
async function initFlow(flow) {
|
|
601
|
-
flow.hooks.addHooks(flow.options.hooks);
|
|
602
652
|
nuxtCtx.set(flow);
|
|
603
653
|
flow.hook("close", () => nuxtCtx.unset());
|
|
604
|
-
await flow.callHook("modules:before");
|
|
654
|
+
await flow.callHook("modules:before", { nuxt: flow });
|
|
605
655
|
const modulesToInstall = [
|
|
606
|
-
// ...flow.options.buildModules,
|
|
607
656
|
...flow.options.modules,
|
|
608
657
|
...flow.options._modules
|
|
609
658
|
];
|
|
@@ -613,26 +662,17 @@ async function initFlow(flow) {
|
|
|
613
662
|
else
|
|
614
663
|
await installModule(m, {});
|
|
615
664
|
}
|
|
616
|
-
await flow.callHook("modules:done");
|
|
617
|
-
|
|
618
|
-
addModuleTranspiles();
|
|
665
|
+
await flow.callHook("modules:done", { nuxt: flow });
|
|
666
|
+
await addModuleTranspiles();
|
|
619
667
|
await initNitro(flow);
|
|
620
668
|
await flow.callHook("ready", flow);
|
|
621
669
|
}
|
|
622
670
|
async function loadFlow(opts) {
|
|
623
|
-
const start1 = Date.now();
|
|
624
671
|
const options = await loadFlowConfig(opts);
|
|
625
|
-
logger.info(`Config loading in ${Date.now() - start1}ms`);
|
|
626
672
|
options.appDir = resolve(distDir, "app");
|
|
627
673
|
options.alias["#app"] = resolve(distDir, "app/index");
|
|
628
674
|
options._majorVersion = 3;
|
|
629
|
-
options._modules.push(pagesModule, metaModule);
|
|
630
|
-
options._modules.push([importsModule, {
|
|
631
|
-
transform: {
|
|
632
|
-
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\\/)`))
|
|
633
|
-
}
|
|
634
|
-
}]);
|
|
635
|
-
options.modulesDir.push(resolve(options.workspaceDir, "node_modules"));
|
|
675
|
+
options._modules.push(pagesModule, metaModule, autoImportsModule, viteModule);
|
|
636
676
|
options.modulesDir.push(resolve(pkgDir, "node_modules"));
|
|
637
677
|
const flow = createFlow(options);
|
|
638
678
|
if (opts.ready !== false)
|
|
@@ -647,17 +687,11 @@ const serverPluginTemplate = {
|
|
|
647
687
|
filename: "plugins/server.mjs",
|
|
648
688
|
getContents(ctx) {
|
|
649
689
|
const serverPlugins = ctx.app.plugins;
|
|
650
|
-
const exports = [];
|
|
651
|
-
const imports = [];
|
|
652
|
-
for (const plugin of serverPlugins) {
|
|
653
|
-
const path = relative(ctx.nuxt.options.rootDir, plugin.src);
|
|
654
|
-
const variable = `${genSafeVariableName(path).replace(/_(45|46|47)/g, "_")}_${hash(path)}`;
|
|
655
|
-
exports.push(variable);
|
|
656
|
-
imports.push(genImport(plugin.src, variable));
|
|
657
|
-
}
|
|
658
690
|
return [
|
|
659
|
-
|
|
660
|
-
`export default ${genArrayFromRaw(
|
|
691
|
+
templateUtils.importSources(serverPlugins.map((p) => p.src)),
|
|
692
|
+
`export default ${genArrayFromRaw([
|
|
693
|
+
...serverPlugins.map((p) => templateUtils.importName(p.src))
|
|
694
|
+
])}`
|
|
661
695
|
].join("\n");
|
|
662
696
|
}
|
|
663
697
|
};
|
|
@@ -686,52 +720,43 @@ export { }
|
|
|
686
720
|
`;
|
|
687
721
|
}
|
|
688
722
|
};
|
|
689
|
-
const adHocModules = ["imports", "meta", "pages"];
|
|
723
|
+
const adHocModules = ["auto-imports", "meta", "pages", "vite-client"];
|
|
690
724
|
const schemaTemplate = {
|
|
691
725
|
filename: "types/schema.d.ts",
|
|
692
|
-
getContents:
|
|
726
|
+
getContents: ({ nuxt }) => {
|
|
693
727
|
const moduleInfo = nuxt.options._installedModules.map((m) => ({
|
|
694
728
|
...m.meta || {},
|
|
695
729
|
importName: m.entryPath || m.meta?.name
|
|
696
730
|
})).filter((m) => m.configKey && m.name && !adHocModules.includes(m.name));
|
|
697
|
-
const relativeRoot = relative(resolve(nuxt.options.buildDir, "types"), nuxt.options.rootDir);
|
|
698
731
|
return [
|
|
699
732
|
"import { FlowModule } from '@monkeyplus/flow-schema'",
|
|
700
733
|
"declare module '@monkeyplus/flow-schema' {",
|
|
701
734
|
" interface FlowConfig {",
|
|
702
|
-
...moduleInfo.filter(Boolean).map(
|
|
703
|
-
(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>`
|
|
704
|
-
),
|
|
735
|
+
...moduleInfo.filter(Boolean).map((meta) => ` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends FlowModule<infer O> ? Partial<O> : Record<string, any>`),
|
|
705
736
|
" }",
|
|
706
|
-
generateTypes(
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
addExport: false,
|
|
721
|
-
addDefaults: false,
|
|
722
|
-
allowExtraKeys: false,
|
|
723
|
-
indentation: 2
|
|
724
|
-
}
|
|
725
|
-
),
|
|
737
|
+
generateTypes(resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== "public"))), {
|
|
738
|
+
interfaceName: "RuntimeConfig",
|
|
739
|
+
addExport: false,
|
|
740
|
+
addDefaults: false,
|
|
741
|
+
allowExtraKeys: false,
|
|
742
|
+
indentation: 2
|
|
743
|
+
}),
|
|
744
|
+
generateTypes(resolveSchema(nuxt.options.runtimeConfig.public), {
|
|
745
|
+
interfaceName: "PublicRuntimeConfig",
|
|
746
|
+
addExport: false,
|
|
747
|
+
addDefaults: false,
|
|
748
|
+
allowExtraKeys: false,
|
|
749
|
+
indentation: 2
|
|
750
|
+
}),
|
|
726
751
|
"}"
|
|
727
752
|
].join("\n");
|
|
728
753
|
}
|
|
729
754
|
};
|
|
730
755
|
const publicPathTemplate = {
|
|
731
756
|
filename: "paths.mjs",
|
|
732
|
-
|
|
757
|
+
getContents({ nuxt }) {
|
|
733
758
|
return [
|
|
734
|
-
|
|
759
|
+
"import { joinURL } from 'ufo'",
|
|
735
760
|
!nuxt.options.dev && "import { useRuntimeConfig } from '#internal/nitro'",
|
|
736
761
|
nuxt.options.dev ? `const appConfig = ${JSON.stringify(nuxt.options.app)}` : "const appConfig = useRuntimeConfig().app",
|
|
737
762
|
"export const baseURL = () => appConfig.baseURL",
|
|
@@ -744,39 +769,15 @@ const publicPathTemplate = {
|
|
|
744
769
|
].filter(Boolean).join("\n");
|
|
745
770
|
}
|
|
746
771
|
};
|
|
747
|
-
function _resolveId(id) {
|
|
748
|
-
return resolvePath(id, {
|
|
749
|
-
url: [
|
|
750
|
-
// @ts-ignore
|
|
751
|
-
global.__NUXT_PREPATHS__,
|
|
752
|
-
import.meta.url,
|
|
753
|
-
process.cwd(),
|
|
754
|
-
// @ts-ignore
|
|
755
|
-
global.__NUXT_PATHS__
|
|
756
|
-
]
|
|
757
|
-
});
|
|
758
|
-
}
|
|
759
772
|
|
|
760
773
|
const defaultTemplates = {
|
|
761
774
|
__proto__: null,
|
|
775
|
+
serverPluginTemplate: serverPluginTemplate,
|
|
762
776
|
pluginsDeclaration: pluginsDeclaration,
|
|
763
|
-
publicPathTemplate: publicPathTemplate,
|
|
764
777
|
schemaTemplate: schemaTemplate,
|
|
765
|
-
|
|
778
|
+
publicPathTemplate: publicPathTemplate
|
|
766
779
|
};
|
|
767
780
|
|
|
768
|
-
function uniqueBy(arr, key) {
|
|
769
|
-
const res = [];
|
|
770
|
-
const seen = /* @__PURE__ */ new Set();
|
|
771
|
-
for (const item of arr) {
|
|
772
|
-
if (seen.has(item[key]))
|
|
773
|
-
continue;
|
|
774
|
-
seen.add(item[key]);
|
|
775
|
-
res.push(item);
|
|
776
|
-
}
|
|
777
|
-
return res;
|
|
778
|
-
}
|
|
779
|
-
|
|
780
781
|
function createApp(flow, options = {}) {
|
|
781
782
|
return defu(options, {
|
|
782
783
|
dir: flow.options.srcDir,
|
|
@@ -785,95 +786,51 @@ function createApp(flow, options = {}) {
|
|
|
785
786
|
templates: []
|
|
786
787
|
});
|
|
787
788
|
}
|
|
788
|
-
async function generateApp(flow, app
|
|
789
|
+
async function generateApp(flow, app) {
|
|
789
790
|
await resolveApp(flow, app);
|
|
790
|
-
app.templates = Object.values(defaultTemplates).concat(
|
|
791
|
-
flow.options.build.templates
|
|
792
|
-
);
|
|
791
|
+
app.templates = Object.values(defaultTemplates).concat(flow.options.build.templates);
|
|
793
792
|
await flow.callHook("app:templates", app);
|
|
794
793
|
app.templates = app.templates.map((tmpl) => normalizeTemplate(tmpl));
|
|
795
794
|
const templateContext = { utils: templateUtils, nuxt: flow, app };
|
|
796
|
-
await Promise.all(
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
})
|
|
810
|
-
);
|
|
795
|
+
await Promise.all(app.templates.map(async (template) => {
|
|
796
|
+
const contents = await compileTemplate(template, templateContext);
|
|
797
|
+
const fullPath = template.dst || resolve(flow.options.buildDir, template.filename);
|
|
798
|
+
flow.vfs[fullPath] = contents;
|
|
799
|
+
const aliasPath = `#build/${template.filename.replace(/\.\w+$/, "")}`;
|
|
800
|
+
flow.vfs[aliasPath] = contents;
|
|
801
|
+
if (process.platform === "win32")
|
|
802
|
+
flow.vfs[fullPath.replace(/\//g, "\\")] = contents;
|
|
803
|
+
if (template.write) {
|
|
804
|
+
await promises.mkdir(dirname(fullPath), { recursive: true });
|
|
805
|
+
await promises.writeFile(fullPath, contents, "utf8");
|
|
806
|
+
}
|
|
807
|
+
}));
|
|
811
808
|
await flow.callHook("app:templatesGenerated", app);
|
|
812
809
|
}
|
|
813
810
|
async function resolveApp(flow, app) {
|
|
814
|
-
app.plugins = [
|
|
815
|
-
...flow.options.plugins.map(normalizePlugin)
|
|
816
|
-
];
|
|
811
|
+
app.plugins = [...flow.options.plugins.map(normalizePlugin)];
|
|
817
812
|
for (const config of flow.options._layers.map((layer) => layer.config)) {
|
|
818
813
|
app.plugins.push(...[
|
|
819
814
|
...config.plugins || [],
|
|
820
|
-
...
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
])
|
|
815
|
+
...await resolveFilesFlow(config.srcDir, [
|
|
816
|
+
"plugins/*.{ts,js,mjs,cjs,mts,cts}",
|
|
817
|
+
"plugins/*/index.*{ts,js,mjs,cjs,mts,cts}"
|
|
818
|
+
])
|
|
824
819
|
].map((plugin) => normalizePlugin(plugin)));
|
|
825
820
|
}
|
|
826
|
-
app.configs = [];
|
|
827
|
-
for (const config of flow.options._layers.map((layer) => layer.config)) {
|
|
828
|
-
const appConfigPath = await findPath(resolve(config.srcDir, "app.config"));
|
|
829
|
-
if (appConfigPath)
|
|
830
|
-
app.configs.push(appConfigPath);
|
|
831
|
-
}
|
|
832
|
-
await flow.callHook("app:resolve", app);
|
|
833
821
|
app.plugins = uniqueBy(app.plugins, "src");
|
|
822
|
+
await flow.callHook("app:resolve", app);
|
|
834
823
|
}
|
|
835
|
-
|
|
836
|
-
const
|
|
837
|
-
|
|
838
|
-
const
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
return null;
|
|
846
|
-
};
|
|
847
|
-
return {
|
|
848
|
-
name: "virtual",
|
|
849
|
-
resolveId(id, importer) {
|
|
850
|
-
if (process.platform === "win32" && isAbsolute(id)) {
|
|
851
|
-
id = resolve(id);
|
|
852
|
-
}
|
|
853
|
-
const resolvedId = resolveWithExt(id);
|
|
854
|
-
if (resolvedId)
|
|
855
|
-
return PREFIX + resolvedId;
|
|
856
|
-
if (importer && !isAbsolute(id)) {
|
|
857
|
-
const importerNoPrefix = importer.startsWith(PREFIX) ? importer.slice(PREFIX.length) : importer;
|
|
858
|
-
const importedDir = dirname(importerNoPrefix);
|
|
859
|
-
const resolved = resolveWithExt(join(importedDir, id));
|
|
860
|
-
if (resolved)
|
|
861
|
-
return PREFIX + resolved;
|
|
862
|
-
}
|
|
863
|
-
return null;
|
|
864
|
-
},
|
|
865
|
-
load(id) {
|
|
866
|
-
if (!id.startsWith(PREFIX))
|
|
867
|
-
return null;
|
|
868
|
-
const idNoPrefix = id.slice(PREFIX.length);
|
|
869
|
-
if (idNoPrefix in vfs) {
|
|
870
|
-
return {
|
|
871
|
-
code: vfs[idNoPrefix],
|
|
872
|
-
map: null
|
|
873
|
-
};
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
};
|
|
824
|
+
function uniqueBy(arr, key) {
|
|
825
|
+
const res = [];
|
|
826
|
+
const seen = /* @__PURE__ */ new Set();
|
|
827
|
+
for (const item of arr) {
|
|
828
|
+
if (seen.has(item[key]))
|
|
829
|
+
continue;
|
|
830
|
+
seen.add(item[key]);
|
|
831
|
+
res.push(item);
|
|
832
|
+
}
|
|
833
|
+
return res;
|
|
877
834
|
}
|
|
878
835
|
|
|
879
836
|
async function warmupViteServer(server, entries) {
|
|
@@ -894,11 +851,6 @@ async function warmupViteServer(server, entries) {
|
|
|
894
851
|
await Promise.all(entries.map((entry) => warmup(entry)));
|
|
895
852
|
}
|
|
896
853
|
|
|
897
|
-
const wpfs = {
|
|
898
|
-
...fse,
|
|
899
|
-
join
|
|
900
|
-
};
|
|
901
|
-
|
|
902
854
|
function cacheDirPlugin(rootDir, name) {
|
|
903
855
|
const optimizeCacheDir = resolve(rootDir, "node_modules/.cache/vite", name);
|
|
904
856
|
return {
|
|
@@ -909,63 +861,279 @@ function cacheDirPlugin(rootDir, name) {
|
|
|
909
861
|
};
|
|
910
862
|
}
|
|
911
863
|
|
|
912
|
-
|
|
864
|
+
const wpfs = {
|
|
865
|
+
...fse,
|
|
866
|
+
join
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
function uniq(arr) {
|
|
870
|
+
return Array.from(new Set(arr));
|
|
871
|
+
}
|
|
872
|
+
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|less|stylus|styl)(\?[^.]+)?$/;
|
|
873
|
+
function isCSS(file) {
|
|
874
|
+
return IS_CSS_RE.test(file);
|
|
875
|
+
}
|
|
876
|
+
function hashId(id) {
|
|
877
|
+
return `$id_${hash(id)}`;
|
|
878
|
+
}
|
|
879
|
+
function hash(input, length = 8) {
|
|
880
|
+
return createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
881
|
+
}
|
|
882
|
+
function readDirRecursively(dir) {
|
|
883
|
+
return readdirSync(dir).reduce((files, file) => {
|
|
884
|
+
const name = join(dir, file);
|
|
885
|
+
const isDirectory2 = statSync(name).isDirectory();
|
|
886
|
+
return isDirectory2 ? [...files, ...readDirRecursively(name)] : [...files, name];
|
|
887
|
+
}, []);
|
|
888
|
+
}
|
|
889
|
+
async function isDirectory(path) {
|
|
890
|
+
try {
|
|
891
|
+
return (await promises.stat(path)).isDirectory();
|
|
892
|
+
} catch (_err) {
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
function isExternal(opts, id) {
|
|
898
|
+
const ssrConfig = opts.viteServer.config.ssr;
|
|
899
|
+
const externalOpts = {
|
|
900
|
+
inline: [
|
|
901
|
+
/virtual:/,
|
|
902
|
+
/\.ts$/,
|
|
903
|
+
...ExternalsDefaults.inline,
|
|
904
|
+
...ssrConfig.noExternal
|
|
905
|
+
],
|
|
906
|
+
external: [
|
|
907
|
+
/node_modules/
|
|
908
|
+
],
|
|
909
|
+
resolve: {
|
|
910
|
+
type: "module",
|
|
911
|
+
extensions: [".ts", ".js", ".json", ".vue", ".mjs", ".jsx", ".tsx", ".wasm"]
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
return isExternal$1(id, opts.viteServer.config.root, externalOpts);
|
|
915
|
+
}
|
|
916
|
+
async function transformRequest(opts, id) {
|
|
917
|
+
if (id && id.startsWith("/@id/__x00__"))
|
|
918
|
+
id = `\0${id.slice("/@id/__x00__".length)}`;
|
|
919
|
+
if (id && id.startsWith("/@id/"))
|
|
920
|
+
id = id.slice("/@id/".length);
|
|
921
|
+
if (id && id.startsWith("/@fs/")) {
|
|
922
|
+
id = id.slice("/@fs".length);
|
|
923
|
+
if (id.match(/^\/\w:/))
|
|
924
|
+
id = id.slice(1);
|
|
925
|
+
} else if (!id.includes("entry") && id.startsWith("/")) {
|
|
926
|
+
const resolvedPath = resolve(opts.viteServer.config.root, `.${id}`);
|
|
927
|
+
if (existsSync(resolvedPath))
|
|
928
|
+
id = resolvedPath;
|
|
929
|
+
}
|
|
930
|
+
const withoutVersionQuery = id.replace(/\?v=\w+$/, "");
|
|
931
|
+
if (await isExternal(opts, withoutVersionQuery)) {
|
|
932
|
+
const path = builtinModules.includes(withoutVersionQuery.split("node:").pop()) ? withoutVersionQuery : pathToFileURL(withoutVersionQuery).href;
|
|
933
|
+
return {
|
|
934
|
+
code: `(global, module, _, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) =>
|
|
935
|
+
${genDynamicImport(path, { wrapper: false })}
|
|
936
|
+
.then(r => {
|
|
937
|
+
if (r.default && r.default.__esModule)
|
|
938
|
+
r = r.default
|
|
939
|
+
exports.default = r.default
|
|
940
|
+
ssrExportAll(r)
|
|
941
|
+
})
|
|
942
|
+
.catch(e => {
|
|
943
|
+
console.error(e)
|
|
944
|
+
throw new Error(${JSON.stringify(`[vite dev] Error loading external "${id}".`)})
|
|
945
|
+
})`,
|
|
946
|
+
deps: [],
|
|
947
|
+
dynamicDeps: []
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
const res = await opts.viteServer.transformRequest(id, { ssr: true }).catch((err) => {
|
|
951
|
+
console.warn(`[SSR] Error transforming ${id}:`, err);
|
|
952
|
+
}) || { code: "", map: {}, deps: [], dynamicDeps: [] };
|
|
953
|
+
const code = `async function (global, module, exports, __vite_ssr_exports__, __vite_ssr_import_meta__, __vite_ssr_import__, __vite_ssr_dynamic_import__, __vite_ssr_exportAll__) {
|
|
954
|
+
${res.code || "/* empty */"};
|
|
955
|
+
}`;
|
|
956
|
+
return { code, deps: res.deps || [], dynamicDeps: res.dynamicDeps || [] };
|
|
957
|
+
}
|
|
958
|
+
async function transformRequestRecursive(opts, id, parent = "<entry>", chunks = {}) {
|
|
959
|
+
if (chunks[id]) {
|
|
960
|
+
chunks[id].parents.push(parent);
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
const res = await transformRequest(opts, id);
|
|
964
|
+
const deps = uniq([...res.deps, ...res.dynamicDeps]);
|
|
965
|
+
chunks[id] = {
|
|
966
|
+
id,
|
|
967
|
+
code: res.code,
|
|
968
|
+
deps,
|
|
969
|
+
parents: [parent]
|
|
970
|
+
};
|
|
971
|
+
for (const dep of deps)
|
|
972
|
+
await transformRequestRecursive(opts, dep, id, chunks);
|
|
973
|
+
return Object.values(chunks);
|
|
974
|
+
}
|
|
975
|
+
async function bundleRequest(opts, entryURL) {
|
|
976
|
+
const chunks = await transformRequestRecursive(opts, entryURL);
|
|
977
|
+
const listIds = (ids) => ids.map((id) => `// - ${id} (${hashId(id)})`).join("\n");
|
|
978
|
+
const chunksCode = chunks.map((chunk) => `
|
|
979
|
+
// --------------------
|
|
980
|
+
// Request: ${chunk.id}
|
|
981
|
+
// Parents:
|
|
982
|
+
${listIds(chunk.parents)}
|
|
983
|
+
// Dependencies:
|
|
984
|
+
${listIds(chunk.deps)}
|
|
985
|
+
// --------------------
|
|
986
|
+
const ${hashId(chunk.id)} = ${chunk.code}
|
|
987
|
+
`).join("\n");
|
|
988
|
+
const manifestCode = `const __modules__ = ${genObjectFromRawEntries(chunks.map((chunk) => [chunk.id, hashId(chunk.id)]))}`;
|
|
989
|
+
const ssrModuleLoader = `
|
|
990
|
+
const __pendingModules__ = new Map()
|
|
991
|
+
const __pendingImports__ = new Map()
|
|
992
|
+
const __ssrContext__ = { global: globalThis }
|
|
993
|
+
|
|
994
|
+
function __ssrLoadModule__(url, urlStack = []) {
|
|
995
|
+
const pendingModule = __pendingModules__.get(url)
|
|
996
|
+
if (pendingModule) { return pendingModule }
|
|
997
|
+
const modulePromise = __instantiateModule__(url, urlStack)
|
|
998
|
+
__pendingModules__.set(url, modulePromise)
|
|
999
|
+
modulePromise.catch(() => { __pendingModules__.delete(url) })
|
|
1000
|
+
.finally(() => { __pendingModules__.delete(url) })
|
|
1001
|
+
return modulePromise
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
async function __instantiateModule__(url, urlStack) {
|
|
1005
|
+
const mod = __modules__[url]
|
|
1006
|
+
if (mod.stubModule) { return mod.stubModule }
|
|
1007
|
+
const stubModule = { [Symbol.toStringTag]: 'Module' }
|
|
1008
|
+
Object.defineProperty(stubModule, '__esModule', { value: true })
|
|
1009
|
+
mod.stubModule = stubModule
|
|
1010
|
+
// https://vitejs.dev/guide/api-hmr.html
|
|
1011
|
+
const importMeta = { url, hot: { accept() {}, prune() {}, dispose() {}, invalidate() {}, decline() {}, on() {} } }
|
|
1012
|
+
urlStack = urlStack.concat(url)
|
|
1013
|
+
const isCircular = url => urlStack.includes(url)
|
|
1014
|
+
const pendingDeps = []
|
|
1015
|
+
const ssrImport = async (dep) => {
|
|
1016
|
+
// TODO: Handle externals if dep[0] !== '.' | '/'
|
|
1017
|
+
if (!isCircular(dep) && !__pendingImports__.get(dep)?.some(isCircular)) {
|
|
1018
|
+
pendingDeps.push(dep)
|
|
1019
|
+
if (pendingDeps.length === 1) {
|
|
1020
|
+
__pendingImports__.set(url, pendingDeps)
|
|
1021
|
+
}
|
|
1022
|
+
await __ssrLoadModule__(dep, urlStack)
|
|
1023
|
+
if (pendingDeps.length === 1) {
|
|
1024
|
+
__pendingImports__.delete(url)
|
|
1025
|
+
} else {
|
|
1026
|
+
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
return __modules__[dep].stubModule
|
|
1030
|
+
}
|
|
1031
|
+
function ssrDynamicImport (dep) {
|
|
1032
|
+
// TODO: Handle dynamic import starting with . relative to url
|
|
1033
|
+
return ssrImport(dep)
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
function ssrExportAll(sourceModule) {
|
|
1037
|
+
for (const key in sourceModule) {
|
|
1038
|
+
if (key !== 'default') {
|
|
1039
|
+
try {
|
|
1040
|
+
Object.defineProperty(stubModule, key, {
|
|
1041
|
+
enumerable: true,
|
|
1042
|
+
configurable: true,
|
|
1043
|
+
get() { return sourceModule[key] }
|
|
1044
|
+
})
|
|
1045
|
+
} catch (_err) { }
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
const cjsModule = {
|
|
1051
|
+
get exports () {
|
|
1052
|
+
return stubModule.default
|
|
1053
|
+
},
|
|
1054
|
+
set exports (v) {
|
|
1055
|
+
stubModule.default = v
|
|
1056
|
+
},
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
await mod(
|
|
1060
|
+
__ssrContext__.global,
|
|
1061
|
+
cjsModule,
|
|
1062
|
+
stubModule.default,
|
|
1063
|
+
stubModule,
|
|
1064
|
+
importMeta,
|
|
1065
|
+
ssrImport,
|
|
1066
|
+
ssrDynamicImport,
|
|
1067
|
+
ssrExportAll
|
|
1068
|
+
)
|
|
1069
|
+
|
|
1070
|
+
return stubModule
|
|
1071
|
+
}
|
|
1072
|
+
`;
|
|
1073
|
+
const code = [
|
|
1074
|
+
chunksCode,
|
|
1075
|
+
manifestCode,
|
|
1076
|
+
ssrModuleLoader,
|
|
1077
|
+
`export default await __ssrLoadModule__(${JSON.stringify(entryURL)})`
|
|
1078
|
+
].join("\n\n");
|
|
1079
|
+
return {
|
|
1080
|
+
code,
|
|
1081
|
+
ids: chunks.map((i) => i.id)
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
async function writeManifest(ctx, extraEntries = []) {
|
|
1086
|
+
const clientDist = resolve(ctx.flow.options.buildDir, "dist/client");
|
|
1087
|
+
const serverDist = resolve(ctx.flow.options.buildDir, "dist/server");
|
|
1088
|
+
const entries = [
|
|
1089
|
+
"@vite/client",
|
|
1090
|
+
"entry.mjs",
|
|
1091
|
+
...extraEntries
|
|
1092
|
+
];
|
|
1093
|
+
const devClientManifest = {
|
|
1094
|
+
publicPath: joinURL(ctx.flow.options.app.baseURL, ctx.flow.options.app.buildAssetsDir),
|
|
1095
|
+
all: entries,
|
|
1096
|
+
initial: entries,
|
|
1097
|
+
async: [],
|
|
1098
|
+
modules: {}
|
|
1099
|
+
};
|
|
1100
|
+
const clientManifest = ctx.flow.options.dev ? devClientManifest : await fse.readJSON(resolve(clientDist, "manifest.json"));
|
|
1101
|
+
await fse.mkdirp(serverDist);
|
|
1102
|
+
await fse.writeFile(resolve(serverDist, "client.manifest.json"), JSON.stringify(clientManifest, null, 2), "utf8");
|
|
1103
|
+
await fse.writeFile(resolve(serverDist, "client.manifest.mjs"), `export default ${JSON.stringify(clientManifest, null, 2)}`, "utf8");
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
const buildServer = async (ctx) => {
|
|
913
1107
|
const serverConfig = vite.mergeConfig(ctx.config, {
|
|
914
|
-
|
|
915
|
-
// experimental: {
|
|
916
|
-
// renderBuiltUrl: (filename, { type, hostType }) => {
|
|
917
|
-
// if (hostType !== 'js') {
|
|
918
|
-
// // In CSS we only use relative paths until we craft a clever runtime CSS hack
|
|
919
|
-
// return { relative: true };
|
|
920
|
-
// }
|
|
921
|
-
// if (type === 'public')
|
|
922
|
-
// return { runtime: `globalThis.__publicAssetsURL(${JSON.stringify(filename)})` };
|
|
923
|
-
// if (type === 'asset') {
|
|
924
|
-
// const relativeFilename = filename.replace(withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir)), '');
|
|
925
|
-
// return { runtime: `globalThis.__buildAssetsURL(${JSON.stringify(relativeFilename)})` };
|
|
926
|
-
// }
|
|
927
|
-
// },
|
|
928
|
-
// },
|
|
1108
|
+
configFile: false,
|
|
929
1109
|
define: {
|
|
930
1110
|
"process.server": true,
|
|
931
|
-
"process.client": false,
|
|
932
1111
|
"typeof window": '"undefined"',
|
|
933
1112
|
"typeof document": '"undefined"',
|
|
934
1113
|
"typeof navigator": '"undefined"',
|
|
935
1114
|
"typeof location": '"undefined"',
|
|
936
1115
|
"typeof XMLHttpRequest": '"undefined"'
|
|
937
1116
|
},
|
|
938
|
-
resolve: {
|
|
939
|
-
alias: {
|
|
940
|
-
"#build/plugins": resolve(ctx.nuxt.options.buildDir, "plugins/server")
|
|
941
|
-
}
|
|
942
|
-
},
|
|
943
1117
|
ssr: {
|
|
944
|
-
external: ["#internal/nitro", "#internal/nitro/utils"],
|
|
945
1118
|
noExternal: [
|
|
946
|
-
...ctx.
|
|
947
|
-
// TODO: Use externality for production (rollup) build
|
|
1119
|
+
...ctx.flow.options.build.transpile,
|
|
948
1120
|
/\/esm\/.*\.js$/,
|
|
949
1121
|
/\.(es|esm|esm-browser|esm-bundler).js$/,
|
|
950
1122
|
"/__vue-jsx",
|
|
951
1123
|
"#app",
|
|
952
1124
|
/(nuxt|nuxt3)\/(dist|src|app)/,
|
|
953
|
-
/flow\/(dist|src|app)/,
|
|
954
|
-
/flow\/modules\/(content|icons|images|netlify|netlify-cms|seo|sitemap|vue)/,
|
|
955
1125
|
/@monkeyplus\/flow\/(dist|src|app)/,
|
|
956
1126
|
/@nuxt\/nitro\/(dist|src)/
|
|
957
1127
|
]
|
|
958
1128
|
},
|
|
959
1129
|
build: {
|
|
960
|
-
outDir: resolve(ctx.
|
|
961
|
-
|
|
1130
|
+
outDir: resolve(ctx.flow.options.buildDir, "dist/server"),
|
|
1131
|
+
manifest: true,
|
|
962
1132
|
rollupOptions: {
|
|
963
1133
|
external: ["#internal/nitro"],
|
|
964
1134
|
output: {
|
|
965
1135
|
entryFileNames: "server.mjs",
|
|
966
1136
|
preferConst: true,
|
|
967
|
-
// TODO: https://github.com/vitejs/vite/pull/8641
|
|
968
|
-
inlineDynamicImports: !ctx.nuxt.options.experimental.viteServerDynamicImports,
|
|
969
1137
|
format: "module"
|
|
970
1138
|
},
|
|
971
1139
|
onwarn(warning, rollupWarn) {
|
|
@@ -975,31 +1143,39 @@ async function buildServer(ctx) {
|
|
|
975
1143
|
}
|
|
976
1144
|
},
|
|
977
1145
|
server: {
|
|
978
|
-
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
|
|
979
1146
|
preTransformRequests: false,
|
|
980
|
-
|
|
1147
|
+
cors: true
|
|
981
1148
|
},
|
|
982
1149
|
plugins: [
|
|
983
|
-
cacheDirPlugin(ctx.
|
|
984
|
-
replace({
|
|
985
|
-
...Object.fromEntries([";", "(", "{", "}", " ", " ", "\n"].map((d) => [`${d}global.`, `${d}globalThis.`])),
|
|
986
|
-
preventAssignment: true
|
|
987
|
-
})
|
|
988
|
-
// vuePlugin(ctx.config.vue),
|
|
989
|
-
// viteJsxPlugin(),
|
|
1150
|
+
cacheDirPlugin(ctx.flow.options.rootDir, "server")
|
|
990
1151
|
]
|
|
991
1152
|
});
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1153
|
+
await ctx.flow.callHook("vite:extendConfig", serverConfig, { isClient: false, isServer: true });
|
|
1154
|
+
ctx.flow.hook("nitro:build:before", async () => {
|
|
1155
|
+
if (ctx.flow.options.dev)
|
|
1156
|
+
return;
|
|
1157
|
+
const clientDist = resolve(ctx.flow.options.buildDir, "dist/client");
|
|
1158
|
+
const publicDir = join(ctx.flow.options.srcDir, ctx.flow.options.dir.public);
|
|
1159
|
+
let publicFiles = [];
|
|
1160
|
+
if (await isDirectory(publicDir)) {
|
|
1161
|
+
publicFiles = readDirRecursively(publicDir).map((r) => r.replace(publicDir, ""));
|
|
1162
|
+
for (const file of publicFiles) {
|
|
1163
|
+
try {
|
|
1164
|
+
fse.rmSync(join(clientDist, file));
|
|
1165
|
+
} catch {
|
|
1166
|
+
}
|
|
997
1167
|
}
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1168
|
+
}
|
|
1169
|
+
if (await isDirectory(clientDist)) {
|
|
1170
|
+
const nestedAssetsPath = withoutTrailingSlash(join(clientDist, ctx.flow.options.app.buildAssetsDir));
|
|
1171
|
+
if (await isDirectory(nestedAssetsPath)) {
|
|
1172
|
+
await fse.copy(nestedAssetsPath, clientDist, { recursive: true });
|
|
1173
|
+
await fse.remove(nestedAssetsPath);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
});
|
|
1177
|
+
const onBuild = () => ctx.flow.callHook("build:resources", wpfs);
|
|
1178
|
+
if (!ctx.flow.options.dev) {
|
|
1003
1179
|
const start = Date.now();
|
|
1004
1180
|
logger.info("Building server...");
|
|
1005
1181
|
await vite.build(serverConfig);
|
|
@@ -1009,87 +1185,188 @@ async function buildServer(ctx) {
|
|
|
1009
1185
|
}
|
|
1010
1186
|
const viteServer = await vite.createServer(serverConfig);
|
|
1011
1187
|
ctx.ssrServer = viteServer;
|
|
1012
|
-
await ctx.
|
|
1013
|
-
ctx.
|
|
1188
|
+
await ctx.flow.callHook("vite:serverCreated", viteServer, { isClient: false, isServer: true });
|
|
1189
|
+
ctx.flow.hook("close", () => viteServer.close());
|
|
1014
1190
|
await viteServer.pluginContainer.buildStart({});
|
|
1015
|
-
|
|
1191
|
+
const _doBuild = async () => {
|
|
1192
|
+
const start = Date.now();
|
|
1193
|
+
const { code, ids } = await bundleRequest({ viteServer }, resolve(ctx.flow.options.appDir, "entry"));
|
|
1194
|
+
await fse.ensureFile(resolve(ctx.flow.options.buildDir, "dist/server/server.mjs"));
|
|
1195
|
+
await fse.writeFile(resolve(ctx.flow.options.buildDir, "dist/server/server.mjs"), code, "utf-8");
|
|
1196
|
+
await writeManifest(ctx, ids.filter(isCSS).map((i) => i.slice(1)));
|
|
1197
|
+
const time = Date.now() - start;
|
|
1198
|
+
logger.success(`Vite server built in ${time}ms`);
|
|
1199
|
+
await onBuild();
|
|
1200
|
+
ctx.flow.callHook("bundler:change", {});
|
|
1201
|
+
};
|
|
1202
|
+
const doBuild = debounce(_doBuild, 50);
|
|
1203
|
+
await _doBuild();
|
|
1204
|
+
viteServer.watcher.on("all", (_event, file) => {
|
|
1205
|
+
if (file.includes("/pages/"))
|
|
1206
|
+
return;
|
|
1207
|
+
file = normalize(file);
|
|
1208
|
+
if (file.indexOf(ctx.flow.options.buildDir) === 0)
|
|
1209
|
+
return;
|
|
1210
|
+
doBuild();
|
|
1211
|
+
});
|
|
1212
|
+
ctx.flow.hook("app:templatesGenerated", () => doBuild());
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
const PREFIX = "virtual:";
|
|
1216
|
+
function virtual(vfs) {
|
|
1217
|
+
const extensions = ["", ".ts", ".vue", ".mjs", ".cjs", ".js", ".json"];
|
|
1218
|
+
const resolveWithExt = (id) => {
|
|
1219
|
+
for (const ext of extensions) {
|
|
1220
|
+
const rId = id + ext;
|
|
1221
|
+
if (rId in vfs)
|
|
1222
|
+
return rId;
|
|
1223
|
+
}
|
|
1224
|
+
return null;
|
|
1225
|
+
};
|
|
1226
|
+
return {
|
|
1227
|
+
name: "virtual",
|
|
1228
|
+
resolveId(id, importer) {
|
|
1229
|
+
if (process.platform === "win32" && isAbsolute(id)) {
|
|
1230
|
+
id = resolve(id);
|
|
1231
|
+
}
|
|
1232
|
+
const resolvedId = resolveWithExt(id);
|
|
1233
|
+
if (resolvedId)
|
|
1234
|
+
return PREFIX + resolvedId;
|
|
1235
|
+
if (importer && !isAbsolute(id)) {
|
|
1236
|
+
const importerNoPrefix = importer.startsWith(PREFIX) ? importer.slice(PREFIX.length) : importer;
|
|
1237
|
+
const importedDir = dirname(importerNoPrefix);
|
|
1238
|
+
const resolved = resolveWithExt(join(importedDir, id));
|
|
1239
|
+
if (resolved)
|
|
1240
|
+
return PREFIX + resolved;
|
|
1241
|
+
}
|
|
1242
|
+
return null;
|
|
1243
|
+
},
|
|
1244
|
+
load(id) {
|
|
1245
|
+
if (!id.startsWith(PREFIX))
|
|
1246
|
+
return null;
|
|
1247
|
+
const idNoPrefix = id.slice(PREFIX.length);
|
|
1248
|
+
return {
|
|
1249
|
+
code: vfs[idNoPrefix],
|
|
1250
|
+
map: null
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1253
|
+
};
|
|
1016
1254
|
}
|
|
1017
1255
|
|
|
1018
|
-
|
|
1019
|
-
|
|
1256
|
+
const VITE_ASSET_RE = /^export default ["'](__VITE_ASSET.*)["']$/;
|
|
1257
|
+
const DynamicBasePlugin = createUnplugin((options = {}) => {
|
|
1258
|
+
return {
|
|
1259
|
+
name: "nuxt:dynamic-base-path",
|
|
1260
|
+
resolveId(id) {
|
|
1261
|
+
if (id.startsWith("/__NUXT_BASE__"))
|
|
1262
|
+
return id.replace("/__NUXT_BASE__", "");
|
|
1263
|
+
if (id === "#internal/nitro")
|
|
1264
|
+
return "#internal/nitro";
|
|
1265
|
+
return null;
|
|
1266
|
+
},
|
|
1267
|
+
enforce: "post",
|
|
1268
|
+
transform(code, id) {
|
|
1269
|
+
const s = new MagicString(code);
|
|
1270
|
+
if (options.globalPublicPath && id.includes("paths.mjs") && code.includes("const appConfig = "))
|
|
1271
|
+
s.append(`${options.globalPublicPath} = buildAssetsURL();
|
|
1272
|
+
`);
|
|
1273
|
+
const assetId = code.match(VITE_ASSET_RE);
|
|
1274
|
+
if (assetId) {
|
|
1275
|
+
s.overwrite(0, code.length, [
|
|
1276
|
+
"import { buildAssetsURL } from '#build/paths.mjs';",
|
|
1277
|
+
`export default buildAssetsURL("${assetId[1]}".replace("/__NUXT_BASE__", ""));`
|
|
1278
|
+
].join("\n"));
|
|
1279
|
+
}
|
|
1280
|
+
if (!id.includes("paths.mjs") && code.includes("NUXT_BASE") && !code.includes("import { publicAssetsURL as __publicAssetsURL }"))
|
|
1281
|
+
s.prepend("import { publicAssetsURL as __publicAssetsURL } from '#build/paths.mjs';\n");
|
|
1282
|
+
if (id === "vite/preload-helper") {
|
|
1283
|
+
s.prepend("import { buildAssetsDir } from '#build/paths.mjs';\n");
|
|
1284
|
+
s.replace(/const base = ['"]\/__NUXT_BASE__\/['"]/, "const base = buildAssetsDir()");
|
|
1285
|
+
}
|
|
1286
|
+
s.replace(/from *['"]\/__NUXT_BASE__(\/[^'"]*)['"]/g, 'from "$1"');
|
|
1287
|
+
const delimiterRE = /(?<!(const base = |from *))(`([^`]*)\/__NUXT_BASE__\/([^`]*)`|'([^']*)\/__NUXT_BASE__\/([^']*)'|"([^"]*)\/__NUXT_BASE__\/([^"]*)")/g;
|
|
1288
|
+
s.replace(delimiterRE, (r) => `\`${r.replace(/\/__NUXT_BASE__\//g, "${__publicAssetsURL()}").slice(1, -1)}\``);
|
|
1289
|
+
if (s.hasChanged()) {
|
|
1290
|
+
return {
|
|
1291
|
+
code: s.toString(),
|
|
1292
|
+
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
});
|
|
1298
|
+
|
|
1299
|
+
async function bundleVite(flow) {
|
|
1300
|
+
const hmrPortDefault = 24678;
|
|
1301
|
+
const hmrPort = await getPort({
|
|
1302
|
+
port: hmrPortDefault,
|
|
1303
|
+
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
|
|
1304
|
+
});
|
|
1020
1305
|
const ctx = {
|
|
1021
|
-
nuxt,
|
|
1022
|
-
|
|
1023
|
-
config: vite.mergeConfig(
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
alias
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
}
|
|
1038
|
-
},
|
|
1039
|
-
optimizeDeps: {
|
|
1040
|
-
entries: [entry],
|
|
1041
|
-
include: []
|
|
1042
|
-
},
|
|
1043
|
-
// css: resolveCSSOptions(nuxt),
|
|
1044
|
-
build: {
|
|
1045
|
-
rollupOptions: {
|
|
1046
|
-
output: { sanitizeFileName: sanitizeFilePath },
|
|
1047
|
-
input: resolve(nuxt.options.appDir, "entry")
|
|
1048
|
-
},
|
|
1049
|
-
watch: {
|
|
1050
|
-
exclude: nuxt.options.ignore
|
|
1051
|
-
}
|
|
1052
|
-
},
|
|
1053
|
-
plugins: [
|
|
1054
|
-
// composableKeysPlugin.vite({ sourcemap: nuxt.options.sourcemap, rootDir: nuxt.options.rootDir }),
|
|
1055
|
-
replace({
|
|
1056
|
-
...Object.fromEntries([";", "(", "{", "}", " ", " ", "\n"].map((d) => [`${d}global.`, `${d}globalThis.`])),
|
|
1057
|
-
preventAssignment: true
|
|
1058
|
-
}),
|
|
1059
|
-
virtual(nuxt.vfs)
|
|
1306
|
+
nuxt: flow,
|
|
1307
|
+
flow,
|
|
1308
|
+
config: vite.mergeConfig({
|
|
1309
|
+
mode: flow.options.dev ? "development" : void 0,
|
|
1310
|
+
resolve: {
|
|
1311
|
+
alias: {
|
|
1312
|
+
...flow.options.alias,
|
|
1313
|
+
"#app": flow.options.appDir,
|
|
1314
|
+
"#build/plugins": resolve(flow.options.buildDir, "plugins/server"),
|
|
1315
|
+
"#build": flow.options.buildDir,
|
|
1316
|
+
"/entry.mjs": resolve(flow.options.appDir, "entry")
|
|
1317
|
+
}
|
|
1318
|
+
},
|
|
1319
|
+
optimizeDeps: {
|
|
1320
|
+
entries: [
|
|
1321
|
+
resolve(flow.options.appDir, "entry.ts")
|
|
1060
1322
|
],
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
allow: [
|
|
1068
|
-
nuxt.options.appDir
|
|
1069
|
-
]
|
|
1070
|
-
},
|
|
1071
|
-
hmr: false
|
|
1323
|
+
include: []
|
|
1324
|
+
},
|
|
1325
|
+
build: {
|
|
1326
|
+
rollupOptions: {
|
|
1327
|
+
output: { sanitizeFileName: sanitizeFilePath },
|
|
1328
|
+
input: resolve(flow.options.appDir, "entry")
|
|
1072
1329
|
}
|
|
1073
1330
|
},
|
|
1074
|
-
|
|
1075
|
-
|
|
1331
|
+
plugins: [
|
|
1332
|
+
replace({
|
|
1333
|
+
...Object.fromEntries([";", "(", "{", "}", " ", " ", "\n"].map((d) => [`${d}global.`, `${d}globalThis.`])),
|
|
1334
|
+
preventAssignment: true
|
|
1335
|
+
}),
|
|
1336
|
+
virtual(flow.vfs),
|
|
1337
|
+
DynamicBasePlugin.vite({ sourcemap: flow.options.sourcemap })
|
|
1338
|
+
],
|
|
1339
|
+
server: {
|
|
1340
|
+
watch: {
|
|
1341
|
+
ignored: isIgnoredFlow
|
|
1342
|
+
},
|
|
1343
|
+
hmr: {
|
|
1344
|
+
protocol: "ws",
|
|
1345
|
+
clientPort: hmrPort,
|
|
1346
|
+
port: hmrPort
|
|
1347
|
+
},
|
|
1348
|
+
fs: {
|
|
1349
|
+
allow: [
|
|
1350
|
+
flow.options.appDir
|
|
1351
|
+
]
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}, flow.options.vite)
|
|
1076
1355
|
};
|
|
1077
|
-
if (
|
|
1078
|
-
ctx.config.
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
await nuxt.callHook("vite:extend", ctx);
|
|
1082
|
-
nuxt.hook("vite:serverCreated", (server, env) => {
|
|
1356
|
+
if (flow.options.dev)
|
|
1357
|
+
ctx.config.mode = process.env.NODE_ENV || "development";
|
|
1358
|
+
await flow.callHook("vite:extend", ctx);
|
|
1359
|
+
flow.hook("vite:serverCreated", (server) => {
|
|
1083
1360
|
ctx.nuxt.hook("app:templatesGenerated", () => {
|
|
1084
1361
|
for (const [id, mod] of server.moduleGraph.idToModuleMap) {
|
|
1085
|
-
if (id.includes("pages."))
|
|
1362
|
+
if (id.includes("pages.mjs"))
|
|
1086
1363
|
server.moduleGraph.invalidateModule(mod);
|
|
1087
|
-
if (id.startsWith("\
|
|
1364
|
+
if (id.startsWith("\0virtual:"))
|
|
1088
1365
|
server.moduleGraph.invalidateModule(mod);
|
|
1089
1366
|
}
|
|
1090
1367
|
});
|
|
1091
1368
|
const start = Date.now();
|
|
1092
|
-
warmupViteServer(server, [
|
|
1369
|
+
warmupViteServer(server, ["/entry.mjs"]).then(() => logger.info(`Vite server warmed up in ${Date.now() - start}ms`)).catch(logger.error);
|
|
1093
1370
|
});
|
|
1094
1371
|
await buildServer(ctx);
|
|
1095
1372
|
}
|
|
@@ -1104,16 +1381,12 @@ async function build(flow) {
|
|
|
1104
1381
|
if (event !== "change" && /app|error|plugins/i.test(path))
|
|
1105
1382
|
await generateApp$1();
|
|
1106
1383
|
});
|
|
1107
|
-
flow.hook("builder:generateApp",
|
|
1108
|
-
if (options)
|
|
1109
|
-
return generateApp(flow, app);
|
|
1110
|
-
return generateApp$1();
|
|
1111
|
-
});
|
|
1384
|
+
flow.hook("builder:generateApp", generateApp$1);
|
|
1112
1385
|
}
|
|
1113
|
-
await flow.callHook("build:before");
|
|
1386
|
+
await flow.callHook("build:before", { flow }, flow.options.build);
|
|
1114
1387
|
if (!flow.options._prepare) {
|
|
1115
1388
|
await bundle(flow);
|
|
1116
|
-
await flow.callHook("build:done");
|
|
1389
|
+
await flow.callHook("build:done", { flow });
|
|
1117
1390
|
}
|
|
1118
1391
|
if (!flow.options.dev)
|
|
1119
1392
|
await flow.callHook("close", flow);
|
|
@@ -1135,7 +1408,7 @@ function watch(flow) {
|
|
|
1135
1408
|
}
|
|
1136
1409
|
async function bundle(nuxt) {
|
|
1137
1410
|
try {
|
|
1138
|
-
return
|
|
1411
|
+
return bundleVite(nuxt);
|
|
1139
1412
|
} catch (error) {
|
|
1140
1413
|
await nuxt.callHook("build:error", error);
|
|
1141
1414
|
throw error;
|