@modern-js/plugin-ssg 0.0.0-next-45d726979 → 0.0.0-next-0bf5ec13d2
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/CHANGELOG.md +2 -2
- package/dist/js/modern/index.js +189 -0
- package/dist/js/modern/libs/make.js +50 -0
- package/dist/js/modern/libs/output.js +15 -0
- package/dist/js/modern/libs/replace.js +63 -0
- package/dist/js/modern/libs/util.js +171 -0
- package/dist/js/modern/server/consts.js +4 -0
- package/dist/js/modern/server/index.js +63 -0
- package/dist/js/modern/server/prerender.js +53 -0
- package/dist/js/modern/server/process.js +84 -0
- package/dist/js/modern/types.js +0 -0
- package/dist/js/node/index.js +207 -0
- package/dist/js/node/libs/make.js +78 -0
- package/dist/js/node/libs/output.js +44 -0
- package/dist/js/node/libs/replace.js +91 -0
- package/dist/js/node/libs/util.js +203 -0
- package/dist/js/node/server/consts.js +27 -0
- package/dist/js/node/server/index.js +92 -0
- package/dist/js/node/server/prerender.js +80 -0
- package/dist/js/node/server/process.js +93 -0
- package/dist/js/node/types.js +15 -0
- package/dist/js/treeshaking/index.js +334 -0
- package/dist/js/treeshaking/libs/make.js +83 -0
- package/dist/js/treeshaking/libs/output.js +13 -0
- package/dist/js/treeshaking/libs/replace.js +115 -0
- package/dist/js/treeshaking/libs/util.js +239 -0
- package/dist/js/treeshaking/server/consts.js +2 -0
- package/dist/js/treeshaking/server/index.js +63 -0
- package/dist/js/treeshaking/server/prerender.js +97 -0
- package/dist/js/treeshaking/server/process.js +226 -0
- package/dist/js/treeshaking/types.js +1 -0
- package/package.json +9 -9
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
import path from "path";
|
|
41
|
+
import { logger, PLUGIN_SCHEMAS } from "@modern-js/utils";
|
|
42
|
+
import { generatePath } from "react-router-dom";
|
|
43
|
+
import {
|
|
44
|
+
flattenRoutes,
|
|
45
|
+
formatOutput,
|
|
46
|
+
isDynamicUrl,
|
|
47
|
+
readJSONSpec,
|
|
48
|
+
standardOptions,
|
|
49
|
+
writeJSONSpec
|
|
50
|
+
} from "./libs/util";
|
|
51
|
+
import { createServer } from "./server";
|
|
52
|
+
import { writeHtmlFile } from "./libs/output";
|
|
53
|
+
import { replaceRoute } from "./libs/replace";
|
|
54
|
+
import { makeRoute } from "./libs/make";
|
|
55
|
+
var src_default = () => ({
|
|
56
|
+
name: "@modern-js/plugin-ssg",
|
|
57
|
+
pre: ["@modern-js/plugin-server", "@modern-js/plugin-bff"],
|
|
58
|
+
setup: (api) => {
|
|
59
|
+
const agreedRouteMap = {};
|
|
60
|
+
return {
|
|
61
|
+
validateSchema() {
|
|
62
|
+
return PLUGIN_SCHEMAS["@modern-js/plugin-ssg"];
|
|
63
|
+
},
|
|
64
|
+
modifyFileSystemRoutes({ entrypoint, routes }) {
|
|
65
|
+
const { entryName } = entrypoint;
|
|
66
|
+
const flattedRoutes = flattenRoutes(routes);
|
|
67
|
+
agreedRouteMap[entryName] = flattedRoutes;
|
|
68
|
+
return { entrypoint, routes };
|
|
69
|
+
},
|
|
70
|
+
afterBuild() {
|
|
71
|
+
return __async(this, null, function* () {
|
|
72
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
73
|
+
const appContext = api.useAppContext();
|
|
74
|
+
const { appDirectory, entrypoints } = appContext;
|
|
75
|
+
const { output, server } = resolvedConfig;
|
|
76
|
+
const { ssg, distPath: { root: outputPath } = {} } = output;
|
|
77
|
+
const ssgOptions = (Array.isArray(ssg) ? ssg.pop() : ssg) || true;
|
|
78
|
+
const buildDir = path.join(appDirectory, outputPath);
|
|
79
|
+
const routes = readJSONSpec(buildDir);
|
|
80
|
+
const pageRoutes = routes.filter((route) => !route.isApi);
|
|
81
|
+
const apiRoutes = routes.filter((route) => route.isApi);
|
|
82
|
+
if (pageRoutes.length === 0) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const intermediateOptions = standardOptions(
|
|
86
|
+
ssgOptions,
|
|
87
|
+
entrypoints,
|
|
88
|
+
pageRoutes,
|
|
89
|
+
server
|
|
90
|
+
);
|
|
91
|
+
if (!intermediateOptions) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const ssgRoutes = [];
|
|
95
|
+
pageRoutes.forEach((pageRoute) => {
|
|
96
|
+
const { entryName, entryPath } = pageRoute;
|
|
97
|
+
const agreedRoutes = agreedRouteMap[entryName];
|
|
98
|
+
let entryOptions = intermediateOptions[entryName] || intermediateOptions[pageRoute.urlPath];
|
|
99
|
+
if (!agreedRoutes) {
|
|
100
|
+
if (!entryOptions) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (entryOptions === true) {
|
|
104
|
+
ssgRoutes.push(__spreadProps(__spreadValues({}, pageRoute), { output: entryPath }));
|
|
105
|
+
} else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
106
|
+
const { routes: enrtyRoutes, headers } = entryOptions;
|
|
107
|
+
enrtyRoutes.forEach((route) => {
|
|
108
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
if (!entryOptions) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (entryOptions === true) {
|
|
116
|
+
entryOptions = { preventDefault: [], routes: [], headers: {} };
|
|
117
|
+
}
|
|
118
|
+
const {
|
|
119
|
+
preventDefault = [],
|
|
120
|
+
routes: userRoutes = [],
|
|
121
|
+
headers
|
|
122
|
+
} = entryOptions;
|
|
123
|
+
if (userRoutes.length > 0) {
|
|
124
|
+
userRoutes.forEach((route) => {
|
|
125
|
+
if (typeof route === "string") {
|
|
126
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
127
|
+
} else if (Array.isArray(route.params)) {
|
|
128
|
+
route.params.forEach((param) => {
|
|
129
|
+
ssgRoutes.push(
|
|
130
|
+
makeRoute(
|
|
131
|
+
pageRoute,
|
|
132
|
+
__spreadProps(__spreadValues({}, route), { url: generatePath(route.url, param) }),
|
|
133
|
+
headers
|
|
134
|
+
)
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
} else {
|
|
138
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
} else {
|
|
142
|
+
agreedRoutes.filter((route) => !preventDefault.includes(route.path)).forEach((route) => {
|
|
143
|
+
if (!isDynamicUrl(route.path)) {
|
|
144
|
+
ssgRoutes.push(makeRoute(pageRoute, route.path, headers));
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
if (ssgRoutes.length === 0) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
ssgRoutes.forEach((ssgRoute) => {
|
|
154
|
+
if (ssgRoute.isSSR) {
|
|
155
|
+
const isOriginRoute = pageRoutes.some(
|
|
156
|
+
(pageRoute) => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName
|
|
157
|
+
);
|
|
158
|
+
if (isOriginRoute) {
|
|
159
|
+
throw new Error(
|
|
160
|
+
`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
logger.warn(
|
|
164
|
+
`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
ssgRoute.isSSR = false;
|
|
168
|
+
ssgRoute.output = formatOutput(ssgRoute.output);
|
|
169
|
+
});
|
|
170
|
+
const htmlAry = yield createServer(
|
|
171
|
+
api,
|
|
172
|
+
ssgRoutes,
|
|
173
|
+
pageRoutes,
|
|
174
|
+
apiRoutes,
|
|
175
|
+
resolvedConfig,
|
|
176
|
+
appDirectory
|
|
177
|
+
);
|
|
178
|
+
writeHtmlFile(htmlAry, ssgRoutes, buildDir);
|
|
179
|
+
replaceRoute(ssgRoutes, pageRoutes);
|
|
180
|
+
writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
|
|
181
|
+
logger.info("ssg Compiled successfully");
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
export {
|
|
188
|
+
src_default as default
|
|
189
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import path from "path";
|
|
21
|
+
import normalize from "normalize-path";
|
|
22
|
+
function makeRender(ssgRoutes, render, port) {
|
|
23
|
+
return ssgRoutes.map(
|
|
24
|
+
(ssgRoute) => render({
|
|
25
|
+
url: ssgRoute.urlPath,
|
|
26
|
+
headers: __spreadValues({ host: `localhost:${port}` }, ssgRoute.headers),
|
|
27
|
+
connection: {}
|
|
28
|
+
})
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
function makeRoute(baseRoute, route, headers = {}) {
|
|
32
|
+
const { urlPath, entryPath } = baseRoute;
|
|
33
|
+
if (typeof route === "string") {
|
|
34
|
+
return __spreadProps(__spreadValues({}, baseRoute), {
|
|
35
|
+
urlPath: normalize(`${urlPath}${route}`) || "/",
|
|
36
|
+
headers,
|
|
37
|
+
output: path.join(entryPath, `..${route === "/" ? "" : route}`)
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
return __spreadProps(__spreadValues({}, baseRoute), {
|
|
41
|
+
urlPath: normalize(`${urlPath}${route.url}`) || "/",
|
|
42
|
+
headers: __spreadValues(__spreadValues({}, headers), route.headers),
|
|
43
|
+
output: route.output ? path.normalize(route.output) : path.join(entryPath, `..${route.url === "/" ? "" : route.url}`)
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
makeRender,
|
|
49
|
+
makeRoute
|
|
50
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
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))) {
|
|
8
|
+
fs.ensureDirSync(path.dirname(filepath));
|
|
9
|
+
}
|
|
10
|
+
fs.writeFileSync(filepath, html);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
writeHtmlFile
|
|
15
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __objRest = (source, exclude) => {
|
|
18
|
+
var target = {};
|
|
19
|
+
for (var prop in source)
|
|
20
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
21
|
+
target[prop] = source[prop];
|
|
22
|
+
if (source != null && __getOwnPropSymbols)
|
|
23
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
24
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
25
|
+
target[prop] = source[prop];
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
};
|
|
29
|
+
import normalize from "normalize-path";
|
|
30
|
+
function exist(route, pageRoutes) {
|
|
31
|
+
return pageRoutes.slice().findIndex((pageRoute) => {
|
|
32
|
+
const urlEqual = normalize(pageRoute.urlPath) === normalize(route.urlPath);
|
|
33
|
+
const entryEqual = pageRoute.entryName === route.entryName;
|
|
34
|
+
if (urlEqual && entryEqual) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function replaceRoute(ssgRoutes, pageRoutes) {
|
|
41
|
+
const cleanSsgRoutes = ssgRoutes.map((ssgRoute) => {
|
|
42
|
+
const _a = ssgRoute, { output, headers } = _a, cleanSsgRoute = __objRest(_a, ["output", "headers"]);
|
|
43
|
+
return Object.assign(
|
|
44
|
+
cleanSsgRoute,
|
|
45
|
+
output ? { entryPath: output } : {}
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
const freshRoutes = [];
|
|
49
|
+
cleanSsgRoutes.forEach((ssgRoute) => {
|
|
50
|
+
const index = exist(ssgRoute, pageRoutes);
|
|
51
|
+
if (index < 0) {
|
|
52
|
+
freshRoutes.push(__spreadValues({}, ssgRoute));
|
|
53
|
+
} else {
|
|
54
|
+
pageRoutes[index].entryPath = ssgRoute.entryPath;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
pageRoutes.push(...freshRoutes);
|
|
58
|
+
return pageRoutes;
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
exist,
|
|
62
|
+
replaceRoute
|
|
63
|
+
};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import path from "path";
|
|
21
|
+
import {
|
|
22
|
+
ROUTE_SPEC_FILE,
|
|
23
|
+
fs,
|
|
24
|
+
isSingleEntry,
|
|
25
|
+
SERVER_BUNDLE_DIRECTORY
|
|
26
|
+
} from "@modern-js/utils";
|
|
27
|
+
function formatOutput(filename) {
|
|
28
|
+
const outputPath = path.extname(filename) ? filename : `${filename}/index.html`;
|
|
29
|
+
return outputPath;
|
|
30
|
+
}
|
|
31
|
+
function formatPath(str) {
|
|
32
|
+
let addr = str;
|
|
33
|
+
if (!addr || typeof addr !== "string") {
|
|
34
|
+
return addr;
|
|
35
|
+
}
|
|
36
|
+
if (addr.startsWith(".")) {
|
|
37
|
+
addr = addr.slice(1);
|
|
38
|
+
}
|
|
39
|
+
if (!addr.startsWith("/")) {
|
|
40
|
+
addr = `/${addr}`;
|
|
41
|
+
}
|
|
42
|
+
if (addr.endsWith("/") && addr !== "/") {
|
|
43
|
+
addr = addr.slice(0, addr.length - 1);
|
|
44
|
+
}
|
|
45
|
+
return addr;
|
|
46
|
+
}
|
|
47
|
+
function isDynamicUrl(url) {
|
|
48
|
+
return url.includes(":");
|
|
49
|
+
}
|
|
50
|
+
function getUrlPrefix(route, baseUrl) {
|
|
51
|
+
let base = "";
|
|
52
|
+
if (Array.isArray(baseUrl)) {
|
|
53
|
+
const filters = baseUrl.filter((url) => route.urlPath.includes(url));
|
|
54
|
+
if (filters.length > 1) {
|
|
55
|
+
const matched = filters.sort((a, b) => a.length - b.length)[0];
|
|
56
|
+
if (!matched) {
|
|
57
|
+
throw new Error("");
|
|
58
|
+
}
|
|
59
|
+
base = matched;
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
base = baseUrl;
|
|
63
|
+
}
|
|
64
|
+
base = base === "/" ? "" : base;
|
|
65
|
+
const entryName = route.entryName === "main" ? "" : route.entryName;
|
|
66
|
+
const prefix = `${base}/${entryName}`;
|
|
67
|
+
return prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
68
|
+
}
|
|
69
|
+
function getOutput(route, base, agreed) {
|
|
70
|
+
const { output } = route;
|
|
71
|
+
if (output) {
|
|
72
|
+
return output;
|
|
73
|
+
}
|
|
74
|
+
if (agreed) {
|
|
75
|
+
const urlWithoutBase = route.urlPath.replace(base, "");
|
|
76
|
+
return urlWithoutBase.startsWith("/") ? urlWithoutBase.slice(1) : urlWithoutBase;
|
|
77
|
+
}
|
|
78
|
+
throw new Error(
|
|
79
|
+
`routing must provide output when calling createPage(), check ${route.urlPath}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
const readJSONSpec = (dir) => {
|
|
83
|
+
const routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
|
|
84
|
+
const routeJSON = require(routeJSONPath);
|
|
85
|
+
const { routes } = routeJSON;
|
|
86
|
+
return routes;
|
|
87
|
+
};
|
|
88
|
+
const writeJSONSpec = (dir, routes) => {
|
|
89
|
+
const routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
|
|
90
|
+
fs.writeJSONSync(routeJSONPath, { routes }, { spaces: 2 });
|
|
91
|
+
};
|
|
92
|
+
const replaceWithAlias = (base, filePath, alias) => path.posix.join(alias, path.posix.relative(base, filePath));
|
|
93
|
+
const standardOptions = (ssgOptions, entrypoints, routes, server) => {
|
|
94
|
+
if (ssgOptions === false) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
if (ssgOptions === true) {
|
|
98
|
+
return entrypoints.reduce((opt, entry) => {
|
|
99
|
+
opt[entry.entryName] = ssgOptions;
|
|
100
|
+
return opt;
|
|
101
|
+
}, {});
|
|
102
|
+
} else if (typeof ssgOptions === "object") {
|
|
103
|
+
const isSingle = isSingleEntry(entrypoints);
|
|
104
|
+
if (isSingle && typeof ssgOptions.main === "undefined") {
|
|
105
|
+
return { main: ssgOptions };
|
|
106
|
+
} else {
|
|
107
|
+
return ssgOptions;
|
|
108
|
+
}
|
|
109
|
+
} else if (typeof ssgOptions === "function") {
|
|
110
|
+
const intermediateOptions = {};
|
|
111
|
+
for (const entrypoint of entrypoints) {
|
|
112
|
+
const { entryName } = entrypoint;
|
|
113
|
+
if (Array.isArray(server == null ? void 0 : server.baseUrl)) {
|
|
114
|
+
for (const url of server.baseUrl) {
|
|
115
|
+
const matchUrl = entryName === "main" ? url : `${url}/${entryName}`;
|
|
116
|
+
const route = routes.find((route2) => route2.urlPath === matchUrl);
|
|
117
|
+
intermediateOptions[route == null ? void 0 : route.urlPath] = ssgOptions(
|
|
118
|
+
entryName,
|
|
119
|
+
{ baseUrl: url }
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
intermediateOptions[entryName] = ssgOptions(entryName, {
|
|
124
|
+
baseUrl: server == null ? void 0 : server.baseUrl
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return intermediateOptions;
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
};
|
|
132
|
+
const openRouteSSR = (routes, entries = []) => routes.map((ssgRoute) => __spreadProps(__spreadValues({}, ssgRoute), {
|
|
133
|
+
isSSR: entries.includes(ssgRoute.entryName),
|
|
134
|
+
bundle: `${SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
|
|
135
|
+
}));
|
|
136
|
+
const flattenRoutes = (routes) => {
|
|
137
|
+
const parents = [];
|
|
138
|
+
const newRoutes = [];
|
|
139
|
+
const traverseRoute = (route) => {
|
|
140
|
+
const parent = parents[parents.length - 1];
|
|
141
|
+
let path2 = parent ? `${parent.path}/${route.path || ""}`.replace(/\/+/g, "/") : route.path || "";
|
|
142
|
+
path2 = path2.replace(/\/$/, "");
|
|
143
|
+
if (route._component && (path2 !== "/" || path2 === "/" && !parent)) {
|
|
144
|
+
newRoutes.push(__spreadProps(__spreadValues({}, route), {
|
|
145
|
+
path: path2
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
if (route.children) {
|
|
149
|
+
parents.push(__spreadProps(__spreadValues({}, route), {
|
|
150
|
+
path: path2
|
|
151
|
+
}));
|
|
152
|
+
route.children.forEach(traverseRoute);
|
|
153
|
+
parents.pop();
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
routes.forEach(traverseRoute);
|
|
157
|
+
return newRoutes;
|
|
158
|
+
};
|
|
159
|
+
export {
|
|
160
|
+
flattenRoutes,
|
|
161
|
+
formatOutput,
|
|
162
|
+
formatPath,
|
|
163
|
+
getOutput,
|
|
164
|
+
getUrlPrefix,
|
|
165
|
+
isDynamicUrl,
|
|
166
|
+
openRouteSSR,
|
|
167
|
+
readJSONSpec,
|
|
168
|
+
replaceWithAlias,
|
|
169
|
+
standardOptions,
|
|
170
|
+
writeJSONSpec
|
|
171
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import childProcess from "child_process";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { logger } from "@modern-js/utils";
|
|
4
|
+
import { openRouteSSR } from "../libs/util";
|
|
5
|
+
import { CLOSE_SIGN } from "./consts";
|
|
6
|
+
const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) => new Promise((resolve, reject) => {
|
|
7
|
+
const entries = ssgRoutes.map((route) => route.entryName);
|
|
8
|
+
const backup = openRouteSSR(pageRoutes, entries);
|
|
9
|
+
const total = backup.concat(apiRoutes);
|
|
10
|
+
const cp = childProcess.fork(path.join(__dirname, "process"), {
|
|
11
|
+
cwd: appDirectory,
|
|
12
|
+
silent: true
|
|
13
|
+
});
|
|
14
|
+
const appContext = api.useAppContext();
|
|
15
|
+
const plugins = appContext.serverInternalPlugins;
|
|
16
|
+
cp.send(
|
|
17
|
+
JSON.stringify({
|
|
18
|
+
options,
|
|
19
|
+
renderRoutes: ssgRoutes,
|
|
20
|
+
routes: total,
|
|
21
|
+
appDirectory,
|
|
22
|
+
plugins
|
|
23
|
+
})
|
|
24
|
+
);
|
|
25
|
+
const htmlChunks = [];
|
|
26
|
+
const htmlAry = [];
|
|
27
|
+
cp.on("message", (chunk) => {
|
|
28
|
+
if (chunk !== null) {
|
|
29
|
+
htmlChunks.push(chunk);
|
|
30
|
+
} else {
|
|
31
|
+
const html = htmlChunks.join("");
|
|
32
|
+
htmlAry.push(html);
|
|
33
|
+
htmlChunks.length = 0;
|
|
34
|
+
}
|
|
35
|
+
if (htmlAry.length === ssgRoutes.length) {
|
|
36
|
+
cp.send(CLOSE_SIGN);
|
|
37
|
+
resolve(htmlAry);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
cp.stderr.on("data", (chunk) => {
|
|
41
|
+
const str = chunk.toString();
|
|
42
|
+
if (str.includes("Error")) {
|
|
43
|
+
logger.error(str);
|
|
44
|
+
reject(new Error("ssg render failed"));
|
|
45
|
+
cp.kill("SIGKILL");
|
|
46
|
+
} else {
|
|
47
|
+
logger.info(str.replace(/[^\S\n]+/g, " "));
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
cp.stdout.on("data", (chunk) => {
|
|
51
|
+
const str = chunk.toString();
|
|
52
|
+
if (str.includes("Error")) {
|
|
53
|
+
logger.error(str);
|
|
54
|
+
reject(new Error("ssg render failed"));
|
|
55
|
+
cp.kill("SIGKILL");
|
|
56
|
+
} else {
|
|
57
|
+
logger.info(str.replace(/[^\S\n]+/g, " "));
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
export {
|
|
62
|
+
createServer
|
|
63
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import EventEmitter from "events";
|
|
21
|
+
import { Readable } from "stream";
|
|
22
|
+
import httpMocks from "node-mocks-http";
|
|
23
|
+
const compile = (requestHandler) => (options, extend = {}) => new Promise((resolve, reject) => {
|
|
24
|
+
const req = httpMocks.createRequest(__spreadProps(__spreadValues({}, options), {
|
|
25
|
+
eventEmitter: Readable
|
|
26
|
+
}));
|
|
27
|
+
const res = httpMocks.createResponse({ eventEmitter: EventEmitter });
|
|
28
|
+
Object.assign(req, extend);
|
|
29
|
+
const proxyRes = new Proxy(res, {
|
|
30
|
+
get(obj, prop) {
|
|
31
|
+
if (typeof prop === "symbol" && !obj[prop]) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return obj[prop];
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
res.on("finish", () => {
|
|
38
|
+
if (res.statusCode !== 200) {
|
|
39
|
+
reject(new Error(res.statusMessage));
|
|
40
|
+
} else {
|
|
41
|
+
resolve(res._getData());
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
res.on("error", (e) => reject(e));
|
|
45
|
+
try {
|
|
46
|
+
requestHandler(req, proxyRes);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
reject(e);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
export {
|
|
52
|
+
compile
|
|
53
|
+
};
|