@modern-js/plugin-ssg 2.69.4 → 3.0.0-alpha.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/dist/cjs/index.js +130 -156
- package/dist/cjs/libs/make.js +69 -62
- package/dist/cjs/libs/output.js +50 -44
- package/dist/cjs/libs/replace.js +68 -64
- package/dist/cjs/libs/util.js +200 -182
- package/dist/cjs/server/consts.js +33 -25
- package/dist/cjs/server/index.js +137 -92
- package/dist/cjs/server/prerender.js +72 -68
- package/dist/cjs/types.js +17 -15
- package/dist/esm/index.mjs +89 -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-node/index.mjs +89 -0
- package/dist/esm-node/libs/make.mjs +31 -0
- package/dist/esm-node/libs/output.mjs +11 -0
- package/dist/esm-node/libs/replace.mjs +28 -0
- package/dist/esm-node/libs/util.mjs +147 -0
- package/dist/esm-node/server/consts.mjs +2 -0
- package/dist/esm-node/server/index.mjs +97 -0
- package/dist/esm-node/server/prerender.mjs +30 -0
- package/dist/types/libs/util.d.ts +1 -1
- package/dist/types/server/index.d.ts +2 -2
- package/package.json +27 -29
- package/rslib.config.mts +4 -0
- package/rstest.config.ts +7 -0
- package/dist/cjs/server/process.js +0 -108
- package/dist/esm/index.js +0 -163
- package/dist/esm/libs/make.js +0 -36
- package/dist/esm/libs/output.js +0 -15
- package/dist/esm/libs/replace.js +0 -41
- package/dist/esm/libs/util.js +0 -210
- package/dist/esm/server/consts.js +0 -4
- package/dist/esm/server/index.js +0 -66
- package/dist/esm/server/prerender.js +0 -46
- package/dist/esm/server/process.js +0 -263
- package/dist/esm-node/index.js +0 -128
- package/dist/esm-node/libs/make.js +0 -37
- package/dist/esm-node/libs/output.js +0 -15
- package/dist/esm-node/libs/replace.js +0 -36
- package/dist/esm-node/libs/util.js +0 -161
- package/dist/esm-node/server/consts.js +0 -4
- package/dist/esm-node/server/index.js +0 -62
- package/dist/esm-node/server/prerender.js +0 -37
- package/dist/esm-node/server/process.js +0 -85
- package/dist/types/server/process.d.ts +0 -1
- /package/dist/esm/{types.js → types.mjs} +0 -0
- /package/dist/esm-node/{types.js → types.mjs} +0 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import path_0 from "path";
|
|
2
|
+
import { ROUTE_SPEC_FILE, SERVER_BUNDLE_DIRECTORY, fs, isSingleEntry } from "@modern-js/utils";
|
|
3
|
+
function formatOutput(filename) {
|
|
4
|
+
const outputPath = path_0.extname(filename) ? filename : `${filename}/index.html`;
|
|
5
|
+
return outputPath;
|
|
6
|
+
}
|
|
7
|
+
function formatPath(str) {
|
|
8
|
+
let addr = str;
|
|
9
|
+
if (!addr || 'string' != typeof addr) return addr;
|
|
10
|
+
if (addr.startsWith('.')) addr = addr.slice(1);
|
|
11
|
+
if (!addr.startsWith('/')) addr = `/${addr}`;
|
|
12
|
+
if (addr.endsWith('/') && '/' !== addr) addr = addr.slice(0, addr.length - 1);
|
|
13
|
+
return addr;
|
|
14
|
+
}
|
|
15
|
+
function isDynamicUrl(url) {
|
|
16
|
+
return url.includes(':') || url.endsWith('*');
|
|
17
|
+
}
|
|
18
|
+
function getUrlPrefix(route, baseUrl) {
|
|
19
|
+
let base = '';
|
|
20
|
+
if (Array.isArray(baseUrl)) {
|
|
21
|
+
const filters = baseUrl.filter((url)=>route.urlPath.includes(url));
|
|
22
|
+
if (filters.length > 1) {
|
|
23
|
+
const matched = filters.sort((a, b)=>a.length - b.length)[0];
|
|
24
|
+
if (!matched) throw new Error('');
|
|
25
|
+
base = matched;
|
|
26
|
+
}
|
|
27
|
+
} else base = baseUrl;
|
|
28
|
+
base = '/' === base ? '' : base;
|
|
29
|
+
const entryName = 'main' === route.entryName ? '' : route.entryName;
|
|
30
|
+
const prefix = `${base}/${entryName}`;
|
|
31
|
+
return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
|
|
32
|
+
}
|
|
33
|
+
function getOutput(route, base, agreed) {
|
|
34
|
+
const { output } = route;
|
|
35
|
+
if (output) return output;
|
|
36
|
+
if (agreed) {
|
|
37
|
+
const urlWithoutBase = route.urlPath.replace(base, '');
|
|
38
|
+
return urlWithoutBase.startsWith('/') ? urlWithoutBase.slice(1) : urlWithoutBase;
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`routing must provide output when calling createPage(), check ${route.urlPath}`);
|
|
41
|
+
}
|
|
42
|
+
const readJSONSpec = (dir)=>{
|
|
43
|
+
const routeJSONPath = path_0.join(dir, ROUTE_SPEC_FILE);
|
|
44
|
+
const routeJSON = require(routeJSONPath);
|
|
45
|
+
const { routes } = routeJSON;
|
|
46
|
+
return routes;
|
|
47
|
+
};
|
|
48
|
+
const writeJSONSpec = (dir, routes)=>{
|
|
49
|
+
const routeJSONPath = path_0.join(dir, ROUTE_SPEC_FILE);
|
|
50
|
+
fs.writeJSONSync(routeJSONPath, {
|
|
51
|
+
routes
|
|
52
|
+
}, {
|
|
53
|
+
spaces: 2
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
const replaceWithAlias = (base, filePath, alias)=>path_0.posix.join(alias, path_0.posix.relative(base, filePath));
|
|
57
|
+
const standardOptions = (ssgOptions, entrypoints, routes, server, ssgByEntries)=>{
|
|
58
|
+
if (ssgByEntries && Object.keys(ssgByEntries).length > 0) {
|
|
59
|
+
const result = {};
|
|
60
|
+
Object.keys(ssgByEntries).forEach((key)=>{
|
|
61
|
+
const val = ssgByEntries[key];
|
|
62
|
+
if ('function' != typeof val) result[key] = val;
|
|
63
|
+
});
|
|
64
|
+
for (const entry of entrypoints){
|
|
65
|
+
const { entryName } = entry;
|
|
66
|
+
const configured = ssgByEntries[entryName];
|
|
67
|
+
if ('function' == typeof configured) {
|
|
68
|
+
const routesForEntry = routes.filter((r)=>r.entryName === entryName);
|
|
69
|
+
if (Array.isArray(server?.baseUrl)) for (const url of server.baseUrl)routesForEntry.filter((r)=>'string' == typeof r.urlPath && r.urlPath.startsWith(url)).forEach((r)=>{
|
|
70
|
+
result[r.urlPath] = configured(entryName, {
|
|
71
|
+
baseUrl: url
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
else result[entryName] = configured(entryName, {
|
|
75
|
+
baseUrl: server?.baseUrl
|
|
76
|
+
});
|
|
77
|
+
} else if (void 0 !== configured) result[entryName] = configured;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
if (false === ssgOptions) return false;
|
|
82
|
+
if (true === ssgOptions) return entrypoints.reduce((opt, entry)=>{
|
|
83
|
+
opt[entry.entryName] = ssgOptions;
|
|
84
|
+
return opt;
|
|
85
|
+
}, {});
|
|
86
|
+
if ('object' == typeof ssgOptions) {
|
|
87
|
+
const isSingle = isSingleEntry(entrypoints);
|
|
88
|
+
if (isSingle) return {
|
|
89
|
+
main: ssgOptions
|
|
90
|
+
};
|
|
91
|
+
return entrypoints.reduce((opt, entry)=>{
|
|
92
|
+
opt[entry.entryName] = ssgOptions;
|
|
93
|
+
return opt;
|
|
94
|
+
}, {});
|
|
95
|
+
}
|
|
96
|
+
if ('function' == typeof ssgOptions) {
|
|
97
|
+
const intermediateOptions = {};
|
|
98
|
+
for (const entrypoint of entrypoints){
|
|
99
|
+
const { entryName } = entrypoint;
|
|
100
|
+
const routesForEntry = routes.filter((r)=>r.entryName === entryName);
|
|
101
|
+
if (Array.isArray(server?.baseUrl)) for (const url of server.baseUrl)routesForEntry.filter((r)=>'string' == typeof r.urlPath && r.urlPath.startsWith(url)).forEach((r)=>{
|
|
102
|
+
intermediateOptions[r.urlPath] = ssgOptions(entryName, {
|
|
103
|
+
baseUrl: url
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
else intermediateOptions[entryName] = ssgOptions(entryName, {
|
|
107
|
+
baseUrl: server?.baseUrl
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return intermediateOptions;
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
};
|
|
114
|
+
const openRouteSSR = (routes, entries = [])=>routes.map((ssgRoute)=>({
|
|
115
|
+
...ssgRoute,
|
|
116
|
+
isSSR: entries.includes(ssgRoute.entryName),
|
|
117
|
+
bundle: `${SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
|
|
118
|
+
}));
|
|
119
|
+
const flattenRoutes = (routes)=>{
|
|
120
|
+
const parents = [];
|
|
121
|
+
const newRoutes = [];
|
|
122
|
+
const traverseRoute = (route)=>{
|
|
123
|
+
const parent = parents[parents.length - 1];
|
|
124
|
+
let path = parent ? `${parent.path}/${route.path || ''}`.replace(/\/+/g, '/') : route.path || '';
|
|
125
|
+
path = path.replace(/\/$/, '');
|
|
126
|
+
if (route._component && ('/' !== path || '/' === path && !parent)) newRoutes.push({
|
|
127
|
+
...route,
|
|
128
|
+
path
|
|
129
|
+
});
|
|
130
|
+
if (route.children) {
|
|
131
|
+
parents.push({
|
|
132
|
+
...route,
|
|
133
|
+
path
|
|
134
|
+
});
|
|
135
|
+
route.children.forEach(traverseRoute);
|
|
136
|
+
parents.pop();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
routes.forEach(traverseRoute);
|
|
140
|
+
return newRoutes;
|
|
141
|
+
};
|
|
142
|
+
function chunkArray(arr, size) {
|
|
143
|
+
const result = [];
|
|
144
|
+
for(let i = 0; i < arr.length; i += size)result.push(arr.slice(i, i + size));
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
export { chunkArray, flattenRoutes, formatOutput, formatPath, getOutput, getUrlPrefix, isDynamicUrl, openRouteSSR, readJSONSpec, replaceWithAlias, standardOptions, writeJSONSpec };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { createProdServer, loadServerPlugins } from "@modern-js/prod-server";
|
|
4
|
+
import { SERVER_DIR, createLogger, getMeta, logger } from "@modern-js/utils";
|
|
5
|
+
import { chunkArray, openRouteSSR } from "../libs/util";
|
|
6
|
+
function getLogger() {
|
|
7
|
+
const l = createLogger({
|
|
8
|
+
level: 'verbose'
|
|
9
|
+
});
|
|
10
|
+
return {
|
|
11
|
+
...l,
|
|
12
|
+
error: (...args)=>{
|
|
13
|
+
console.error(...args);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const MAX_CONCURRENT_REQUESTS = 10;
|
|
18
|
+
function createMockIncomingMessage(url, headers = {}) {
|
|
19
|
+
const urlObj = new URL(url);
|
|
20
|
+
const mockReq = new IncomingMessage({});
|
|
21
|
+
mockReq.url = urlObj.pathname + urlObj.search;
|
|
22
|
+
mockReq.method = 'GET';
|
|
23
|
+
mockReq.headers = {
|
|
24
|
+
host: urlObj.host,
|
|
25
|
+
'user-agent': 'SSG-Renderer/1.0',
|
|
26
|
+
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
27
|
+
'accept-language': 'en-US,en;q=0.5',
|
|
28
|
+
'accept-encoding': 'gzip, deflate',
|
|
29
|
+
connection: 'keep-alive',
|
|
30
|
+
...headers
|
|
31
|
+
};
|
|
32
|
+
mockReq.httpVersion = '1.1';
|
|
33
|
+
mockReq.httpVersionMajor = 1;
|
|
34
|
+
mockReq.httpVersionMinor = 1;
|
|
35
|
+
mockReq.complete = true;
|
|
36
|
+
mockReq.rawHeaders = [];
|
|
37
|
+
mockReq.socket = {};
|
|
38
|
+
mockReq.connection = mockReq.socket;
|
|
39
|
+
return mockReq;
|
|
40
|
+
}
|
|
41
|
+
function createMockServerResponse() {
|
|
42
|
+
const mockRes = new ServerResponse({});
|
|
43
|
+
return mockRes;
|
|
44
|
+
}
|
|
45
|
+
const createServer = async (appContext, ssgRoutes, pageRoutes, apiRoutes, options)=>{
|
|
46
|
+
const entries = ssgRoutes.map((route)=>route.entryName);
|
|
47
|
+
const backup = openRouteSSR(pageRoutes, entries);
|
|
48
|
+
const total = backup.concat(apiRoutes);
|
|
49
|
+
try {
|
|
50
|
+
const meta = getMeta(appContext.metaName);
|
|
51
|
+
const distDirectory = appContext.distDirectory;
|
|
52
|
+
const serverConfigPath = path.resolve(distDirectory, SERVER_DIR, `${meta}.server`);
|
|
53
|
+
const plugins = appContext.serverPlugins;
|
|
54
|
+
const serverOptions = {
|
|
55
|
+
pwd: distDirectory,
|
|
56
|
+
config: options,
|
|
57
|
+
appContext,
|
|
58
|
+
serverConfigPath,
|
|
59
|
+
routes: total,
|
|
60
|
+
plugins: await loadServerPlugins(plugins, appContext.appDirectory || distDirectory),
|
|
61
|
+
staticGenerate: true,
|
|
62
|
+
logger: getLogger()
|
|
63
|
+
};
|
|
64
|
+
const nodeServer = await createProdServer(serverOptions);
|
|
65
|
+
const requestHandler = nodeServer.getRequestHandler();
|
|
66
|
+
const chunkedRoutes = chunkArray(ssgRoutes, MAX_CONCURRENT_REQUESTS);
|
|
67
|
+
const results = [];
|
|
68
|
+
for (const routes of chunkedRoutes){
|
|
69
|
+
const promises = routes.map(async (route)=>{
|
|
70
|
+
const url = `http://localhost${route.urlPath}`;
|
|
71
|
+
const request = new Request(url, {
|
|
72
|
+
method: 'GET',
|
|
73
|
+
headers: {
|
|
74
|
+
host: 'localhost',
|
|
75
|
+
'x-modern-ssg-render': 'true'
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
const mockReq = createMockIncomingMessage(url);
|
|
79
|
+
const mockRes = createMockServerResponse();
|
|
80
|
+
const response = await requestHandler(request, {
|
|
81
|
+
node: {
|
|
82
|
+
req: mockReq,
|
|
83
|
+
res: mockRes
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
return await response.text();
|
|
87
|
+
});
|
|
88
|
+
const batch = await Promise.all(promises);
|
|
89
|
+
results.push(...batch);
|
|
90
|
+
}
|
|
91
|
+
return results;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
logger.error(e instanceof Error ? e.stack : e.toString());
|
|
94
|
+
throw new Error('ssg render failed');
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
export { createServer };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import events from "events";
|
|
2
|
+
import { Readable } from "stream";
|
|
3
|
+
import node_mocks_http from "node-mocks-http";
|
|
4
|
+
const compile = (requestHandler)=>(options, extend = {})=>new Promise((resolve, reject)=>{
|
|
5
|
+
const req = node_mocks_http.createRequest({
|
|
6
|
+
...options,
|
|
7
|
+
eventEmitter: Readable
|
|
8
|
+
});
|
|
9
|
+
const res = node_mocks_http.createResponse({
|
|
10
|
+
eventEmitter: events
|
|
11
|
+
});
|
|
12
|
+
Object.assign(req, extend);
|
|
13
|
+
const proxyRes = new Proxy(res, {
|
|
14
|
+
get (obj, prop) {
|
|
15
|
+
if ('symbol' == typeof prop && !obj[prop]) return null;
|
|
16
|
+
return obj[prop];
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
res.on('finish', ()=>{
|
|
20
|
+
if (200 !== res.statusCode) reject(new Error(res.statusMessage));
|
|
21
|
+
else resolve(res._getData());
|
|
22
|
+
});
|
|
23
|
+
res.on('error', (e)=>reject(e));
|
|
24
|
+
try {
|
|
25
|
+
requestHandler(req, proxyRes);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
export { compile };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { filterRoutesForServer, logger } from "@modern-js/utils";
|
|
3
|
+
import { makeRoute } from "./libs/make.mjs";
|
|
4
|
+
import { writeHtmlFile } from "./libs/output.mjs";
|
|
5
|
+
import { replaceRoute } from "./libs/replace.mjs";
|
|
6
|
+
import { flattenRoutes, formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util.mjs";
|
|
7
|
+
import { createServer } from "./server/index.mjs";
|
|
8
|
+
const ssgPlugin = ()=>({
|
|
9
|
+
name: '@modern-js/plugin-ssg',
|
|
10
|
+
pre: [
|
|
11
|
+
'@modern-js/plugin-bff'
|
|
12
|
+
],
|
|
13
|
+
setup: (api)=>{
|
|
14
|
+
const agreedRouteMap = {};
|
|
15
|
+
api.modifyFileSystemRoutes(async ({ entrypoint, routes })=>{
|
|
16
|
+
const { entryName } = entrypoint;
|
|
17
|
+
const flattedRoutes = flattenRoutes(filterRoutesForServer(routes));
|
|
18
|
+
agreedRouteMap[entryName] = flattedRoutes;
|
|
19
|
+
return {
|
|
20
|
+
entrypoint,
|
|
21
|
+
routes
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
api.onAfterBuild(async ()=>{
|
|
25
|
+
const resolvedConfig = api.getNormalizedConfig();
|
|
26
|
+
const appContext = api.getAppContext();
|
|
27
|
+
const { appDirectory, entrypoints } = appContext;
|
|
28
|
+
const { output, server } = resolvedConfig;
|
|
29
|
+
const { ssg, ssgByEntries, distPath: { root: outputPath } = {} } = output;
|
|
30
|
+
const ssgOptions = (Array.isArray(ssg) ? ssg.pop() : ssg) ?? true;
|
|
31
|
+
const buildDir = path.join(appDirectory, outputPath);
|
|
32
|
+
const routes = readJSONSpec(buildDir);
|
|
33
|
+
const pageRoutes = routes.filter((route)=>route.isSPA);
|
|
34
|
+
const apiRoutes = routes.filter((route)=>!route.isSPA);
|
|
35
|
+
if (0 === pageRoutes.length) return;
|
|
36
|
+
const intermediateOptions = standardOptions(ssgOptions, entrypoints, pageRoutes, server, ssgByEntries);
|
|
37
|
+
if (!intermediateOptions) return;
|
|
38
|
+
const ssgRoutes = [];
|
|
39
|
+
pageRoutes.forEach((pageRoute)=>{
|
|
40
|
+
const { entryName, entryPath } = pageRoute;
|
|
41
|
+
const agreedRoutes = agreedRouteMap[entryName];
|
|
42
|
+
let entryOptions = intermediateOptions[entryName] || intermediateOptions[pageRoute.urlPath];
|
|
43
|
+
if (agreedRoutes) {
|
|
44
|
+
if (!entryOptions) return;
|
|
45
|
+
if (true === entryOptions) entryOptions = {
|
|
46
|
+
routes: [],
|
|
47
|
+
headers: {}
|
|
48
|
+
};
|
|
49
|
+
const { routes: userRoutes = [], headers } = entryOptions || {};
|
|
50
|
+
if (userRoutes.length > 0) userRoutes.forEach((route)=>{
|
|
51
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
52
|
+
});
|
|
53
|
+
else agreedRoutes.forEach((route)=>{
|
|
54
|
+
if (!isDynamicUrl(route.path)) ssgRoutes.push(makeRoute(pageRoute, route.path, headers));
|
|
55
|
+
});
|
|
56
|
+
} else {
|
|
57
|
+
if (!entryOptions) return;
|
|
58
|
+
if (true === entryOptions) ssgRoutes.push({
|
|
59
|
+
...pageRoute,
|
|
60
|
+
output: entryPath
|
|
61
|
+
});
|
|
62
|
+
else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
63
|
+
const { routes: enrtyRoutes, headers } = entryOptions;
|
|
64
|
+
enrtyRoutes.forEach((route)=>{
|
|
65
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
if (0 === ssgRoutes.length) return;
|
|
71
|
+
ssgRoutes.forEach((ssgRoute)=>{
|
|
72
|
+
if (ssgRoute.isSSR) {
|
|
73
|
+
const isOriginRoute = pageRoutes.some((pageRoute)=>pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
|
|
74
|
+
if (isOriginRoute) throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
|
|
75
|
+
logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
|
|
76
|
+
}
|
|
77
|
+
ssgRoute.isSSR = false;
|
|
78
|
+
ssgRoute.output = formatOutput(ssgRoute.output);
|
|
79
|
+
});
|
|
80
|
+
const htmlAry = await createServer(appContext, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig);
|
|
81
|
+
writeHtmlFile(htmlAry, ssgRoutes, buildDir);
|
|
82
|
+
replaceRoute(ssgRoutes, pageRoutes);
|
|
83
|
+
writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
|
|
84
|
+
logger.info('ssg Compiled successfully');
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const src = ssgPlugin;
|
|
89
|
+
export { src as default, ssgPlugin };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import normalize_path from "normalize-path";
|
|
3
|
+
function makeRender(ssgRoutes, render, port) {
|
|
4
|
+
return ssgRoutes.map((ssgRoute)=>render({
|
|
5
|
+
url: ssgRoute.urlPath,
|
|
6
|
+
headers: {
|
|
7
|
+
host: `localhost:${port}`,
|
|
8
|
+
...ssgRoute.headers
|
|
9
|
+
},
|
|
10
|
+
connection: {}
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
function makeRoute(baseRoute, route, headers = {}) {
|
|
14
|
+
const { urlPath, entryPath } = baseRoute;
|
|
15
|
+
if ('string' == typeof route) return {
|
|
16
|
+
...baseRoute,
|
|
17
|
+
urlPath: normalize_path(`${urlPath}${route}`) || '/',
|
|
18
|
+
headers,
|
|
19
|
+
output: path.join(entryPath, `..${'/' === route ? '' : route}`)
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
...baseRoute,
|
|
23
|
+
urlPath: normalize_path(`${urlPath}${route.url}`) || '/',
|
|
24
|
+
headers: {
|
|
25
|
+
...headers,
|
|
26
|
+
...route.headers
|
|
27
|
+
},
|
|
28
|
+
output: route.output ? path.normalize(route.output) : path.join(entryPath, `..${'/' === route.url ? '' : route.url}`)
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export { makeRender, makeRoute };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
function writeHtmlFile(htmlAry, ssgRoutes, baseDir) {
|
|
4
|
+
htmlAry.forEach((html, index)=>{
|
|
5
|
+
const ssgRoute = ssgRoutes[index];
|
|
6
|
+
const filepath = path.join(baseDir, ssgRoute.output);
|
|
7
|
+
if (!fs.existsSync(path.dirname(filepath))) fs.ensureDirSync(path.dirname(filepath));
|
|
8
|
+
fs.writeFileSync(filepath, html);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export { writeHtmlFile };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import normalize_path from "normalize-path";
|
|
2
|
+
function exist(route, pageRoutes) {
|
|
3
|
+
return pageRoutes.slice().findIndex((pageRoute)=>{
|
|
4
|
+
const urlEqual = normalize_path(pageRoute.urlPath) === normalize_path(route.urlPath);
|
|
5
|
+
const entryEqual = pageRoute.entryName === route.entryName;
|
|
6
|
+
if (urlEqual && entryEqual) return true;
|
|
7
|
+
return false;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function replaceRoute(ssgRoutes, pageRoutes) {
|
|
11
|
+
const cleanSsgRoutes = ssgRoutes.map((ssgRoute)=>{
|
|
12
|
+
const { output, headers, ...cleanSsgRoute } = ssgRoute;
|
|
13
|
+
return Object.assign(cleanSsgRoute, output ? {
|
|
14
|
+
entryPath: output
|
|
15
|
+
} : {});
|
|
16
|
+
});
|
|
17
|
+
const freshRoutes = [];
|
|
18
|
+
cleanSsgRoutes.forEach((ssgRoute)=>{
|
|
19
|
+
const index = exist(ssgRoute, pageRoutes);
|
|
20
|
+
if (index < 0) freshRoutes.push({
|
|
21
|
+
...ssgRoute
|
|
22
|
+
});
|
|
23
|
+
else pageRoutes[index].entryPath = ssgRoute.entryPath;
|
|
24
|
+
});
|
|
25
|
+
pageRoutes.push(...freshRoutes);
|
|
26
|
+
return pageRoutes;
|
|
27
|
+
}
|
|
28
|
+
export { exist, replaceRoute };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import path_0 from "path";
|
|
2
|
+
import { ROUTE_SPEC_FILE, SERVER_BUNDLE_DIRECTORY, fs, isSingleEntry } from "@modern-js/utils";
|
|
3
|
+
function formatOutput(filename) {
|
|
4
|
+
const outputPath = path_0.extname(filename) ? filename : `${filename}/index.html`;
|
|
5
|
+
return outputPath;
|
|
6
|
+
}
|
|
7
|
+
function formatPath(str) {
|
|
8
|
+
let addr = str;
|
|
9
|
+
if (!addr || 'string' != typeof addr) return addr;
|
|
10
|
+
if (addr.startsWith('.')) addr = addr.slice(1);
|
|
11
|
+
if (!addr.startsWith('/')) addr = `/${addr}`;
|
|
12
|
+
if (addr.endsWith('/') && '/' !== addr) addr = addr.slice(0, addr.length - 1);
|
|
13
|
+
return addr;
|
|
14
|
+
}
|
|
15
|
+
function isDynamicUrl(url) {
|
|
16
|
+
return url.includes(':') || url.endsWith('*');
|
|
17
|
+
}
|
|
18
|
+
function getUrlPrefix(route, baseUrl) {
|
|
19
|
+
let base = '';
|
|
20
|
+
if (Array.isArray(baseUrl)) {
|
|
21
|
+
const filters = baseUrl.filter((url)=>route.urlPath.includes(url));
|
|
22
|
+
if (filters.length > 1) {
|
|
23
|
+
const matched = filters.sort((a, b)=>a.length - b.length)[0];
|
|
24
|
+
if (!matched) throw new Error('');
|
|
25
|
+
base = matched;
|
|
26
|
+
}
|
|
27
|
+
} else base = baseUrl;
|
|
28
|
+
base = '/' === base ? '' : base;
|
|
29
|
+
const entryName = 'main' === route.entryName ? '' : route.entryName;
|
|
30
|
+
const prefix = `${base}/${entryName}`;
|
|
31
|
+
return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
|
|
32
|
+
}
|
|
33
|
+
function getOutput(route, base, agreed) {
|
|
34
|
+
const { output } = route;
|
|
35
|
+
if (output) return output;
|
|
36
|
+
if (agreed) {
|
|
37
|
+
const urlWithoutBase = route.urlPath.replace(base, '');
|
|
38
|
+
return urlWithoutBase.startsWith('/') ? urlWithoutBase.slice(1) : urlWithoutBase;
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`routing must provide output when calling createPage(), check ${route.urlPath}`);
|
|
41
|
+
}
|
|
42
|
+
const readJSONSpec = (dir)=>{
|
|
43
|
+
const routeJSONPath = path_0.join(dir, ROUTE_SPEC_FILE);
|
|
44
|
+
const routeJSON = require(routeJSONPath);
|
|
45
|
+
const { routes } = routeJSON;
|
|
46
|
+
return routes;
|
|
47
|
+
};
|
|
48
|
+
const writeJSONSpec = (dir, routes)=>{
|
|
49
|
+
const routeJSONPath = path_0.join(dir, ROUTE_SPEC_FILE);
|
|
50
|
+
fs.writeJSONSync(routeJSONPath, {
|
|
51
|
+
routes
|
|
52
|
+
}, {
|
|
53
|
+
spaces: 2
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
const replaceWithAlias = (base, filePath, alias)=>path_0.posix.join(alias, path_0.posix.relative(base, filePath));
|
|
57
|
+
const standardOptions = (ssgOptions, entrypoints, routes, server, ssgByEntries)=>{
|
|
58
|
+
if (ssgByEntries && Object.keys(ssgByEntries).length > 0) {
|
|
59
|
+
const result = {};
|
|
60
|
+
Object.keys(ssgByEntries).forEach((key)=>{
|
|
61
|
+
const val = ssgByEntries[key];
|
|
62
|
+
if ('function' != typeof val) result[key] = val;
|
|
63
|
+
});
|
|
64
|
+
for (const entry of entrypoints){
|
|
65
|
+
const { entryName } = entry;
|
|
66
|
+
const configured = ssgByEntries[entryName];
|
|
67
|
+
if ('function' == typeof configured) {
|
|
68
|
+
const routesForEntry = routes.filter((r)=>r.entryName === entryName);
|
|
69
|
+
if (Array.isArray(server?.baseUrl)) for (const url of server.baseUrl)routesForEntry.filter((r)=>'string' == typeof r.urlPath && r.urlPath.startsWith(url)).forEach((r)=>{
|
|
70
|
+
result[r.urlPath] = configured(entryName, {
|
|
71
|
+
baseUrl: url
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
else result[entryName] = configured(entryName, {
|
|
75
|
+
baseUrl: server?.baseUrl
|
|
76
|
+
});
|
|
77
|
+
} else if (void 0 !== configured) result[entryName] = configured;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
if (false === ssgOptions) return false;
|
|
82
|
+
if (true === ssgOptions) return entrypoints.reduce((opt, entry)=>{
|
|
83
|
+
opt[entry.entryName] = ssgOptions;
|
|
84
|
+
return opt;
|
|
85
|
+
}, {});
|
|
86
|
+
if ('object' == typeof ssgOptions) {
|
|
87
|
+
const isSingle = isSingleEntry(entrypoints);
|
|
88
|
+
if (isSingle) return {
|
|
89
|
+
main: ssgOptions
|
|
90
|
+
};
|
|
91
|
+
return entrypoints.reduce((opt, entry)=>{
|
|
92
|
+
opt[entry.entryName] = ssgOptions;
|
|
93
|
+
return opt;
|
|
94
|
+
}, {});
|
|
95
|
+
}
|
|
96
|
+
if ('function' == typeof ssgOptions) {
|
|
97
|
+
const intermediateOptions = {};
|
|
98
|
+
for (const entrypoint of entrypoints){
|
|
99
|
+
const { entryName } = entrypoint;
|
|
100
|
+
const routesForEntry = routes.filter((r)=>r.entryName === entryName);
|
|
101
|
+
if (Array.isArray(server?.baseUrl)) for (const url of server.baseUrl)routesForEntry.filter((r)=>'string' == typeof r.urlPath && r.urlPath.startsWith(url)).forEach((r)=>{
|
|
102
|
+
intermediateOptions[r.urlPath] = ssgOptions(entryName, {
|
|
103
|
+
baseUrl: url
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
else intermediateOptions[entryName] = ssgOptions(entryName, {
|
|
107
|
+
baseUrl: server?.baseUrl
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return intermediateOptions;
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
};
|
|
114
|
+
const openRouteSSR = (routes, entries = [])=>routes.map((ssgRoute)=>({
|
|
115
|
+
...ssgRoute,
|
|
116
|
+
isSSR: entries.includes(ssgRoute.entryName),
|
|
117
|
+
bundle: `${SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
|
|
118
|
+
}));
|
|
119
|
+
const flattenRoutes = (routes)=>{
|
|
120
|
+
const parents = [];
|
|
121
|
+
const newRoutes = [];
|
|
122
|
+
const traverseRoute = (route)=>{
|
|
123
|
+
const parent = parents[parents.length - 1];
|
|
124
|
+
let path = parent ? `${parent.path}/${route.path || ''}`.replace(/\/+/g, '/') : route.path || '';
|
|
125
|
+
path = path.replace(/\/$/, '');
|
|
126
|
+
if (route._component && ('/' !== path || '/' === path && !parent)) newRoutes.push({
|
|
127
|
+
...route,
|
|
128
|
+
path
|
|
129
|
+
});
|
|
130
|
+
if (route.children) {
|
|
131
|
+
parents.push({
|
|
132
|
+
...route,
|
|
133
|
+
path
|
|
134
|
+
});
|
|
135
|
+
route.children.forEach(traverseRoute);
|
|
136
|
+
parents.pop();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
routes.forEach(traverseRoute);
|
|
140
|
+
return newRoutes;
|
|
141
|
+
};
|
|
142
|
+
function chunkArray(arr, size) {
|
|
143
|
+
const result = [];
|
|
144
|
+
for(let i = 0; i < arr.length; i += size)result.push(arr.slice(i, i + size));
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
export { chunkArray, flattenRoutes, formatOutput, formatPath, getOutput, getUrlPrefix, isDynamicUrl, openRouteSSR, readJSONSpec, replaceWithAlias, standardOptions, writeJSONSpec };
|