@modern-js/plugin-ssg 1.21.3 → 2.0.0-beta.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/CHANGELOG.md CHANGED
@@ -1,10 +1,28 @@
1
1
  # @modern-js/plugin-ssg
2
2
 
3
- ## 1.21.3
3
+ ## 2.0.0-beta.0
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9: chore: v2
4
8
 
5
9
  ### Patch Changes
6
10
 
7
- - @modern-js/utils@1.21.3
11
+ - cc971eabf: refactor: move server plugin load logic in `@modern-js/core`
12
+ refactor:移除在 `@modern-js/core` 中的 server 插件加载逻辑
13
+ - 8b8e1bb57: feat: support nested routes
14
+ feat: 支持嵌套路由
15
+ - Updated dependencies [edd1cfb1a]
16
+ - Updated dependencies [cc971eabf]
17
+ - Updated dependencies [5b9049f]
18
+ - Updated dependencies [b8bbe036c]
19
+ - Updated dependencies [d5a31df78]
20
+ - Updated dependencies [dda38c9]
21
+ - Updated dependencies [3bbea92b2]
22
+ - Updated dependencies [abf3421]
23
+ - Updated dependencies [543be95]
24
+ - Updated dependencies [14b712d]
25
+ - @modern-js/utils@2.0.0-beta.0
8
26
 
9
27
  ## 1.21.2
10
28
 
@@ -7,13 +7,14 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
7
7
  import path from 'path';
8
8
  import { logger, PLUGIN_SCHEMAS } from '@modern-js/utils';
9
9
  import { generatePath } from 'react-router-dom';
