@modern-js/plugin-ssg 1.2.3 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # @modern-js/plugin-ssg
2
2
 
3
+ ## 1.2.6
4
+
5
+ ### Patch Changes
6
+
7
+ - c7dc7f54: migrate to new plugin style
8
+ - Updated dependencies [5bf5868d]
9
+ - @modern-js/utils@1.3.5
10
+
11
+ ## 1.2.5
12
+
13
+ ### Patch Changes
14
+
15
+ - 73306c0d: fix ssg type
16
+ - 59010b7a: rewrite server lifecycle, add unit test
17
+ - aed9912e: fix: output.ssg type error
18
+ - Updated dependencies [cc5e8001]
19
+ - Updated dependencies [2520ea86]
20
+ - Updated dependencies [db43dce6]
21
+ - Updated dependencies [e81fd9b7]
22
+ - Updated dependencies [1c411e71]
23
+ - @modern-js/core@1.4.6
24
+ - @modern-js/utils@1.3.4
25
+
26
+ ## 1.2.4
27
+
28
+ ### Patch Changes
29
+
30
+ - 02fb4146: support product server
31
+ - cc8501c1: fix ssg build after modify server
32
+ - Updated dependencies [969f172f]
33
+ - Updated dependencies [4c792f68]
34
+ - Updated dependencies [4b5d4bf4]
35
+ - Updated dependencies [62f5b8c8]
36
+ - Updated dependencies [55e18278]
37
+ - Updated dependencies [4499a674]
38
+ - Updated dependencies [403f5169]
39
+ - Updated dependencies [a7f42f48]
40
+ - @modern-js/core@1.4.4
41
+ - @modern-js/utils@1.3.3
42
+
3
43
  ## 1.2.3
4
44
 
5
45
  ### Patch Changes
@@ -6,183 +6,179 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
6
6
 
7
7
  import path from 'path';
8
8
  import { logger, PLUGIN_SCHEMAS } from '@modern-js/utils';
9
- import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
10
9
  import { generatePath } from 'react-router-dom';
