@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 +40 -0
- package/dist/js/modern/index.js +146 -150
- package/dist/js/modern/server/process.js +4 -4
- package/dist/js/modern/types.js +1 -1
- package/dist/js/node/index.js +145 -150
- package/dist/js/node/server/process.js +4 -4
- package/dist/js/node/types.js +1 -3
- package/dist/types/index.d.ts +3 -1
- package/dist/types/types.d.ts +8 -2
- package/package.json +23 -8
- package/tests/index.test.ts +1 -0
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
|
package/dist/js/modern/index.js
CHANGED
|
@@ -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
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
61
|
-
|
|
55
|
+
if (!ssgOptions) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
const buildDir = path.join(appDirectory, outputPath);
|
|
60
|
+
const routes = readJSONSpec(buildDir); // filter all routes not web
|
|
65
61
|
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
65
|
+
if (pageRoutes.length === 0) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
75
68
|
|
|
76
|
-
|
|
69
|
+
const intermediateOptions = standardOptions(ssgOptions, entrypoints);
|
|
77
70
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
}
|
|
136
|
-
|
|
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
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
162
|
+
if (isOriginRoute) {
|
|
163
|
+
throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
|
|
164
|
+
}
|
|
168
165
|
|
|
169
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
174
|
+
writeHtmlFile(htmlAry, ssgRoutes, buildDir); // format route info, side effect
|
|
178
175
|
|
|
179
|
-
|
|
176
|
+
replaceRoute(ssgRoutes, pageRoutes); // write routes to spec file
|
|
180
177
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
178
|
+
writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
|
|
179
|
+
logger.info('ssg Compiled successfully');
|
|
180
|
+
}
|
|
184
181
|
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
});
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
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) ||
|
|
42
|
+
const defaultPort = Number(process.env.PORT) || serverOptions.port;
|
|
43
43
|
portfinder.basePort = defaultPort;
|
|
44
44
|
const port = await portfinder.getPortPromise();
|
|
45
|
-
modernServer = await
|
|
45
|
+
modernServer = await server({
|
|
46
46
|
pwd: appDirectory,
|
|
47
47
|
config: options,
|
|
48
48
|
routes,
|
package/dist/js/modern/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import '@modern-js/core';
|
package/dist/js/node/index.js
CHANGED
|
@@ -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 = (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
72
|
+
if (!ssgOptions) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
81
75
|
|
|
82
|
-
|
|
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
|
-
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
78
|
+
const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
|
|
88
79
|
|
|
89
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
83
|
+
if (pageRoutes.length === 0) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
94
86
|
|
|
95
|
-
|
|
87
|
+
const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints);
|
|
96
88
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
//
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
180
|
+
if (isOriginRoute) {
|
|
181
|
+
throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
|
|
182
|
+
}
|
|
187
183
|
|
|
188
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
192
|
+
(0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir); // format route info, side effect
|
|
197
193
|
|
|
198
|
-
|
|
194
|
+
(0, _replace.replaceRoute)(ssgRoutes, pageRoutes); // write routes to spec file
|
|
199
195
|
|
|
200
|
-
|
|
196
|
+
(0, _util.writeJSONSpec)(buildDir, pageRoutes.concat(apiRoutes));
|
|
201
197
|
|
|
202
|
-
|
|
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
|
|
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) ||
|
|
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,
|
|
54
|
+
modernServer = await (0, _prodServer.default)({
|
|
55
55
|
pwd: appDirectory,
|
|
56
56
|
config: options,
|
|
57
57
|
routes,
|
package/dist/js/node/types.js
CHANGED
package/dist/types/index.d.ts
CHANGED
package/dist/types/types.d.ts
CHANGED
|
@@ -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
|
|
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.
|
|
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.
|
|
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/
|
|
48
|
-
"@modern-js/
|
|
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.
|
|
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": {
|