rspack-plugin-mock 0.4.0 → 0.4.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/{chunk-VKD4VKFQ.js → chunk-4XOUX6CL.js} +118 -117
- package/dist/{chunk-2SSCBSV5.js → chunk-JMXFIX5Q.js} +260 -258
- package/dist/{chunk-T2VK464W.cjs → chunk-TKSRTJX5.cjs} +240 -238
- package/dist/{chunk-ZUCMHLWW.cjs → chunk-YAHWW6TX.cjs} +118 -117
- package/dist/helper.d.cts +6 -6
- package/dist/helper.d.ts +6 -6
- package/dist/index.cjs +25 -25
- package/dist/index.d.cts +9 -9
- package/dist/index.d.ts +9 -9
- package/dist/index.js +14 -14
- package/dist/{mockWebsocket-CPuTAvL0.d.ts → mockWebsocket-DBgZBsdo.d.ts} +15 -15
- package/dist/{mockWebsocket-Dc9CZBfv.d.cts → mockWebsocket-Ki_cShTv.d.cts} +15 -15
- package/dist/rsbuild.cjs +11 -11
- package/dist/rsbuild.d.cts +5 -5
- package/dist/rsbuild.d.ts +5 -5
- package/dist/rsbuild.js +3 -3
- package/dist/server.cjs +2 -2
- package/dist/server.d.cts +8 -8
- package/dist/server.d.ts +8 -8
- package/dist/server.js +1 -1
- package/dist/{types-BgpcN3jm.d.ts → types-Aw0AciTG.d.cts} +5 -5
- package/dist/{types-BgpcN3jm.d.cts → types-Aw0AciTG.d.ts} +5 -5
- package/package.json +15 -15
|
@@ -9,121 +9,23 @@ import {
|
|
|
9
9
|
transformRawData,
|
|
10
10
|
urlParse,
|
|
11
11
|
vfs
|
|
12
|
-
} from "./chunk-
|
|
13
|
-
|
|
14
|
-
// src/core/mockMiddleware.ts
|
|
15
|
-
import cors from "cors";
|
|
16
|
-
import { pathToRegexp } from "path-to-regexp";
|
|
17
|
-
function createMockMiddleware(compiler, options) {
|
|
18
|
-
function mockMiddleware(middlewares, reload) {
|
|
19
|
-
middlewares.unshift(baseMiddleware(compiler, options));
|
|
20
|
-
const corsMiddleware = createCorsMiddleware(compiler, options);
|
|
21
|
-
if (corsMiddleware) {
|
|
22
|
-
middlewares.unshift(corsMiddleware);
|
|
23
|
-
}
|
|
24
|
-
if (options.reload) {
|
|
25
|
-
compiler.on("update", () => reload?.());
|
|
26
|
-
}
|
|
27
|
-
return middlewares;
|
|
28
|
-
}
|
|
29
|
-
return mockMiddleware;
|
|
30
|
-
}
|
|
31
|
-
function createCorsMiddleware(compiler, options) {
|
|
32
|
-
let corsOptions = {};
|
|
33
|
-
const enabled = options.cors !== false;
|
|
34
|
-
if (enabled) {
|
|
35
|
-
corsOptions = {
|
|
36
|
-
...corsOptions,
|
|
37
|
-
...typeof options.cors === "boolean" ? {} : options.cors
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
const proxies = options.proxies;
|
|
41
|
-
return !enabled ? void 0 : function(req, res, next) {
|
|
42
|
-
const { pathname } = urlParse(req.url);
|
|
43
|
-
if (!pathname || proxies.length === 0 || !proxies.some(
|
|
44
|
-
(context) => doesProxyContextMatchUrl(context, req.url, req)
|
|
45
|
-
)) {
|
|
46
|
-
return next();
|
|
47
|
-
}
|
|
48
|
-
const mockData = compiler.mockData;
|
|
49
|
-
const mockUrl = Object.keys(mockData).find(
|
|
50
|
-
(key) => pathToRegexp(key).test(pathname)
|
|
51
|
-
);
|
|
52
|
-
if (!mockUrl)
|
|
53
|
-
return next();
|
|
54
|
-
cors(corsOptions)(req, res, next);
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/core/resolvePluginOptions.ts
|
|
59
|
-
import process from "process";
|
|
60
|
-
import { isBoolean, toArray } from "@pengzhanbo/utils";
|
|
61
|
-
function resolvePluginOptions({
|
|
62
|
-
prefix = [],
|
|
63
|
-
wsPrefix = [],
|
|
64
|
-
cwd,
|
|
65
|
-
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
66
|
-
exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
|
|
67
|
-
reload = false,
|
|
68
|
-
log = "info",
|
|
69
|
-
cors: cors2 = true,
|
|
70
|
-
formidableOptions = {},
|
|
71
|
-
build = false,
|
|
72
|
-
cookiesOptions = {},
|
|
73
|
-
bodyParserOptions = {},
|
|
74
|
-
priority = {}
|
|
75
|
-
} = {}, { alias, context, plugins, proxies }) {
|
|
76
|
-
const logger = createLogger(
|
|
77
|
-
"rspack:mock",
|
|
78
|
-
isBoolean(log) ? log ? "info" : "error" : log
|
|
79
|
-
);
|
|
80
|
-
return {
|
|
81
|
-
prefix,
|
|
82
|
-
wsPrefix,
|
|
83
|
-
cwd: cwd || context || process.cwd(),
|
|
84
|
-
include,
|
|
85
|
-
exclude,
|
|
86
|
-
reload,
|
|
87
|
-
cors: cors2,
|
|
88
|
-
cookiesOptions,
|
|
89
|
-
log,
|
|
90
|
-
formidableOptions: {
|
|
91
|
-
multiples: true,
|
|
92
|
-
...formidableOptions
|
|
93
|
-
},
|
|
94
|
-
bodyParserOptions,
|
|
95
|
-
priority,
|
|
96
|
-
build: build ? Object.assign(
|
|
97
|
-
{
|
|
98
|
-
serverPort: 8080,
|
|
99
|
-
dist: "mockServer",
|
|
100
|
-
log: "error"
|
|
101
|
-
},
|
|
102
|
-
typeof build === "object" ? build : {}
|
|
103
|
-
) : false,
|
|
104
|
-
alias,
|
|
105
|
-
plugins,
|
|
106
|
-
proxies,
|
|
107
|
-
wsProxies: toArray(wsPrefix),
|
|
108
|
-
logger
|
|
109
|
-
};
|
|
110
|
-
}
|
|
12
|
+
} from "./chunk-JMXFIX5Q.js";
|
|
111
13
|
|
|
112
14
|
// src/core/build.ts
|
|
113
15
|
import fs from "fs";
|
|
114
16
|
import fsp from "fs/promises";
|
|
115
17
|
import path from "path";
|
|
116
|
-
import
|
|
117
|
-
import
|
|
18
|
+
import process from "process";
|
|
19
|
+
import { toArray } from "@pengzhanbo/utils";
|
|
118
20
|
import { createFilter } from "@rollup/pluginutils";
|
|
21
|
+
import fg from "fast-glob";
|
|
119
22
|
import color2 from "picocolors";
|
|
120
|
-
import { toArray as toArray2 } from "@pengzhanbo/utils";
|
|
121
23
|
|
|
122
24
|
// src/core/createRspackCompiler.ts
|
|
123
25
|
import { createRequire } from "module";
|
|
124
26
|
import * as rspackCore from "@rspack/core";
|
|
125
|
-
import color from "picocolors";
|
|
126
27
|
import isCore from "is-core-module";
|
|
28
|
+
import color from "picocolors";
|
|
127
29
|
var require2 = createRequire(import.meta.url);
|
|
128
30
|
function createCompiler(options, callback) {
|
|
129
31
|
const rspackOptions = resolveRspackOptions(options);
|
|
@@ -259,7 +161,7 @@ function resolveRspackOptions({
|
|
|
259
161
|
|
|
260
162
|
// src/core/build.ts
|
|
261
163
|
async function buildMockServer(options, outputDir) {
|
|
262
|
-
const entryFile = path.resolve(
|
|
164
|
+
const entryFile = path.resolve(process.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
|
|
263
165
|
const mockFileList = await getMockFileList(options);
|
|
264
166
|
await writeMockEntryFile(entryFile, mockFileList, options.cwd);
|
|
265
167
|
const { code, externals } = await transformWithRspack({
|
|
@@ -276,7 +178,7 @@ async function buildMockServer(options, outputDir) {
|
|
|
276
178
|
];
|
|
277
179
|
const dist = path.resolve(outputDir, options.build.dist);
|
|
278
180
|
options.logger.info(
|
|
279
|
-
`${color2.green("\u2713")} generate mock server in ${color2.cyan(path.relative(
|
|
181
|
+
`${color2.green("\u2713")} generate mock server in ${color2.cyan(path.relative(process.cwd(), dist))}`
|
|
280
182
|
);
|
|
281
183
|
if (!fs.existsSync(dist)) {
|
|
282
184
|
await fsp.mkdir(dist, { recursive: true });
|
|
@@ -334,7 +236,7 @@ const app = connect();
|
|
|
334
236
|
const server = createServer(app);
|
|
335
237
|
const logger = createLogger('mock-server', '${log}');
|
|
336
238
|
const proxies = ${JSON.stringify(proxies)};
|
|
337
|
-
const wsProxies = ${JSON.stringify(
|
|
239
|
+
const wsProxies = ${JSON.stringify(toArray(wsPrefix))};
|
|
338
240
|
const cookiesOptions = ${JSON.stringify(cookiesOptions)};
|
|
339
241
|
const bodyParserOptions = ${JSON.stringify(bodyParserOptions)};
|
|
340
242
|
const priority = ${JSON.stringify(priority)};
|
|
@@ -410,16 +312,16 @@ function getHostDependencies(context) {
|
|
|
410
312
|
|
|
411
313
|
// src/core/mockCompiler.ts
|
|
412
314
|
import EventEmitter from "events";
|
|
413
|
-
import process3 from "process";
|
|
414
315
|
import path3 from "path";
|
|
415
|
-
import
|
|
416
|
-
import
|
|
316
|
+
import process2 from "process";
|
|
317
|
+
import { toArray as toArray2 } from "@pengzhanbo/utils";
|
|
417
318
|
import { createFilter as createFilter2 } from "@rollup/pluginutils";
|
|
418
|
-
import
|
|
319
|
+
import chokidar from "chokidar";
|
|
320
|
+
import fastGlob from "fast-glob";
|
|
419
321
|
|
|
420
322
|
// src/core/loadFromCode.ts
|
|
421
|
-
import path2 from "path";
|
|
422
323
|
import fs2, { promises as fsp2 } from "fs";
|
|
324
|
+
import path2 from "path";
|
|
423
325
|
import { pathToFileURL } from "url";
|
|
424
326
|
async function loadFromCode({
|
|
425
327
|
filepath,
|
|
@@ -451,7 +353,7 @@ var MockCompiler = class extends EventEmitter {
|
|
|
451
353
|
constructor(options) {
|
|
452
354
|
super();
|
|
453
355
|
this.options = options;
|
|
454
|
-
this.cwd = options.cwd ||
|
|
356
|
+
this.cwd = options.cwd || process2.cwd();
|
|
455
357
|
const { include, exclude } = this.options;
|
|
456
358
|
this.fileFilter = createFilter2(include, exclude, { resolve: false });
|
|
457
359
|
try {
|
|
@@ -459,7 +361,7 @@ var MockCompiler = class extends EventEmitter {
|
|
|
459
361
|
this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
|
|
460
362
|
} catch {
|
|
461
363
|
}
|
|
462
|
-
this.entryFile = path3.resolve(
|
|
364
|
+
this.entryFile = path3.resolve(process2.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
|
|
463
365
|
}
|
|
464
366
|
cwd;
|
|
465
367
|
mockWatcher;
|
|
@@ -522,7 +424,7 @@ var MockCompiler = class extends EventEmitter {
|
|
|
522
424
|
}
|
|
523
425
|
watchMockFiles() {
|
|
524
426
|
const { include } = this.options;
|
|
525
|
-
const [firstGlob, ...otherGlob] =
|
|
427
|
+
const [firstGlob, ...otherGlob] = toArray2(include);
|
|
526
428
|
const watcher = this.mockWatcher = chokidar.watch(firstGlob, {
|
|
527
429
|
ignoreInitial: true,
|
|
528
430
|
cwd: this.cwd
|
|
@@ -547,10 +449,109 @@ var MockCompiler = class extends EventEmitter {
|
|
|
547
449
|
}
|
|
548
450
|
};
|
|
549
451
|
|
|
452
|
+
// src/core/mockMiddleware.ts
|
|
453
|
+
import cors from "cors";
|
|
454
|
+
import { pathToRegexp } from "path-to-regexp";
|
|
455
|
+
function createMockMiddleware(compiler, options) {
|
|
456
|
+
function mockMiddleware(middlewares, reload) {
|
|
457
|
+
middlewares.unshift(baseMiddleware(compiler, options));
|
|
458
|
+
const corsMiddleware = createCorsMiddleware(compiler, options);
|
|
459
|
+
if (corsMiddleware) {
|
|
460
|
+
middlewares.unshift(corsMiddleware);
|
|
461
|
+
}
|
|
462
|
+
if (options.reload) {
|
|
463
|
+
compiler.on("update", () => reload?.());
|
|
464
|
+
}
|
|
465
|
+
return middlewares;
|
|
466
|
+
}
|
|
467
|
+
return mockMiddleware;
|
|
468
|
+
}
|
|
469
|
+
function createCorsMiddleware(compiler, options) {
|
|
470
|
+
let corsOptions = {};
|
|
471
|
+
const enabled = options.cors !== false;
|
|
472
|
+
if (enabled) {
|
|
473
|
+
corsOptions = {
|
|
474
|
+
...corsOptions,
|
|
475
|
+
...typeof options.cors === "boolean" ? {} : options.cors
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
const proxies = options.proxies;
|
|
479
|
+
return !enabled ? void 0 : function(req, res, next) {
|
|
480
|
+
const { pathname } = urlParse(req.url);
|
|
481
|
+
if (!pathname || proxies.length === 0 || !proxies.some(
|
|
482
|
+
(context) => doesProxyContextMatchUrl(context, req.url, req)
|
|
483
|
+
)) {
|
|
484
|
+
return next();
|
|
485
|
+
}
|
|
486
|
+
const mockData = compiler.mockData;
|
|
487
|
+
const mockUrl = Object.keys(mockData).find(
|
|
488
|
+
(key) => pathToRegexp(key).test(pathname)
|
|
489
|
+
);
|
|
490
|
+
if (!mockUrl)
|
|
491
|
+
return next();
|
|
492
|
+
cors(corsOptions)(req, res, next);
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// src/core/resolvePluginOptions.ts
|
|
497
|
+
import process3 from "process";
|
|
498
|
+
import { isBoolean, toArray as toArray3 } from "@pengzhanbo/utils";
|
|
499
|
+
function resolvePluginOptions({
|
|
500
|
+
prefix = [],
|
|
501
|
+
wsPrefix = [],
|
|
502
|
+
cwd,
|
|
503
|
+
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
504
|
+
exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
|
|
505
|
+
reload = false,
|
|
506
|
+
log = "info",
|
|
507
|
+
cors: cors2 = true,
|
|
508
|
+
formidableOptions = {},
|
|
509
|
+
build = false,
|
|
510
|
+
cookiesOptions = {},
|
|
511
|
+
bodyParserOptions = {},
|
|
512
|
+
priority = {}
|
|
513
|
+
} = {}, { alias, context, plugins, proxies }) {
|
|
514
|
+
const logger = createLogger(
|
|
515
|
+
"rspack:mock",
|
|
516
|
+
isBoolean(log) ? log ? "info" : "error" : log
|
|
517
|
+
);
|
|
518
|
+
prefix = toArray3(prefix);
|
|
519
|
+
return {
|
|
520
|
+
prefix,
|
|
521
|
+
wsPrefix,
|
|
522
|
+
cwd: cwd || context || process3.cwd(),
|
|
523
|
+
include,
|
|
524
|
+
exclude,
|
|
525
|
+
reload,
|
|
526
|
+
cors: cors2,
|
|
527
|
+
cookiesOptions,
|
|
528
|
+
log,
|
|
529
|
+
formidableOptions: {
|
|
530
|
+
multiples: true,
|
|
531
|
+
...formidableOptions
|
|
532
|
+
},
|
|
533
|
+
bodyParserOptions,
|
|
534
|
+
priority,
|
|
535
|
+
build: build ? Object.assign(
|
|
536
|
+
{
|
|
537
|
+
serverPort: 8080,
|
|
538
|
+
dist: "mockServer",
|
|
539
|
+
log: "error"
|
|
540
|
+
},
|
|
541
|
+
typeof build === "object" ? build : {}
|
|
542
|
+
) : false,
|
|
543
|
+
alias,
|
|
544
|
+
plugins,
|
|
545
|
+
proxies: [...proxies, ...prefix],
|
|
546
|
+
wsProxies: toArray3(wsPrefix),
|
|
547
|
+
logger
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
550
551
|
export {
|
|
551
|
-
createMockMiddleware,
|
|
552
|
-
resolvePluginOptions,
|
|
553
552
|
buildMockServer,
|
|
554
553
|
createMockCompiler,
|
|
555
|
-
MockCompiler
|
|
554
|
+
MockCompiler,
|
|
555
|
+
createMockMiddleware,
|
|
556
|
+
resolvePluginOptions
|
|
556
557
|
};
|