@modern-js/plugin-ssg 2.69.5 → 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,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.mjs";
|
|
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 };
|
|
@@ -9,7 +9,7 @@ export declare function getOutput(route: SsgRoute, base: string, agreed?: boolea
|
|
|
9
9
|
export declare const readJSONSpec: (dir: string) => ModernRoute[];
|
|
10
10
|
export declare const writeJSONSpec: (dir: string, routes: ModernRoute[]) => void;
|
|
11
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) => false | SSGMultiEntryOptions;
|
|
12
|
+
export declare const standardOptions: (ssgOptions: SSGConfig, entrypoints: EntryPoint[], routes: ModernRoute[], server: ServerUserConfig, ssgByEntries?: SSGMultiEntryOptions) => false | SSGMultiEntryOptions;
|
|
13
13
|
export declare const openRouteSSR: (routes: ModernRoute[], entries?: string[]) => {
|
|
14
14
|
isSSR: boolean;
|
|
15
15
|
bundle: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AppNormalizedConfig,
|
|
1
|
+
import type { AppNormalizedConfig, AppToolsContext } from '@modern-js/app-tools';
|
|
2
2
|
import type { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
3
3
|
import type { SsgRoute } from '../types';
|
|
4
|
-
export declare const createServer: (
|
|
4
|
+
export declare const createServer: (appContext: AppToolsContext, ssgRoutes: SsgRoute[], pageRoutes: ModernRoute[], apiRoutes: ModernRoute[], options: AppNormalizedConfig) => Promise<string[]>;
|
package/package.json
CHANGED
|
@@ -15,11 +15,11 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "
|
|
18
|
+
"version": "3.0.0-alpha.0",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
22
|
-
"module": "./dist/esm/index.
|
|
22
|
+
"module": "./dist/esm/index.mjs",
|
|
23
23
|
"typesVersions": {
|
|
24
24
|
"*": {
|
|
25
25
|
".": [
|
|
@@ -33,41 +33,41 @@
|
|
|
33
33
|
"exports": {
|
|
34
34
|
".": {
|
|
35
35
|
"types": "./dist/types/index.d.ts",
|
|
36
|
+
"jsnext:source": "./src/index.ts",
|
|
36
37
|
"node": {
|
|
37
|
-
"
|
|
38
|
-
"import": "./dist/esm-node/index.js",
|
|
38
|
+
"import": "./dist/esm-node/index.mjs",
|
|
39
39
|
"require": "./dist/cjs/index.js"
|
|
40
40
|
},
|
|
41
|
-
"default": "./dist/esm/index.
|
|
41
|
+
"default": "./dist/esm/index.mjs"
|
|
42
42
|
},
|
|
43
43
|
"./cli": {
|
|
44
|
+
"types": "./dist/types/index.d.ts",
|
|
44
45
|
"jsnext:source": "./src/index.ts",
|
|
45
46
|
"node": {
|
|
46
|
-
"import": "./dist/esm-node/index.
|
|
47
|
+
"import": "./dist/esm-node/index.mjs",
|
|
47
48
|
"require": "./dist/cjs/index.js"
|
|
48
49
|
},
|
|
49
|
-
"default": "./dist/esm/index.
|
|
50
|
+
"default": "./dist/esm/index.mjs"
|
|
50
51
|
},
|
|
51
52
|
"./types": {
|
|
52
53
|
"types": "./dist/types/types.d.ts",
|
|
54
|
+
"jsnext:source": "./src/types.ts",
|
|
53
55
|
"node": {
|
|
54
|
-
"
|
|
55
|
-
"import": "./dist/esm-node/types.js",
|
|
56
|
+
"import": "./dist/esm-node/types.mjs",
|
|
56
57
|
"require": "./dist/cjs/types.js"
|
|
57
58
|
},
|
|
58
|
-
"default": "./dist/esm/types.
|
|
59
|
+
"default": "./dist/esm/types.mjs"
|
|
59
60
|
}
|
|
60
61
|
},
|
|
61
62
|
"dependencies": {
|
|
62
63
|
"@swc/helpers": "^0.5.17",
|
|
63
|
-
"node-mocks-http": "^1.
|
|
64
|
+
"node-mocks-http": "^1.17.2",
|
|
64
65
|
"normalize-path": "3.0.0",
|
|
65
|
-
"
|
|
66
|
-
"@modern-js/
|
|
67
|
-
"@modern-js/utils": "2.69.5"
|
|
66
|
+
"@modern-js/prod-server": "3.0.0-alpha.0",
|
|
67
|
+
"@modern-js/utils": "3.0.0-alpha.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"react-router-dom": ">=
|
|
70
|
+
"react-router-dom": ">=7.0.0"
|
|
71
71
|
},
|
|
72
72
|
"peerDependenciesMeta": {
|
|
73
73
|
"react-router-dom": {
|
|
@@ -75,17 +75,16 @@
|
|
|
75
75
|
}
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@
|
|
79
|
-
"@types/node": "^
|
|
80
|
-
"
|
|
81
|
-
"react": "^
|
|
82
|
-
"react-dom": "^
|
|
83
|
-
"react-router-dom": "6.27.0",
|
|
78
|
+
"@rslib/core": "0.18.5",
|
|
79
|
+
"@types/node": "^20",
|
|
80
|
+
"react": "^19.2.3",
|
|
81
|
+
"react-dom": "^19.2.3",
|
|
82
|
+
"react-router-dom": "^7.6.0",
|
|
84
83
|
"typescript": "^5",
|
|
85
|
-
"@modern-js/app-tools": "
|
|
86
|
-
"@modern-js/types": "
|
|
87
|
-
"@scripts/
|
|
88
|
-
"@
|
|
84
|
+
"@modern-js/app-tools": "3.0.0-alpha.0",
|
|
85
|
+
"@modern-js/types": "3.0.0-alpha.0",
|
|
86
|
+
"@scripts/rstest-config": "2.66.0",
|
|
87
|
+
"@modern-js/rslib": "2.68.10"
|
|
89
88
|
},
|
|
90
89
|
"sideEffects": false,
|
|
91
90
|
"publishConfig": {
|
|
@@ -93,9 +92,8 @@
|
|
|
93
92
|
"access": "public"
|
|
94
93
|
},
|
|
95
94
|
"scripts": {
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"test": "jest --passWithNoTests"
|
|
95
|
+
"build": "rslib build",
|
|
96
|
+
"dev": "rslib build --watch",
|
|
97
|
+
"test": "rstest --passWithNoTests"
|
|
100
98
|
}
|
|
101
99
|
}
|
package/rslib.config.mts
ADDED
package/rstest.config.ts
ADDED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
-
for (let key of __getOwnPropNames(from))
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
-
}
|
|
14
|
-
return to;
|
|
15
|
-
};
|
|
16
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
-
mod
|
|
23
|
-
));
|
|
24
|
-
var import_node_assert = __toESM(require("node:assert"));
|
|
25
|
-
var import_node_http = require("node:http");
|
|
26
|
-
var import_prod_server = require("@modern-js/prod-server");
|
|
27
|
-
var import_utils = require("@modern-js/utils");
|
|
28
|
-
var import_portfinder = __toESM(require("portfinder"));
|
|
29
|
-
var import_util = require("../libs/util");
|
|
30
|
-
var import_consts = require("./consts");
|
|
31
|
-
function getLogger() {
|
|
32
|
-
const logger = (0, import_utils.createLogger)({
|
|
33
|
-
level: "verbose"
|
|
34
|
-
});
|
|
35
|
-
return {
|
|
36
|
-
...logger,
|
|
37
|
-
error: (...args) => {
|
|
38
|
-
console.error(...args);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
const MAX_CONCURRENT_REQUESTS = 10;
|
|
43
|
-
process.on("message", async (chunk) => {
|
|
44
|
-
if (chunk === import_consts.CLOSE_SIGN) {
|
|
45
|
-
process.exit();
|
|
46
|
-
}
|
|
47
|
-
const context = JSON.parse(chunk);
|
|
48
|
-
const { routes, renderRoutes, options, appContext, plugins, distDirectory } = context;
|
|
49
|
-
let nodeServer = null;
|
|
50
|
-
try {
|
|
51
|
-
const { server: serverConfig } = options;
|
|
52
|
-
const defaultPort = Number(process.env.PORT) || serverConfig.port;
|
|
53
|
-
import_portfinder.default.basePort = defaultPort;
|
|
54
|
-
const port = await import_portfinder.default.getPortPromise();
|
|
55
|
-
const serverOptions = {
|
|
56
|
-
pwd: distDirectory,
|
|
57
|
-
config: options,
|
|
58
|
-
appContext,
|
|
59
|
-
// TODO
|
|
60
|
-
serverConfigPath: "",
|
|
61
|
-
routes,
|
|
62
|
-
plugins: await (0, import_prod_server.loadServerPlugins)(plugins, appContext.appDirectory || distDirectory),
|
|
63
|
-
staticGenerate: true,
|
|
64
|
-
logger: getLogger()
|
|
65
|
-
};
|
|
66
|
-
(0, import_node_assert.default)(process.send, "process.send is not available");
|
|
67
|
-
const sendProcessMessage = process.send.bind(process);
|
|
68
|
-
nodeServer = await (0, import_prod_server.createProdServer)(serverOptions);
|
|
69
|
-
nodeServer.listen(port, async () => {
|
|
70
|
-
if (!nodeServer)
|
|
71
|
-
return;
|
|
72
|
-
const chunkedRoutes = (0, import_util.chunkArray)(renderRoutes, MAX_CONCURRENT_REQUESTS);
|
|
73
|
-
for (const routes2 of chunkedRoutes) {
|
|
74
|
-
const promises = routes2.map(async (route) => getHtml(`http://localhost:${port}${route.urlPath}`, port));
|
|
75
|
-
for (const result of await Promise.all(promises)) {
|
|
76
|
-
sendProcessMessage(result);
|
|
77
|
-
sendProcessMessage(null);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
nodeServer.close();
|
|
81
|
-
});
|
|
82
|
-
} catch (e) {
|
|
83
|
-
nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.close();
|
|
84
|
-
process.stderr.write(e instanceof Error ? e.stack : e.toString());
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
function getHtml(url, port) {
|
|
88
|
-
const headers = {
|
|
89
|
-
host: `localhost:${port}`
|
|
90
|
-
};
|
|
91
|
-
return new Promise((resolve, reject) => {
|
|
92
|
-
(0, import_node_http.request)(url, {
|
|
93
|
-
headers
|
|
94
|
-
}, (res) => {
|
|
95
|
-
const chunks = [];
|
|
96
|
-
res.on("error", (error) => {
|
|
97
|
-
reject(error);
|
|
98
|
-
});
|
|
99
|
-
res.on("data", (chunk) => {
|
|
100
|
-
chunks.push(chunk);
|
|
101
|
-
});
|
|
102
|
-
res.on("end", () => {
|
|
103
|
-
const html = Buffer.concat(chunks).toString();
|
|
104
|
-
resolve(html);
|
|
105
|
-
});
|
|
106
|
-
}).end();
|
|
107
|
-
});
|
|
108
|
-
}
|
package/dist/esm/index.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
-
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
|
-
import path from "path";
|
|
6
|
-
import { filterRoutesForServer, logger } from "@modern-js/utils";
|
|
7
|
-
import { generatePath } from "react-router-dom";
|
|
8
|
-
import { makeRoute } from "./libs/make";
|
|
9
|
-
import { writeHtmlFile } from "./libs/output";
|
|
10
|
-
import { replaceRoute } from "./libs/replace";
|
|
11
|
-
import { flattenRoutes, formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util";
|
|
12
|
-
import { createServer } from "./server";
|
|
13
|
-
var ssgPlugin = function() {
|
|
14
|
-
return {
|
|
15
|
-
name: "@modern-js/plugin-ssg",
|
|
16
|
-
pre: [
|
|
17
|
-
"@modern-js/plugin-server",
|
|
18
|
-
"@modern-js/plugin-bff"
|
|
19
|
-
],
|
|
20
|
-
setup: function(api) {
|
|
21
|
-
var agreedRouteMap = {};
|
|
22
|
-
return {
|
|
23
|
-
modifyFileSystemRoutes: function modifyFileSystemRoutes(param) {
|
|
24
|
-
var entrypoint = param.entrypoint, routes = param.routes;
|
|
25
|
-
var entryName = entrypoint.entryName;
|
|
26
|
-
var flattedRoutes = flattenRoutes(filterRoutesForServer(routes));
|
|
27
|
-
agreedRouteMap[entryName] = flattedRoutes;
|
|
28
|
-
return {
|
|
29
|
-
entrypoint,
|
|
30
|
-
routes
|
|
31
|
-
};
|
|
32
|
-
},
|
|
33
|
-
afterBuild: function afterBuild() {
|
|
34
|
-
return _async_to_generator(function() {
|
|
35
|
-
var resolvedConfig, appContext, appDirectory, entrypoints, output, server, ssg, tmp, _ref, outputPath, ssgOptions, buildDir, routes, pageRoutes, apiRoutes, intermediateOptions, ssgRoutes, htmlAry;
|
|
36
|
-
return _ts_generator(this, function(_state) {
|
|
37
|
-
switch (_state.label) {
|
|
38
|
-
case 0:
|
|
39
|
-
resolvedConfig = api.useResolvedConfigContext();
|
|
40
|
-
appContext = api.useAppContext();
|
|
41
|
-
appDirectory = appContext.appDirectory, entrypoints = appContext.entrypoints;
|
|
42
|
-
output = resolvedConfig.output, server = resolvedConfig.server;
|
|
43
|
-
ssg = output.ssg, tmp = output.distPath, _ref = tmp === void 0 ? {} : tmp, outputPath = _ref.root;
|
|
44
|
-
ssgOptions = (Array.isArray(ssg) ? ssg.pop() : ssg) || true;
|
|
45
|
-
buildDir = path.join(appDirectory, outputPath);
|
|
46
|
-
routes = readJSONSpec(buildDir);
|
|
47
|
-
pageRoutes = routes.filter(function(route) {
|
|
48
|
-
return route.isSPA;
|
|
49
|
-
});
|
|
50
|
-
apiRoutes = routes.filter(function(route) {
|
|
51
|
-
return !route.isSPA;
|
|
52
|
-
});
|
|
53
|
-
if (pageRoutes.length === 0) {
|
|
54
|
-
return [
|
|
55
|
-
2
|
|
56
|
-
];
|
|
57
|
-
}
|
|
58
|
-
intermediateOptions = standardOptions(ssgOptions, entrypoints, pageRoutes, server);
|
|
59
|
-
if (!intermediateOptions) {
|
|
60
|
-
return [
|
|
61
|
-
2
|
|
62
|
-
];
|
|
63
|
-
}
|
|
64
|
-
ssgRoutes = [];
|
|
65
|
-
pageRoutes.forEach(function(pageRoute) {
|
|
66
|
-
var entryName = pageRoute.entryName, entryPath = pageRoute.entryPath;
|
|
67
|
-
var agreedRoutes = agreedRouteMap[entryName];
|
|
68
|
-
var entryOptions = intermediateOptions[entryName] || intermediateOptions[pageRoute.urlPath];
|
|
69
|
-
if (!agreedRoutes) {
|
|
70
|
-
if (!entryOptions) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
if (entryOptions === true) {
|
|
74
|
-
ssgRoutes.push(_object_spread_props(_object_spread({}, pageRoute), {
|
|
75
|
-
output: entryPath
|
|
76
|
-
}));
|
|
77
|
-
} else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
78
|
-
var enrtyRoutes = entryOptions.routes, headers = entryOptions.headers;
|
|
79
|
-
enrtyRoutes.forEach(function(route) {
|
|
80
|
-
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
if (!entryOptions) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (entryOptions === true) {
|
|
88
|
-
entryOptions = {
|
|
89
|
-
preventDefault: [],
|
|
90
|
-
routes: [],
|
|
91
|
-
headers: {}
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
var _entryOptions_preventDefault = entryOptions.preventDefault, preventDefault = _entryOptions_preventDefault === void 0 ? [] : _entryOptions_preventDefault, tmp2 = entryOptions.routes, userRoutes = tmp2 === void 0 ? [] : tmp2, headers1 = entryOptions.headers;
|
|
95
|
-
if (userRoutes.length > 0) {
|
|
96
|
-
userRoutes.forEach(function(route) {
|
|
97
|
-
if (typeof route === "string") {
|
|
98
|
-
ssgRoutes.push(makeRoute(pageRoute, route, headers1));
|
|
99
|
-
} else if (Array.isArray(route.params)) {
|
|
100
|
-
route.params.forEach(function(param) {
|
|
101
|
-
ssgRoutes.push(makeRoute(pageRoute, _object_spread_props(_object_spread({}, route), {
|
|
102
|
-
url: generatePath(route.url, param)
|
|
103
|
-
}), headers1));
|
|
104
|
-
});
|
|
105
|
-
} else {
|
|
106
|
-
ssgRoutes.push(makeRoute(pageRoute, route, headers1));
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
} else {
|
|
110
|
-
agreedRoutes.filter(function(route) {
|
|
111
|
-
return !preventDefault.includes(route.path);
|
|
112
|
-
}).forEach(function(route) {
|
|
113
|
-
if (!isDynamicUrl(route.path)) {
|
|
114
|
-
ssgRoutes.push(makeRoute(pageRoute, route.path, headers1));
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
if (ssgRoutes.length === 0) {
|
|
121
|
-
return [
|
|
122
|
-
2
|
|
123
|
-
];
|
|
124
|
-
}
|
|
125
|
-
ssgRoutes.forEach(function(ssgRoute) {
|
|
126
|
-
if (ssgRoute.isSSR) {
|
|
127
|
-
var isOriginRoute = pageRoutes.some(function(pageRoute) {
|
|
128
|
-
return pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName;
|
|
129
|
-
});
|
|
130
|
-
if (isOriginRoute) {
|
|
131
|
-
throw new Error("ssg can not using with ssr,url - ".concat(ssgRoute.urlPath, ", entry - ").concat(ssgRoute.entryName, " "));
|
|
132
|
-
}
|
|
133
|
-
logger.warn("new ssg route ".concat(ssgRoute.urlPath, " is using ssr now,maybe from parent route ").concat(ssgRoute.entryName, ",close ssr"));
|
|
134
|
-
}
|
|
135
|
-
ssgRoute.isSSR = false;
|
|
136
|
-
ssgRoute.output = formatOutput(ssgRoute.output);
|
|
137
|
-
});
|
|
138
|
-
return [
|
|
139
|
-
4,
|
|
140
|
-
createServer(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory)
|
|
141
|
-
];
|
|
142
|
-
case 1:
|
|
143
|
-
htmlAry = _state.sent();
|
|
144
|
-
writeHtmlFile(htmlAry, ssgRoutes, buildDir);
|
|
145
|
-
replaceRoute(ssgRoutes, pageRoutes);
|
|
146
|
-
writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
|
|
147
|
-
logger.info("ssg Compiled successfully");
|
|
148
|
-
return [
|
|
149
|
-
2
|
|
150
|
-
];
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
})();
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
var src_default = ssgPlugin;
|
|
160
|
-
export {
|
|
161
|
-
src_default as default,
|
|
162
|
-
ssgPlugin
|
|
163
|
-
};
|
package/dist/esm/libs/make.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
-
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import normalize from "normalize-path";
|
|
5
|
-
function makeRender(ssgRoutes, render, port) {
|
|
6
|
-
return ssgRoutes.map(function(ssgRoute) {
|
|
7
|
-
return render({
|
|
8
|
-
url: ssgRoute.urlPath,
|
|
9
|
-
headers: _object_spread({
|
|
10
|
-
host: "localhost:".concat(port)
|
|
11
|
-
}, ssgRoute.headers),
|
|
12
|
-
connection: {}
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
function makeRoute(baseRoute, route) {
|
|
17
|
-
var headers = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
18
|
-
var urlPath = baseRoute.urlPath, entryPath = baseRoute.entryPath;
|
|
19
|
-
if (typeof route === "string") {
|
|
20
|
-
return _object_spread_props(_object_spread({}, baseRoute), {
|
|
21
|
-
urlPath: normalize("".concat(urlPath).concat(route)) || "/",
|
|
22
|
-
headers,
|
|
23
|
-
output: path.join(entryPath, "..".concat(route === "/" ? "" : route))
|
|
24
|
-
});
|
|
25
|
-
} else {
|
|
26
|
-
return _object_spread_props(_object_spread({}, baseRoute), {
|
|
27
|
-
urlPath: normalize("".concat(urlPath).concat(route.url)) || "/",
|
|
28
|
-
headers: _object_spread({}, headers, route.headers),
|
|
29
|
-
output: route.output ? path.normalize(route.output) : path.join(entryPath, "..".concat(route.url === "/" ? "" : route.url))
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export {
|
|
34
|
-
makeRender,
|
|
35
|
-
makeRoute
|
|
36
|
-
};
|
package/dist/esm/libs/output.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
function writeHtmlFile(htmlAry, ssgRoutes, baseDir) {
|
|
4
|
-
htmlAry.forEach(function(html, index) {
|
|
5
|
-
var ssgRoute = ssgRoutes[index];
|
|
6
|
-
var filepath = path.join(baseDir, ssgRoute.output);
|
|
7
|
-
if (!fs.existsSync(path.dirname(filepath))) {
|
|
8
|
-
fs.ensureDirSync(path.dirname(filepath));
|
|
9
|
-
}
|
|
10
|
-
fs.writeFileSync(filepath, html);
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
export {
|
|
14
|
-
writeHtmlFile
|
|
15
|
-
};
|
package/dist/esm/libs/replace.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
-
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
|
|
3
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
|
-
import normalize from "normalize-path";
|
|
5
|
-
function exist(route, pageRoutes) {
|
|
6
|
-
return pageRoutes.slice().findIndex(function(pageRoute) {
|
|
7
|
-
var urlEqual = normalize(pageRoute.urlPath) === normalize(route.urlPath);
|
|
8
|
-
var entryEqual = pageRoute.entryName === route.entryName;
|
|
9
|
-
if (urlEqual && entryEqual) {
|
|
10
|
-
return true;
|
|
11
|
-
}
|
|
12
|
-
return false;
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
function replaceRoute(ssgRoutes, pageRoutes) {
|
|
16
|
-
var _pageRoutes;
|
|
17
|
-
var cleanSsgRoutes = ssgRoutes.map(function(ssgRoute) {
|
|
18
|
-
var output = ssgRoute.output, headers = ssgRoute.headers, cleanSsgRoute = _object_without_properties(ssgRoute, [
|
|
19
|
-
"output",
|
|
20
|
-
"headers"
|
|
21
|
-
]);
|
|
22
|
-
return Object.assign(cleanSsgRoute, output ? {
|
|
23
|
-
entryPath: output
|
|
24
|
-
} : {});
|
|
25
|
-
});
|
|
26
|
-
var freshRoutes = [];
|
|
27
|
-
cleanSsgRoutes.forEach(function(ssgRoute) {
|
|
28
|
-
var index = exist(ssgRoute, pageRoutes);
|
|
29
|
-
if (index < 0) {
|
|
30
|
-
freshRoutes.push(_object_spread({}, ssgRoute));
|
|
31
|
-
} else {
|
|
32
|
-
pageRoutes[index].entryPath = ssgRoute.entryPath;
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
(_pageRoutes = pageRoutes).push.apply(_pageRoutes, _to_consumable_array(freshRoutes));
|
|
36
|
-
return pageRoutes;
|
|
37
|
-
}
|
|
38
|
-
export {
|
|
39
|
-
exist,
|
|
40
|
-
replaceRoute
|
|
41
|
-
};
|