@modern-js/prod-server 1.0.3 → 1.0.4

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,5 +1,22 @@
1
1
  # @modern-js/prod-server
2
2
 
3
+ ## 1.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - d95f28c3: should enable babel register before server plugin require
8
+ - d95f28c3: remove server hook when api only
9
+ - 0923c182: fix static handler when static file not exist
10
+ - 2008fdbd: convert two packages server part, support server load plugin itself
11
+ - ca0bcf13: publish prod-server local
12
+ - Updated dependencies [5bf5868d]
13
+ - Updated dependencies [d95f28c3]
14
+ - Updated dependencies [2e8dec93]
15
+ - Updated dependencies [2008fdbd]
16
+ - Updated dependencies [2e8dec93]
17
+ - @modern-js/utils@1.3.5
18
+ - @modern-js/server-core@1.2.3
19
+
3
20
  ## 1.0.2
4
21
 
5
22
  ### Patch Changes
@@ -5,8 +5,8 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
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
 
7
7
  import path from 'path';
8
- import { serverManager, AppContext, ConfigContext } from '@modern-js/server-core';
9
- import { compatRequire, logger as defaultLogger, SHARED_DIR } from '@modern-js/utils';
8
+ import { serverManager, AppContext, ConfigContext, loadPlugins } from '@modern-js/server-core';
9
+ import { logger as defaultLogger, SHARED_DIR } from '@modern-js/utils';
10
10
  import { metrics as defaultMetrics } from "../libs/metrics";
11
11
  import { createProdServer } from "./modern-server-split";
12
12
  export class Server {
@@ -67,26 +67,29 @@ export class Server {
67
67
  options
68
68
  } = this;
69
69
  const {
70
- plugins = []
70
+ plugins = [],
71
+ pwd,
72
+ config
71
73
  } = options; // server app context for serve plugin
72
74
 
73
- plugins.forEach(p => {
74
- serverManager.usePlugin(compatRequire(p.pluginPath));
75
+ const loadedPlugins = loadPlugins(plugins, pwd);
76
+ loadedPlugins.forEach(p => {
77
+ serverManager.usePlugin(p);
75
78
  });
76
79
  const appContext = this.initAppContext();
77
80
  serverManager.run(() => {
78
- var _options$config$outpu;
81
+ var _config$output;
79
82
 
80
- ConfigContext.set(this.options.config);
83
+ ConfigContext.set(config);
81
84
  AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
82
- distDirectory: path.join(options.pwd, ((_options$config$outpu = options.config.output) === null || _options$config$outpu === void 0 ? void 0 : _options$config$outpu.path) || 'dist')
85
+ distDirectory: path.join(pwd, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist')
83
86
  }));
84
87
  });
85
88
  return serverManager.init({});
86
89
  }
87
90
 
88
91
  initAppContext() {
89
- var _config$output;
92
+ var _config$output2;
90
93
 
91
94
  const {
92
95
  options
@@ -101,7 +104,7 @@ export class Server {
101
104
  }));
102
105
  return {
103
106
  appDirectory,
104
- distDirectory: path.join(appDirectory, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist'),
107
+ distDirectory: path.join(appDirectory, ((_config$output2 = config.output) === null || _config$output2 === void 0 ? void 0 : _config$output2.path) || 'dist'),
105
108
  sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
106
109
  plugins: serverPlugins
107
110
  };
@@ -88,7 +88,7 @@ export const getStaticReg = (output = {}) => {
88
88
  } = output;
89
89
  const favicons = prepareFavicons(favicon, faviconByEntries);
90
90
  const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
91
- const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}|${staticFiles.join('|')})`);
91
+ const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}${staticFiles.length > 0 ? `|${staticFiles.join('|')}` : ''})`);
92
92
  return staticPathRegExp;
93
93
  };
