@modern-js/plugin-ssg 1.21.5 → 2.0.0-beta.1

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,40 +4,27 @@ 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',
21
+ pre: ['@modern-js/plugin-server', '@modern-js/plugin-bff'],
34
22
  setup: api => {
35
23
  const agreedRouteMap = {};
36
24
  return {
37
25
  validateSchema() {
38
26
  return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
39
27
  },
40
-
41
28
  modifyFileSystemRoutes({
42
29
  entrypoint,
43
30
  routes
@@ -45,13 +32,13 @@ var _default = () => ({
45
32
  const {
46
33
  entryName
47
34
  } = entrypoint;
48
- agreedRouteMap[entryName] = routes;
35
+ const flattedRoutes = (0, _util.flattenRoutes)(routes);
36
+ agreedRouteMap[entryName] = flattedRoutes;
49
37
  return {
50
38
  entrypoint,
51
39
  routes
52
40
  };
53
41
  },
54
-
55
42
  async afterBuild() {
56
43
  const resolvedConfig = api.useResolvedConfigContext();
57
44
  const appContext = api.useAppContext();
@@ -67,31 +54,28 @@ var _default = () => ({
67
54
  ssg,
68
55
  path: outputPath
69
56
  } = output;
70
- const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
71
-
57
+ const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg;
58
+ // no ssg configuration, skip ssg render.
72
59
  if (!ssgOptions) {
73
60
  return;
74
61
  }
75
-
76
62
  const buildDir = _path.default.join(appDirectory, outputPath);
63
+ const routes = (0, _util.readJSONSpec)(buildDir);
77
64
 
78
- const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
79
-
65
+ // filter all routes not web
80
66
  const pageRoutes = routes.filter(route => !route.isApi);
81
- const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
67
+ const apiRoutes = routes.filter(route => route.isApi);
82
68
 
69
+ // if no web page route, skip ssg render
83
70
  if (pageRoutes.length === 0) {
84
71
  return;
85
72
  }
86
-
87
73
  const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints, pageRoutes, server);
88
-
89
74
  if (!intermediateOptions) {
90
75
  return;
91
76
  }
92
-
93
- const ssgRoutes = []; // each route will try to match the configuration
94
-
77
+ const ssgRoutes = [];
78
+ // each route will try to match the configuration
95
79
  pageRoutes.forEach(pageRoute => {
96
80
  const {
97
81
  entryName,
@@ -99,14 +83,13 @@ var _default = () => ({
99
83
  } = pageRoute;
100
84
  const agreedRoutes = agreedRouteMap[entryName];
101
85
  let entryOptions = intermediateOptions[entryName] || intermediateOptions[pageRoute.urlPath];
102
-
103
86
  if (!agreedRoutes) {
104
87
  // default behavior for non-agreed route
105
88
  if (!entryOptions) {
106
89
  return;
107
- } // only add entry route if entryOptions is true
108
-
90
+ }
109
91
 
92
+ // only add entry route if entryOptions is true
110
93
  if (entryOptions === true) {
111
94
  ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
112
95
  output: entryPath
@@ -128,7 +111,6 @@ var _default = () => ({
128
111
  if (!entryOptions) {
129
112
  return;
130
113
  }
131
-
132
114
  if (entryOptions === true) {
133
115
  entryOptions = {
134
116
  preventDefault: [],
@@ -136,13 +118,12 @@ var _default = () => ({
136
118
  headers: {}
137
119
  };
138
120
  }
139
-
140
121
  const {
141
122
  preventDefault = [],
142
123
  routes: userRoutes = [],
143
124
  headers
144
- } = entryOptions; // if the user sets the routes, then only add them
145
-
125
+ } = entryOptions;
126
+ // if the user sets the routes, then only add them
146
127
  if (userRoutes.length > 0) {
147
128
  userRoutes.forEach(route => {
148
129
  if (typeof route === 'string') {
@@ -167,39 +148,35 @@ var _default = () => ({
167
148
  }
168
149
  }
169
150
  });
170
-
171
151
  if (ssgRoutes.length === 0) {
172
152
  return;
173
- } // currently SSG and SSR cannot be turned on at the same time、same route
174
-
153
+ }
175
154
 
155
+ // currently SSG and SSR cannot be turned on at the same time、same route
176
156
  ssgRoutes.forEach(ssgRoute => {
177
157
  if (ssgRoute.isSSR) {
178
158
  const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
179
-
180
159
  if (isOriginRoute) {
181
160
  throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
182
161
  }
183
-
184
162
  _utils.logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
185
163
  }
186
-
187
164
  ssgRoute.isSSR = false;
188
165
  ssgRoute.output = (0, _util.formatOutput)(ssgRoute.output);
189
166
  });
190
- const htmlAry = await (0, _server.createServer)(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
167
+ const htmlAry = await (0, _server.createServer)(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory);
191
168
 
192
- (0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir); // format route info, side effect
169
+ // write to dist file
170
+ (0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir);
193
171
 
194
- (0, _replace.replaceRoute)(ssgRoutes, pageRoutes); // write routes to spec file
172
+ // format route info, side effect
173
+ (0, _replace.replaceRoute)(ssgRoutes, pageRoutes);
195
174
 
175
+ // write routes to spec file
196
176
  (0, _util.writeJSONSpec)(buildDir, pageRoutes.concat(apiRoutes));
197
-
198
177
  _utils.logger.info('ssg Compiled successfully');
199
178
  }
200
-
201
179
  };
202
180
  }
203
181
  });
204
-
205
182
  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));
@@ -3,134 +3,103 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.flattenRoutes = void 0;
6
7
  exports.formatOutput = formatOutput;
7
8
  exports.formatPath = formatPath;
8
9
  exports.getOutput = getOutput;
9
10
  exports.getUrlPrefix = getUrlPrefix;
10
11
  exports.isDynamicUrl = isDynamicUrl;
11
12
  exports.writeJSONSpec = exports.standardOptions = exports.replaceWithAlias = exports.readJSONSpec = exports.openRouteSSR = void 0;
12
-
13
13
  var _path = _interopRequireDefault(require("path"));
14
-
15
14
  var _utils = require("@modern-js/utils");
16
-
17
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
-
19
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; }
20
-
21
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; }
22
-
23
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; }
24
-
25
19
  function formatOutput(filename) {
26
20
  const outputPath = _path.default.extname(filename) ? filename : `${filename}/index.html`;
27
21
  return outputPath;
28
22
  }
29
-
30
23
  function formatPath(str) {
31
24
  let addr = str;
32
-
33
25
  if (!addr || typeof addr !== 'string') {
34
26
  return addr;
35
27
  }
36
-
37
28
  if (addr.startsWith('.')) {
38
29
  addr = addr.slice(1);
39
30
  }
40
-
41
31
  if (!addr.startsWith('/')) {
42
32
  addr = `/${addr}`;
43
33
  }
44
-
45
34
  if (addr.endsWith('/') && addr !== '/') {
46
35
  addr = addr.slice(0, addr.length - 1);
47
36
  }
48
-
49
37
  return addr;
50
38
  }
51
-
52
39
  function isDynamicUrl(url) {
53
40
  return url.includes(':');
54
41
  }
55
-
56
42
  function getUrlPrefix(route, baseUrl) {
57
43
  let base = '';
58
-
59
44
  if (Array.isArray(baseUrl)) {
60
45
  const filters = baseUrl.filter(url => route.urlPath.includes(url));
61
-
62
46
  if (filters.length > 1) {
63
- 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];
64
48
 
49
+ // this should never happened
65
50
  if (!matched) {
66
51
  throw new Error('');
67
52
  }
68
-
69
53
  base = matched;
70
54
  }
71
55
  } else {
72
56
  base = baseUrl;
73
57
  }
74
-
75
58
  base = base === '/' ? '' : base;
76
59
  const entryName = route.entryName === 'main' ? '' : route.entryName;
77
60
  const prefix = `${base}/${entryName}`;
78
61
  return prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;
79
- } // if no output, return default path for aggred-route(relative),
80
- // or throw error for control-route
81
-
62
+ }
82
63
 
64
+ // if no output, return default path for aggred-route(relative),
65
+ // or throw error for control-route
83
66
  function getOutput(route, base, agreed) {
84
67
  const {
85
68
  output
86
69
  } = route;
87
-
88
70
  if (output) {
89
71
  return output;
90
72
  }
91
-
92
73
  if (agreed) {
93
74
  const urlWithoutBase = route.urlPath.replace(base, '');
94
75
  return urlWithoutBase.startsWith('/') ? urlWithoutBase.slice(1) : urlWithoutBase;
95
76
  }
96
-
97
77
  throw new Error(`routing must provide output when calling createPage(), check ${route.urlPath}`);
98
78
  }
99
-
100
79
  const readJSONSpec = dir => {
101
80
  const routeJSONPath = _path.default.join(dir, _utils.ROUTE_SPEC_FILE);
102
-
103
81
  const routeJSON = require(routeJSONPath);
104
-
105
82
  const {
106
83
  routes
107
84
  } = routeJSON;
108
85
  return routes;
109
86
  };
110
-
111
87
  exports.readJSONSpec = readJSONSpec;
112
-
113
88
  const writeJSONSpec = (dir, routes) => {
114
89
  const routeJSONPath = _path.default.join(dir, _utils.ROUTE_SPEC_FILE);
115
-
116
90
  _utils.fs.writeJSONSync(routeJSONPath, {
117
91
  routes
118
92
  }, {
119
93
  spaces: 2
120
94
  });
121
95
  };
122
-
123
96
  exports.writeJSONSpec = writeJSONSpec;
124
-
125
97
  const replaceWithAlias = (base, filePath, alias) => _path.default.posix.join(alias, _path.default.posix.relative(base, filePath));
126
-
127
98
  exports.replaceWithAlias = replaceWithAlias;
128
-
129
99
  const standardOptions = (ssgOptions, entrypoints, routes, server) => {
130
100
  if (ssgOptions === false) {
131
101
  return false;
132
102
  }
133
-
134
103
  if (ssgOptions === true) {
135
104
  return entrypoints.reduce((opt, entry) => {
136
105
  opt[entry.entryName] = ssgOptions;
@@ -138,7 +107,6 @@ const standardOptions = (ssgOptions, entrypoints, routes, server) => {
138
107
  }, {});
139
108
  } else if (typeof ssgOptions === 'object') {
140
109
  const isSingle = (0, _utils.isSingleEntry)(entrypoints);
141
-
142
110
  if (isSingle && typeof ssgOptions.main === 'undefined') {
143
111
  return {
144
112
  main: ssgOptions
@@ -148,12 +116,11 @@ const standardOptions = (ssgOptions, entrypoints, routes, server) => {
148
116
  }
149
117
  } else if (typeof ssgOptions === 'function') {
150
118
  const intermediateOptions = {};
151
-
152
119
  for (const entrypoint of entrypoints) {
153
120
  const {
154
121
  entryName
155
- } = entrypoint; // TODO: may be async function
156
-
122
+ } = entrypoint;
123
+ // TODO: may be async function
157
124
  if (Array.isArray(server === null || server === void 0 ? void 0 : server.baseUrl)) {
158
125
  for (const url of server.baseUrl) {
159
126
  const matchUrl = entryName === 'main' ? url : `${url}/${entryName}`;
@@ -168,18 +135,41 @@ const standardOptions = (ssgOptions, entrypoints, routes, server) => {
168
135
  });
169
136
  }
170
137
  }
171
-
172
138
  return intermediateOptions;
173
139
  }
174
-
175
140
  return false;
176
141
  };
177
-
178
142
  exports.standardOptions = standardOptions;
179
-
180
143
  const openRouteSSR = (routes, entries = []) => routes.map(ssgRoute => _objectSpread(_objectSpread({}, ssgRoute), {}, {
181
144
  isSSR: entries.includes(ssgRoute.entryName),
182
145
  bundle: `${_utils.SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
183
146
  }));
184
147
 
185
- exports.openRouteSSR = openRouteSSR;
148
+ // TODO: 过滤带有 server loader 的路由
149
+ exports.openRouteSSR = openRouteSSR;
150
+ const flattenRoutes = routes => {
151
+ const parents = [];
152
+ const newRoutes = [];
153
+ const traverseRoute = route => {
154
+ const parent = parents[parents.length - 1];
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(/\/$/, '');
157
+
158
+ // If the route path is / and is not the root route, it should not be used as an ssg route
159
+ if (route._component && (path !== '/' || path === '/' && !parent)) {
160
+ newRoutes.push(_objectSpread(_objectSpread({}, route), {}, {
161
+ path
162
+ }));
163
+ }
164
+ if (route.children) {
165
+ parents.push(_objectSpread(_objectSpread({}, route), {}, {
166
+ path
167
+ }));
168
+ route.children.forEach(traverseRoute);
169
+ parents.pop();
170
+ }
171
+ };
172
+ routes.forEach(traverseRoute);
173
+ return newRoutes;
174
+ };
175
+ exports.flattenRoutes = flattenRoutes;
@@ -4,33 +4,24 @@ 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
- const plugins = appContext.plugins.filter(p => p.server).map(p => p.server);
24
+ const plugins = appContext.serverInternalPlugins;
34
25
  cp.send(JSON.stringify({
35
26
  options,
36
27
  renderRoutes: ssgRoutes,
@@ -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;