rspack-plugin-mock 0.3.1 → 0.3.3

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.
@@ -120,16 +120,23 @@ var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefau
120
120
 
121
121
 
122
122
  // src/core/createRspackCompiler.ts
123
-
123
+ var _module = require('module');
124
124
  var _core = require('@rspack/core'); var rspackCore = _interopRequireWildcard(_core);
125
125
 
126
126
  var _iscoremodule = require('is-core-module'); var _iscoremodule2 = _interopRequireDefault(_iscoremodule);
127
+ var require2 = _module.createRequire.call(void 0, import.meta.url);
127
128
  function createCompiler(options, callback) {
128
129
  const rspackOptions = resolveRspackOptions(options);
129
130
  const isWatch = rspackOptions.watch === true;
130
131
  async function handler(err, stats) {
131
132
  const name = "[rspack:mock]";
132
- const logError = _optionalChain([stats, 'optionalAccess', _2 => _2.compilation, 'access', _3 => _3.getLogger, 'call', _4 => _4(name), 'access', _5 => _5.error]) || ((...args) => console.error(_picocolors2.default.red(name), ...args));
133
+ const logError = (...args) => {
134
+ if (stats) {
135
+ stats.compilation.getLogger(name).error(...args);
136
+ } else {
137
+ console.error(_picocolors2.default.red(name), ...args);
138
+ }
139
+ };
133
140
  if (err) {
134
141
  logError(err.stack || err);
135
142
  if ("details" in err) {
@@ -137,17 +144,17 @@ function createCompiler(options, callback) {
137
144
  }
138
145
  return;
139
146
  }
140
- if (_optionalChain([stats, 'optionalAccess', _6 => _6.hasErrors, 'call', _7 => _7()])) {
147
+ if (_optionalChain([stats, 'optionalAccess', _2 => _2.hasErrors, 'call', _3 => _3()])) {
141
148
  const info = stats.toJson();
142
149
  logError(info.errors);
143
150
  }
144
151
  const code = _chunkZEC4FWWYcjs.vfs.readFileSync("/output.js", "utf-8");
145
152
  const externals = [];
146
153
  if (!isWatch) {
147
- const modules = _optionalChain([stats, 'optionalAccess', _8 => _8.toJson, 'call', _9 => _9(), 'access', _10 => _10.modules]) || [];
154
+ const modules = _optionalChain([stats, 'optionalAccess', _4 => _4.toJson, 'call', _5 => _5(), 'access', _6 => _6.modules]) || [];
148
155
  const aliasList = Object.keys(options.alias || {}).map((key) => key.replace(/\$$/g, ""));
149
156
  for (const { name: name2 } of modules) {
150
- if (_optionalChain([name2, 'optionalAccess', _11 => _11.startsWith, 'call', _12 => _12("external")])) {
157
+ if (_optionalChain([name2, 'optionalAccess', _7 => _7.startsWith, 'call', _8 => _8("external")])) {
151
158
  const packageName = normalizePackageName(name2);
152
159
  if (!_iscoremodule2.default.call(void 0, packageName) && !aliasList.includes(packageName))
153
160
  externals.push(normalizePackageName(name2));
@@ -160,7 +167,7 @@ function createCompiler(options, callback) {
160
167
  if (compiler)
161
168
  compiler.outputFileSystem = _chunkZEC4FWWYcjs.vfs;
162
169
  if (!isWatch) {
163
- _optionalChain([compiler, 'optionalAccess', _13 => _13.run, 'call', _14 => _14(async (...args) => {
170
+ _optionalChain([compiler, 'optionalAccess', _9 => _9.run, 'call', _10 => _10(async (...args) => {
164
171
  await handler(...args);
165
172
  compiler.close(() => {
166
173
  });
@@ -216,7 +223,7 @@ function resolveRspackOptions({
216
223
  rules: [
217
224
  {
218
225
  test: /\.json5?$/,
219
- loader: _path2.default.join(_chunkZEC4FWWYcjs.packageDir, "json5-loader.cjs"),
226
+ loader: require2.resolve("#json5-loader"),
220
227
  type: "javascript/auto"
221
228
  },
222
229
  {
@@ -329,12 +336,15 @@ const wsProxies = ${JSON.stringify(_utils.toArray.call(void 0, wsPrefix))};
329
336
  const cookiesOptions = ${JSON.stringify(cookiesOptions)};
330
337
  const bodyParserOptions = ${JSON.stringify(bodyParserOptions)};
331
338
  const priority = ${JSON.stringify(priority)};
332
- const data = { mockData: transformMockData(transformRawData(rawData)) };
339
+ const mockConfig = {
340
+ mockData: transformMockData(transformRawData(rawData)),
341
+ on: () => {},
342
+ };
333
343
 
334
- mockWebSocket(data, server, { wsProxies, cookiesOptions, logger });
344
+ mockWebSocket(mockConfig, server, { wsProxies, cookiesOptions, logger });
335
345
 
336
346
  app.use(corsMiddleware());
337
- app.use(baseMiddleware(data, {
347
+ app.use(baseMiddleware(mockConfig, {
338
348
  formidableOptions: { multiples: true },
339
349
  proxies,
340
350
  priority,
@@ -488,7 +498,7 @@ var MockCompiler = (_class = class extends _events2.default {
488
498
  }
489
499
  close() {
490
500
  this.mockWatcher.close();
491
- _optionalChain([this, 'access', _15 => _15.compiler, 'optionalAccess', _16 => _16.close, 'call', _17 => _17(() => {
501
+ _optionalChain([this, 'access', _11 => _11.compiler, 'optionalAccess', _12 => _12.close, 'call', _13 => _13(() => {
492
502
  })]);
493
503
  this.emit("close");
494
504
  }
@@ -112,7 +112,7 @@ function resolvePluginOptions({
112
112
  // src/core/build.ts
113
113
  import fs from "fs";
114
114
  import fsp from "fs/promises";
115
- import path2 from "path";
115
+ import path from "path";
116
116
  import process2 from "process";
117
117
  import fg from "fast-glob";
118
118
  import { createFilter } from "@rollup/pluginutils";
@@ -120,16 +120,23 @@ import color2 from "picocolors";
120
120
  import { toArray as toArray2 } from "@pengzhanbo/utils";
121
121
 
122
122
  // src/core/createRspackCompiler.ts
123
- import path from "path";
123
+ import { createRequire } from "module";
124
124
  import * as rspackCore from "@rspack/core";
125
125
  import color from "picocolors";
126
126
  import isCore from "is-core-module";
127
+ var require2 = createRequire(import.meta.url);
127
128
  function createCompiler(options, callback) {
128
129
  const rspackOptions = resolveRspackOptions(options);
129
130
  const isWatch = rspackOptions.watch === true;
130
131
  async function handler(err, stats) {
131
132
  const name = "[rspack:mock]";
132
- const logError = stats?.compilation.getLogger(name).error || ((...args) => console.error(color.red(name), ...args));
133
+ const logError = (...args) => {
134
+ if (stats) {
135
+ stats.compilation.getLogger(name).error(...args);
136
+ } else {
137
+ console.error(color.red(name), ...args);
138
+ }
139
+ };
133
140
  if (err) {
134
141
  logError(err.stack || err);
135
142
  if ("details" in err) {
@@ -216,7 +223,7 @@ function resolveRspackOptions({
216
223
  rules: [
217
224
  {
218
225
  test: /\.json5?$/,
219
- loader: path.join(packageDir, "json5-loader.cjs"),
226
+ loader: require2.resolve("#json5-loader"),
220
227
  type: "javascript/auto"
221
228
  },
222
229
  {
@@ -250,7 +257,7 @@ function resolveRspackOptions({
250
257
 
251
258
  // src/core/build.ts
252
259
  async function buildMockServer(options, outputDir) {
253
- const entryFile = path2.resolve(process2.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
260
+ const entryFile = path.resolve(process2.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
254
261
  const mockFileList = await getMockFileList(options);
255
262
  await writeMockEntryFile(entryFile, mockFileList, options.cwd);
256
263
  const { code, externals } = await transformWithRspack({
@@ -265,15 +272,15 @@ async function buildMockServer(options, outputDir) {
265
272
  { filename: "index.js", source: generatorServerEntryCode(options) },
266
273
  { filename: "package.json", source: generatePackageJson(options, externals) }
267
274
  ];
268
- const dist = path2.resolve(outputDir, options.build.dist);
275
+ const dist = path.resolve(outputDir, options.build.dist);
269
276
  options.logger.info(
270
- `${color2.green("\u2713")} generate mock server in ${color2.cyan(path2.relative(process2.cwd(), dist))}`
277
+ `${color2.green("\u2713")} generate mock server in ${color2.cyan(path.relative(process2.cwd(), dist))}`
271
278
  );
272
279
  if (!fs.existsSync(dist)) {
273
280
  await fsp.mkdir(dist, { recursive: true });
274
281
  }
275
282
  for (const { filename, source } of outputList) {
276
- await fsp.writeFile(path2.join(dist, filename), source, "utf8");
283
+ await fsp.writeFile(path.join(dist, filename), source, "utf8");
277
284
  const sourceSize = (source.length / 1024).toFixed(2);
278
285
  const space = filename.length < 24 ? " ".repeat(24 - filename.length) : "";
279
286
  options.logger.info(` ${color2.green(filename)}${space}${color2.bold(color2.dim(`${sourceSize} kB`))}`);
@@ -329,12 +336,15 @@ const wsProxies = ${JSON.stringify(toArray2(wsPrefix))};
329
336
  const cookiesOptions = ${JSON.stringify(cookiesOptions)};
330
337
  const bodyParserOptions = ${JSON.stringify(bodyParserOptions)};
331
338
  const priority = ${JSON.stringify(priority)};
332
- const data = { mockData: transformMockData(transformRawData(rawData)) };
339
+ const mockConfig = {
340
+ mockData: transformMockData(transformRawData(rawData)),
341
+ on: () => {},
342
+ };
333
343
 
334
- mockWebSocket(data, server, { wsProxies, cookiesOptions, logger });
344
+ mockWebSocket(mockConfig, server, { wsProxies, cookiesOptions, logger });
335
345
 
336
346
  app.use(corsMiddleware());
337
- app.use(baseMiddleware(data, {
347
+ app.use(baseMiddleware(mockConfig, {
338
348
  formidableOptions: { multiples: true },
339
349
  proxies,
340
350
  priority,
@@ -356,7 +366,7 @@ async function writeMockEntryFile(entryFile, files, cwd) {
356
366
  const importers = [];
357
367
  const exporters = [];
358
368
  for (const [index, filepath] of files.entries()) {
359
- const file = normalizePath(path2.join(cwd, filepath));
369
+ const file = normalizePath(path.join(cwd, filepath));
360
370
  importers.push(`import * as m${index} from '${file}'`);
361
371
  exporters.push(`[m${index}, '${filepath}']`);
362
372
  }
@@ -365,7 +375,7 @@ async function writeMockEntryFile(entryFile, files, cwd) {
365
375
  export default [
366
376
  ${exporters.join(",\n ")}
367
377
  ]`;
368
- const dirname = path2.dirname(entryFile);
378
+ const dirname = path.dirname(entryFile);
369
379
  if (!fs.existsSync(dirname)) {
370
380
  await fsp.mkdir(dirname, { recursive: true });
371
381
  }
@@ -374,7 +384,7 @@ export default [
374
384
  function getPluginPackageInfo() {
375
385
  let pkg = {};
376
386
  try {
377
- const filepath = path2.join(packageDir, "../package.json");
387
+ const filepath = path.join(packageDir, "../package.json");
378
388
  if (fs.existsSync(filepath)) {
379
389
  pkg = JSON.parse(fs.readFileSync(filepath, "utf8"));
380
390
  }
@@ -399,14 +409,14 @@ function getHostDependencies(context) {
399
409
  // src/core/mockCompiler.ts
400
410
  import EventEmitter from "events";
401
411
  import process3 from "process";
402
- import path4 from "path";
412
+ import path3 from "path";
403
413
  import fastGlob from "fast-glob";
404
414
  import chokidar from "chokidar";
405
415
  import { createFilter as createFilter2 } from "@rollup/pluginutils";
406
416
  import { toArray as toArray3 } from "@pengzhanbo/utils";
407
417
 
408
418
  // src/core/loadFromCode.ts
409
- import path3 from "path";
419
+ import path2 from "path";
410
420
  import fs2, { promises as fsp2 } from "fs";
411
421
  async function loadFromCode({
412
422
  filepath,
@@ -414,7 +424,7 @@ async function loadFromCode({
414
424
  isESM,
415
425
  cwd
416
426
  }) {
417
- filepath = path3.resolve(cwd, filepath);
427
+ filepath = path2.resolve(cwd, filepath);
418
428
  const fileBase = `${filepath}.timestamp-${Date.now()}`;
419
429
  const ext = isESM ? ".mjs" : ".cjs";
420
430
  const fileNameTmp = `${fileBase}${ext}`;
@@ -446,7 +456,7 @@ var MockCompiler = class extends EventEmitter {
446
456
  this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
447
457
  } catch {
448
458
  }
449
- this.entryFile = path4.resolve(process3.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
459
+ this.entryFile = path3.resolve(process3.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
450
460
  }
451
461
  cwd;
452
462
  mockWatcher;
package/dist/index.cjs CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- var _chunk4GMRSIINcjs = require('./chunk-4GMRSIIN.cjs');
7
+ var _chunkGI65LCB7cjs = require('./chunk-GI65LCB7.cjs');
8
8
 
9
9
 
10
10
 
@@ -25,8 +25,8 @@ var MockServerPlugin = class {
25
25
  const compilerOptions = compiler.options;
26
26
  const options = resolvePluginOptions2(compiler, this.options);
27
27
  if (_process2.default.env.NODE_ENV !== "production") {
28
- const mockCompiler = _chunk4GMRSIINcjs.createMockCompiler.call(void 0, options);
29
- const mockMiddleware = _chunk4GMRSIINcjs.createMockMiddleware.call(void 0, mockCompiler, options);
28
+ const mockCompiler = _chunkGI65LCB7cjs.createMockCompiler.call(void 0, options);
29
+ const mockMiddleware = _chunkGI65LCB7cjs.createMockMiddleware.call(void 0, mockCompiler, options);
30
30
  const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
31
31
  const waitServer = _chunkZEC4FWWYcjs.waitingFor.call(void 0, (server) => {
32
32
  _chunkZEC4FWWYcjs.mockWebSocket.call(void 0, mockCompiler, server, options);
@@ -66,7 +66,7 @@ var MockServerPlugin = class {
66
66
  compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
67
67
  compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
68
68
  } else if (options.build !== false) {
69
- compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunk4GMRSIINcjs.buildMockServer.call(void 0,
69
+ compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkGI65LCB7cjs.buildMockServer.call(void 0,
70
70
  options,
71
71
  compilerOptions.output.path || _path2.default.resolve(_process2.default.cwd(), "dist")
72
72
  ));
@@ -86,7 +86,7 @@ function resolvePluginOptions2(compiler, options) {
86
86
  }
87
87
  return [];
88
88
  });
89
- return _chunk4GMRSIINcjs.resolvePluginOptions.call(void 0, options, {
89
+ return _chunkGI65LCB7cjs.resolvePluginOptions.call(void 0, options, {
90
90
  alias,
91
91
  context,
92
92
  plugins: _utils.toArray.call(void 0, definePluginInstance),
@@ -100,4 +100,4 @@ function resolvePluginOptions2(compiler, options) {
100
100
 
101
101
 
102
102
 
103
- exports.MockCompiler = _chunk4GMRSIINcjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunk4GMRSIINcjs.createMockCompiler; exports.createMockMiddleware = _chunk4GMRSIINcjs.createMockMiddleware; exports.mockWebSocket = _chunkZEC4FWWYcjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
103
+ exports.MockCompiler = _chunkGI65LCB7cjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunkGI65LCB7cjs.createMockCompiler; exports.createMockMiddleware = _chunkGI65LCB7cjs.createMockMiddleware; exports.mockWebSocket = _chunkZEC4FWWYcjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  createMockCompiler,
5
5
  createMockMiddleware,
6
6
  resolvePluginOptions
7
- } from "./chunk-B5W3PWCX.js";
7
+ } from "./chunk-OURZXZQM.js";
8
8
  import {
9
9
  mockWebSocket,
10
10
  rewriteRequest,
@@ -1,11 +1,17 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/json5-loader.ts
2
- var _json5 = require('json5'); var _json52 = _interopRequireDefault(_json5);
3
- var json5Loader = function(content) {
4
- if (!content)
5
- return "export default {}";
6
- return `export default ${JSON.stringify(_json52.default.parse(content))}`;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = (cb, mod) => function __require() {
3
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
7
4
  };
8
- var json5_loader_default = json5Loader;
9
-
10
5
 
11
- exports.default = json5_loader_default;
6
+ // src/json5-loader.cts
7
+ var _json5 = require('json5'); var _json52 = _interopRequireDefault(_json5);
8
+ var require_json5_loader = __commonJS({
9
+ "src/json5-loader.cts"(exports, module) {
10
+ module.exports = function(content) {
11
+ if (!content)
12
+ return "export default {}";
13
+ return `export default ${JSON.stringify(_json52.default.parse(content))}`;
14
+ };
15
+ }
16
+ });
17
+ exports. default = require_json5_loader();
package/dist/rsbuild.cjs CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
 
5
5
 
6
- var _chunk4GMRSIINcjs = require('./chunk-4GMRSIIN.cjs');
6
+ var _chunkGI65LCB7cjs = require('./chunk-GI65LCB7.cjs');
7
7
 
8
8
 
9
9
 
@@ -22,7 +22,7 @@ function pluginMockServer(options = {}) {
22
22
  name: "plugin-mock-server",
23
23
  setup(api) {
24
24
  const rsbuildConfig = api.getRsbuildConfig();
25
- const resolvedOptions = _chunk4GMRSIINcjs.resolvePluginOptions.call(void 0, options, {
25
+ const resolvedOptions = _chunkGI65LCB7cjs.resolvePluginOptions.call(void 0, options, {
26
26
  proxies: resolveConfigProxies(rsbuildConfig),
27
27
  alias: {},
28
28
  context: api.context.rootPath,
@@ -32,7 +32,7 @@ function pluginMockServer(options = {}) {
32
32
  if (resolvedOptions.build) {
33
33
  api.onAfterBuild(async () => {
34
34
  const config = api.getNormalizedConfig();
35
- await _chunk4GMRSIINcjs.buildMockServer.call(void 0,
35
+ await _chunkGI65LCB7cjs.buildMockServer.call(void 0,
36
36
  resolvedOptions,
37
37
  _path2.default.resolve(_process2.default.cwd(), config.output.distPath.root || "dist")
38
38
  );
@@ -40,10 +40,10 @@ function pluginMockServer(options = {}) {
40
40
  }
41
41
  return;
42
42
  }
43
- const mockCompiler = _chunk4GMRSIINcjs.createMockCompiler.call(void 0, resolvedOptions);
43
+ const mockCompiler = _chunkGI65LCB7cjs.createMockCompiler.call(void 0, resolvedOptions);
44
44
  api.modifyRsbuildConfig((config) => {
45
45
  updateServerProxyConfigByHttpMock(config);
46
- const mockMiddleware = _chunk4GMRSIINcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
46
+ const mockMiddleware = _chunkGI65LCB7cjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
47
47
  config.dev ??= {};
48
48
  config.dev.setupMiddlewares ??= [];
49
49
  config.dev.setupMiddlewares.push((middlewares, server2) => {
package/dist/rsbuild.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  createMockCompiler,
4
4
  createMockMiddleware,
5
5
  resolvePluginOptions
6
- } from "./chunk-B5W3PWCX.js";
6
+ } from "./chunk-OURZXZQM.js";
7
7
  import {
8
8
  mockWebSocket,
9
9
  rewriteRequest
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "rspack-plugin-mock",
3
3
  "type": "module",
4
- "version": "0.3.1",
4
+ "version": "0.3.3",
5
5
  "description": "inject api mock server to development server",
6
6
  "author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
7
7
  "license": "MIT",
8
+ "homepage": "https://github.com/pengzhanbo/rspack-plugin-mock",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/pengzhanbo/rspack-plugin-mock"
12
+ },
8
13
  "keywords": [
9
14
  "mock",
10
15
  "mock server",
@@ -13,6 +18,9 @@
13
18
  "rspack-plugin",
14
19
  "rsbuild-plugin"
15
20
  ],
21
+ "imports": {
22
+ "#json5-loader": "./dist/json5-loader.cjs"
23
+ },
16
24
  "exports": {
17
25
  ".": {
18
26
  "import": {
@@ -1,5 +0,0 @@
1
- import { LoaderDefinitionFunction } from '@rspack/core';
2
-
3
- declare const json5Loader: LoaderDefinitionFunction;
4
-
5
- export { json5Loader as default };
@@ -1,5 +0,0 @@
1
- import { LoaderDefinitionFunction } from '@rspack/core';
2
-
3
- declare const json5Loader: LoaderDefinitionFunction;
4
-
5
- export { json5Loader as default };
@@ -1,11 +0,0 @@
1
- // src/json5-loader.ts
2
- import JSON5 from "json5";
3
- var json5Loader = function(content) {
4
- if (!content)
5
- return "export default {}";
6
- return `export default ${JSON.stringify(JSON5.parse(content))}`;
7
- };
8
- var json5_loader_default = json5Loader;
9
- export {
10
- json5_loader_default as default
11
- };