@modern-js/plugin-ssg 1.0.1 → 1.1.2
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 +38 -6
- package/dist/js/modern/index.js +96 -58
- package/dist/js/modern/libs/make.js +37 -0
- package/dist/js/modern/libs/replace.js +5 -4
- package/dist/js/modern/libs/util.js +38 -22
- package/dist/js/modern/server/index.js +2 -2
- package/dist/js/modern/server/prerender.js +2 -2
- package/dist/js/modern/server/process.js +1 -1
- package/dist/js/modern/types.js +0 -1
- package/dist/js/node/index.js +92 -55
- package/dist/js/node/libs/make.js +50 -0
- package/dist/js/node/libs/replace.js +5 -4
- package/dist/js/node/libs/util.js +38 -29
- package/dist/js/node/server/index.js +2 -2
- package/dist/js/node/server/prerender.js +2 -2
- package/dist/js/node/server/process.js +2 -2
- 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 +84 -83
- package/src/libs/make.ts +45 -0
- package/src/libs/output.ts +1 -1
- package/src/libs/replace.ts +7 -4
- package/src/libs/util.ts +39 -27
- package/src/server/index.ts +1 -1
- package/src/server/process.ts +3 -2
- package/src/types.ts +26 -20
- package/tests/lib.test.ts +48 -169
- package/tests/util.test.ts +71 -32
- package/dist/js/modern/libs/createPage.js +0 -46
- package/dist/js/modern/libs/invoker.js +0 -56
- 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 -101
- package/dist/js/node/libs/createPage.js +0 -57
- package/dist/js/node/libs/invoker.js +0 -67
- 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 -124
- 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 -56
- package/src/libs/render.ts +0 -16
- package/src/loader/index.ts +0 -99
- package/src/manifest-op.ts +0 -111
- package/tests/operate.test.ts +0 -39
package/CHANGELOG.md
CHANGED
|
@@ -1,14 +1,46 @@
|
|
|
1
1
|
# @modern-js/plugin-ssg
|
|
2
2
|
|
|
3
|
-
## 1.
|
|
3
|
+
## 1.1.2
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
- d73ff455: support multi process product
|
|
8
|
+
- d73ff455: support multi process product
|
|
9
|
+
- d73ff455: support multi process product
|
|
10
|
+
- d73ff455: support multi process product
|
|
11
|
+
- d73ff455: support multi process product
|
|
12
|
+
- Updated dependencies [d927bc83]
|
|
13
|
+
- Updated dependencies [d73ff455]
|
|
14
|
+
- Updated dependencies [9c1ab865]
|
|
15
|
+
- Updated dependencies [d73ff455]
|
|
16
|
+
- Updated dependencies [d73ff455]
|
|
17
|
+
- Updated dependencies [d73ff455]
|
|
18
|
+
- Updated dependencies [d73ff455]
|
|
19
|
+
- @modern-js/utils@1.1.4
|
|
20
|
+
- @modern-js/core@1.1.4
|
|
21
|
+
|
|
22
|
+
## 1.1.1
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- 0fa83663: support more .env files
|
|
27
|
+
- Updated dependencies [6f7fe574]
|
|
28
|
+
- Updated dependencies [0fa83663]
|
|
29
|
+
- Updated dependencies [f594fbc8]
|
|
30
|
+
- @modern-js/core@1.1.2
|
|
31
|
+
- @modern-js/utils@1.1.2
|
|
32
|
+
|
|
33
|
+
## 1.1.0
|
|
34
|
+
|
|
35
|
+
### Minor Changes
|
|
36
|
+
|
|
37
|
+
- 96119db2: Relese v1.1.0
|
|
38
|
+
|
|
39
|
+
### Patch Changes
|
|
40
|
+
|
|
41
|
+
- Updated dependencies [96119db2]
|
|
42
|
+
- @modern-js/core@1.1.0
|
|
43
|
+
- @modern-js/utils@1.1.0
|
|
12
44
|
|
|
13
45
|
## 1.0.0
|
|
14
46
|
|
package/dist/js/modern/index.js
CHANGED
|
@@ -1,38 +1,21 @@
|
|
|
1
|
+
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; }
|
|
2
|
+
|
|
3
|
+
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; }
|
|
4
|
+
|
|
5
|
+
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; }
|
|
6
|
+
|
|
1
7
|
import path from 'path';
|
|
2
|
-
import {
|
|
8
|
+
import { logger, PLUGIN_SCHEMAS } from '@modern-js/utils';
|
|
3
9
|
import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
|
|
4
|
-
import {
|
|
5
|
-
import { formatOutput,
|
|
6
|
-
import { invoker } from "./libs/invoker";
|
|
10
|
+
import { generatePath } from 'react-router-dom';
|
|
11
|
+
import { formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util";
|
|
7
12
|
import { createServer } from "./server";
|
|
8
13
|
import { writeHtmlFile } from "./libs/output";
|
|
9
14
|
import { replaceRoute } from "./libs/replace";
|
|
10
|
-
|
|
11
|
-
const listStaticFiles = (pwd, entriesDir, useSSG) => {
|
|
12
|
-
const absEntriesDir = path.join(pwd, entriesDir);
|
|
13
|
-
const staticRenderLevel = getSSGRenderLevel(useSSG);
|
|
14
|
-
const staticFiles = new LoaderManifest().get(absEntriesDir, staticRenderLevel); // 将绝对路径转换成 alias,因为获取到的约定路由也是使用别名的
|
|
15
|
-
|
|
16
|
-
const staticAlias = staticFiles.map(filepath => replaceWithAlias(path.join(pwd, 'src'), filepath, INTERNAL_SRC_ALIAS));
|
|
17
|
-
return staticAlias;
|
|
18
|
-
};
|
|
19
|
-
|
|
15
|
+
import { makeRoute } from "./libs/make";
|
|
20
16
|
export default createPlugin(() => {
|
|
21
17
|
const agreedRouteMap = {};
|
|
22
18
|
return {
|
|
23
|
-
config() {
|
|
24
|
-
return {
|
|
25
|
-
tools: {
|
|
26
|
-
babel(config, {
|
|
27
|
-
chain
|
|
28
|
-
}) {
|
|
29
|
-
chain.plugin('./loader').use(require.resolve("./loader"));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
},
|
|
35
|
-
|
|
36
19
|
validateSchema() {
|
|
37
20
|
return PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
|
|
38
21
|
},
|
|
@@ -58,16 +41,11 @@ export default createPlugin(() => {
|
|
|
58
41
|
|
|
59
42
|
const appContext = useAppContext();
|
|
60
43
|
const {
|
|
61
|
-
appDirectory
|
|
44
|
+
appDirectory,
|
|
45
|
+
entrypoints
|
|
62
46
|
} = appContext;
|
|
63
47
|
const {
|
|
64
|
-
output
|
|
65
|
-
server: {
|
|
66
|
-
baseUrl
|
|
67
|
-
},
|
|
68
|
-
source: {
|
|
69
|
-
entriesDir
|
|
70
|
-
}
|
|
48
|
+
output
|
|
71
49
|
} = resolvedConfig;
|
|
72
50
|
const {
|
|
73
51
|
ssg,
|
|
@@ -79,13 +57,8 @@ export default createPlugin(() => {
|
|
|
79
57
|
return;
|
|
80
58
|
}
|
|
81
59
|
|
|
82
|
-
const {
|
|
83
|
-
useSSG,
|
|
84
|
-
userHook
|
|
85
|
-
} = parsedSSGConfig(ssgOptions);
|
|
86
60
|
const buildDir = path.join(appDirectory, outputPath);
|
|
87
|
-
const routes = readJSONSpec(buildDir);
|
|
88
|
-
const staticAlias = listStaticFiles(appDirectory, entriesDir, useSSG); // filter all routes not web
|
|
61
|
+
const routes = readJSONSpec(buildDir); // filter all routes not web
|
|
89
62
|
|
|
90
63
|
const pageRoutes = routes.filter(route => !route.isApi);
|
|
91
64
|
const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
|
|
@@ -94,27 +67,91 @@ export default createPlugin(() => {
|
|
|
94
67
|
return;
|
|
95
68
|
}
|
|
96
69
|
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
const listener = (route, agreed) => {
|
|
100
|
-
const urlPrefix = getUrlPrefix(route, baseUrl);
|
|
101
|
-
const ssgOutput = getOutput(route, urlPrefix, agreed);
|
|
102
|
-
route.output = formatOutput(route.entryPath, ssgOutput);
|
|
103
|
-
ssgRoutes.push(route);
|
|
104
|
-
}; // check if every allowed agreed route was collected
|
|
105
|
-
|
|
70
|
+
const intermediateOptions = standardOptions(ssgOptions, entrypoints);
|
|
106
71
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return false;
|
|
111
|
-
} // if allowed, return collection state
|
|
72
|
+
if (!intermediateOptions) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
112
75
|
|
|
76
|
+
const ssgRoutes = []; // each route will try to match the configuration
|
|
77
|
+
|
|
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
|
+
}
|
|
113
116
|
|
|
114
|
-
|
|
115
|
-
|
|
117
|
+
if (!entryOptions || entryOptions === true) {
|
|
118
|
+
entryOptions = {
|
|
119
|
+
preventDefault: [],
|
|
120
|
+
routes: [],
|
|
121
|
+
headers: {}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
116
124
|
|
|
117
|
-
|
|
125
|
+
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') {
|
|
134
|
+
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
|
+
});
|
|
145
|
+
} 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
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
118
155
|
|
|
119
156
|
if (ssgRoutes.length === 0) {
|
|
120
157
|
return;
|
|
@@ -133,6 +170,7 @@ export default createPlugin(() => {
|
|
|
133
170
|
}
|
|
134
171
|
|
|
135
172
|
ssgRoute.isSSR = false;
|
|
173
|
+
ssgRoute.output = formatOutput(ssgRoute.output);
|
|
136
174
|
});
|
|
137
175
|
const htmlAry = await createServer(ssgRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
|
|
138
176
|
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
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; }
|
|
2
|
+
|
|
3
|
+
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; }
|
|
4
|
+
|
|
5
|
+
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; }
|
|
6
|
+
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import normalize from 'normalize-path';
|
|
9
|
+
export function makeRender(ssgRoutes, render, port) {
|
|
10
|
+
return ssgRoutes.map(ssgRoute => render({
|
|
11
|
+
url: ssgRoute.urlPath,
|
|
12
|
+
headers: _objectSpread({
|
|
13
|
+
host: `localhost:${port}`
|
|
14
|
+
}, ssgRoute.headers),
|
|
15
|
+
connection: {}
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
export function makeRoute(baseRoute, route, headers = {}) {
|
|
19
|
+
const {
|
|
20
|
+
urlPath,
|
|
21
|
+
entryPath
|
|
22
|
+
} = baseRoute;
|
|
23
|
+
|
|
24
|
+
if (typeof route === 'string') {
|
|
25
|
+
return _objectSpread(_objectSpread({}, baseRoute), {}, {
|
|
26
|
+
urlPath: normalize(`${urlPath}${route}`) || '/',
|
|
27
|
+
headers,
|
|
28
|
+
output: path.join(entryPath, `..${route}`)
|
|
29
|
+
});
|
|
30
|
+
} else {
|
|
31
|
+
return _objectSpread(_objectSpread({}, baseRoute), {}, {
|
|
32
|
+
urlPath: normalize(`${urlPath}${route.url}`) || '/',
|
|
33
|
+
headers: _objectSpread(_objectSpread({}, headers), route.headers),
|
|
34
|
+
output: route.output ? path.normalize(route.output) : path.join(entryPath, `..${route.url}`)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const _excluded = ["output"];
|
|
1
|
+
const _excluded = ["output", "headers"];
|
|
2
2
|
|
|
3
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object);
|
|
3
|
+
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; }
|
|
4
4
|
|
|
5
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]
|
|
5
|
+
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; }
|
|
6
6
|
|
|
7
7
|
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; }
|
|
8
8
|
|
|
@@ -27,7 +27,8 @@ export function replaceRoute(ssgRoutes, pageRoutes) {
|
|
|
27
27
|
// remove redundant fields and replace rendered entryPath
|
|
28
28
|
const cleanSsgRoutes = ssgRoutes.map(ssgRoute => {
|
|
29
29
|
const {
|
|
30
|
-
output
|
|
30
|
+
output,
|
|
31
|
+
headers
|
|
31
32
|
} = ssgRoute,
|
|
32
33
|
cleanSsgRoute = _objectWithoutProperties(ssgRoute, _excluded);
|
|
33
34
|
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { ROUTE_SPEC_FILE, fs } from '@modern-js/utils';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const dirname = path.dirname(base);
|
|
7
|
-
return path.join(dirname, file);
|
|
2
|
+
import { ROUTE_SPEC_FILE, fs, isSingleEntry } from '@modern-js/utils';
|
|
3
|
+
export function formatOutput(filename) {
|
|
4
|
+
const outputPath = path.extname(filename) ? filename : `${filename}/index.html`;
|
|
5
|
+
return outputPath;
|
|
8
6
|
}
|
|
9
7
|
export function formatPath(str) {
|
|
10
8
|
let addr = str;
|
|
@@ -90,22 +88,40 @@ export const writeJSONSpec = (dir, routes) => {
|
|
|
90
88
|
spaces: 2
|
|
91
89
|
});
|
|
92
90
|
};
|
|
93
|
-
export const
|
|
94
|
-
|
|
91
|
+
export const replaceWithAlias = (base, filePath, alias) => path.posix.join(alias, path.posix.relative(base, filePath));
|
|
92
|
+
export const standardOptions = (ssgOptions, entrypoints) => {
|
|
93
|
+
if (ssgOptions === false) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (ssgOptions === true) {
|
|
98
|
+
return entrypoints.reduce((opt, entry) => {
|
|
99
|
+
opt[entry.entryName] = ssgOptions;
|
|
100
|
+
return opt;
|
|
101
|
+
}, {});
|
|
102
|
+
} else if (typeof ssgOptions === 'object') {
|
|
103
|
+
const isSingle = isSingleEntry(entrypoints);
|
|
104
|
+
|
|
105
|
+
if (isSingle && typeof ssgOptions.main === 'undefined') {
|
|
106
|
+
return {
|
|
107
|
+
main: ssgOptions
|
|
108
|
+
};
|
|
109
|
+
} else {
|
|
110
|
+
return ssgOptions;
|
|
111
|
+
}
|
|
112
|
+
} else if (typeof ssgOptions === 'function') {
|
|
113
|
+
const intermediateOptions = {};
|
|
114
|
+
|
|
115
|
+
for (const entrypoint of entrypoints) {
|
|
116
|
+
const {
|
|
117
|
+
entryName
|
|
118
|
+
} = entrypoint; // Todo may be async function
|
|
95
119
|
|
|
96
|
-
|
|
97
|
-
|
|
120
|
+
intermediateOptions[entryName] = ssgOptions(entryName);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return intermediateOptions;
|
|
98
124
|
}
|
|
99
125
|
|
|
100
|
-
return
|
|
101
|
-
};
|
|
102
|
-
export const parsedSSGConfig = ssg => {
|
|
103
|
-
const useSSG = Boolean(ssg); // eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
104
|
-
|
|
105
|
-
const userHook = typeof ssg === 'function' ? ssg : () => {};
|
|
106
|
-
return {
|
|
107
|
-
useSSG,
|
|
108
|
-
userHook
|
|
109
|
-
};
|
|
110
|
-
};
|
|
111
|
-
export const replaceWithAlias = (base, filePath, alias) => path.join(alias, path.relative(base, filePath));
|
|
126
|
+
return false;
|
|
127
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object);
|
|
1
|
+
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; }
|
|
2
2
|
|
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]
|
|
3
|
+
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; }
|
|
4
4
|
|
|
5
5
|
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; }
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object);
|
|
1
|
+
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; }
|
|
2
2
|
|
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]
|
|
3
|
+
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; }
|
|
4
4
|
|
|
5
5
|
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; }
|
|
6
6
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Server from '@modern-js/server';
|
|
2
2
|
import portfinder from 'portfinder';
|
|
3
3
|
import { compatRequire } from '@modern-js/utils';
|
|
4
|
-
import { makeRender } from "../libs/
|
|
4
|
+
import { makeRender } from "../libs/make";
|
|
5
5
|
import { compile as createRender } from "./prerender";
|
|
6
6
|
import { CLOSE_SIGN } from "./consts";
|
|
7
7
|
|
package/dist/js/modern/types.js
CHANGED
package/dist/js/node/index.js
CHANGED
|
@@ -11,46 +11,29 @@ var _utils = require("@modern-js/utils");
|
|
|
11
11
|
|
|
12
12
|
var _core = require("@modern-js/core");
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var _reactRouterDom = require("react-router-dom");
|
|
15
15
|
|
|
16
16
|
var _util = require("./libs/util");
|
|
17
17
|
|
|
18
|
-
var _invoker = require("./libs/invoker");
|
|
19
|
-
|
|
20
18
|
var _server = require("./server");
|
|
21
19
|
|
|
22
20
|
var _output = require("./libs/output");
|
|
23
21
|
|
|
24
22
|
var _replace = require("./libs/replace");
|
|
25
23
|
|
|
24
|
+
var _make = require("./libs/make");
|
|
25
|
+
|
|
26
26
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
const absEntriesDir = _path.default.join(pwd, entriesDir);
|
|
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; }
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
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; }
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
return staticAlias;
|
|
36
|
-
};
|
|
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; }
|
|
37
33
|
|
|
38
34
|
var _default = (0, _core.createPlugin)(() => {
|
|
39
35
|
const agreedRouteMap = {};
|
|
40
36
|
return {
|
|
41
|
-
config() {
|
|
42
|
-
return {
|
|
43
|
-
tools: {
|
|
44
|
-
babel(config, {
|
|
45
|
-
chain
|
|
46
|
-
}) {
|
|
47
|
-
chain.plugin('./loader').use(require.resolve("./loader"));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
},
|
|
53
|
-
|
|
54
37
|
validateSchema() {
|
|
55
38
|
return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-ssg'];
|
|
56
39
|
},
|
|
@@ -76,16 +59,11 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
76
59
|
|
|
77
60
|
const appContext = (0, _core.useAppContext)();
|
|
78
61
|
const {
|
|
79
|
-
appDirectory
|
|
62
|
+
appDirectory,
|
|
63
|
+
entrypoints
|
|
80
64
|
} = appContext;
|
|
81
65
|
const {
|
|
82
|
-
output
|
|
83
|
-
server: {
|
|
84
|
-
baseUrl
|
|
85
|
-
},
|
|
86
|
-
source: {
|
|
87
|
-
entriesDir
|
|
88
|
-
}
|
|
66
|
+
output
|
|
89
67
|
} = resolvedConfig;
|
|
90
68
|
const {
|
|
91
69
|
ssg,
|
|
@@ -97,15 +75,9 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
97
75
|
return;
|
|
98
76
|
}
|
|
99
77
|
|
|
100
|
-
const {
|
|
101
|
-
useSSG,
|
|
102
|
-
userHook
|
|
103
|
-
} = (0, _util.parsedSSGConfig)(ssgOptions);
|
|
104
|
-
|
|
105
78
|
const buildDir = _path.default.join(appDirectory, outputPath);
|
|
106
79
|
|
|
107
|
-
const routes = (0, _util.readJSONSpec)(buildDir);
|
|
108
|
-
const staticAlias = listStaticFiles(appDirectory, entriesDir, useSSG); // filter all routes not web
|
|
80
|
+
const routes = (0, _util.readJSONSpec)(buildDir); // filter all routes not web
|
|
109
81
|
|
|
110
82
|
const pageRoutes = routes.filter(route => !route.isApi);
|
|
111
83
|
const apiRoutes = routes.filter(route => route.isApi); // if no web page route, skip ssg render
|
|
@@ -114,27 +86,91 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
114
86
|
return;
|
|
115
87
|
}
|
|
116
88
|
|
|
117
|
-
const
|
|
89
|
+
const intermediateOptions = (0, _util.standardOptions)(ssgOptions, entrypoints);
|
|
118
90
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
route.output = (0, _util.formatOutput)(route.entryPath, ssgOutput);
|
|
123
|
-
ssgRoutes.push(route);
|
|
124
|
-
}; // check if every allowed agreed route was collected
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const autoAddAgreed = context => {
|
|
128
|
-
// if not exist in allowed list, return false
|
|
129
|
-
if (!staticAlias.includes(context.component)) {
|
|
130
|
-
return false;
|
|
131
|
-
} // if allowed, return collection state
|
|
91
|
+
if (!intermediateOptions) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
132
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
|
+
}
|
|
133
135
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
+
if (!entryOptions || entryOptions === true) {
|
|
137
|
+
entryOptions = {
|
|
138
|
+
preventDefault: [],
|
|
139
|
+
routes: [],
|
|
140
|
+
headers: {}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
136
143
|
|
|
137
|
-
|
|
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
|
+
});
|
|
138
174
|
|
|
139
175
|
if (ssgRoutes.length === 0) {
|
|
140
176
|
return;
|
|
@@ -153,6 +189,7 @@ var _default = (0, _core.createPlugin)(() => {
|
|
|
153
189
|
}
|
|
154
190
|
|
|
155
191
|
ssgRoute.isSSR = false;
|
|
192
|
+
ssgRoute.output = (0, _util.formatOutput)(ssgRoute.output);
|
|
156
193
|
});
|
|
157
194
|
const htmlAry = await (0, _server.createServer)(ssgRoutes, apiRoutes, resolvedConfig, appDirectory); // write to dist file
|
|
158
195
|
|