@modern-js/plugin-server 2.0.0-beta.3 → 2.0.0-beta.4
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +38 -0
- package/dist/js/modern/cli.js +67 -48
- package/dist/js/modern/modern-app-env.d.js +0 -0
- package/dist/js/modern/server.js +64 -57
- package/dist/js/node/cli.js +90 -54
- package/dist/js/node/modern-app-env.d.js +0 -0
- package/dist/js/node/server.js +85 -66
- package/dist/js/treeshaking/cli.js +186 -59
- package/dist/js/treeshaking/modern-app-env.d.js +1 -0
- package/dist/js/treeshaking/server.js +186 -131
- package/dist/types/cli.d.ts +2 -0
- package/dist/types/server.d.ts +2 -0
- package/package.json +9 -9
package/dist/js/node/server.js
CHANGED
@@ -1,83 +1,111 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Object.
|
4
|
-
|
1
|
+
var __create = Object.create;
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
7
|
+
var __export = (target, all) => {
|
8
|
+
for (var name in all)
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
10
|
+
};
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
13
|
+
for (let key of __getOwnPropNames(from))
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
16
|
+
}
|
17
|
+
return to;
|
18
|
+
};
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
21
|
+
mod
|
22
|
+
));
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
24
|
+
var stdin_exports = {};
|
25
|
+
__export(stdin_exports, {
|
26
|
+
default: () => server_default
|
5
27
|
});
|
6
|
-
exports
|
7
|
-
var
|
8
|
-
var
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
28
|
+
module.exports = __toCommonJS(stdin_exports);
|
29
|
+
var import_path = __toESM(require("path"));
|
30
|
+
var import_utils = require("@modern-js/utils");
|
31
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
32
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
33
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
34
|
+
var __objRest = (source, exclude) => {
|
35
|
+
var target = {};
|
36
|
+
for (var prop in source)
|
37
|
+
if (__hasOwnProp2.call(source, prop) && exclude.indexOf(prop) < 0)
|
38
|
+
target[prop] = source[prop];
|
39
|
+
if (source != null && __getOwnPropSymbols)
|
40
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
41
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
42
|
+
target[prop] = source[prop];
|
43
|
+
}
|
44
|
+
return target;
|
45
|
+
};
|
46
|
+
const WEB_APP_NAME = "index";
|
47
|
+
var HOOKS = /* @__PURE__ */ ((HOOKS2) => {
|
48
|
+
HOOKS2["AFTER_MATCH"] = "afterMatch";
|
49
|
+
HOOKS2["AFTER_RENDER"] = "afterRender";
|
50
|
+
return HOOKS2;
|
51
|
+
})(HOOKS || {});
|
20
52
|
class Storage {
|
21
53
|
constructor() {
|
22
|
-
|
23
|
-
|
54
|
+
this.middlewares = [];
|
55
|
+
this.hooks = {};
|
24
56
|
}
|
25
57
|
reset() {
|
26
58
|
this.middlewares = [];
|
27
59
|
this.hooks = {};
|
28
60
|
}
|
29
61
|
}
|
30
|
-
const createTransformAPI = storage => new Proxy(
|
31
|
-
|
32
|
-
|
33
|
-
|
62
|
+
const createTransformAPI = (storage) => new Proxy(
|
63
|
+
{},
|
64
|
+
{
|
65
|
+
get(target, name) {
|
66
|
+
if (name === "addMiddleware") {
|
67
|
+
return (fn) => storage.middlewares.push(fn);
|
68
|
+
}
|
69
|
+
return (fn) => {
|
70
|
+
import_utils.logger.warn(
|
71
|
+
"declare hook in default export is to be deprecated, use named export instead"
|
72
|
+
);
|
73
|
+
storage.hooks[name] = fn;
|
74
|
+
};
|
34
75
|
}
|
35
|
-
return fn => {
|
36
|
-
_utils.logger.warn('declare hook in default export is to be deprecated, use named export instead');
|
37
|
-
storage.hooks[name] = fn;
|
38
|
-
};
|
39
76
|
}
|
40
|
-
|
41
|
-
const compose = middlewares => {
|
77
|
+
);
|
78
|
+
const compose = (middlewares) => {
|
42
79
|
return (ctx, resolve, reject) => {
|
43
80
|
let i = 0;
|
44
81
|
const dispatch = () => {
|
45
|
-
var
|
82
|
+
var _a, _b;
|
46
83
|
const handler = middlewares[i++];
|
47
84
|
if (!handler) {
|
48
85
|
return resolve();
|
49
86
|
}
|
50
|
-
return (
|
87
|
+
return (_b = (_a = handler(ctx, dispatch)) == null ? void 0 : _a.catch) == null ? void 0 : _b.call(_a, reject);
|
51
88
|
};
|
52
89
|
return dispatch;
|
53
90
|
};
|
54
91
|
};
|
55
|
-
var
|
56
|
-
name:
|
57
|
-
setup: api => {
|
58
|
-
const {
|
59
|
-
appDirectory,
|
60
|
-
distDirectory
|
61
|
-
} = api.useAppContext();
|
92
|
+
var server_default = () => ({
|
93
|
+
name: "@modern-js/plugin-server",
|
94
|
+
setup: (api) => {
|
95
|
+
const { appDirectory, distDirectory } = api.useAppContext();
|
62
96
|
const storage = new Storage();
|
63
97
|
const transformAPI = createTransformAPI(storage);
|
64
|
-
const pwd = (0,
|
65
|
-
const webAppPath =
|
98
|
+
const pwd = (0, import_utils.isProd)() ? distDirectory : appDirectory;
|
99
|
+
const webAppPath = import_path.default.resolve(pwd, import_utils.SERVER_DIR, WEB_APP_NAME);
|
66
100
|
const loadMod = () => {
|
67
|
-
const mod = (0,
|
101
|
+
const mod = (0, import_utils.requireExistModule)(webAppPath, {
|
68
102
|
interop: false
|
69
103
|
});
|
70
|
-
const {
|
71
|
-
default: defaultExports,
|
72
|
-
middleware
|
73
|
-
} = mod,
|
74
|
-
hooks = _objectWithoutProperties(mod, _excluded);
|
104
|
+
const _a = mod, { default: defaultExports, middleware } = _a, hooks = __objRest(_a, ["default", "middleware"]);
|
75
105
|
if (defaultExports) {
|
76
106
|
defaultExports(transformAPI);
|
77
107
|
}
|
78
|
-
|
79
|
-
// named export hooks will overrides hooks in default export function
|
80
|
-
Object.values(HOOKS).forEach(key => {
|
108
|
+
Object.values(HOOKS).forEach((key) => {
|
81
109
|
const fn = hooks[key];
|
82
110
|
if (fn) {
|
83
111
|
storage.hooks[key] = fn;
|
@@ -95,10 +123,8 @@ var _default = () => ({
|
|
95
123
|
storage.reset();
|
96
124
|
loadMod();
|
97
125
|
},
|
98
|
-
gather({
|
99
|
-
|
100
|
-
}) {
|
101
|
-
storage.middlewares.forEach(mid => {
|
126
|
+
gather({ addWebMiddleware }) {
|
127
|
+
storage.middlewares.forEach((mid) => {
|
102
128
|
addWebMiddleware(mid);
|
103
129
|
});
|
104
130
|
},
|
@@ -114,21 +140,15 @@ var _default = () => ({
|
|
114
140
|
}
|
115
141
|
return storage.hooks.afterRender(context, next);
|
116
142
|
},
|
117
|
-
prepareWebServer({
|
118
|
-
config
|
119
|
-
}) {
|
120
|
-
const {
|
121
|
-
middleware
|
122
|
-
} = config;
|
143
|
+
prepareWebServer({ config }) {
|
144
|
+
const { middleware } = config;
|
123
145
|
const factory = compose(middleware);
|
124
|
-
return ctx => {
|
146
|
+
return (ctx) => {
|
125
147
|
const {
|
126
|
-
source: {
|
127
|
-
res
|
128
|
-
}
|
148
|
+
source: { res }
|
129
149
|
} = ctx;
|
130
150
|
return new Promise((resolve, reject) => {
|
131
|
-
res.on(
|
151
|
+
res.on("finish", (err) => {
|
132
152
|
if (err) {
|
133
153
|
return reject(err);
|
134
154
|
}
|
@@ -142,4 +162,3 @@ var _default = () => ({
|
|
142
162
|
};
|
143
163
|
}
|
144
164
|
});
|
145
|
-
exports.default = _default;
|
@@ -1,65 +1,192 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
31
|
+
var f, y, t, g, _ = {
|
32
|
+
label: 0,
|
33
|
+
sent: function() {
|
34
|
+
if (t[0] & 1) throw t[1];
|
35
|
+
return t[1];
|
15
36
|
},
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
37
|
+
trys: [],
|
38
|
+
ops: []
|
39
|
+
};
|
40
|
+
return g = {
|
41
|
+
next: verb(0),
|
42
|
+
"throw": verb(1),
|
43
|
+
"return": verb(2)
|
44
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
45
|
+
return this;
|
46
|
+
}), g;
|
47
|
+
function verb(n) {
|
48
|
+
return function(v) {
|
49
|
+
return step([
|
50
|
+
n,
|
51
|
+
v
|
52
|
+
]);
|
53
|
+
};
|
54
|
+
}
|
55
|
+
function step(op) {
|
56
|
+
if (f) throw new TypeError("Generator is already executing.");
|
57
|
+
while(_)try {
|
58
|
+
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;
|
59
|
+
if (y = 0, t) op = [
|
60
|
+
op[0] & 2,
|
61
|
+
t.value
|
62
|
+
];
|
63
|
+
switch(op[0]){
|
64
|
+
case 0:
|
65
|
+
case 1:
|
66
|
+
t = op;
|
67
|
+
break;
|
68
|
+
case 4:
|
69
|
+
_.label++;
|
70
|
+
return {
|
71
|
+
value: op[1],
|
72
|
+
done: false
|
73
|
+
};
|
74
|
+
case 5:
|
75
|
+
_.label++;
|
76
|
+
y = op[1];
|
77
|
+
op = [
|
78
|
+
0
|
79
|
+
];
|
80
|
+
continue;
|
81
|
+
case 7:
|
82
|
+
op = _.ops.pop();
|
83
|
+
_.trys.pop();
|
84
|
+
continue;
|
85
|
+
default:
|
86
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
87
|
+
_ = 0;
|
88
|
+
continue;
|
32
89
|
}
|
33
|
-
if (
|
34
|
-
|
90
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
91
|
+
_.label = op[1];
|
92
|
+
break;
|
35
93
|
}
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
_context.next = 15;
|
41
|
-
break;
|
94
|
+
if (op[0] === 6 && _.label < t[1]) {
|
95
|
+
_.label = t[1];
|
96
|
+
t = op;
|
97
|
+
break;
|
42
98
|
}
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
99
|
+
if (t && _.label < t[2]) {
|
100
|
+
_.label = t[2];
|
101
|
+
_.ops.push(op);
|
102
|
+
break;
|
103
|
+
}
|
104
|
+
if (t[2]) _.ops.pop();
|
105
|
+
_.trys.pop();
|
106
|
+
continue;
|
107
|
+
}
|
108
|
+
op = body.call(thisArg, _);
|
109
|
+
} catch (e) {
|
110
|
+
op = [
|
111
|
+
6,
|
112
|
+
e
|
113
|
+
];
|
114
|
+
y = 0;
|
115
|
+
} finally{
|
116
|
+
f = t = 0;
|
61
117
|
}
|
62
|
-
|
118
|
+
if (op[0] & 5) throw op[1];
|
119
|
+
return {
|
120
|
+
value: op[0] ? op[1] : void 0,
|
121
|
+
done: true
|
122
|
+
};
|
63
123
|
}
|
64
|
-
|
65
|
-
|
124
|
+
};
|
125
|
+
import fs from "fs";
|
126
|
+
import path from "path";
|
127
|
+
import { compile } from "@modern-js/server-utils";
|
128
|
+
import { SHARED_DIR, SERVER_DIR } from "@modern-js/utils";
|
129
|
+
var TS_CONFIG_FILENAME = "tsconfig.json";
|
130
|
+
var cli_default = function() {
|
131
|
+
return {
|
132
|
+
name: "@modern-js/plugin-server",
|
133
|
+
setup: function(api) {
|
134
|
+
return {
|
135
|
+
config: function config() {
|
136
|
+
return {};
|
137
|
+
},
|
138
|
+
afterBuild: function afterBuild() {
|
139
|
+
return _asyncToGenerator(function() {
|
140
|
+
var ref, appDirectory, distDirectory, modernConfig, distDir, serverDir, sharedDir, tsconfigPath, sourceDirs, server, _source, alias, globalVars, babel;
|
141
|
+
return __generator(this, function(_state) {
|
142
|
+
switch(_state.label){
|
143
|
+
case 0:
|
144
|
+
ref = api.useAppContext(), appDirectory = ref.appDirectory, distDirectory = ref.distDirectory;
|
145
|
+
modernConfig = api.useResolvedConfigContext();
|
146
|
+
distDir = path.resolve(distDirectory);
|
147
|
+
serverDir = path.resolve(appDirectory, SERVER_DIR);
|
148
|
+
sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
149
|
+
tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
150
|
+
sourceDirs = [];
|
151
|
+
if (fs.existsSync(serverDir)) {
|
152
|
+
sourceDirs.push(serverDir);
|
153
|
+
}
|
154
|
+
if (fs.existsSync(sharedDir)) {
|
155
|
+
sourceDirs.push(sharedDir);
|
156
|
+
}
|
157
|
+
server = modernConfig.server;
|
158
|
+
_source = modernConfig.source, alias = _source.alias, globalVars = _source.globalVars;
|
159
|
+
babel = modernConfig.tools.babel;
|
160
|
+
if (!(sourceDirs.length > 0)) return [
|
161
|
+
3,
|
162
|
+
2
|
163
|
+
];
|
164
|
+
return [
|
165
|
+
4,
|
166
|
+
compile(appDirectory, {
|
167
|
+
server: server,
|
168
|
+
alias: alias,
|
169
|
+
globalVars: globalVars,
|
170
|
+
babelConfig: babel
|
171
|
+
}, {
|
172
|
+
sourceDirs: sourceDirs,
|
173
|
+
distDir: distDir,
|
174
|
+
tsconfigPath: tsconfigPath
|
175
|
+
})
|
176
|
+
];
|
177
|
+
case 1:
|
178
|
+
_state.sent();
|
179
|
+
_state.label = 2;
|
180
|
+
case 2:
|
181
|
+
return [
|
182
|
+
2
|
183
|
+
];
|
184
|
+
}
|
185
|
+
});
|
186
|
+
})();
|
187
|
+
}
|
188
|
+
};
|
189
|
+
}
|
190
|
+
};
|
191
|
+
};
|
192
|
+
export { cli_default as default };
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|