@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.
Files changed (52) hide show
  1. package/dist/cjs/index.js +130 -156
  2. package/dist/cjs/libs/make.js +69 -62
  3. package/dist/cjs/libs/output.js +50 -44
  4. package/dist/cjs/libs/replace.js +68 -64
  5. package/dist/cjs/libs/util.js +200 -182
  6. package/dist/cjs/server/consts.js +33 -25
  7. package/dist/cjs/server/index.js +137 -92
  8. package/dist/cjs/server/prerender.js +72 -68
  9. package/dist/cjs/types.js +17 -15
  10. package/dist/esm/index.mjs +89 -0
  11. package/dist/esm/libs/make.mjs +31 -0
  12. package/dist/esm/libs/output.mjs +11 -0
  13. package/dist/esm/libs/replace.mjs +28 -0
  14. package/dist/esm/libs/util.mjs +147 -0
  15. package/dist/esm/server/consts.mjs +2 -0
  16. package/dist/esm/server/index.mjs +97 -0
  17. package/dist/esm/server/prerender.mjs +30 -0
  18. package/dist/esm-node/index.mjs +89 -0
  19. package/dist/esm-node/libs/make.mjs +31 -0
  20. package/dist/esm-node/libs/output.mjs +11 -0
  21. package/dist/esm-node/libs/replace.mjs +28 -0
  22. package/dist/esm-node/libs/util.mjs +147 -0
  23. package/dist/esm-node/server/consts.mjs +2 -0
  24. package/dist/esm-node/server/index.mjs +97 -0
  25. package/dist/esm-node/server/prerender.mjs +30 -0
  26. package/dist/types/libs/util.d.ts +1 -1
  27. package/dist/types/server/index.d.ts +2 -2
  28. package/package.json +27 -29
  29. package/rslib.config.mts +4 -0
  30. package/rstest.config.ts +7 -0
  31. package/dist/cjs/server/process.js +0 -108
  32. package/dist/esm/index.js +0 -163
  33. package/dist/esm/libs/make.js +0 -36
  34. package/dist/esm/libs/output.js +0 -15
  35. package/dist/esm/libs/replace.js +0 -41
  36. package/dist/esm/libs/util.js +0 -210
  37. package/dist/esm/server/consts.js +0 -4
  38. package/dist/esm/server/index.js +0 -66
  39. package/dist/esm/server/prerender.js +0 -46
  40. package/dist/esm/server/process.js +0 -263
  41. package/dist/esm-node/index.js +0 -128
  42. package/dist/esm-node/libs/make.js +0 -37
  43. package/dist/esm-node/libs/output.js +0 -15
  44. package/dist/esm-node/libs/replace.js +0 -36
  45. package/dist/esm-node/libs/util.js +0 -161
  46. package/dist/esm-node/server/consts.js +0 -4
  47. package/dist/esm-node/server/index.js +0 -62
  48. package/dist/esm-node/server/prerender.js +0 -37
  49. package/dist/esm-node/server/process.js +0 -85
  50. package/dist/types/server/process.d.ts +0 -1
  51. /package/dist/esm/{types.js → types.mjs} +0 -0
  52. /package/dist/esm-node/{types.js → types.mjs} +0 -0
