@modern-js/plugin-server 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 CHANGED
@@ -1,6 +1,63 @@
1
1
  # @modern-js/plugin-server
2
2
 
3
+ ## 1.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 272cab15: refactor server plugin manager
8
+ - Updated dependencies [d9cc5ea9]
9
+ - Updated dependencies [bd819a8d]
10
+ - Updated dependencies [ec4dbffb]
11
+ - Updated dependencies [d099e5c5]
12
+ - Updated dependencies [bada2879]
13
+ - Updated dependencies [24f616ca]
14
+ - Updated dependencies [bd819a8d]
15
+ - Updated dependencies [272cab15]
16
+ - @modern-js/core@1.4.0
17
+ - @modern-js/utils@1.3.0
18
+ - @modern-js/server-core@1.2.2
19
+
20
+ ## 1.1.1
21
+
22
+ ### Patch Changes
23
+
24
+ - 83166714: change .npmignore
25
+ - Updated dependencies [83166714]
26
+ - Updated dependencies [c3de9882]
27
+ - Updated dependencies [33ff48af]
28
+ - @modern-js/core@1.3.2
29
+ - @modern-js/server-plugin@1.2.1
30
+ - @modern-js/server-utils@1.2.1
31
+ - @modern-js/babel-compiler@1.2.1
32
+ - @modern-js/utils@1.2.2
33
+
34
+ ## 1.1.0
35
+
36
+ ### Minor Changes
37
+
38
+ - cfe11628: Make Modern.js self bootstraping
39
+
40
+ ### Patch Changes
41
+
42
+ - 146dcd85: modify server framework plugin hook types and hook context
43
+ - 146dcd85: modify server framework plugin hook types
44
+ - 146dcd85: fix test case in babel compiler
45
+ - Updated dependencies [2da09c69]
46
+ - Updated dependencies [fc71e36f]
47
+ - Updated dependencies [146dcd85]
48
+ - Updated dependencies [c3d46ee4]
49
+ - Updated dependencies [cfe11628]
50
+ - Updated dependencies [146dcd85]
51
+ - Updated dependencies [146dcd85]
52
+ - Updated dependencies [1ebc7ee2]
53
+ - @modern-js/utils@1.2.0
54
+ - @modern-js/core@1.3.0
55
+ - @modern-js/server-utils@1.2.0
56
+ - @modern-js/server-plugin@1.2.0
57
+ - @modern-js/babel-compiler@1.2.0
58
+
3
59
  ## 1.0.1
60
+
4
61
  ### Patch Changes
5
62
 
6
63
  - 085a6a58: refactor server plugin
@@ -1,12 +1,5 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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
1
  import path from 'path';
8
- import { createPlugin } from '@modern-js/server-plugin';
9
- import { useAppContext } from '@modern-js/core';
2
+ import { createPlugin, useAppContext } from '@modern-js/server-core';
10
3
  import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
11
4
  const WEB_APP_NAME = 'index';
12
5
 
@@ -73,7 +66,7 @@ export default createPlugin(() => {
73
66
  }, next) {
74
67
  var _storage$hooks$before, _storage$hooks;
75
68
 
76
- return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, _objectSpread({}, context), next);
69
+ return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
77
70
  },
78
71
 
