rspack-plugin-mock 0.3.2 → 0.3.4

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
  {
@@ -411,6 +418,7 @@ var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_ch
411
418
  // src/core/loadFromCode.ts
412
419
 
413
420
 
421
+ var _url = require('url');
414
422
  async function loadFromCode({
415
423
  filepath,
416
424
  code,
@@ -421,9 +429,10 @@ async function loadFromCode({
421
429
  const fileBase = `${filepath}.timestamp-${Date.now()}`;
422
430
  const ext = isESM ? ".mjs" : ".cjs";
423
431
  const fileNameTmp = `${fileBase}${ext}`;
432
+ const fileUrl = _url.pathToFileURL.call(void 0, fileNameTmp).toString();
424
433
  await _fs.promises.writeFile(fileNameTmp, code, "utf8");
425
434
  try {
426
- const result = await Promise.resolve().then(() => _interopRequireWildcard(require(fileNameTmp)));
435
+ const result = await import(fileUrl);
427
436
  return result.default || result;
428
437
  } finally {
429
438
  try {
@@ -491,7 +500,7 @@ var MockCompiler = (_class = class extends _events2.default {
491
500
  }
492
501
  close() {
493
502
  this.mockWatcher.close();
494
- _optionalChain([this, 'access', _15 => _15.compiler, 'optionalAccess', _16 => _16.close, 'call', _17 => _17(() => {
503
+ _optionalChain([this, 'access', _11 => _11.compiler, 'optionalAccess', _12 => _12.close, 'call', _13 => _13(() => {
495
504
  })]);
496
505
  this.emit("close");
497
506
  }
@@ -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`))}`);
@@ -359,7 +366,7 @@ async function writeMockEntryFile(entryFile, files, cwd) {
359
366
  const importers = [];
360
367
  const exporters = [];
361
368
  for (const [index, filepath] of files.entries()) {
362
- const file = normalizePath(path2.join(cwd, filepath));
369
+ const file = normalizePath(path.join(cwd, filepath));
363
370
  importers.push(`import * as m${index} from '${file}'`);
364
371
  exporters.push(`[m${index}, '${filepath}']`);
365
372
  }
@@ -368,7 +375,7 @@ async function writeMockEntryFile(entryFile, files, cwd) {
368
375
  export default [
369
376
  ${exporters.join(",\n ")}
370
377
  ]`;
371
- const dirname = path2.dirname(entryFile);
378
+ const dirname = path.dirname(entryFile);
372
379
  if (!fs.existsSync(dirname)) {
373
380
  await fsp.mkdir(dirname, { recursive: true });
374
381
  }
@@ -377,7 +384,7 @@ export default [
377
384
  function getPluginPackageInfo() {
378
385
  let pkg = {};
379
386
  try {
380
- const filepath = path2.join(packageDir, "../package.json");
387
+ const filepath = path.join(packageDir, "../package.json");
381
388
  if (fs.existsSync(filepath)) {
382
389
  pkg = JSON.parse(fs.readFileSync(filepath, "utf8"));
383
390
  }
@@ -402,28 +409,30 @@ function getHostDependencies(context) {
402
409
  // src/core/mockCompiler.ts
403
410
  import EventEmitter from "events";
404
411
  import process3 from "process";
405
- import path4 from "path";
412
+ import path3 from "path";
406
413
  import fastGlob from "fast-glob";
407
414
  import chokidar from "chokidar";
408
415
  import { createFilter as createFilter2 } from "@rollup/pluginutils";
409
416
  import { toArray as toArray3 } from "@pengzhanbo/utils";
410
417
 
411
418
  // src/core/loadFromCode.ts
412
- import path3 from "path";
419
+ import path2 from "path";
413
420
  import fs2, { promises as fsp2 } from "fs";
421
+ import { pathToFileURL } from "url";
414
422
  async function loadFromCode({
415
423
  filepath,
416
424
  code,
417
425
  isESM,
418
426
  cwd
419
427
  }) {
420
- filepath = path3.resolve(cwd, filepath);
428
+ filepath = path2.resolve(cwd, filepath);
421
429
  const fileBase = `${filepath}.timestamp-${Date.now()}`;
422
430
  const ext = isESM ? ".mjs" : ".cjs";
423
431
  const fileNameTmp = `${fileBase}${ext}`;
432
+ const fileUrl = pathToFileURL(fileNameTmp).toString();
424
433
  await fsp2.writeFile(fileNameTmp, code, "utf8");
425
434
  try {
426
- const result = await import(fileNameTmp);
435
+ const result = await import(fileUrl);
427
436
  return result.default || result;
428
437
  } finally {
429
438
  try {
@@ -449,7 +458,7 @@ var MockCompiler = class extends EventEmitter {
449
458
  this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
450
459
  } catch {
451
460
  }
452
- this.entryFile = path4.resolve(process3.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
461
+ this.entryFile = path3.resolve(process3.cwd(), "node_modules/.cache/mock-server/mock-server.ts");
453
462
  }
454
463
  cwd;
455
464
  mockWatcher;
package/dist/index.cjs CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- var _chunk4HRWYJ3Ecjs = require('./chunk-4HRWYJ3E.cjs');
7
+ var _chunkHLMEDDGXcjs = require('./chunk-HLMEDDGX.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 = _chunk4HRWYJ3Ecjs.createMockCompiler.call(void 0, options);
29
- const mockMiddleware = _chunk4HRWYJ3Ecjs.createMockMiddleware.call(void 0, mockCompiler, options);
28
+ const mockCompiler = _chunkHLMEDDGXcjs.createMockCompiler.call(void 0, options);
29
+ const mockMiddleware = _chunkHLMEDDGXcjs.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, () => _chunk4HRWYJ3Ecjs.buildMockServer.call(void 0,
69
+ compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkHLMEDDGXcjs.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 _chunk4HRWYJ3Ecjs.resolvePluginOptions.call(void 0, options, {
89
+ return _chunkHLMEDDGXcjs.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 = _chunk4HRWYJ3Ecjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunk4HRWYJ3Ecjs.createMockCompiler; exports.createMockMiddleware = _chunk4HRWYJ3Ecjs.createMockMiddleware; exports.mockWebSocket = _chunkZEC4FWWYcjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
103
+ exports.MockCompiler = _chunkHLMEDDGXcjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunkHLMEDDGXcjs.createMockCompiler; exports.createMockMiddleware = _chunkHLMEDDGXcjs.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-ZYUUWKWW.js";
7
+ } from "./chunk-O6PRDW23.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 _chunk4HRWYJ3Ecjs = require('./chunk-4HRWYJ3E.cjs');
6
+ var _chunkHLMEDDGXcjs = require('./chunk-HLMEDDGX.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 = _chunk4HRWYJ3Ecjs.resolvePluginOptions.call(void 0, options, {
25
+ const resolvedOptions = _chunkHLMEDDGXcjs.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 _chunk4HRWYJ3Ecjs.buildMockServer.call(void 0,
35
+ await _chunkHLMEDDGXcjs.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 = _chunk4HRWYJ3Ecjs.createMockCompiler.call(void 0, resolvedOptions);
43
+ const mockCompiler = _chunkHLMEDDGXcjs.createMockCompiler.call(void 0, resolvedOptions);
44
44
  api.modifyRsbuildConfig((config) => {
45
45
  updateServerProxyConfigByHttpMock(config);
46
- const mockMiddleware = _chunk4HRWYJ3Ecjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
46
+ const mockMiddleware = _chunkHLMEDDGXcjs.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-ZYUUWKWW.js";
6
+ } from "./chunk-O6PRDW23.js";
7
7
  import {
8
8
  mockWebSocket,
9
9
  rewriteRequest
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rspack-plugin-mock",
3
3
  "type": "module",
4
- "version": "0.3.2",
4
+ "version": "0.3.4",
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",
@@ -18,6 +18,9 @@
18
18
  "rspack-plugin",
19
19
  "rsbuild-plugin"
20
20
  ],
21
+ "imports": {
22
+ "#json5-loader": "./dist/json5-loader.cjs"
23
+ },
21
24
  "exports": {
22
25
  ".": {
23
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
- };