@modern-js/plugin-ssg 0.0.0-next-45d726979 → 0.0.0-next-0bf5ec13d2

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.
@@ -0,0 +1,334 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _asyncToGenerator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ function _defineProperty(obj, key, value) {
31
+ if (key in obj) {
32
+ Object.defineProperty(obj, key, {
33
+ value: value,
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true
37
+ });
38
+ } else {
39
+ obj[key] = value;
40
+ }
41
+ return obj;
42
+ }
43
+ function _objectSpread(target) {
44
+ for(var i = 1; i < arguments.length; i++){
45
+ var source = arguments[i] != null ? arguments[i] : {};
46
+ var ownKeys = Object.keys(source);
47
+ if (typeof Object.getOwnPropertySymbols === "function") {
48
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
49
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
50
+ }));
51
+ }
52
+ ownKeys.forEach(function(key) {
53
+ _defineProperty(target, key, source[key]);
54
+ });
55
+ }
56
+ return target;
57
+ }
58
+ function ownKeys(object, enumerableOnly) {
59
+ var keys = Object.keys(object);
60
+ if (Object.getOwnPropertySymbols) {
61
+ var symbols = Object.getOwnPropertySymbols(object);
62
+ if (enumerableOnly) {
63
+ symbols = symbols.filter(function(sym) {
64
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
65
+ });
66
+ }
67
+ keys.push.apply(keys, symbols);
68
+ }
69
+ return keys;
70
+ }
71
+ function _objectSpreadProps(target, source) {
72
+ source = source != null ? source : {};
73
+ if (Object.getOwnPropertyDescriptors) {
74
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
75
+ } else {
76
+ ownKeys(Object(source)).forEach(function(key) {
77
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
78
+ });
79
+ }
80
+ return target;
81
+ }
82
+ var __generator = this && this.__generator || function(thisArg, body) {
83
+ var f, y, t, g, _ = {
84
+ label: 0,
85
+ sent: function() {
86
+ if (t[0] & 1) throw t[1];
87
+ return t[1];
88
+ },
89
+ trys: [],
90
+ ops: []
91
+ };
92
+ return g = {
93
+ next: verb(0),
94
+ "throw": verb(1),
95
+ "return": verb(2)
96
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
97
+ return this;
98
+ }), g;
99
+ function verb(n) {
100
+ return function(v) {
101
+ return step([
102
+ n,
103
+ v
104
+ ]);
105
+ };
106
+ }
107
+ function step(op) {
108
+ if (f) throw new TypeError("Generator is already executing.");
109
+ while(_)try {
110
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
111
+ if (y = 0, t) op = [
112
+ op[0] & 2,
113
+ t.value
114
+ ];
115
+ switch(op[0]){
116
+ case 0:
117
+ case 1:
118
+ t = op;
119
+ break;
120
+ case 4:
121
+ _.label++;
122
+ return {
123
+ value: op[1],
124
+ done: false
125
+ };
126
+ case 5:
127
+ _.label++;
128
+ y = op[1];
129
+ op = [
130
+ 0
131
+ ];
132
+ continue;
133
+ case 7:
134
+ op = _.ops.pop();
135
+ _.trys.pop();
136
+ continue;
137
+ default:
138
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
139
+ _ = 0;
140
+ continue;
141
+ }
142
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
143
+ _.label = op[1];
144
+ break;
145
+ }
146
+ if (op[0] === 6 && _.label < t[1]) {
147
+ _.label = t[1];
148
+ t = op;
149
+ break;
150
+ }
151
+ if (t && _.label < t[2]) {
152
+ _.label = t[2];
153
+ _.ops.push(op);
154
+ break;
155
+ }
156
+ if (t[2]) _.ops.pop();
157
+ _.trys.pop();
158
+ continue;
159
+ }
160
+ op = body.call(thisArg, _);
161
+ } catch (e) {
162
+ op = [
163
+ 6,
164
+ e
165
+ ];
166
+ y = 0;
167
+ } finally{
168
+ f = t = 0;
169
+ }
170
+ if (op[0] & 5) throw op[1];
171
+ return {
172
+ value: op[0] ? op[1] : void 0,
173
+ done: true
174
+ };
175
+ }
176
+ };
177
+ import path from "path";
178
+ import { logger, PLUGIN_SCHEMAS } from "@modern-js/utils";
179
+ import { generatePath } from "react-router-dom";
180
+ import { flattenRoutes, formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util";
181
+ import { createServer } from "./server";
182
+ import { writeHtmlFile } from "./libs/output";
183
+ import { replaceRoute } from "./libs/replace";
184
+ import { makeRoute } from "./libs/make";
185
+ var src_default = function() {
186
+ return {
187
+ name: "@modern-js/plugin-ssg",
188
+ pre: [
189
+ "@modern-js/plugin-server",
190
+ "@modern-js/plugin-bff"
191
+ ],
192
+ setup: function(api) {
193
+ var agreedRouteMap = {};
194
+ return {
195
+ validateSchema: function validateSchema() {
196
+ return PLUGIN_SCHEMAS["@modern-js/plugin-ssg"];
197
+ },
198
+ modifyFileSystemRoutes: function modifyFileSystemRoutes(param) {
199
+ var entrypoint = param.entrypoint, routes = param.routes;
200
+ var entryName = entrypoint.entryName;
201
+ var flattedRoutes = flattenRoutes(routes);
202
+ agreedRouteMap[entryName] = flattedRoutes;
203
+ return {
204
+ entrypoint: entrypoint,
205
+ routes: routes
206
+ };
207
+ },
208
+ afterBuild: function afterBuild() {
209
+ return _asyncToGenerator(function() {
210
+ var resolvedConfig, appContext, appDirectory, entrypoints, output, server, ssg, tmp, _ref, outputPath, ssgOptions, buildDir, routes, pageRoutes, apiRoutes, intermediateOptions, ssgRoutes, htmlAry;
211
+ return __generator(this, function(_state) {
212
+ switch(_state.label){
213
+ case 0:
214
+ resolvedConfig = api.useResolvedConfigContext();
215
+ appContext = api.useAppContext();
216
+ appDirectory = appContext.appDirectory, entrypoints = appContext.entrypoints;
217
+ output = resolvedConfig.output, server = resolvedConfig.server;
218
+ ssg = output.ssg, tmp = output.distPath, _ref = tmp === void 0 ? {} : tmp, outputPath = _ref.root;
219
+ ssgOptions = (Array.isArray(ssg) ? ssg.pop() : ssg) || true;
220
+ buildDir = path.join(appDirectory, outputPath);
221
+ routes = readJSONSpec(buildDir);
222
+ pageRoutes = routes.filter(function(route) {
223
+ return !route.isApi;
224
+ });
225
+ apiRoutes = routes.filter(function(route) {
226
+ return route.isApi;
227
+ });
228
+ if (pageRoutes.length === 0) {
229
+ return [
230
+ 2
231
+ ];
232
+ }
233
+ intermediateOptions = standardOptions(ssgOptions, entrypoints, pageRoutes, server);
234
+ if (!intermediateOptions) {
235
+ return [
236
+ 2
237
+ ];
238
+ }
239
+ ssgRoutes = [];
240
+ pageRoutes.forEach(function(pageRoute) {
241
+ var entryName = pageRoute.entryName, entryPath = pageRoute.entryPath;
242
+ var agreedRoutes = agreedRouteMap[entryName];
243
+ var entryOptions = intermediateOptions[entryName] || intermediateOptions[pageRoute.urlPath];
244
+ if (!agreedRoutes) {
245
+ if (!entryOptions) {
246
+ return;
247
+ }
248
+ if (entryOptions === true) {
249
+ ssgRoutes.push(_objectSpreadProps(_objectSpread({}, pageRoute), {
250
+ output: entryPath
251
+ }));
252
+ } else if (entryOptions.routes && entryOptions.routes.length > 0) {
253
+ var enrtyRoutes = entryOptions.routes, headers = entryOptions.headers;
254
+ enrtyRoutes.forEach(function(route) {
255
+ ssgRoutes.push(makeRoute(pageRoute, route, headers));
256
+ });
257
+ }
258
+ } else {
259
+ if (!entryOptions) {
260
+ return;
261
+ }
262
+ if (entryOptions === true) {
263
+ entryOptions = {
264
+ preventDefault: [],
265
+ routes: [],
266
+ headers: {}
267
+ };
268
+ }
269
+ var _entryOptions_preventDefault = entryOptions.preventDefault, preventDefault = _entryOptions_preventDefault === void 0 ? [] : _entryOptions_preventDefault, tmp = entryOptions.routes, userRoutes = tmp === void 0 ? [] : tmp, headers1 = entryOptions.headers;
270
+ if (userRoutes.length > 0) {
271
+ userRoutes.forEach(function(route) {
272
+ if (typeof route === "string") {
273
+ ssgRoutes.push(makeRoute(pageRoute, route, headers1));
274
+ } else if (Array.isArray(route.params)) {
275
+ route.params.forEach(function(param) {
276
+ ssgRoutes.push(makeRoute(pageRoute, _objectSpreadProps(_objectSpread({}, route), {
277
+ url: generatePath(route.url, param)
278
+ }), headers1));
279
+ });
280
+ } else {
281
+ ssgRoutes.push(makeRoute(pageRoute, route, headers1));
282
+ }
283
+ });
284
+ } else {
285
+ agreedRoutes.filter(function(route) {
286
+ return !preventDefault.includes(route.path);
287
+ }).forEach(function(route) {
288
+ if (!isDynamicUrl(route.path)) {
289
+ ssgRoutes.push(makeRoute(pageRoute, route.path, headers1));
290
+ }
291
+ });
292
+ }
293
+ }
294
+ });
295
+ if (ssgRoutes.length === 0) {
296
+ return [
297
+ 2
298
+ ];
299
+ }
300
+ ssgRoutes.forEach(function(ssgRoute) {
301
+ if (ssgRoute.isSSR) {
302
+ var isOriginRoute = pageRoutes.some(function(pageRoute) {
303
+ return pageRoute.urlPath === ssgRoute.urlPath && pageRoute.entryName === ssgRoute.entryName;
304
+ });
305
+ if (isOriginRoute) {
306
+ throw new Error("ssg can not using with ssr,url - ".concat(ssgRoute.urlPath, ", entry - ").concat(ssgRoute.entryName, " "));
307
+ }
308
+ logger.warn("new ssg route ".concat(ssgRoute.urlPath, " is using ssr now,maybe from parent route ").concat(ssgRoute.entryName, ",close ssr"));
309
+ }
310
+ ssgRoute.isSSR = false;
311
+ ssgRoute.output = formatOutput(ssgRoute.output);
312
+ });
313
+ return [
314
+ 4,
315
+ createServer(api, ssgRoutes, pageRoutes, apiRoutes, resolvedConfig, appDirectory)
316
+ ];
317
+ case 1:
318
+ htmlAry = _state.sent();
319
+ writeHtmlFile(htmlAry, ssgRoutes, buildDir);
320
+ replaceRoute(ssgRoutes, pageRoutes);
321
+ writeJSONSpec(buildDir, pageRoutes.concat(apiRoutes));
322
+ logger.info("ssg Compiled successfully");
323
+ return [
324
+ 2
325
+ ];
326
+ }
327
+ });
328
+ })();
329
+ }
330
+ };
331
+ }
332
+ };
333
+ };
334
+ export { src_default as default };
@@ -0,0 +1,83 @@
1
+ function _defineProperty(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ function _objectSpread(target) {
15
+ for(var i = 1; i < arguments.length; i++){
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
22
+ }
23
+ ownKeys.forEach(function(key) {
24
+ _defineProperty(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
+ }
29
+ function ownKeys(object, enumerableOnly) {
30
+ var keys = Object.keys(object);
31
+ if (Object.getOwnPropertySymbols) {
32
+ var symbols = Object.getOwnPropertySymbols(object);
33
+ if (enumerableOnly) {
34
+ symbols = symbols.filter(function(sym) {
35
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
+ });
37
+ }
38
+ keys.push.apply(keys, symbols);
39
+ }
40
+ return keys;
41
+ }
42
+ function _objectSpreadProps(target, source) {
43
+ source = source != null ? source : {};
44
+ if (Object.getOwnPropertyDescriptors) {
45
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
+ } else {
47
+ ownKeys(Object(source)).forEach(function(key) {
48
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
+ });
50
+ }
51
+ return target;
52
+ }
53
+ import path from "path";
54
+ import normalize from "normalize-path";
55
+ function makeRender(ssgRoutes, render, port) {
56
+ return ssgRoutes.map(function(ssgRoute) {
57
+ return render({
58
+ url: ssgRoute.urlPath,
59
+ headers: _objectSpread({
60
+ host: "localhost:".concat(port)
61
+ }, ssgRoute.headers),
62
+ connection: {}
63
+ });
64
+ });
65
+ }
66
+ function makeRoute(baseRoute, route) {
67
+ var headers = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
68
+ var urlPath = baseRoute.urlPath, entryPath = baseRoute.entryPath;
69
+ if (typeof route === "string") {
70
+ return _objectSpreadProps(_objectSpread({}, baseRoute), {
71
+ urlPath: normalize("".concat(urlPath).concat(route)) || "/",
72
+ headers: headers,
73
+ output: path.join(entryPath, "..".concat(route === "/" ? "" : route))
74
+ });
75
+ } else {
76
+ return _objectSpreadProps(_objectSpread({}, baseRoute), {
77
+ urlPath: normalize("".concat(urlPath).concat(route.url)) || "/",
78
+ headers: _objectSpread({}, headers, route.headers),
79
+ output: route.output ? path.normalize(route.output) : path.join(entryPath, "..".concat(route.url === "/" ? "" : route.url))
80
+ });
81
+ }
82
+ }
83
+ export { makeRender, makeRoute };
@@ -0,0 +1,13 @@
1
+ import path from "path";
2
+ import { fs } from "@modern-js/utils";
3
+ function writeHtmlFile(htmlAry, ssgRoutes, baseDir) {
4
+ htmlAry.forEach(function(html, index) {
5
+ var ssgRoute = ssgRoutes[index];
6
+ var filepath = path.join(baseDir, ssgRoute.output);
7
+ if (!fs.existsSync(path.dirname(filepath))) {
8
+ fs.ensureDirSync(path.dirname(filepath));
9
+ }
10
+ fs.writeFileSync(filepath, html);
11
+ });
12
+ }
13
+ export { writeHtmlFile };
@@ -0,0 +1,115 @@
1
+ function _arrayLikeToArray(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _arrayWithoutHoles(arr) {
7
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
8
+ }
9
+ function _defineProperty(obj, key, value) {
10
+ if (key in obj) {
11
+ Object.defineProperty(obj, key, {
12
+ value: value,
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true
16
+ });
17
+ } else {
18
+ obj[key] = value;
19
+ }
20
+ return obj;
21
+ }
22
+ function _iterableToArray(iter) {
23
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
24
+ }
25
+ function _nonIterableSpread() {
26
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
27
+ }
28
+ function _objectSpread(target) {
29
+ for(var i = 1; i < arguments.length; i++){
30
+ var source = arguments[i] != null ? arguments[i] : {};
31
+ var ownKeys = Object.keys(source);
32
+ if (typeof Object.getOwnPropertySymbols === "function") {
33
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
34
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
35
+ }));
36
+ }
37
+ ownKeys.forEach(function(key) {
38
+ _defineProperty(target, key, source[key]);
39
+ });
40
+ }
41
+ return target;
42
+ }
43
+ function _objectWithoutProperties(source, excluded) {
44
+ if (source == null) return {};
45
+ var target = _objectWithoutPropertiesLoose(source, excluded);
46
+ var key, i;
47
+ if (Object.getOwnPropertySymbols) {
48
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
49
+ for(i = 0; i < sourceSymbolKeys.length; i++){
50
+ key = sourceSymbolKeys[i];
51
+ if (excluded.indexOf(key) >= 0) continue;
52
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
53
+ target[key] = source[key];
54
+ }
55
+ }
56
+ return target;
57
+ }
58
+ function _objectWithoutPropertiesLoose(source, excluded) {
59
+ if (source == null) return {};
60
+ var target = {};
61
+ var sourceKeys = Object.keys(source);
62
+ var key, i;
63
+ for(i = 0; i < sourceKeys.length; i++){
64
+ key = sourceKeys[i];
65
+ if (excluded.indexOf(key) >= 0) continue;
66
+ target[key] = source[key];
67
+ }
68
+ return target;
69
+ }
70
+ function _toConsumableArray(arr) {
71
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
72
+ }
73
+ function _unsupportedIterableToArray(o, minLen) {
74
+ if (!o) return;
75
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
76
+ var n = Object.prototype.toString.call(o).slice(8, -1);
77
+ if (n === "Object" && o.constructor) n = o.constructor.name;
78
+ if (n === "Map" || n === "Set") return Array.from(n);
79
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
80
+ }
81
+ import normalize from "normalize-path";
82
+ function exist(route, pageRoutes) {
83
+ return pageRoutes.slice().findIndex(function(pageRoute) {
84
+ var urlEqual = normalize(pageRoute.urlPath) === normalize(route.urlPath);
85
+ var entryEqual = pageRoute.entryName === route.entryName;
86
+ if (urlEqual && entryEqual) {
87
+ return true;
88
+ }
89
+ return false;
90
+ });
91
+ }
92
+ function replaceRoute(ssgRoutes, pageRoutes) {
93
+ var _pageRoutes;
94
+ var cleanSsgRoutes = ssgRoutes.map(function(ssgRoute) {
95
+ var output = ssgRoute.output, headers = ssgRoute.headers, cleanSsgRoute = _objectWithoutProperties(ssgRoute, [
96
+ "output",
97
+ "headers"
98
+ ]);
99
+ return Object.assign(cleanSsgRoute, output ? {
100
+ entryPath: output
101
+ } : {});
102
+ });
103
+ var freshRoutes = [];
104
+ cleanSsgRoutes.forEach(function(ssgRoute) {
105
+ var index = exist(ssgRoute, pageRoutes);
106
+ if (index < 0) {
107
+ freshRoutes.push(_objectSpread({}, ssgRoute));
108
+ } else {
109
+ pageRoutes[index].entryPath = ssgRoute.entryPath;
110
+ }
111
+ });
112
+ (_pageRoutes = pageRoutes).push.apply(_pageRoutes, _toConsumableArray(freshRoutes));
113
+ return pageRoutes;
114
+ }
115
+ export { exist, replaceRoute };