Skip to content

Commit 9263021

Browse files
committed
feat: add vite-plugin-http-request-mock
1 parent 3329350 commit 9263021

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
### Changelog
2+
[2023-02-12]
3+
1. feat: add vite-plugin-http-request-mock
4+
25
[2023-01-30]
36
1. feat: add @requestHeaders for @remote config
47

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-request-mock",
3-
"version": "1.8.11",
3+
"version": "1.8.12",
44
"description": "Intercept & mock http requests issued by XMLHttpRequest, fetch, nodejs https/http module, axios, jquery, superagent, ky, node-fetch, request, got or any other request libraries by intercepting XMLHttpRequest, fetch and nodejs native requests in low level.",
55
"main": "src/index.js",
66
"module": "http-request-mock.esm.mjs",

tool/plugin/vite.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* eslint-env node */
2+
const path = require('path');
3+
const fs = require('fs');
4+
const { getAppRoot, resolve, formatPath } = require('../lib/misc');
5+
const pluginName = 'vite-plugin-http-request-mock';
6+
7+
/**
8+
* Use regexp to test against path which treats '\' as '/' on windows.
9+
*
10+
* @param {RegExp} regexp
11+
* @param {string} assetFile
12+
*/
13+
const testPath = (regexp, assetFile) => {
14+
return regexp.test(assetFile) || (process.platform === 'win32' && regexp.test(formatPath(assetFile)));
15+
};
16+
17+
/**
18+
* @param {regexp} appEntry Required, app entry file which mock dependencies will be injected into.
19+
* @param {string} mockDir Required, mock directory which contains all mock files & the runtime mock config entry file.
20+
* @param {boolean} enable Optional, whether or not to enable this plugin. Default: true
21+
* @param {boolean} debug Optional, output some debug logs. Default: false
22+
*/
23+
const vitePluginHttpRequestMock = ({ appEntry, mockDir, enable = true, debug = false}) => {
24+
if (!(appEntry instanceof RegExp)) {
25+
throw new Error(`${pluginName} expects [appEntry] to be a valid RegExp Object.`);
26+
}
27+
28+
const absoluteDir = resolve(path.isAbsolute(mockDir) ? mockDir : path.resolve(getAppRoot(), mockDir));
29+
if (!mockDir || !fs.existsSync(absoluteDir)) {
30+
throw new Error(`${pluginName} expects [mockDir] to be a valid directory.`);
31+
}
32+
const runtimeFile = resolve(absoluteDir, '.runtime.js');
33+
if (!fs.existsSync(runtimeFile)) {
34+
throw new Error(`${pluginName} can not find the runtime mock config entry file: ${runtimeFile}.`);
35+
}
36+
37+
return {
38+
name: pluginName,
39+
transform(code, id) {
40+
if (!enable) return;
41+
42+
const match = testPath(appEntry, id);
43+
if (debug) {
44+
console.log(`${appEntry.toString()} -> ${id} ${match ? '-> hit!!!' : ''}`);
45+
}
46+
if (match) {
47+
return `import "${runtimeFile}"; // runtime mock config entry file.\n${code}`;
48+
}
49+
},
50+
};
51+
};
52+
53+
module.exports = vitePluginHttpRequestMock;

0 commit comments

Comments
 (0)