@modern-js/plugin-ssg 1.0.0 → 1.1.2-rc.0
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/.eslintrc.js +6 -0
- package/CHANGELOG.md +42 -0
- package/README.md +1 -4
- package/dist/js/modern/index.js +97 -58
- package/dist/js/modern/libs/make.js +37 -0
- package/dist/js/modern/libs/output.js +2 -1
- package/dist/js/modern/libs/replace.js +5 -4
- package/dist/js/modern/libs/util.js +39 -22
- package/dist/js/modern/server/index.js +4 -3
- package/dist/js/modern/server/prerender.js +2 -2
- package/dist/js/modern/server/process.js +8 -7
- package/dist/js/node/index.js +97 -56
- package/dist/js/node/libs/make.js +50 -0
- package/dist/js/node/libs/output.js +7 -3
- package/dist/js/node/libs/replace.js +5 -4
- package/dist/js/node/libs/util.js +43 -30
- package/dist/js/node/server/index.js +5 -3
- package/dist/js/node/server/prerender.js +2 -2
- package/dist/js/node/server/process.js +7 -6
- package/dist/types/libs/make.d.ts +5 -0
- package/dist/types/libs/replace.d.ts +1 -1
- package/dist/types/libs/util.d.ts +4 -8
- package/dist/types/types.d.ts +13 -17
- package/package.json +10 -11
- package/src/index.ts +85 -89
- package/src/libs/make.ts +45 -0
- package/src/libs/output.ts +3 -2
- package/src/libs/replace.ts +7 -4
- package/src/libs/util.ts +40 -27
- package/src/server/index.ts +3 -2
- package/src/server/process.ts +7 -5
- package/src/types.ts +26 -20
- package/tests/.eslintrc.js +6 -0
- package/tests/lib.test.ts +49 -170
- package/tests/util.test.ts +71 -32
- package/dist/js/modern/libs/createPage.js +0 -46
- package/dist/js/modern/libs/invoker.js +0 -55
- package/dist/js/modern/libs/render.js +0 -15
- package/dist/js/modern/loader/index.js +0 -105
- package/dist/js/modern/manifest-op.js +0 -100
- package/dist/js/node/libs/createPage.js +0 -57
- package/dist/js/node/libs/invoker.js +0 -66
- package/dist/js/node/libs/render.js +0 -22
- package/dist/js/node/loader/index.js +0 -115
- package/dist/js/node/manifest-op.js +0 -120
- package/dist/types/libs/createPage.d.ts +0 -2
- package/dist/types/libs/invoker.d.ts +0 -5
- package/dist/types/libs/render.d.ts +0 -3
- package/dist/types/loader/index.d.ts +0 -4
- package/dist/types/manifest-op.d.ts +0 -18
- package/src/libs/createPage.ts +0 -42
- package/src/libs/invoker.ts +0 -55
- package/src/libs/render.ts +0 -16
- package/src/loader/index.ts +0 -99
- package/src/manifest-op.ts +0 -110
- package/tests/operate.test.ts +0 -39
package/dist/js/node/index.js
CHANGED
|
@@ -5,48 +5,35 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
|
|
8
10
|
var _utils = require("@modern-js/utils");
|
|
9
11
|
|
|
10
12
|
var _core = require("@modern-js/core");
|
|
11
13
|
|
|
12
|
-
var
|
|
14
|
+
var _reactRouterDom = require("react-router-dom");
|
|
13
15
|
|
|
14
16
|
var _util = require("./libs/util");
|
|
15
17
|
|
|
16
|
-
var _invoker = require("./libs/invoker");
|
|
17
|
-
|
|
18
18
|
var _server = require("./server");
|
|
19
19
|
|
|
20
20
|
var _output = require("./libs/output");
|
|
21
21
|
|
|
22
22
|
var _replace = require("./libs/replace");
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
var _make = require("./libs/make");
|
|
25
|
+
|
|
26
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
|
+
|
|
28
|
+
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; }
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
const staticFiles = new _manifestOp.LoaderManifest().get(absEntriesDir, staticRenderLevel); // 将绝对路径转换成 alias,因为获取到的约定路由也是使用别名的
|
|
30
|
+
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
31
|
|
|
30
|
-
|
|
31
|
-
return staticAlias;
|
|
32
|
-
};
|
|
32
|
+
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
33
|
|
|
34
34
|
var _default = (0, _core.createPlugin)(() => {
|
|
35
35
|
const agreedRouteMap = {};
|
|
36
36
|
return {
|
|
37
|
-
config() {
|
|
38
|
-
return {
|
|
39
|
-
tools: {
|
|
40
|
-
babel(config, {
|
|
41
|
-
chain
|
|
42
|
-
}) {
|
|
43
|
-
chain.plugin('./loader').use(_utils.upath.normalizeSafe(require.resolve("./loader")));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
},
|
|
49
|
-
|
|
50
37
|
validateSchema() {
|
|
51
38
|
return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
|
|
52
39
|
},
|
|
@@ -72,16 +59,11 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
72
59
|
|
|
73
60
|
const appContext = (0, _core.useAppContext)();
|
|
74
61
|
const {
|
|
75
|
-
appDirectory
|
|
62
|
+
appDirectory,
|
|
63
|
+
entrypoints
|
|
76
64
|
} = appContext;
|
|
77
65
|
const {
|
|
78
|
-
output
|
|
79
|
-
server: {
|
|
80
|
-
baseUrl
|
|
81
|
-
},
|
|
82
|
-
source: {
|
|
83
|
-
entriesDir
|
|
84
|
-
}
|
|
66
|
+
output
|
|
85
67
|
} = resolvedConfig;
|
|
86
68
|
const {
|
|
87
69
|
ssg,
|
|
@@ -93,15 +75,9 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
93
75
|
return;
|
|
94
76
|
}
|
|
95
77
|
|
|
96
|
-
const
|
|
97
|
-
useSSG,
|
|
98
|
-
userHook
|
|
99
|
-
} = (0, _util.parsedSSGConfig)(ssgOptions);
|
|
78
|
+
const buildDir = _path.default.join(appDirectory, outputPath);
|
|
100
79
|
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
const routes = (0, _util.readJSONSpec)(buildDir);
|
|
104
|
-
const staticAlias = listStaticFiles(appDirectory, entriesDir, useSSG); // filter all routes not web
|
|
80
|
+
const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
|
|
105
81
|
|
|
106
82
|
const pageRoutes = routes.filter(route => !route.isApi);
|
|
107
83
|
const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
|
|
@@ -110,27 +86,91 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
110
86
|
return;
|
|
111
87
|
}
|
|
112
88
|
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
const listener = (route, agreed) => {
|
|
116
|
-
const urlPrefix = (0, _util.getUrlPrefix)(route, baseUrl);
|
|
117
|
-
const ssgOutput = (0, _util.getOutput)(route, urlPrefix, agreed);
|
|
118
|
-
route.output = (0, _util.formatOutput)(route.entryPath, ssgOutput);
|
|
119
|
-
ssgRoutes.push(route);
|
|
120
|
-
}; // check if every allowed agreed route was collected
|
|
121
|
-
|
|
89
|
+
const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints);
|
|
122
90
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return false;
|
|
127
|
-
} // if allowed, return collection state
|
|
91
|
+
if (!intermediateOptions) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
128
94
|
|
|
95
|
+
const ssgRoutes = []; // each route will try to match the configuration
|
|
96
|
+
|
|
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
|
+
}
|
|
129
135
|
|
|
130
|
-
|
|
131
|
-
|
|
136
|
+
if (!entryOptions || entryOptions === true) {
|
|
137
|
+
entryOptions = {
|
|
138
|
+
preventDefault: [],
|
|
139
|
+
routes: [],
|
|
140
|
+
headers: {}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
132
143
|
|
|
133
|
-
|
|
144
|
+
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 {
|
|
161
|
+
ssgRoutes.push((0, _make.makeRoute)(pageRoute, route, headers));
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
} 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
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
134
174
|
|
|
135
175
|
if (ssgRoutes.length === 0) {
|
|
136
176
|
return;
|
|
@@ -149,6 +189,7 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
149
189
|
}
|
|
150
190
|
|
|
151
191
|
ssgRoute.isSSR = false;
|
|
192
|
+
ssgRoute.output = (0, _util.formatOutput)(ssgRoute.output);
|
|
152
193
|
});
|
|
153
194
|
const htmlAry = await (0, _server.createServer)(ssgRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
|
|
154
195
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makeRender = makeRender;
|
|
7
|
+
exports.makeRoute = makeRoute;
|
|
8
|
+
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
10
|
+
|
|
11
|
+
var _normalizePath = _interopRequireDefault(require("normalize-path"));
|
|
12
|
+
|
|
13
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
+
|
|
15
|
+
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
|
+
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
|
+
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
|
+
function makeRender(ssgRoutes, render, port) {
|
|
22
|
+
return ssgRoutes.map(ssgRoute => render({
|
|
23
|
+
url: ssgRoute.urlPath,
|
|
24
|
+
headers: _objectSpread({
|
|
25
|
+
host: `localhost:${port}`
|
|
26
|
+
}, ssgRoute.headers),
|
|
27
|
+
connection: {}
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function makeRoute(baseRoute, route, headers = {}) {
|
|
32
|
+
const {
|
|
33
|
+
urlPath,
|
|
34
|
+
entryPath
|
|
35
|
+
} = baseRoute;
|
|
36
|
+
|
|
37
|
+
if (typeof route === 'string') {
|
|
38
|
+
return _objectSpread(_objectSpread({}, baseRoute), {}, {
|
|
39
|
+
urlPath: (0, _normalizePath.default)(`${urlPath}${route}`) || '/',
|
|
40
|
+
headers,
|
|
41
|
+
output: _path.default.join(entryPath, `..${route}`)
|
|
42
|
+
});
|
|
43
|
+
} else {
|
|
44
|
+
return _objectSpread(_objectSpread({}, baseRoute), {}, {
|
|
45
|
+
urlPath: (0, _normalizePath.default)(`${urlPath}${route.url}`) || '/',
|
|
46
|
+
headers: _objectSpread(_objectSpread({}, headers), route.headers),
|
|
47
|
+
output: route.output ? _path.default.normalize(route.output) : _path.default.join(entryPath, `..${route.url}`)
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -5,16 +5,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.writeHtmlFile = writeHtmlFile;
|
|
7
7
|
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
|
|
8
10
|
var _utils = require("@modern-js/utils");
|
|
9
11
|
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
10
14
|
function writeHtmlFile(htmlAry, ssgRoutes, baseDir) {
|
|
11
15
|
htmlAry.forEach((html, index) => {
|
|
12
16
|
const ssgRoute = ssgRoutes[index];
|
|
13
17
|
|
|
14
|
-
const filepath =
|
|
18
|
+
const filepath = _path.default.join(baseDir, ssgRoute.output);
|
|
15
19
|
|
|
16
|
-
if (!_utils.fs.existsSync(
|
|
17
|
-
_utils.fs.ensureDirSync(
|
|
20
|
+
if (!_utils.fs.existsSync(_path.default.dirname(filepath))) {
|
|
21
|
+
_utils.fs.ensureDirSync(_path.default.dirname(filepath));
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
_utils.fs.writeFileSync(filepath, html);
|
|
@@ -8,13 +8,13 @@ exports.replaceRoute = replaceRoute;
|
|
|
8
8
|
|
|
9
9
|
var _normalizePath = _interopRequireDefault(require("normalize-path"));
|
|
10
10
|
|
|
11
|
-
const _excluded = ["output"];
|
|
11
|
+
const _excluded = ["output", "headers"];
|
|
12
12
|
|
|
13
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
14
|
|
|
15
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object);
|
|
15
|
+
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
16
|
|
|
17
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]
|
|
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; }
|
|
18
18
|
|
|
19
19
|
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
20
|
|
|
@@ -39,7 +39,8 @@ function replaceRoute(ssgRoutes, pageRoutes) {
|
|
|
39
39
|
// remove redundant fields and replace rendered entryPath
|
|
40
40
|
const cleanSsgRoutes = ssgRoutes.map(ssgRoute => {
|
|
41
41
|
const {
|
|
42
|
-
output
|
|
42
|
+
output,
|
|
43
|
+
headers
|
|
43
44
|
} = ssgRoute,
|
|
44
45
|
cleanSsgRoute = _objectWithoutProperties(ssgRoute, _excluded);
|
|
45
46
|
|
|
@@ -6,21 +6,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.formatOutput = formatOutput;
|
|
7
7
|
exports.formatPath = formatPath;
|
|
8
8
|
exports.getOutput = getOutput;
|
|
9
|
-
exports.getSSGRenderLevel = void 0;
|
|
10
9
|
exports.getUrlPrefix = getUrlPrefix;
|
|
11
10
|
exports.isDynamicUrl = isDynamicUrl;
|
|
12
|
-
exports.writeJSONSpec = exports.
|
|
11
|
+
exports.writeJSONSpec = exports.standardOptions = exports.replaceWithAlias = exports.readJSONSpec = void 0;
|
|
13
12
|
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
var _manifestOp = require("../manifest-op");
|
|
13
|
+
var _path = _interopRequireDefault(require("path"));
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
const file = _utils.path.extname(filename) ? filename : `${filename}/index.html`;
|
|
15
|
+
var _utils = require("@modern-js/utils");
|
|
20
16
|
|
|
21
|
-
|
|
17
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
18
|
|
|
23
|
-
|
|
19
|
+
function formatOutput(filename) {
|
|
20
|
+
const outputPath = _path.default.extname(filename) ? filename : `${filename}/index.html`;
|
|
21
|
+
return outputPath;
|
|
24
22
|
}
|
|
25
23
|
|
|
26
24
|
function formatPath(str) {
|
|
@@ -94,7 +92,7 @@ function getOutput(route, base, agreed) {
|
|
|
94
92
|
}
|
|
95
93
|
|
|
96
94
|
const readJSONSpec = dir => {
|
|
97
|
-
const routeJSONPath =
|
|
95
|
+
const routeJSONPath = _path.default.join(dir, _utils.ROUTE_SPEC_FILE);
|
|
98
96
|
|
|
99
97
|
const routeJSON = require(routeJSONPath);
|
|
100
98
|
|
|
@@ -107,7 +105,7 @@ const readJSONSpec = dir => {
|
|
|
107
105
|
exports.readJSONSpec = readJSONSpec;
|
|
108
106
|
|
|
109
107
|
const writeJSONSpec = (dir, routes) => {
|
|
110
|
-
const routeJSONPath =
|
|
108
|
+
const routeJSONPath = _path.default.join(dir, _utils.ROUTE_SPEC_FILE);
|
|
111
109
|
|
|
112
110
|
_utils.fs.writeJSONSync(routeJSONPath, {
|
|
113
111
|
routes
|
|
@@ -118,30 +116,45 @@ const writeJSONSpec = (dir, routes) => {
|
|
|
118
116
|
|
|
119
117
|
exports.writeJSONSpec = writeJSONSpec;
|
|
120
118
|
|
|
121
|
-
const
|
|
122
|
-
const level = typeof key === 'boolean' ? _manifestOp.MODE.LOOSE : _manifestOp.MODE[key.toUpperCase()]; // currently only MODE.STRICT and MODE.LOOSE are supported
|
|
119
|
+
const replaceWithAlias = (base, filePath, alias) => _path.default.posix.join(alias, _path.default.posix.relative(base, filePath));
|
|
123
120
|
|
|
124
|
-
|
|
125
|
-
throw new Error(`[SSG Render Fail] SSG 不支持当前 Mode,useSSG: ${key.toString()}, Level: ${level}`);
|
|
126
|
-
}
|
|
121
|
+
exports.replaceWithAlias = replaceWithAlias;
|
|
127
122
|
|
|
128
|
-
|
|
129
|
-
|
|
123
|
+
const standardOptions = (ssgOptions, entrypoints) => {
|
|
124
|
+
if (ssgOptions === false) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
130
127
|
|
|
131
|
-
|
|
128
|
+
if (ssgOptions === true) {
|
|
129
|
+
return entrypoints.reduce((opt, entry) => {
|
|
130
|
+
opt[entry.entryName] = ssgOptions;
|
|
131
|
+
return opt;
|
|
132
|
+
}, {});
|
|
133
|
+
} else if (typeof ssgOptions === 'object') {
|
|
134
|
+
const isSingle = (0, _utils.isSingleEntry)(entrypoints);
|
|
135
|
+
|
|
136
|
+
if (isSingle && typeof ssgOptions.main === 'undefined') {
|
|
137
|
+
return {
|
|
138
|
+
main: ssgOptions
|
|
139
|
+
};
|
|
140
|
+
} else {
|
|
141
|
+
return ssgOptions;
|
|
142
|
+
}
|
|
143
|
+
} else if (typeof ssgOptions === 'function') {
|
|
144
|
+
const intermediateOptions = {};
|
|
132
145
|
|
|
133
|
-
const
|
|
134
|
-
|
|
146
|
+
for (const entrypoint of entrypoints) {
|
|
147
|
+
const {
|
|
148
|
+
entryName
|
|
149
|
+
} = entrypoint; // Todo may be async function
|
|
135
150
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
useSSG,
|
|
139
|
-
userHook
|
|
140
|
-
};
|
|
141
|
-
};
|
|
151
|
+
intermediateOptions[entryName] = ssgOptions(entryName);
|
|
152
|
+
}
|
|
142
153
|
|
|
143
|
-
|
|
154
|
+
return intermediateOptions;
|
|
155
|
+
}
|
|
144
156
|
|
|
145
|
-
|
|
157
|
+
return false;
|
|
158
|
+
};
|
|
146
159
|
|
|
147
|
-
exports.
|
|
160
|
+
exports.standardOptions = standardOptions;
|
|
@@ -7,6 +7,8 @@ exports.createServer = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _child_process = _interopRequireDefault(require("child_process"));
|
|
9
9
|
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
|
|
10
12
|
var _utils = require("@modern-js/utils");
|
|
11
13
|
|
|
12
14
|
var _core = require("@modern-js/core");
|
|
@@ -15,9 +17,9 @@ var _consts = require("./consts");
|
|
|
15
17
|
|
|
16
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
19
|
|
|
18
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object);
|
|
20
|
+
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; }
|
|
19
21
|
|
|
20
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]
|
|
22
|
+
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; }
|
|
21
23
|
|
|
22
24
|
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; }
|
|
23
25
|
|
|
@@ -30,7 +32,7 @@ const createServer = (ssgRoutes, apiRoutes, options, appDirectory) => new Promis
|
|
|
30
32
|
}));
|
|
31
33
|
const total = backup.concat(apiRoutes);
|
|
32
34
|
|
|
33
|
-
const cp = _child_process.default.fork(
|
|
35
|
+
const cp = _child_process.default.fork(_path.default.join(__dirname, 'process'), {
|
|
34
36
|
cwd: appDirectory,
|
|
35
37
|
silent: true
|
|
36
38
|
});
|
|
@@ -13,9 +13,9 @@ var _nodeMocksHttp = _interopRequireDefault(require("node-mocks-http"));
|
|
|
13
13
|
|
|
14
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
15
|
|
|
16
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object);
|
|
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; }
|
|
17
17
|
|
|
18
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]
|
|
18
|
+
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
19
|
|
|
20
20
|
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
21
|
|
|
@@ -6,7 +6,7 @@ var _portfinder = _interopRequireDefault(require("portfinder"));
|
|
|
6
6
|
|
|
7
7
|
var _utils = require("@modern-js/utils");
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _make = require("../libs/make");
|
|
10
10
|
|
|
11
11
|
var _prerender = require("./prerender");
|
|
12
12
|
|
|
@@ -16,13 +16,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
16
16
|
|
|
17
17
|
const safetyRequire = (filename, base) => {
|
|
18
18
|
try {
|
|
19
|
-
return (0, _utils.compatRequire)(
|
|
19
|
+
return (0, _utils.compatRequire)(require.resolve(`${filename}/server`, {
|
|
20
20
|
paths: [base]
|
|
21
|
-
}))
|
|
21
|
+
}));
|
|
22
22
|
} catch (e) {
|
|
23
|
-
return (0, _utils.compatRequire)(
|
|
23
|
+
return (0, _utils.compatRequire)(require.resolve(filename, {
|
|
24
24
|
paths: [base]
|
|
25
|
-
}))
|
|
25
|
+
}));
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
|
|
@@ -65,7 +65,8 @@ process.on('message', async chunk => {
|
|
|
65
65
|
|
|
66
66
|
|
|
67
67
|
const render = (0, _prerender.compile)(modernServer.getRequestHandler());
|
|
68
|
-
const renderPromiseAry = (0,
|
|
68
|
+
const renderPromiseAry = (0, _make.makeRender)(routes.filter(route => !route.isApi), render, port); // eslint-disable-next-line promise/no-promise-in-callback
|
|
69
|
+
|
|
69
70
|
const htmlAry = await Promise.all(renderPromiseAry);
|
|
70
71
|
htmlAry.forEach(html => {
|
|
71
72
|
process.send(html);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ModernRoute } from '@modern-js/server';
|
|
2
|
+
import { compile } from '../server/prerender';
|
|
3
|
+
import { RouteOptions, SsgRoute } from '../types';
|
|
4
|
+
export declare function makeRender(ssgRoutes: SsgRoute[], render: ReturnType<typeof compile>, port: number): Promise<string>[];
|
|
5
|
+
export declare function makeRoute(baseRoute: ModernRoute, route: string | RouteOptions, headers?: Record<string, any>): SsgRoute;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ModernRoute } from '@modern-js/server';
|
|
2
2
|
import { SsgRoute } from '../types';
|
|
3
|
-
export declare function exist(route:
|
|
3
|
+
export declare function exist(route: ModernRoute, pageRoutes: ModernRoute[]): number;
|
|
4
4
|
export declare function replaceRoute(ssgRoutes: SsgRoute[], pageRoutes: ModernRoute[]): ModernRoute[];
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { ModernRoute } from '@modern-js/server';
|
|
2
|
-
import {
|
|
3
|
-
export declare function formatOutput(
|
|
2
|
+
import { EntryPoint, MultiEntryOptions, SSG, SsgRoute } from '../types';
|
|
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;
|
|
6
6
|
export declare function getUrlPrefix(route: SsgRoute, baseUrl: string | string[]): string;
|
|
7
7
|
export declare function getOutput(route: SsgRoute, base: string, agreed?: boolean): string;
|
|
8
8
|
export declare const readJSONSpec: (dir: string) => ModernRoute[];
|
|
9
9
|
export declare const writeJSONSpec: (dir: string, routes: ModernRoute[]) => void;
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const
|
|
12
|
-
useSSG: boolean;
|
|
13
|
-
userHook: (context: any) => void;
|
|
14
|
-
};
|
|
15
|
-
export declare const replaceWithAlias: (base: string, filePath: string, alias: string) => string;
|
|
10
|
+
export declare const replaceWithAlias: (base: string, filePath: string, alias: string) => string;
|
|
11
|
+
export declare const standardOptions: (ssgOptions: SSG, entrypoints: EntryPoint[]) => false | MultiEntryOptions;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -12,27 +12,23 @@ export declare type EntryPoint = {
|
|
|
12
12
|
export declare type AgreedRouteMap = {
|
|
13
13
|
[propNames: string]: AgreedRoute[];
|
|
14
14
|
};
|
|
15
|
-
export declare type
|
|
16
|
-
|
|
17
|
-
output?: string;
|
|
18
|
-
params?: Record<string, string | number>;
|
|
15
|
+
export declare type SsgRoute = ModernRoute & {
|
|
16
|
+
output: string;
|
|
19
17
|
headers?: Record<string, string>;
|
|
20
18
|
};
|
|
21
|
-
export declare type
|
|
22
|
-
|
|
23
|
-
agreed?: boolean;
|
|
24
|
-
};
|
|
25
|
-
export declare type CreatePageParam = FreshPageConfig | FreshPageConfig[];
|
|
26
|
-
export declare type CreatePageListener = (route: SsgRoute, agreed?: boolean) => void;
|
|
27
|
-
export declare type SsgRoute = ModernRoute & {
|
|
19
|
+
export declare type RouteOptions = string | {
|
|
20
|
+
url: string;
|
|
28
21
|
output?: string;
|
|
29
|
-
|
|
22
|
+
params?: Record<string, any>[];
|
|
23
|
+
headers?: Record<string, any>;
|
|
30
24
|
};
|
|
31
|
-
export declare type
|
|
32
|
-
|
|
33
|
-
|
|
25
|
+
export declare type SingleEntryOptions = boolean | {
|
|
26
|
+
preventDefault?: string[];
|
|
27
|
+
headers?: Record<string, any>;
|
|
28
|
+
routes: RouteOptions[];
|
|
34
29
|
};
|
|
35
|
-
export declare type
|
|
30
|
+
export declare type MultiEntryOptions = Record<string, SingleEntryOptions>;
|
|
31
|
+
export declare type SSG = boolean | SingleEntryOptions | MultiEntryOptions | ((entryName: string) => SingleEntryOptions);
|
|
36
32
|
export declare type ExtendOutputConfig = {
|
|
37
|
-
ssg:
|
|
33
|
+
ssg: SSG;
|
|
38
34
|
};
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.1.2-rc.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -35,16 +35,14 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@babel/runtime": "^7",
|
|
38
|
-
"@modern-js/
|
|
39
|
-
"@modern-js/utils": "^1.0.0",
|
|
38
|
+
"@modern-js/utils": "^1.1.4-rc.0",
|
|
40
39
|
"node-mocks-http": "^1.10.1",
|
|
41
40
|
"normalize-path": "^3.0.0",
|
|
42
41
|
"portfinder": "^1.0.28",
|
|
43
|
-
"react-router-dom": "^5.2.1"
|
|
44
|
-
"webpack-chain": "^6.5.1"
|
|
42
|
+
"react-router-dom": "^5.2.1"
|
|
45
43
|
},
|
|
46
44
|
"devDependencies": {
|
|
47
|
-
"@modern-js/server": "^1.
|
|
45
|
+
"@modern-js/server": "^1.1.4-rc.0",
|
|
48
46
|
"@types/jest": "^26",
|
|
49
47
|
"@types/node": "^14",
|
|
50
48
|
"@types/react": "^17",
|
|
@@ -52,12 +50,12 @@
|
|
|
52
50
|
"@types/react-router": "^5.1.16",
|
|
53
51
|
"@types/react-router-dom": "^5.1.8",
|
|
54
52
|
"typescript": "^4",
|
|
55
|
-
"@modern-js/core": "^1.
|
|
56
|
-
"@modern-js/plugin-testing": "^1.
|
|
57
|
-
"@modern-js/module-tools": "^1.
|
|
53
|
+
"@modern-js/core": "^1.1.4-rc.0",
|
|
54
|
+
"@modern-js/plugin-testing": "^1.1.1",
|
|
55
|
+
"@modern-js/module-tools": "^1.1.1"
|
|
58
56
|
},
|
|
59
57
|
"peerDependencies": {
|
|
60
|
-
"@modern-js/core": "^1.
|
|
58
|
+
"@modern-js/core": "^1.1.4-rc.0"
|
|
61
59
|
},
|
|
62
60
|
"sideEffects": false,
|
|
63
61
|
"modernConfig": {
|
|
@@ -74,5 +72,6 @@
|
|
|
74
72
|
"build": "modern build",
|
|
75
73
|
"dev": "modern build --watch",
|
|
76
74
|
"test": "modern test --passWithNoTests"
|
|
77
|
-
}
|
|
75
|
+
},
|
|
76
|
+
"readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
|
|
78
77
|
}
|