94
94
  export const prepareFavicons = (favicon, faviconByEntries) => {
@@ -82,21 +82,24 @@ class Server {
82
82
  options
83
83
  } = this;
84
84
  const {
85
- plugins = []
85
+ plugins = [],
86
+ pwd,
87
+ config
86
88
  } = options; // server app context for serve plugin
87
89
 
88
- plugins.forEach(p => {
89
- _serverCore.serverManager.usePlugin((0, _utils.compatRequire)(p.pluginPath));
90
+ const loadedPlugins = (0, _serverCore.loadPlugins)(plugins, pwd);
91
+ loadedPlugins.forEach(p => {
92
+ _serverCore.serverManager.usePlugin(p);
90
93
  });
91
94
  const appContext = this.initAppContext();
92
95
 
93
96
  _serverCore.serverManager.run(() => {
94
- var _options$config$outpu;
97
+ var _config$output;
95
98
 
96
- _serverCore.ConfigContext.set(this.options.config);
99
+ _serverCore.ConfigContext.set(config);
97
100
 
98
101
  _serverCore.AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
99
- distDirectory: _path.default.join(options.pwd, ((_options$config$outpu = options.config.output) === null || _options$config$outpu === void 0 ? void 0 : _options$config$outpu.path) || 'dist')
102
+ distDirectory: _path.default.join(pwd, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist')
100
103
  }));
101
104
  });
102
105
 
@@ -104,7 +107,7 @@ class Server {
104
107
  }
105
108
 
106
109
  initAppContext() {
107
- var _config$output;
110
+ var _config$output2;
108
111
 
109
112
  const {
110
113
  options
@@ -119,7 +122,7 @@ class Server {
119
122
  }));
120
123
  return {
121
124
  appDirectory,
122
- distDirectory: _path.default.join(appDirectory, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist'),
125
+ distDirectory: _path.default.join(appDirectory, ((_config$output2 = config.output) === null || _config$output2 === void 0 ? void 0 : _config$output2.path) || 'dist'),
123
126
  sharedDirectory: _path.default.resolve(appDirectory, _utils.SHARED_DIR),
124
127
  plugins: serverPlugins
125
128
  };
@@ -114,7 +114,7 @@ const getStaticReg = (output = {}) => {
114
114
  } = output;
115
115
  const favicons = prepareFavicons(favicon, faviconByEntries);
116
116
  const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
117
- const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}|${staticFiles.join('|')})`);
117
+ const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''}${staticFiles.length > 0 ? `|${staticFiles.join('|')}` : ''})`);
118
118
  return staticPathRegExp;
119
119
  };
120
120
 
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, Server, ServerResponse } from 'http';
3
- import { serverManager } from '@modern-js/server-core';
3
+ import { serverManager, ServerPlugin } from '@modern-js/server-core';
4
4
  import type { NormalizedConfig } from '@modern-js/core';
5
5
  import type { Metrics, Logger, NextFunction } from '@modern-js/types/server';
6
6
  import { ModernRouteInterface } from './libs/route';
@@ -17,12 +17,11 @@ declare module '@modern-js/core' {
17
17
  };
18
18
  }
19
19
  }
20
+ declare type Plugin = string | [string, any] | ServerPlugin;
20
21
  export declare type ModernServerOptions = {
21
22
  pwd: string;
22
23
  config: NormalizedConfig;
23
- plugins?: {
24
- pluginPath: string;
25
- }[];
24
+ plugins?: Plugin[];
26
25
  routes?: ModernRouteInterface[];
27
26
  staticGenerate?: boolean;
28
27
  loggerOptions?: Record<string, string>;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.0.3",
14
+ "version": "1.0.4",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -28,9 +28,9 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@modern-js/utils": "^1.3.4",
31
+ "@modern-js/utils": "^1.3.5",
32
32
  "@babel/compat-data": "^7.17.0",
33
- "@modern-js/server-core": "^1.2.2",
33
+ "@modern-js/server-core": "^1.2.3",
34
34
  "axios": "^0.24.0",
35
35
  "compare-versions": "^3.6.0",
36
36
  "http-proxy-middleware": "^2.0.1",
@@ -44,8 +44,8 @@
44
44
  "cookie": "^0.4.2"
45
45
  },