@@ -1,210 +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 { _ as _type_of } from "@swc/helpers/_/_type_of";
4
- import path from "path";
5
- import { fs, ROUTE_SPEC_FILE, SERVER_BUNDLE_DIRECTORY, isSingleEntry } from "@modern-js/utils";
6
- function formatOutput(filename) {
7
- var outputPath = path.extname(filename) ? filename : "".concat(filename, "/index.html");
8
- return outputPath;
9
- }
10
- function formatPath(str) {
11
- var addr = str;
12
- if (!addr || typeof addr !== "string") {
13
- return addr;
14
- }
15
- if (addr.startsWith(".")) {
16
- addr = addr.slice(1);
17
- }
18
- if (!addr.startsWith("/")) {
19
- addr = "/".concat(addr);
20
- }
21
- if (addr.endsWith("/") && addr !== "/") {
22
- addr = addr.slice(0, addr.length - 1);
23
- }
24
- return addr;
25
- }
26
- function isDynamicUrl(url) {
27
- return url.includes(":");
28
- }
29
- function getUrlPrefix(route, baseUrl) {
30
- var base = "";
31
- if (Array.isArray(baseUrl)) {
32
- var filters = baseUrl.filter(function(url) {
33
- return route.urlPath.includes(url);
34
- });
35
- if (filters.length > 1) {
36
- var matched = filters.sort(function(a, b) {
37
- return a.length - b.length;
38
- })[0];
39
- if (!matched) {
40
- throw new Error("");
41
- }
42
- base = matched;
43
- }
44
- } else {
45
- base = baseUrl;
46
- }
47
- base = base === "/" ? "" : base;
48
- var entryName = route.entryName === "main" ? "" : route.entryName;
49
- var prefix = "".concat(base, "/").concat(entryName);
50
- return prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
51
- }
52
- function getOutput(route, base, agreed) {
53
- var output = route.output;
54
- if (output) {
55
- return output;
56
- }
57
- if (agreed) {
58
- var urlWithoutBase = route.urlPath.replace(base, "");
59
- return urlWithoutBase.startsWith("/") ? urlWithoutBase.slice(1) : urlWithoutBase;
60
- }
61
- throw new Error("routing must provide output when calling createPage(), check ".concat(route.urlPath));
62
- }
63
- var readJSONSpec = function(dir) {
64
- var routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
65
- var routeJSON = require(routeJSONPath);
66
- var routes = routeJSON.routes;
67
- return routes;
68
- };
69
- var writeJSONSpec = function(dir, routes) {
70
- var routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
71
- fs.writeJSONSync(routeJSONPath, {
72
- routes
73
- }, {
74
- spaces: 2
75
- });
76
- };
77
- var replaceWithAlias = function(base, filePath, alias) {
78
- return path.posix.join(alias, path.posix.relative(base, filePath));
79
- };
80
- var standardOptions = function(ssgOptions, entrypoints, routes, server) {
81
- if (ssgOptions === false) {
82
- return false;
83
- }
84
- if (ssgOptions === true) {
85
- return entrypoints.reduce(function(opt, entry) {
86
- opt[entry.entryName] = ssgOptions;
87
- return opt;
88
- }, {});
89
- } else if ((typeof ssgOptions === "undefined" ? "undefined" : _type_of(ssgOptions)) === "object") {
90
- var isSingle = isSingleEntry(entrypoints);
91
- if (isSingle && typeof ssgOptions.main === "undefined") {
92
- return {
93
- main: ssgOptions
94
- };
95
- } else {
96
- return ssgOptions;
97
- }
98
- } else if (typeof ssgOptions === "function") {
99
- var intermediateOptions = {};
100
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
101
- try {
102
- for (var _iterator = entrypoints[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
103
- var entrypoint = _step.value;
104
- var entryName = entrypoint.entryName;
105
- if (Array.isArray(server === null || server === void 0 ? void 0 : server.baseUrl)) {
106
- var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
107
- try {
108
- var _loop = function() {
109
- var url = _step1.value;
110
- var matchUrl = entryName === "main" ? url : "".concat(url, "/").concat(entryName);
111
- var route = routes.find(function(route2) {
112
- return route2.urlPath === matchUrl;
113
- });
114
- intermediateOptions[route === null || route === void 0 ? void 0 : route.urlPath] = ssgOptions(entryName, {
115
- baseUrl: url
116
- });
117
- };
118
- for (var _iterator1 = server.baseUrl[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true)
119
- _loop();
120
- } catch (err) {
121
- _didIteratorError1 = true;
122
- _iteratorError1 = err;
123
- } finally {
124
- try {
125
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
126
- _iterator1.return();
127
- }
128
- } finally {
129
- if (_didIteratorError1) {
130
- throw _iteratorError1;
131
- }
132
- }
133
- }
134
- } else {
135
- intermediateOptions[entryName] = ssgOptions(entryName, {
136
- baseUrl: server === null || server === void 0 ? void 0 : server.baseUrl
137
- });
138
- }
139
- }
140
- } catch (err) {
141
- _didIteratorError = true;
142
- _iteratorError = err;
143
- } finally {
144
- try {
145
- if (!_iteratorNormalCompletion && _iterator.return != null) {
146
- _iterator.return();
147
- }
148
- } finally {
149
- if (_didIteratorError) {
150
- throw _iteratorError;
151
- }
152
- }
153
- }
154
- return intermediateOptions;
155
- }
156
- return false;
157
- };
158
- var openRouteSSR = function(routes) {
159
- var entries = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
160
- return routes.map(function(ssgRoute) {
161
- return _object_spread_props(_object_spread({}, ssgRoute), {
162
- isSSR: entries.includes(ssgRoute.entryName),
163
- bundle: "".concat(SERVER_BUNDLE_DIRECTORY, "/").concat(ssgRoute.entryName, ".js")
164
- });
165
- });
166
- };
167
- var flattenRoutes = function(routes) {
168
- var parents = [];
169
- var newRoutes = [];
170
- var traverseRoute = function(route) {
171
- var parent = parents[parents.length - 1];
172
- var path2 = parent ? "".concat(parent.path, "/").concat(route.path || "").replace(/\/+/g, "/") : route.path || "";
173
- path2 = path2.replace(/\/$/, "");
174
- if (route._component && (path2 !== "/" || path2 === "/" && !parent)) {
175
- newRoutes.push(_object_spread_props(_object_spread({}, route), {
176
- path: path2
177
- }));
178
- }
179
- if (route.children) {
180
- parents.push(_object_spread_props(_object_spread({}, route), {
181
- path: path2
182
- }));
183
- route.children.forEach(traverseRoute);
184
- parents.pop();
185
- }
186
- };
187
- routes.forEach(traverseRoute);
188
- return newRoutes;
189
- };
190
- function chunkArray(arr, size) {
191
- var result = [];
192
- for (var i = 0; i < arr.length; i += size) {
193
- result.push(arr.slice(i, i + size));
194
- }
195
- return result;
196
- }
197
- export {
198
- chunkArray,
199
- flattenRoutes,
200
- formatOutput,
201
- formatPath,
202
- getOutput,
203
- getUrlPrefix,
204
- isDynamicUrl,
205
- openRouteSSR,
206
- readJSONSpec,
207
- replaceWithAlias,
208
- standardOptions,
209
- writeJSONSpec
210
- };
@@ -1,4 +0,0 @@
1
- var CLOSE_SIGN = "modern_close_server";
2
- export {
3
- CLOSE_SIGN
4
- };
@@ -1,66 +0,0 @@
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
- var createServer = function(api, ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) {
7
- return new Promise(function(resolve, reject) {
8
- var _cp_stderr, _cp_stdout;
9
- var entries = ssgRoutes.map(function(route) {
10
- return route.entryName;
11
- });
12
- var backup = openRouteSSR(pageRoutes, entries);
13
- var total = backup.concat(apiRoutes);
14
- var cp = childProcess.fork(path.join(__dirname, "process"), {
15
- cwd: appDirectory,
16
- silent: true
17
- });
18
- var appContext = api.useAppContext();
19
- var plugins = appContext.serverPlugins;
20
- cp.send(JSON.stringify({
21
- options,
22
- renderRoutes: ssgRoutes,
23
- routes: total,
24
- appContext: {
25
- // Make sure that bff runs the product of the dist directory, because we dont register ts-node in the child process
26
- apiDirectory: path.join(appContext.distDirectory, path.relative(appContext.appDirectory, appContext.apiDirectory)),
27
- lambdaDirectory: path.join(appContext.distDirectory, path.relative(appContext.appDirectory, appContext.lambdaDirectory)),
28
- appDirectory: appContext.appDirectory
29
- },
30
- plugins,
31
- distDirectory: appContext.distDirectory
32
- }));
33
- var htmlChunks = [];
34
- var htmlAry = [];
35
- cp.on("message", function(chunk) {
36
- if (chunk !== null) {
37
- htmlChunks.push(chunk);
38
- } else {
39
- var html = htmlChunks.join("");
40
- htmlAry.push(html);
41
- htmlChunks.length = 0;
42
- }
43
- if (htmlAry.length === ssgRoutes.length) {
44
- cp.send(CLOSE_SIGN);
45
- resolve(htmlAry);
46
- }
47
- });
48
- (_cp_stderr = cp.stderr) === null || _cp_stderr === void 0 ? void 0 : _cp_stderr.on("data", function(chunk) {
49
- var str = chunk.toString();
50
- if (str.includes("Error")) {
51
- logger.error(str);
52
- reject(new Error("ssg render failed"));
53
- cp.kill("SIGKILL");
54
- } else {
55
- logger.info(str.replace(/[^\S\n]+/g, " "));
56
- }
57
- });
58
- (_cp_stdout = cp.stdout) === null || _cp_stdout === void 0 ? void 0 : _cp_stdout.on("data", function(chunk) {
59
- var str = chunk.toString();
60
- logger.info(str.replace(/[^\S\n]+/g, " "));
61
- });
62
- });
63
- };
64
- export {
65
- createServer
66
- };
@@ -1,46 +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 { _ as _type_of } from "@swc/helpers/_/_type_of";
4
- import EventEmitter from "events";
5
- import { Readable } from "stream";
6
- import httpMocks from "node-mocks-http";
7
- var compile = function(requestHandler) {
8
- return function(options) {
9
- var extend = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
10
- return new Promise(function(resolve, reject) {
11
- var req = httpMocks.createRequest(_object_spread_props(_object_spread({}, options), {
12
- eventEmitter: Readable
13
- }));
14
- var res = httpMocks.createResponse({
15
- eventEmitter: EventEmitter
16
- });
17
- Object.assign(req, extend);
18
- var proxyRes = new Proxy(res, {
19
- get: function get(obj, prop) {
20
- if ((typeof prop === "undefined" ? "undefined" : _type_of(prop)) === "symbol" && !obj[prop]) {
21
- return null;
22
- }
23
- return obj[prop];
24
- }
25
- });
26
- res.on("finish", function() {
27
- if (res.statusCode !== 200) {
28
- reject(new Error(res.statusMessage));
29
- } else {
30
- resolve(res._getData());
31
- }
32
- });
33
- res.on("error", function(e) {
34
- return reject(e);
35
- });
36
- try {
37
- requestHandler(req, proxyRes);
38
- } catch (e) {
39
- reject(e);
40
- }
41
- });
42
- };
43
- };
44
- export {
45
- compile
46
- };
@@ -1,263 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
3
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
4
- import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
5
- import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
6
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
- import assert from "node:assert";
8
- import { request } from "node:http";
9
- import { createProdServer, loadServerPlugins } from "@modern-js/prod-server";
10
- import { createLogger } from "@modern-js/utils";
11
- import portfinder from "portfinder";
12
- import { chunkArray } from "../libs/util";
13
- import { CLOSE_SIGN } from "./consts";
14
- function getLogger() {
15
- var logger = createLogger({
16
- level: "verbose"
17
- });
18
- return _object_spread_props(_object_spread({}, logger), {
19
- error: function() {
20
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
21
- args[_key] = arguments[_key];
22
- }
23
- var _console;
24
- (_console = console).error.apply(_console, _to_consumable_array(args));
25
- }
26
- });
27
- }
28
- var MAX_CONCURRENT_REQUESTS = 10;
29
- process.on("message", function() {
30
- var _ref = _async_to_generator(function(chunk) {
31
- var context, routes, renderRoutes, options, appContext, plugins, distDirectory, nodeServer, serverConfig, defaultPort, port, serverOptions, _tmp, sendProcessMessage, e;
32
- return _ts_generator(this, function(_state) {
33
- switch (_state.label) {
34
- case 0:
35
- if (chunk === CLOSE_SIGN) {
36
- process.exit();
37
- }
38
- context = JSON.parse(chunk);
39
- routes = context.routes, renderRoutes = context.renderRoutes, options = context.options, appContext = context.appContext, plugins = context.plugins, distDirectory = context.distDirectory;
40
- nodeServer = null;
41
- _state.label = 1;
42
- case 1:
43
- _state.trys.push([
44
- 1,
45
- 5,
46
- ,
47
- 6
48
- ]);
49
- serverConfig = options.server;
50
- defaultPort = Number(process.env.PORT) || serverConfig.port;
51
- portfinder.basePort = defaultPort;
52
- return [
53
- 4,
54
- portfinder.getPortPromise()
55
- ];
56
- case 2:
57
- port = _state.sent();
58
- _tmp = {
59
- pwd: distDirectory,
60
- config: options,
61
- appContext,
62
- // TODO
63
- serverConfigPath: "",
64
- routes
65
- };
66
- return [
67
- 4,
68
- loadServerPlugins(plugins, appContext.appDirectory || distDirectory)
69
- ];
70
- case 3:
71
- serverOptions = (_tmp.plugins = _state.sent(), _tmp.staticGenerate = true, _tmp.logger = getLogger(), _tmp);
72
- assert(process.send, "process.send is not available");
73
- sendProcessMessage = process.send.bind(process);
74
- return [
75
- 4,
76
- createProdServer(serverOptions)
77
- ];
78
- case 4:
79
- nodeServer = _state.sent();
80
- nodeServer.listen(port, /* @__PURE__ */ _async_to_generator(function() {
81
- var chunkedRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, routes2, promises, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, result, err, err;
82
- return _ts_generator(this, function(_state2) {
83
- switch (_state2.label) {
84
- case 0:
85
- if (!nodeServer)
86
- return [
87
- 2
88
- ];
89
- chunkedRoutes = chunkArray(renderRoutes, MAX_CONCURRENT_REQUESTS);
90
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
91
- _state2.label = 1;
92
- case 1:
93
- _state2.trys.push([
94
- 1,
95
- 12,
96
- 13,
97
- 14
98
- ]);
99
- _iterator = chunkedRoutes[Symbol.iterator]();
100
- _state2.label = 2;
101
- case 2:
102
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
103
- return [
104
- 3,
105
- 11
106
- ];
107
- routes2 = _step.value;
108
- promises = routes2.map(function() {
109
- var _ref2 = _async_to_generator(function(route) {
110
- return _ts_generator(this, function(_state3) {
111
- return [
112
- 2,
113
- getHtml("http://localhost:".concat(port).concat(route.urlPath), port)
114
- ];
115
- });
116
- });
117
- return function(route) {
118
- return _ref2.apply(this, arguments);
119
- };
120
- }());
121
- _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
122
- _state2.label = 3;
123
- case 3:
124
- _state2.trys.push([
125
- 3,
126
- 8,
127
- 9,
128
- 10
129
- ]);
130
- return [
131
- 4,
132
- Promise.all(promises)
133
- ];
134
- case 4:
135
- _iterator1 = _state2.sent()[Symbol.iterator]();
136
- _state2.label = 5;
137
- case 5:
138
- if (!!(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done))
139
- return [
140
- 3,
141
- 7
142
- ];
143
- result = _step1.value;
144
- sendProcessMessage(result);
145
- sendProcessMessage(null);
146
- _state2.label = 6;
147
- case 6:
148
- _iteratorNormalCompletion1 = true;
149
- return [
150
- 3,
151
- 5
152
- ];
153
- case 7:
154
- return [
155
- 3,
156
- 10
157
- ];
158
- case 8:
159
- err = _state2.sent();
160
- _didIteratorError1 = true;
161
- _iteratorError1 = err;
162
- return [
163
- 3,
164
- 10
165
- ];
166
- case 9:
167
- try {
168
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
169
- _iterator1.return();
170
- }
171
- } finally {
172
- if (_didIteratorError1) {
173
- throw _iteratorError1;
174
- }
175
- }
176
- return [
177
- 7
178
- ];
179
- case 10:
180
- _iteratorNormalCompletion = true;
181
- return [
182
- 3,
183
- 2
184
- ];
185
- case 11:
186
- return [
187
- 3,
188
- 14
189
- ];
190
- case 12:
191
- err = _state2.sent();
192
- _didIteratorError = true;
193
- _iteratorError = err;
194
- return [
195
- 3,
196
- 14
197
- ];
198
- case 13:
199
- try {
200
- if (!_iteratorNormalCompletion && _iterator.return != null) {
201
- _iterator.return();
202
- }
203
- } finally {
204
- if (_didIteratorError) {
205
- throw _iteratorError;
206
- }
207
- }
208
- return [
209
- 7
210
- ];
211
- case 14:
212
- nodeServer.close();
213
- return [
214
- 2
215
- ];
216
- }
217
- });
218
- }));
219
- return [
220
- 3,
221
- 6
222
- ];
223
- case 5:
224
- e = _state.sent();
225
- nodeServer === null || nodeServer === void 0 ? void 0 : nodeServer.close();
226
- process.stderr.write(_instanceof(e, Error) ? e.stack : e.toString());
227
- return [
228
- 3,
229
- 6
230
- ];
231
- case 6:
232
- return [
233
- 2
234
- ];
235
- }
236
- });
237
- });
238
- return function(chunk) {
239
- return _ref.apply(this, arguments);
240
- };
241
- }());
242
- function getHtml(url, port) {
243
- var headers = {
244
- host: "localhost:".concat(port)
245
- };
246
- return new Promise(function(resolve, reject) {
247
- request(url, {
248
- headers
249
- }, function(res) {
250
- var chunks = [];
251
- res.on("error", function(error) {
252
- reject(error);
253
- });
254
- res.on("data", function(chunk) {
255
- chunks.push(chunk);
256
- });
257
- res.on("end", function() {
258
- var html = Buffer.concat(chunks).toString();
259
- resolve(html);
260
- });
261
- }).end();
262
- });
263
- }