11
10
  import { formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util";
12
11
  import { createServer } from "./server";
13
12
  import { writeHtmlFile } from "./libs/output";
14
13
  import { replaceRoute } from "./libs/replace";
15
14
  import { makeRoute } from "./libs/make";
16
- export default createPlugin(() => {
17
- const agreedRouteMap = {};
18
- return {
19
- validateSchema() {
20
- return PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
21
- },
22
-
23
- modifyFileSystemRoutes({
24
- entrypoint,
25
- routes
26
- }) {
27
- const {
28
- entryName
29
- } = entrypoint;
30
- agreedRouteMap[entryName] = routes;
31
- return {
15
+ export default (() => ({
16
+ name: '@modern-js/plugin-ssg',
17
+ setup: api => {
18
+ const agreedRouteMap = {};
19
+ return {
20
+ validateSchema() {
21
+ return PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
22
+ },
23
+
24
+ modifyFileSystemRoutes({
32
25
  entrypoint,
33
26
  routes
34
- };
35
- },
36
-
37
- // eslint-disable-next-line max-statements
38
- async afterBuild() {
39
- // eslint-disable-next-line react-hooks/rules-of-hooks
40
- const resolvedConfig = useResolvedConfigContext(); // eslint-disable-next-line react-hooks/rules-of-hooks
41
-
42
- const appContext = useAppContext();
43
- const {
44
- appDirectory,
45
- entrypoints
46
- } = appContext;
47
- const {
48
- output
49
- } = resolvedConfig;
50
- const {
51
- ssg,
52
- path: outputPath
53
- } = output;
54
- const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
55
-
56
- if (!ssgOptions) {
57
- return;
58
- }
27
+ }) {
28
+ const {
29
+ entryName
30
+ } = entrypoint;
31
+ agreedRouteMap[entryName] = routes;
32
+ return {
33
+ entrypoint,
34
+ routes
35
+ };
36
+ },
37
+
38
+ // eslint-disable-next-line max-statements
39
+ async afterBuild() {
40
+ const resolvedConfig = api.useResolvedConfigContext();
41
+ const appContext = api.useAppContext();
42
+ const {
43
+ appDirectory,
44
+ entrypoints
45
+ } = appContext;
46
+ const {
47
+ output
48
+ } = resolvedConfig;
49
+ const {
50
+ ssg,
51
+ path: outputPath
52
+ } = output;
53
+ const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
59
54
 
60
- const buildDir = path.join(appDirectory, outputPath);
61
- const routes = readJSONSpec(buildDir); // filter all routes not web
55
+ if (!ssgOptions) {
56
+ return;
57
+ }
62
58
 
63
- const pageRoutes = routes.filter(route => !route.isApi);
64
- const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
59
+ const buildDir = path.join(appDirectory, outputPath);
60
+ const routes = readJSONSpec(buildDir); // filter all routes not web
65
61
 
66
- if (pageRoutes.length === 0) {
67
- return;
68
- }
62
+ const pageRoutes = routes.filter(route => !route.isApi);
63
+ const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
69
64
 
70
- const intermediateOptions = standardOptions(ssgOptions, entrypoints);
71
-
72
- if (!intermediateOptions) {
73
- return;
74
- }
65
+ if (pageRoutes.length === 0) {
66
+ return;
67
+ }
75
68
 
76
- const ssgRoutes = []; // each route will try to match the configuration
69
+ const intermediateOptions = standardOptions(ssgOptions, entrypoints);
77
70
 
78
- pageRoutes.forEach(pageRoute => {
79
- const {
80
- entryName,
81
- entryPath
82
- } = pageRoute;
83
- const agreedRoutes = agreedRouteMap[entryName];
84
- let entryOptions = intermediateOptions[entryName];
85
-
86
- if (!agreedRoutes) {
87
- var _entryOptions$routes;
88
-
89
- // default behavior for non-agreed route
90
- if (!entryOptions) {
91
- return;
92
- } // only add entry route if entryOptions is true
93
-
94
-
95
- if (entryOptions === true) {
96
- ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
97
- output: entryPath
98
- }));
99
- } else if (((_entryOptions$routes = entryOptions.routes) === null || _entryOptions$routes === void 0 ? void 0 : _entryOptions$routes.length) > 0) {
100
- // if entryOptions is object and has routes options
101
- // add every route in options
102
- const {
103
- routes: enrtyRoutes,
104
- headers
105
- } = entryOptions;
106
- enrtyRoutes.forEach(route => {
107
- ssgRoutes.push(makeRoute(pageRoute, route, headers));
108
- });
109
- }
110
- } else {
111
- // Unless entryOptions is set to false
112
- // the default behavior is to add all file-based routes
113
- if (entryOptions === false) {
114
- return;
115
- }
71
+ if (!intermediateOptions) {
72
+ return;
73
+ }
116
74
 
117
- if (!entryOptions || entryOptions === true) {
118
- entryOptions = {
119
- preventDefault: [],
120
- routes: [],
121
- headers: {}
122
- };
123
- }
75
+ const ssgRoutes = []; // each route will try to match the configuration
124
76
 
77
+ pageRoutes.forEach(pageRoute => {
125
78
  const {
126
- preventDefault = [],
127
- routes: userRoutes = [],
128
- headers
129
- } = entryOptions; // if the user sets the routes, then only add them
130
-
131
- if (userRoutes.length > 0) {
132
- userRoutes.forEach(route => {
133
- if (typeof route === 'string') {
79
+ entryName,
80
+ entryPath
81
+ } = pageRoute;
82
+ const agreedRoutes = agreedRouteMap[entryName];
83
+ let entryOptions = intermediateOptions[entryName];
84
+
85
+ if (!agreedRoutes) {
86
+ // default behavior for non-agreed route
87
+ if (!entryOptions) {
88
+ return;
89
+ } // only add entry route if entryOptions is true
90
+
91
+
92
+ if (entryOptions === true) {
93
+ ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
94
+ output: entryPath
95
+ }));
96
+ } else if (entryOptions.routes && entryOptions.routes.length > 0) {
97
+ // if entryOptions is object and has routes options
98
+ // add every route in options
99
+ const {
100
+ routes: enrtyRoutes,
101
+ headers
102
+ } = entryOptions;
103
+ enrtyRoutes.forEach(route => {
134
104
  ssgRoutes.push(makeRoute(pageRoute, route, headers));
135
- } else if (Array.isArray(route.params)) {
136
- route.params.forEach(param => {
137
- ssgRoutes.push(makeRoute(pageRoute, _objectSpread(_objectSpread({}, route), {}, {
138
- url: generatePath(route.url, param)
139
- }), headers));
140
- });
141
- } else {
142
- ssgRoutes.push(makeRoute(pageRoute, route, headers));
143
- }
144
- });
105
+ });
106
+ }
145
107
  } else {
146
- // otherwith add all except dynamic routes
147
- agreedRoutes.filter(route => !preventDefault.includes(route.path)).forEach(route => {
148
- if (!isDynamicUrl(route.path)) {
149
- ssgRoutes.push(makeRoute(pageRoute, route.path, headers));
150
- }
151
- });
108
+ // Unless entryOptions is set to false
109
+ // the default behavior is to add all file-based routes
110
+ if (entryOptions === false) {
111
+ return;
112
+ }
113
+
114
+ if (!entryOptions || entryOptions === true) {
115
+ entryOptions = {
116
+ preventDefault: [],
117
+ routes: [],
118
+ headers: {}
119
+ };
120
+ }
121
+
122
+ const {
123
+ preventDefault = [],
124
+ routes: userRoutes = [],
125
+ headers
126
+ } = entryOptions; // if the user sets the routes, then only add them
127
+
128
+ if (userRoutes.length > 0) {
129
+ userRoutes.forEach(route => {
130
+ if (typeof route === 'string') {
131
+ ssgRoutes.push(makeRoute(pageRoute, route, headers));
132
+ } else if (Array.isArray(route.params)) {
133
+ route.params.forEach(param => {
134
+ ssgRoutes.push(makeRoute(pageRoute, _objectSpread(_objectSpread({}, route), {}, {
135
+ url: generatePath(route.url, param)
136
+ }), headers));
137
+ });
138
+ } else {
139
+ ssgRoutes.push(makeRoute(pageRoute, route, headers));
140
+ }
141
+ });
142
+ } else {
143
+ // otherwith add all except dynamic routes
144
+ agreedRoutes.filter(route => !preventDefault.includes(route.path)).forEach(route => {
145
+ if (!isDynamicUrl(route.path)) {
146
+ ssgRoutes.push(makeRoute(pageRoute, route.path, headers));
147
+ }
148
+ });
149
+ }
152
150
  }
153
- }
154
- });
151
+ });
155
152
 
156
- if (ssgRoutes.length === 0) {
157
- return;
158
- } // currently SSG and SSR cannot be turned on at the same time、same route
153
+ if (ssgRoutes.length === 0) {
154
+ return;
155
+ } // currently SSG and SSR cannot be turned on at the same time、same route
159
156
 
160
157
 
161
- ssgRoutes.forEach(ssgRoute => {
162
- if (ssgRoute.isSSR) {
163
- const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
158
+ ssgRoutes.forEach(ssgRoute => {
159
+ if (ssgRoute.isSSR) {
160
+ const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
164
161
 
165
- if (isOriginRoute) {
166
- throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
167
- }
162
+ if (isOriginRoute) {
163
+ throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
164
+ }
168
165
 
169
- logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
170
- }
166
+ logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
167
+ }
171
168
 
172
- ssgRoute.isSSR = false;
173
- ssgRoute.output = formatOutput(ssgRoute.output);
174
- });
175
- const htmlAry = await createServer(ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
169
+ ssgRoute.isSSR = false;
170
+ ssgRoute.output = formatOutput(ssgRoute.output);
171
+ });
172
+ const htmlAry = await createServer(ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
176
173
 
177
- writeHtmlFile(htmlAry, ssgRoutes, buildDir); // format route info, side effect
174
+ writeHtmlFile(htmlAry, ssgRoutes, buildDir); // format route info, side effect
178
175
 
179
- replaceRoute(ssgRoutes, pageRoutes); // write routes to spec file
176
+ replaceRoute(ssgRoutes, pageRoutes); // write routes to spec file
180
177
 
181
- writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
182
- logger.info('ssg Compiled successfully');
183
- }
178
+ writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
179
+ logger.info('ssg Compiled successfully');
180
+ }
184
181
 
185
- };
186
- }, {
187
- name: '@modern-js/plugin-ssg'
188
- });
182
+ };
183
+ }
184
+ }));
@@ -1,4 +1,4 @@
1
- import Server from '@modern-js/server';
1
+ import server from '@modern-js/prod-server';
2
2
  import portfinder from 'portfinder';
