@modern-js/plugin-ssg 1.2.5 → 1.2.8
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 +30 -0
- package/dist/js/modern/index.js +145 -148
- package/dist/js/modern/server/index.js +2 -3
- package/dist/js/modern/types.js +0 -1
- package/dist/js/node/index.js +144 -148
- package/dist/js/node/server/index.js +2 -4
- package/dist/js/node/types.js +0 -3
- package/dist/types/index.d.ts +3 -1
- package/dist/types/libs/make.d.ts +2 -2
- package/dist/types/libs/util.d.ts +2 -2
- package/dist/types/server/index.d.ts +2 -2
- package/dist/types/types.d.ts +5 -22
- package/jest.config.js +0 -1
- package/package.json +5 -8
- package/tests/index.test.ts +1 -0
- package/tests/util.test.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @modern-js/plugin-ssg
|
|
2
2
|
|
|
3
|
+
## 1.2.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 046e58aa: fix: plugin-analyze and plugin-ssg should not import method from core
|
|
8
|
+
- Updated dependencies [77ff9754]
|
|
9
|
+
- Updated dependencies [d2d1d6b2]
|
|
10
|
+
- Updated dependencies [07a4887e]
|
|
11
|
+
- Updated dependencies [ea2ae711]
|
|
12
|
+
- Updated dependencies [17d0cc46]
|
|
13
|
+
- Updated dependencies [d2d1d6b2]
|
|
14
|
+
- @modern-js/utils@1.4.0
|
|
15
|
+
|
|
16
|
+
## 1.2.7
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- bebb39b6: chore: improve devDependencies and peerDependencies
|
|
21
|
+
- 132f7b53: feat: move config declarations to @modern-js/core
|
|
22
|
+
- Updated dependencies [132f7b53]
|
|
23
|
+
- @modern-js/utils@1.3.7
|
|
24
|
+
|
|
25
|
+
## 1.2.6
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- c7dc7f54: migrate to new plugin style
|
|
30
|
+
- Updated dependencies [5bf5868d]
|
|
31
|
+
- @modern-js/utils@1.3.5
|
|
32
|
+
|
|
3
33
|
## 1.2.5
|
|
4
34
|
|
|
5
35
|
### Patch Changes
|
package/dist/js/modern/index.js
CHANGED
|
@@ -6,181 +6,178 @@ 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
|
+
async afterBuild() {
|
|
39
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
40
|
+
const appContext = api.useAppContext();
|
|
41
|
+
const {
|
|
42
|
+
appDirectory,
|
|
43
|
+
entrypoints
|
|
44
|
+
} = appContext;
|
|
45
|
+
const {
|
|
46
|
+
output
|
|
47
|
+
} = resolvedConfig;
|
|
48
|
+
const {
|
|
49
|
+
ssg,
|
|
50
|
+
path: outputPath
|
|
51
|
+
} = output;
|
|
52
|
+
const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
|
|
59
53
|
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
if (!ssgOptions) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
62
57
|
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
const buildDir = path.join(appDirectory, outputPath);
|
|
59
|
+
const routes = readJSONSpec(buildDir); // filter all routes not web
|
|
65
60
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
61
|
+
const pageRoutes = routes.filter(route => !route.isApi);
|
|
62
|
+
const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
|
|
69
63
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
64
|
+
if (pageRoutes.length === 0) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
75
67
|
|
|
76
|
-
|
|
68
|
+
const intermediateOptions = standardOptions(ssgOptions, entrypoints);
|
|
77
69
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
entryPath
|
|
82
|
-
} = pageRoute;
|
|
83
|
-
const agreedRoutes = agreedRouteMap[entryName];
|
|
84
|
-
let entryOptions = intermediateOptions[entryName];
|
|
85
|
-
|
|
86
|
-
if (!agreedRoutes) {
|
|
87
|
-
// default behavior for non-agreed route
|
|
88
|
-
if (!entryOptions) {
|
|
89
|
-
return;
|
|
90
|
-
} // only add entry route if entryOptions is true
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (entryOptions === true) {
|
|
94
|
-
ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
|
|
95
|
-
output: entryPath
|
|
96
|
-
}));
|
|
97
|
-
} else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
98
|
-
// if entryOptions is object and has routes options
|
|
99
|
-
// add every route in options
|
|
100
|
-
const {
|
|
101
|
-
routes: enrtyRoutes,
|
|
102
|
-
headers
|
|
103
|
-
} = entryOptions;
|
|
104
|
-
enrtyRoutes.forEach(route => {
|
|
105
|
-
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
} else {
|
|
109
|
-
// Unless entryOptions is set to false
|
|
110
|
-
// the default behavior is to add all file-based routes
|
|
111
|
-
if (entryOptions === false) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
70
|
+
if (!intermediateOptions) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
114
73
|
|
|
115
|
-
|
|
116
|
-
entryOptions = {
|
|
117
|
-
preventDefault: [],
|
|
118
|
-
routes: [],
|
|
119
|
-
headers: {}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
74
|
+
const ssgRoutes = []; // each route will try to match the configuration
|
|
122
75
|
|
|
76
|
+
pageRoutes.forEach(pageRoute => {
|
|
123
77
|
const {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
78
|
+
entryName,
|
|
79
|
+
entryPath
|
|
80
|
+
} = pageRoute;
|
|
81
|
+
const agreedRoutes = agreedRouteMap[entryName];
|
|
82
|
+
let entryOptions = intermediateOptions[entryName];
|
|
83
|
+
|
|
84
|
+
if (!agreedRoutes) {
|
|
85
|
+
// default behavior for non-agreed route
|
|
86
|
+
if (!entryOptions) {
|
|
87
|
+
return;
|
|
88
|
+
} // only add entry route if entryOptions is true
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
if (entryOptions === true) {
|
|
92
|
+
ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
|
|
93
|
+
output: entryPath
|
|
94
|
+
}));
|
|
95
|
+
} else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
96
|
+
// if entryOptions is object and has routes options
|
|
97
|
+
// add every route in options
|
|
98
|
+
const {
|
|
99
|
+
routes: enrtyRoutes,
|
|
100
|
+
headers
|
|
101
|
+
} = entryOptions;
|
|
102
|
+
enrtyRoutes.forEach(route => {
|
|
132
103
|
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
ssgRoutes.push(makeRoute(pageRoute, _objectSpread(_objectSpread({}, route), {}, {
|
|
136
|
-
url: generatePath(route.url, param)
|
|
137
|
-
}), headers));
|
|
138
|
-
});
|
|
139
|
-
} else {
|
|
140
|
-
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
141
|
-
}
|
|
142
|
-
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
143
106
|
} else {
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
107
|
+
// Unless entryOptions is set to false
|
|
108
|
+
// the default behavior is to add all file-based routes
|
|
109
|
+
if (entryOptions === false) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!entryOptions || entryOptions === true) {
|
|
114
|
+
entryOptions = {
|
|
115
|
+
preventDefault: [],
|
|
116
|
+
routes: [],
|
|
117
|
+
headers: {}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const {
|
|
122
|
+
preventDefault = [],
|
|
123
|
+
routes: userRoutes = [],
|
|
124
|
+
headers
|
|
125
|
+
} = entryOptions; // if the user sets the routes, then only add them
|
|
126
|
+
|
|
127
|
+
if (userRoutes.length > 0) {
|
|
128
|
+
userRoutes.forEach(route => {
|
|
129
|
+
if (typeof route === 'string') {
|
|
130
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
131
|
+
} else if (Array.isArray(route.params)) {
|
|
132
|
+
route.params.forEach(param => {
|
|
133
|
+
ssgRoutes.push(makeRoute(pageRoute, _objectSpread(_objectSpread({}, route), {}, {
|
|
134
|
+
url: generatePath(route.url, param)
|
|
135
|
+
}), headers));
|
|
136
|
+
});
|
|
137
|
+
} else {
|
|
138
|
+
ssgRoutes.push(makeRoute(pageRoute, route, headers));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
} else {
|
|
142
|
+
// otherwith add all except dynamic routes
|
|
143
|
+
agreedRoutes.filter(route => !preventDefault.includes(route.path)).forEach(route => {
|
|
144
|
+
if (!isDynamicUrl(route.path)) {
|
|
145
|
+
ssgRoutes.push(makeRoute(pageRoute, route.path, headers));
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
150
149
|
}
|
|
151
|
-
}
|
|
152
|
-
});
|
|
150
|
+
});
|
|
153
151
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
152
|
+
if (ssgRoutes.length === 0) {
|
|
153
|
+
return;
|
|
154
|
+
} // currently SSG and SSR cannot be turned on at the same time、same route
|
|
157
155
|
|
|
158
156
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
157
|
+
ssgRoutes.forEach(ssgRoute => {
|
|
158
|
+
if (ssgRoute.isSSR) {
|
|
159
|
+
const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
|
|
162
160
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
161
|
+
if (isOriginRoute) {
|
|
162
|
+
throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
|
|
163
|
+
}
|
|
166
164
|
|
|
167
|
-
|
|
168
|
-
|
|
165
|
+
logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
|
|
166
|
+
}
|
|
169
167
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
168
|
+
ssgRoute.isSSR = false;
|
|
169
|
+
ssgRoute.output = formatOutput(ssgRoute.output);
|
|
170
|
+
});
|
|
171
|
+
const htmlAry = await createServer(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
|
|
174
172
|
|
|
175
|
-
|
|
173
|
+
writeHtmlFile(htmlAry, ssgRoutes, buildDir); // format route info, side effect
|
|
176
174
|
|
|
177
|
-
|
|
175
|
+
replaceRoute(ssgRoutes, pageRoutes); // write routes to spec file
|
|
178
176
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
177
|
+
writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
|
|
178
|
+
logger.info('ssg Compiled successfully');
|
|
179
|
+
}
|
|
182
180
|
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
});
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}));
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import childProcess from 'child_process';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { logger } from '@modern-js/utils';
|
|
4
|
-
import { useAppContext } from '@modern-js/core';
|
|
5
4
|
import { openRouteSSR } from "../libs/util";
|
|
6
5
|
import { CLOSE_SIGN } from "./consts";
|
|
7
|
-
export const createServer = (ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) => new Promise((resolve, reject) => {
|
|
6
|
+
export const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) => new Promise((resolve, reject) => {
|
|
8
7
|
// this side of the shallow copy of a route for subsequent render processing, to prevent the modification of the current field
|
|
9
8
|
// manually enable the server-side rendering configuration for all routes that require SSG
|
|
10
9
|
const backup = openRouteSSR(pageRoutes);
|
|
@@ -13,7 +12,7 @@ export const createServer = (ssgRoutes, pageRoutes, apiRoutes, options, appDirec
|
|
|
13
12
|
cwd: appDirectory,
|
|
14
13
|
silent: true
|
|
15
14
|
});
|
|
16
|
-
const appContext = useAppContext();
|
|
15
|
+
const appContext = api.useAppContext();
|
|
17
16
|
const serverPlugins = appContext.plugins.filter(p => p.server).map(p => p.server);
|
|
18
17
|
const plugins = serverPlugins.map(p => p.name);
|
|
19
18
|
cp.send(JSON.stringify({
|
package/dist/js/modern/types.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
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,178 +29,176 @@ 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
|
-
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
|
+
async afterBuild() {
|
|
56
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
|
57
|
+
const appContext = api.useAppContext();
|
|
58
|
+
const {
|
|
59
|
+
appDirectory,
|
|
60
|
+
entrypoints
|
|
61
|
+
} = appContext;
|
|
62
|
+
const {
|
|
63
|
+
output
|
|
64
|
+
} = resolvedConfig;
|
|
65
|
+
const {
|
|
66
|
+
ssg,
|
|
67
|
+
path: outputPath
|
|
68
|
+
} = output;
|
|
69
|
+
const ssgOptions = Array.isArray(ssg) ? ssg.pop() : ssg; // no ssg configuration, skip ssg render.
|
|
79
70
|
|
|
80
|
-
|
|
71
|
+
if (!ssgOptions) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
81
74
|
|
|
82
|
-
|
|
83
|
-
const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
|
|
75
|
+
const buildDir = _path.default.join(appDirectory, outputPath);
|
|
84
76
|
|
|
85
|
-
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
77
|
+
const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
|
|
88
78
|
|
|
89
|
-
|
|
79
|
+
const pageRoutes = routes.filter(route => !route.isApi);
|
|
80
|
+
const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
|
|
90
81
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
82
|
+
if (pageRoutes.length === 0) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
94
85
|
|
|
95
|
-
|
|
86
|
+
const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints);
|
|
96
87
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
entryPath
|
|
101
|
-
} = pageRoute;
|
|
102
|
-
const agreedRoutes = agreedRouteMap[entryName];
|
|
103
|
-
let entryOptions = intermediateOptions[entryName];
|
|
104
|
-
|
|
105
|
-
if (!agreedRoutes) {
|
|
106
|
-
// default behavior for non-agreed route
|
|
107
|
-
if (!entryOptions) {
|
|
108
|
-
return;
|
|
109
|
-
} // only add entry route if entryOptions is true
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (entryOptions === true) {
|
|
113
|
-
ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
|
|
114
|
-
output: entryPath
|
|
115
|
-
}));
|
|
116
|
-
} else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
117
|
-
// if entryOptions is object and has routes options
|
|
118
|
-
// add every route in options
|
|
119
|
-
const {
|
|
120
|
-
routes: enrtyRoutes,
|
|
121
|
-
headers
|
|
122
|
-
} = entryOptions;
|
|
123
|
-
enrtyRoutes.forEach(route => {
|
|
124
|
-
ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
} else {
|
|
128
|
-
// Unless entryOptions is set to false
|
|
129
|
-
// the default behavior is to add all file-based routes
|
|
130
|
-
if (entryOptions === false) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
88
|
+
if (!intermediateOptions) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
133
91
|
|
|
134
|
-
|
|
135
|
-
entryOptions = {
|
|
136
|
-
preventDefault: [],
|
|
137
|
-
routes: [],
|
|
138
|
-
headers: {}
|
|
139
|
-
};
|
|
140
|
-
}
|
|
92
|
+
const ssgRoutes = []; // each route will try to match the configuration
|
|
141
93
|
|
|
94
|
+
pageRoutes.forEach(pageRoute => {
|
|
142
95
|
const {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
96
|
+
entryName,
|
|
97
|
+
entryPath
|
|
98
|
+
} = pageRoute;
|
|
99
|
+
const agreedRoutes = agreedRouteMap[entryName];
|
|
100
|
+
let entryOptions = intermediateOptions[entryName];
|
|
101
|
+
|
|
102
|
+
if (!agreedRoutes) {
|
|
103
|
+
// default behavior for non-agreed route
|
|
104
|
+
if (!entryOptions) {
|
|
105
|
+
return;
|
|
106
|
+
} // only add entry route if entryOptions is true
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
if (entryOptions === true) {
|
|
110
|
+
ssgRoutes.push(_objectSpread(_objectSpread({}, pageRoute), {}, {
|
|
111
|
+
output: entryPath
|
|
112
|
+
}));
|
|
113
|
+
} else if (entryOptions.routes && entryOptions.routes.length > 0) {
|
|
114
|
+
// if entryOptions is object and has routes options
|
|
115
|
+
// add every route in options
|
|
116
|
+
const {
|
|
117
|
+
routes: enrtyRoutes,
|
|
118
|
+
headers
|
|
119
|
+
} = entryOptions;
|
|
120
|
+
enrtyRoutes.forEach(route => {
|
|
159
121
|
ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
|
|
160
|
-
}
|
|
161
|
-
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
162
124
|
} else {
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
125
|
+
// Unless entryOptions is set to false
|
|
126
|
+
// the default behavior is to add all file-based routes
|
|
127
|
+
if (entryOptions === false) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!entryOptions || entryOptions === true) {
|
|
132
|
+
entryOptions = {
|
|
133
|
+
preventDefault: [],
|
|
134
|
+
routes: [],
|
|
135
|
+
headers: {}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const {
|
|
140
|
+
preventDefault = [],
|
|
141
|
+
routes: userRoutes = [],
|
|
142
|
+
headers
|
|
143
|
+
} = entryOptions; // if the user sets the routes, then only add them
|
|
144
|
+
|
|
145
|
+
if (userRoutes.length > 0) {
|
|
146
|
+
userRoutes.forEach(route => {
|
|
147
|
+
if (typeof route === 'string') {
|
|
148
|
+
ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
|
|
149
|
+
} else if (Array.isArray(route.params)) {
|
|
150
|
+
route.params.forEach(param => {
|
|
151
|
+
ssgRoutes.push((0, _make.makeRoute)(pageRoute, _objectSpread(_objectSpread({}, route), {}, {
|
|
152
|
+
url: (0, _reactRouterDom.generatePath)(route.url, param)
|
|
153
|
+
}), headers));
|
|
154
|
+
});
|
|
155
|
+
} else {
|
|
156
|
+
ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
} else {
|
|
160
|
+
// otherwith add all except dynamic routes
|
|
161
|
+
agreedRoutes.filter(route => !preventDefault.includes(route.path)).forEach(route => {
|
|
162
|
+
if (!(0, _util.isDynamicUrl)(route.path)) {
|
|
163
|
+
ssgRoutes.push((0, _make.makeRoute)(pageRoute, route.path, headers));
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
169
167
|
}
|
|
170
|
-
}
|
|
171
|
-
});
|
|
168
|
+
});
|
|
172
169
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
170
|
+
if (ssgRoutes.length === 0) {
|
|
171
|
+
return;
|
|
172
|
+
} // currently SSG and SSR cannot be turned on at the same time、same route
|
|
176
173
|
|
|
177
174
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
175
|
+
ssgRoutes.forEach(ssgRoute => {
|
|
176
|
+
if (ssgRoute.isSSR) {
|
|
177
|
+
const isOriginRoute = pageRoutes.some(pageRoute => pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName);
|
|
181
178
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
if (isOriginRoute) {
|
|
180
|
+
throw new Error(`ssg can not using with ssr,url - ${ssgRoute.urlPath}, entry - ${ssgRoute.entryName} `);
|
|
181
|
+
}
|
|
185
182
|
|
|
186
|
-
|
|
187
|
-
|
|
183
|
+
_utils.logger.warn(`new ssg route ${ssgRoute.urlPath} is using ssr now,maybe from parent route ${ssgRoute.entryName},close ssr`);
|
|
184
|
+
}
|
|
188
185
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
186
|
+
ssgRoute.isSSR = false;
|
|
187
|
+
ssgRoute.output = (0, _util.formatOutput)(ssgRoute.output);
|
|
188
|
+
});
|
|
189
|
+
const htmlAry = await (0, _server.createServer)(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
|
|
193
190
|
|
|
194
|
-
|
|
191
|
+
(0, _output.writeHtmlFile)(htmlAry, ssgRoutes, buildDir); // format route info, side effect
|
|
195
192
|
|
|
196
|
-
|
|
193
|
+
(0, _replace.replaceRoute)(ssgRoutes, pageRoutes); // write routes to spec file
|
|
197
194
|
|
|
198
|
-
|
|
195
|
+
(0, _util.writeJSONSpec)(buildDir, pageRoutes.concat(apiRoutes));
|
|
199
196
|
|
|
200
|
-
|
|
201
|
-
|
|
197
|
+
_utils.logger.info('ssg Compiled successfully');
|
|
198
|
+
}
|
|
202
199
|
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
name: '@modern-js/plugin-ssg'
|
|
200
|
+
};
|
|
201
|
+
}
|
|
206
202
|
});
|
|
207
203
|
|
|
208
204
|
exports.default = _default;
|
|
@@ -11,15 +11,13 @@ var _path = _interopRequireDefault(require("path"));
|
|
|
11
11
|
|
|
12
12
|
var _utils = require("@modern-js/utils");
|
|
13
13
|
|
|
14
|
-
var _core = require("@modern-js/core");
|
|
15
|
-
|
|
16
14
|
var _util = require("../libs/util");
|
|
17
15
|
|
|
18
16
|
var _consts = require("./consts");
|
|
19
17
|
|
|
20
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
19
|
|
|
22
|
-
const createServer = (ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) => new Promise((resolve, reject) => {
|
|
20
|
+
const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) => new Promise((resolve, reject) => {
|
|
23
21
|
// this side of the shallow copy of a route for subsequent render processing, to prevent the modification of the current field
|
|
24
22
|
// manually enable the server-side rendering configuration for all routes that require SSG
|
|
25
23
|
const backup = (0, _util.openRouteSSR)(pageRoutes);
|
|
@@ -30,7 +28,7 @@ const createServer = (ssgRoutes, pageRoutes, apiRoutes, options, appDirectory) =
|
|
|
30
28
|
silent: true
|
|
31
29
|
});
|
|
32
30
|
|
|
33
|
-
const appContext =
|
|
31
|
+
const appContext = api.useAppContext();
|
|
34
32
|
const serverPlugins = appContext.plugins.filter(p => p.server).map(p => p.server);
|
|
35
33
|
const plugins = serverPlugins.map(p => p.name);
|
|
36
34
|
cp.send(JSON.stringify({
|
package/dist/js/node/types.js
CHANGED
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
2
2
|
import { compile } from '../server/prerender';
|
|
3
|
-
import {
|
|
3
|
+
import { SSGRouteOptions, SsgRoute } from '../types';
|
|
4
4
|
export declare function makeRender(ssgRoutes: SsgRoute[], render: ReturnType<typeof compile>, port: number): Promise<string>[];
|
|
5
|
-
export declare function makeRoute(baseRoute: ModernRoute, route: string |
|
|
5
|
+
export declare function makeRoute(baseRoute: ModernRoute, route: string | SSGRouteOptions, headers?: Record<string, any>): SsgRoute;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
2
|
-
import {
|
|
2
|
+
import { SsgRoute, SSGConfig, EntryPoint, SSGMultiEntryOptions } from '../types';
|
|
3
3
|
export declare function formatOutput(filename: string): string;
|
|
4
4
|
export declare function formatPath(str: string): string;
|
|
5
5
|
export declare function isDynamicUrl(url: string): boolean;
|
|
@@ -8,7 +8,7 @@ export declare function getOutput(route: SsgRoute, base: string, agreed?: boolea
|
|
|
8
8
|
export declare const readJSONSpec: (dir: string) => ModernRoute[];
|
|
9
9
|
export declare const writeJSONSpec: (dir: string, routes: ModernRoute[]) => void;
|
|
10
10
|
export declare const replaceWithAlias: (base: string, filePath: string, alias: string) => string;
|
|
11
|
-
export declare const standardOptions: (ssgOptions:
|
|
11
|
+
export declare const standardOptions: (ssgOptions: SSGConfig, entrypoints: EntryPoint[]) => false | SSGMultiEntryOptions;
|
|
12
12
|
export declare const openRouteSSR: (routes: ModernRoute[]) => {
|
|
13
13
|
isSSR: boolean;
|
|
14
14
|
bundle: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NormalizedConfig } from '@modern-js/core';
|
|
1
|
+
import type { NormalizedConfig, PluginAPI } from '@modern-js/core';
|
|
2
2
|
import { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
3
3
|
import { SsgRoute } from '../types';
|
|
4
|
-
export declare const createServer: (ssgRoutes: SsgRoute[], pageRoutes: ModernRoute[], apiRoutes: ModernRoute[], options: NormalizedConfig, appDirectory: string) => Promise<string[]>;
|
|
4
|
+
export declare const createServer: (api: PluginAPI, ssgRoutes: SsgRoute[], pageRoutes: ModernRoute[], apiRoutes: ModernRoute[], options: NormalizedConfig, appDirectory: string) => Promise<string[]>;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
2
|
-
import '@modern-js/core';
|
|
1
|
+
import type { ServerRoute as ModernRoute } from '@modern-js/types';
|
|
2
|
+
import type { SSGConfig, SSGRouteOptions, SSGMultiEntryOptions, SSGSingleEntryOptions } from '@modern-js/core';
|
|
3
|
+
export type { SSGConfig, SSGRouteOptions, SSGMultiEntryOptions, SSGSingleEntryOptions };
|
|
3
4
|
export declare type AgreedRoute = {
|
|
4
5
|
path: string;
|
|
5
6
|
component: string;
|
|
@@ -17,24 +18,6 @@ export declare type SsgRoute = ModernRoute & {
|
|
|
17
18
|
output: string;
|
|
18
19
|
headers?: Record<string, string>;
|
|
19
20
|
};
|
|
20
|
-
export declare type RouteOptions = string | {
|
|
21
|
-
url: string;
|
|
22
|
-
output?: string;
|
|
23
|
-
params?: Record<string, any>[];
|
|
24
|
-
headers?: Record<string, any>;
|
|
25
|
-
};
|
|
26
|
-
export declare type SingleEntryOptions = boolean | {
|
|
27
|
-
preventDefault?: string[];
|
|
28
|
-
headers?: Record<string, any>;
|
|
29
|
-
routes?: RouteOptions[];
|
|
30
|
-
};
|
|
31
|
-
export declare type MultiEntryOptions = Record<string, SingleEntryOptions>;
|
|
32
|
-
export declare type SSG = boolean | SingleEntryOptions | MultiEntryOptions | ((entryName: string) => SingleEntryOptions);
|
|
33
21
|
export declare type ExtendOutputConfig = {
|
|
34
|
-
ssg:
|
|
35
|
-
};
|
|
36
|
-
declare module '@modern-js/core' {
|
|
37
|
-
interface OutputConfig {
|
|
38
|
-
ssg?: SSG;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
22
|
+
ssg: SSGConfig;
|
|
23
|
+
};
|
package/jest.config.js
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.2.
|
|
14
|
+
"version": "1.2.8",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -55,15 +55,15 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@babel/runtime": "^7",
|
|
58
|
-
"@modern-js/utils": "^1.
|
|
58
|
+
"@modern-js/utils": "^1.4.0",
|
|
59
59
|
"node-mocks-http": "^1.10.1",
|
|
60
60
|
"normalize-path": "^3.0.0",
|
|
61
61
|
"portfinder": "^1.0.28",
|
|
62
62
|
"react-router-dom": "^5.2.1"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@modern-js/types": "^1.
|
|
66
|
-
"@modern-js/prod-server": "^1.0
|
|
65
|
+
"@modern-js/types": "^1.4.0",
|
|
66
|
+
"@modern-js/prod-server": "^1.1.0",
|
|
67
67
|
"@types/jest": "^26",
|
|
68
68
|
"@types/node": "^14",
|
|
69
69
|
"@types/react": "^17",
|
|
@@ -71,14 +71,11 @@
|
|
|
71
71
|
"@types/react-router": "^5.1.16",
|
|
72
72
|
"@types/react-router-dom": "^5.1.8",
|
|
73
73
|
"typescript": "^4",
|
|
74
|
-
"@modern-js/core": "
|
|
74
|
+
"@modern-js/core": "1.7.0",
|
|
75
75
|
"@scripts/build": "0.0.0",
|
|
76
76
|
"jest": "^27",
|
|
77
77
|
"@scripts/jest-config": "0.0.0"
|
|
78
78
|
},
|
|
79
|
-
"peerDependencies": {
|
|
80
|
-
"@modern-js/core": "^1.4.6"
|
|
81
|
-
},
|
|
82
79
|
"sideEffects": false,
|
|
83
80
|
"modernConfig": {
|
|
84
81
|
"output": {
|
package/tests/index.test.ts
CHANGED
package/tests/util.test.ts
CHANGED
|
@@ -74,7 +74,6 @@ describe('test ssg util function', () => {
|
|
|
74
74
|
expect(replaceWithAlias('/src', '/src/app.js', '@src')).toBe('@src/app.js');
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
-
// eslint-disable-next-line max-statements
|
|
78
77
|
it('should starndar user config correctly', () => {
|
|
79
78
|
const opt0 = standardOptions(false, []);
|
|
80
79
|
expect(opt0).toBeFalsy();
|