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

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.
@@ -1,149 +0,0 @@
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 };
@@ -1,2 +0,0 @@
1
- declare function _default(): Promise<any>;
2
- export default _default;
@@ -1,6 +0,0 @@
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 });
@@ -1 +0,0 @@
1
- export function getViteNodeOptions(): any;
@@ -1,8 +0,0 @@
1
- export interface ViteNodeRuntimeOptions {
2
- baseURL: string
3
- rootDir: string
4
- entryPath: string
5
- base: string
6
- }
7
-
8
- export function getViteNodeOptions (): ViteNodeRuntimeOptions;
@@ -1,3 +0,0 @@
1
- export function getViteNodeOptions() {
2
- return JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}');
3
- }
@@ -1,2 +0,0 @@
1
- declare function _default(ssrContext: any): Promise<any>;
2
- export default _default;
@@ -1,42 +0,0 @@
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,96 +0,0 @@
1
- import { joinURL } from "ufo";
2
- import consola from "consola";
3
- import { definePage } from "./helpers/index.mjs";
4
- import { defineFlowPlugin, useRuntimeConfig } from "#app";
5
- import pages from "#build/pages";
6
- import contexts from "#build/pages.contexts";
7
- export default defineFlowPlugin(async (flow) => {
8
- const { app } = useRuntimeConfig();
9
- const allPages = [];
10
- const basePages = Object.entries(pages);
11
- consola.success("Parsed %i pages files", basePages.length);
12
- basePages.forEach(([name, _pages]) => {
13
- const __pages = Array.isArray(_pages) ? _pages : [_pages];
14
- __pages.forEach((page) => {
15
- const { getPages } = definePage({
16
- name,
17
- ...page
18
- });
19
- const _pages2 = getPages(app.locale.location, app.locale.language, app.locale.locales);
20
- _pages2.forEach((page2) => {
21
- flow.router.byUrl.insert(page2.url, page2.context);
22
- flow.router.byName.insert(page2.name, page2.context);
23
- allPages.push(page2.context);
24
- });
25
- });
26
- });
27
- const cache = {};
28
- async function getUrl(namePage, localeCode) {
29
- const code = localeCode || this?.getLocale()?.code;
30
- const [lang, loc] = code.split("-");
31
- const name = joinURL("/", loc, lang, namePage);
32
- const { path, params, page } = flow.router.byName.lookup(name) || {};
33
- if (params?._ && page.dynamic) {
34
- if (!cache[path]) {
35
- cache[path] = this.defineCachedFunction(async (_ctx) => {
36
- return await page.dynamic.method(_ctx);
37
- }, { maxAge: 8, getKey: () => path, swr: false });
38
- }
39
- const fn = cache[path];
40
- const list = await fn({ utils: this, locale: this.getLocale() });
41
- const dPage = list.find((el) => el.name === params._);
42
- return dPage ? path.replace("**", dPage.url) : "/404";
43
- }
44
- return path || "/404";
45
- }
46
- async function getUrls(withLocale = false) {
47
- const urls = [];
48
- for (const page of allPages) {
49
- if (page.path.includes("/**") && page.page.dynamic) {
50
- const dPages = await page.page.dynamic.method({
51
- locale: page.locale,
52
- utils: Object.assign({ getLocale: () => page.locale }, flow.app.utils),
53
- chunks: {},
54
- page: {}
55
- });
56
- dPages.forEach((dPage) => {
57
- const _path = joinURL(page.path.replace("/**", ""), dPage.url);
58
- urls.push(withLocale ? { url: _path, locale: page.locale.code, name: joinURL(page.name, dPage.name) } : _path);
59
- });
60
- } else {
61
- const url = page.path.replaceAll("*", "");
62
- urls.push(withLocale ? { url, locale: page.locale.code, name: page.name } : url);
63
- }
64
- }
65
- return urls.sort();
66
- }
67
- async function getSharedContext(ctx) {
68
- const entries = Object.entries(contexts);
69
- if (!entries.length)
70
- return {};
71
- const _contexts = await Promise.all(entries.map(async ([key, method]) => {
72
- const data = await method.setup(ctx);
73
- const setupLocal = method?.locales?.[ctx.locale.code];
74
- let dataLocal = {};
75
- if (setupLocal)
76
- dataLocal = await setupLocal(ctx);
77
- const obj = method.merge ? { ...data, ...dataLocal } : { [key]: { ...data, ...dataLocal } };
78
- return obj;
79
- }));
80
- return _contexts.reduce((acc, curr) => {
81
- return {
82
- ...acc,
83
- ...curr
84
- };
85
- }, {});
86
- }
87
- flow.setUtil("getUrl", getUrl);
88
- flow.setUtil("getUrls", getUrls);
89
- flow.setUtil("getPages", () => allPages);
90
- flow.setUtil("getSharedContext", getSharedContext);
91
- return {
92
- provide: {
93
- pages: { allPages }
94
- }
95
- };
96
- });
File without changes