@modern-js/plugin-ssg 2.0.0-beta.0 → 2.0.0-beta.2

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.
@@ -4,31 +4,18 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
9
  var _reactRouterDom = require("react-router-dom");
13
-
14
10
  var _util = require("./libs/util");
15
-
16
11
  var _server = require("./server");
17
-
18
12
  var _output = require("./libs/output");
19
-
20
13
  var _replace = require("./libs/replace");
21
-
22
14
  var _make = require("./libs/make");
23
-
24
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
16
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
27
-
28
17
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
29
-
30
18
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
31
-
32
19
  var _default = () => ({
33
20
  name: '@modern-js/plugin-ssg',
34
21
  pre: ['@modern-js/plugin-server', '@modern-js/plugin-bff'],
@@ -38,7 +25,6 @@ var _default = () => ({
38
25
  validateSchema() {
39
26
  return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
40
27
  },
41
-
42
28
  modifyFileSystemRoutes({
43
29
  entrypoint,
44
30
  routes
@@ -53,7 +39,6 @@ var _default = () => ({
53
39
  routes
54
40
  };
55
41
  },
56
-
57
42
  async afterBuild() {
58
43
  const resolvedConfig = api.useResolvedConfigContext();
59
44
  const appContext = api.useAppContext();
@@ -67,33 +52,32 @@ var _default = () => ({
67
52
  } = resolvedConfig;
68
53
  const {
69
54
  ssg,
70
- path: outputPath
55
+ distPath: {
56
+ root: outputPath
57
+ } = {}
71
58
  } = output;
72
- const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
73
-
59
+ const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg;
60
+ // no ssg configuration, skip ssg render.
74
61
  if (!ssgOptions) {
75
62
  return;
76
63
  }
77
-
78
64
  const buildDir = _path.default.join(appDirectory, outputPath);
65
+ const routes = (0, _util.readJSONSpec)(buildDir);
79
66
 
80
- const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
81
-
67
+ // filter all routes not web
82
68
  const pageRoutes = routes.filter(route => !route.isApi);
83
- const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
69
+ const apiRoutes = routes.filter(route => route.isApi);
84
70
 
71
+ // if no web page route, skip ssg render
85
72
  if (pageRoutes.length === 0) {
86
73
  return;
87
74
  }
88
-
89
75
  const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints, pageRoutes, server);
90
-
91
76
  if (!intermediateOptions) {
92
77
  return;
93
78
  }
94
-
95
- const ssgRoutes = []; // each route will try to match the configuration
96
-
79
+ const ssgRoutes = [];
80
+ // each route will try to match the configuration
97
81
  pageRoutes.forEach(pageRoute => {
98
82
  const {
99
83
  entryName,
@@ -101,14 +85,13 @@ var _default = () => ({
101
85
  } = pageRoute;
102
86
  const agreedRoutes = agreedRouteMap[entryName];
103
87
  let entryOptions = intermediateOptions[entryName] || intermediateOptions[pageRoute.urlPath];
104
-
105
88
  if (!agreedRoutes) {
106
89
  // default behavior for non-agreed route
107
90
  if (!entryOptions) {
108
91
  return;
109
- } // only add entry route if entryOptions is true
110
-
92
+ }
111
93
 
94
+ // only add entry route if entryOptions is true
112
95
  if (entryOptions === true) {
113
96
  ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
114
97
  output: entryPath
@@ -130,7 +113,6 @@ var _default = () => ({
130
113
  if (!entryOptions) {
131
114
  return;
132
115
  }
133
-
134
116
  if (entryOptions === true) {
135
117
  entryOptions = {
136
118
  preventDefault: [],
@@ -138,13 +120,12 @@ var _default = () => ({
138
120
  headers: {}
139
121
  };
140
122
  }
141
-
142
123
  const {
143
124
  preventDefault = [],
144
125
  routes: userRoutes = [],
145
126
  headers
146
- } = entryOptions; // if the user sets the routes, then only add them
147
-
127
+ } = entryOptions;
128
+ // if the user sets the routes, then only add them
148
129
  if (userRoutes.length > 0) {
149
130
  userRoutes.forEach(route => {
150
131
  if (typeof route === 'string') {
@@ -169,39 +150,35 @@ var _default = () => ({
169
150
  }
170
151
  }
171
152
  });
172
-
173
153
  if (ssgRoutes.length === 0) {
174
154
  return;
175
- } // currently SSG and SSR cannot be turned on at the same time、same route
176
-
155
+ }
177
156
 
157
+ // currently SSG and SSR cannot be turned on at the same time、same route
178
158
  ssgRoutes.forEach(ssgRoute => {
179
159
  if (ssgRoute.isSSR) {
180
160
  const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
181
-
182
161
  if (isOriginRoute) {
183
162
  throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
184
163
  }
185
-
186
164
  _utils.logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
187
165
  }
188
-
189
166
  ssgRoute.isSSR = false;
190
167
  ssgRoute.output = (0, _util.formatOutput)(ssgRoute.output);
191
168
  });
192
- const htmlAry = await (0, _server.createServer)(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
169
+ const htmlAry = await (0, _server.createServer)(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory);
193
170
 
194
- (0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir); // format route info, side effect
171
+ // write to dist file
172
+ (0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir);
195
173
 
196
- (0, _replace.replaceRoute)(ssgRoutes, pageRoutes); // write routes to spec file
174
+ // format route info, side effect
175
+ (0, _replace.replaceRoute)(ssgRoutes, pageRoutes);
197
176
 
177
+ // write routes to spec file
198
178
  (0, _util.writeJSONSpec)(buildDir, pageRoutes.concat(apiRoutes));
199
-
200
179
  _utils.logger.info('ssg Compiled successfully');
201
180
  }
202
-
203
181
  };
204
182
  }
205
183
  });
206
-
207
184
  exports.default = _default;
@@ -5,19 +5,12 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.makeRender = makeRender;
7
7
  exports.makeRoute = makeRoute;
8
-
9
8
  var _path = _interopRequireDefault(require("path"));
10
-
11
9
  var _normalizePath = _interopRequireDefault(require("normalize-path"));
12
-
13
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
-
15
11
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
16
-
17
12
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
18
-
19
13
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
20
-
21
14
  function makeRender(ssgRoutes, render, port) {
22
15
  return ssgRoutes.map(ssgRoute => render({
23
16
  url: ssgRoute.urlPath,
@@ -27,13 +20,11 @@ function makeRender(ssgRoutes, render, port) {
27
20
  connection: {}
28
21
  }));
29
22
  }
30
-
31
23
  function makeRoute(baseRoute, route, headers = {}) {
32
24
  const {
33
25
  urlPath,
34
26
  entryPath
35
27
  } = baseRoute;
36
-
37
28
  if (typeof route === 'string') {
38
29
  return _objectSpread(_objectSpread({}, baseRoute), {}, {
39
30
  urlPath: (0, _normalizePath.default)(`${urlPath}${route}`) || '/',
@@ -4,23 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.writeHtmlFile = writeHtmlFile;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
10
  function writeHtmlFile(htmlAry, ssgRoutes, baseDir) {
15
11
  htmlAry.forEach((html, index) => {
16
12
  const ssgRoute = ssgRoutes[index];
17
-
18
13
  const filepath = _path.default.join(baseDir, ssgRoute.output);
19
-
20
14
  if (!_utils.fs.existsSync(_path.default.dirname(filepath))) {
21
15
  _utils.fs.ensureDirSync(_path.default.dirname(filepath));
22
16
  }
23
-
24
17
  _utils.fs.writeFileSync(filepath, html);
25
18
  });
26
19
  }
@@ -5,54 +5,41 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.exist = exist;
7
7
  exports.replaceRoute = replaceRoute;
8
-
9
8
  var _normalizePath = _interopRequireDefault(require("normalize-path"));
10
-
11
9
  const _excluded = ["output", "headers"];
12
-
13
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
-
15
11
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
16
-
17
12
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
18
-
19
13
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
20
-
21
14
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
22
-
23
15
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
24
-
25
16
  function exist(route, pageRoutes) {
26
17
  return pageRoutes.slice().findIndex(pageRoute => {
27
18
  const urlEqual = (0, _normalizePath.default)(pageRoute.urlPath) === (0, _normalizePath.default)(route.urlPath);
28
19
  const entryEqual = pageRoute.entryName === route.entryName;
29
-
30
20
  if (urlEqual && entryEqual) {
31
21
  return true;
32
22
  }
33
-
34
23
  return false;
35
24
  });
36
25
  }
37
-
38
26
  function replaceRoute(ssgRoutes, pageRoutes) {
39
27
  // remove redundant fields and replace rendered entryPath
40
28
  const cleanSsgRoutes = ssgRoutes.map(ssgRoute => {
41
29
  const {
42
- output,
43
- headers
44
- } = ssgRoute,
45
- cleanSsgRoute = _objectWithoutProperties(ssgRoute, _excluded);
46
-
30
+ output,
31
+ headers
32
+ } = ssgRoute,
33
+ cleanSsgRoute = _objectWithoutProperties(ssgRoute, _excluded);
47
34
  return Object.assign(cleanSsgRoute, output ? {
48
35
  entryPath: output
49
36
  } : {});
50
- }); // all routes that need to be added and replaced
37
+ });
51
38
 
39
+ // all routes that need to be added and replaced
52
40
  const freshRoutes = [];
53
41
  cleanSsgRoutes.forEach(ssgRoute => {
54
42
  const index = exist(ssgRoute, pageRoutes);
55
-
56
43
  if (index < 0) {
57
44
  // new route
58
45
  freshRoutes.push(_objectSpread({}, ssgRoute));
@@ -10,128 +10,96 @@ exports.getOutput = getOutput;
10
10
  exports.getUrlPrefix = getUrlPrefix;
11
11
  exports.isDynamicUrl = isDynamicUrl;
12
12
  exports.writeJSONSpec = exports.standardOptions = exports.replaceWithAlias = exports.readJSONSpec = exports.openRouteSSR = void 0;
13
-
14
13
  var _path = _interopRequireDefault(require("path"));
15
-
16
14
  var _utils = require("@modern-js/utils");
17
-
18
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
16
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
21
-
22
17
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
23
-
24
18
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
25
-
26
19
  function formatOutput(filename) {
27
20
  const outputPath = _path.default.extname(filename) ? filename : `${filename}/index.html`;
28
21
  return outputPath;
29
22
  }
30
-
31
23
  function formatPath(str) {
32
24
  let addr = str;
33
-
34
25
  if (!addr || typeof addr !== 'string') {
35
26
  return addr;
36
27
  }
37
-
38
28
  if (addr.startsWith('.')) {
39
29
  addr = addr.slice(1);
40
30
  }
41
-
42
31
  if (!addr.startsWith('/')) {
43
32
  addr = `/${addr}`;
44
33
  }
45
-
46
34
  if (addr.endsWith('/') && addr !== '/') {
47
35
  addr = addr.slice(0, addr.length - 1);
48
36
  }
49
-
50
37
  return addr;
51
38
  }
52
-
53
39
  function isDynamicUrl(url) {
54
40
  return url.includes(':');
55
41
  }
56
-
57
42
  function getUrlPrefix(route, baseUrl) {
58
43
  let base = '';
59
-
60
44
  if (Array.isArray(baseUrl)) {
61
45
  const filters = baseUrl.filter(url => route.urlPath.includes(url));
62
-
63
46
  if (filters.length > 1) {
64
- const matched = filters.sort((a, b) => a.length - b.length)[0]; // this should never happened
47
+ const matched = filters.sort((a, b) => a.length - b.length)[0];
65
48
 
49
+ // this should never happened
66
50
  if (!matched) {
67
51
  throw new Error('');
68
52
  }
69
-
70
53
  base = matched;
71
54
  }
72
55
  } else {
73
56
  base = baseUrl;
74
57
  }
75
-
76
58
  base = base === '/' ? '' : base;
77
59
  const entryName = route.entryName === 'main' ? '' : route.entryName;
78
60
  const prefix = `${base}/${entryName}`;
79
61
  return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
80
- } // if no output, return default path for aggred-route(relative),
81
- // or throw error for control-route
82
-
62
+ }
83
63
 
64
+ // if no output, return default path for aggred-route(relative),
65
+ // or throw error for control-route
84
66
  function getOutput(route, base, agreed) {
85
67
  const {
86
68
  output
87
69
  } = route;
88
-
89
70
  if (output) {
90
71
  return output;
91
72
  }
92
-
93
73
  if (agreed) {
94
74
  const urlWithoutBase = route.urlPath.replace(base, '');
95
75
  return urlWithoutBase.startsWith('/') ? urlWithoutBase.slice(1) : urlWithoutBase;
96
76
  }
97
-
98
77
  throw new Error(`routing must provide output when calling createPage(), check ${route.urlPath}`);
99
78
  }
100
-
101
79
  const readJSONSpec = dir => {
102
80
  const routeJSONPath = _path.default.join(dir, _utils.ROUTE_SPEC_FILE);
103
-
104
81
  const routeJSON = require(routeJSONPath);
105
-
106
82
  const {
107
83
  routes
108
84
  } = routeJSON;
109
85
  return routes;
110
86
  };
111
-
112
87
  exports.readJSONSpec = readJSONSpec;
113
-
114
88
  const writeJSONSpec = (dir, routes) => {
115
89
  const routeJSONPath = _path.default.join(dir, _utils.ROUTE_SPEC_FILE);
116
-
117
90
  _utils.fs.writeJSONSync(routeJSONPath, {
118
91
  routes
119
92
  }, {
120
93
  spaces: 2
121
94
  });
122
95
  };
123
-
124
96
  exports.writeJSONSpec = writeJSONSpec;
125
-
126
97
  const replaceWithAlias = (base, filePath, alias) => _path.default.posix.join(alias, _path.default.posix.relative(base, filePath));
127
-
128
98
  exports.replaceWithAlias = replaceWithAlias;
129
-
130
99
  const standardOptions = (ssgOptions, entrypoints, routes, server) => {
131
100
  if (ssgOptions === false) {
132
101
  return false;
133
102
  }
134
-
135
103
  if (ssgOptions === true) {
136
104
  return entrypoints.reduce((opt, entry) => {
137
105
  opt[entry.entryName] = ssgOptions;
@@ -139,7 +107,6 @@ const standardOptions = (ssgOptions, entrypoints, routes, server) => {
139
107
  }, {});
140
108
  } else if (typeof ssgOptions === 'object') {
141
109
  const isSingle = (0, _utils.isSingleEntry)(entrypoints);
142
-
143
110
  if (isSingle && typeof ssgOptions.main === 'undefined') {
144
111
  return {
145
112
  main: ssgOptions
@@ -149,12 +116,11 @@ const standardOptions = (ssgOptions, entrypoints, routes, server) => {
149
116
  }
150
117
  } else if (typeof ssgOptions === 'function') {
151
118
  const intermediateOptions = {};
152
-
153
119
  for (const entrypoint of entrypoints) {
154
120
  const {
155
121
  entryName
156
- } = entrypoint; // TODO: may be async function
157
-
122
+ } = entrypoint;
123
+ // TODO: may be async function
158
124
  if (Array.isArray(server === null || server === void 0 ? void 0 : server.baseUrl)) {
159
125
  for (const url of server.baseUrl) {
160
126
  const matchUrl = entryName === 'main' ? url : `${url}/${entryName}`;
@@ -169,39 +135,32 @@ const standardOptions = (ssgOptions, entrypoints, routes, server) => {
169
135
  });
170
136
  }
171
137
  }
172
-
173
138
  return intermediateOptions;
174
139
  }
175
-
176
140
  return false;
177
141
  };
178
-
179
142
  exports.standardOptions = standardOptions;
180
-
181
143
  const openRouteSSR = (routes, entries = []) => routes.map(ssgRoute => _objectSpread(_objectSpread({}, ssgRoute), {}, {
182
144
  isSSR: entries.includes(ssgRoute.entryName),
183
145
  bundle: `${_utils.SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
184
- })); // TODO: 过滤带有 server loader 的路由
185
-
146
+ }));
186
147
 
148
+ // TODO: 过滤带有 server loader 的路由
187
149
  exports.openRouteSSR = openRouteSSR;
188
-
189
150
  const flattenRoutes = routes => {
190
151
  const parents = [];
191
152
  const newRoutes = [];
192
-
193
153
  const traverseRoute = route => {
194
154
  const parent = parents[parents.length - 1];
195
155
  let path = parent ? `${parent.path}/${route.path || ''}`.replace(/\/+/g, '/') : route.path || ''; // If the route is an index route, the route has no path property
156
+ path = path.replace(/\/$/, '');
196
157
 
197
- path = path.replace(/\/$/, ''); // If the route path is / and is not the root route, it should not be used as an ssg route
198
-
158
+ // If the route path is / and is not the root route, it should not be used as an ssg route
199
159
  if (route._component && (path !== '/' || path === '/' && !parent)) {
200
160
  newRoutes.push(_objectSpread(_objectSpread({}, route), {}, {
201
161
  path
202
162
  }));
203
163
  }
204
-
205
164
  if (route.children) {
206
165
  parents.push(_objectSpread(_objectSpread({}, route), {}, {
207
166
  path
@@ -210,9 +169,7 @@ const flattenRoutes = routes => {
210
169
  parents.pop();
211
170
  }
212
171
  };
213
-
214
172
  routes.forEach(traverseRoute);
215
173
  return newRoutes;
216
174
  };
217
-
218
175
  exports.flattenRoutes = flattenRoutes;
@@ -4,31 +4,22 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.createServer = void 0;
7
-
8
7
  var _child_process = _interopRequireDefault(require("child_process"));
9
-
10
8
  var _path = _interopRequireDefault(require("path"));
11
-
12
9
  var _utils = require("@modern-js/utils");
13
-
14
10
  var _util = require("../libs/util");
15
-
16
11
  var _consts = require("./consts");
17
-
18
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
13
  const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) => new Promise((resolve, reject) => {
21
14
  // this side of the shallow copy of a route for subsequent render processing, to prevent the modification of the current field
22
15
  // manually enable the server-side rendering configuration for all routes that require SSG
23
16
  const entries = ssgRoutes.map(route => route.entryName);
24
17
  const backup = (0, _util.openRouteSSR)(pageRoutes, entries);
25
18
  const total = backup.concat(apiRoutes);
26
-
27
19
  const cp = _child_process.default.fork(_path.default.join(__dirname, 'process'), {
28
20
  cwd: appDirectory,
29
21
  silent: true
30
22
  });
31
-
32
23
  const appContext = api.useAppContext();
33
24
  const plugins = appContext.serverInternalPlugins;
34
25
  cp.send(JSON.stringify({
@@ -48,7 +39,6 @@ const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirecto
48
39
  htmlAry.push(html);
49
40
  htmlChunks.length = 0;
50
41
  }
51
-
52
42
  if (htmlAry.length === ssgRoutes.length) {
53
43
  cp.send(_consts.CLOSE_SIGN);
54
44
  resolve(htmlAry);
@@ -56,10 +46,8 @@ const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirecto
56
46
  });
57
47
  cp.stderr.on('data', chunk => {
58
48
  const str = chunk.toString();
59
-
60
49
  if (str.includes('Error')) {
61
50
  _utils.logger.error(str);
62
-
63
51
  reject(new Error('ssg render failed'));
64
52
  cp.kill('SIGKILL');
65
53
  } else {
@@ -68,10 +56,8 @@ const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirecto
68
56
  });
69
57
  cp.stdout.on('data', chunk => {
70
58
  const str = chunk.toString();
71
-
72
59
  if (str.includes('Error')) {
73
60
  _utils.logger.error(str);
74
-
75
61
  reject(new Error('ssg render failed'));
76
62
  cp.kill('SIGKILL');
77
63
  } else {
@@ -79,5 +65,4 @@ const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirecto
79
65
  }
80
66
  });
81
67
  });
82
-
83
68
  exports.createServer = createServer;
@@ -4,40 +4,28 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.compile = void 0;
7
-
8
7
  var _events = _interopRequireDefault(require("events"));
9
-
10
8
  var _stream = require("stream");
11
-
12
9
  var _nodeMocksHttp = _interopRequireDefault(require("node-mocks-http"));
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
17
-
18
12
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
19
-
20
13
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
-
22
14
  const compile = requestHandler => (options, extend = {}) => new Promise((resolve, reject) => {
23
15
  const req = _nodeMocksHttp.default.createRequest(_objectSpread(_objectSpread({}, options), {}, {
24
16
  eventEmitter: _stream.Readable
25
17
  }));
26
-
27
18
  const res = _nodeMocksHttp.default.createResponse({
28
19
  eventEmitter: _events.default
29
20
  });
30
-
31
21
  Object.assign(req, extend);
32
22
  const proxyRes = new Proxy(res, {
33
23
  get(obj, prop) {
34
24
  if (typeof prop === 'symbol' && !obj[prop]) {
35
25
  return null;
36
26
  }
37
-
38
27
  return obj[prop];
39
28
  }
40
-
41
29
  });
42
30
  res.on('finish', () => {
43
31
  if (res.statusCode !== 200) {
@@ -47,12 +35,10 @@ const compile = requestHandler => (options, extend = {}) => new Promise((resolve
47
35
  }
48
36
  });
49
37
  res.on('error', e => reject(e));
50
-
51
38
  try {
52
39
  requestHandler(req, proxyRes);
53
40
  } catch (e) {
54
41
  reject(e);
55
42
  }
56
43
  });
57
-
58
44
  exports.compile = compile;