@modern-js/prod-server 1.2.1 → 1.2.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 +10 -0
- package/dist/js/modern/libs/route/route.js +3 -0
- package/dist/js/modern/libs/serve-file.js +20 -1
- package/dist/js/modern/server/modern-server.js +16 -2
- package/dist/js/modern/utils.js +11 -3
- package/dist/js/node/libs/route/route.js +3 -0
- package/dist/js/node/libs/serve-file.js +20 -1
- package/dist/js/node/server/modern-server.js +16 -2
- package/dist/js/node/utils.js +14 -3
- package/dist/types/libs/route/route.d.ts +1 -0
- package/dist/types/libs/serve-file.d.ts +2 -1
- package/dist/types/utils.d.ts +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @modern-js/prod-server
|
|
2
2
|
|
|
3
|
+
## 1.2.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0e28456: fix assets prefix bug in prod env
|
|
8
|
+
- 44e3bb1: feat: support response headers
|
|
9
|
+
feat: 支持设置响应头
|
|
10
|
+
- @modern-js/server-core@1.4.1
|
|
11
|
+
- @modern-js/utils@1.8.0
|
|
12
|
+
|
|
3
13
|
## 1.2.1
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
|
@@ -22,6 +22,8 @@ export class ModernRoute {
|
|
|
22
22
|
|
|
23
23
|
_defineProperty(this, "params", {});
|
|
24
24
|
|
|
25
|
+
_defineProperty(this, "responseHeaders", void 0);
|
|
26
|
+
|
|
25
27
|
this.entryName = routeSpec.entryName || '';
|
|
26
28
|
this.urlPath = routeSpec.urlPath;
|
|
27
29
|
this.entryPath = routeSpec.entryPath || '';
|
|
@@ -30,6 +32,7 @@ export class ModernRoute {
|
|
|
30
32
|
this.isApi = routeSpec.isApi || false;
|
|
31
33
|
this.bundle = routeSpec.bundle || '';
|
|
32
34
|
this.enableModernMode = (_routeSpec$enableMode = routeSpec.enableModernMode) !== null && _routeSpec$enableMode !== void 0 ? _routeSpec$enableMode : false;
|
|
35
|
+
this.responseHeaders = routeSpec.responseHeaders;
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
}
|
|
@@ -1,13 +1,30 @@
|
|
|
1
1
|
// Todo 看看是不是能 fork 一份,即使命中也返回
|
|
2
2
|
import serve from 'serve-static';
|
|
3
3
|
import { isString, isRegExp } from '@modern-js/utils';
|
|
4
|
-
|
|
4
|
+
import { useLocalPrefix } from "../utils";
|
|
5
|
+
|
|
6
|
+
const removedPrefix = (req, prefix) => {
|
|
7
|
+
if (useLocalPrefix(prefix)) {
|
|
8
|
+
req.url = req.url.slice(prefix.length);
|
|
9
|
+
return () => {
|
|
10
|
+
req.url = prefix + req.url;
|
|
11
|
+
};
|
|
12
|
+
} else {
|
|
13
|
+
return () => {// emptyy
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const createStaticFileHandler = (rules, output = {}) => // eslint-disable-next-line consistent-return
|
|
5
19
|
async (context, next) => {
|
|
6
20
|
const {
|
|
7
21
|
url: requestUrl,
|
|
8
22
|
req,
|
|
9
23
|
res
|
|
10
24
|
} = context;
|
|
25
|
+
const {
|
|
26
|
+
assetPrefix = '/'
|
|
27
|
+
} = output;
|
|
11
28
|
const hitRule = rules.find(item => {
|
|
12
29
|
if (isString(item.path) && requestUrl.startsWith(item.path)) {
|
|
13
30
|
return true;
|
|
@@ -19,7 +36,9 @@ async (context, next) => {
|
|
|
19
36
|
});
|
|
20
37
|
|
|
21
38
|
if (hitRule) {
|
|
39
|
+
const resume = removedPrefix(req, assetPrefix);
|
|
22
40
|
serve(hitRule.target)(req, res, () => {
|
|
41
|
+
resume();
|
|
23
42
|
next();
|
|
24
43
|
});
|
|
25
44
|
} else {
|
|
@@ -136,7 +136,7 @@ export class ModernServer {
|
|
|
136
136
|
this.staticFileHandler = createStaticFileHandler([{
|
|
137
137
|
path: staticPathRegExp,
|
|
138
138
|
target: distDir
|
|
139
|
-
}]);
|
|
139
|
+
}], this.conf.output);
|
|
140
140
|
this.routeRenderHandler = createRenderHandler({
|
|
141
141
|
distDir,
|
|
142
142
|
staticGenerate
|
|
@@ -415,6 +415,16 @@ export class ModernServer {
|
|
|
415
415
|
return;
|
|
416
416
|
}
|
|
417
417
|
|
|
418
|
+
if (route.responseHeaders) {
|
|
419
|
+
Object.keys(route.responseHeaders).forEach(key => {
|
|
420
|
+
const value = route.responseHeaders[key];
|
|
421
|
+
|
|
422
|
+
if (value) {
|
|
423
|
+
context.res.setHeader(key, value);
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
|
|
418
428
|
if (route.entryName) {
|
|
419
429
|
await this.emitRouteHook('beforeRender', {
|
|
420
430
|
context
|
|
@@ -541,7 +551,11 @@ export class ModernServer {
|
|
|
541
551
|
this._handler = (context, next) => {
|
|
542
552
|
let i = 0;
|
|
543
553
|
|
|
544
|
-
const dispatch =
|
|
554
|
+
const dispatch = error => {
|
|
555
|
+
if (error) {
|
|
556
|
+
return this.onError(context, error);
|
|
557
|
+
}
|
|
558
|
+
|
|
545
559
|
const handler = handlers[i++];
|
|
546
560
|
|
|
547
561
|
if (!handler) {
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { compile } from 'path-to-regexp';
|
|
2
|
-
import { createDebugger } from '@modern-js/utils';
|
|
2
|
+
import { createDebugger, isProd } from '@modern-js/utils';
|
|
3
3
|
export const debug = createDebugger('prod-server');
|
|
4
4
|
export const mergeExtension = users => {
|
|
5
5
|
const output = [];
|
|
@@ -71,17 +71,25 @@ export const toPath = (reg, params) => {
|
|
|
71
71
|
});
|
|
72
72
|
return fn(params);
|
|
73
73
|
};
|
|
74
|
+
export const useLocalPrefix = url => {
|
|
75
|
+
return isProd() && !url.includes('.');
|
|
76
|
+
};
|
|
74
77
|
export const getStaticReg = (output = {}) => {
|
|
75
78
|
const {
|
|
76
79
|
favicon,
|
|
77
80
|
faviconByEntries,
|
|
78
81
|
cssPath,
|
|
79
82
|
jsPath,
|
|
80
|
-
mediaPath
|
|
83
|
+
mediaPath,
|
|
84
|
+
assetPrefix = '/'
|
|
81
85
|
} = output;
|
|
86
|
+
const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : '';
|
|
82
87
|
const favicons = prepareFavicons(favicon, faviconByEntries);
|
|
83
88
|
const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
|
|
84
|
-
const
|
|
89
|
+
const staticReg = ['static/', 'upload/', ...staticFiles];
|
|
90
|
+
const iconReg = ['favicon.ico', 'icon.png', ...favicons];
|
|
91
|
+
const regPrefix = prefix === '/' ? '' : prefix;
|
|
92
|
+
const staticPathRegExp = new RegExp(`^${regPrefix}/(${[...staticReg, ...iconReg].join('|')})`);
|
|
85
93
|
return staticPathRegExp;
|
|
86
94
|
};
|
|
87
95
|
export const prepareFavicons = (favicon, faviconByEntries) => {
|
|
@@ -29,6 +29,8 @@ class ModernRoute {
|
|
|
29
29
|
|
|
30
30
|
_defineProperty(this, "params", {});
|
|
31
31
|
|
|
32
|
+
_defineProperty(this, "responseHeaders", void 0);
|
|
33
|
+
|
|
32
34
|
this.entryName = routeSpec.entryName || '';
|
|
33
35
|
this.urlPath = routeSpec.urlPath;
|
|
34
36
|
this.entryPath = routeSpec.entryPath || '';
|
|
@@ -37,6 +39,7 @@ class ModernRoute {
|
|
|
37
39
|
this.isApi = routeSpec.isApi || false;
|
|
38
40
|
this.bundle = routeSpec.bundle || '';
|
|
39
41
|
this.enableModernMode = (_routeSpec$enableMode = routeSpec.enableModernMode) !== null && _routeSpec$enableMode !== void 0 ? _routeSpec$enableMode : false;
|
|
42
|
+
this.responseHeaders = routeSpec.responseHeaders;
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
}
|
|
@@ -9,16 +9,33 @@ var _serveStatic = _interopRequireDefault(require("serve-static"));
|
|
|
9
9
|
|
|
10
10
|
var _utils = require("@modern-js/utils");
|
|
11
11
|
|
|
12
|
+
var _utils2 = require("../utils");
|
|
13
|
+
|
|
12
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
15
|
|
|
14
16
|
// Todo 看看是不是能 fork 一份,即使命中也返回
|
|
15
|
-
const
|
|
17
|
+
const removedPrefix = (req, prefix) => {
|
|
18
|
+
if ((0, _utils2.useLocalPrefix)(prefix)) {
|
|
19
|
+
req.url = req.url.slice(prefix.length);
|
|
20
|
+
return () => {
|
|
21
|
+
req.url = prefix + req.url;
|
|
22
|
+
};
|
|
23
|
+
} else {
|
|
24
|
+
return () => {// emptyy
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const createStaticFileHandler = (rules, output = {}) => // eslint-disable-next-line consistent-return
|
|
16
30
|
async (context, next) => {
|
|
17
31
|
const {
|
|
18
32
|
url: requestUrl,
|
|
19
33
|
req,
|
|
20
34
|
res
|
|
21
35
|
} = context;
|
|
36
|
+
const {
|
|
37
|
+
assetPrefix = '/'
|
|
38
|
+
} = output;
|
|
22
39
|
const hitRule = rules.find(item => {
|
|
23
40
|
if ((0, _utils.isString)(item.path) && requestUrl.startsWith(item.path)) {
|
|
24
41
|
return true;
|
|
@@ -30,7 +47,9 @@ async (context, next) => {
|
|
|
30
47
|
});
|
|
31
48
|
|
|
32
49
|
if (hitRule) {
|
|
50
|
+
const resume = removedPrefix(req, assetPrefix);
|
|
33
51
|
(0, _serveStatic.default)(hitRule.target)(req, res, () => {
|
|
52
|
+
resume();
|
|
34
53
|
next();
|
|
35
54
|
});
|
|
36
55
|
} else {
|
|
@@ -165,7 +165,7 @@ class ModernServer {
|
|
|
165
165
|
this.staticFileHandler = (0, _serveFile.createStaticFileHandler)([{
|
|
166
166
|
path: staticPathRegExp,
|
|
167
167
|
target: distDir
|
|
168
|
-
}]);
|
|
168
|
+
}], this.conf.output);
|
|
169
169
|
this.routeRenderHandler = (0, _render.createRenderHandler)({
|
|
170
170
|
distDir,
|
|
171
171
|
staticGenerate
|
|
@@ -452,6 +452,16 @@ class ModernServer {
|
|
|
452
452
|
return;
|
|
453
453
|
}
|
|
454
454
|
|
|
455
|
+
if (route.responseHeaders) {
|
|
456
|
+
Object.keys(route.responseHeaders).forEach(key => {
|
|
457
|
+
const value = route.responseHeaders[key];
|
|
458
|
+
|
|
459
|
+
if (value) {
|
|
460
|
+
context.res.setHeader(key, value);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
|
|
455
465
|
if (route.entryName) {
|
|
456
466
|
await this.emitRouteHook('beforeRender', {
|
|
457
467
|
context
|
|
@@ -578,7 +588,11 @@ class ModernServer {
|
|
|
578
588
|
this._handler = (context, next) => {
|
|
579
589
|
let i = 0;
|
|
580
590
|
|
|
581
|
-
const dispatch =
|
|
591
|
+
const dispatch = error => {
|
|
592
|
+
if (error) {
|
|
593
|
+
return this.onError(context, error);
|
|
594
|
+
}
|
|
595
|
+
|
|
582
596
|
const handler = handlers[i++];
|
|
583
597
|
|
|
584
598
|
if (!handler) {
|
package/dist/js/node/utils.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.toPath = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
|
|
6
|
+
exports.useLocalPrefix = exports.toPath = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
|
|
7
7
|
|
|
8
8
|
var _pathToRegexp = require("path-to-regexp");
|
|
9
9
|
|
|
@@ -97,17 +97,28 @@ const toPath = (reg, params) => {
|
|
|
97
97
|
|
|
98
98
|
exports.toPath = toPath;
|
|
99
99
|
|
|
100
|
+
const useLocalPrefix = url => {
|
|
101
|
+
return (0, _utils.isProd)() && !url.includes('.');
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
exports.useLocalPrefix = useLocalPrefix;
|
|
105
|
+
|
|
100
106
|
const getStaticReg = (output = {}) => {
|
|
101
107
|
const {
|
|
102
108
|
favicon,
|
|
103
109
|
faviconByEntries,
|
|
104
110
|
cssPath,
|
|
105
111
|
jsPath,
|
|
106
|
-
mediaPath
|
|
112
|
+
mediaPath,
|
|
113
|
+
assetPrefix = '/'
|
|
107
114
|
} = output;
|
|
115
|
+
const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : '';
|
|
108
116
|
const favicons = prepareFavicons(favicon, faviconByEntries);
|
|
109
117
|
const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
|
|
110
|
-
const
|
|
118
|
+
const staticReg = ['static/', 'upload/', ...staticFiles];
|
|
119
|
+
const iconReg = ['favicon.ico', 'icon.png', ...favicons];
|
|
120
|
+
const regPrefix = prefix === '/' ? '' : prefix;
|
|
121
|
+
const staticPathRegExp = new RegExp(`^${regPrefix}/(${[...staticReg, ...iconReg].join('|')})`);
|
|
111
122
|
return staticPathRegExp;
|
|
112
123
|
};
|
|
113
124
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { NormalizedConfig } from '@modern-js/core';
|
|
1
2
|
import { NextFunction } from '../type';
|
|
2
3
|
import { ModernServerContext } from './context';
|
|
3
4
|
declare type Rule = {
|
|
4
5
|
path: string | RegExp;
|
|
5
6
|
target: string;
|
|
6
7
|
};
|
|
7
|
-
export declare const createStaticFileHandler: (rules: Rule[]) => (context: ModernServerContext, next: NextFunction) => Promise<void>;
|
|
8
|
+
export declare const createStaticFileHandler: (rules: Rule[], output?: NormalizedConfig['output']) => (context: ModernServerContext, next: NextFunction) => Promise<void>;
|
|
8
9
|
export {};
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -15,5 +15,6 @@ export declare const createMiddlewareCollecter: () => {
|
|
|
15
15
|
addAPIMiddleware: (input: any) => void;
|
|
16
16
|
};
|
|
17
17
|
export declare const toPath: (reg: string, params: Record<string, any>) => string;
|
|
18
|
+
export declare const useLocalPrefix: (url: string) => boolean;
|
|
18
19
|
export declare const getStaticReg: (output?: NormalizedConfig['output']) => RegExp;
|
|
19
20
|
export declare const prepareFavicons: (favicon: string | undefined, faviconByEntries?: Record<string, string | undefined>) => string[];
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.2.
|
|
14
|
+
"version": "1.2.2",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"ua-parser-js": "^0.7.28"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@modern-js/types": "1.6.
|
|
47
|
-
"@modern-js/core": "1.13.
|
|
46
|
+
"@modern-js/types": "1.6.1",
|
|
47
|
+
"@modern-js/core": "1.13.2",
|
|
48
48
|
"@scripts/jest-config": "0.0.0",
|
|
49
49
|
"@scripts/build": "0.0.0",
|
|
50
50
|
"@types/cookie": "^0.4.1",
|