@modern-js/server 3.7.0 → 3.8.1

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/dist/cjs/dev.js CHANGED
@@ -1,14 +1,5 @@
1
1
  "use strict";
2
2
  var __webpack_require__ = {};
3
- (()=>{
4
- __webpack_require__.n = (module)=>{
5
- var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
- __webpack_require__.d(getter, {
7
- a: getter
8
- });
9
- return getter;
10
- };
11
- })();
12
3
  (()=>{
13
4
  __webpack_require__.d = (exports1, getters, values)=>{
14
5
  var define = (defs, kind)=>{
@@ -40,9 +31,6 @@ __webpack_require__.d(__webpack_exports__, {
40
31
  devRuntimeMiddlewarePlugin: ()=>devRuntimeMiddlewarePlugin,
41
32
  setupDevInfra: ()=>setupDevInfra
42
33
  });
43
- const external_node_path_namespaceObject = require("node:path");
44
- var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
45
- const server_core_namespaceObject = require("@modern-js/server-core");
46
34
  const node_namespaceObject = require("@modern-js/server-core/node");
47
35
  const utils_namespaceObject = require("@modern-js/utils");
48
36
  const index_js_namespaceObject = require("./helpers/index.js");
@@ -71,7 +59,7 @@ const devRuntimeMiddlewarePlugin = (options, compiler)=>({
71
59
  handler: (0, node_namespaceObject.connectMid2HonoMid)(middleware)
72
60
  });
73
61
  });
74
- const mockMiddleware = await (0, index_js_namespaceObject.getMockMiddleware)(pwd);
62
+ const mockMiddleware = await (0, index_js_namespaceObject.getMockMiddleware)(pwd, options.config?.dev?.mockDir);
75
63
  middlewares.push({
76
64
  name: 'mock-dev',
77
65
  handler: mockMiddleware
@@ -109,16 +97,18 @@ function setupDevInfra({ config, pwd, distDir, apiDir, sharedDir, builder, build
109
97
  }
110
98
  });
111
99
  const { watchOptions } = config.server;
112
- const mockPath = external_node_path_default().normalize(external_node_path_default().join(pwd, server_core_namespaceObject.AGGRED_DIR.mock));
100
+ const mockDir = config.dev?.mockDir;
101
+ const mockPath = (0, index_js_namespaceObject.resolveMockDirectory)(pwd, mockDir);
113
102
  const watcher = (0, index_js_namespaceObject.startWatcher)({
114
103
  pwd,
115
104
  distDir,
116
105
  apiDir: apiDir || utils_namespaceObject.API_DIR,
117
106
  sharedDir: sharedDir || utils_namespaceObject.SHARED_DIR,
107
+ mockDir,
118
108
  watchOptions,
119
109
  onChange: (filepath, event)=>{
120
110
  const runtimeServer = getRuntimeServer();
121
- if (runtimeServer && !filepath.startsWith(mockPath)) {
111
+ if (runtimeServer && !(0, index_js_namespaceObject.isPathInsideDirectory)(filepath, mockPath)) {
122
112
  const fileChangeEvent = {
123
113
  type: 'file-change',
124
114
  payload: [
@@ -15,9 +15,6 @@ var __webpack_modules__ = {
15
15
  "./repack" (module) {
16
16
  module.exports = require("./repack.js");
17
17
  },
18
- "@modern-js/server-core" (module) {
19
- module.exports = require("@modern-js/server-core");
20
- },
21
18
  "@modern-js/utils" (module) {
22
19
  module.exports = require("@modern-js/utils");
23
20
  },
@@ -74,10 +71,10 @@ var __webpack_exports__ = {};
74
71
  __webpack_require__.r(__webpack_exports__);
75
72
  var path__rspack_import_0 = __webpack_require__("path");
76
73
  var path__rspack_import_0_default = /*#__PURE__*/ __webpack_require__.n(path__rspack_import_0);
77
- var _modern_js_server_core__rspack_import_1 = __webpack_require__("@modern-js/server-core");
78
- var _modern_js_utils__rspack_import_2 = __webpack_require__("@modern-js/utils");
79
- var _dev_tools_watcher__rspack_import_3 = __webpack_require__("../dev-tools/watcher");
80
- var _dev_tools_watcher__rspack_import_3_default = /*#__PURE__*/ __webpack_require__.n(_dev_tools_watcher__rspack_import_3);
74
+ var _modern_js_utils__rspack_import_1 = __webpack_require__("@modern-js/utils");
75
+ var _dev_tools_watcher__rspack_import_2 = __webpack_require__("../dev-tools/watcher");
76
+ var _dev_tools_watcher__rspack_import_2_default = /*#__PURE__*/ __webpack_require__.n(_dev_tools_watcher__rspack_import_2);
77
+ var _mock__rspack_import_3 = __webpack_require__("./mock");
81
78
  var _repack__rspack_import_4 = __webpack_require__("./repack");
82
79
  var __rspack_reexport = {};
83
80
  for(const __rspack_import_key in _repack__rspack_import_4)if ([
@@ -99,28 +96,26 @@ var __webpack_exports__ = {};
99
96
  "default"
100
97
  ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_fileReader__rspack_import_6[__rspack_import_key];
101
98
  __webpack_require__.d(__webpack_exports__, __rspack_reexport);
102
- var _mock__rspack_import_7 = __webpack_require__("./mock");
103
99
  var __rspack_reexport = {};
104
- for(const __rspack_import_key in _mock__rspack_import_7)if ([
100
+ for(const __rspack_import_key in _mock__rspack_import_3)if ([
105
101
  "startWatcher",
106
102
  "default"
107
- ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_mock__rspack_import_7[__rspack_import_key];
103
+ ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_mock__rspack_import_3[__rspack_import_key];
108
104
  __webpack_require__.d(__webpack_exports__, __rspack_reexport);
109
- function startWatcher({ pwd, distDir, apiDir, sharedDir, watchOptions, onChange }) {
110
- const { mock } = _modern_js_server_core__rspack_import_1.AGGRED_DIR;
105
+ function startWatcher({ pwd, distDir, apiDir, sharedDir, mockDir, watchOptions, onChange }) {
111
106
  const defaultWatched = [
112
- `${mock}/**/*`,
113
- `${_modern_js_utils__rspack_import_2.SERVER_DIR}/**/*`,
107
+ path__rspack_import_0_default().join((0, _mock__rspack_import_3.resolveMockDirectory)(pwd, mockDir), '**/*'),
108
+ `${_modern_js_utils__rspack_import_1.SERVER_DIR}/**/*`,
114
109
  `${apiDir}/**`,
115
110
  `${sharedDir}/**/*`,
116
- `${distDir}/${_modern_js_utils__rspack_import_2.SERVER_BUNDLE_DIRECTORY}/*-server-loaders.js`
111
+ `${distDir}/${_modern_js_utils__rspack_import_1.SERVER_BUNDLE_DIRECTORY}/*-server-loaders.js`
117
112
  ];
118
- const mergedWatchOptions = (0, _dev_tools_watcher__rspack_import_3.mergeWatchOptions)(watchOptions);
113
+ const mergedWatchOptions = (0, _dev_tools_watcher__rspack_import_2.mergeWatchOptions)(watchOptions);
119
114
  const defaultWatchedPaths = defaultWatched.map((p)=>{
120
115
  const finalPath = path__rspack_import_0_default().isAbsolute(p) ? p : path__rspack_import_0_default().join(pwd, p);
121
116
  return path__rspack_import_0_default().normalize(finalPath);
122
117
  });
123
- const watcher = new (_dev_tools_watcher__rspack_import_3_default())();
118
+ const watcher = new (_dev_tools_watcher__rspack_import_2_default())();
124
119
  watcher.createDepTree();
125
120
  watcher.listen(defaultWatchedPaths, mergedWatchOptions, (filepath, event)=>{
126
121
  if (filepath.includes('-server-loaders.js')) return void delete require.cache[filepath];
@@ -39,7 +39,9 @@ __webpack_require__.r(__webpack_exports__);
39
39
  __webpack_require__.d(__webpack_exports__, {
40
40
  getMatched: ()=>getMatched,
41
41
  getMockMiddleware: ()=>getMockMiddleware,
42
- initOrUpdateMockMiddlewares: ()=>initOrUpdateMockMiddlewares
42
+ initOrUpdateMockMiddlewares: ()=>initOrUpdateMockMiddlewares,
43
+ isPathInsideDirectory: ()=>isPathInsideDirectory,
44
+ resolveMockDirectory: ()=>resolveMockDirectory
43
45
  });
44
46
  const external_node_path_namespaceObject = require("node:path");
45
47
  var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
@@ -49,6 +51,11 @@ const utils_namespaceObject = require("@modern-js/utils");
49
51
  const external_path_to_regexp_namespaceObject = require("path-to-regexp");
50
52
  let mockAPIs = [];
51
53
  let mockConfig;
54
+ const resolveMockDirectory = (pwd, mockDir = server_core_namespaceObject.AGGRED_DIR.mock)=>external_node_path_default().resolve(pwd, mockDir);
55
+ const isPathInsideDirectory = (filepath, directory)=>{
56
+ const relativePath = external_node_path_default().relative(directory, external_node_path_default().resolve(filepath));
57
+ return '' === relativePath || '..' !== relativePath && !relativePath.startsWith(`..${external_node_path_default().sep}`) && !external_node_path_default().isAbsolute(relativePath);
58
+ };
52
59
  const parseKey = (key)=>{
53
60
  const _blank = ' ';
54
61
  const splitted = key.split(_blank).filter(Boolean);
@@ -64,14 +71,15 @@ const parseKey = (key)=>{
64
71
  path: key
65
72
  };
66
73
  };
67
- const getMockModule = async (pwd)=>{
74
+ const getMockModule = async (pwd, mockDir)=>{
68
75
  const exts = [
69
76
  '.ts',
70
77
  '.js'
71
78
  ];
72
79
  let mockFilePath = '';
80
+ const mockDirectory = resolveMockDirectory(pwd, mockDir);
73
81
  for (const ext of exts){
74
- const maybeMatch = external_node_path_default().join(pwd, `${server_core_namespaceObject.AGGRED_DIR.mock}/index${ext}`);
82
+ const maybeMatch = external_node_path_default().join(mockDirectory, `index${ext}`);
75
83
  if (await utils_namespaceObject.fs.pathExists(maybeMatch)) {
76
84
  mockFilePath = maybeMatch;
77
85
  break;
@@ -99,8 +107,8 @@ const getMatched = (request, mockApis)=>{
99
107
  });
100
108
  return matched;
101
109
  };
102
- async function initOrUpdateMockMiddlewares(pwd) {
103
- const mockModule = await getMockModule(pwd);
110
+ async function initOrUpdateMockMiddlewares(pwd, mockDir) {
111
+ const mockModule = await getMockModule(pwd, mockDir);
104
112
  mockConfig = mockModule?.config;
105
113
  mockAPIs = Object.entries(mockModule?.mockHandlers || {}).map(([key, handler])=>{
106
114
  const { method, path } = parseKey(key);
@@ -111,8 +119,8 @@ async function initOrUpdateMockMiddlewares(pwd) {
111
119
  };
112
120
  });
113
121
  }
114
- async function getMockMiddleware(pwd) {
115
- await initOrUpdateMockMiddlewares(pwd);
122
+ async function getMockMiddleware(pwd, mockDir) {
123
+ await initOrUpdateMockMiddlewares(pwd, mockDir);
116
124
  const mockMiddleware = async (c, next)=>{
117
125
  if ('function' == typeof mockConfig?.enable) {
118
126
  const isEnabled = mockConfig.enable(c.env.node.req, c.env.node.res);
@@ -131,10 +139,14 @@ async function getMockMiddleware(pwd) {
131
139
  exports.getMatched = __webpack_exports__.getMatched;
132
140
  exports.getMockMiddleware = __webpack_exports__.getMockMiddleware;
133
141
  exports.initOrUpdateMockMiddlewares = __webpack_exports__.initOrUpdateMockMiddlewares;
142
+ exports.isPathInsideDirectory = __webpack_exports__.isPathInsideDirectory;
143
+ exports.resolveMockDirectory = __webpack_exports__.resolveMockDirectory;
134
144
  for(var __rspack_i in __webpack_exports__)if (-1 === [
135
145
  "getMatched",
136
146
  "getMockMiddleware",
137
- "initOrUpdateMockMiddlewares"
147
+ "initOrUpdateMockMiddlewares",
148
+ "isPathInsideDirectory",
149
+ "resolveMockDirectory"
138
150
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
139
151
  Object.defineProperty(exports, '__esModule', {
140
152
  value: true
package/dist/esm/dev.mjs CHANGED
@@ -1,8 +1,6 @@
1
- import node_path from "node:path";
2
- import { AGGRED_DIR } from "@modern-js/server-core";
3
1
  import { connectMid2HonoMid } from "@modern-js/server-core/node";
4
2
  import { API_DIR, SHARED_DIR, logger } from "@modern-js/utils";
5
- import { getDevOptions, getMockMiddleware, initFileReader, onRepack, startWatcher } from "./helpers/index.mjs";
3
+ import { getDevOptions, getMockMiddleware, initFileReader, isPathInsideDirectory, onRepack, resolveMockDirectory, startWatcher } from "./helpers/index.mjs";
6
4
  const devRuntimeMiddlewarePlugin = (options, compiler)=>({
7
5
  name: '@modern-js/plugin-dev',
8
6
  setup (api) {
@@ -28,7 +26,7 @@ const devRuntimeMiddlewarePlugin = (options, compiler)=>({
28
26
  handler: connectMid2HonoMid(middleware)
29
27
  });
30
28
  });
31
- const mockMiddleware = await getMockMiddleware(pwd);
29
+ const mockMiddleware = await getMockMiddleware(pwd, options.config?.dev?.mockDir);
32
30
  middlewares.push({
33
31
  name: 'mock-dev',
34
32
  handler: mockMiddleware
@@ -66,16 +64,18 @@ function setupDevInfra({ config, pwd, distDir, apiDir, sharedDir, builder, build
66
64
  }
67
65
  });
68
66
  const { watchOptions } = config.server;
69
- const mockPath = node_path.normalize(node_path.join(pwd, AGGRED_DIR.mock));
67
+ const mockDir = config.dev?.mockDir;
68
+ const mockPath = resolveMockDirectory(pwd, mockDir);
70
69
  const watcher = startWatcher({
71
70
  pwd,
72
71
  distDir,
73
72
  apiDir: apiDir || API_DIR,
74
73
  sharedDir: sharedDir || SHARED_DIR,
74
+ mockDir,
75
75
  watchOptions,
76
76
  onChange: (filepath, event)=>{
77
77
  const runtimeServer = getRuntimeServer();
78
- if (runtimeServer && !filepath.startsWith(mockPath)) {
78
+ if (runtimeServer && !isPathInsideDirectory(filepath, mockPath)) {
79
79
  const fileChangeEvent = {
80
80
  type: 'file-change',
81
81
  payload: [
@@ -1,15 +1,14 @@
1
1
  import path from "path";
2
- import { AGGRED_DIR } from "@modern-js/server-core";
3
2
  import { SERVER_BUNDLE_DIRECTORY, SERVER_DIR } from "@modern-js/utils";
4
3
  import dev_tools_watcher, { mergeWatchOptions } from "../dev-tools/watcher/index.mjs";
4
+ import { resolveMockDirectory } from "./mock.mjs";
5
5
  export * from "./repack.mjs";
6
6
  export * from "./devOptions.mjs";
7
7
  export * from "./fileReader.mjs";
8
8
  export * from "./mock.mjs";
9
- function startWatcher({ pwd, distDir, apiDir, sharedDir, watchOptions, onChange }) {
10
- const { mock } = AGGRED_DIR;
9
+ function startWatcher({ pwd, distDir, apiDir, sharedDir, mockDir, watchOptions, onChange }) {
11
10
  const defaultWatched = [
12
- `${mock}/**/*`,
11
+ path.join(resolveMockDirectory(pwd, mockDir), '**/*'),
13
12
  `${SERVER_DIR}/**/*`,
14
13
  `${apiDir}/**`,
15
14
  `${sharedDir}/**/*`,
@@ -5,6 +5,11 @@ import { compatibleRequire, fs } from "@modern-js/utils";
5
5
  import { match } from "path-to-regexp";
6
6
  let mockAPIs = [];
7
7
  let mockConfig;
8
+ const resolveMockDirectory = (pwd, mockDir = AGGRED_DIR.mock)=>node_path.resolve(pwd, mockDir);
9
+ const isPathInsideDirectory = (filepath, directory)=>{
10
+ const relativePath = node_path.relative(directory, node_path.resolve(filepath));
11
+ return '' === relativePath || '..' !== relativePath && !relativePath.startsWith(`..${node_path.sep}`) && !node_path.isAbsolute(relativePath);
12
+ };
8
13
  const parseKey = (key)=>{
9
14
  const _blank = ' ';
10
15
  const splitted = key.split(_blank).filter(Boolean);
@@ -20,14 +25,15 @@ const parseKey = (key)=>{
20
25
  path: key
21
26
  };
22
27
  };
23
- const getMockModule = async (pwd)=>{
28
+ const getMockModule = async (pwd, mockDir)=>{
24
29
  const exts = [
25
30
  '.ts',
26
31
  '.js'
27
32
  ];
28
33
  let mockFilePath = '';
34
+ const mockDirectory = resolveMockDirectory(pwd, mockDir);
29
35
  for (const ext of exts){
30
- const maybeMatch = node_path.join(pwd, `${AGGRED_DIR.mock}/index${ext}`);
36
+ const maybeMatch = node_path.join(mockDirectory, `index${ext}`);
31
37
  if (await fs.pathExists(maybeMatch)) {
32
38
  mockFilePath = maybeMatch;
33
39
  break;
@@ -55,8 +61,8 @@ const getMatched = (request, mockApis)=>{
55
61
  });
56
62
  return matched;
57
63
  };
58
- async function initOrUpdateMockMiddlewares(pwd) {
59
- const mockModule = await getMockModule(pwd);
64
+ async function initOrUpdateMockMiddlewares(pwd, mockDir) {
65
+ const mockModule = await getMockModule(pwd, mockDir);
60
66
  mockConfig = mockModule?.config;
61
67
  mockAPIs = Object.entries(mockModule?.mockHandlers || {}).map(([key, handler])=>{
62
68
  const { method, path } = parseKey(key);
@@ -67,8 +73,8 @@ async function initOrUpdateMockMiddlewares(pwd) {
67
73
  };
68
74
  });
69
75
  }
70
- async function getMockMiddleware(pwd) {
71
- await initOrUpdateMockMiddlewares(pwd);
76
+ async function getMockMiddleware(pwd, mockDir) {
77
+ await initOrUpdateMockMiddlewares(pwd, mockDir);
72
78
  const mockMiddleware = async (c, next)=>{
73
79
  if ('function' == typeof mockConfig?.enable) {
74
80
  const isEnabled = mockConfig.enable(c.env.node.req, c.env.node.res);
@@ -84,4 +90,4 @@ async function getMockMiddleware(pwd) {
84
90
  };
85
91
  return mockMiddleware;
86
92
  }
87
- export { getMatched, getMockMiddleware, initOrUpdateMockMiddlewares };
93
+ export { getMatched, getMockMiddleware, initOrUpdateMockMiddlewares, isPathInsideDirectory, resolveMockDirectory };
@@ -1,9 +1,7 @@
1
1
  import "node:module";
2
- import node_path from "node:path";
3
- import { AGGRED_DIR } from "@modern-js/server-core";
4
2
  import { connectMid2HonoMid } from "@modern-js/server-core/node";
5
3
  import { API_DIR, SHARED_DIR, logger } from "@modern-js/utils";
6
- import { getDevOptions, getMockMiddleware, initFileReader, onRepack, startWatcher } from "./helpers/index.mjs";
4
+ import { getDevOptions, getMockMiddleware, initFileReader, isPathInsideDirectory, onRepack, resolveMockDirectory, startWatcher } from "./helpers/index.mjs";
7
5
  const devRuntimeMiddlewarePlugin = (options, compiler)=>({
8
6
  name: '@modern-js/plugin-dev',
9
7
  setup (api) {
@@ -29,7 +27,7 @@ const devRuntimeMiddlewarePlugin = (options, compiler)=>({
29
27
  handler: connectMid2HonoMid(middleware)
30
28
  });
31
29
  });
32
- const mockMiddleware = await getMockMiddleware(pwd);
30
+ const mockMiddleware = await getMockMiddleware(pwd, options.config?.dev?.mockDir);
33
31
  middlewares.push({
34
32
  name: 'mock-dev',
35
33
  handler: mockMiddleware
@@ -67,16 +65,18 @@ function setupDevInfra({ config, pwd, distDir, apiDir, sharedDir, builder, build
67
65
  }
68
66
  });
69
67
  const { watchOptions } = config.server;
70
- const mockPath = node_path.normalize(node_path.join(pwd, AGGRED_DIR.mock));
68
+ const mockDir = config.dev?.mockDir;
69
+ const mockPath = resolveMockDirectory(pwd, mockDir);
71
70
  const watcher = startWatcher({
72
71
  pwd,
73
72
  distDir,
74
73
  apiDir: apiDir || API_DIR,
75
74
  sharedDir: sharedDir || SHARED_DIR,
75
+ mockDir,
76
76
  watchOptions,
77
77
  onChange: (filepath, event)=>{
78
78
  const runtimeServer = getRuntimeServer();
79
- if (runtimeServer && !filepath.startsWith(mockPath)) {
79
+ if (runtimeServer && !isPathInsideDirectory(filepath, mockPath)) {
80
80
  const fileChangeEvent = {
81
81
  type: 'file-change',
82
82
  payload: [
@@ -1,17 +1,16 @@
1
1
  import __rslib_shim_module__ from "node:module";
2
2
  const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
3
3
  import path from "path";
4
- import { AGGRED_DIR } from "@modern-js/server-core";
5
4
  import { SERVER_BUNDLE_DIRECTORY, SERVER_DIR } from "@modern-js/utils";
6
5
  import dev_tools_watcher, { mergeWatchOptions } from "../dev-tools/watcher/index.mjs";
6
+ import { resolveMockDirectory } from "./mock.mjs";
7
7
  export * from "./repack.mjs";
8
8
  export * from "./devOptions.mjs";
9
9
  export * from "./fileReader.mjs";
10
10
  export * from "./mock.mjs";
11
- function startWatcher({ pwd, distDir, apiDir, sharedDir, watchOptions, onChange }) {
12
- const { mock } = AGGRED_DIR;
11
+ function startWatcher({ pwd, distDir, apiDir, sharedDir, mockDir, watchOptions, onChange }) {
13
12
  const defaultWatched = [
14
- `${mock}/**/*`,
13
+ path.join(resolveMockDirectory(pwd, mockDir), '**/*'),
15
14
  `${SERVER_DIR}/**/*`,
16
15
  `${apiDir}/**`,
17
16
  `${sharedDir}/**/*`,
@@ -6,6 +6,11 @@ import { compatibleRequire, fs } from "@modern-js/utils";
6
6
  import { match } from "path-to-regexp";
7
7
  let mockAPIs = [];
8
8
  let mockConfig;
9
+ const resolveMockDirectory = (pwd, mockDir = AGGRED_DIR.mock)=>node_path.resolve(pwd, mockDir);
10
+ const isPathInsideDirectory = (filepath, directory)=>{
11
+ const relativePath = node_path.relative(directory, node_path.resolve(filepath));
12
+ return '' === relativePath || '..' !== relativePath && !relativePath.startsWith(`..${node_path.sep}`) && !node_path.isAbsolute(relativePath);
13
+ };
9
14
  const parseKey = (key)=>{
10
15
  const _blank = ' ';
11
16
  const splitted = key.split(_blank).filter(Boolean);
@@ -21,14 +26,15 @@ const parseKey = (key)=>{
21
26
  path: key
22
27
  };
23
28
  };
24
- const getMockModule = async (pwd)=>{
29
+ const getMockModule = async (pwd, mockDir)=>{
25
30
  const exts = [
26
31
  '.ts',
27
32
  '.js'
28
33
  ];
29
34
  let mockFilePath = '';
35
+ const mockDirectory = resolveMockDirectory(pwd, mockDir);
30
36
  for (const ext of exts){
31
- const maybeMatch = node_path.join(pwd, `${AGGRED_DIR.mock}/index${ext}`);
37
+ const maybeMatch = node_path.join(mockDirectory, `index${ext}`);
32
38
  if (await fs.pathExists(maybeMatch)) {
33
39
  mockFilePath = maybeMatch;
34
40
  break;
@@ -56,8 +62,8 @@ const getMatched = (request, mockApis)=>{
56
62
  });
57
63
  return matched;
58
64
  };
59
- async function initOrUpdateMockMiddlewares(pwd) {
60
- const mockModule = await getMockModule(pwd);
65
+ async function initOrUpdateMockMiddlewares(pwd, mockDir) {
66
+ const mockModule = await getMockModule(pwd, mockDir);
61
67
  mockConfig = mockModule?.config;
62
68
  mockAPIs = Object.entries(mockModule?.mockHandlers || {}).map(([key, handler])=>{
63
69
  const { method, path } = parseKey(key);
@@ -68,8 +74,8 @@ async function initOrUpdateMockMiddlewares(pwd) {
68
74
  };
69
75
  });
70
76
  }
71
- async function getMockMiddleware(pwd) {
72
- await initOrUpdateMockMiddlewares(pwd);
77
+ async function getMockMiddleware(pwd, mockDir) {
78
+ await initOrUpdateMockMiddlewares(pwd, mockDir);
73
79
  const mockMiddleware = async (c, next)=>{
74
80
  if ('function' == typeof mockConfig?.enable) {
75
81
  const isEnabled = mockConfig.enable(c.env.node.req, c.env.node.res);
@@ -85,4 +91,4 @@ async function getMockMiddleware(pwd) {
85
91
  };
86
92
  return mockMiddleware;
87
93
  }
88
- export { getMatched, getMockMiddleware, initOrUpdateMockMiddlewares };
94
+ export { getMatched, getMockMiddleware, initOrUpdateMockMiddlewares, isPathInsideDirectory, resolveMockDirectory };
@@ -4,11 +4,12 @@ export * from './repack';
4
4
  export * from './devOptions';
5
5
  export * from './fileReader';
6
6
  export * from './mock';
7
- export declare function startWatcher({ pwd, distDir, apiDir, sharedDir, watchOptions, onChange, }: {
7
+ export declare function startWatcher({ pwd, distDir, apiDir, sharedDir, mockDir, watchOptions, onChange, }: {
8
8
  pwd: string;
9
9
  distDir: string;
10
10
  apiDir: string;
11
11
  sharedDir: string;
12
+ mockDir?: string;
12
13
  watchOptions?: WatchOptions;
13
14
  /**
14
15
  * Called after the require cache for a changed user server file has been
@@ -10,7 +10,9 @@ type MockAPI = {
10
10
  path: string;
11
11
  handler: MockHandler;
12
12
  };
13
+ export declare const resolveMockDirectory: (pwd: string, mockDir?: string) => string;
14
+ export declare const isPathInsideDirectory: (filepath: string, directory: string) => boolean;
13
15
  export declare const getMatched: (request: InternalRequest, mockApis: MockAPI[]) => MockAPI | undefined;
14
- export declare function initOrUpdateMockMiddlewares(pwd: string): Promise<void>;
15
- export declare function getMockMiddleware(pwd: string): Promise<Middleware>;
16
+ export declare function initOrUpdateMockMiddlewares(pwd: string, mockDir?: string): Promise<void>;
17
+ export declare function getMockMiddleware(pwd: string, mockDir?: string): Promise<Middleware>;
16
18
  export {};
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "3.7.0",
18
+ "version": "3.8.1",
19
19
  "types": "./dist/types/index.d.ts",
20
20
  "main": "./dist/cjs/index.js",
21
21
  "exports": {
@@ -44,11 +44,11 @@
44
44
  "minimatch": "^3.1.2",
45
45
  "path-to-regexp": "^6.3.0",
46
46
  "ws": "^8.21.0",
47
- "@modern-js/runtime-utils": "3.7.0",
48
- "@modern-js/server-core": "3.7.0",
49
- "@modern-js/server-utils": "3.7.0",
50
- "@modern-js/types": "3.7.0",
51
- "@modern-js/utils": "3.7.0"
47
+ "@modern-js/runtime-utils": "3.8.1",
48
+ "@modern-js/server-core": "3.8.1",
49
+ "@modern-js/server-utils": "3.8.1",
50
+ "@modern-js/utils": "3.8.1",
51
+ "@modern-js/types": "3.8.1"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@rslib/core": "0.23.2",
@@ -61,7 +61,7 @@
61
61
  "tsconfig-paths": "4.2.0",
62
62
  "typescript": "^5",
63
63
  "websocket": "^1.0.35",
64
- "@modern-js/builder": "3.7.0",
64
+ "@modern-js/builder": "3.8.1",
65
65
  "@modern-js/rslib": "2.68.10",
66
66
  "@scripts/rstest-config": "2.66.0"
67
67
  },