@modern-js/plugin-server 2.0.0-beta.3 → 2.0.0-beta.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,83 +1,115 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __objRest = (source, exclude) => {
10
+ var target = {};
11
+ for (var prop in source)
12
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
13
+ target[prop] = source[prop];
14
+ if (source != null && __getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(source)) {
16
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
17
+ target[prop] = source[prop];
18
+ }
19
+ return target;
20
+ };
21
+ var __export = (target, all) => {
22
+ for (var name in all)
23
+ __defProp(target, name, { get: all[name], enumerable: true });
24
+ };
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") {
27
+ for (let key of __getOwnPropNames(from))
28
+ if (!__hasOwnProp.call(to, key) && key !== except)
29
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
30
+ }
31
+ return to;
32
+ };
33
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var server_exports = {};
39
+ __export(server_exports, {
40
+ default: () => server_default
5
41
  });
6
- exports.default = void 0;
7
- var _path = _interopRequireDefault(require("path"));
8
- var _utils = require("@modern-js/utils");
9
- const _excluded = ["default", "middleware"];
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
12
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
13
- 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; }
14
- const WEB_APP_NAME = 'index';
15
- var HOOKS;
16
- (function (HOOKS) {
17
- HOOKS["AFTER_MATCH"] = "afterMatch";
18
- HOOKS["AFTER_RENDER"] = "afterRender";
19
- })(HOOKS || (HOOKS = {}));
42
+ module.exports = __toCommonJS(server_exports);
43
+ var import_path = __toESM(require("path"));
44
+ var import_utils = require("@modern-js/utils");
45
+ const WEB_APP_NAME = "index";
46
+ var HOOKS = /* @__PURE__ */ ((HOOKS2) => {
47
+ HOOKS2["AFTER_MATCH"] = "afterMatch";
48
+ HOOKS2["AFTER_RENDER"] = "afterRender";
49
+ return HOOKS2;
50
+ })(HOOKS || {});
20
51
  class Storage {
21
52
  constructor() {
22
- _defineProperty(this, "middlewares", []);
23
- _defineProperty(this, "hooks", {});
53
+ this.middlewares = [];
54
+ this.hooks = {};
24
55
  }
25
56
  reset() {
26
57
  this.middlewares = [];
27
58
  this.hooks = {};
28
59
  }
29
60
  }
