rspack-plugin-mock 0.3.4 → 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/README.md +2 -2
- package/README.zh-CN.md +2 -2
- package/dist/{chunk-O6PRDW23.js → chunk-4XOUX6CL.js} +127 -125
- package/dist/{chunk-EY46RSAC.js → chunk-JMXFIX5Q.js} +274 -272
- package/dist/{chunk-ZEC4FWWY.cjs → chunk-TKSRTJX5.cjs} +254 -252
- package/dist/{chunk-HLMEDDGX.cjs → chunk-YAHWW6TX.cjs} +127 -125
- package/dist/helper.cjs +55 -2
- package/dist/helper.d.cts +21 -7
- package/dist/helper.d.ts +21 -7
- package/dist/helper.js +54 -1
- 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 +17 -17
- package/dist/rsbuild.d.cts +5 -5
- package/dist/rsbuild.d.ts +5 -5
- package/dist/rsbuild.js +8 -8
- 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.cts → types-Aw0AciTG.d.cts} +5 -5
- package/dist/{types-BgpcN3jm.d.ts → types-Aw0AciTG.d.ts} +5 -5
- package/package.json +20 -20
package/README.md
CHANGED
|
@@ -7,8 +7,8 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
|
|
|
7
7
|
<p align="center">
|
|
8
8
|
<a href="https://www.npmjs.com/package/rspack-plugin-mock"><img alt="npm" src="https://img.shields.io/npm/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91"></a>
|
|
9
9
|
<img alt="node-current" src="https://img.shields.io/node/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
|
|
10
|
-
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rspack/core?style=flat-square&colorA=564341&colorB=EDED91">
|
|
11
|
-
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91">
|
|
10
|
+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rspack/core?style=flat-square&colorA=564341&colorB=EDED91&label=rspack">
|
|
11
|
+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91&label=rsbuild">
|
|
12
12
|
<img alt="npm" src="https://img.shields.io/npm/dm/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
|
|
13
13
|
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/rspack-plugin-mock/lint.yml?style=flat-square&colorA=564341&colorB=EDED91">
|
|
14
14
|
</p>
|
package/README.zh-CN.md
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
<p align="center">
|
|
8
8
|
<a href="https://www.npmjs.com/package/rspack-plugin-mock"><img alt="npm" src="https://img.shields.io/npm/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91"></a>
|
|
9
9
|
<img alt="node-current" src="https://img.shields.io/node/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
|
|
10
|
-
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rspack/core?style=flat-square&colorA=564341&colorB=EDED91">
|
|
11
|
-
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91">
|
|
10
|
+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rspack/core?style=flat-square&colorA=564341&colorB=EDED91&label=rspack">
|
|
11
|
+
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91&label=rsbuild">
|
|
12
12
|
<img alt="npm" src="https://img.shields.io/npm/dm/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
|
|
13
13
|
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/rspack-plugin-mock/lint.yml?style=flat-square&colorA=564341&colorB=EDED91">
|
|
14
14
|
</p>
|
|
@@ -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);
|
|
@@ -199,6 +101,9 @@ function resolveRspackOptions({
|
|
|
199
101
|
watch = false
|
|
200
102
|
}) {
|
|
201
103
|
const targets = ["node >= 18.0.0"];
|
|
104
|
+
if (alias && "@swc/helpers" in alias) {
|
|
105
|
+
delete alias["@swc/helpers"];
|
|
106
|
+
}
|
|
202
107
|
return {
|
|
203
108
|
mode: "production",
|
|
204
109
|
context: cwd,
|
|
@@ -206,7 +111,6 @@ function resolveRspackOptions({
|
|
|
206
111
|
watch,
|
|
207
112
|
target: "node18.0",
|
|
208
113
|
externalsType: isEsm ? "module" : "commonjs2",
|
|
209
|
-
externals: /^[^./].*/,
|
|
210
114
|
resolve: {
|
|
211
115
|
alias,
|
|
212
116
|
extensions: [".js", ".ts", ".cjs", ".mjs", ".json5", ".json"]
|
|
@@ -257,7 +161,7 @@ function resolveRspackOptions({
|
|
|
257
161
|
|
|
258
162
|
// src/core/build.ts
|
|
259
163
|
async function buildMockServer(options, outputDir) {
|
|
260
|
-
const entryFile = path.resolve(
|
|
164
|
+
const entryFile = path.resolve(process.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
|
|
261
165
|
const mockFileList = await getMockFileList(options);
|
|
262
166
|
await writeMockEntryFile(entryFile, mockFileList, options.cwd);
|
|
263
167
|
const { code, externals } = await transformWithRspack({
|
|
@@ -274,7 +178,7 @@ async function buildMockServer(options, outputDir) {
|
|
|
274
178
|
];
|
|
275
179
|
const dist = path.resolve(outputDir, options.build.dist);
|
|
276
180
|
options.logger.info(
|
|
277
|
-
`${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))}`
|
|
278
182
|
);
|
|
279
183
|
if (!fs.existsSync(dist)) {
|
|
280
184
|
await fsp.mkdir(dist, { recursive: true });
|
|
@@ -332,7 +236,7 @@ const app = connect();
|
|
|
332
236
|
const server = createServer(app);
|
|
333
237
|
const logger = createLogger('mock-server', '${log}');
|
|
334
238
|
const proxies = ${JSON.stringify(proxies)};
|
|
335
|
-
const wsProxies = ${JSON.stringify(
|
|
239
|
+
const wsProxies = ${JSON.stringify(toArray(wsPrefix))};
|
|
336
240
|
const cookiesOptions = ${JSON.stringify(cookiesOptions)};
|
|
337
241
|
const bodyParserOptions = ${JSON.stringify(bodyParserOptions)};
|
|
338
242
|
const priority = ${JSON.stringify(priority)};
|
|
@@ -408,16 +312,16 @@ function getHostDependencies(context) {
|
|
|
408
312
|
|
|
409
313
|
// src/core/mockCompiler.ts
|
|
410
314
|
import EventEmitter from "events";
|
|
411
|
-
import process3 from "process";
|
|
412
315
|
import path3 from "path";
|
|
413
|
-
import
|
|
414
|
-
import
|
|
316
|
+
import process2 from "process";
|
|
317
|
+
import { toArray as toArray2 } from "@pengzhanbo/utils";
|
|
415
318
|
import { createFilter as createFilter2 } from "@rollup/pluginutils";
|
|
416
|
-
import
|
|
319
|
+
import chokidar from "chokidar";
|
|
320
|
+
import fastGlob from "fast-glob";
|
|
417
321
|
|
|
418
322
|
// src/core/loadFromCode.ts
|
|
419
|
-
import path2 from "path";
|
|
420
323
|
import fs2, { promises as fsp2 } from "fs";
|
|
324
|
+
import path2 from "path";
|
|
421
325
|
import { pathToFileURL } from "url";
|
|
422
326
|
async function loadFromCode({
|
|
423
327
|
filepath,
|
|
@@ -426,17 +330,16 @@ async function loadFromCode({
|
|
|
426
330
|
cwd
|
|
427
331
|
}) {
|
|
428
332
|
filepath = path2.resolve(cwd, filepath);
|
|
429
|
-
const fileBase = `${filepath}.timestamp-${Date.now()}`;
|
|
430
333
|
const ext = isESM ? ".mjs" : ".cjs";
|
|
431
|
-
const
|
|
432
|
-
const
|
|
433
|
-
await fsp2.writeFile(
|
|
334
|
+
const filepathTmp = `${filepath}.timestamp-${Date.now()}${ext}`;
|
|
335
|
+
const file = pathToFileURL(filepathTmp).toString();
|
|
336
|
+
await fsp2.writeFile(filepathTmp, code, "utf8");
|
|
434
337
|
try {
|
|
435
|
-
const
|
|
436
|
-
return
|
|
338
|
+
const mod = await import(file);
|
|
339
|
+
return mod.default || mod;
|
|
437
340
|
} finally {
|
|
438
341
|
try {
|
|
439
|
-
fs2.unlinkSync(
|
|
342
|
+
fs2.unlinkSync(filepathTmp);
|
|
440
343
|
} catch {
|
|
441
344
|
}
|
|
442
345
|
}
|
|
@@ -450,7 +353,7 @@ var MockCompiler = class extends EventEmitter {
|
|
|
450
353
|
constructor(options) {
|
|
451
354
|
super();
|
|
452
355
|
this.options = options;
|
|
453
|
-
this.cwd = options.cwd ||
|
|
356
|
+
this.cwd = options.cwd || process2.cwd();
|
|
454
357
|
const { include, exclude } = this.options;
|
|
455
358
|
this.fileFilter = createFilter2(include, exclude, { resolve: false });
|
|
456
359
|
try {
|
|
@@ -458,7 +361,7 @@ var MockCompiler = class extends EventEmitter {
|
|
|
458
361
|
this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
|
|
459
362
|
} catch {
|
|
460
363
|
}
|
|
461
|
-
this.entryFile = path3.resolve(
|
|
364
|
+
this.entryFile = path3.resolve(process2.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
|
|
462
365
|
}
|
|
463
366
|
cwd;
|
|
464
367
|
mockWatcher;
|
|
@@ -521,7 +424,7 @@ var MockCompiler = class extends EventEmitter {
|
|
|
521
424
|
}
|
|
522
425
|
watchMockFiles() {
|
|
523
426
|
const { include } = this.options;
|
|
524
|
-
const [firstGlob, ...otherGlob] =
|
|
427
|
+
const [firstGlob, ...otherGlob] = toArray2(include);
|
|
525
428
|
const watcher = this.mockWatcher = chokidar.watch(firstGlob, {
|
|
526
429
|
ignoreInitial: true,
|
|
527
430
|
cwd: this.cwd
|
|
@@ -546,10 +449,109 @@ var MockCompiler = class extends EventEmitter {
|
|
|
546
449
|
}
|
|
547
450
|
};
|
|
548
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
|
+
|
|
549
551
|
export {
|
|
550
|
-
createMockMiddleware,
|
|
551
|
-
resolvePluginOptions,
|
|
552
552
|
buildMockServer,
|
|
553
553
|
createMockCompiler,
|
|
554
|
-
MockCompiler
|
|
554
|
+
MockCompiler,
|
|
555
|
+
createMockMiddleware,
|
|
556
|
+
resolvePluginOptions
|
|
555
557
|
};
|