46
46
  "devDependencies": {
47
- "@modern-js/types": "^1.3.4",
48
- "@modern-js/core": "^1.4.4",
47
+ "@modern-js/types": "^1.3.5",
48
+ "@modern-js/core": "^1.5.0",
49
49
  "@scripts/jest-config": "0.0.0",
50
50
  "@scripts/build": "0.0.0",
51
51
  "del-cli": "^4.0.1",
@@ -4,12 +4,9 @@ import {
4
4
  serverManager,
5
5
  AppContext,
6
6
  ConfigContext,
7
+ loadPlugins,
7
8
  } from '@modern-js/server-core';
8
- import {
9
- compatRequire,
10
- logger as defaultLogger,
11
- SHARED_DIR,
12
- } from '@modern-js/utils';
9
+ import { logger as defaultLogger, SHARED_DIR } from '@modern-js/utils';
13
10
  import type { UserConfig } from '@modern-js/core';
14
11
  import { ISAppContext } from '@modern-js/types';
15
12
  import {
@@ -88,22 +85,20 @@ export class Server {
88
85
  serverManager.clear();
89
86
 
90
87
  const { options } = this;
91
- const { plugins = [] } = options;
88
+ const { plugins = [], pwd, config } = options;
92
89
 
93
90
  // server app context for serve plugin
94
- plugins.forEach(p => {
95
- serverManager.usePlugin(compatRequire(p.pluginPath));
91
+ const loadedPlugins = loadPlugins(plugins, pwd);
92
+ loadedPlugins.forEach(p => {
93
+ serverManager.usePlugin(p);
96
94
  });
97
95
 
98
96
  const appContext = this.initAppContext();
99
97
  serverManager.run(() => {
100
- ConfigContext.set(this.options.config as UserConfig);
98
+ ConfigContext.set(config as UserConfig);
101
99
  AppContext.set({
102
100
  ...appContext,
103
- distDirectory: path.join(
104
- options.pwd,
105
- options.config.output?.path || 'dist',
106
- ),
101
+ distDirectory: path.join(pwd, config.output?.path || 'dist'),
107
102
  });
108
103
  });
109
104
 
package/src/type.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Buffer } from 'buffer';
2
2
  import { IncomingMessage, Server, ServerResponse } from 'http';
3
- import { serverManager } from '@modern-js/server-core';
3
+ import { serverManager, ServerPlugin } from '@modern-js/server-core';
4
4
  import type { NormalizedConfig } from '@modern-js/core';
5
5
  import type { Metrics, Logger, NextFunction } from '@modern-js/types/server';
6
6
  import { ModernRouteInterface } from './libs/route';
@@ -20,10 +20,11 @@ declare module '@modern-js/core' {
20
20
  }
21
21
  }
22
22
 
23
+ type Plugin = string | [string, any] | ServerPlugin;
23
24
  export type ModernServerOptions = {
24
25
  pwd: string;
25
26
  config: NormalizedConfig;
26
- plugins?: { pluginPath: string }[];
27
+ plugins?: Plugin[];
27
28
  routes?: ModernRouteInterface[];
28
29
  staticGenerate?: boolean;
29
30
  loggerOptions?: Record<string, string>;
package/src/utils.ts CHANGED
@@ -94,7 +94,7 @@ export const getStaticReg = (output: NormalizedConfig['output'] = {}) => {
94
94
  const staticPathRegExp = new RegExp(
95
95
  `^/(static/|upload/|favicon.ico|icon.png${
96
96
  favicons.length > 0 ? `|${favicons.join('|')}` : ''
97
- }|${staticFiles.join('|')})`,
97
+ }${staticFiles.length > 0 ? `|${staticFiles.join('|')}` : ''})`,
98
98
  );
99
99
 
100
100
  return staticPathRegExp;
@@ -79,8 +79,8 @@ describe('test server utils', () => {
79
79
  describe('test create static reg', () => {
80
80
  test('should test static path correctly', () => {
81
81
  const reg = getStaticReg();
82
- expect(reg.test('/static')).toBeTruthy();
83
- expect(reg.test('/upload')).toBeTruthy();
82
+ expect(reg.test('/static/')).toBeTruthy();
83
+ expect(reg.test('/upload/')).toBeTruthy();
84
84
  });
85
85
 
86
86
  test('should test custom static path correctly', () => {