@monkeyplus/flow 5.0.0-rc.233 → 5.0.0-rc.25
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 -64
- package/dist/head/runtime/plugin.mjs +1 -0
- package/dist/index.mjs +759 -483
- 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.25";
|
|
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,63 @@ 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
521
|
"@monkeyplus/flow/dist",
|
|
465
|
-
|
|
466
|
-
"C:/Users/gnu/Documents/GitHub/flow/packages/flow/dist/app",
|
|
467
|
-
distDir
|
|
522
|
+
"C:/Users/gnu/Documents/GitHub/flow/packages/flow/dist/app"
|
|
468
523
|
]
|
|
469
524
|
},
|
|
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
525
|
alias: {
|
|
478
|
-
// Vue 3 mocks
|
|
479
526
|
"estree-walker": "unenv/runtime/mock/proxy",
|
|
480
527
|
"@babel/parser": "unenv/runtime/mock/proxy",
|
|
481
|
-
// Paths
|
|
482
528
|
"#paths": resolve(distDir, "core/runtime/nitro/paths"),
|
|
483
|
-
// Shortcut to server main file
|
|
484
529
|
"#server": "#build/dist/server/server.mjs",
|
|
485
|
-
// Nuxt aliases
|
|
486
530
|
...flow.options.alias
|
|
487
531
|
},
|
|
488
532
|
rollupConfig: {
|
|
489
|
-
plugins: []
|
|
490
|
-
|
|
491
|
-
// watchOptions:{}
|
|
492
|
-
//* Include to refresh server
|
|
493
|
-
devServer: {
|
|
494
|
-
watch: [resolve(flow.options.buildDir, "dist/server/server.mjs")]
|
|
533
|
+
plugins: [],
|
|
534
|
+
external: [""]
|
|
495
535
|
}
|
|
496
536
|
});
|
|
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
537
|
await flow.callHook("nitro:config", nitroConfig);
|
|
538
|
+
nitroConfig.handlers.unshift({
|
|
539
|
+
middleware: true,
|
|
540
|
+
handler: resolve(distDir, "core/runtime/nitro/flow")
|
|
541
|
+
});
|
|
507
542
|
const nitro = await createNitro(nitroConfig);
|
|
508
|
-
flow._nitro = nitro;
|
|
509
|
-
flow.nitro = nitro;
|
|
510
543
|
await flow.callHook("nitro:init", nitro);
|
|
511
544
|
nitro.vfs = flow.vfs = nitro.vfs || flow.vfs || {};
|
|
512
545
|
flow.hook("close", () => nitro.hooks.callHook("close"));
|
|
513
|
-
nitro.hooks.hook("
|
|
514
|
-
|
|
546
|
+
nitro.hooks.hook("rollup:before", (nitro2) => {
|
|
547
|
+
const plugin = ImportProtectionPlugin.rollup({
|
|
548
|
+
rootDir: flow.options.rootDir,
|
|
549
|
+
patterns: [
|
|
550
|
+
...["#app", /^#build(\/|$)/].map((p) => [p, "Flow app aliases are not allowed in server routes."])
|
|
551
|
+
]
|
|
552
|
+
});
|
|
553
|
+
nitro2.options.rollupConfig.plugins.push(plugin);
|
|
515
554
|
});
|
|
516
|
-
const
|
|
517
|
-
nitro.options.devHandlers.unshift({ handler:
|
|
518
|
-
nitro.options.devHandlers.push(...
|
|
555
|
+
const devMidlewareHandler = dynamicEventHandler();
|
|
556
|
+
nitro.options.devHandlers.unshift({ handler: devMidlewareHandler });
|
|
557
|
+
nitro.options.devHandlers.push(...devHandlers);
|
|
519
558
|
nitro.options.handlers.unshift({
|
|
520
|
-
route: "/
|
|
559
|
+
route: "/__flow_error",
|
|
521
560
|
lazy: true,
|
|
522
561
|
handler: resolve(distDir, "core/runtime/nitro/renderer")
|
|
523
562
|
});
|
|
524
563
|
flow.hook("prepare:types", async (opts) => {
|
|
525
|
-
if (
|
|
564
|
+
if (flow.options._prepare) {
|
|
526
565
|
await scanHandlers(nitro);
|
|
527
566
|
await writeTypes(nitro);
|
|
528
567
|
}
|
|
@@ -531,15 +570,21 @@ async function initNitro(flow) {
|
|
|
531
570
|
flow.hook("build:done", async () => {
|
|
532
571
|
await flow.callHook("nitro:build:before", nitro);
|
|
533
572
|
if (flow.options.dev) {
|
|
534
|
-
await build$
|
|
573
|
+
await build$2(nitro);
|
|
535
574
|
} else {
|
|
536
|
-
console.log("build", flow.options._generate);
|
|
537
575
|
await prepare(nitro);
|
|
538
576
|
await copyPublicAssets(nitro);
|
|
539
577
|
await prerender(nitro);
|
|
540
578
|
if (!flow.options._generate) {
|
|
541
|
-
await build$
|
|
579
|
+
await build$2(nitro);
|
|
542
580
|
} else {
|
|
581
|
+
const nitroDev = await createNitro({
|
|
582
|
+
...nitro.options._config,
|
|
583
|
+
rootDir: nitro.options.rootDir,
|
|
584
|
+
logLevel: 0,
|
|
585
|
+
preset: "nitro-prerender"
|
|
586
|
+
});
|
|
587
|
+
flow.server = nitroDev;
|
|
543
588
|
const distDir2 = resolve(flow.options.rootDir, "dist");
|
|
544
589
|
if (!existsSync(distDir2))
|
|
545
590
|
await promises.symlink(nitro.options.output.publicDir, distDir2, "junction").catch(() => {
|
|
@@ -548,27 +593,37 @@ async function initNitro(flow) {
|
|
|
548
593
|
}
|
|
549
594
|
});
|
|
550
595
|
if (flow.options.dev) {
|
|
551
|
-
flow.hook("
|
|
552
|
-
|
|
596
|
+
flow.hook("build:compile", ({ compiler }) => {
|
|
597
|
+
compiler.outputFileSystem = { ...fse, join };
|
|
553
598
|
});
|
|
554
|
-
flow.hook("server:
|
|
555
|
-
|
|
599
|
+
flow.hook("server:devMiddleware", (m) => {
|
|
600
|
+
devMidlewareHandler.set(toEventHandler(m));
|
|
556
601
|
});
|
|
557
602
|
flow.server = createDevServer(nitro);
|
|
603
|
+
flow.hook("build:resources", () => {
|
|
604
|
+
flow.server.reload();
|
|
605
|
+
});
|
|
558
606
|
const waitUntilCompile = new Promise((resolve2) => nitro.hooks.hook("compiled", () => resolve2()));
|
|
559
607
|
flow.hook("build:done", () => waitUntilCompile);
|
|
560
608
|
}
|
|
561
609
|
}
|
|
610
|
+
async function resolveHandlers(flow) {
|
|
611
|
+
const handlers = [...flow.options.serverHandlers];
|
|
612
|
+
const devHandlers = [...flow.options.devServerHandlers];
|
|
613
|
+
return {
|
|
614
|
+
handlers,
|
|
615
|
+
devHandlers
|
|
616
|
+
};
|
|
617
|
+
}
|
|
562
618
|
|
|
563
619
|
const addModuleTranspiles = (opts = {}) => {
|
|
564
620
|
const flow = useNuxt();
|
|
565
621
|
const modules = [
|
|
566
622
|
...opts.additionalModules || [],
|
|
567
|
-
// ...flow.options.buildModules,
|
|
568
623
|
...flow.options.modules,
|
|
569
624
|
...flow.options._modules
|
|
570
625
|
].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)
|
|
626
|
+
flow.options.build.transpile = flow.options.build.transpile.map((m) => typeof m === "string" ? m.split("node_modules/").pop() : m);
|
|
572
627
|
function isTranspilePresent(mod) {
|
|
573
628
|
return flow.options.build.transpile.some((t) => !(t instanceof Function) && (t instanceof RegExp ? t.test(mod) : new RegExp(t).test(mod)));
|
|
574
629
|
}
|
|
@@ -581,10 +636,7 @@ const addModuleTranspiles = (opts = {}) => {
|
|
|
581
636
|
function createFlow(options) {
|
|
582
637
|
const hooks = createHooks();
|
|
583
638
|
const flow = {
|
|
584
|
-
|
|
585
|
-
_version: "3.0.0",
|
|
586
|
-
//* Include version
|
|
587
|
-
// @ts-ignore
|
|
639
|
+
_version: "3.0.0-rc.3",
|
|
588
640
|
version,
|
|
589
641
|
options,
|
|
590
642
|
hooks,
|
|
@@ -598,12 +650,10 @@ function createFlow(options) {
|
|
|
598
650
|
return flow;
|
|
599
651
|
}
|
|
600
652
|
async function initFlow(flow) {
|
|
601
|
-
flow.hooks.addHooks(flow.options.hooks);
|
|
602
653
|
nuxtCtx.set(flow);
|
|
603
654
|
flow.hook("close", () => nuxtCtx.unset());
|
|
604
|
-
await flow.callHook("modules:before");
|
|
655
|
+
await flow.callHook("modules:before", { nuxt: flow });
|
|
605
656
|
const modulesToInstall = [
|
|
606
|
-
// ...flow.options.buildModules,
|
|
607
657
|
...flow.options.modules,
|
|
608
658
|
...flow.options._modules
|
|
609
659
|
];
|
|
@@ -613,26 +663,17 @@ async function initFlow(flow) {
|
|
|
613
663
|
else
|
|
614
664
|
await installModule(m, {});
|
|
615
665
|
}
|
|
616
|
-
await flow.callHook("modules:done");
|
|
617
|
-
|
|
618
|
-
addModuleTranspiles();
|
|
666
|
+
await flow.callHook("modules:done", { nuxt: flow });
|
|
667
|
+
await addModuleTranspiles();
|
|
619
668
|
await initNitro(flow);
|
|
620
669
|
await flow.callHook("ready", flow);
|
|
621
670
|
}
|
|
622
671
|
async function loadFlow(opts) {
|
|
623
|
-
const start1 = Date.now();
|
|
624
672
|
const options = await loadFlowConfig(opts);
|
|
625
|
-
logger.info(`Config loading in ${Date.now() - start1}ms`);
|
|
626
673
|
options.appDir = resolve(distDir, "app");
|
|
627
674
|
options.alias["#app"] = resolve(distDir, "app/index");
|
|
628
675
|
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"));
|
|
676
|
+
options._modules.push(pagesModule, metaModule, autoImportsModule, viteModule);
|
|
636
677
|
options.modulesDir.push(resolve(pkgDir, "node_modules"));
|
|
637
678
|
const flow = createFlow(options);
|
|
638
679
|
if (opts.ready !== false)
|
|
@@ -647,17 +688,11 @@ const serverPluginTemplate = {
|
|
|
647
688
|
filename: "plugins/server.mjs",
|
|
648
689
|
getContents(ctx) {
|
|
649
690
|
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
691
|
return [
|
|
659
|
-
|
|
660
|
-
`export default ${genArrayFromRaw(
|
|
692
|
+
templateUtils.importSources(serverPlugins.map((p) => p.src)),
|
|
693
|
+
`export default ${genArrayFromRaw([
|
|
694
|
+
...serverPlugins.map((p) => templateUtils.importName(p.src))
|
|
695
|
+
])}`
|
|
661
696
|
].join("\n");
|
|
662
697
|
}
|
|
663
698
|
};
|
|
@@ -686,52 +721,43 @@ export { }
|
|
|
686
721
|
`;
|
|
687
722
|
}
|
|
688
723
|
};
|
|
689
|
-
const adHocModules = ["imports", "meta", "pages"];
|
|
724
|
+
const adHocModules = ["auto-imports", "meta", "pages", "vite-client"];
|
|
690
725
|
const schemaTemplate = {
|
|
691
726
|
filename: "types/schema.d.ts",
|
|
692
|
-
getContents:
|
|
727
|
+
getContents: ({ nuxt }) => {
|
|
693
728
|
const moduleInfo = nuxt.options._installedModules.map((m) => ({
|
|
694
729
|
...m.meta || {},
|
|
695
730
|
importName: m.entryPath || m.meta?.name
|
|
696
731
|
})).filter((m) => m.configKey && m.name && !adHocModules.includes(m.name));
|
|
697
|
-
const relativeRoot = relative(resolve(nuxt.options.buildDir, "types"), nuxt.options.rootDir);
|
|
698
732
|
return [
|
|
699
733
|
"import { FlowModule } from '@monkeyplus/flow-schema'",
|
|
700
734
|
"declare module '@monkeyplus/flow-schema' {",
|
|
701
735
|
" 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
|
-
),
|
|
736
|
+
...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
737
|
" }",
|
|
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
|
-
),
|
|
738
|
+
generateTypes(resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== "public"))), {
|
|
739
|
+
interfaceName: "RuntimeConfig",
|
|
740
|
+
addExport: false,
|
|
741
|
+
addDefaults: false,
|
|
742
|
+
allowExtraKeys: false,
|
|
743
|
+
indentation: 2
|
|
744
|
+
}),
|
|
745
|
+
generateTypes(resolveSchema(nuxt.options.runtimeConfig.public), {
|
|
746
|
+
interfaceName: "PublicRuntimeConfig",
|
|
747
|
+
addExport: false,
|
|
748
|
+
addDefaults: false,
|
|
749
|
+
allowExtraKeys: false,
|
|
750
|
+
indentation: 2
|
|
751
|
+
}),
|
|
726
752
|
"}"
|
|
727
753
|
].join("\n");
|
|
728
754
|
}
|
|
729
755
|
};
|
|
730
756
|
const publicPathTemplate = {
|
|
731
757
|
filename: "paths.mjs",
|
|
732
|
-
|
|
758
|
+
getContents({ nuxt }) {
|
|
733
759
|
return [
|
|
734
|
-
|
|
760
|
+
"import { joinURL } from 'ufo'",
|
|
735
761
|
!nuxt.options.dev && "import { useRuntimeConfig } from '#internal/nitro'",
|
|
736
762
|
nuxt.options.dev ? `const appConfig = ${JSON.stringify(nuxt.options.app)}` : "const appConfig = useRuntimeConfig().app",
|
|
737
763
|
"export const baseURL = () => appConfig.baseURL",
|
|
@@ -744,39 +770,15 @@ const publicPathTemplate = {
|
|
|
744
770
|
].filter(Boolean).join("\n");
|
|
745
771
|
}
|
|
746
772
|
};
|
|
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
773
|
|
|
760
774
|
const defaultTemplates = {
|
|
761
775
|
__proto__: null,
|
|
776
|
+
serverPluginTemplate: serverPluginTemplate,
|
|
762
777
|
pluginsDeclaration: pluginsDeclaration,
|
|
763
|
-
publicPathTemplate: publicPathTemplate,
|
|
764
778
|
schemaTemplate: schemaTemplate,
|
|
765
|
-
|
|
779
|
+
publicPathTemplate: publicPathTemplate
|
|
766
780
|
};
|
|
767
781
|
|
|
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
782
|
function createApp(flow, options = {}) {
|
|
781
783
|
return defu(options, {
|
|
782
784
|
dir: flow.options.srcDir,
|
|
@@ -785,95 +787,51 @@ function createApp(flow, options = {}) {
|
|
|
785
787
|
templates: []
|
|
786
788
|
});
|
|
787
789
|
}
|
|
788
|
-
async function generateApp(flow, app
|
|
790
|
+
async function generateApp(flow, app) {
|
|
789
791
|
await resolveApp(flow, app);
|
|
790
|
-
app.templates = Object.values(defaultTemplates).concat(
|
|
791
|
-
flow.options.build.templates
|
|
792
|
-
);
|
|
792
|
+
app.templates = Object.values(defaultTemplates).concat(flow.options.build.templates);
|
|
793
793
|
await flow.callHook("app:templates", app);
|
|
794
794
|
app.templates = app.templates.map((tmpl) => normalizeTemplate(tmpl));
|
|
795
795
|
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
|
-
);
|
|
796
|
+
await Promise.all(app.templates.map(async (template) => {
|
|
797
|
+
const contents = await compileTemplate(template, templateContext);
|
|
798
|
+
const fullPath = template.dst || resolve(flow.options.buildDir, template.filename);
|
|
799
|
+
flow.vfs[fullPath] = contents;
|
|
800
|
+
const aliasPath = `#build/${template.filename.replace(/\.\w+$/, "")}`;
|
|
801
|
+
flow.vfs[aliasPath] = contents;
|
|
802
|
+
if (process.platform === "win32")
|
|
803
|
+
flow.vfs[fullPath.replace(/\//g, "\\")] = contents;
|
|
804
|
+
if (template.write) {
|
|
805
|
+
await promises.mkdir(dirname(fullPath), { recursive: true });
|
|
806
|
+
await promises.writeFile(fullPath, contents, "utf8");
|
|
807
|
+
}
|
|
808
|
+
}));
|
|
811
809
|
await flow.callHook("app:templatesGenerated", app);
|
|
812
810
|
}
|
|
813
811
|
async function resolveApp(flow, app) {
|
|
814
|
-
app.plugins = [
|
|
815
|
-
...flow.options.plugins.map(normalizePlugin)
|
|
816
|
-
];
|
|
812
|
+
app.plugins = [...flow.options.plugins.map(normalizePlugin)];
|
|
817
813
|
for (const config of flow.options._layers.map((layer) => layer.config)) {
|
|
818
814
|
app.plugins.push(...[
|
|
819
815
|
...config.plugins || [],
|
|
820
|
-
...
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
])
|
|
816
|
+
...await resolveFilesFlow(config.srcDir, [
|
|
817
|
+
"plugins/*.{ts,js,mjs,cjs,mts,cts}",
|
|
818
|
+
"plugins/*/index.*{ts,js,mjs,cjs,mts,cts}"
|
|
819
|
+
])
|
|
824
820
|
].map((plugin) => normalizePlugin(plugin)));
|
|
825
821
|
}
|
|
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
822
|
app.plugins = uniqueBy(app.plugins, "src");
|
|
823
|
+
await flow.callHook("app:resolve", app);
|
|
834
824
|
}
|
|
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
|
-
};
|
|
825
|
+
function uniqueBy(arr, key) {
|
|
826
|
+
const res = [];
|
|
827
|
+
const seen = /* @__PURE__ */ new Set();
|
|
828
|
+
for (const item of arr) {
|
|
829
|
+
if (seen.has(item[key]))
|
|
830
|
+
continue;
|
|
831
|
+
seen.add(item[key]);
|
|
832
|
+
res.push(item);
|
|
833
|
+
}
|
|
834
|
+
return res;
|
|
877
835
|
}
|
|
878
836
|
|
|
879
837
|
async function warmupViteServer(server, entries) {
|
|
@@ -894,11 +852,6 @@ async function warmupViteServer(server, entries) {
|
|
|
894
852
|
await Promise.all(entries.map((entry) => warmup(entry)));
|
|
895
853
|
}
|
|
896
854
|
|
|
897
|
-
const wpfs = {
|
|
898
|
-
...fse,
|
|
899
|
-
join
|
|
900
|
-
};
|
|
901
|
-
|
|
902
855
|
function cacheDirPlugin(rootDir, name) {
|
|
903
856
|
const optimizeCacheDir = resolve(rootDir, "node_modules/.cache/vite", name);
|
|
904
857
|
return {
|
|
@@ -909,42 +862,262 @@ function cacheDirPlugin(rootDir, name) {
|
|
|
909
862
|
};
|
|
910
863
|
}
|
|
911
864
|
|
|
912
|
-
|
|
865
|
+
const wpfs = {
|
|
866
|
+
...fse,
|
|
867
|
+
join
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
function uniq(arr) {
|
|
871
|
+
return Array.from(new Set(arr));
|
|
872
|
+
}
|
|
873
|
+
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|less|stylus|styl)(\?[^.]+)?$/;
|
|
874
|
+
function isCSS(file) {
|
|
875
|
+
return IS_CSS_RE.test(file);
|
|
876
|
+
}
|
|
877
|
+
function hashId(id) {
|
|
878
|
+
return `$id_${hash(id)}`;
|
|
879
|
+
}
|
|
880
|
+
function hash(input, length = 8) {
|
|
881
|
+
return createHash("sha256").update(input).digest("hex").slice(0, length);
|
|
882
|
+
}
|
|
883
|
+
function readDirRecursively(dir) {
|
|
884
|
+
return readdirSync(dir).reduce((files, file) => {
|
|
885
|
+
const name = join(dir, file);
|
|
886
|
+
const isDirectory2 = statSync(name).isDirectory();
|
|
887
|
+
return isDirectory2 ? [...files, ...readDirRecursively(name)] : [...files, name];
|
|
888
|
+
}, []);
|
|
889
|
+
}
|
|
890
|
+
async function isDirectory(path) {
|
|
891
|
+
try {
|
|
892
|
+
return (await promises.stat(path)).isDirectory();
|
|
893
|
+
} catch (_err) {
|
|
894
|
+
return false;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
function isExternal(opts, id) {
|
|
899
|
+
const ssrConfig = opts.viteServer.config.ssr;
|
|
900
|
+
const externalOpts = {
|
|
901
|
+
inline: [
|
|
902
|
+
/virtual:/,
|
|
903
|
+
/\.ts$/,
|
|
904
|
+
...ExternalsDefaults.inline,
|
|
905
|
+
...ssrConfig.noExternal
|
|
906
|
+
],
|
|
907
|
+
external: [
|
|
908
|
+
/node_modules/
|
|
909
|
+
],
|
|
910
|
+
resolve: {
|
|
911
|
+
type: "module",
|
|
912
|
+
extensions: [".ts", ".js", ".json", ".vue", ".mjs", ".jsx", ".tsx", ".wasm"]
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
return isExternal$1(id, opts.viteServer.config.root, externalOpts);
|
|
916
|
+
}
|
|
917
|
+
async function transformRequest(opts, id) {
|
|
918
|
+
if (id && id.startsWith("/@id/__x00__"))
|
|
919
|
+
id = `\0${id.slice("/@id/__x00__".length)}`;
|
|
920
|
+
if (id && id.startsWith("/@id/"))
|
|
921
|
+
id = id.slice("/@id/".length);
|
|
922
|
+
if (id && id.startsWith("/@fs/")) {
|
|
923
|
+
id = id.slice("/@fs".length);
|
|
924
|
+
if (id.match(/^\/\w:/))
|
|
925
|
+
id = id.slice(1);
|
|
926
|
+
} else if (!id.includes("entry") && id.startsWith("/")) {
|
|
927
|
+
const resolvedPath = resolve(opts.viteServer.config.root, `.${id}`);
|
|
928
|
+
if (existsSync(resolvedPath))
|
|
929
|
+
id = resolvedPath;
|
|
930
|
+
}
|
|
931
|
+
const withoutVersionQuery = id.replace(/\?v=\w+$/, "");
|
|
932
|
+
if (await isExternal(opts, withoutVersionQuery)) {
|
|
933
|
+
const path = builtinModules.includes(withoutVersionQuery.split("node:").pop()) ? withoutVersionQuery : pathToFileURL(withoutVersionQuery).href;
|
|
934
|
+
return {
|
|
935
|
+
code: `(global, module, _, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) =>
|
|
936
|
+
${genDynamicImport(path, { wrapper: false })}
|
|
937
|
+
.then(r => {
|
|
938
|
+
if (r.default && r.default.__esModule)
|
|
939
|
+
r = r.default
|
|
940
|
+
exports.default = r.default
|
|
941
|
+
ssrExportAll(r)
|
|
942
|
+
})
|
|
943
|
+
.catch(e => {
|
|
944
|
+
console.error(e)
|
|
945
|
+
throw new Error(${JSON.stringify(`[vite dev] Error loading external "${id}".`)})
|
|
946
|
+
})`,
|
|
947
|
+
deps: [],
|
|
948
|
+
dynamicDeps: []
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
const res = await opts.viteServer.transformRequest(id, { ssr: true }).catch((err) => {
|
|
952
|
+
console.warn(`[SSR] Error transforming ${id}:`, err);
|
|
953
|
+
}) || { code: "", map: {}, deps: [], dynamicDeps: [] };
|
|
954
|
+
const code = `async function (global, module, exports, __vite_ssr_exports__, __vite_ssr_import_meta__, __vite_ssr_import__, __vite_ssr_dynamic_import__, __vite_ssr_exportAll__) {
|
|
955
|
+
${res.code || "/* empty */"};
|
|
956
|
+
}`;
|
|
957
|
+
return { code, deps: res.deps || [], dynamicDeps: res.dynamicDeps || [] };
|
|
958
|
+
}
|
|
959
|
+
async function transformRequestRecursive(opts, id, parent = "<entry>", chunks = {}) {
|
|
960
|
+
if (chunks[id]) {
|
|
961
|
+
chunks[id].parents.push(parent);
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
const res = await transformRequest(opts, id);
|
|
965
|
+
const deps = uniq([...res.deps, ...res.dynamicDeps]);
|
|
966
|
+
chunks[id] = {
|
|
967
|
+
id,
|
|
968
|
+
code: res.code,
|
|
969
|
+
deps,
|
|
970
|
+
parents: [parent]
|
|
971
|
+
};
|
|
972
|
+
for (const dep of deps)
|
|
973
|
+
await transformRequestRecursive(opts, dep, id, chunks);
|
|
974
|
+
return Object.values(chunks);
|
|
975
|
+
}
|
|
976
|
+
async function bundleRequest(opts, entryURL) {
|
|
977
|
+
const chunks = await transformRequestRecursive(opts, entryURL);
|
|
978
|
+
const listIds = (ids) => ids.map((id) => `// - ${id} (${hashId(id)})`).join("\n");
|
|
979
|
+
const chunksCode = chunks.map((chunk) => `
|
|
980
|
+
// --------------------
|
|
981
|
+
// Request: ${chunk.id}
|
|
982
|
+
// Parents:
|
|
983
|
+
${listIds(chunk.parents)}
|
|
984
|
+
// Dependencies:
|
|
985
|
+
${listIds(chunk.deps)}
|
|
986
|
+
// --------------------
|
|
987
|
+
const ${hashId(chunk.id)} = ${chunk.code}
|
|
988
|
+
`).join("\n");
|
|
989
|
+
const manifestCode = `const __modules__ = ${genObjectFromRawEntries(chunks.map((chunk) => [chunk.id, hashId(chunk.id)]))}`;
|
|
990
|
+
const ssrModuleLoader = `
|
|
991
|
+
const __pendingModules__ = new Map()
|
|
992
|
+
const __pendingImports__ = new Map()
|
|
993
|
+
const __ssrContext__ = { global: globalThis }
|
|
994
|
+
|
|
995
|
+
function __ssrLoadModule__(url, urlStack = []) {
|
|
996
|
+
const pendingModule = __pendingModules__.get(url)
|
|
997
|
+
if (pendingModule) { return pendingModule }
|
|
998
|
+
const modulePromise = __instantiateModule__(url, urlStack)
|
|
999
|
+
__pendingModules__.set(url, modulePromise)
|
|
1000
|
+
modulePromise.catch(() => { __pendingModules__.delete(url) })
|
|
1001
|
+
.finally(() => { __pendingModules__.delete(url) })
|
|
1002
|
+
return modulePromise
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
async function __instantiateModule__(url, urlStack) {
|
|
1006
|
+
const mod = __modules__[url]
|
|
1007
|
+
if (mod.stubModule) { return mod.stubModule }
|
|
1008
|
+
const stubModule = { [Symbol.toStringTag]: 'Module' }
|
|
1009
|
+
Object.defineProperty(stubModule, '__esModule', { value: true })
|
|
1010
|
+
mod.stubModule = stubModule
|
|
1011
|
+
// https://vitejs.dev/guide/api-hmr.html
|
|
1012
|
+
const importMeta = { url, hot: { accept() {}, prune() {}, dispose() {}, invalidate() {}, decline() {}, on() {} } }
|
|
1013
|
+
urlStack = urlStack.concat(url)
|
|
1014
|
+
const isCircular = url => urlStack.includes(url)
|
|
1015
|
+
const pendingDeps = []
|
|
1016
|
+
const ssrImport = async (dep) => {
|
|
1017
|
+
// TODO: Handle externals if dep[0] !== '.' | '/'
|
|
1018
|
+
if (!isCircular(dep) && !__pendingImports__.get(dep)?.some(isCircular)) {
|
|
1019
|
+
pendingDeps.push(dep)
|
|
1020
|
+
if (pendingDeps.length === 1) {
|
|
1021
|
+
__pendingImports__.set(url, pendingDeps)
|
|
1022
|
+
}
|
|
1023
|
+
await __ssrLoadModule__(dep, urlStack)
|
|
1024
|
+
if (pendingDeps.length === 1) {
|
|
1025
|
+
__pendingImports__.delete(url)
|
|
1026
|
+
} else {
|
|
1027
|
+
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
return __modules__[dep].stubModule
|
|
1031
|
+
}
|
|
1032
|
+
function ssrDynamicImport (dep) {
|
|
1033
|
+
// TODO: Handle dynamic import starting with . relative to url
|
|
1034
|
+
return ssrImport(dep)
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
function ssrExportAll(sourceModule) {
|
|
1038
|
+
for (const key in sourceModule) {
|
|
1039
|
+
if (key !== 'default') {
|
|
1040
|
+
try {
|
|
1041
|
+
Object.defineProperty(stubModule, key, {
|
|
1042
|
+
enumerable: true,
|
|
1043
|
+
configurable: true,
|
|
1044
|
+
get() { return sourceModule[key] }
|
|
1045
|
+
})
|
|
1046
|
+
} catch (_err) { }
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
const cjsModule = {
|
|
1052
|
+
get exports () {
|
|
1053
|
+
return stubModule.default
|
|
1054
|
+
},
|
|
1055
|
+
set exports (v) {
|
|
1056
|
+
stubModule.default = v
|
|
1057
|
+
},
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
await mod(
|
|
1061
|
+
__ssrContext__.global,
|
|
1062
|
+
cjsModule,
|
|
1063
|
+
stubModule.default,
|
|
1064
|
+
stubModule,
|
|
1065
|
+
importMeta,
|
|
1066
|
+
ssrImport,
|
|
1067
|
+
ssrDynamicImport,
|
|
1068
|
+
ssrExportAll
|
|
1069
|
+
)
|
|
1070
|
+
|
|
1071
|
+
return stubModule
|
|
1072
|
+
}
|
|
1073
|
+
`;
|
|
1074
|
+
const code = [
|
|
1075
|
+
chunksCode,
|
|
1076
|
+
manifestCode,
|
|
1077
|
+
ssrModuleLoader,
|
|
1078
|
+
`export default await __ssrLoadModule__(${JSON.stringify(entryURL)})`
|
|
1079
|
+
].join("\n\n");
|
|
1080
|
+
return {
|
|
1081
|
+
code,
|
|
1082
|
+
ids: chunks.map((i) => i.id)
|
|
1083
|
+
};
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
async function writeManifest(ctx, extraEntries = []) {
|
|
1087
|
+
const clientDist = resolve(ctx.flow.options.buildDir, "dist/client");
|
|
1088
|
+
const serverDist = resolve(ctx.flow.options.buildDir, "dist/server");
|
|
1089
|
+
const entries = [
|
|
1090
|
+
"@vite/client",
|
|
1091
|
+
"entry.mjs",
|
|
1092
|
+
...extraEntries
|
|
1093
|
+
];
|
|
1094
|
+
const devClientManifest = {
|
|
1095
|
+
publicPath: joinURL(ctx.flow.options.app.baseURL, ctx.flow.options.app.buildAssetsDir),
|
|
1096
|
+
all: entries,
|
|
1097
|
+
initial: entries,
|
|
1098
|
+
async: [],
|
|
1099
|
+
modules: {}
|
|
1100
|
+
};
|
|
1101
|
+
const clientManifest = ctx.flow.options.dev ? devClientManifest : await fse.readJSON(resolve(clientDist, "manifest.json"));
|
|
1102
|
+
await fse.mkdirp(serverDist);
|
|
1103
|
+
await fse.writeFile(resolve(serverDist, "client.manifest.json"), JSON.stringify(clientManifest, null, 2), "utf8");
|
|
1104
|
+
await fse.writeFile(resolve(serverDist, "client.manifest.mjs"), `export default ${JSON.stringify(clientManifest, null, 2)}`, "utf8");
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
const buildServer = async (ctx) => {
|
|
913
1108
|
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
|
-
// },
|
|
1109
|
+
configFile: false,
|
|
929
1110
|
define: {
|
|
930
1111
|
"process.server": true,
|
|
931
|
-
"process.client": false,
|
|
932
1112
|
"typeof window": '"undefined"',
|
|
933
1113
|
"typeof document": '"undefined"',
|
|
934
1114
|
"typeof navigator": '"undefined"',
|
|
935
1115
|
"typeof location": '"undefined"',
|
|
936
1116
|
"typeof XMLHttpRequest": '"undefined"'
|
|
937
1117
|
},
|
|
938
|
-
resolve: {
|
|
939
|
-
alias: {
|
|
940
|
-
"#build/plugins": resolve(ctx.nuxt.options.buildDir, "plugins/server")
|
|
941
|
-
}
|
|
942
|
-
},
|
|
943
1118
|
ssr: {
|
|
944
|
-
external: ["#internal/nitro", "#internal/nitro/utils"],
|
|
945
1119
|
noExternal: [
|
|
946
|
-
...ctx.
|
|
947
|
-
// TODO: Use externality for production (rollup) build
|
|
1120
|
+
...ctx.flow.options.build.transpile,
|
|
948
1121
|
/\/esm\/.*\.js$/,
|
|
949
1122
|
/\.(es|esm|esm-browser|esm-bundler).js$/,
|
|
950
1123
|
"/__vue-jsx",
|
|
@@ -957,15 +1130,13 @@ async function buildServer(ctx) {
|
|
|
957
1130
|
]
|
|
958
1131
|
},
|
|
959
1132
|
build: {
|
|
960
|
-
outDir: resolve(ctx.
|
|
961
|
-
|
|
1133
|
+
outDir: resolve(ctx.flow.options.buildDir, "dist/server"),
|
|
1134
|
+
manifest: true,
|
|
962
1135
|
rollupOptions: {
|
|
963
1136
|
external: ["#internal/nitro"],
|
|
964
1137
|
output: {
|
|
965
1138
|
entryFileNames: "server.mjs",
|
|
966
1139
|
preferConst: true,
|
|
967
|
-
// TODO: https://github.com/vitejs/vite/pull/8641
|
|
968
|
-
inlineDynamicImports: !ctx.nuxt.options.experimental.viteServerDynamicImports,
|
|
969
1140
|
format: "module"
|
|
970
1141
|
},
|
|
971
1142
|
onwarn(warning, rollupWarn) {
|
|
@@ -975,31 +1146,39 @@ async function buildServer(ctx) {
|
|
|
975
1146
|
}
|
|
976
1147
|
},
|
|
977
1148
|
server: {
|
|
978
|
-
// https://github.com/vitest-dev/vitest/issues/229#issuecomment-1002685027
|
|
979
1149
|
preTransformRequests: false,
|
|
980
|
-
|
|
1150
|
+
cors: true
|
|
981
1151
|
},
|
|
982
1152
|
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(),
|
|
1153
|
+
cacheDirPlugin(ctx.flow.options.rootDir, "server")
|
|
990
1154
|
]
|
|
991
1155
|
});
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1156
|
+
await ctx.flow.callHook("vite:extendConfig", serverConfig, { isClient: false, isServer: true });
|
|
1157
|
+
ctx.flow.hook("nitro:build:before", async () => {
|
|
1158
|
+
if (ctx.flow.options.dev)
|
|
1159
|
+
return;
|
|
1160
|
+
const clientDist = resolve(ctx.flow.options.buildDir, "dist/client");
|
|
1161
|
+
const publicDir = join(ctx.flow.options.srcDir, ctx.flow.options.dir.public);
|
|
1162
|
+
let publicFiles = [];
|
|
1163
|
+
if (await isDirectory(publicDir)) {
|
|
1164
|
+
publicFiles = readDirRecursively(publicDir).map((r) => r.replace(publicDir, ""));
|
|
1165
|
+
for (const file of publicFiles) {
|
|
1166
|
+
try {
|
|
1167
|
+
fse.rmSync(join(clientDist, file));
|
|
1168
|
+
} catch {
|
|
1169
|
+
}
|
|
997
1170
|
}
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1171
|
+
}
|
|
1172
|
+
if (await isDirectory(clientDist)) {
|
|
1173
|
+
const nestedAssetsPath = withoutTrailingSlash(join(clientDist, ctx.flow.options.app.buildAssetsDir));
|
|
1174
|
+
if (await isDirectory(nestedAssetsPath)) {
|
|
1175
|
+
await fse.copy(nestedAssetsPath, clientDist, { recursive: true });
|
|
1176
|
+
await fse.remove(nestedAssetsPath);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
const onBuild = () => ctx.flow.callHook("build:resources", wpfs);
|
|
1181
|
+
if (!ctx.flow.options.dev) {
|
|
1003
1182
|
const start = Date.now();
|
|
1004
1183
|
logger.info("Building server...");
|
|
1005
1184
|
await vite.build(serverConfig);
|
|
@@ -1009,87 +1188,188 @@ async function buildServer(ctx) {
|
|
|
1009
1188
|
}
|
|
1010
1189
|
const viteServer = await vite.createServer(serverConfig);
|
|
1011
1190
|
ctx.ssrServer = viteServer;
|
|
1012
|
-
await ctx.
|
|
1013
|
-
ctx.
|
|
1191
|
+
await ctx.flow.callHook("vite:serverCreated", viteServer, { isClient: false, isServer: true });
|
|
1192
|
+
ctx.flow.hook("close", () => viteServer.close());
|
|
1014
1193
|
await viteServer.pluginContainer.buildStart({});
|
|
1015
|
-
|
|
1194
|
+
const _doBuild = async () => {
|
|
1195
|
+
const start = Date.now();
|
|
1196
|
+
const { code, ids } = await bundleRequest({ viteServer }, resolve(ctx.flow.options.appDir, "entry"));
|
|
1197
|
+
await fse.ensureFile(resolve(ctx.flow.options.buildDir, "dist/server/server.mjs"));
|
|
1198
|
+
await fse.writeFile(resolve(ctx.flow.options.buildDir, "dist/server/server.mjs"), code, "utf-8");
|
|
1199
|
+
await writeManifest(ctx, ids.filter(isCSS).map((i) => i.slice(1)));
|
|
1200
|
+
const time = Date.now() - start;
|
|
1201
|
+
logger.success(`Vite server built in ${time}ms`);
|
|
1202
|
+
await onBuild();
|
|
1203
|
+
ctx.flow.callHook("bundler:change", {});
|
|
1204
|
+
};
|
|
1205
|
+
const doBuild = debounce(_doBuild, 50);
|
|
1206
|
+
await _doBuild();
|
|
1207
|
+
viteServer.watcher.on("all", (_event, file) => {
|
|
1208
|
+
if (file.includes("/pages/"))
|
|
1209
|
+
return;
|
|
1210
|
+
file = normalize(file);
|
|
1211
|
+
if (file.indexOf(ctx.flow.options.buildDir) === 0)
|
|
1212
|
+
return;
|
|
1213
|
+
doBuild();
|
|
1214
|
+
});
|
|
1215
|
+
ctx.flow.hook("app:templatesGenerated", () => doBuild());
|
|
1216
|
+
};
|
|
1217
|
+
|
|
1218
|
+
const PREFIX = "virtual:";
|
|
1219
|
+
function virtual(vfs) {
|
|
1220
|
+
const extensions = ["", ".ts", ".vue", ".mjs", ".cjs", ".js", ".json"];
|
|
1221
|
+
const resolveWithExt = (id) => {
|
|
1222
|
+
for (const ext of extensions) {
|
|
1223
|
+
const rId = id + ext;
|
|
1224
|
+
if (rId in vfs)
|
|
1225
|
+
return rId;
|
|
1226
|
+
}
|
|
1227
|
+
return null;
|
|
1228
|
+
};
|
|
1229
|
+
return {
|
|
1230
|
+
name: "virtual",
|
|
1231
|
+
resolveId(id, importer) {
|
|
1232
|
+
if (process.platform === "win32" && isAbsolute(id)) {
|
|
1233
|
+
id = resolve(id);
|
|
1234
|
+
}
|
|
1235
|
+
const resolvedId = resolveWithExt(id);
|
|
1236
|
+
if (resolvedId)
|
|
1237
|
+
return PREFIX + resolvedId;
|
|
1238
|
+
if (importer && !isAbsolute(id)) {
|
|
1239
|
+
const importerNoPrefix = importer.startsWith(PREFIX) ? importer.slice(PREFIX.length) : importer;
|
|
1240
|
+
const importedDir = dirname(importerNoPrefix);
|
|
1241
|
+
const resolved = resolveWithExt(join(importedDir, id));
|
|
1242
|
+
if (resolved)
|
|
1243
|
+
return PREFIX + resolved;
|
|
1244
|
+
}
|
|
1245
|
+
return null;
|
|
1246
|
+
},
|
|
1247
|
+
load(id) {
|
|
1248
|
+
if (!id.startsWith(PREFIX))
|
|
1249
|
+
return null;
|
|
1250
|
+
const idNoPrefix = id.slice(PREFIX.length);
|
|
1251
|
+
return {
|
|
1252
|
+
code: vfs[idNoPrefix],
|
|
1253
|
+
map: null
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
};
|
|
1016
1257
|
}
|
|
1017
1258
|
|
|
1018
|
-
|
|
1019
|
-
|
|
1259
|
+
const VITE_ASSET_RE = /^export default ["'](__VITE_ASSET.*)["']$/;
|
|
1260
|
+
const DynamicBasePlugin = createUnplugin((options = {}) => {
|
|
1261
|
+
return {
|
|
1262
|
+
name: "nuxt:dynamic-base-path",
|
|
1263
|
+
resolveId(id) {
|
|
1264
|
+
if (id.startsWith("/__NUXT_BASE__"))
|
|
1265
|
+
return id.replace("/__NUXT_BASE__", "");
|
|
1266
|
+
if (id === "#internal/nitro")
|
|
1267
|
+
return "#internal/nitro";
|
|
1268
|
+
return null;
|
|
1269
|
+
},
|
|
1270
|
+
enforce: "post",
|
|
1271
|
+
transform(code, id) {
|
|
1272
|
+
const s = new MagicString(code);
|
|
1273
|
+
if (options.globalPublicPath && id.includes("paths.mjs") && code.includes("const appConfig = "))
|
|
1274
|
+
s.append(`${options.globalPublicPath} = buildAssetsURL();
|
|
1275
|
+
`);
|
|
1276
|
+
const assetId = code.match(VITE_ASSET_RE);
|
|
1277
|
+
if (assetId) {
|
|
1278
|
+
s.overwrite(0, code.length, [
|
|
1279
|
+
"import { buildAssetsURL } from '#build/paths.mjs';",
|
|
1280
|
+
`export default buildAssetsURL("${assetId[1]}".replace("/__NUXT_BASE__", ""));`
|
|
1281
|
+
].join("\n"));
|
|
1282
|
+
}
|
|
1283
|
+
if (!id.includes("paths.mjs") && code.includes("NUXT_BASE") && !code.includes("import { publicAssetsURL as __publicAssetsURL }"))
|
|
1284
|
+
s.prepend("import { publicAssetsURL as __publicAssetsURL } from '#build/paths.mjs';\n");
|
|
1285
|
+
if (id === "vite/preload-helper") {
|
|
1286
|
+
s.prepend("import { buildAssetsDir } from '#build/paths.mjs';\n");
|
|
1287
|
+
s.replace(/const base = ['"]\/__NUXT_BASE__\/['"]/, "const base = buildAssetsDir()");
|
|
1288
|
+
}
|
|
1289
|
+
s.replace(/from *['"]\/__NUXT_BASE__(\/[^'"]*)['"]/g, 'from "$1"');
|
|
1290
|
+
const delimiterRE = /(?<!(const base = |from *))(`([^`]*)\/__NUXT_BASE__\/([^`]*)`|'([^']*)\/__NUXT_BASE__\/([^']*)'|"([^"]*)\/__NUXT_BASE__\/([^"]*)")/g;
|
|
1291
|
+
s.replace(delimiterRE, (r) => `\`${r.replace(/\/__NUXT_BASE__\//g, "${__publicAssetsURL()}").slice(1, -1)}\``);
|
|
1292
|
+
if (s.hasChanged()) {
|
|
1293
|
+
return {
|
|
1294
|
+
code: s.toString(),
|
|
1295
|
+
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
};
|
|
1300
|
+
});
|
|
1301
|
+
|
|
1302
|
+
async function bundleVite(flow) {
|
|
1303
|
+
const hmrPortDefault = 24678;
|
|
1304
|
+
const hmrPort = await getPort({
|
|
1305
|
+
port: hmrPortDefault,
|
|
1306
|
+
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
|
|
1307
|
+
});
|
|
1020
1308
|
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)
|
|
1309
|
+
nuxt: flow,
|
|
1310
|
+
flow,
|
|
1311
|
+
config: vite.mergeConfig({
|
|
1312
|
+
mode: flow.options.dev ? "development" : void 0,
|
|
1313
|
+
resolve: {
|
|
1314
|
+
alias: {
|
|
1315
|
+
...flow.options.alias,
|
|
1316
|
+
"#app": flow.options.appDir,
|
|
1317
|
+
"#build/plugins": resolve(flow.options.buildDir, "plugins/server"),
|
|
1318
|
+
"#build": flow.options.buildDir,
|
|
1319
|
+
"/entry.mjs": resolve(flow.options.appDir, "entry")
|
|
1320
|
+
}
|
|
1321
|
+
},
|
|
1322
|
+
optimizeDeps: {
|
|
1323
|
+
entries: [
|
|
1324
|
+
resolve(flow.options.appDir, "entry.ts")
|
|
1060
1325
|
],
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
allow: [
|
|
1068
|
-
nuxt.options.appDir
|
|
1069
|
-
]
|
|
1070
|
-
},
|
|
1071
|
-
hmr: false
|
|
1326
|
+
include: []
|
|
1327
|
+
},
|
|
1328
|
+
build: {
|
|
1329
|
+
rollupOptions: {
|
|
1330
|
+
output: { sanitizeFileName: sanitizeFilePath },
|
|
1331
|
+
input: resolve(flow.options.appDir, "entry")
|
|
1072
1332
|
}
|
|
1073
1333
|
},
|
|
1074
|
-
|
|
1075
|
-
|
|
1334
|
+
plugins: [
|
|
1335
|
+
replace({
|
|
1336
|
+
...Object.fromEntries([";", "(", "{", "}", " ", " ", "\n"].map((d) => [`${d}global.`, `${d}globalThis.`])),
|
|
1337
|
+
preventAssignment: true
|
|
1338
|
+
}),
|
|
1339
|
+
virtual(flow.vfs),
|
|
1340
|
+
DynamicBasePlugin.vite({ sourcemap: flow.options.sourcemap })
|
|
1341
|
+
],
|
|
1342
|
+
server: {
|
|
1343
|
+
watch: {
|
|
1344
|
+
ignored: isIgnoredFlow
|
|
1345
|
+
},
|
|
1346
|
+
hmr: {
|
|
1347
|
+
protocol: "ws",
|
|
1348
|
+
clientPort: hmrPort,
|
|
1349
|
+
port: hmrPort
|
|
1350
|
+
},
|
|
1351
|
+
fs: {
|
|
1352
|
+
allow: [
|
|
1353
|
+
flow.options.appDir
|
|
1354
|
+
]
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
}, flow.options.vite)
|
|
1076
1358
|
};
|
|
1077
|
-
if (
|
|
1078
|
-
ctx.config.
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
await nuxt.callHook("vite:extend", ctx);
|
|
1082
|
-
nuxt.hook("vite:serverCreated", (server, env) => {
|
|
1359
|
+
if (flow.options.dev)
|
|
1360
|
+
ctx.config.mode = process.env.NODE_ENV || "development";
|
|
1361
|
+
await flow.callHook("vite:extend", ctx);
|
|
1362
|
+
flow.hook("vite:serverCreated", (server) => {
|
|
1083
1363
|
ctx.nuxt.hook("app:templatesGenerated", () => {
|
|
1084
1364
|
for (const [id, mod] of server.moduleGraph.idToModuleMap) {
|
|
1085
|
-
if (id.includes("pages."))
|
|
1365
|
+
if (id.includes("pages.mjs"))
|
|
1086
1366
|
server.moduleGraph.invalidateModule(mod);
|
|
1087
|
-
if (id.startsWith("\
|
|
1367
|
+
if (id.startsWith("\0virtual:"))
|
|
1088
1368
|
server.moduleGraph.invalidateModule(mod);
|
|
1089
1369
|
}
|
|
1090
1370
|
});
|
|
1091
1371
|
const start = Date.now();
|
|
1092
|
-
warmupViteServer(server, [
|
|
1372
|
+
warmupViteServer(server, ["/entry.mjs"]).then(() => logger.info(`Vite server warmed up in ${Date.now() - start}ms`)).catch(logger.error);
|
|
1093
1373
|
});
|
|
1094
1374
|
await buildServer(ctx);
|
|
1095
1375
|
}
|
|
@@ -1104,16 +1384,12 @@ async function build(flow) {
|
|
|
1104
1384
|
if (event !== "change" && /app|error|plugins/i.test(path))
|
|
1105
1385
|
await generateApp$1();
|
|
1106
1386
|
});
|
|
1107
|
-
flow.hook("builder:generateApp",
|
|
1108
|
-
if (options)
|
|
1109
|
-
return generateApp(flow, app);
|
|
1110
|
-
return generateApp$1();
|
|
1111
|
-
});
|
|
1387
|
+
flow.hook("builder:generateApp", generateApp$1);
|
|
1112
1388
|
}
|
|
1113
|
-
await flow.callHook("build:before");
|
|
1389
|
+
await flow.callHook("build:before", { flow }, flow.options.build);
|
|
1114
1390
|
if (!flow.options._prepare) {
|
|
1115
1391
|
await bundle(flow);
|
|
1116
|
-
await flow.callHook("build:done");
|
|
1392
|
+
await flow.callHook("build:done", { flow });
|
|
1117
1393
|
}
|
|
1118
1394
|
if (!flow.options.dev)
|
|
1119
1395
|
await flow.callHook("close", flow);
|
|
@@ -1135,7 +1411,7 @@ function watch(flow) {
|
|
|
1135
1411
|
}
|
|
1136
1412
|
async function bundle(nuxt) {
|
|
1137
1413
|
try {
|
|
1138
|
-
return
|
|
1414
|
+
return bundleVite(nuxt);
|
|
1139
1415
|
} catch (error) {
|
|
1140
1416
|
await nuxt.callHook("build:error", error);
|
|
1141
1417
|
throw error;
|