79
72
  afterMatch({
@@ -82,9 +75,8 @@ export default createPlugin(() => {
82
75
  }, next) {
83
76
  var _storage$hooks$afterM, _storage$hooks2;
84
77
 
85
- return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, _objectSpread(_objectSpread({}, context), {}, {
86
- routeAPI
87
- }), next);
78
+ context.router = routeAPI;
79
+ return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
88
80
  },
89
81
 
90
82
  beforeRender({
@@ -92,7 +84,7 @@ export default createPlugin(() => {
92
84
  }, next) {
93
85
  var _storage$hooks$before2, _storage$hooks3;
94
86
 
95
- return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, _objectSpread({}, context), next);
87
+ return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
96
88
  },
97
89
 
98
90
  afterRender({
@@ -101,9 +93,8 @@ export default createPlugin(() => {
101
93
  }, next) {
102
94
  var _storage$hooks$afterR, _storage$hooks4;
103
95
 
104
- return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, _objectSpread(_objectSpread({}, context), {}, {
105
- templateAPI
106
- }), next);
96
+ context.template = templateAPI;
97
+ return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
107
98
  }
108
99
 
109
100
  };
@@ -7,20 +7,12 @@ exports.default = void 0;
7
7
 
8
8
  var _path = _interopRequireDefault(require("path"));
9
9
 
10
- var _serverPlugin = require("@modern-js/server-plugin");
11
-
12
- var _core = require("@modern-js/core");
10
+ var _serverCore = require("@modern-js/server-core");
13
11
 
14
12
  var _utils = require("@modern-js/utils");
15
13
 
16
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
15
 
18
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
19
-
20
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
21
-
22
- 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
-
24
16
  const WEB_APP_NAME = 'index';
25
17
 
26
18
  class Storage {
@@ -47,11 +39,11 @@ const createTransformAPI = storage => new Proxy({}, {
47
39
 
48
40
  });
49
41
 
50
- var _default = (0, _serverPlugin.createPlugin)(() => {
42
+ var _default = (0, _serverCore.createPlugin)(() => {
51
43
  const {
52
44
  appDirectory,
53
45
  distDirectory
54
- } = (0, _core.useAppContext)();
46
+ } = (0, _serverCore.useAppContext)();
55
47
  const pwd = (0, _utils.isProd)() ? distDirectory : appDirectory;
56
48
 
57
49
  const serverPath = _path.default.resolve(pwd, _utils.SERVER_DIR);
@@ -89,7 +81,7 @@ var _default = (0, _serverPlugin.createPlugin)(() => {
89
81
  }, next) {
90
82
  var _storage$hooks$before, _storage$hooks;
91
83
 
92
- return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, _objectSpread({}, context), next);
84
+ return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
93
85
  },
94
86
 
95
87
  afterMatch({
@@ -98,9 +90,8 @@ var _default = (0, _serverPlugin.createPlugin)(() => {
98
90
  }, next) {
99
91
  var _storage$hooks$afterM, _storage$hooks2;
100
92
 
101
- return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, _objectSpread(_objectSpread({}, context), {}, {
102
- routeAPI
103
- }), next);
93
+ context.router = routeAPI;
94
+ return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
104
95
  },
105
96
 
106
97
  beforeRender({
@@ -108,7 +99,7 @@ var _default = (0, _serverPlugin.createPlugin)(() => {
108
99
  }, next) {
109
100
  var _storage$hooks$before2, _storage$hooks3;
110
101
 
111
- return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, _objectSpread({}, context), next);
102
+ return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
112
103
  },
113
104
 
114
105
  afterRender({
@@ -117,9 +108,8 @@ var _default = (0, _serverPlugin.createPlugin)(() => {
117
108
  }, next) {
118
109
  var _storage$hooks$afterR, _storage$hooks4;
119
110
 
120
- return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, _objectSpread(_objectSpread({}, context), {}, {
121
- templateAPI
122
- }), next);
111
+ context.template = templateAPI;
112
+ return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
123
113
  }
124
114
 
125
115
  };
@@ -1,18 +1,11 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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
1
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
2
 
9
3
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
10
4
 
11
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
5
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
12
6
 
13
7
  import path from 'path';
14
- import { createPlugin } from '@modern-js/server-plugin';
15
- import { useAppContext } from '@modern-js/core';
8
+ import { createPlugin, useAppContext } from '@modern-js/server-core';
16
9
  import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
17
10
  var WEB_APP_NAME = 'index';
18
11
 
@@ -86,31 +79,29 @@ export default createPlugin(function () {
86
79
  var _storage$hooks$before, _storage$hooks;
87
80
 
88
81
  var context = _ref2.context;
89
- return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, _objectSpread({}, context), next);
82
+ return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
90
83
  },
91
84
  afterMatch: function afterMatch(_ref3, next) {
92
85
  var _storage$hooks$afterM, _storage$hooks2;
93
86
 
94
87
  var context = _ref3.context,
95
88
  routeAPI = _ref3.routeAPI;
96
- return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, _objectSpread(_objectSpread({}, context), {}, {
97
- routeAPI: routeAPI
98
- }), next);
89
+ context.router = routeAPI;
90
+ return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
99
91
  },
100
92
  beforeRender: function beforeRender(_ref4, next) {
101
93
  var _storage$hooks$before2, _storage$hooks3;
102
94
 
103
95
  var context = _ref4.context;
104
- return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, _objectSpread({}, context), next);
96
+ return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
105
97
  },
106
98
  afterRender: function afterRender(_ref5, next) {
107
99
  var _storage$hooks$afterR, _storage$hooks4;
108
100
 
109
101
  var context = _ref5.context,
110
102
  templateAPI = _ref5.templateAPI;
111
- return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, _objectSpread(_objectSpread({}, context), {}, {
112
- templateAPI: templateAPI
113
- }), next);
103
+ context.template = templateAPI;
104
+ return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
114
105
  }
115
106
  };
116
107
  }, {
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ const sharedConfig = require('@scripts/jest-config');
2
+
3
+ /** @type {import('@jest/types').Config.InitialOptions} */
4
+ module.exports = {
5
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
+ ...sharedConfig,
7
+ rootDir: __dirname,
8
+ };
package/package.json CHANGED
@@ -11,38 +11,48 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.0.1",
14
+ "version": "1.1.2",
15
15
  "types": "./dist/types/server.d.ts",
16
+ "jsnext:source": "./src/server.ts",
16
17
  "main": "./dist/js/node/server.js",
17
18
  "exports": {
18
19
  ".": {
19
20
  "node": {
21
+ "jsnext:source": "./src/server.ts",
20
22
  "import": "./dist/js/modern/server.js",
21
23
  "require": "./dist/js/node/server.js"
22
24
  },
23
25
  "default": "./dist/js/treeshaking/server.js"
24
26
  },
25
- "./cli": "./dist/js/node/cli.js",
26
- "./server": "./dist/js/node/server.js"
27
+ "./cli": {
28
+ "jsnext:source": "./src/cli.ts",
29
+ "default": "./dist/js/node/cli.js"
30
+ },
31
+ "./server": {
32
+ "jsnext:source": "./src/server.ts",
33
+ "default": "./dist/js/node/server.js"
34
+ }
27
35
  },
28
36
  "dependencies": {
29
37
  "@babel/runtime": "^7",
30
- "@modern-js/babel-compiler": "^1.1.2",
31
- "@modern-js/server-utils": "^1.1.2",
32
- "@modern-js/utils": "^1.1.3",
33
- "@modern-js/server-plugin": "^1.1.2"
38
+ "@modern-js/babel-compiler": "^1.2.1",
39
+ "@modern-js/server-utils": "^1.2.1",
40
+ "@modern-js/utils": "^1.3.0",
41
+ "@modern-js/server-core": "^1.2.2"
34
42
  },
35
43
  "devDependencies": {
36
- "@modern-js/core": "^1.1.3",
37
- "@modern-js/module-tools": "^1.1.1",
38
- "@modern-js/plugin-testing": "^1.1.1",
44
+ "@modern-js/core": "^1.4.0",
45
+ "@scripts/build": "0.0.0",
46
+ "@modern-js/types": "^1.3.0",
39
47
  "del-cli": "^4.0.1",
40
48
  "typescript": "^4",
41
49
  "@types/jest": "^26.0.9",
42
- "@types/node": "^14"
50
+ "@types/node": "^14",
51
+ "jest": "^27",
52
+ "@scripts/jest-config": "0.0.0"
43
53
  },
44
54
  "peerDependencies": {
45
- "@modern-js/core": "^1.1.3"
55
+ "@modern-js/core": "^1.4.0"
46
56
  },
47
57
  "sideEffects": [
48
58
  "*.css",
@@ -52,12 +62,14 @@
52
62
  ],
53
63
  "publishConfig": {
54
64
  "access": "public",
55
- "registry": "https://registry.npmjs.org/"
65
+ "registry": "https://registry.npmjs.org/",
66
+ "types": "./dist/types/server.d.ts"
56
67
  },
57
68
  "scripts": {
58
69
  "dev": "modern dev",
59
70
  "build": "modern build",
60
71
  "new": "modern new",
61
- "test": "modern test --passWithNoTests"
62
- }
72
+ "test": "jest --passWithNoTests"
73
+ },
74
+ "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"
63
75
  }
package/tsconfig.json CHANGED
@@ -4,10 +4,7 @@
4
4
  "declaration": false,
5
5
  "jsx": "preserve",
6
6
  "baseUrl": "./",
7
- "paths": {
8
- "@/*": ["./src/*"],
9
- "@style/*": ["./styles/*"]
10
- }
7
+ "paths": {}
11
8
  },