3
3
  import { compatRequire } from '@modern-js/utils';
4
4
  import { makeRender } from "../libs/make";
@@ -36,13 +36,13 @@ process.on('message', async chunk => {
36
36
 
37
37
  try {
38
38
  const {
39
- server
39
+ server: serverOptions
40
40
  } = options; // start server in default port
41
41
 
42
- const defaultPort = Number(process.env.PORT) || server.port;
42
+ const defaultPort = Number(process.env.PORT) || serverOptions.port;
43
43
  portfinder.basePort = defaultPort;
44
44
  const port = await portfinder.getPortPromise();
45
- modernServer = await Server({
45
+ modernServer = await server({
46
46
  pwd: appDirectory,
47
47
  config: options,
48
48
  routes,
@@ -1 +1 @@
1
- export {};
1
+ import '@modern-js/core';
@@ -9,8 +9,6 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- var _core = require("@modern-js/core");
13
-
14
12
  var _reactRouterDom = require("react-router-dom");
15
13
 
16
14
  var _util = require("./libs/util");
@@ -31,180 +29,177 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
31
29
 
32
30
  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; }
33
31
 
34
- var _default = (0, _core.createPlugin)(() => {
35
- const agreedRouteMap = {};
36
- return {
37
- validateSchema() {
38
- return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
39
- },
40
-
41
- modifyFileSystemRoutes({
42
- entrypoint,
43
- routes
44
- }) {
45
- const {
46
- entryName
47
- } = entrypoint;
48
- agreedRouteMap[entryName] = routes;
49
- return {
32
+ var _default = () => ({
33
+ name: '@modern-js/plugin-ssg',
34
+ setup: api => {
35
+ const agreedRouteMap = {};
36
+ return {
37
+ validateSchema() {
38
+ return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
39
+ },
40
+
41
+ modifyFileSystemRoutes({
50
42
  entrypoint,
51
43
  routes
52
- };
53
- },
54
-
55
- // eslint-disable-next-line max-statements
56
- async afterBuild() {
57
- // eslint-disable-next-line react-hooks/rules-of-hooks
58
- const resolvedConfig = (0, _core.useResolvedConfigContext)(); // eslint-disable-next-line react-hooks/rules-of-hooks
59
-
60
- const appContext = (0, _core.useAppContext)();
61
- const {
62
- appDirectory,
63
- entrypoints
64
- } = appContext;
65
- const {
66
- output
67
- } = resolvedConfig;
68
- const {
69
- ssg,
70
- path: outputPath
71
- } = output;
72
- const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
73
-
74
- if (!ssgOptions) {
75
- return;
76
- }
77
-
78
- const buildDir = _path.default.join(appDirectory, outputPath);
44
+ }) {
45
+ const {
46
+ entryName
47
+ } = entrypoint;
48
+ agreedRouteMap[entryName] = routes;
49
+ return {
50
+ entrypoint,
51
+ routes
52
+ };
53
+ },
54
+
55
+ // eslint-disable-next-line max-statements
56
+ async afterBuild() {
57
+ const resolvedConfig = api.useResolvedConfigContext();
58
+ const appContext = api.useAppContext();
59
+ const {
60
+ appDirectory,
61
+ entrypoints
62
+ } = appContext;
63
+ const {
64
+ output
65
+ } = resolvedConfig;
66
+ const {
67
+ ssg,
68
+ path: outputPath
69
+ } = output;
70
+ const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
79
71
 
80
- const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
72
+ if (!ssgOptions) {
73
+ return;
74
+ }
81
75
 
82
- const pageRoutes = routes.filter(route => !route.isApi);
83
- const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
76
+ const buildDir = _path.default.join(appDirectory, outputPath);
84
77
 
85
- if (pageRoutes.length === 0) {
86
- return;
87
- }
78
+ const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
88
79
 
89
- const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints);
80
+ const pageRoutes = routes.filter(route => !route.isApi);
81
+ const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
90
82
 
91
- if (!intermediateOptions) {
92
- return;
93
- }
83
+ if (pageRoutes.length === 0) {
84
+ return;
85
+ }
94
86
 
95
- const ssgRoutes = []; // each route will try to match the configuration
87
+ const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints);
96
88
 
97
- pageRoutes.forEach(pageRoute => {
98
- const {
99
- entryName,
100
- entryPath
101
- } = pageRoute;
102
- const agreedRoutes = agreedRouteMap[entryName];
103
- let entryOptions = intermediateOptions[entryName];
104
-
105
- if (!agreedRoutes) {
106
- var _entryOptions$routes;
107
-
108
- // default behavior for non-agreed route
109
- if (!entryOptions) {
110
- return;
111
- } // only add entry route if entryOptions is true
112
-
113
-
114
- if (entryOptions === true) {
115
- ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
116
- output: entryPath
117
- }));
118
- } else if (((_entryOptions$routes = entryOptions.routes) === null || _entryOptions$routes === void 0 ? void 0 : _entryOptions$routes.length) > 0) {
119
- // if entryOptions is object and has routes options
120
- // add every route in options
121
- const {
122
- routes: enrtyRoutes,
123
- headers
124
- } = entryOptions;
125
- enrtyRoutes.forEach(route => {
126
- ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
127
- });
128
- }
129
- } else {
130
- // Unless entryOptions is set to false
131
- // the default behavior is to add all file-based routes
132
- if (entryOptions === false) {
133
- return;
134
- }
89
+ if (!intermediateOptions) {
90
+ return;
91
+ }
135
92
 
136
- if (!entryOptions || entryOptions === true) {
137
- entryOptions = {
138
- preventDefault: [],
139
- routes: [],
140
- headers: {}
141
- };
142
- }
93
+ const ssgRoutes = []; // each route will try to match the configuration
143
94
 
95
+ pageRoutes.forEach(pageRoute => {
144
96
  const {
145
- preventDefault = [],
146
- routes: userRoutes = [],
147
- headers
148
- } = entryOptions; // if the user sets the routes, then only add them
149
-
150
- if (userRoutes.length > 0) {
151
- userRoutes.forEach(route => {
152
- if (typeof route === 'string') {
153
- ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
154
- } else if (Array.isArray(route.params)) {
155
- route.params.forEach(param => {
156
- ssgRoutes.push((0, _make.makeRoute)(pageRoute, _objectSpread(_objectSpread({}, route), {}, {
157
- url: (0, _reactRouterDom.generatePath)(route.url, param)
158
- }), headers));
159
- });
160
- } else {
97
+ entryName,
98
+ entryPath
99
+ } = pageRoute;
100
+ const agreedRoutes = agreedRouteMap[entryName];
101
+ let entryOptions = intermediateOptions[entryName];
102
+
103
+ if (!agreedRoutes) {
104
+ // default behavior for non-agreed route
105
+ if (!entryOptions) {
106
+ return;
107
+ } // only add entry route if entryOptions is true
108
+
109
+
110
+ if (entryOptions === true) {
111
+ ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
112
+ output: entryPath
113
+ }));
114
+ } else if (entryOptions.routes && entryOptions.routes.length > 0) {
115
+ // if entryOptions is object and has routes options
116
+ // add every route in options
117
+ const {
118
+ routes: enrtyRoutes,
119
+ headers
120
+ } = entryOptions;
121
+ enrtyRoutes.forEach(route => {
161
122
  ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
162
- }
163
- });
123
+ });
124
+ }
164
125
  } else {
165
- // otherwith add all except dynamic routes
166
- agreedRoutes.filter(route => !preventDefault.includes(route.path)).forEach(route => {
167
- if (!(0, _util.isDynamicUrl)(route.path)) {
168
- ssgRoutes.push((0, _make.makeRoute)(pageRoute, route.path, headers));
169
- }
170
- });
126
+ // Unless entryOptions is set to false
127
+ // the default behavior is to add all file-based routes
128
+ if (entryOptions === false) {
129
+ return;
130
+ }
131
+
132
+ if (!entryOptions || entryOptions === true) {
133
+ entryOptions = {
134
+ preventDefault: [],
135
+ routes: [],
136
+ headers: {}
137
+ };
138
+ }
139
+
140
+ const {
141
+ preventDefault = [],
142
+ routes: userRoutes = [],
143
+ headers
144
+ } = entryOptions; // if the user sets the routes, then only add them
145
+
146
+ if (userRoutes.length > 0) {
147
+ userRoutes.forEach(route => {
148
+ if (typeof route === 'string') {
149
+ ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
150
+ } else if (Array.isArray(route.params)) {
151
+ route.params.forEach(param => {
152
+ ssgRoutes.push((0, _make.makeRoute)(pageRoute, _objectSpread(_objectSpread({}, route), {}, {
153
+ url: (0, _reactRouterDom.generatePath)(route.url, param)
154
+ }), headers));
155
+ });
156
+ } else {
157
+ ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
158
+ }
159
+ });
160
+ } else {
161
+ // otherwith add all except dynamic routes
162
+ agreedRoutes.filter(route => !preventDefault.includes(route.path)).forEach(route => {
163
+ if (!(0, _util.isDynamicUrl)(route.path)) {
164
+ ssgRoutes.push((0, _make.makeRoute)(pageRoute, route.path, headers));
165
+ }
166
+ });
167
+ }
171
168
  }
172
- }
173
- });
169
+ });
174
170
 
175
- if (ssgRoutes.length === 0) {
176
- return;
177
- } // currently SSG and SSR cannot be turned on at the same time、same route
171
+ if (ssgRoutes.length === 0) {
172
+ return;
173
+ } // currently SSG and SSR cannot be turned on at the same time、same route
178
174
 
179
175
 
180
- ssgRoutes.forEach(ssgRoute => {
181
- if (ssgRoute.isSSR) {
182
- const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
176
+ ssgRoutes.forEach(ssgRoute => {
177
+ if (ssgRoute.isSSR) {
178
+ const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
183
179
 
184
- if (isOriginRoute) {
185
- throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
186
- }
180
+ if (isOriginRoute) {
181
+ throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
182
+ }
187
183
 
188
- _utils.logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
189
- }
184
+ _utils.logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
185
+ }
190
186
 
191
- ssgRoute.isSSR = false;
192
- ssgRoute.output = (0, _util.formatOutput)(ssgRoute.output);
193
- });
194
- const htmlAry = await (0, _server.createServer)(ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
187
+ ssgRoute.isSSR = false;
188
+ ssgRoute.output = (0, _util.formatOutput)(ssgRoute.output);
189
+ });
190
+ const htmlAry = await (0, _server.createServer)(ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
195
191
 
196
- (0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir); // format route info, side effect
192
+ (0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir); // format route info, side effect
197
193
 
198
- (0, _replace.replaceRoute)(ssgRoutes, pageRoutes); // write routes to spec file
194
+ (0, _replace.replaceRoute)(ssgRoutes, pageRoutes); // write routes to spec file
199
195
 
200
- (0, _util.writeJSONSpec)(buildDir, pageRoutes.concat(apiRoutes));
196
+ (0, _util.writeJSONSpec)(buildDir, pageRoutes.concat(apiRoutes));
201
197
 
202
- _utils.logger.info('ssg Compiled successfully');
203
- }
198
+ _utils.logger.info('ssg Compiled successfully');
199
+ }
204
200
 
205
- };
206
- }, {
207
- name: '@modern-js/plugin-ssg'
201
+ };
202
+ }
208
203
  });
209
204
 
210
205
  exports.default = _default;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- var _server = _interopRequireDefault(require("@modern-js/server"));
3
+ var _prodServer = _interopRequireDefault(require("@modern-js/prod-server"));
4
4
 
5
5
  var _portfinder = _interopRequireDefault(require("portfinder"));
6
6
 
@@ -45,13 +45,13 @@ process.on('message', async chunk => {
45
45
 
46
46
  try {
47
47
  const {
48
- server
48
+ server: serverOptions
49
49
  } = options; // start server in default port
50
50
 
51
- const defaultPort = Number(process.env.PORT) || server.port;
51
+ const defaultPort = Number(process.env.PORT) || serverOptions.port;
52
52
  _portfinder.default.basePort = defaultPort;
53
53
  const port = await _portfinder.default.getPortPromise();
54
- modernServer = await (0, _server.default)({
54
+ modernServer = await (0, _prodServer.default)({
55
55
  pwd: appDirectory,
56
56
  config: options,
57
57
  routes,
@@ -1,5 +1,3 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
3
+ require("@modern-js/core");
@@ -1,3 +1,5 @@
1
- declare const _default: any;
1
+ import type { CliPlugin } from '@modern-js/core';
2
+
3
+ declare const _default: () => CliPlugin;
2
4
 
3
5
  export default _default;
@@ -1,4 +1,5 @@
1
1
  import { ServerRoute as ModernRoute } from '@modern-js/types';
2
+ import '@modern-js/core';
2
3
  export declare type AgreedRoute = {
3
4
  path: string;
4
5
  component: string;
@@ -25,10 +26,15 @@ export declare type RouteOptions = string | {
25
26
  export declare type SingleEntryOptions = boolean | {
26
27
  preventDefault?: string[];
27
28
  headers?: Record<string, any>;
28
- routes: RouteOptions[];
29
+ routes?: RouteOptions[];
29
30
  };
30
31
  export declare type MultiEntryOptions = Record<string, SingleEntryOptions>;
31
32
  export declare type SSG = boolean | SingleEntryOptions | MultiEntryOptions | ((entryName: string) => SingleEntryOptions);
32
33
  export declare type ExtendOutputConfig = {
33
34
  ssg: SSG;
34
- };
35
+ };
36
+ declare module '@modern-js/core' {
37
+ interface OutputConfig {
38
+ ssg?: SSG;
39
+ }
40
+ }
package/package.json CHANGED
@@ -11,12 +11,22 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.3",
14
+ "version": "1.2.6",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
18
18
  "module": "./dist/js/treeshaking/index.js",
19
19
  "jsnext:modern": "./dist/js/modern/index.js",
20
+ "typesVersions": {
21
+ "*": {
22
+ ".": [
23
+ "./dist/types/index.d.ts"
24
+ ],
25
+ "types": [
26
+ "./dist/types/types.d.ts"
27
+ ]
28
+ }
29
+ },
20
30
  "exports": {
21
31
  ".": {
22
32
  "node": {
@@ -33,19 +43,27 @@
33
43
  "require": "./dist/js/node/index.js"
34
44
  },
35
45
  "default": "./dist/js/treeshaking/index.js"
46
+ },
47
+ "./types": {
48
+ "node": {
49
+ "import": "./dist/js/modern/types.js",
50
+ "require": "./dist/js/node/types.js",
51
+ "types": "./dist/types/types.d.ts"
52
+ },
53
+ "default": "./dist/js/treeshaking/types.js"
36
54
  }
37
55
  },
38
56
  "dependencies": {
39
57
  "@babel/runtime": "^7",
40
- "@modern-js/utils": "^1.3.1",
58
+ "@modern-js/utils": "^1.3.5",
41
59
  "node-mocks-http": "^1.10.1",
42
60
  "normalize-path": "^3.0.0",
43
61
  "portfinder": "^1.0.28",
44
62
  "react-router-dom": "^5.2.1"
45
63
  },
46
64
  "devDependencies": {
47
- "@modern-js/server": "^1.4.1",
48
- "@modern-js/types": "^1.3.1",
65
+ "@modern-js/types": "^1.3.5",
66
+ "@modern-js/prod-server": "^1.0.4",
49
67
  "@types/jest": "^26",
50
68
  "@types/node": "^14",
51
69
  "@types/react": "^17",
@@ -53,14 +71,11 @@
53
71
  "@types/react-router": "^5.1.16",
54
72
  "@types/react-router-dom": "^5.1.8",
55
73
  "typescript": "^4",
56
- "@modern-js/core": "^1.4.1",
74
+ "@modern-js/core": "^1.5.0",
57
75
  "@scripts/build": "0.0.0",
58
76
  "jest": "^27",
59
77
  "@scripts/jest-config": "0.0.0"
60
78
  },
61
- "peerDependencies": {
62
- "@modern-js/core": "^1.4.1"
63
- },
64
79
  "sideEffects": false,
65
80
  "modernConfig": {
66
81
  "output": {
@@ -3,5 +3,6 @@ import plugin from '../src';
3
3
  describe('plugin-ssg', () => {
4
4
  it('default', () => {
5
5
  expect(plugin).toBeDefined();
6
+ expect(plugin().name).toBe('@modern-js/plugin-ssg');
6
7
  });
7
8
  });