vite-plugin-mock-data 4.0.6 → 6.0.0

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 CHANGED
@@ -156,6 +156,7 @@ fetch('/package.json')
156
156
 
157
157
  ## Examples
158
158
 
159
- * [See vite3 demo](../../examples/vite3-mock-data)
160
- * [See vite4 demo](../../examples/vite4-mock-data)
161
- * [See vite5 demo](../../examples/vite5-mock-data)
159
+ * [See vite3 demo](../../examples/vite3-demo)
160
+ * [See vite4 demo](../../examples/vite4-demo)
161
+ * [See vite5 demo](../../examples/vite5-demo)
162
+ * [See vite6 demo](../../examples/vite6-demo)
package/dist/index.d.ts CHANGED
@@ -1,34 +1,6 @@
1
- import { Config as SirvConfig, HTTPVersion, RouteOptions, Handler } from 'find-my-way';
2
1
  import { Plugin } from 'vite';
3
- export interface HandleRoute {
4
- file?: string;
5
- handler?: any | Handler<HTTPVersion.V1>;
6
- options?: RouteOptions;
7
- store?: any;
8
- }
9
- export interface RouteConfig {
10
- [route: string]: string | Handler<HTTPVersion.V1> | HandleRoute;
11
- }
12
- export interface Options {
13
- /**
14
- * The directory to serve files from.
15
- * @default `process.cwd()`
16
- */
17
- cwd?: string;
18
- /**
19
- * If `true`, these mock routes is matched after internal middlewares are installed.
20
- * @default `false`
21
- */
22
- isAfter?: boolean;
23
- /** Specify the directory to define mock assets. */
24
- mockAssetsDir?: string;
25
- /** Initial options of `find-my-way`. see more at https://github.com/delvedor/find-my-way#findmywayoptions */
26
- mockRouterOptions?: SirvConfig<HTTPVersion.V1> | SirvConfig<HTTPVersion.V2>;
27
- /** Initial list of mock routes that should be added to the dev server. */
28
- mockRoutes?: RouteConfig | RouteConfig[];
29
- /** Specify the directory to define mock routes that should be added to the dev server. */
30
- mockRoutesDir?: string;
31
- }
2
+ import { Options } from './typings';
3
+ export * from './typings';
32
4
  /**
33
5
  * Provides a simple way to mock data.
34
6
  *
@@ -40,7 +12,7 @@ export interface Options {
40
12
  * export default defineConfig({
41
13
  * plugins: [
42
14
  * mockData({
43
- * mockRoutesDir: './mock'
15
+ * routes: './mock'
44
16
  * })
45
17
  * ]
46
18
  * });
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  const node_path = require("node:path");
3
3
  const node_module = require("node:module");
4
4
  const node_fs = require("node:fs");
5
- const globby = require("globby");
5
+ const tinyglobby = require("tinyglobby");
6
6
  const getRouter = require("find-my-way");
7
7
  const sirv = require("sirv");
8
8
  const vite = require("vite");
@@ -13,6 +13,31 @@ function isObject(val) {
13
13
  function toAbsolute(pth, cwd) {
14
14
  return node_path.isAbsolute(pth) ? pth : node_path.posix.join(cwd || process.cwd(), pth);
15
15
  }
16
+ async function getRoute(filename) {
17
+ let config;
18
+ switch (node_path.extname(filename)) {
19
+ case ".js":
20
+ config = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.js", document.baseURI).href)(filename);
21
+ break;
22
+ case ".mjs":
23
+ config = (await import(filename)).default;
24
+ break;
25
+ case ".json":
26
+ config = JSON.parse(node_fs.readFileSync(filename, "utf-8"));
27
+ break;
28
+ }
29
+ return config;
30
+ }
31
+ async function loadRoutes(dir, routes) {
32
+ const paths = await tinyglobby.glob(`${dir}/**/*.{js,mjs,json}`, { absolute: true });
33
+ const configs = await Promise.all(paths.map(getRoute));
34
+ configs.reduce((prev, cur) => {
35
+ if (cur) {
36
+ prev.push(cur);
37
+ }
38
+ return prev;
39
+ }, routes);
40
+ }
16
41
  function sirvOptions(headers) {
17
42
  return {
18
43
  dev: true,
@@ -81,47 +106,30 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
81
106
  });
82
107
  }
83
108
  function createPlugin(opts) {
84
- const { isAfter, mockRouterOptions, mockAssetsDir } = opts;
85
- let { cwd, mockRoutesDir } = opts;
86
- let mockRoutes = opts.mockRoutes || [];
87
- if (!cwd) {
88
- cwd = process.cwd();
89
- }
90
- if (isObject(mockRoutes) && !Array.isArray(mockRoutes)) {
91
- mockRoutes = [mockRoutes];
92
- }
109
+ const { isAfter, routerOptions, routes, assets, cwd = process.cwd() } = opts;
110
+ const allRoutes = [];
93
111
  return {
94
112
  name: "vite-plugin-mock-data",
95
113
  async configureServer(server) {
96
- if (mockRoutesDir) {
97
- mockRoutesDir = toAbsolute(mockRoutesDir, cwd);
98
- const paths = await globby.globby(`${mockRoutesDir}/**/*.{js,mjs,json}`);
99
- await Promise.all(paths.map((file) => {
100
- return (async () => {
101
- let config;
102
- switch (node_path.extname(file)) {
103
- case ".js":
104
- config = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.src || new URL("index.js", document.baseURI).href)(file);
105
- break;
106
- case ".mjs":
107
- config = (await import(file)).default;
108
- break;
109
- case ".json":
110
- config = JSON.parse(node_fs.readFileSync(file, "utf-8"));
111
- break;
112
- }
113
- if (config) {
114
- mockRoutes.push(config);
115
- }
116
- })();
117
- }));
114
+ if (typeof routes === "string") {
115
+ await loadRoutes(toAbsolute(routes, cwd), allRoutes);
116
+ } else if (Array.isArray(routes)) {
117
+ for (const route of routes) {
118
+ if (typeof route === "string") {
119
+ await loadRoutes(toAbsolute(route, cwd), allRoutes);
120
+ } else {
121
+ allRoutes.push(route);
122
+ }
123
+ }
124
+ } else if (isObject(routes)) {
125
+ allRoutes.push(routes);
118
126
  }
119
127
  let serve = null;
120
- if (mockAssetsDir) {
121
- serve = sirv(toAbsolute(mockAssetsDir, cwd), sirvOptions(server.config.server.headers));
128
+ if (assets) {
129
+ serve = sirv(toAbsolute(assets, cwd), sirvOptions(server.config.server.headers));
122
130
  }
123
- if (mockRoutes && mockRoutes.length > 0) {
124
- return isAfter ? () => configureServer(server, mockRouterOptions, mockRoutes, serve, cwd) : configureServer(server, mockRouterOptions, mockRoutes, serve, cwd);
131
+ if (allRoutes && allRoutes.length > 0) {
132
+ return isAfter ? () => configureServer(server, routerOptions, allRoutes, serve, cwd) : configureServer(server, routerOptions, allRoutes, serve, cwd);
125
133
  }
126
134
  }
127
135
  };
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { extname, isAbsolute, posix, parse } from "node:path";
1
+ import { posix, isAbsolute, extname, parse } from "node:path";
2
2
  import { createRequire } from "node:module";
3
3
  import { readFileSync } from "node:fs";
4
- import { globby } from "globby";
4
+ import { glob } from "tinyglobby";
5
5
  import getRouter from "find-my-way";
6
6
  import sirv from "sirv";
7
7
  import { send } from "vite";
@@ -11,6 +11,31 @@ function isObject(val) {
11
11
  function toAbsolute(pth, cwd) {
12
12
  return isAbsolute(pth) ? pth : posix.join(cwd || process.cwd(), pth);
13
13
  }
14
+ async function getRoute(filename) {
15
+ let config;
16
+ switch (extname(filename)) {
17
+ case ".js":
18
+ config = createRequire(import.meta.url)(filename);
19
+ break;
20
+ case ".mjs":
21
+ config = (await import(filename)).default;
22
+ break;
23
+ case ".json":
24
+ config = JSON.parse(readFileSync(filename, "utf-8"));
25
+ break;
26
+ }
27
+ return config;
28
+ }
29
+ async function loadRoutes(dir, routes) {
30
+ const paths = await glob(`${dir}/**/*.{js,mjs,json}`, { absolute: true });
31
+ const configs = await Promise.all(paths.map(getRoute));
32
+ configs.reduce((prev, cur) => {
33
+ if (cur) {
34
+ prev.push(cur);
35
+ }
36
+ return prev;
37
+ }, routes);
38
+ }
14
39
  function sirvOptions(headers) {
15
40
  return {
16
41
  dev: true,
@@ -79,47 +104,30 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
79
104
  });
80
105
  }
81
106
  function createPlugin(opts) {
82
- const { isAfter, mockRouterOptions, mockAssetsDir } = opts;
83
- let { cwd, mockRoutesDir } = opts;
84
- let mockRoutes = opts.mockRoutes || [];
85
- if (!cwd) {
86
- cwd = process.cwd();
87
- }
88
- if (isObject(mockRoutes) && !Array.isArray(mockRoutes)) {
89
- mockRoutes = [mockRoutes];
90
- }
107
+ const { isAfter, routerOptions, routes, assets, cwd = process.cwd() } = opts;
108
+ const allRoutes = [];
91
109
  return {
92
110
  name: "vite-plugin-mock-data",
93
111
  async configureServer(server) {
94
- if (mockRoutesDir) {
95
- mockRoutesDir = toAbsolute(mockRoutesDir, cwd);
96
- const paths = await globby(`${mockRoutesDir}/**/*.{js,mjs,json}`);
97
- await Promise.all(paths.map((file) => {
98
- return (async () => {
99
- let config;
100
- switch (extname(file)) {
101
- case ".js":
102
- config = createRequire(import.meta.url)(file);
103
- break;
104
- case ".mjs":
105
- config = (await import(file)).default;
106
- break;
107
- case ".json":
108
- config = JSON.parse(readFileSync(file, "utf-8"));
109
- break;
110
- }
111
- if (config) {
112
- mockRoutes.push(config);
113
- }
114
- })();
115
- }));
112
+ if (typeof routes === "string") {
113
+ await loadRoutes(toAbsolute(routes, cwd), allRoutes);
114
+ } else if (Array.isArray(routes)) {
115
+ for (const route of routes) {
116
+ if (typeof route === "string") {
117
+ await loadRoutes(toAbsolute(route, cwd), allRoutes);
118
+ } else {
119
+ allRoutes.push(route);
120
+ }
121
+ }
122
+ } else if (isObject(routes)) {
123
+ allRoutes.push(routes);
116
124
  }
117
125
  let serve = null;
118
- if (mockAssetsDir) {
119
- serve = sirv(toAbsolute(mockAssetsDir, cwd), sirvOptions(server.config.server.headers));
126
+ if (assets) {
127
+ serve = sirv(toAbsolute(assets, cwd), sirvOptions(server.config.server.headers));
120
128
  }
121
- if (mockRoutes && mockRoutes.length > 0) {
122
- return isAfter ? () => configureServer(server, mockRouterOptions, mockRoutes, serve, cwd) : configureServer(server, mockRouterOptions, mockRoutes, serve, cwd);
129
+ if (allRoutes && allRoutes.length > 0) {
130
+ return isAfter ? () => configureServer(server, routerOptions, allRoutes, serve, cwd) : configureServer(server, routerOptions, allRoutes, serve, cwd);
123
131
  }
124
132
  }
125
133
  };
@@ -0,0 +1,35 @@
1
+ import { Config as SirvConfig, HTTPVersion, RouteOptions, Handler } from 'find-my-way';
2
+ export interface HandleRoute {
3
+ file?: string;
4
+ handler?: any | Handler<HTTPVersion.V1>;
5
+ options?: RouteOptions;
6
+ store?: any;
7
+ }
8
+ export interface RouteConfig {
9
+ [route: string]: string | Handler<HTTPVersion.V1> | HandleRoute;
10
+ }
11
+ export interface Options {
12
+ /**
13
+ * The directory to serve files from.
14
+ * @default `process.cwd()`
15
+ */
16
+ cwd?: string;
17
+ /**
18
+ * If `true`, these mock routes is matched after internal middlewares are installed.
19
+ * @default `false`
20
+ */
21
+ isAfter?: boolean;
22
+ /**
23
+ * Specify the directory to define mock assets.
24
+ */
25
+ assets?: string;
26
+ /**
27
+ * Initial options of `find-my-way`. see more at https://github.com/delvedor/find-my-way#findmywayoptions
28
+ */
29
+ routerOptions?: SirvConfig<HTTPVersion.V1> | SirvConfig<HTTPVersion.V2>;
30
+ /**
31
+ * Initial list of mock routes that should be added to the dev server
32
+ * or specify the directory to define mock routes that should be added to the dev server.
33
+ */
34
+ routes?: RouteConfig | Array<RouteConfig | string> | string;
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-data",
3
- "version": "4.0.6",
3
+ "version": "6.0.0",
4
4
  "description": "Provides a simple way to mock data.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "scripts": {
20
20
  "release": "npm publish",
21
- "build": "vite build",
21
+ "build:lib": "vite build",
22
22
  "watch": "vite build --watch"
23
23
  },
24
24
  "repository": {
@@ -37,9 +37,9 @@
37
37
  },
38
38
  "homepage": "https://github.com/fengxinming/vite-plugins#readme",
39
39
  "dependencies": {
40
- "find-my-way": "^7.6.2",
41
- "globby": "^13.2.2",
42
- "sirv": "^2.0.2"
40
+ "find-my-way": "^9.2.0",
41
+ "sirv": "^3.0.1",
42
+ "tinyglobby": "^0.2.12"
43
43
  },
44
44
  "files": [
45
45
  "dist"