30
- const createTransformAPI = storage => new Proxy({}, {
31
- get(target, name) {
32
- if (name === 'addMiddleware') {
33
- return fn => storage.middlewares.push(fn);
61
+ const createTransformAPI = (storage) => new Proxy(
62
+ {},
63
+ {
64
+ get(target, name) {
65
+ if (name === "addMiddleware") {
66
+ return (fn) => storage.middlewares.push(fn);
67
+ }
68
+ return (fn) => {
69
+ import_utils.logger.warn(
70
+ "declare hook in default export is to be deprecated, use named export instead"
71
+ );
72
+ storage.hooks[name] = fn;
73
+ };
34
74
  }
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
75
  }
40
- });
41
- const compose = middlewares => {
76
+ );
77
+ const compose = (middlewares) => {
42
78
  return (ctx, resolve, reject) => {
43
79
  let i = 0;
44
80
  const dispatch = () => {
45
- var _handler, _handler$catch;
81
+ var _a, _b;
46
82
  const handler = middlewares[i++];
47
83
  if (!handler) {
48
84
  return resolve();
49
85
  }
50
- return (_handler = handler(ctx, dispatch)) === null || _handler === void 0 ? void 0 : (_handler$catch = _handler.catch) === null || _handler$catch === void 0 ? void 0 : _handler$catch.call(_handler, reject);
86
+ return (_b = (_a = handler(ctx, dispatch)) == null ? void 0 : _a.catch) == null ? void 0 : _b.call(_a, reject);
51
87
  };
52
88
  return dispatch;
53
89
  };
54
90
  };
55
- var _default = () => ({
56
- name: '@modern-js/plugin-server',
57
- setup: api => {
58
- const {
59
- appDirectory,
60
- distDirectory
61
- } = api.useAppContext();
91
+ var server_default = () => ({
92
+ name: "@modern-js/plugin-server",
93
+ setup: (api) => {
94
+ const { appDirectory, distDirectory } = api.useAppContext();
62
95
  const storage = new Storage();
63
96
  const transformAPI = createTransformAPI(storage);
64
- const pwd = (0, _utils.isProd)() ? distDirectory : appDirectory;
65
- const webAppPath = _path.default.resolve(pwd, _utils.SERVER_DIR, WEB_APP_NAME);
97
+ const pwd = (0, import_utils.isProd)() ? distDirectory : appDirectory;
98
+ const webAppPath = import_path.default.resolve(pwd, import_utils.SERVER_DIR, WEB_APP_NAME);
99
+ const middlewarePath = import_path.default.resolve(pwd, import_utils.SERVER_DIR, "_middleware");
66
100
  const loadMod = () => {
67
- const mod = (0, _utils.requireExistModule)(webAppPath, {
101
+ const mod = (0, import_utils.requireExistModule)(webAppPath, {
102
+ interop: false
103
+ });
104
+ const middlewareMode = (0, import_utils.requireExistModule)(middlewarePath, {
68
105
  interop: false
69
106
  });
70
- const {
71
- default: defaultExports,
72
- middleware
73
- } = mod,
74
- hooks = _objectWithoutProperties(mod, _excluded);
107
+ const _a = mod || {}, { default: defaultExports, middleware } = _a, hooks = __objRest(_a, ["default", "middleware"]);
108
+ const { middleware: unstableMiddleware = [] } = middlewareMode || {};
75
109
  if (defaultExports) {
76
110
  defaultExports(transformAPI);
77
111
  }
78
-
79
- // named export hooks will overrides hooks in default export function
80
- Object.values(HOOKS).forEach(key => {
112
+ Object.values(HOOKS).forEach((key) => {
81
113
  const fn = hooks[key];
82
114
  if (fn) {
83
115
  storage.hooks[key] = fn;
@@ -86,6 +118,7 @@ var _default = () => ({
86
118
  if (middleware) {
87
119
  storage.middlewares = [].concat(middleware);
88
120
  }
121
+ storage.middlewares = storage.middlewares.concat(unstableMiddleware);
89
122
  };
90
123
  return {
91
124
  prepare() {
@@ -95,10 +128,8 @@ var _default = () => ({
95
128
  storage.reset();
96
129
  loadMod();
97
130
  },
98
- gather({
99
- addWebMiddleware
100
- }) {
101
- storage.middlewares.forEach(mid => {
131
+ gather({ addWebMiddleware }) {
132
+ storage.middlewares.forEach((mid) => {
102
133
  addWebMiddleware(mid);
103
134
  });
104
135
  },
@@ -114,21 +145,15 @@ var _default = () => ({
114
145
  }
115
146
  return storage.hooks.afterRender(context, next);
116
147
  },
117
- prepareWebServer({
118
- config
119
- }) {
120
- const {
121
- middleware
122
- } = config;
148
+ prepareWebServer({ config }) {
149
+ const { middleware } = config;
123
150
  const factory = compose(middleware);
124
- return ctx => {
151
+ return (ctx) => {
125
152
  const {
126
- source: {
127
- res
128
- }
153
+ source: { res }
129
154
  } = ctx;
130
155
  return new Promise((resolve, reject) => {
131
- res.on('finish', err => {
156
+ res.on("finish", (err) => {
132
157
  if (err) {
133
158
  return reject(err);
134
159
  }
@@ -142,4 +167,5 @@ var _default = () => ({
142
167
  };
143
168
  }
144
169
  });
145
- exports.default = _default;
170
+ // Annotate the CommonJS export names for ESM import in node:
171
+ 0 && (module.exports = {});
@@ -1,65 +1,201 @@
1
- import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
- import fs from 'fs';
4
- import path from 'path';
5
- import { compile } from '@modern-js/server-utils';
6
- import { SHARED_DIR, SERVER_DIR } from '@modern-js/utils';
7
- var TS_CONFIG_FILENAME = 'tsconfig.json';
8
- export default (function () {
9
- return {
10
- name: '@modern-js/plugin-server',
11
- setup: function setup(api) {
12
- return {
13
- config: function config() {
14
- return {};
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
- afterBuild: function afterBuild() {
17
- return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
18
- var _api$useAppContext, appDirectory, distDirectory, modernConfig, distDir, serverDir, sharedDir, tsconfigPath, sourceDirs, server, _modernConfig$source, alias, globalVars, babel;
19
- return _regeneratorRuntime().wrap(function _callee$(_context) {
20
- while (1) {
21
- switch (_context.prev = _context.next) {
22
- case 0:
23
- _api$useAppContext = api.useAppContext(), appDirectory = _api$useAppContext.appDirectory, distDirectory = _api$useAppContext.distDirectory;
24
- modernConfig = api.useResolvedConfigContext();
25
- distDir = path.resolve(distDirectory);
26
- serverDir = path.resolve(appDirectory, SERVER_DIR);
27
- sharedDir = path.resolve(appDirectory, SHARED_DIR);
28
- tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
29
- sourceDirs = [];
30
- if (fs.existsSync(serverDir)) {
31
- sourceDirs.push(serverDir);
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;
89
+ }
90
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
+ _.label = op[1];
92
+ break;
32
93
  }
33
- if (fs.existsSync(sharedDir)) {
34
- sourceDirs.push(sharedDir);
94
+ if (op[0] === 6 && _.label < t[1]) {
95
+ _.label = t[1];
96
+ t = op;
97
+ break;
35
98
  }
36
- server = modernConfig.server;
37
- _modernConfig$source = modernConfig.source, alias = _modernConfig$source.alias, globalVars = _modernConfig$source.globalVars;
38
- babel = modernConfig.tools.babel;
39
- if (!(sourceDirs.length > 0)) {
40
- _context.next = 15;
41
- break;
99
+ if (t && _.label < t[2]) {
100
+ _.label = t[2];
101
+ _.ops.push(op);
102
+ break;
42
103
  }
43
- _context.next = 15;
44
- return compile(appDirectory, {
45
- server: server,
46
- alias: alias,
47
- globalVars: globalVars,
48
- babelConfig: babel
49
- }, {
50
- sourceDirs: sourceDirs,
51
- distDir: distDir,
52
- tsconfigPath: tsconfigPath
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;
117
+ }
118
+ if (op[0] & 5) throw op[1];
119
+ return {
120
+ value: op[0] ? op[1] : void 0,
121
+ done: true
122
+ };
123
+ }
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
+ collectServerPlugins: function collectServerPlugins(param) {
189
+ var plugins = param.plugins;
190
+ plugins.push({
191
+ "@modern-js/plugin-server": "@modern-js/plugin-server/server"
53
192
  });
54
- case 15:
55
- case "end":
56
- return _context.stop();
193
+ return {
194
+ plugins: plugins
195
+ };
57
196
  }
58
- }
59
- }, _callee);
60
- }))();
197
+ };
61
198
  }
62
- };
63
- }
64
- };
65
- });
199
+ };
200
+ };
201
+ export { cli_default as default };
@@ -0,0 +1 @@
1
+ "use strict";