12
9
  "include": ["src"]
13
10
  }
package/src/cli.ts DELETED
@@ -1,97 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import {
4
- createPlugin,
5
- useAppContext,
6
- useResolvedConfigContext,
7
- NormalizedConfig,
8
- } from '@modern-js/core';
9
- import { compiler } from '@modern-js/babel-compiler';
10
- import { resolveBabelConfig } from '@modern-js/server-utils';
11
- import { SHARED_DIR, SERVER_DIR } from '@modern-js/utils';
12
-
13
- const TS_CONFIG_FILENAME = 'tsconfig.json';
14
- const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
15
-
16
- interface Pattern {
17
- from: string;
18
- to: string;
19
- tsconfigPath?: string;
20
- }
21
-
22
- interface CompileOptions {
23
- patterns: Pattern[];
24
- }
25
-
26
- const compile = async (
27
- appDirectory: string,
28
- modernConfig: NormalizedConfig,
29
- compileOptions: CompileOptions,
30
- ) => {
31
- const { patterns } = compileOptions;
32
- const results = await Promise.all(
33
- patterns.map(pattern => {
34
- const { from, to, tsconfigPath } = pattern;
35
- const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
36
- tsconfigPath: tsconfigPath ? tsconfigPath : '',
37
- syntax: 'es6+',
38
- type: 'commonjs',
39
- });
40
- return compiler(
41
- {
42
- rootDir: appDirectory,
43
- distDir: to,
44
- sourceDir: from,
45
- extensions: FILE_EXTENSIONS,
46
- },
47
- babelConfig,
48
- );
49
- }),
50
- );
51
- results.forEach(result => {
52
- if (result.code === 1) {
53
- throw new Error(result.message);
54
- }
55
- });
56
- };
57
-
58
- export default createPlugin(
59
- () => ({
60
- config() {
61
- return {};
62
- },
63
- async afterBuild() {
64
- // eslint-disable-next-line react-hooks/rules-of-hooks
65
- const { appDirectory, distDirectory } = useAppContext();
66
- // eslint-disable-next-line react-hooks/rules-of-hooks
67
- const modernConfig = useResolvedConfigContext();
68
-
69
- const distDir = path.resolve(distDirectory);
70
- const serverDir = path.resolve(appDirectory, SERVER_DIR);
71
- const sharedDir = path.resolve(appDirectory, SHARED_DIR);
72
- const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
73
-
74
- const patterns = [];
75
- if (fs.existsSync(serverDir)) {
76
- patterns.push({
77
- from: serverDir,
78
- to: distDir,
79
- tsconfigPath,
80
- });
81
- }
82
-
83
- if (fs.existsSync(sharedDir)) {
84
- patterns.push({
85
- from: sharedDir,
86
- to: distDir,
87
- tsconfigPath,
88
- });
89
- }
90
-
91
- if (patterns.length > 0) {
92
- await compile(appDirectory, modernConfig, { patterns });
93
- }
94
- },
95
- }),
96
- { name: '@modern-js/plugin-server' },
97
- ) as any;
@@ -1,2 +0,0 @@
1
- /// <reference types='@modern-js/module-tools' />
2
- /// <reference types='@modern-js/plugin-testing/type' />
package/src/server.ts DELETED
@@ -1,79 +0,0 @@
1
- import path from 'path';
2
- import { createPlugin } from '@modern-js/server-plugin';
3
- import { useAppContext } from '@modern-js/core';
4
- import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
5
-
6
- const WEB_APP_NAME = 'index';
7
-
8
- type SF = (args: any, next: any) => void;
9
- class Storage {
10
- public middlewares: SF[] = [];
11
-
12
- public hooks: Record<string, SF> = {};
13
-
14
- reset() {
15
- this.middlewares = [];
16
- this.hooks = {};
17
- }
18
- }
19
-
20
- const createTransformAPI = (storage: Storage) =>
21
- new Proxy(
22
- {},
23
- {
24
- get(target: any, name: string) {
25
- if (name === 'addMiddleware') {
26
- return (fn: SF) => storage.middlewares.push(fn);
27
- }
28
- return (fn: SF) => (storage.hooks[name] = fn);
29
- },
30
- },
31
- );
32
-
33
- export default createPlugin(
34
- () => {
35
- const { appDirectory, distDirectory } = useAppContext();
36
- const pwd = isProd() ? distDirectory : appDirectory;
37
-
38
- const serverPath = path.resolve(pwd, SERVER_DIR);
39
- const webAppPath = path.resolve(serverPath, WEB_APP_NAME);
40
-
41
- const storage = new Storage();
42
- const transformAPI = createTransformAPI(storage);
43
-
44
- const webMod = requireExistModule(webAppPath);
45
- if (webMod) {
46
- webMod(transformAPI);
47
- }
48
-
49
- return {
50
- reset() {
51
- storage.reset();
52
- const newWebModule = requireExistModule(webAppPath);
53
- if (newWebModule) {
54
- newWebModule(transformAPI);
55
- }
56
- },
57
- gather({ addWebMiddleware }) {
58
- storage.middlewares.forEach(mid => {
59
- addWebMiddleware(mid);
60
- });
61
- },
62
- beforeMatch({ context }, next) {
63
- return storage.hooks.beforeMatch?.({ ...context }, next);
64
- },
65
- afterMatch({ context, routeAPI }, next) {
66
- return storage.hooks.afterMatch?.({ ...context, routeAPI }, next);
67
- },
68
- beforeRender({ context }, next) {
69
- return storage.hooks.beforeRender?.({ ...context }, next);
70
- },
71
- afterRender({ context, templateAPI }, next) {
72
- return storage.hooks.afterRender?.({ ...context, templateAPI }, next);
73
- },
74
- };
75
- },
76
- {
77
- name: '@modern-js/plugin-server',
78
- },
79
- ) as any;