10
- import { formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util";
10
+ import { flattenRoutes, formatOutput, isDynamicUrl, readJSONSpec, standardOptions, writeJSONSpec } from "./libs/util";
11
11
  import { createServer } from "./server";
12
12
  import { writeHtmlFile } from "./libs/output";
13
13
  import { replaceRoute } from "./libs/replace";
14
14
  import { makeRoute } from "./libs/make";
15
15
  export default (() => ({
16
16
  name: '@modern-js/plugin-ssg',
17
+ pre: ['@modern-js/plugin-server', '@modern-js/plugin-bff'],
17
18
  setup: api => {
18
19
  const agreedRouteMap = {};
19
20
  return {
@@ -28,7 +29,8 @@ export default (() => ({
28
29
  const {
29
30
  entryName
30
31
  } = entrypoint;
31
- agreedRouteMap[entryName] = routes;
32
+ const flattedRoutes = flattenRoutes(routes);
33
+ agreedRouteMap[entryName] = flattedRoutes;
32
34
  return {
33
35
  entrypoint,
34
36
  routes
@@ -146,4 +146,33 @@ export const standardOptions = (ssgOptions, entrypoints, routes, server) => {
146
146
  export const openRouteSSR = (routes, entries = []) => routes.map(ssgRoute => _objectSpread(_objectSpread({}, ssgRoute), {}, {
147
147
  isSSR: entries.includes(ssgRoute.entryName),
148
148
  bundle: `${SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
149
- }));
149
+ })); // TODO: 过滤带有 server loader 的路由
150
+
151
+ export const flattenRoutes = routes => {
152
+ const parents = [];
153
+ const newRoutes = [];
154
+
155
+ const traverseRoute = route => {
156
+ const parent = parents[parents.length - 1];
157
+ let path = parent ? `${parent.path}/${route.path || ''}`.replace(/\/+/g, '/') : route.path || ''; // If the route is an index route, the route has no path property
158
+
159
+ path = path.replace(/\/$/, ''); // If the route path is / and is not the root route, it should not be used as an ssg route
160
+
161
+ if (route._component && (path !== '/' || path === '/' && !parent)) {
162
+ newRoutes.push(_objectSpread(_objectSpread({}, route), {}, {
163
+ path
164
+ }));
165
+ }
166
+
167
+ if (route.children) {
168
+ parents.push(_objectSpread(_objectSpread({}, route), {}, {
169
+ path
170
+ }));
171
+ route.children.forEach(traverseRoute);
172
+ parents.pop();
173
+ }
174
+ };
175
+
176
+ routes.forEach(traverseRoute);
177
+ return newRoutes;
178
+ };
@@ -14,7 +14,7 @@ export const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, app
14
14
  silent: true
15
15
  });
16
16
  const appContext = api.useAppContext();
17
- const plugins = appContext.plugins.filter(p => p.server).map(p => p.server);
17
+ const plugins = appContext.serverInternalPlugins;
18
18
  cp.send(JSON.stringify({
19
19
  options,
20
20
  renderRoutes: ssgRoutes,
@@ -1,22 +1,8 @@
1
1
  import server from '@modern-js/prod-server';
2
2
  import portfinder from 'portfinder';
3
- import { compatRequire } from '@modern-js/utils';
4
3
  import { makeRender } from "../libs/make";
5
4
  import { compile as createRender } from "./prerender";
6
5
  import { CLOSE_SIGN } from "./consts";
7
-
8
- const safetyRequire = (filename, base) => {
9
- try {
10
- return compatRequire(require.resolve(`${filename}/server`, {
11
- paths: [base]
12
- }));
13
- } catch (e) {
14
- return compatRequire(require.resolve(filename, {
15
- paths: [base]
16
- }));
17
- }
18
- };
19
-
20
6
  process.on('message', async chunk => {
21
7
  if (chunk === CLOSE_SIGN) {
22
8
  // eslint-disable-next-line no-process-exit
@@ -31,10 +17,6 @@ process.on('message', async chunk => {
31
17
  appDirectory,
32
18
  plugins
33
19
  } = context;
34
- const instances = plugins.map(plugin => {
35
- const mod = safetyRequire(plugin, appDirectory);
36
- return mod();
37
- });
38
20
  let modernServer = null;
39
21
 
40
22
  try {
@@ -50,7 +32,7 @@ process.on('message', async chunk => {
50
32
  config: options,
51
33
  routes,
52
34
  staticGenerate: true,
53
- plugins: instances
35
+ internalPlugins: plugins
54
36
  }); // listen just for bff request in ssr page
55
37
 
56
38
  modernServer.listen(port, async err => {
@@ -31,6 +31,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
31
31
 
32
32
  var _default = () => ({
33
33
  name: '@modern-js/plugin-ssg',
34
+ pre: ['@modern-js/plugin-server', '@modern-js/plugin-bff'],
34
35
  setup: api => {
35
36
  const agreedRouteMap = {};
36
37
  return {
@@ -45,7 +46,8 @@ var _default = () => ({
45
46
  const {
46
47
  entryName
47
48
  } = entrypoint;
48
- agreedRouteMap[entryName] = routes;
49
+ const flattedRoutes = (0, _util.flattenRoutes)(routes);
50
+ agreedRouteMap[entryName] = flattedRoutes;
49
51
  return {
50
52
  entrypoint,
51
53
  routes
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.flattenRoutes = void 0;
6
7
  exports.formatOutput = formatOutput;
7
8
  exports.formatPath = formatPath;
8
9
  exports.getOutput = getOutput;
@@ -180,6 +181,38 @@ exports.standardOptions = standardOptions;
180
181
  const openRouteSSR = (routes, entries = []) => routes.map(ssgRoute => _objectSpread(_objectSpread({}, ssgRoute), {}, {
181
182
  isSSR: entries.includes(ssgRoute.entryName),
182
183
  bundle: `${_utils.SERVER_BUNDLE_DIRECTORY}/${ssgRoute.entryName}.js`
183
- }));
184
+ })); // TODO: 过滤带有 server loader 的路由
184
185
 
185
- exports.openRouteSSR = openRouteSSR;
186
+
187
+ exports.openRouteSSR = openRouteSSR;
188
+
189
+ const flattenRoutes = routes => {
190
+ const parents = [];
191
+ const newRoutes = [];
192
+
193
+ const traverseRoute = route => {
194
+ const parent = parents[parents.length - 1];
195
+ let path = parent ? `${parent.path}/${route.path || ''}`.replace(/\/+/g, '/') : route.path || ''; // If the route is an index route, the route has no path property
196
+
197
+ path = path.replace(/\/$/, ''); // If the route path is / and is not the root route, it should not be used as an ssg route
198
+
199
+ if (route._component && (path !== '/' || path === '/' && !parent)) {
200
+ newRoutes.push(_objectSpread(_objectSpread({}, route), {}, {
201
+ path
202
+ }));
203
+ }
204
+
205
+ if (route.children) {
206
+ parents.push(_objectSpread(_objectSpread({}, route), {}, {
207
+ path
208
+ }));
209
+ route.children.forEach(traverseRoute);
210
+ parents.pop();
211
+ }
212
+ };
213
+
214
+ routes.forEach(traverseRoute);
215
+ return newRoutes;
216
+ };
217
+
218
+ exports.flattenRoutes = flattenRoutes;
@@ -30,7 +30,7 @@ const createServer = (api, ssgRoutes, pageRoutes, apiRoutes, options, appDirecto
30
30
  });
31
31
 
32
32
  const appContext = api.useAppContext();
33
- const plugins = appContext.plugins.filter(p => p.server).map(p => p.server);
33
+ const plugins = appContext.serverInternalPlugins;
34
34
  cp.send(JSON.stringify({
35
35
  options,
36
36
  renderRoutes: ssgRoutes,
@@ -4,8 +4,6 @@ var _prodServer = _interopRequireDefault(require("@modern-js/prod-server"));
4
4
 
5
5
  var _portfinder = _interopRequireDefault(require("portfinder"));
6
6
 
7
- var _utils = require("@modern-js/utils");
8
-
9
7
  var _make = require("../libs/make");
10
8
 
11
9
  var _prerender = require("./prerender");
@@ -14,18 +12,6 @@ var _consts = require("./consts");
14
12
 
15
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
14
 
17
- const safetyRequire = (filename, base) => {
18
- try {
19
- return (0, _utils.compatRequire)(require.resolve(`${filename}/server`, {
20
- paths: [base]
21
- }));
22
- } catch (e) {
23
- return (0, _utils.compatRequire)(require.resolve(filename, {
24
- paths: [base]
25
- }));
26
- }
27
- };
28
-
29
15
  process.on('message', async chunk => {
30
16
  if (chunk === _consts.CLOSE_SIGN) {
31
17
  // eslint-disable-next-line no-process-exit
@@ -40,10 +26,6 @@ process.on('message', async chunk => {
40
26
  appDirectory,
41
27
  plugins
42
28
  } = context;
43
- const instances = plugins.map(plugin => {
44
- const mod = safetyRequire(plugin, appDirectory);
45
- return mod();
46
- });
47
29
  let modernServer = null;
48
30
 
49
31
  try {
@@ -59,7 +41,7 @@ process.on('message', async chunk => {
59
41
  config: options,
60
42
  routes,
61
43
  staticGenerate: true,
62
- plugins: instances
44
+ internalPlugins: plugins
63
45
  }); // listen just for bff request in ssr page
64
46
 
65
47
  modernServer.listen(port, async err => {
@@ -1,6 +1,6 @@
1
1
  import { ServerRoute as ModernRoute } from '@modern-js/types';
2
2
  import { ServerConfig } from '@modern-js/core';
3
- import { SsgRoute, SSGConfig, EntryPoint, SSGMultiEntryOptions } from '../types';
3
+ import { SsgRoute, SSGConfig, EntryPoint, SSGMultiEntryOptions, AgreedRoute } from '../types';
4
4
  export declare function formatOutput(filename: string): string;
5
5
  export declare function formatPath(str: string): string;
6
6
  export declare function isDynamicUrl(url: string): boolean;
@@ -20,4 +20,5 @@ export declare const openRouteSSR: (routes: ModernRoute[], entries?: string[]) =
20
20
  isApi?: boolean | undefined;
21
21
  enableModernMode?: boolean | undefined;
22
22
  responseHeaders?: Record<string, unknown> | undefined;
23
- }[];
23
+ }[];
24
+ export declare const flattenRoutes: (routes: AgreedRoute[]) => AgreedRoute[];
@@ -2,10 +2,11 @@ import type { ServerRoute as ModernRoute } from '@modern-js/types';
2
2
  import type { SSGConfig, SSGRouteOptions, SSGMultiEntryOptions, SSGSingleEntryOptions } from '@modern-js/core';
3
3
  export type { SSGConfig, SSGRouteOptions, SSGMultiEntryOptions, SSGSingleEntryOptions };
4
4
  export declare type AgreedRoute = {
5
- path: string;
6
- component: string;
7
- _component: string;
8
- exact: boolean;
5
+ path?: string;
6
+ component?: string;
7
+ _component?: string;
8
+ children?: AgreedRoute[];
9
+ exact?: boolean;
9
10
  };
10
11
  export declare type EntryPoint = {
11
12
  entryName: string;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.21.3",
14
+ "version": "2.0.0-beta.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -55,23 +55,29 @@
55
55
  },
56
56
  "dependencies": {
57
57
  "@babel/runtime": "^7.18.0",
58
- "@modern-js/utils": "1.21.3",
58
+ "@modern-js/utils": "2.0.0-beta.0",
59
59
  "node-mocks-http": "^1.10.1",
60
60
  "normalize-path": "^3.0.0",
61
- "portfinder": "^1.0.28",
62
- "react-router-dom": "^5.2.1"
61
+ "portfinder": "^1.0.28"
62
+ },
63
+ "peerDependencies": {
64
+ "react-router-dom": ">=5.1.2"
65
+ },
66
+ "peerDependenciesMeta": {
67
+ "react-router-dom": {
68
+ "optional": true
69
+ }
63
70
  },
64
71
  "devDependencies": {
65
- "@modern-js/types": "1.21.3",
66
- "@modern-js/prod-server": "1.21.3",
72
+ "@modern-js/types": "2.0.0-beta.0",
73
+ "@modern-js/prod-server": "2.0.0-beta.0",
67
74
  "@types/jest": "^27",
68
75
  "@types/node": "^14",
69
- "@types/react-router": "^5.1.16",
70
- "@types/react-router-dom": "^5.1.8",
71
- "@modern-js/core": "1.21.3",
72
- "@scripts/build": "1.21.3",
73
- "@scripts/jest-config": "1.21.3",
74
- "react": "^17",
76
+ "@modern-js/core": "2.0.0-beta.0",
77
+ "@scripts/build": "2.0.0-beta.0",
78
+ "@scripts/jest-config": "2.0.0-beta.0",
79
+ "react-router-dom": "^6.4.2",
80
+ "react": "^18",
75
81
  "typescript": "^4",
76
82
  "jest": "^27"
77
83
  },
@@ -85,33 +91,10 @@
85
91
  "registry": "https://registry.npmjs.org/",
86
92
  "access": "public"
87
93
  },
88
- "wireit": {
89
- "build": {
90
- "command": "modern build",
91
- "files": [
92
- "src/**/*",
93
- "tsconfig.json",
94
- "package.json"
95
- ],
96
- "output": [
97
- "dist/**/*"
98
- ]
99
- },
100
- "test": {
101
- "command": "jest --passWithNoTests",
102
- "files": [
103
- "src/**/*",
104
- "tsconfig.json",
105
- "package.json",
106
- "tests/**/*"
107
- ],
108
- "output": []
109
- }
110
- },
111
94
  "scripts": {
112
95
  "new": "modern new",
113
- "build": "wireit",
96
+ "build": "modern build",
114
97
  "dev": "modern build --watch",
115
- "test": "wireit"
98
+ "test": "jest --passWithNoTests"
116
99
  }
117
100
  }