@monkeyplus/flow 5.0.0-rc.86 → 5.0.0-rc.87

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.
@@ -0,0 +1,149 @@
1
+ import { pathToFileURL } from 'node:url';
2
+ import { createApp, defineEventHandler, defineLazyEventHandler, createError } from 'h3';
3
+ import { ViteNodeServer } from 'vite-node/server';
4
+ import fse from 'fs-extra';
5
+ import { resolve as resolve$1 } from 'pathe';
6
+ import { addServerMiddleware } from '@monkeyplus/flow-kit';
7
+ import { normalizeViteManifest } from 'vue-bundle-renderer';
8
+ import { resolve } from 'mlly';
9
+ import { d as distDir } from './index.mjs';
10
+ import { c as createIsExternal, i as isCSS } from './external.mjs';
11
+ import 'hookable';
12
+ import 'url';
13
+ import 'unimport';
14
+ import 'unplugin';
15
+ import 'ufo';
16
+ import 'fs';
17
+ import 'escape-string-regexp';
18
+ import 'scule';
19
+ import 'knitwork';
20
+ import 'consola';
21
+ import 'vite';
22
+ import 'perfect-debounce';
23
+ import 'node:fs';
24
+ import 'nitropack';
25
+ import 'defu';
26
+ import 'node:module';
27
+ import 'chokidar';
28
+ import 'untyped';
29
+ import '@rollup/plugin-replace';
30
+ import 'pkg-types';
31
+ import 'ohash';
32
+ import 'externality';
33
+
34
+ function viteNodePlugin(ctx) {
35
+ const invalidates = /* @__PURE__ */ new Set();
36
+ return {
37
+ name: "nuxt:vite-node-server",
38
+ enforce: "post",
39
+ configureServer(server) {
40
+ server.middlewares.use("/__nuxt_vite_node__", createViteNodeMiddleware(ctx, invalidates));
41
+ },
42
+ handleHotUpdate({ file, server }) {
43
+ function markInvalidate(mod) {
44
+ if (invalidates.has(mod.id))
45
+ return;
46
+ invalidates.add(mod.id);
47
+ for (const importer of mod.importers)
48
+ markInvalidate(importer);
49
+ }
50
+ const mods = server.moduleGraph.getModulesByFile(file) || [];
51
+ for (const mod of mods)
52
+ markInvalidate(mod);
53
+ }
54
+ };
55
+ }
56
+ function registerViteNodeMiddleware(ctx) {
57
+ addServerMiddleware({
58
+ route: "/__nuxt_vite_node__/",
59
+ handler: createViteNodeMiddleware(ctx)
60
+ });
61
+ }
62
+ function getManifest(ctx) {
63
+ const css = Array.from(ctx.ssrServer.moduleGraph.urlToModuleMap.keys()).filter((i) => isCSS(i));
64
+ const manifest = normalizeViteManifest({
65
+ "@vite/client": {
66
+ file: "@vite/client",
67
+ css,
68
+ module: true,
69
+ isEntry: true
70
+ },
71
+ [ctx.entry]: {
72
+ file: ctx.entry,
73
+ isEntry: true,
74
+ module: true,
75
+ resourceType: "script"
76
+ }
77
+ });
78
+ return manifest;
79
+ }
80
+ function createViteNodeMiddleware(ctx, invalidates = /* @__PURE__ */ new Set()) {
81
+ const app = createApp();
82
+ app.use("/manifest", defineEventHandler(() => {
83
+ const manifest = getManifest(ctx);
84
+ return manifest;
85
+ }));
86
+ app.use("/invalidates", defineEventHandler(() => {
87
+ if (invalidates.size) {
88
+ for (const key of ctx.ssrServer.moduleGraph.fileToModulesMap.keys()) {
89
+ if (key.startsWith(`${ctx.nuxt.options.appDir}/entry`))
90
+ invalidates.add(key);
91
+ }
92
+ }
93
+ const ids = Array.from(invalidates);
94
+ invalidates.clear();
95
+ return ids;
96
+ }));
97
+ app.use("/module", defineLazyEventHandler(() => {
98
+ const viteServer = ctx.ssrServer;
99
+ const node = new ViteNodeServer(viteServer, {
100
+ deps: {
101
+ inline: [
102
+ /\/(nuxt|nuxt3)\//,
103
+ /^#/,
104
+ ...ctx.nuxt.options.build.transpile
105
+ ]
106
+ },
107
+ transformMode: {
108
+ ssr: [/.*/],
109
+ web: []
110
+ }
111
+ });
112
+ const isExternal = createIsExternal(viteServer, ctx.nuxt.options.rootDir);
113
+ node.shouldExternalize = async (id) => {
114
+ const result = await isExternal(id);
115
+ if (result?.external)
116
+ return resolve(result.id, { url: ctx.nuxt.options.rootDir });
117
+ return false;
118
+ };
119
+ return async (event) => {
120
+ const moduleId = decodeURI(event.req.url).substring(1);
121
+ if (moduleId === "/")
122
+ throw createError({ statusCode: 400 });
123
+ const module = await node.fetchModule(moduleId);
124
+ return module;
125
+ };
126
+ }));
127
+ return app.nodeHandler;
128
+ }
129
+ async function initViteNodeServer(ctx) {
130
+ let entryPath = resolve$1(ctx.nuxt.options.appDir, "entry.async.mjs");
131
+ if (!fse.existsSync(entryPath))
132
+ entryPath = resolve$1(ctx.nuxt.options.appDir, "entry.async.ts");
133
+ const host = ctx.nuxt.options.server.host || "localhost";
134
+ const port = ctx.nuxt.options.server.port || "3000";
135
+ const protocol = ctx.nuxt.options.server.https ? "https" : "http";
136
+ const viteNodeServerOptions = {
137
+ baseURL: `${protocol}://${host}:${port}/__nuxt_vite_node__`,
138
+ root: ctx.nuxt.options.srcDir,
139
+ entryPath,
140
+ base: ctx.ssrServer.config.base || "/_nuxt/"
141
+ };
142
+ process.env.NUXT_VITE_NODE_OPTIONS = JSON.stringify(viteNodeServerOptions);
143
+ const serverResolvedPath = resolve$1(distDir, "core/runtime/vite-node.mjs");
144
+ const manifestResolvedPath = resolve$1(distDir, "core/runtime/client.manifest.mjs");
145
+ await fse.writeFile(resolve$1(ctx.nuxt.options.buildDir, "dist/server/server.mjs"), `export { default } from ${JSON.stringify(pathToFileURL(serverResolvedPath).href)}`);
146
+ await fse.writeFile(resolve$1(ctx.nuxt.options.buildDir, "dist/server/client.manifest.mjs"), `export { default } from ${JSON.stringify(pathToFileURL(manifestResolvedPath).href)}`);
147
+ }
148
+
149
+ export { initViteNodeServer, registerViteNodeMiddleware, viteNodePlugin };
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<any>;
2
+ export default _default;
@@ -0,0 +1,6 @@
1
+ import { $fetch } from 'ohmyfetch';
2
+ import { getViteNodeOptions } from './vite-node-shared.mjs';
3
+
4
+ const viteNodeOptions = getViteNodeOptions();
5
+
6
+ export default () => $fetch('/manifest', { baseURL: viteNodeOptions.baseURL });
@@ -0,0 +1 @@
1
+ export function getViteNodeOptions(): any;
@@ -0,0 +1,8 @@
1
+ export interface ViteNodeRuntimeOptions {
2
+ baseURL: string
3
+ rootDir: string
4
+ entryPath: string
5
+ base: string
6
+ }
7
+
8
+ export function getViteNodeOptions (): ViteNodeRuntimeOptions;
@@ -0,0 +1,3 @@
1
+ export function getViteNodeOptions() {
2
+ return JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}');
3
+ }
@@ -0,0 +1,2 @@
1
+ declare function _default(ssrContext: any): Promise<any>;
2
+ export default _default;
@@ -0,0 +1,42 @@
1
+ import { performance } from 'node:perf_hooks';
2
+ import { ViteNodeRunner } from 'vite-node/client';
3
+ import { $fetch } from 'ohmyfetch';
4
+ import consola from 'consola';
5
+ import { getViteNodeOptions } from './vite-node-shared.mjs';
6
+
7
+ const viteNodeOptions = getViteNodeOptions();
8
+ console.log(viteNodeOptions);
9
+ const runner = new ViteNodeRunner({
10
+ root: viteNodeOptions.root, // Equals to Nuxt `srcDir`
11
+ base: viteNodeOptions.base,
12
+ async fetchModule(id) {
13
+ return await $fetch(`/module/${encodeURI(id)}`, {
14
+ baseURL: viteNodeOptions.baseURL,
15
+ });
16
+ },
17
+ });
18
+
19
+ let render;
20
+
21
+ export default async(ssrContext) => {
22
+ // Workaround for stub mode
23
+ // https://github.com/nuxt/framework/pull/3983
24
+ process.server = true;
25
+
26
+ // Invalidate cache for files changed since last rendering
27
+ const invalidates = await $fetch('/invalidates', {
28
+ baseURL: viteNodeOptions.baseURL,
29
+ });
30
+ const updates = runner.moduleCache.invalidateDepTree(invalidates);
31
+
32
+ // Execute SSR bundle on demand
33
+ const start = performance.now();
34
+ render = render || (await runner.executeFile(viteNodeOptions.entryPath)).default;
35
+ if (updates.size) {
36
+ const time = Math.round((performance.now() - start) * 1000) / 1000;
37
+ consola.success(`Vite server hmr ${updates.size} files`, time ? `in ${time}ms` : '');
38
+ }
39
+
40
+ const result = await render(ssrContext);
41
+ return result;
42
+ };
@@ -1,6 +1,5 @@
1
1
  import { defineFlowPlugin } from "#app";
2
2
  export default defineFlowPlugin((flow) => {
3
3
  flow._useHead = (_meta) => {
4
- console.log(_meta);
5
4
  };
6
5
  });