vite-plugin-mock-dev-server 0.1.0 → 0.1.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/index.cjs CHANGED
@@ -39,9 +39,9 @@ var import_node_url = require("url");
39
39
  var import_chokidar = __toESM(require("chokidar"), 1);
40
40
  var import_co_body = __toESM(require("co-body"), 1);
41
41
  var import_debug = __toESM(require("debug"), 1);
42
+ var import_esbuild = require("esbuild");
42
43
  var import_fast_glob = __toESM(require("fast-glob"), 1);
43
44
  var import_path_to_regexp = require("path-to-regexp");
44
- var import_vite = require("vite");
45
45
 
46
46
  // src/utils.ts
47
47
  var isArray = (val) => Array.isArray(val);
@@ -56,10 +56,11 @@ var debug = (0, import_debug.default)("vite:plugin-mock-dev-server");
56
56
  async function mockServerMiddleware(httpServer, config, options) {
57
57
  const include = isArray(options.include) ? options.include : [options.include];
58
58
  const includePaths = await (0, import_fast_glob.default)(include, { cwd: process.cwd() });
59
+ const external = await getExternal(process.cwd());
59
60
  const modules = /* @__PURE__ */ Object.create(null);
60
61
  let mockList;
61
62
  for (const filepath of includePaths) {
62
- modules[filepath] = await loadModule(filepath);
63
+ modules[filepath] = await loadModule(filepath, external);
63
64
  }
64
65
  setupMockList();
65
66
  debug("start watcher: ", include);
@@ -71,12 +72,12 @@ async function mockServerMiddleware(httpServer, config, options) {
71
72
  include.length > 0 && include.forEach((item) => watcher.add(item));
72
73
  watcher.on("add", async (filepath) => {
73
74
  debug("watcher add: ", filepath);
74
- modules[filepath] = await loadModule(filepath);
75
+ modules[filepath] = await loadModule(filepath, external);
75
76
  setupMockList();
76
77
  });
77
78
  watcher.on("change", async (filepath) => {
78
79
  debug("watcher change", filepath);
79
- modules[filepath] = await loadModule(filepath);
80
+ modules[filepath] = await loadModule(filepath, external);
80
81
  setupMockList();
81
82
  });
82
83
  watcher.on("unlink", (filepath) => {
@@ -201,13 +202,17 @@ async function parseReqBody(req) {
201
202
  }
202
203
  return await (0, import_co_body.default)(req);
203
204
  }
204
- async function loadModule(filepath) {
205
- const ext = import_node_path.default.extname(filepath);
206
- if (ext === ".ts") {
207
- const tsText = await import_promises.default.readFile(filepath, "utf-8");
208
- const { code } = await (0, import_vite.transformWithEsbuild)(tsText, filepath, {
205
+ async function loadModule(filepath, external) {
206
+ try {
207
+ const result = await (0, import_esbuild.build)({
208
+ entryPoints: [filepath],
209
+ outfile: "out.js",
210
+ write: false,
209
211
  target: "es2020",
210
212
  platform: "node",
213
+ bundle: true,
214
+ external,
215
+ metafile: true,
211
216
  format: "esm"
212
217
  });
213
218
  const tempFile = import_node_path.default.join(
@@ -217,16 +222,26 @@ async function loadModule(filepath) {
217
222
  );
218
223
  const tempBasename = import_node_path.default.dirname(tempFile);
219
224
  await import_promises.default.mkdir(tempBasename, { recursive: true });
220
- await import_promises.default.writeFile(tempFile, code, "utf8");
221
- return await loadESModule(tempFile);
225
+ await import_promises.default.writeFile(tempFile, result.outputFiles[0].text, "utf8");
226
+ const handle = await import(`${tempFile}?${Date.now()}`);
227
+ if (handle && handle.default)
228
+ return handle.default;
229
+ return Object.keys(handle || {}).map((key) => handle[key]);
230
+ } catch (e) {
231
+ console.error(e);
222
232
  }
223
- return await loadESModule(filepath);
233
+ return { url: "" };
224
234
  }
225
- async function loadESModule(filepath) {
226
- const handle = await import(`${filepath}?${Date.now()}`);
227
- if (handle && handle.default)
228
- return handle.default;
229
- return Object.keys(handle || {}).map((key) => handle[key]);
235
+ async function getExternal(cwd) {
236
+ const filepath = import_node_path.default.resolve(cwd, "package.json");
237
+ const content = await import_promises.default.readFile(filepath, "utf-8");
238
+ const pkg = JSON.parse(content);
239
+ const { dependencies = {}, devDependencies = {} } = pkg;
240
+ const external = [
241
+ ...Object.keys(dependencies),
242
+ ...Object.keys(devDependencies)
243
+ ];
244
+ return external;
230
245
  }
231
246
  function validate(request, validator) {
232
247
  return equalObj(request.headers, validator.headers) && equalObj(request.body, validator.body) && equalObj(request.params, validator.params) && equalObj(request.query, validator.query);
package/dist/index.js CHANGED
@@ -5,9 +5,9 @@ import { parse as urlParse } from "url";
5
5
  import chokidar from "chokidar";
6
6
  import bodyParser from "co-body";
7
7
  import Debug from "debug";
8
+ import { build } from "esbuild";
8
9
  import fastGlob from "fast-glob";
9
10
  import { match, pathToRegexp } from "path-to-regexp";
10
- import { transformWithEsbuild } from "vite";
11
11
 
12
12
  // src/utils.ts
13
13
  var isArray = (val) => Array.isArray(val);
@@ -22,10 +22,11 @@ var debug = Debug("vite:plugin-mock-dev-server");
22
22
  async function mockServerMiddleware(httpServer, config, options) {
23
23
  const include = isArray(options.include) ? options.include : [options.include];
24
24
  const includePaths = await fastGlob(include, { cwd: process.cwd() });
25
+ const external = await getExternal(process.cwd());
25
26
  const modules = /* @__PURE__ */ Object.create(null);
26
27
  let mockList;
27
28
  for (const filepath of includePaths) {
28
- modules[filepath] = await loadModule(filepath);
29
+ modules[filepath] = await loadModule(filepath, external);
29
30
  }
30
31
  setupMockList();
31
32
  debug("start watcher: ", include);
@@ -37,12 +38,12 @@ async function mockServerMiddleware(httpServer, config, options) {
37
38
  include.length > 0 && include.forEach((item) => watcher.add(item));
38
39
  watcher.on("add", async (filepath) => {
39
40
  debug("watcher add: ", filepath);
40
- modules[filepath] = await loadModule(filepath);
41
+ modules[filepath] = await loadModule(filepath, external);
41
42
  setupMockList();
42
43
  });
43
44
  watcher.on("change", async (filepath) => {
44
45
  debug("watcher change", filepath);
45
- modules[filepath] = await loadModule(filepath);
46
+ modules[filepath] = await loadModule(filepath, external);
46
47
  setupMockList();
47
48
  });
48
49
  watcher.on("unlink", (filepath) => {
@@ -167,13 +168,17 @@ async function parseReqBody(req) {
167
168
  }
168
169
  return await bodyParser(req);
169
170
  }
170
- async function loadModule(filepath) {
171
- const ext = path.extname(filepath);
172
- if (ext === ".ts") {
173
- const tsText = await fs.readFile(filepath, "utf-8");
174
- const { code } = await transformWithEsbuild(tsText, filepath, {
171
+ async function loadModule(filepath, external) {
172
+ try {
173
+ const result = await build({
174
+ entryPoints: [filepath],
175
+ outfile: "out.js",
176
+ write: false,
175
177
  target: "es2020",
176
178
  platform: "node",
179
+ bundle: true,
180
+ external,
181
+ metafile: true,
177
182
  format: "esm"
178
183
  });
179
184
  const tempFile = path.join(
@@ -183,16 +188,26 @@ async function loadModule(filepath) {
183
188
  );
184
189
  const tempBasename = path.dirname(tempFile);
185
190
  await fs.mkdir(tempBasename, { recursive: true });
186
- await fs.writeFile(tempFile, code, "utf8");
187
- return await loadESModule(tempFile);
191
+ await fs.writeFile(tempFile, result.outputFiles[0].text, "utf8");
192
+ const handle = await import(`${tempFile}?${Date.now()}`);
193
+ if (handle && handle.default)
194
+ return handle.default;
195
+ return Object.keys(handle || {}).map((key) => handle[key]);
196
+ } catch (e) {
197
+ console.error(e);
188
198
  }
189
- return await loadESModule(filepath);
199
+ return { url: "" };
190
200
  }
191
- async function loadESModule(filepath) {
192
- const handle = await import(`${filepath}?${Date.now()}`);
193
- if (handle && handle.default)
194
- return handle.default;
195
- return Object.keys(handle || {}).map((key) => handle[key]);
201
+ async function getExternal(cwd) {
202
+ const filepath = path.resolve(cwd, "package.json");
203
+ const content = await fs.readFile(filepath, "utf-8");
204
+ const pkg = JSON.parse(content);
205
+ const { dependencies = {}, devDependencies = {} } = pkg;
206
+ const external = [
207
+ ...Object.keys(dependencies),
208
+ ...Object.keys(devDependencies)
209
+ ];
210
+ return external;
196
211
  }
197
212
  function validate(request, validator) {
198
213
  return equalObj(request.headers, validator.headers) && equalObj(request.body, validator.body) && equalObj(request.params, validator.params) && equalObj(request.query, validator.query);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-dev-server",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/pengzhanbo/vite-plugin-mock-dev-server"
@@ -38,6 +38,7 @@
38
38
  "chokidar": "^3.5.3",
39
39
  "co-body": "^6.1.0",
40
40
  "debug": "^4.3.4",
41
+ "esbuild": "^0.15.12",
41
42
  "fast-glob": "^3.2.12",
42
43
  "path-to-regexp": "^6.2.1",
43
44
  "vite": "^3.2.0"