@monkeyplus/flow 5.0.0-rc.169 → 5.0.0-rc.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/entry.mjs +1 -1
- package/dist/app/flow.d.ts +3 -12
- package/dist/app/flow.mjs +0 -3
- package/dist/core/runtime/nitro/flow.d.ts +0 -1
- package/dist/core/runtime/nitro/flow.mjs +14 -9
- package/dist/core/runtime/nitro/renderer.mjs +15 -69
- package/dist/head/runtime/plugin.mjs +1 -0
- package/dist/index.mjs +765 -527
- 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 -10
- package/dist/pages/runtime/index.mjs +2 -10
- package/dist/pages/runtime/plugin.mjs +59 -0
- 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/pages/runtime/pages.mjs +0 -120
- /package/dist/pages/runtime/{pages.d.ts → plugin.d.ts} +0 -0
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { joinURL, withTrailingSlash } from "ufo";
|
|
2
|
-
const buildPages = (page) => (location, language
|
|
3
|
-
const locales =
|
|
4
|
-
const _page = page.locales[key];
|
|
5
|
-
return [key, _page];
|
|
6
|
-
}).filter((el) => !!el[1]);
|
|
2
|
+
const buildPages = (page) => (location, language) => {
|
|
3
|
+
const locales = Object.entries(page.locales);
|
|
7
4
|
return locales.map(([locale, localePage]) => {
|
|
8
5
|
const [_language, _location] = locale.split("-");
|
|
9
6
|
const name = joinURL("/", _location, _language, page.name);
|
|
@@ -19,15 +16,7 @@ const buildPages = (page) => (location, language, _locales) => {
|
|
|
19
16
|
return {
|
|
20
17
|
name,
|
|
21
18
|
url: path,
|
|
22
|
-
context: {
|
|
23
|
-
page: localePage,
|
|
24
|
-
locale: _locale,
|
|
25
|
-
path,
|
|
26
|
-
view: page.view,
|
|
27
|
-
name: page.name,
|
|
28
|
-
noPublish: page.noPublish
|
|
29
|
-
}
|
|
30
|
-
// locale: _locale,
|
|
19
|
+
context: { page: localePage, locale: _locale, path, view: page.view, name: page.name }
|
|
31
20
|
};
|
|
32
21
|
});
|
|
33
22
|
};
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import type { DynamicPage,
|
|
2
|
-
export declare function definePage
|
|
3
|
-
export declare function
|
|
4
|
-
export interface SharedContext {
|
|
5
|
-
assign?: 'global' | 'local';
|
|
6
|
-
merge?: boolean;
|
|
7
|
-
setup: (cxt: PageCtx) => any;
|
|
8
|
-
locales?: Record<string, (ctx: PageCtx) => any>;
|
|
9
|
-
}
|
|
10
|
-
export declare function defineSharedContext(shared: SharedContext): SharedContext;
|
|
1
|
+
import type { DynamicPage, SimplePage } from '@monkeyplus/flow-schema';
|
|
2
|
+
export declare function definePage(...pages: SimplePage[]): SimplePage[];
|
|
3
|
+
export declare function defineDinamycPage(...pages: DynamicPage[]): DynamicPage[];
|
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
export function definePage(...pages) {
|
|
2
2
|
return pages;
|
|
3
3
|
}
|
|
4
|
-
export function
|
|
5
|
-
return pages
|
|
6
|
-
page.originalName = page.name;
|
|
7
|
-
if (!page.name.endsWith("/**"))
|
|
8
|
-
page.name = `${page.name}/**`;
|
|
9
|
-
return page;
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
export function defineSharedContext(shared) {
|
|
13
|
-
return shared;
|
|
4
|
+
export function defineDinamycPage(...pages) {
|
|
5
|
+
return pages;
|
|
14
6
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
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 "#pages";
|
|
6
|
+
export default defineFlowPlugin(async (flow) => {
|
|
7
|
+
const { app } = useRuntimeConfig();
|
|
8
|
+
const allPages = [];
|
|
9
|
+
const basePages = Object.entries(pages);
|
|
10
|
+
consola.success("Parsed %i pages files", basePages.length);
|
|
11
|
+
basePages.forEach(([name, _pages]) => {
|
|
12
|
+
const __pages = Array.isArray(_pages) ? _pages : [_pages];
|
|
13
|
+
__pages.forEach((page) => {
|
|
14
|
+
const { getPages } = definePage({
|
|
15
|
+
name,
|
|
16
|
+
...page
|
|
17
|
+
});
|
|
18
|
+
const _pages2 = getPages(app.locale.location, app.locale.language);
|
|
19
|
+
_pages2.forEach((page2) => {
|
|
20
|
+
flow.router.byUrl.insert(page2.url, page2.context);
|
|
21
|
+
flow.router.byName.insert(page2.name, page2.context);
|
|
22
|
+
allPages.push(page2.context);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
function getUrl(namePage, localeCode) {
|
|
27
|
+
const code = localeCode || this?.getLocale()?.code;
|
|
28
|
+
const [lang, loc] = code.split("-");
|
|
29
|
+
const name = joinURL("/", loc, lang, namePage);
|
|
30
|
+
const { path } = flow.router.byName.lookup(name) || {};
|
|
31
|
+
return path || "/404";
|
|
32
|
+
}
|
|
33
|
+
async function getUrls(withLocale = false) {
|
|
34
|
+
const urls = [];
|
|
35
|
+
for (const page of allPages) {
|
|
36
|
+
if (!page.path.includes("/**")) {
|
|
37
|
+
urls.push(withLocale ? { url: page.path, locale: page.locale.code, name: page.name } : page.path);
|
|
38
|
+
} else {
|
|
39
|
+
const dPages = await page.page.dynamic.method({
|
|
40
|
+
locale: page.locale,
|
|
41
|
+
utils: Object.assign({ getLocale: () => page.locale }, flow.app.utils)
|
|
42
|
+
});
|
|
43
|
+
dPages.forEach((dPage) => {
|
|
44
|
+
const _path = joinURL(page.path.replace("/**", ""), dPage.url);
|
|
45
|
+
urls.push(withLocale ? { url: _path, locale: page.locale.code, name: joinURL(page.name, dPage.name) } : _path);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return urls.sort();
|
|
50
|
+
}
|
|
51
|
+
flow.setUtil("getUrl", getUrl);
|
|
52
|
+
flow.setUtil("getUrls", getUrls);
|
|
53
|
+
flow.setUtil("getPages", () => allPages);
|
|
54
|
+
return {
|
|
55
|
+
provide: {
|
|
56
|
+
pages: { allPages }
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare type Manifest = Record<string, ManifestChunk>;
|
|
2
|
+
export interface ManifestChunk {
|
|
3
|
+
src?: string;
|
|
4
|
+
file: string;
|
|
5
|
+
css?: string[];
|
|
6
|
+
assets?: string[];
|
|
7
|
+
isEntry?: boolean;
|
|
8
|
+
isDynamicEntry?: boolean;
|
|
9
|
+
imports?: string[];
|
|
10
|
+
dynamicImports?: string[];
|
|
11
|
+
}
|
|
12
|
+
export interface HtmlTagDescriptor {
|
|
13
|
+
tag: string;
|
|
14
|
+
attrs?: Record<string, string | boolean | undefined>;
|
|
15
|
+
children?: string | HtmlTagDescriptor[];
|
|
16
|
+
/**
|
|
17
|
+
* default: 'head-prepend'
|
|
18
|
+
*/
|
|
19
|
+
injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend';
|
|
20
|
+
}
|
|
21
|
+
export declare const generateBundle: (config: any, bundle: Manifest, _id: string) => {
|
|
22
|
+
head: string;
|
|
23
|
+
body: string;
|
|
24
|
+
};
|
|
25
|
+
export declare const externalRE: RegExp;
|
|
26
|
+
export declare const isExternalUrl: (url: string) => boolean;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { joinURL } from "ufo";
|
|
2
|
+
export const generateBundle = (config, bundle, _id) => {
|
|
3
|
+
const analyzedChunk = new Map();
|
|
4
|
+
const getImportedChunks = (chunk2, seen = new Set()) => {
|
|
5
|
+
const chunks = [];
|
|
6
|
+
chunk2.imports?.forEach((file) => {
|
|
7
|
+
const importee = bundle[file];
|
|
8
|
+
if (file.startsWith("_") && !seen.has(file)) {
|
|
9
|
+
seen.add(file);
|
|
10
|
+
chunks.push(...getImportedChunks(importee, seen));
|
|
11
|
+
chunks.push(importee);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return chunks;
|
|
15
|
+
};
|
|
16
|
+
const toScriptTag = (chunk2, publicBase2, isAsync2) => ({
|
|
17
|
+
tag: "script",
|
|
18
|
+
attrs: {
|
|
19
|
+
...isAsync2 ? { async: true } : {},
|
|
20
|
+
type: "module",
|
|
21
|
+
crossorigin: true,
|
|
22
|
+
src: toPublicPath(chunk2.file, publicBase2)
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const toPreloadTag = (chunk2, publicBase2) => ({
|
|
26
|
+
tag: "link",
|
|
27
|
+
attrs: {
|
|
28
|
+
rel: "modulepreload",
|
|
29
|
+
crossorigin: true,
|
|
30
|
+
href: toPublicPath(chunk2.file, publicBase2)
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const getCssTagsForChunk = (chunk2, publicBase2, seen = new Set()) => {
|
|
34
|
+
const tags = [];
|
|
35
|
+
if (!analyzedChunk.has(chunk2)) {
|
|
36
|
+
analyzedChunk.set(chunk2, 1);
|
|
37
|
+
chunk2.imports?.forEach((file) => {
|
|
38
|
+
const importee = bundle[file];
|
|
39
|
+
if (file.startsWith("_"))
|
|
40
|
+
tags.push(...getCssTagsForChunk(importee, publicBase2, seen));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
chunk2.css?.forEach((file) => {
|
|
44
|
+
if (!seen.has(file)) {
|
|
45
|
+
seen.add(file);
|
|
46
|
+
tags.push({
|
|
47
|
+
tag: "link",
|
|
48
|
+
attrs: {
|
|
49
|
+
rel: "stylesheet",
|
|
50
|
+
href: toPublicPath(file, publicBase2)
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return tags;
|
|
56
|
+
};
|
|
57
|
+
const publicBase = config.baseURL || "/";
|
|
58
|
+
const isAsync = false;
|
|
59
|
+
const chunk = Object.values(bundle).find((chunk2) => chunk2.isEntry && chunk2.src === _id);
|
|
60
|
+
if (chunk) {
|
|
61
|
+
const imports = getImportedChunks(chunk);
|
|
62
|
+
const assetTags = [
|
|
63
|
+
toScriptTag(chunk, publicBase, isAsync),
|
|
64
|
+
...imports.map((i) => toPreloadTag(i, publicBase))
|
|
65
|
+
];
|
|
66
|
+
assetTags.push(...getCssTagsForChunk(chunk, publicBase));
|
|
67
|
+
const tags = serializeTags(assetTags);
|
|
68
|
+
return { head: tags, body: "" };
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export const externalRE = /^(https?:)?\/\//;
|
|
72
|
+
export const isExternalUrl = (url) => externalRE.test(url);
|
|
73
|
+
function toPublicPath(filename, publicBase) {
|
|
74
|
+
return isExternalUrl(filename) ? filename : joinURL(publicBase, filename.replace("scripts", "assets"));
|
|
75
|
+
}
|
|
76
|
+
const unaryTags = new Set(["link", "meta", "base"]);
|
|
77
|
+
function serializeTag({ tag, attrs, children }, indent = "") {
|
|
78
|
+
if (unaryTags.has(tag)) {
|
|
79
|
+
return `<${tag}${serializeAttrs(attrs)}>`;
|
|
80
|
+
} else {
|
|
81
|
+
return `<${tag}${serializeAttrs(attrs)}>${serializeTags(children, incrementIndent(indent))}</${tag}>`;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function serializeTags(tags, indent = "") {
|
|
85
|
+
if (typeof tags === "string")
|
|
86
|
+
return tags;
|
|
87
|
+
else if (tags && tags.length)
|
|
88
|
+
return tags.map((tag) => `${indent}${serializeTag(tag, indent)}
|
|
89
|
+
`).join("");
|
|
90
|
+
return "";
|
|
91
|
+
}
|
|
92
|
+
function serializeAttrs(attrs) {
|
|
93
|
+
let res = "";
|
|
94
|
+
for (const key in attrs) {
|
|
95
|
+
if (typeof attrs[key] === "boolean")
|
|
96
|
+
res += attrs[key] ? ` ${key}` : "";
|
|
97
|
+
else
|
|
98
|
+
res += ` ${key}=${JSON.stringify(attrs[key])}`;
|
|
99
|
+
}
|
|
100
|
+
return res;
|
|
101
|
+
}
|
|
102
|
+
function incrementIndent(indent = "") {
|
|
103
|
+
return `${indent}${indent[0] === " " ? " " : " "}`;
|
|
104
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import logger from "consola";
|
|
2
|
+
import { generateBundle } from "./injectManifest.mjs";
|
|
3
|
+
import { defineFlowPlugin } from "#app";
|
|
4
|
+
import manifest from "#viteManifest";
|
|
5
|
+
export default defineFlowPlugin((flow) => {
|
|
6
|
+
if (typeof manifest === "function") {
|
|
7
|
+
flow.hook("page:chunks", (bundle, chunks) => {
|
|
8
|
+
try {
|
|
9
|
+
const chunk = generateBundle(flow.$config.app || {}, manifest(), `client/pages/${bundle}.ts`);
|
|
10
|
+
if (chunk) {
|
|
11
|
+
chunks.head.push(chunk.head);
|
|
12
|
+
chunks.body.push(chunk.body);
|
|
13
|
+
} else {
|
|
14
|
+
logger.warn('Entry "%s" not found ', bundle);
|
|
15
|
+
}
|
|
16
|
+
} catch (error) {
|
|
17
|
+
logger.error("Error in inject %s ", bundle);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
} else {
|
|
21
|
+
flow.hook("page:chunks", (bundle, chunks) => {
|
|
22
|
+
chunks.head.push(manifest.head());
|
|
23
|
+
chunks.body.push(manifest.body(bundle));
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@monkeyplus/flow",
|
|
3
|
-
"version": "5.0.0-rc.
|
|
3
|
+
"version": "5.0.0-rc.17",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -25,56 +25,52 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@monkeyplus/flow-cli": "5.0.0-rc.
|
|
29
|
-
"@monkeyplus/flow-kit": "5.0.0-rc.
|
|
30
|
-
"@monkeyplus/flow-schema": "5.0.0-rc.
|
|
28
|
+
"@monkeyplus/flow-cli": "5.0.0-rc.17",
|
|
29
|
+
"@monkeyplus/flow-kit": "5.0.0-rc.17",
|
|
30
|
+
"@monkeyplus/flow-schema": "5.0.0-rc.17",
|
|
31
31
|
"@rollup/plugin-replace": "^4.0.0",
|
|
32
|
-
"@vueuse/head": "^
|
|
33
|
-
"c12": "^
|
|
32
|
+
"@vueuse/head": "^0.7.6",
|
|
33
|
+
"c12": "^0.2.7",
|
|
34
34
|
"chokidar": "^3.5.3",
|
|
35
35
|
"consola": "^2.15.3",
|
|
36
|
-
"defu": "^6.
|
|
37
|
-
"esbuild": "^0.
|
|
36
|
+
"defu": "^6.0.0",
|
|
37
|
+
"esbuild": "^0.14.38",
|
|
38
38
|
"escape-string-regexp": "^5.0.0",
|
|
39
|
-
"esno": "^0.
|
|
39
|
+
"esno": "^0.14.1",
|
|
40
40
|
"eta": "^1.12.3",
|
|
41
|
-
"externality": "^
|
|
41
|
+
"externality": "^0.2.1",
|
|
42
42
|
"fs-extra": "^10.1.0",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"knitwork": "^1.
|
|
49
|
-
"listhen": "^
|
|
50
|
-
"magic-string": "^0.26.
|
|
51
|
-
"mlly": "^
|
|
43
|
+
"get-port-please": "^2.5.0",
|
|
44
|
+
"globby": "^13.1.1",
|
|
45
|
+
"h3": "^0.7.4",
|
|
46
|
+
"hookable": "^5.1.1",
|
|
47
|
+
"jiti": "^1.13.0",
|
|
48
|
+
"knitwork": "^0.1.1",
|
|
49
|
+
"listhen": "^0.2.10",
|
|
50
|
+
"magic-string": "^0.26.2",
|
|
51
|
+
"mlly": "^0.5.2",
|
|
52
52
|
"mri": "^1.2.0",
|
|
53
|
-
"nitropack": "^
|
|
54
|
-
"pathe": "^
|
|
53
|
+
"nitropack": "^0.4.4",
|
|
54
|
+
"pathe": "^0.2.0",
|
|
55
55
|
"perfect-debounce": "^0.1.3",
|
|
56
|
-
"radix3": "^1.
|
|
57
|
-
"unenv": "^
|
|
58
|
-
"ohmyfetch": "^0.4.
|
|
59
|
-
"node-fetch-native": "^
|
|
60
|
-
"rollup": "^
|
|
61
|
-
"rollup-plugin-visualizer": "^5.
|
|
62
|
-
"scule": "^
|
|
63
|
-
"ufo": "^
|
|
64
|
-
"unctx": "^
|
|
65
|
-
"unimport": "^
|
|
66
|
-
"unplugin": "^
|
|
67
|
-
"untyped": "^
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"vite-node": "^0.24.5",
|
|
71
|
-
"vite-plugin-checker": "^0.5.1",
|
|
72
|
-
"vue-bundle-renderer": "^1.0.0",
|
|
73
|
-
"vue": "^3.2.41"
|
|
56
|
+
"radix3": "^0.1.2",
|
|
57
|
+
"unenv": "^0.5.2",
|
|
58
|
+
"ohmyfetch": "^0.4.18",
|
|
59
|
+
"node-fetch-native": "^0.1.3",
|
|
60
|
+
"rollup": "^2.72.1",
|
|
61
|
+
"rollup-plugin-visualizer": "^5.6.0",
|
|
62
|
+
"scule": "^0.2.1",
|
|
63
|
+
"ufo": "^0.8.3",
|
|
64
|
+
"unctx": "^1.1.4",
|
|
65
|
+
"unimport": "^0.2.1",
|
|
66
|
+
"unplugin": "^0.6.3",
|
|
67
|
+
"untyped": "^0.4.4",
|
|
68
|
+
"vite": "^2.9.9",
|
|
69
|
+
"vue": "^3.2.33"
|
|
74
70
|
},
|
|
75
71
|
"devDependencies": {
|
|
76
72
|
"@types/fs-extra": "^9.0.13",
|
|
77
|
-
"vue-router": "^4.
|
|
73
|
+
"vue-router": "^4.0.15"
|
|
78
74
|
},
|
|
79
75
|
"engines": {
|
|
80
76
|
"node": "^16.11.0 || ^17.0.0 || ^18.0.0"
|
package/dist/app/entry.async.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default (ctx) => import("#app/entry").then((m) => m.default(ctx));
|
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
import { pathToFileURL } from 'node:url';
|
|
2
|
-
import { existsSync } from 'node:fs';
|
|
3
|
-
import { builtinModules } from 'node:module';
|
|
4
|
-
import { resolve, normalize, isAbsolute } from 'pathe';
|
|
5
|
-
import { genObjectFromRawEntries, genDynamicImport } from 'knitwork';
|
|
6
|
-
import fse from 'fs-extra';
|
|
7
|
-
import { debounce } from 'perfect-debounce';
|
|
8
|
-
import { isIgnoredFlow, logger } from '@monkeyplus/flow-kit';
|
|
9
|
-
import { hash } from 'ohash';
|
|
10
|
-
import { ExternalsDefaults, isExternal } from 'externality';
|
|
11
|
-
import { withTrailingSlash, withoutLeadingSlash } from 'ufo';
|
|
12
|
-
import escapeRE from 'escape-string-regexp';
|
|
13
|
-
import { normalizeViteManifest } from 'vue-bundle-renderer';
|
|
14
|
-
|
|
15
|
-
function uniq(arr) {
|
|
16
|
-
return Array.from(new Set(arr));
|
|
17
|
-
}
|
|
18
|
-
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|less|stylus|styl)(\?[^.]+)?$/;
|
|
19
|
-
function isCSS(file) {
|
|
20
|
-
return IS_CSS_RE.test(file);
|
|
21
|
-
}
|
|
22
|
-
function hashId(id) {
|
|
23
|
-
return `$id_${hash(id)}`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function createIsExternal(viteServer, rootDir) {
|
|
27
|
-
const externalOpts = {
|
|
28
|
-
inline: [
|
|
29
|
-
/virtual:/,
|
|
30
|
-
/\.ts$/,
|
|
31
|
-
...ExternalsDefaults.inline || [],
|
|
32
|
-
...viteServer.config.ssr.noExternal
|
|
33
|
-
],
|
|
34
|
-
external: [
|
|
35
|
-
...viteServer.config.ssr.external || [],
|
|
36
|
-
/node_modules/
|
|
37
|
-
],
|
|
38
|
-
resolve: {
|
|
39
|
-
type: "module",
|
|
40
|
-
extensions: [".ts", ".js", ".json", ".vue", ".mjs", ".jsx", ".tsx", ".wasm"]
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
return (id) => isExternal(id, rootDir, externalOpts);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function writeManifest(ctx, css = []) {
|
|
47
|
-
const clientDist = resolve(ctx.nuxt.options.buildDir, "dist/client");
|
|
48
|
-
const serverDist = resolve(ctx.nuxt.options.buildDir, "dist/server");
|
|
49
|
-
const devClientManifest = {
|
|
50
|
-
"@vite/client": {
|
|
51
|
-
isEntry: true,
|
|
52
|
-
file: "@vite/client",
|
|
53
|
-
css,
|
|
54
|
-
module: true,
|
|
55
|
-
resourceType: "script"
|
|
56
|
-
},
|
|
57
|
-
[ctx.entry]: {
|
|
58
|
-
isEntry: true,
|
|
59
|
-
file: ctx.entry,
|
|
60
|
-
module: true,
|
|
61
|
-
resourceType: "script"
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
const clientManifest = ctx.nuxt.options.dev ? devClientManifest : await fse.readJSON(resolve(clientDist, "manifest.json"));
|
|
65
|
-
const buildAssetsDir = withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir));
|
|
66
|
-
const BASE_RE = new RegExp(`^${escapeRE(buildAssetsDir)}`);
|
|
67
|
-
for (const key in clientManifest) {
|
|
68
|
-
if (clientManifest[key].file)
|
|
69
|
-
clientManifest[key].file = clientManifest[key].file.replace(BASE_RE, "");
|
|
70
|
-
for (const item of ["css", "assets"]) {
|
|
71
|
-
if (clientManifest[key][item])
|
|
72
|
-
clientManifest[key][item] = clientManifest[key][item].map((i) => i.replace(BASE_RE, ""));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
await fse.mkdirp(serverDist);
|
|
76
|
-
const manifest = normalizeViteManifest(clientManifest);
|
|
77
|
-
await ctx.nuxt.callHook("build:manifest", manifest);
|
|
78
|
-
await fse.writeFile(resolve(serverDist, "client.manifest.json"), JSON.stringify(manifest, null, 2), "utf8");
|
|
79
|
-
await fse.writeFile(resolve(serverDist, "client.manifest.mjs"), `export default ${JSON.stringify(manifest, null, 2)}`, "utf8");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async function transformRequest(opts, id) {
|
|
83
|
-
if (id && id.startsWith("/@id/__x00__"))
|
|
84
|
-
id = `\0${id.slice("/@id/__x00__".length)}`;
|
|
85
|
-
if (id && id.startsWith("/@id/"))
|
|
86
|
-
id = id.slice("/@id/".length);
|
|
87
|
-
if (id && !id.startsWith("/@fs/") && id.startsWith("/")) {
|
|
88
|
-
const resolvedPath = resolve(opts.viteServer.config.root, `.${id}`);
|
|
89
|
-
if (existsSync(resolvedPath))
|
|
90
|
-
id = resolvedPath;
|
|
91
|
-
}
|
|
92
|
-
id = id.replace(/^\/?(?=\w:)/, "/@fs/");
|
|
93
|
-
const externalId = id.replace(/\?v=\w+$|^\/@fs/, "");
|
|
94
|
-
if (await opts.isExternal(externalId)) {
|
|
95
|
-
const path = builtinModules.includes(externalId.split("node:").pop()) ? externalId : isAbsolute(externalId) ? pathToFileURL(externalId).href : externalId;
|
|
96
|
-
return {
|
|
97
|
-
code: `(global, module, _, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) =>
|
|
98
|
-
${genDynamicImport(path, { wrapper: false })}
|
|
99
|
-
.then(r => {
|
|
100
|
-
if (r.default && r.default.__esModule)
|
|
101
|
-
r = r.default
|
|
102
|
-
exports.default = r.default
|
|
103
|
-
ssrExportAll(r)
|
|
104
|
-
})
|
|
105
|
-
.catch(e => {
|
|
106
|
-
console.error(e)
|
|
107
|
-
throw new Error(${JSON.stringify(`[vite dev] Error loading external "${id}".`)})
|
|
108
|
-
})`,
|
|
109
|
-
deps: [],
|
|
110
|
-
dynamicDeps: []
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
const res = await opts.viteServer.transformRequest(id, { ssr: true }).catch((err) => {
|
|
114
|
-
console.warn(`[SSR] Error transforming ${id}:`, err);
|
|
115
|
-
}) || { code: "", map: {}, deps: [], dynamicDeps: [] };
|
|
116
|
-
const code = `async function (global, module, exports, __vite_ssr_exports__, __vite_ssr_import_meta__, __vite_ssr_import__, __vite_ssr_dynamic_import__, __vite_ssr_exportAll__) {
|
|
117
|
-
${res.code || "/* empty */"};
|
|
118
|
-
}`;
|
|
119
|
-
return { code, deps: res.deps || [], dynamicDeps: res.dynamicDeps || [] };
|
|
120
|
-
}
|
|
121
|
-
async function transformRequestRecursive(opts, id, parent = "<entry>", chunks = {}) {
|
|
122
|
-
if (chunks[id]) {
|
|
123
|
-
chunks[id].parents.push(parent);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const res = await transformRequest(opts, id);
|
|
127
|
-
const deps = uniq([...res.deps, ...res.dynamicDeps]);
|
|
128
|
-
chunks[id] = {
|
|
129
|
-
id,
|
|
130
|
-
code: res.code,
|
|
131
|
-
deps,
|
|
132
|
-
parents: [parent]
|
|
133
|
-
};
|
|
134
|
-
for (const dep of deps)
|
|
135
|
-
await transformRequestRecursive(opts, dep, id, chunks);
|
|
136
|
-
return Object.values(chunks);
|
|
137
|
-
}
|
|
138
|
-
async function bundleRequest(opts, entryURL) {
|
|
139
|
-
const chunks = await transformRequestRecursive(opts, entryURL);
|
|
140
|
-
const listIds = (ids) => ids.map((id) => `// - ${id} (${hashId(id)})`).join("\n");
|
|
141
|
-
const chunksCode = chunks.map((chunk) => `
|
|
142
|
-
// --------------------
|
|
143
|
-
// Request: ${chunk.id}
|
|
144
|
-
// Parents:
|
|
145
|
-
${listIds(chunk.parents)}
|
|
146
|
-
// Dependencies:
|
|
147
|
-
${listIds(chunk.deps)}
|
|
148
|
-
// --------------------
|
|
149
|
-
const ${hashId(`${chunk.id}-${chunk.code}`)} = ${chunk.code}
|
|
150
|
-
`).join("\n");
|
|
151
|
-
const manifestCode = `const __modules__ = ${genObjectFromRawEntries(chunks.map((chunk) => [chunk.id, hashId(`${chunk.id}-${chunk.code}`)]))}`;
|
|
152
|
-
const ssrModuleLoader = `
|
|
153
|
-
const __pendingModules__ = new Map()
|
|
154
|
-
const __pendingImports__ = new Map()
|
|
155
|
-
const __ssrContext__ = { global: globalThis }
|
|
156
|
-
|
|
157
|
-
function __ssrLoadModule__(url, urlStack = []) {
|
|
158
|
-
const pendingModule = __pendingModules__.get(url)
|
|
159
|
-
if (pendingModule) { return pendingModule }
|
|
160
|
-
const modulePromise = __instantiateModule__(url, urlStack)
|
|
161
|
-
__pendingModules__.set(url, modulePromise)
|
|
162
|
-
modulePromise.catch(() => { __pendingModules__.delete(url) })
|
|
163
|
-
.finally(() => { __pendingModules__.delete(url) })
|
|
164
|
-
return modulePromise
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
async function __instantiateModule__(url, urlStack) {
|
|
168
|
-
const mod = __modules__[url]
|
|
169
|
-
if (mod.stubModule) { return mod.stubModule }
|
|
170
|
-
const stubModule = { [Symbol.toStringTag]: 'Module' }
|
|
171
|
-
Object.defineProperty(stubModule, '__esModule', { value: true })
|
|
172
|
-
mod.stubModule = stubModule
|
|
173
|
-
// https://vitejs.dev/guide/api-hmr.html
|
|
174
|
-
const importMeta = { url, hot: { accept() {}, prune() {}, dispose() {}, invalidate() {}, decline() {}, on() {} } }
|
|
175
|
-
urlStack = urlStack.concat(url)
|
|
176
|
-
const isCircular = url => urlStack.includes(url)
|
|
177
|
-
const pendingDeps = []
|
|
178
|
-
const ssrImport = async (dep) => {
|
|
179
|
-
// TODO: Handle externals if dep[0] !== '.' | '/'
|
|
180
|
-
if (!isCircular(dep) && !__pendingImports__.get(dep)?.some(isCircular)) {
|
|
181
|
-
pendingDeps.push(dep)
|
|
182
|
-
if (pendingDeps.length === 1) {
|
|
183
|
-
__pendingImports__.set(url, pendingDeps)
|
|
184
|
-
}
|
|
185
|
-
await __ssrLoadModule__(dep, urlStack)
|
|
186
|
-
if (pendingDeps.length === 1) {
|
|
187
|
-
__pendingImports__.delete(url)
|
|
188
|
-
} else {
|
|
189
|
-
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return __modules__[dep].stubModule
|
|
193
|
-
}
|
|
194
|
-
function ssrDynamicImport (dep) {
|
|
195
|
-
// TODO: Handle dynamic import starting with . relative to url
|
|
196
|
-
return ssrImport(dep)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function ssrExportAll(sourceModule) {
|
|
200
|
-
for (const key in sourceModule) {
|
|
201
|
-
if (key !== 'default') {
|
|
202
|
-
try {
|
|
203
|
-
Object.defineProperty(stubModule, key, {
|
|
204
|
-
enumerable: true,
|
|
205
|
-
configurable: true,
|
|
206
|
-
get() { return sourceModule[key] }
|
|
207
|
-
})
|
|
208
|
-
} catch (_err) { }
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const cjsModule = {
|
|
214
|
-
get exports () {
|
|
215
|
-
return stubModule.default
|
|
216
|
-
},
|
|
217
|
-
set exports (v) {
|
|
218
|
-
stubModule.default = v
|
|
219
|
-
},
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
await mod(
|
|
223
|
-
__ssrContext__.global,
|
|
224
|
-
cjsModule,
|
|
225
|
-
stubModule.default,
|
|
226
|
-
stubModule,
|
|
227
|
-
importMeta,
|
|
228
|
-
ssrImport,
|
|
229
|
-
ssrDynamicImport,
|
|
230
|
-
ssrExportAll
|
|
231
|
-
)
|
|
232
|
-
|
|
233
|
-
return stubModule
|
|
234
|
-
}
|
|
235
|
-
`;
|
|
236
|
-
const code = [
|
|
237
|
-
chunksCode,
|
|
238
|
-
manifestCode,
|
|
239
|
-
ssrModuleLoader,
|
|
240
|
-
`export default await __ssrLoadModule__(${JSON.stringify(entryURL)})`
|
|
241
|
-
].join("\n\n");
|
|
242
|
-
return {
|
|
243
|
-
code,
|
|
244
|
-
ids: chunks.map((i) => i.id)
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
async function initViteDevBundler(ctx, onBuild) {
|
|
248
|
-
const viteServer = ctx.ssrServer;
|
|
249
|
-
const options = {
|
|
250
|
-
viteServer,
|
|
251
|
-
isExternal: createIsExternal(viteServer, ctx.nuxt.options.rootDir)
|
|
252
|
-
};
|
|
253
|
-
const _doBuild = async () => {
|
|
254
|
-
const start = Date.now();
|
|
255
|
-
const { code, ids } = await bundleRequest(options, ctx.entry);
|
|
256
|
-
await fse.ensureFile(resolve(ctx.nuxt.options.buildDir, "dist/server/server.mjs"));
|
|
257
|
-
await fse.writeFile(resolve(ctx.nuxt.options.buildDir, "dist/server/server.mjs"), code, "utf-8");
|
|
258
|
-
await writeManifest(ctx, ids.filter(isCSS).map((i) => i.slice(1)));
|
|
259
|
-
const time = Date.now() - start;
|
|
260
|
-
logger.success(`Vite server built in ${time}ms`);
|
|
261
|
-
ctx.nuxt.callHook("bundler:change", {});
|
|
262
|
-
await onBuild();
|
|
263
|
-
};
|
|
264
|
-
const doBuild = debounce(_doBuild);
|
|
265
|
-
await _doBuild();
|
|
266
|
-
viteServer.watcher.on("all", (_event, file) => {
|
|
267
|
-
if (file.includes("/pages/"))
|
|
268
|
-
return;
|
|
269
|
-
file = normalize(file);
|
|
270
|
-
if (file.indexOf(ctx.nuxt.options.buildDir) === 0 || isIgnoredFlow(file))
|
|
271
|
-
return;
|
|
272
|
-
doBuild();
|
|
273
|
-
});
|
|
274
|
-
ctx.nuxt.hook("app:templatesGenerated", () => doBuild());
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export { bundleRequest, initViteDevBundler };
|