@midwayjs/mock 4.0.0-beta.12 → 4.0.0-beta.13

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.
@@ -0,0 +1,15 @@
1
+ export interface DevPluginOptions {
2
+ appDir: string;
3
+ baseDir?: string;
4
+ basePath?: string;
5
+ watch?: {
6
+ include?: RegExp[];
7
+ exclude?: RegExp[];
8
+ };
9
+ getRequestHandler?: (app: any) => (req: any, res: any, next?: (err?: unknown) => void) => any;
10
+ }
11
+ export declare function devPlugin(options: DevPluginOptions): {
12
+ name: string;
13
+ apply(compiler: any): void;
14
+ };
15
+ //# sourceMappingURL=rspack.d.ts.map
package/dist/rspack.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.devPlugin = devPlugin;
4
+ const creator_1 = require("./creator");
5
+ const node_path_1 = require("node:path");
6
+ function startsWithBasePath(url, basePath) {
7
+ if (basePath === '/') {
8
+ return true;
9
+ }
10
+ return url === basePath || url.startsWith(`${basePath}/`);
11
+ }
12
+ function getDefaultRequestHandler(app) {
13
+ if (app && typeof app.callback === 'function') {
14
+ return app.callback();
15
+ }
16
+ if (typeof app === 'function') {
17
+ return app;
18
+ }
19
+ if (app && typeof app.getFramework === 'function') {
20
+ const framework = app.getFramework();
21
+ if (framework) {
22
+ const frameworkApp = typeof framework.getApplication === 'function'
23
+ ? framework.getApplication()
24
+ : undefined;
25
+ if (frameworkApp && typeof frameworkApp.callback === 'function') {
26
+ return frameworkApp.callback();
27
+ }
28
+ if (typeof framework.callback2 === 'function') {
29
+ return framework.callback2();
30
+ }
31
+ }
32
+ }
33
+ throw new Error('[midway:mock] can not resolve request handler, please provide getRequestHandler in devPlugin options');
34
+ }
35
+ function devPlugin(options) {
36
+ if (!options?.appDir) {
37
+ throw new Error('[midway:mock] devPlugin requires "appDir", e.g. devPlugin({ appDir: process.cwd() })');
38
+ }
39
+ const appDir = options.appDir;
40
+ const baseDir = options.baseDir
41
+ ? (0, node_path_1.resolve)(appDir, options.baseDir)
42
+ : (0, node_path_1.resolve)(appDir, 'src');
43
+ const resolvedBaseDir = baseDir;
44
+ const basePath = options.basePath || '/api';
45
+ const watchInclude = options.watch?.include || [/\.(ts|tsx|js|mjs|cjs)$/];
46
+ const watchExclude = options.watch?.exclude || [/\.d\.ts$/];
47
+ const getRequestHandler = options.getRequestHandler || getDefaultRequestHandler;
48
+ const hmrImportQueryEnvKey = 'MIDWAY_HMR_IMPORT_QUERY';
49
+ let appPromise = null;
50
+ let reloadingAppPromise = null;
51
+ const shouldReloadFile = (file) => {
52
+ if (!file.startsWith(resolvedBaseDir)) {
53
+ return false;
54
+ }
55
+ if (watchExclude.some(reg => reg.test(file))) {
56
+ return false;
57
+ }
58
+ return watchInclude.some(reg => reg.test(file));
59
+ };
60
+ const reloadApp = async () => {
61
+ if (reloadingAppPromise) {
62
+ return reloadingAppPromise;
63
+ }
64
+ reloadingAppPromise = (async () => {
65
+ const oldAppPromise = appPromise;
66
+ appPromise = null;
67
+ if (oldAppPromise) {
68
+ const oldApp = await oldAppPromise;
69
+ await (0, creator_1.close)(oldApp);
70
+ }
71
+ })().finally(() => {
72
+ reloadingAppPromise = null;
73
+ });
74
+ return reloadingAppPromise;
75
+ };
76
+ const ensureApp = async () => {
77
+ if (!appPromise) {
78
+ const previousQuery = process.env[hmrImportQueryEnvKey];
79
+ process.env[hmrImportQueryEnvKey] = '1';
80
+ appPromise = (0, creator_1.createApp)({
81
+ appDir,
82
+ baseDir,
83
+ }).finally(() => {
84
+ if (previousQuery === undefined) {
85
+ delete process.env[hmrImportQueryEnvKey];
86
+ }
87
+ else {
88
+ process.env[hmrImportQueryEnvKey] = previousQuery;
89
+ }
90
+ });
91
+ }
92
+ return appPromise;
93
+ };
94
+ return {
95
+ name: 'midway-dev-runtime-rspack',
96
+ apply(compiler) {
97
+ const logger = typeof compiler.getInfrastructureLogger === 'function'
98
+ ? compiler.getInfrastructureLogger('midway:mock')
99
+ : console;
100
+ compiler.hooks.invalid?.tap('midway-dev-runtime-rspack', (fileName) => {
101
+ if (!fileName || !shouldReloadFile(fileName)) {
102
+ return;
103
+ }
104
+ logger.info(`[midway:mock] detected server change, reload midway app: ${fileName}`);
105
+ void reloadApp().catch((err) => {
106
+ logger.error(`[midway:mock] failed to reload midway app: ${err?.message || err}`);
107
+ });
108
+ });
109
+ compiler.hooks.shutdown?.tapPromise('midway-dev-runtime-rspack', async () => {
110
+ await reloadApp();
111
+ });
112
+ compiler.options.devServer = compiler.options.devServer || {};
113
+ const previousSetupMiddlewares = compiler.options.devServer.setupMiddlewares;
114
+ compiler.options.devServer.setupMiddlewares = (middlewares, devServer) => {
115
+ const requestMiddleware = async (req, res, next) => {
116
+ const originalUrl = req.url || '';
117
+ const pathname = originalUrl.split('?')[0];
118
+ if (!startsWithBasePath(pathname, basePath)) {
119
+ next();
120
+ return;
121
+ }
122
+ try {
123
+ const app = await ensureApp();
124
+ const handler = getRequestHandler(app);
125
+ return handler(req, res, next);
126
+ }
127
+ catch (err) {
128
+ next(err);
129
+ return;
130
+ }
131
+ };
132
+ if (Array.isArray(middlewares)) {
133
+ middlewares.unshift(requestMiddleware);
134
+ }
135
+ if (typeof previousSetupMiddlewares === 'function') {
136
+ return previousSetupMiddlewares(middlewares, devServer);
137
+ }
138
+ return middlewares;
139
+ };
140
+ },
141
+ };
142
+ }
143
+ //# sourceMappingURL=rspack.js.map
package/dist/vite.js CHANGED
@@ -55,6 +55,7 @@ function devPlugin(options) {
55
55
  const routeManifestResolvedId = `\0${routeManifestVirtualId}`;
56
56
  const routeManifestFilter = routeManifestOptions?.filter;
57
57
  let appPromise = null;
58
+ const hmrImportQueryEnvKey = 'MIDWAY_HMR_IMPORT_QUERY';
58
59
  let routeManifestPromise = null;
59
60
  let reloadingAppPromise = null;
60
61
  let viteServer;
@@ -99,9 +100,18 @@ function devPlugin(options) {
99
100
  };
100
101
  const ensureApp = async () => {
101
102
  if (!appPromise) {
103
+ const previousQuery = process.env[hmrImportQueryEnvKey];
104
+ process.env[hmrImportQueryEnvKey] = '1';
102
105
  appPromise = (0, creator_1.createApp)({
103
106
  appDir,
104
107
  baseDir,
108
+ }).finally(() => {
109
+ if (previousQuery === undefined) {
110
+ delete process.env[hmrImportQueryEnvKey];
111
+ }
112
+ else {
113
+ process.env[hmrImportQueryEnvKey] = previousQuery;
114
+ }
105
115
  });
106
116
  }
107
117
  return appPromise;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/mock",
3
- "version": "4.0.0-beta.12",
3
+ "version": "4.0.0-beta.13",
4
4
  "description": "create your test app from midway framework",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -14,6 +14,11 @@
14
14
  "types": "./dist/vite.d.ts",
15
15
  "require": "./dist/vite.js",
16
16
  "import": "./dist/vite.js"
17
+ },
18
+ "./rspack": {
19
+ "types": "./dist/rspack.d.ts",
20
+ "require": "./dist/rspack.js",
21
+ "import": "./dist/rspack.js"
17
22
  }
18
23
  },
19
24
  "scripts": {
@@ -41,7 +46,7 @@
41
46
  },
42
47
  "license": "MIT",
43
48
  "devDependencies": {
44
- "@midwayjs/core": "^4.0.0-beta.12",
49
+ "@midwayjs/core": "^4.0.0-beta.13",
45
50
  "@midwayjs/logger": "^4.0.0",
46
51
  "@types/amqplib": "0.10.8",
47
52
  "amqplib": "0.10.9",
@@ -62,5 +67,5 @@
62
67
  "type": "git",
63
68
  "url": "https://github.com/midwayjs/midway.git"
64
69
  },
65
- "gitHead": "1c48179b7c827ba8ec9351e9b6c36ec1726d3e11"
70
+ "gitHead": "9a38b66a84a6880370cac90d737f94f9c5f2f256"
66
71
  }