@bleedingdev/modern-js-plugin-ssg 3.2.0-ultramodern.0
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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/cjs/index.js +136 -0
- package/dist/cjs/libs/make.js +79 -0
- package/dist/cjs/libs/output.js +55 -0
- package/dist/cjs/libs/replace.js +75 -0
- package/dist/cjs/libs/util.js +224 -0
- package/dist/cjs/server/consts.js +36 -0
- package/dist/cjs/server/index.js +141 -0
- package/dist/cjs/server/prerender.js +75 -0
- package/dist/cjs/types.js +18 -0
- package/dist/esm/index.mjs +90 -0
- package/dist/esm/libs/make.mjs +31 -0
- package/dist/esm/libs/output.mjs +11 -0
- package/dist/esm/libs/replace.mjs +28 -0
- package/dist/esm/libs/util.mjs +147 -0
- package/dist/esm/server/consts.mjs +2 -0
- package/dist/esm/server/index.mjs +97 -0
- package/dist/esm/server/prerender.mjs +30 -0
- package/dist/esm/types.mjs +0 -0
- package/dist/esm-node/index.mjs +91 -0
- package/dist/esm-node/libs/make.mjs +32 -0
- package/dist/esm-node/libs/output.mjs +12 -0
- package/dist/esm-node/libs/replace.mjs +29 -0
- package/dist/esm-node/libs/util.mjs +149 -0
- package/dist/esm-node/server/consts.mjs +3 -0
- package/dist/esm-node/server/index.mjs +98 -0
- package/dist/esm-node/server/prerender.mjs +31 -0
- package/dist/esm-node/types.mjs +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/libs/make.d.ts +5 -0
- package/dist/types/libs/output.d.ts +2 -0
- package/dist/types/libs/replace.d.ts +4 -0
- package/dist/types/libs/util.d.ts +27 -0
- package/dist/types/server/consts.d.ts +1 -0
- package/dist/types/server/index.d.ts +4 -0
- package/dist/types/server/prerender.d.ts +10 -0
- package/dist/types/types.d.ts +23 -0
- package/package.json +84 -0
- package/rslib.config.mts +4 -0
- package/rstest.config.mts +7 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { filterRoutesForServer, logger } from "@modern-js/utils";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { makeRoute } from "./libs/make.mjs";
|
|
5
|
+
import { writeHtmlFile } from "./libs/output.mjs";
|
|
6
|
+
import { replaceRoute } from "./libs/replace.mjs";
|
|
7
|
+
import { flattenRoutes, formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util.mjs";
|
|
8
|
+
import { createServer } from "./server/index.mjs";
|
|
9
|
+
const ssgPlugin = ()=>({
|
|
10
|
+
name: '@modern-js/plugin-ssg',
|
|
11
|
+
pre: [
|
|
12
|
+
'@modern-js/plugin-bff'
|
|
13
|
+
],
|
|
14
|
+
setup: (api)=>{
|
|
15
|
+
const agreedRouteMap = {};
|
|
16
|
+
api.modifyFileSystemRoutes(async ({ entrypoint, routes })=>{
|
|
17
|
+
const { entryName } = entrypoint;
|
|
18
|
+
const flattedRoutes = flattenRoutes(filterRoutesForServer(routes));
|
|
19
|
+
agreedRouteMap[entryName] = flattedRoutes;
|
|
20
|
+
return {
|
|
21
|
+
entrypoint,
|
|
22
|
+
routes
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
api.onAfterBuild(async ()=>{
|
|
26
|
+
const resolvedConfig = api.getNormalizedConfig();
|
|
27
|
+
const appContext = api.getAppContext();
|
|
28
|
+
const { appDirectory, entrypoints } = appContext;
|
|
29
|
+
const { output, server } = resolvedConfig;
|
|
30
|
+
const { ssg, ssgByEntries, distPath: { root: outputPath } = {} } = output;
|
|
31
|
+
const ssgOptions = (Array.isArray(ssg) ? ssg.pop() : ssg) ?? true;
|
|
32
|
+
const buildDir = path.join(appDirectory, outputPath);
|
|
33
|
+
const routes = readJSONSpec(buildDir);
|
|
34
|
+
const pageRoutes = routes.filter((route)=>route.isSPA);
|
|
35
|
+
const apiRoutes = routes.filter((route)=>!route.isSPA);
|
|
36
|
+
if (0 === pageRoutes.length) return;
|
|
37
|
+
const intermediateOptions = standardOptions(ssgOptions, entrypoints, pageRoutes, server, ssgByEntries);
|
|
38
|
+
if (!intermediateOptions) return;
|
|
39
|
+
const ssgRoutes = [];
|
|
40
|
+
pageRoutes.forEach((pageRoute)=>{
|
|
41
|
+
const { entryName, entryPath } = pageRoute;
|
|
42
|
+
const agreedRoutes = agreedRouteMap[entryName];
|
|
43
|
+
let entryOptions = intermediateOptions[entryName] || intermediateOptions[pageRoute.urlPath];
|
|
44
|
+
if (agreedRoutes) {
|
|
45
|
+
if (!entryOptions) return;
|
|
46
|
+
if (true === entryOptions) entryOptions = {
|
|
47
|
+
routes: [],
|
|
48
|
+
headers: {}
|
|
49
|
+
};
|
|
50
|
+
const { routes: userRoutes = [], headers } = entryOptions || {};
|
|
51
|
+
if (userRoutes.length > 0) userRoutes.forEach((route)=>{
|
|
52
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
53
|
+
});
|
|
54
|
+
else agreedRoutes.forEach((route)=>{
|
|
55
|
+
if (!isDynamicUrl(route.path)) ssgRoutes.push(makeRoute(pageRoute, route.path, headers));
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
if (!entryOptions) return;
|
|
59
|
+
if (true === entryOptions) ssgRoutes.push({
|
|
60
|
+
...pageRoute,
|
|
61
|
+
output: entryPath
|
|
62
|
+
});
|
|
63
|
+
else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
64
|
+
const { routes: enrtyRoutes, headers } = entryOptions;
|
|
65
|
+
enrtyRoutes.forEach((route)=>{
|
|
66
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
if (0 === ssgRoutes.length) return;
|
|
72
|
+
ssgRoutes.forEach((ssgRoute)=>{
|
|
73
|
+
if (ssgRoute.isSSR) {
|
|
74
|
+
const isOriginRoute = pageRoutes.some((pageRoute)=>pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
|
|
75
|
+
if (isOriginRoute) throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
|
|
76
|
+
logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
|
|
77
|
+
}
|
|
78
|
+
ssgRoute.isSSR = false;
|
|
79
|
+
ssgRoute.output = formatOutput(ssgRoute.output);
|
|
80
|
+
});
|
|
81
|
+
const htmlAry = await createServer(appContext, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig);
|
|
82
|
+
writeHtmlFile(htmlAry, ssgRoutes, buildDir);
|
|
83
|
+
replaceRoute(ssgRoutes, pageRoutes);
|
|
84
|
+
writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
|
|
85
|
+
logger.info('ssg Compiled successfully');
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const src = ssgPlugin;
|
|
90
|
+
export default src;
|
|
91
|
+
export { ssgPlugin };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import normalize_path from "normalize-path";
|
|
3
|
+
import path from "path";
|
|
4
|
+
function makeRender(ssgRoutes, render, port) {
|
|
5
|
+
return ssgRoutes.map((ssgRoute)=>render({
|
|
6
|
+
url: ssgRoute.urlPath,
|
|
7
|
+
headers: {
|
|
8
|
+
host: `localhost:${port}`,
|
|
9
|
+
...ssgRoute.headers
|
|
10
|
+
},
|
|
11
|
+
connection: {}
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
function makeRoute(baseRoute, route, headers = {}) {
|
|
15
|
+
const { urlPath, entryPath } = baseRoute;
|
|
16
|
+
if ('string' == typeof route) return {
|
|
17
|
+
...baseRoute,
|
|
18
|
+
urlPath: normalize_path(`${urlPath}${route}`) || '/',
|
|
19
|
+
headers,
|
|
20
|
+
output: path.join(entryPath, `..${'/' === route ? '' : route}`)
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
...baseRoute,
|
|
24
|
+
urlPath: normalize_path(`${urlPath}${route.url}`) || '/',
|
|
25
|
+
headers: {
|
|
26
|
+
...headers,
|
|
27
|
+
...route.headers
|
|
28
|
+
},
|
|
29
|
+
output: route.output ? path.normalize(route.output) : path.join(entryPath, `..${'/' === route.url ? '' : route.url}`)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export { makeRender, makeRoute };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
import path from "path";
|
|
4
|
+
function writeHtmlFile(htmlAry, ssgRoutes, baseDir) {
|
|
5
|
+
htmlAry.forEach((html, index)=>{
|
|
6
|
+
const ssgRoute = ssgRoutes[index];
|
|
7
|
+
const filepath = path.join(baseDir, ssgRoute.output);
|
|
8
|
+
if (!fs.existsSync(path.dirname(filepath))) fs.ensureDirSync(path.dirname(filepath));
|
|
9
|
+
fs.writeFileSync(filepath, html);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export { writeHtmlFile };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import normalize_path from "normalize-path";
|
|
3
|
+
function exist(route, pageRoutes) {
|
|
4
|
+
return pageRoutes.slice().findIndex((pageRoute)=>{
|
|
5
|
+
const urlEqual = normalize_path(pageRoute.urlPath) === normalize_path(route.urlPath);
|
|
6
|
+
const entryEqual = pageRoute.entryName === route.entryName;
|
|
7
|
+
if (urlEqual && entryEqual) return true;
|
|
8
|
+
return false;
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function replaceRoute(ssgRoutes, pageRoutes) {
|
|
12
|
+
const cleanSsgRoutes = ssgRoutes.map((ssgRoute)=>{
|
|
13
|
+
const { output, headers, ...cleanSsgRoute } = ssgRoute;
|
|
14
|
+
return Object.assign(cleanSsgRoute, output ? {
|
|
15
|
+
entryPath: output
|
|
16
|
+
} : {});
|
|
17
|
+
});
|
|
18
|
+
const freshRoutes = [];
|
|
19
|
+
cleanSsgRoutes.forEach((ssgRoute)=>{
|
|
20
|
+
const index = exist(ssgRoute, pageRoutes);
|
|
21
|
+
if (index < 0) freshRoutes.push({
|
|
22
|
+
...ssgRoute
|
|
23
|
+
});
|
|
24
|
+
else pageRoutes[index].entryPath = ssgRoute.entryPath;
|
|
25
|
+
});
|
|
26
|
+
pageRoutes.push(...freshRoutes);
|
|
27
|
+
return pageRoutes;
|
|
28
|
+
}
|
|
29
|
+
export { exist, replaceRoute };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import __rslib_shim_module__ from "node:module";
|
|
2
|
+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
+
import { ROUTE_SPEC_FILE, SERVER_BUNDLE_DIRECTORY, fs, isSingleEntry } from "@modern-js/utils";
|
|
4
|
+
import path_0 from "path";
|
|
5
|
+
function formatOutput(filename) {
|
|
6
|
+
const outputPath = path_0.extname(filename) ? filename : `${filename}/index.html`;
|
|
7
|
+
return outputPath;
|
|
8
|
+
}
|
|
9
|
+
function formatPath(str) {
|
|
10
|
+
let addr = str;
|
|
11
|
+
if (!addr || 'string' != typeof addr) return addr;
|
|
12
|
+
if (addr.startsWith('.')) addr = addr.slice(1);
|
|
13
|
+
if (!addr.startsWith('/')) addr = `/${addr}`;
|
|
14
|
+
if (addr.endsWith('/') && '/' !== addr) addr = addr.slice(0, addr.length - 1);
|
|
15
|
+
return addr;
|
|
16
|
+
}
|
|
17
|
+
function isDynamicUrl(url) {
|
|
18
|
+
return url.includes(':') || url.endsWith('*');
|
|
19
|
+
}
|
|
20
|
+
function getUrlPrefix(route, baseUrl) {
|
|
21
|
+
let base = '';
|
|
22
|
+
if (Array.isArray(baseUrl)) {
|
|
23
|
+
const filters = baseUrl.filter((url)=>route.urlPath.includes(url));
|
|
24
|
+
if (filters.length > 1) {
|
|
25
|
+
const matched = filters.sort((a, b)=>a.length - b.length)[0];
|
|
26
|
+
if (!matched) throw new Error('');
|
|
27
|
+
base = matched;
|
|
28
|
+
}
|
|
29
|
+
} else base = baseUrl;
|
|
30
|
+
base = '/' === base ? '' : base;
|
|
31
|
+
const entryName = 'main' === route.entryName ? '' : route.entryName;
|
|
32
|
+
const prefix = `${base}/${entryName}`;
|
|
33
|
+
return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
|
|
34
|
+
}
|
|
35
|
+
function getOutput(route, base, agreed) {
|
|
36
|
+
const { output } = route;
|
|
37
|
+
if (output) return output;
|
|
38
|
+
if (agreed) {
|
|
39
|
+
const urlWithoutBase = route.urlPath.replace(base, '');
|
|
40
|
+
return urlWithoutBase.startsWith('/') ? urlWithoutBase.slice(1) : urlWithoutBase;
|
|
41
|
+
}
|
|
42
|
+
throw new Error(`routing must provide output when calling createPage(), check ${route.urlPath}`);
|
|
43
|
+
}
|
|
44
|
+
const readJSONSpec = (dir)=>{
|
|
45
|
+
const routeJSONPath = path_0.join(dir, ROUTE_SPEC_FILE);
|
|
46
|
+
const routeJSON = require(routeJSONPath);
|
|
47
|
+
const { routes } = routeJSON;
|
|
48
|
+
return routes;
|
|
49
|
+
};
|
|
50
|
+
const writeJSONSpec = (dir, routes)=>{
|
|
51
|
+
const routeJSONPath = path_0.join(dir, ROUTE_SPEC_FILE);
|
|
52
|
+
fs.writeJSONSync(routeJSONPath, {
|
|
53
|
+
routes
|
|
54
|
+
}, {
|
|
55
|
+
spaces: 2
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
const replaceWithAlias = (base, filePath, alias)=>path_0.posix.join(alias, path_0.posix.relative(base, filePath));
|
|
59
|
+
const standardOptions = (ssgOptions, entrypoints, routes, server, ssgByEntries)=>{
|
|
60
|
+
if (ssgByEntries && Object.keys(ssgByEntries).length > 0) {
|
|
61
|
+
const result = {};
|
|
62
|
+
Object.keys(ssgByEntries).forEach((key)=>{
|
|
63
|
+
const val = ssgByEntries[key];
|
|
64
|
+
if ('function' != typeof val) result[key] = val;
|
|
65
|
+
});
|
|
66
|
+
for (const entry of entrypoints){
|
|
67
|
+
const { entryName } = entry;
|
|
68
|
+
const configured = ssgByEntries[entryName];
|
|
69
|
+
if ('function' == typeof configured) {
|
|
70
|
+
const routesForEntry = routes.filter((r)=>r.entryName === entryName);
|
|
71
|
+
if (Array.isArray(server?.baseUrl)) for (const url of server.baseUrl)routesForEntry.filter((r)=>'string' == typeof r.urlPath && r.urlPath.startsWith(url)).forEach((r)=>{
|
|
72
|
+
result[r.urlPath] = configured(entryName, {
|
|
73
|
+
baseUrl: url
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
else result[entryName] = configured(entryName, {
|
|
77
|
+
baseUrl: server?.baseUrl
|
|
78
|
+
});
|
|
79
|
+
} else if (void 0 !== configured) result[entryName] = configured;
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
if (false === ssgOptions) return false;
|
|
84
|
+
if (true === ssgOptions) return entrypoints.reduce((opt, entry)=>{
|
|
85
|
+
opt[entry.entryName] = ssgOptions;
|
|
86
|
+
return opt;
|
|
87
|
+
}, {});
|
|
88
|
+
if ('object' == typeof ssgOptions) {
|
|
89
|
+
const isSingle = isSingleEntry(entrypoints);
|
|
90
|
+
if (isSingle) return {
|
|
91
|
+
main: ssgOptions
|
|
92
|
+
};
|
|
93
|
+
return entrypoints.reduce((opt, entry)=>{
|
|
94
|
+
opt[entry.entryName] = ssgOptions;
|
|
95
|
+
return opt;
|
|
96
|
+
}, {});
|
|
97
|
+
}
|
|
98
|
+
if ('function' == typeof ssgOptions) {
|
|
99
|
+
const intermediateOptions = {};
|
|
100
|
+
for (const entrypoint of entrypoints){
|
|
101
|
+
const { entryName } = entrypoint;
|
|
102
|
+
const routesForEntry = routes.filter((r)=>r.entryName === entryName);
|
|
103
|
+
if (Array.isArray(server?.baseUrl)) for (const url of server.baseUrl)routesForEntry.filter((r)=>'string' == typeof r.urlPath && r.urlPath.startsWith(url)).forEach((r)=>{
|
|
104
|
+
intermediateOptions[r.urlPath] = ssgOptions(entryName, {
|
|
105
|
+
baseUrl: url
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
else intermediateOptions[entryName] = ssgOptions(entryName, {
|
|
109
|
+
baseUrl: server?.baseUrl
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return intermediateOptions;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
const openRouteSSR = (routes, entries = [])=>routes.map((ssgRoute)=>({
|
|
117
|
+
...ssgRoute,
|
|
118
|
+
isSSR: entries.includes(ssgRoute.entryName),
|
|
119
|
+
bundle: `${SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
|
|
120
|
+
}));
|
|
121
|
+
const flattenRoutes = (routes)=>{
|
|
122
|
+
const parents = [];
|
|
123
|
+
const newRoutes = [];
|
|
124
|
+
const traverseRoute = (route)=>{
|
|
125
|
+
const parent = parents[parents.length - 1];
|
|
126
|
+
let path = parent ? `${parent.path}/${route.path || ''}`.replace(/\/+/g, '/') : route.path || '';
|
|
127
|
+
path = path.replace(/\/$/, '');
|
|
128
|
+
if (route._component && ('/' !== path || '/' === path && !parent)) newRoutes.push({
|
|
129
|
+
...route,
|
|
130
|
+
path
|
|
131
|
+
});
|
|
132
|
+
if (route.children) {
|
|
133
|
+
parents.push({
|
|
134
|
+
...route,
|
|
135
|
+
path
|
|
136
|
+
});
|
|
137
|
+
route.children.forEach(traverseRoute);
|
|
138
|
+
parents.pop();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
routes.forEach(traverseRoute);
|
|
142
|
+
return newRoutes;
|
|
143
|
+
};
|
|
144
|
+
function chunkArray(arr, size) {
|
|
145
|
+
const result = [];
|
|
146
|
+
for(let i = 0; i < arr.length; i += size)result.push(arr.slice(i, i + size));
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
export { chunkArray, flattenRoutes, formatOutput, formatPath, getOutput, getUrlPrefix, isDynamicUrl, openRouteSSR, readJSONSpec, replaceWithAlias, standardOptions, writeJSONSpec };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
import { createProdServer, loadServerPlugins } from "@modern-js/prod-server";
|
|
4
|
+
import { SERVER_DIR, createLogger, getMeta, logger } from "@modern-js/utils";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import { chunkArray, openRouteSSR } from "../libs/util.mjs";
|
|
7
|
+
function getLogger() {
|
|
8
|
+
const l = createLogger({
|
|
9
|
+
level: 'verbose'
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
...l,
|
|
13
|
+
error: (...args)=>{
|
|
14
|
+
console.error(...args);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const MAX_CONCURRENT_REQUESTS = 10;
|
|
19
|
+
function createMockIncomingMessage(url, headers = {}) {
|
|
20
|
+
const urlObj = new URL(url);
|
|
21
|
+
const mockReq = new IncomingMessage({});
|
|
22
|
+
mockReq.url = urlObj.pathname + urlObj.search;
|
|
23
|
+
mockReq.method = 'GET';
|
|
24
|
+
mockReq.headers = {
|
|
25
|
+
host: urlObj.host,
|
|
26
|
+
'user-agent': 'SSG-Renderer/1.0',
|
|
27
|
+
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
28
|
+
'accept-language': 'en-US,en;q=0.5',
|
|
29
|
+
'accept-encoding': 'gzip, deflate',
|
|
30
|
+
connection: 'keep-alive',
|
|
31
|
+
...headers
|
|
32
|
+
};
|
|
33
|
+
mockReq.httpVersion = '1.1';
|
|
34
|
+
mockReq.httpVersionMajor = 1;
|
|
35
|
+
mockReq.httpVersionMinor = 1;
|
|
36
|
+
mockReq.complete = true;
|
|
37
|
+
mockReq.rawHeaders = [];
|
|
38
|
+
mockReq.socket = {};
|
|
39
|
+
mockReq.connection = mockReq.socket;
|
|
40
|
+
return mockReq;
|
|
41
|
+
}
|
|
42
|
+
function createMockServerResponse() {
|
|
43
|
+
const mockRes = new ServerResponse({});
|
|
44
|
+
return mockRes;
|
|
45
|
+
}
|
|
46
|
+
const createServer = async (appContext, ssgRoutes, pageRoutes, apiRoutes, options)=>{
|
|
47
|
+
const entries = ssgRoutes.map((route)=>route.entryName);
|
|
48
|
+
const backup = openRouteSSR(pageRoutes, entries);
|
|
49
|
+
const total = backup.concat(apiRoutes);
|
|
50
|
+
try {
|
|
51
|
+
const meta = getMeta(appContext.metaName);
|
|
52
|
+
const distDirectory = appContext.distDirectory;
|
|
53
|
+
const serverConfigPath = path.resolve(distDirectory, SERVER_DIR, `${meta}.server`);
|
|
54
|
+
const plugins = appContext.serverPlugins;
|
|
55
|
+
const serverOptions = {
|
|
56
|
+
pwd: distDirectory,
|
|
57
|
+
config: options,
|
|
58
|
+
appContext,
|
|
59
|
+
serverConfigPath,
|
|
60
|
+
routes: total,
|
|
61
|
+
plugins: await loadServerPlugins(plugins, appContext.appDirectory || distDirectory),
|
|
62
|
+
staticGenerate: true,
|
|
63
|
+
logger: getLogger()
|
|
64
|
+
};
|
|
65
|
+
const nodeServer = await createProdServer(serverOptions);
|
|
66
|
+
const requestHandler = nodeServer.getRequestHandler();
|
|
67
|
+
const chunkedRoutes = chunkArray(ssgRoutes, MAX_CONCURRENT_REQUESTS);
|
|
68
|
+
const results = [];
|
|
69
|
+
for (const routes of chunkedRoutes){
|
|
70
|
+
const promises = routes.map(async (route)=>{
|
|
71
|
+
const url = `http://localhost${route.urlPath}`;
|
|
72
|
+
const request = new Request(url, {
|
|
73
|
+
method: 'GET',
|
|
74
|
+
headers: {
|
|
75
|
+
host: 'localhost',
|
|
76
|
+
'x-modern-ssg-render': 'true'
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
const mockReq = createMockIncomingMessage(url);
|
|
80
|
+
const mockRes = createMockServerResponse();
|
|
81
|
+
const response = await requestHandler(request, {
|
|
82
|
+
node: {
|
|
83
|
+
req: mockReq,
|
|
84
|
+
res: mockRes
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return await response.text();
|
|
88
|
+
});
|
|
89
|
+
const batch = await Promise.all(promises);
|
|
90
|
+
results.push(...batch);
|
|
91
|
+
}
|
|
92
|
+
return results;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
logger.error(e instanceof Error ? e.stack : e.toString());
|
|
95
|
+
throw new Error('ssg render failed');
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export { createServer };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import events from "events";
|
|
3
|
+
import node_mocks_http from "node-mocks-http";
|
|
4
|
+
import { Readable } from "stream";
|
|
5
|
+
const compile = (requestHandler)=>(options, extend = {})=>new Promise((resolve, reject)=>{
|
|
6
|
+
const req = node_mocks_http.createRequest({
|
|
7
|
+
...options,
|
|
8
|
+
eventEmitter: Readable
|
|
9
|
+
});
|
|
10
|
+
const res = node_mocks_http.createResponse({
|
|
11
|
+
eventEmitter: events
|
|
12
|
+
});
|
|
13
|
+
Object.assign(req, extend);
|
|
14
|
+
const proxyRes = new Proxy(res, {
|
|
15
|
+
get (obj, prop) {
|
|
16
|
+
if ('symbol' == typeof prop && !obj[prop]) return null;
|
|
17
|
+
return obj[prop];
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
res.on('finish', ()=>{
|
|
21
|
+
if (200 !== res.statusCode) reject(new Error(res.statusMessage || 'Prerender failed'));
|
|
22
|
+
else resolve(res._getData());
|
|
23
|
+
});
|
|
24
|
+
res.on('error', (e)=>reject(e));
|
|
25
|
+
try {
|
|
26
|
+
requestHandler(req, proxyRes);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
reject(e);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
export { compile };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "node:module";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
2
|
+
import type { compile } from '../server/prerender';
|
|
3
|
+
import type { SSGRouteOptions, SsgRoute } from '../types';
|
|
4
|
+
export declare function makeRender(ssgRoutes: SsgRoute[], render: ReturnType<typeof compile>, port: number): Promise<string>[];
|
|
5
|
+
export declare function makeRoute(baseRoute: ModernRoute, route: string | SSGRouteOptions, headers?: Record<string, any>): SsgRoute;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
2
|
+
import type { SsgRoute } from '../types';
|
|
3
|
+
export declare function exist(route: ModernRoute, pageRoutes: ModernRoute[]): number;
|
|
4
|
+
export declare function replaceRoute(ssgRoutes: SsgRoute[], pageRoutes: ModernRoute[]): ModernRoute[];
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ServerUserConfig } from '@modern-js/app-tools';
|
|
2
|
+
import type { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
3
|
+
import type { AgreedRoute, EntryPoint, SSGConfig, SSGMultiEntryOptions, SsgRoute } from '../types';
|
|
4
|
+
export declare function formatOutput(filename: string): string;
|
|
5
|
+
export declare function formatPath(str: string): string;
|
|
6
|
+
export declare function isDynamicUrl(url: string): boolean;
|
|
7
|
+
export declare function getUrlPrefix(route: SsgRoute, baseUrl: string | string[]): string;
|
|
8
|
+
export declare function getOutput(route: SsgRoute, base: string, agreed?: boolean): string;
|
|
9
|
+
export declare const readJSONSpec: (dir: string) => ModernRoute[];
|
|
10
|
+
export declare const writeJSONSpec: (dir: string, routes: ModernRoute[]) => void;
|
|
11
|
+
export declare const replaceWithAlias: (base: string, filePath: string, alias: string) => string;
|
|
12
|
+
export declare const standardOptions: (ssgOptions: SSGConfig, entrypoints: EntryPoint[], routes: ModernRoute[], server: ServerUserConfig, ssgByEntries?: SSGMultiEntryOptions) => false | SSGMultiEntryOptions;
|
|
13
|
+
export declare const openRouteSSR: (routes: ModernRoute[], entries?: string[]) => {
|
|
14
|
+
isSSR: boolean;
|
|
15
|
+
bundle: string;
|
|
16
|
+
entryName?: string;
|
|
17
|
+
urlPath: string;
|
|
18
|
+
entryPath: string;
|
|
19
|
+
isSPA?: boolean;
|
|
20
|
+
isRSC?: boolean;
|
|
21
|
+
isStream?: boolean;
|
|
22
|
+
isApi?: boolean;
|
|
23
|
+
worker?: string;
|
|
24
|
+
responseHeaders?: Record<string, unknown>;
|
|
25
|
+
}[];
|
|
26
|
+
export declare const flattenRoutes: (routes: AgreedRoute[]) => AgreedRoute[];
|
|
27
|
+
export declare function chunkArray<T>(arr: T[], size: number): T[][];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const CLOSE_SIGN = "modern_close_server";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AppNormalizedConfig, AppToolsContext } from '@modern-js/app-tools';
|
|
2
|
+
import type { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
3
|
+
import type { SsgRoute } from '../types';
|
|
4
|
+
export declare const createServer: (appContext: AppToolsContext, ssgRoutes: SsgRoute[], pageRoutes: ModernRoute[], apiRoutes: ModernRoute[], options: AppNormalizedConfig) => Promise<string[]>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NodeRequest, NodeResponse } from '@modern-js/types';
|
|
2
|
+
export type Options = {
|
|
3
|
+
url: string;
|
|
4
|
+
headers: {
|
|
5
|
+
host: string;
|
|
6
|
+
[key: string]: string;
|
|
7
|
+
};
|
|
8
|
+
[propName: string]: any;
|
|
9
|
+
};
|
|
10
|
+
export declare const compile: (requestHandler: (req: NodeRequest, res: NodeResponse) => void) => (options: Options, extend?: {}) => Promise<string>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ServerRoute as ModernRoute, SSGConfig, SSGMultiEntryOptions, SSGRouteOptions, SSGSingleEntryOptions } from '@modern-js/types';
|
|
2
|
+
export type { SSGConfig, SSGMultiEntryOptions, SSGRouteOptions, SSGSingleEntryOptions, };
|
|
3
|
+
export type AgreedRoute = {
|
|
4
|
+
path?: string;
|
|
5
|
+
component?: string;
|
|
6
|
+
_component?: string;
|
|
7
|
+
children?: AgreedRoute[];
|
|
8
|
+
exact?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export type EntryPoint = {
|
|
11
|
+
entryName: string;
|
|
12
|
+
entry: string;
|
|
13
|
+
};
|
|
14
|
+
export type AgreedRouteMap = {
|
|
15
|
+
[propNames: string]: AgreedRoute[];
|
|
16
|
+
};
|
|
17
|
+
export type SsgRoute = ModernRoute & {
|
|
18
|
+
output: string;
|
|
19
|
+
headers?: Record<string, string>;
|
|
20
|
+
};
|
|
21
|
+
export type ExtendOutputConfig = {
|
|
22
|
+
ssg: SSGConfig;
|
|
23
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bleedingdev/modern-js-plugin-ssg",
|
|
3
|
+
"description": "A Progressive React Framework for modern web development.",
|
|
4
|
+
"homepage": "https://github.com/BleedingDev/ultramodern.js#readme",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/BleedingDev/ultramodern.js/issues"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/BleedingDev/ultramodern.js.git",
|
|
11
|
+
"directory": "packages/cli/plugin-ssg"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"react",
|
|
16
|
+
"framework",
|
|
17
|
+
"modern",
|
|
18
|
+
"modern.js"
|
|
19
|
+
],
|
|
20
|
+
"version": "3.2.0-ultramodern.0",
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
|
+
"main": "./dist/cjs/index.js",
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
".": [
|
|
26
|
+
"./dist/types/index.d.ts"
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/types/index.d.ts",
|
|
33
|
+
"node": {
|
|
34
|
+
"import": "./dist/esm-node/index.mjs",
|
|
35
|
+
"require": "./dist/cjs/index.js"
|
|
36
|
+
},
|
|
37
|
+
"default": "./dist/cjs/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./cli": {
|
|
40
|
+
"types": "./dist/types/index.d.ts",
|
|
41
|
+
"node": {
|
|
42
|
+
"import": "./dist/esm-node/index.mjs",
|
|
43
|
+
"require": "./dist/cjs/index.js"
|
|
44
|
+
},
|
|
45
|
+
"default": "./dist/cjs/index.js"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@swc/helpers": "^0.5.21",
|
|
50
|
+
"node-mocks-http": "^1.17.2",
|
|
51
|
+
"normalize-path": "3.0.0",
|
|
52
|
+
"@modern-js/prod-server": "npm:@bleedingdev/modern-js-prod-server@3.2.0-ultramodern.0",
|
|
53
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"react-router-dom": ">=7.12.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"react-router-dom": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@rslib/core": "0.21.5",
|
|
65
|
+
"@types/node": "^25.8.0",
|
|
66
|
+
"@typescript/native-preview": "7.0.0-dev.20260516.1",
|
|
67
|
+
"react": "^19.2.6",
|
|
68
|
+
"react-dom": "^19.2.6",
|
|
69
|
+
"react-router-dom": "^7.15.1",
|
|
70
|
+
"@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.2.0-ultramodern.0",
|
|
71
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.0",
|
|
72
|
+
"@scripts/rstest-config": "2.66.0"
|
|
73
|
+
},
|
|
74
|
+
"sideEffects": false,
|
|
75
|
+
"publishConfig": {
|
|
76
|
+
"registry": "https://registry.npmjs.org/",
|
|
77
|
+
"access": "public"
|
|
78
|
+
},
|
|
79
|
+
"scripts": {
|
|
80
|
+
"build": "rslib build",
|
|
81
|
+
"dev": "rslib build --watch",
|
|
82
|
+
"test": "rstest --passWithNoTests"
|
|
83
|
+
}
|
|
84
|
+
}
|
package/rslib.config.mts
ADDED