@midwayjs/mock 4.0.0-beta.11 → 4.0.0-beta.12
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/creator.js +0 -1
- package/dist/vite.d.ts +33 -0
- package/dist/vite.js +180 -0
- package/package.json +15 -3
package/dist/creator.js
CHANGED
|
@@ -479,7 +479,6 @@ class BootstrapAppStarter {
|
|
|
479
479
|
return applicationManager.getApplication(type);
|
|
480
480
|
}
|
|
481
481
|
async close(options = {}) {
|
|
482
|
-
// eslint-disable-next-line node/no-extraneous-require
|
|
483
482
|
const BootstrapModule = await (0, core_1.loadModule)('@midwayjs/bootstrap', {
|
|
484
483
|
loadMode: this.options.moduleLoadType,
|
|
485
484
|
safeLoad: true,
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
interface RouteManifestLike {
|
|
2
|
+
source: string;
|
|
3
|
+
operationId: string;
|
|
4
|
+
controllerId?: string;
|
|
5
|
+
controllerPrefix: string;
|
|
6
|
+
method: string;
|
|
7
|
+
path: string;
|
|
8
|
+
fullPath: string;
|
|
9
|
+
routerName?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface DevPluginOptions {
|
|
12
|
+
appDir: string;
|
|
13
|
+
baseDir?: string;
|
|
14
|
+
basePath?: string;
|
|
15
|
+
watch?: {
|
|
16
|
+
include?: RegExp[];
|
|
17
|
+
exclude?: RegExp[];
|
|
18
|
+
};
|
|
19
|
+
getRequestHandler?: (app: any) => (req: any, res: any, next?: () => void) => any;
|
|
20
|
+
routeManifest?: boolean | {
|
|
21
|
+
virtualId?: string;
|
|
22
|
+
filter?: (route: RouteManifestLike) => boolean;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export declare function devPlugin(options: DevPluginOptions): {
|
|
26
|
+
name: string;
|
|
27
|
+
apply: string;
|
|
28
|
+
resolveId(source: string): string;
|
|
29
|
+
load(id: string): Promise<string>;
|
|
30
|
+
configureServer(server: any): Promise<void>;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=vite.d.ts.map
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
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
|
+
const core_1 = require("@midwayjs/core");
|
|
7
|
+
const DEFAULT_ROUTE_MANIFEST_VIRTUAL_ID = 'virtual:midway-route-manifest';
|
|
8
|
+
function startsWithBasePath(url, basePath) {
|
|
9
|
+
if (basePath === '/') {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
return url === basePath || url.startsWith(`${basePath}/`);
|
|
13
|
+
}
|
|
14
|
+
function getDefaultRequestHandler(app) {
|
|
15
|
+
if (app && typeof app.callback === 'function') {
|
|
16
|
+
return app.callback();
|
|
17
|
+
}
|
|
18
|
+
if (typeof app === 'function') {
|
|
19
|
+
return app;
|
|
20
|
+
}
|
|
21
|
+
if (app && typeof app.getFramework === 'function') {
|
|
22
|
+
const framework = app.getFramework();
|
|
23
|
+
if (framework) {
|
|
24
|
+
const frameworkApp = typeof framework.getApplication === 'function'
|
|
25
|
+
? framework.getApplication()
|
|
26
|
+
: undefined;
|
|
27
|
+
if (frameworkApp && typeof frameworkApp.callback === 'function') {
|
|
28
|
+
return frameworkApp.callback();
|
|
29
|
+
}
|
|
30
|
+
if (typeof framework.callback2 === 'function') {
|
|
31
|
+
return framework.callback2();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
throw new Error('[midway:mock] can not resolve request handler, please provide getRequestHandler in devPlugin options');
|
|
36
|
+
}
|
|
37
|
+
function devPlugin(options) {
|
|
38
|
+
if (!options?.appDir) {
|
|
39
|
+
throw new Error('[midway:mock] devPlugin requires "appDir", e.g. devPlugin({ appDir: process.cwd() })');
|
|
40
|
+
}
|
|
41
|
+
const appDir = options.appDir;
|
|
42
|
+
const baseDir = options.baseDir
|
|
43
|
+
? (0, node_path_1.resolve)(appDir, options.baseDir)
|
|
44
|
+
: (0, node_path_1.resolve)(appDir, 'src');
|
|
45
|
+
const resolvedBaseDir = baseDir;
|
|
46
|
+
const basePath = options.basePath || '/api';
|
|
47
|
+
const watchInclude = options.watch?.include || [/\.(ts|tsx|js|mjs|cjs)$/];
|
|
48
|
+
const watchExclude = options.watch?.exclude || [/\.d\.ts$/];
|
|
49
|
+
const getRequestHandler = options.getRequestHandler || getDefaultRequestHandler;
|
|
50
|
+
const routeManifestOptions = options.routeManifest && typeof options.routeManifest === 'object'
|
|
51
|
+
? options.routeManifest
|
|
52
|
+
: undefined;
|
|
53
|
+
const routeManifestEnabled = options.routeManifest !== false;
|
|
54
|
+
const routeManifestVirtualId = routeManifestOptions?.virtualId || DEFAULT_ROUTE_MANIFEST_VIRTUAL_ID;
|
|
55
|
+
const routeManifestResolvedId = `\0${routeManifestVirtualId}`;
|
|
56
|
+
const routeManifestFilter = routeManifestOptions?.filter;
|
|
57
|
+
let appPromise = null;
|
|
58
|
+
let routeManifestPromise = null;
|
|
59
|
+
let reloadingAppPromise = null;
|
|
60
|
+
let viteServer;
|
|
61
|
+
const invalidateRouteManifestModule = () => {
|
|
62
|
+
if (!routeManifestEnabled || !viteServer) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const mod = viteServer.moduleGraph?.getModuleById(routeManifestResolvedId);
|
|
66
|
+
if (mod) {
|
|
67
|
+
viteServer.moduleGraph.invalidateModule(mod);
|
|
68
|
+
viteServer.ws?.send({
|
|
69
|
+
type: 'full-reload',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const shouldReloadFile = (file) => {
|
|
74
|
+
if (!file.startsWith(resolvedBaseDir)) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
if (watchExclude.some(reg => reg.test(file))) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return watchInclude.some(reg => reg.test(file));
|
|
81
|
+
};
|
|
82
|
+
const reloadApp = async () => {
|
|
83
|
+
if (reloadingAppPromise) {
|
|
84
|
+
return reloadingAppPromise;
|
|
85
|
+
}
|
|
86
|
+
reloadingAppPromise = (async () => {
|
|
87
|
+
const oldAppPromise = appPromise;
|
|
88
|
+
appPromise = null;
|
|
89
|
+
routeManifestPromise = null;
|
|
90
|
+
if (oldAppPromise) {
|
|
91
|
+
const oldApp = await oldAppPromise;
|
|
92
|
+
await (0, creator_1.close)(oldApp);
|
|
93
|
+
}
|
|
94
|
+
invalidateRouteManifestModule();
|
|
95
|
+
})().finally(() => {
|
|
96
|
+
reloadingAppPromise = null;
|
|
97
|
+
});
|
|
98
|
+
return reloadingAppPromise;
|
|
99
|
+
};
|
|
100
|
+
const ensureApp = async () => {
|
|
101
|
+
if (!appPromise) {
|
|
102
|
+
appPromise = (0, creator_1.createApp)({
|
|
103
|
+
appDir,
|
|
104
|
+
baseDir,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return appPromise;
|
|
108
|
+
};
|
|
109
|
+
const ensureRouteManifest = async () => {
|
|
110
|
+
if (!routeManifestEnabled) {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
if (!routeManifestPromise) {
|
|
114
|
+
routeManifestPromise = (async () => {
|
|
115
|
+
const app = await ensureApp();
|
|
116
|
+
const appContext = app.getApplicationContext?.();
|
|
117
|
+
if (!appContext) {
|
|
118
|
+
throw new Error('[midway:mock] can not resolve applicationContext for route manifest generation');
|
|
119
|
+
}
|
|
120
|
+
const webRouterService = await appContext.getAsync(core_1.MidwayWebRouterService);
|
|
121
|
+
let manifest = await webRouterService.getRouteManifest();
|
|
122
|
+
if (typeof routeManifestFilter === 'function') {
|
|
123
|
+
manifest = manifest.filter(routeManifestFilter);
|
|
124
|
+
}
|
|
125
|
+
return manifest;
|
|
126
|
+
})();
|
|
127
|
+
}
|
|
128
|
+
return routeManifestPromise;
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
name: 'midway-dev-runtime',
|
|
132
|
+
apply: 'serve',
|
|
133
|
+
resolveId(source) {
|
|
134
|
+
if (!routeManifestEnabled) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
if (source === routeManifestVirtualId) {
|
|
138
|
+
return routeManifestResolvedId;
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
},
|
|
142
|
+
async load(id) {
|
|
143
|
+
if (!routeManifestEnabled || id !== routeManifestResolvedId) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
const manifest = await ensureRouteManifest();
|
|
147
|
+
return `export default ${JSON.stringify(manifest)};`;
|
|
148
|
+
},
|
|
149
|
+
async configureServer(server) {
|
|
150
|
+
viteServer = server;
|
|
151
|
+
server.httpServer?.once('close', async () => {
|
|
152
|
+
await reloadApp();
|
|
153
|
+
});
|
|
154
|
+
server.watcher.on('all', async (eventName, file) => {
|
|
155
|
+
if (eventName !== 'add' &&
|
|
156
|
+
eventName !== 'change' &&
|
|
157
|
+
eventName !== 'unlink') {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (!shouldReloadFile(file)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
server.config.logger.info(`[midway:mock] detected server ${eventName}, reload midway app: ${file}`);
|
|
164
|
+
await reloadApp();
|
|
165
|
+
});
|
|
166
|
+
server.middlewares.use(async (req, res, next) => {
|
|
167
|
+
const originalUrl = req.url || '';
|
|
168
|
+
const pathname = originalUrl.split('?')[0];
|
|
169
|
+
if (!startsWithBasePath(pathname, basePath)) {
|
|
170
|
+
next();
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const app = await ensureApp();
|
|
174
|
+
const handler = getRequestHandler(app);
|
|
175
|
+
return handler(req, res, next);
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=vite.js.map
|
package/package.json
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/mock",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.12",
|
|
4
4
|
"description": "create your test app from midway framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./vite": {
|
|
14
|
+
"types": "./dist/vite.d.ts",
|
|
15
|
+
"require": "./dist/vite.js",
|
|
16
|
+
"import": "./dist/vite.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
7
19
|
"scripts": {
|
|
8
20
|
"build": "tsc",
|
|
9
21
|
"test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
|
|
@@ -29,7 +41,7 @@
|
|
|
29
41
|
},
|
|
30
42
|
"license": "MIT",
|
|
31
43
|
"devDependencies": {
|
|
32
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
44
|
+
"@midwayjs/core": "^4.0.0-beta.12",
|
|
33
45
|
"@midwayjs/logger": "^4.0.0",
|
|
34
46
|
"@types/amqplib": "0.10.8",
|
|
35
47
|
"amqplib": "0.10.9",
|
|
@@ -50,5 +62,5 @@
|
|
|
50
62
|
"type": "git",
|
|
51
63
|
"url": "https://github.com/midwayjs/midway.git"
|
|
52
64
|
},
|
|
53
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "1c48179b7c827ba8ec9351e9b6c36ec1726d3e11"
|
|
54
66
|
}
|