@modern-js/plugin-ssg 2.14.0 → 2.16.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.
@@ -1,239 +1,240 @@
1
- function _defineProperty(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
13
  }
14
- function _objectSpread(target) {
15
- for(var i = 1; i < arguments.length; i++){
16
- var source = arguments[i] != null ? arguments[i] : {};
17
- var ownKeys = Object.keys(source);
18
- if (typeof Object.getOwnPropertySymbols === "function") {
19
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
- }));
22
- }
23
- ownKeys.forEach(function(key) {
24
- _defineProperty(target, key, source[key]);
25
- });
14
+ function _object_spread(target) {
15
+ for (var i = 1; i < arguments.length; i++) {
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys2 = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys2 = ownKeys2.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
26
22
  }
27
- return target;
23
+ ownKeys2.forEach(function(key) {
24
+ _define_property(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
28
  }
29
29
  function ownKeys(object, enumerableOnly) {
30
- var keys = Object.keys(object);
31
- if (Object.getOwnPropertySymbols) {
32
- var symbols = Object.getOwnPropertySymbols(object);
33
- if (enumerableOnly) {
34
- symbols = symbols.filter(function(sym) {
35
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
- });
37
- }
38
- keys.push.apply(keys, symbols);
30
+ var keys = Object.keys(object);
31
+ if (Object.getOwnPropertySymbols) {
32
+ var symbols = Object.getOwnPropertySymbols(object);
33
+ if (enumerableOnly) {
34
+ symbols = symbols.filter(function(sym) {
35
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
+ });
39
37
  }
40
- return keys;
38
+ keys.push.apply(keys, symbols);
39
+ }
40
+ return keys;
41
41
  }
42
- function _objectSpreadProps(target, source) {
43
- source = source != null ? source : {};
44
- if (Object.getOwnPropertyDescriptors) {
45
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
- } else {
47
- ownKeys(Object(source)).forEach(function(key) {
48
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
- });
50
- }
51
- return target;
42
+ function _object_spread_props(target, source) {
43
+ source = source != null ? source : {};
44
+ if (Object.getOwnPropertyDescriptors) {
45
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
+ } else {
47
+ ownKeys(Object(source)).forEach(function(key) {
48
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
+ });
50
+ }
51
+ return target;
52
52
  }
53
53
  import path from "path";
54
54
  import { ROUTE_SPEC_FILE, fs, isSingleEntry, SERVER_BUNDLE_DIRECTORY } from "@modern-js/utils";
55
- function formatOutput(filename) {
56
- var outputPath = path.extname(filename) ? filename : "".concat(filename, "/index.html");
57
- return outputPath;
55
+ export function formatOutput(filename) {
56
+ var outputPath = path.extname(filename) ? filename : "".concat(filename, "/index.html");
57
+ return outputPath;
58
58
  }
59
- function formatPath(str) {
60
- var addr = str;
61
- if (!addr || typeof addr !== "string") {
62
- return addr;
63
- }
64
- if (addr.startsWith(".")) {
65
- addr = addr.slice(1);
66
- }
67
- if (!addr.startsWith("/")) {
68
- addr = "/".concat(addr);
69
- }
70
- if (addr.endsWith("/") && addr !== "/") {
71
- addr = addr.slice(0, addr.length - 1);
72
- }
59
+ export function formatPath(str) {
60
+ var addr = str;
61
+ if (!addr || typeof addr !== "string") {
73
62
  return addr;
63
+ }
64
+ if (addr.startsWith(".")) {
65
+ addr = addr.slice(1);
66
+ }
67
+ if (!addr.startsWith("/")) {
68
+ addr = "/".concat(addr);
69
+ }
70
+ if (addr.endsWith("/") && addr !== "/") {
71
+ addr = addr.slice(0, addr.length - 1);
72
+ }
73
+ return addr;
74
74
  }
75
- function isDynamicUrl(url) {
76
- return url.includes(":");
75
+ export function isDynamicUrl(url) {
76
+ return url.includes(":");
77
77
  }
78
- function getUrlPrefix(route, baseUrl) {
79
- var base = "";
80
- if (Array.isArray(baseUrl)) {
81
- var filters = baseUrl.filter(function(url) {
82
- return route.urlPath.includes(url);
83
- });
84
- if (filters.length > 1) {
85
- var matched = filters.sort(function(a, b) {
86
- return a.length - b.length;
87
- })[0];
88
- if (!matched) {
89
- throw new Error("");
90
- }
91
- base = matched;
92
- }
93
- } else {
94
- base = baseUrl;
78
+ export function getUrlPrefix(route, baseUrl) {
79
+ var base = "";
80
+ if (Array.isArray(baseUrl)) {
81
+ var filters = baseUrl.filter(function(url) {
82
+ return route.urlPath.includes(url);
83
+ });
84
+ if (filters.length > 1) {
85
+ var matched = filters.sort(function(a, b) {
86
+ return a.length - b.length;
87
+ })[0];
88
+ if (!matched) {
89
+ throw new Error("");
90
+ }
91
+ base = matched;
95
92
  }
96
- base = base === "/" ? "" : base;
97
- var entryName = route.entryName === "main" ? "" : route.entryName;
98
- var prefix = "".concat(base, "/").concat(entryName);
99
- return prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
93
+ } else {
94
+ base = baseUrl;
95
+ }
96
+ base = base === "/" ? "" : base;
97
+ var entryName = route.entryName === "main" ? "" : route.entryName;
98
+ var prefix = "".concat(base, "/").concat(entryName);
99
+ return prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
100
100
  }
101
- function getOutput(route, base, agreed) {
102
- var output = route.output;
103
- if (output) {
104
- return output;
105
- }
106
- if (agreed) {
107
- var urlWithoutBase = route.urlPath.replace(base, "");
108
- return urlWithoutBase.startsWith("/") ? urlWithoutBase.slice(1) : urlWithoutBase;
109
- }
110
- throw new Error("routing must provide output when calling createPage(), check ".concat(route.urlPath));
101
+ export function getOutput(route, base, agreed) {
102
+ var output = route.output;
103
+ if (output) {
104
+ return output;
105
+ }
106
+ if (agreed) {
107
+ var urlWithoutBase = route.urlPath.replace(base, "");
108
+ return urlWithoutBase.startsWith("/") ? urlWithoutBase.slice(1) : urlWithoutBase;
109
+ }
110
+ throw new Error("routing must provide output when calling createPage(), check ".concat(route.urlPath));
111
111
  }
112
- var readJSONSpec = function(dir) {
113
- var routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
114
- var routeJSON = require(routeJSONPath);
115
- var routes = routeJSON.routes;
116
- return routes;
112
+ export var readJSONSpec = function(dir) {
113
+ var routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
114
+ var routeJSON = require(routeJSONPath);
115
+ var routes = routeJSON.routes;
116
+ return routes;
117
117
  };
118
- var writeJSONSpec = function(dir, routes) {
119
- var routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
120
- fs.writeJSONSync(routeJSONPath, {
121
- routes: routes
122
- }, {
123
- spaces: 2
124
- });
118
+ export var writeJSONSpec = function(dir, routes) {
119
+ var routeJSONPath = path.join(dir, ROUTE_SPEC_FILE);
120
+ fs.writeJSONSync(routeJSONPath, {
121
+ routes
122
+ }, {
123
+ spaces: 2
124
+ });
125
125
  };
126
- var replaceWithAlias = function(base, filePath, alias) {
127
- return path.posix.join(alias, path.posix.relative(base, filePath));
126
+ export var replaceWithAlias = function(base, filePath, alias) {
127
+ return path.posix.join(alias, path.posix.relative(base, filePath));
128
128
  };
129
- var standardOptions = function(ssgOptions, entrypoints, routes, server) {
130
- if (ssgOptions === false) {
131
- return false;
129
+ export var standardOptions = function(ssgOptions, entrypoints, routes, server) {
130
+ if (ssgOptions === false) {
131
+ return false;
132
+ }
133
+ if (ssgOptions === true) {
134
+ return entrypoints.reduce(function(opt, entry) {
135
+ opt[entry.entryName] = ssgOptions;
136
+ return opt;
137
+ }, {});
138
+ } else if (typeof ssgOptions === "object") {
139
+ var isSingle = isSingleEntry(entrypoints);
140
+ if (isSingle && typeof ssgOptions.main === "undefined") {
141
+ return {
142
+ main: ssgOptions
143
+ };
144
+ } else {
145
+ return ssgOptions;
132
146
  }
133
- if (ssgOptions === true) {
134
- return entrypoints.reduce(function(opt, entry) {
135
- opt[entry.entryName] = ssgOptions;
136
- return opt;
137
- }, {});
138
- } else if (typeof ssgOptions === "object") {
139
- var isSingle = isSingleEntry(entrypoints);
140
- if (isSingle && typeof ssgOptions.main === "undefined") {
141
- return {
142
- main: ssgOptions
143
- };
144
- } else {
145
- return ssgOptions;
146
- }
147
- } else if (typeof ssgOptions === "function") {
148
- var intermediateOptions = {};
149
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
150
- try {
151
- var _loop = function() {
152
- var entrypoint = _step.value;
153
- var entryName = entrypoint.entryName;
154
- if (Array.isArray(server === null || server === void 0 ? void 0 : server.baseUrl)) {
155
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
156
- try {
157
- var _loop = function() {
158
- var url = _step1.value;
159
- var matchUrl = entryName === "main" ? url : "".concat(url, "/").concat(entryName);
160
- var route = routes.find(function(route2) {
161
- return route2.urlPath === matchUrl;
162
- });
163
- intermediateOptions[route === null || route === void 0 ? void 0 : route.urlPath] = ssgOptions(entryName, {
164
- baseUrl: url
165
- });
166
- };
167
- for(var _iterator = server.baseUrl[Symbol.iterator](), _step1; !(_iteratorNormalCompletion = (_step1 = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
168
- } catch (err) {
169
- _didIteratorError = true;
170
- _iteratorError = err;
171
- } finally{
172
- try {
173
- if (!_iteratorNormalCompletion && _iterator.return != null) {
174
- _iterator.return();
175
- }
176
- } finally{
177
- if (_didIteratorError) {
178
- throw _iteratorError;
179
- }
180
- }
181
- }
182
- } else {
183
- intermediateOptions[entryName] = ssgOptions(entryName, {
184
- baseUrl: server === null || server === void 0 ? void 0 : server.baseUrl
185
- });
186
- }
147
+ } else if (typeof ssgOptions === "function") {
148
+ var intermediateOptions = {};
149
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
150
+ try {
151
+ var _loop = function() {
152
+ var entrypoint = _step.value;
153
+ var entryName = entrypoint.entryName;
154
+ if (Array.isArray(server === null || server === void 0 ? void 0 : server.baseUrl)) {
155
+ var _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = void 0;
156
+ try {
157
+ var _loop2 = function() {
158
+ var url = _step1.value;
159
+ var matchUrl = entryName === "main" ? url : "".concat(url, "/").concat(entryName);
160
+ var route = routes.find(function(route2) {
161
+ return route2.urlPath === matchUrl;
162
+ });
163
+ intermediateOptions[route === null || route === void 0 ? void 0 : route.urlPath] = ssgOptions(entryName, {
164
+ baseUrl: url
165
+ });
187
166
  };
188
- for(var _iterator = entrypoints[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
189
- } catch (err) {
190
- _didIteratorError = true;
191
- _iteratorError = err;
192
- } finally{
167
+ for (var _iterator2 = server.baseUrl[Symbol.iterator](), _step1; !(_iteratorNormalCompletion2 = (_step1 = _iterator2.next()).done); _iteratorNormalCompletion2 = true)
168
+ _loop2();
169
+ } catch (err) {
170
+ _didIteratorError2 = true;
171
+ _iteratorError2 = err;
172
+ } finally {
193
173
  try {
194
- if (!_iteratorNormalCompletion && _iterator.return != null) {
195
- _iterator.return();
196
- }
197
- } finally{
198
- if (_didIteratorError) {
199
- throw _iteratorError;
200
- }
174
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
175
+ _iterator2.return();
176
+ }
177
+ } finally {
178
+ if (_didIteratorError2) {
179
+ throw _iteratorError2;
180
+ }
201
181
  }
182
+ }
183
+ } else {
184
+ intermediateOptions[entryName] = ssgOptions(entryName, {
185
+ baseUrl: server === null || server === void 0 ? void 0 : server.baseUrl
186
+ });
187
+ }
188
+ };
189
+ for (var _iterator = entrypoints[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)
190
+ _loop();
191
+ } catch (err) {
192
+ _didIteratorError = true;
193
+ _iteratorError = err;
194
+ } finally {
195
+ try {
196
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
197
+ _iterator.return();
202
198
  }
203
- return intermediateOptions;
199
+ } finally {
200
+ if (_didIteratorError) {
201
+ throw _iteratorError;
202
+ }
203
+ }
204
204
  }
205
- return false;
205
+ return intermediateOptions;
206
+ }
207
+ return false;
206
208
  };
207
- var openRouteSSR = function(routes) {
208
- var entries = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
209
- return routes.map(function(ssgRoute) {
210
- return _objectSpreadProps(_objectSpread({}, ssgRoute), {
211
- isSSR: entries.includes(ssgRoute.entryName),
212
- bundle: "".concat(SERVER_BUNDLE_DIRECTORY, "/").concat(ssgRoute.entryName, ".js")
213
- });
209
+ export var openRouteSSR = function(routes) {
210
+ var entries = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
211
+ return routes.map(function(ssgRoute) {
212
+ return _object_spread_props(_object_spread({}, ssgRoute), {
213
+ isSSR: entries.includes(ssgRoute.entryName),
214
+ bundle: "".concat(SERVER_BUNDLE_DIRECTORY, "/").concat(ssgRoute.entryName, ".js")
214
215
  });
216
+ });
215
217
  };
216
- var flattenRoutes = function(routes) {
217
- var parents = [];
218
- var newRoutes = [];
219
- var traverseRoute = function(route) {
220
- var parent = parents[parents.length - 1];
221
- var path2 = parent ? "".concat(parent.path, "/").concat(route.path || "").replace(/\/+/g, "/") : route.path || "";
222
- path2 = path2.replace(/\/$/, "");
223
- if (route._component && (path2 !== "/" || path2 === "/" && !parent)) {
224
- newRoutes.push(_objectSpreadProps(_objectSpread({}, route), {
225
- path: path2
226
- }));
227
- }
228
- if (route.children) {
229
- parents.push(_objectSpreadProps(_objectSpread({}, route), {
230
- path: path2
231
- }));
232
- route.children.forEach(traverseRoute);
233
- parents.pop();
234
- }
235
- };
236
- routes.forEach(traverseRoute);
237
- return newRoutes;
218
+ export var flattenRoutes = function(routes) {
219
+ var parents = [];
220
+ var newRoutes = [];
221
+ var traverseRoute = function(route) {
222
+ var parent = parents[parents.length - 1];
223
+ var path2 = parent ? "".concat(parent.path, "/").concat(route.path || "").replace(/\/+/g, "/") : route.path || "";
224
+ path2 = path2.replace(/\/$/, "");
225
+ if (route._component && (path2 !== "/" || path2 === "/" && !parent)) {
226
+ newRoutes.push(_object_spread_props(_object_spread({}, route), {
227
+ path: path2
228
+ }));
229
+ }
230
+ if (route.children) {
231
+ parents.push(_object_spread_props(_object_spread({}, route), {
232
+ path: path2
233
+ }));
234
+ route.children.forEach(traverseRoute);
235
+ parents.pop();
236
+ }
237
+ };
238
+ routes.forEach(traverseRoute);
239
+ return newRoutes;
238
240
  };
239
- export { flattenRoutes, formatOutput, formatPath, getOutput, getUrlPrefix, isDynamicUrl, openRouteSSR, readJSONSpec, replaceWithAlias, standardOptions, writeJSONSpec };
@@ -1,2 +1 @@
1
- var CLOSE_SIGN = "modern_close_server";
2
- export { CLOSE_SIGN };
1
+ export var CLOSE_SIGN = "modern_close_server";
@@ -3,62 +3,61 @@ import path from "path";
3
3
  import { logger } from "@modern-js/utils";
4
4
  import { openRouteSSR } from "../libs/util";
5
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.serverInternalPlugins;
20
- cp.send(JSON.stringify({
21
- options: options,
22
- renderRoutes: ssgRoutes,
23
- routes: total,
24
- appDirectory: appDirectory,
25
- plugins: plugins
26
- }));
27
- var htmlChunks = [];
28
- var htmlAry = [];
29
- cp.on("message", function(chunk) {
30
- if (chunk !== null) {
31
- htmlChunks.push(chunk);
32
- } else {
33
- var html = htmlChunks.join("");
34
- htmlAry.push(html);
35
- htmlChunks.length = 0;
36
- }
37
- if (htmlAry.length === ssgRoutes.length) {
38
- cp.send(CLOSE_SIGN);
39
- resolve(htmlAry);
40
- }
41
- });
42
- (_cp_stderr = cp.stderr) === null || _cp_stderr === void 0 ? void 0 : _cp_stderr.on("data", function(chunk) {
43
- var str = chunk.toString();
44
- if (str.includes("Error")) {
45
- logger.error(str);
46
- reject(new Error("ssg render failed"));
47
- cp.kill("SIGKILL");
48
- } else {
49
- logger.info(str.replace(/[^\S\n]+/g, " "));
50
- }
51
- });
52
- (_cp_stdout = cp.stdout) === null || _cp_stdout === void 0 ? void 0 : _cp_stdout.on("data", function(chunk) {
53
- var str = chunk.toString();
54
- if (str.includes("Error")) {
55
- logger.error(str);
56
- reject(new Error("ssg render failed"));
57
- cp.kill("SIGKILL");
58
- } else {
59
- logger.info(str.replace(/[^\S\n]+/g, " "));
60
- }
61
- });
6
+ export 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;
62
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.serverInternalPlugins;
20
+ cp.send(JSON.stringify({
21
+ options,
22
+ renderRoutes: ssgRoutes,
23
+ routes: total,
24
+ appDirectory,
25
+ plugins
26
+ }));
27
+ var htmlChunks = [];
28
+ var htmlAry = [];
29
+ cp.on("message", function(chunk) {
30
+ if (chunk !== null) {
31
+ htmlChunks.push(chunk);
32
+ } else {
33
+ var html = htmlChunks.join("");
34
+ htmlAry.push(html);
35
+ htmlChunks.length = 0;
36
+ }
37
+ if (htmlAry.length === ssgRoutes.length) {
38
+ cp.send(CLOSE_SIGN);
39
+ resolve(htmlAry);
40
+ }
41
+ });
42
+ (_cp_stderr = cp.stderr) === null || _cp_stderr === void 0 ? void 0 : _cp_stderr.on("data", function(chunk) {
43
+ var str = chunk.toString();
44
+ if (str.includes("Error")) {
45
+ logger.error(str);
46
+ reject(new Error("ssg render failed"));
47
+ cp.kill("SIGKILL");
48
+ } else {
49
+ logger.info(str.replace(/[^\S\n]+/g, " "));
50
+ }
51
+ });
52
+ (_cp_stdout = cp.stdout) === null || _cp_stdout === void 0 ? void 0 : _cp_stdout.on("data", function(chunk) {
53
+ var str = chunk.toString();
54
+ if (str.includes("Error")) {
55
+ logger.error(str);
56
+ reject(new Error("ssg render failed"));
57
+ cp.kill("SIGKILL");
58
+ } else {
59
+ logger.info(str.replace(/[^\S\n]+/g, " "));
60
+ }
61
+ });
62
+ });
63
63
  };
64
- export { createServer };