vite-plugin-mock-server
Advanced tools
Comparing version 1.1.2 to 1.3.0
@@ -39,4 +39,5 @@ /// <reference types="node" /> | ||
middlewares?: MockLayer[]; | ||
printStartupLog?: boolean; | ||
}; | ||
declare const _default: (options?: MockOptions) => Plugin; | ||
export default _default; |
@@ -7,4 +7,5 @@ import AntPathMatcher from '@howiefh/ant-path-matcher'; | ||
const PLUGIN_NAME = 'vite-plugin-mock-server'; | ||
const TEMPORARY_FILE_SUFFIX = '.tmp.js'; | ||
const TEMPORARY_FILE_SUFFIX = '.tmp.cjs'; | ||
let LOG_LEVEL = 'error'; | ||
const requireCache = new Map(); | ||
export default (options) => { | ||
@@ -24,2 +25,3 @@ return { | ||
options.noHandlerResponse404 = options.noHandlerResponse404 || true; | ||
options.printStartupLog = options.printStartupLog || true; | ||
if (options.mockModules && options.mockModules.length > 0) { | ||
@@ -32,7 +34,17 @@ console.warn('[' + PLUGIN_NAME + '] mock modules will be set automatically, and the configuration will be ignored', options.mockModules); | ||
watchMockFiles(options).then(() => { | ||
console.log('[' + PLUGIN_NAME + '] mock server started. options =', options); | ||
if (options.printStartupLog) { | ||
console.log('[' + PLUGIN_NAME + '] mock server started. options =', options); | ||
} | ||
}); | ||
if (options.middlewares) { | ||
for (const [, layer] of options.middlewares.entries()) { | ||
server.middlewares.use(layer); | ||
server.middlewares.use((req, res, next) => { | ||
const hasMatch = options.urlPrefixes.some((prefix) => req.url.startsWith(prefix)); | ||
if (hasMatch) { | ||
layer(req, res, next); | ||
} | ||
else { | ||
next(); | ||
} | ||
}); | ||
} | ||
@@ -46,2 +58,12 @@ } | ||
}; | ||
async function importCache(modName) { | ||
const mod = await import('file://' + modName); | ||
let module; | ||
if (mod.default && mod.default.default) | ||
module = mod.default; | ||
else | ||
module = mod; | ||
requireCache.set(modName, module); | ||
return module; | ||
} | ||
const doHandle = async (options, matcher, req, res, next) => { | ||
@@ -52,3 +74,3 @@ for (const [, prefix] of options.urlPrefixes.entries()) { | ||
for (const [, modName] of options.mockModules.entries()) { | ||
const module = require.cache[modName]; | ||
const module = requireCache.get(modName); | ||
if (!module) { | ||
@@ -59,3 +81,3 @@ continue; | ||
if (modName.endsWith(TEMPORARY_FILE_SUFFIX)) { | ||
const exports = module.exports.default; | ||
const exports = module.default; | ||
logInfo('typeof exports', typeof exports); | ||
@@ -70,3 +92,3 @@ if (typeof exports === 'function') { | ||
else { | ||
handlers = module.exports; | ||
handlers = module.default; | ||
} | ||
@@ -111,3 +133,3 @@ for (const [, handler] of handlers.entries()) { | ||
if (event === 'unlinkDir') { | ||
for (const [, modName] of Object.keys(require.cache).entries()) { | ||
for (const modName of [...requireCache.keys()]) { | ||
if (modName.startsWith(watchDir)) { | ||
@@ -162,3 +184,3 @@ await deleteMockModule(options, modName); | ||
logInfo('loading js mock module', moduleName); | ||
const handlers = require(moduleName); | ||
const handlers = await importCache(moduleName); | ||
if (!moduleName.endsWith(TEMPORARY_FILE_SUFFIX)) { | ||
@@ -202,3 +224,3 @@ logInfo('loaded mock handlers', handlers); | ||
logInfo('delete module cache', moduleName); | ||
delete require.cache[moduleName]; | ||
requireCache.delete(moduleName); | ||
for (const [i, modName] of options.mockModules.entries()) { | ||
@@ -205,0 +227,0 @@ if (modName === moduleName) { |
{ | ||
"name": "vite-plugin-mock-server", | ||
"version": "1.1.2", | ||
"version": "1.3.0", | ||
"description": "Vite mock server plugin", | ||
@@ -8,3 +8,3 @@ "main": "dist/index.js", | ||
"dev": "tsc -w -p .", | ||
"build": "rm -rf dist && tsc -p ." | ||
"build": "del-cli dist && tsc -p ." | ||
}, | ||
@@ -33,2 +33,3 @@ "repository": { | ||
"@types/node": "^15.12.4", | ||
"del-cli": "^5.1.0", | ||
"typescript": "^4.3.4", | ||
@@ -35,0 +36,0 @@ "vite": "^2.3.8" |
@@ -61,2 +61,16 @@ # vite-plugin-mock-server | ||
- Or just use it with the default parameters, place your mocks in the folder "mock" with name that prefix *.mock.ts or *mock.js, The default api to mock is '/api/' | ||
```ts | ||
import { defineConfig } from 'vite' | ||
import vue from '@vitejs/plugin-vue' | ||
import mockServer from 'vite-plugin-mock-server' | ||
export default defineConfig({ | ||
plugins: [ | ||
vue(), | ||
mockServer()) | ||
] | ||
}) | ||
``` | ||
## Module exports | ||
@@ -78,2 +92,3 @@ | ||
middlewares?: MockLayer[] | ||
printStartupLog?: boolean | ||
} | ||
@@ -90,3 +105,4 @@ | ||
mockModules: [], | ||
middlewares: [] | ||
middlewares: [], | ||
printStartupLog: true | ||
} | ||
@@ -182,3 +198,11 @@ ``` | ||
res.setHeader('Content-Type', 'application/json') | ||
res.end(JSON.stringify(req.body)) | ||
//req is incomingMessage which extends stream.Readable | ||
// --> https://nodejs.org/api/stream.html#readablereadsize | ||
// res.end need to be within the function | ||
// there is a size limit for the bodyString to get parsed | ||
req.on('data', (bodyString: string) => { | ||
let body: object = JSON.parse(bodyString) | ||
res.end(JSON.stringify(body)) | ||
}) | ||
} | ||
@@ -204,2 +228,3 @@ }, | ||
handle: (req, res) => { | ||
res.statusCode = 203 | ||
res.end('Hello world!' + req.url) | ||
@@ -206,0 +231,0 @@ } |
@@ -10,4 +10,5 @@ import { Plugin, ViteDevServer, Connect } from 'vite' | ||
const PLUGIN_NAME = 'vite-plugin-mock-server' | ||
const TEMPORARY_FILE_SUFFIX = '.tmp.js' | ||
const TEMPORARY_FILE_SUFFIX = '.tmp.cjs' | ||
let LOG_LEVEL = 'error' | ||
const requireCache = new Map<string, any>() | ||
@@ -52,2 +53,3 @@ type Request = Connect.IncomingMessage & { | ||
middlewares?: MockLayer[] | ||
printStartupLog?: boolean | ||
} | ||
@@ -70,2 +72,3 @@ | ||
options.noHandlerResponse404 = options.noHandlerResponse404 || true | ||
options.printStartupLog = options.printStartupLog || true | ||
if (options.mockModules && options.mockModules.length > 0) { | ||
@@ -78,7 +81,16 @@ console.warn('[' + PLUGIN_NAME + '] mock modules will be set automatically, and the configuration will be ignored', options.mockModules) | ||
watchMockFiles(options).then(() => { | ||
console.log('[' + PLUGIN_NAME + '] mock server started. options =', options) | ||
if (options.printStartupLog) { | ||
console.log('[' + PLUGIN_NAME + '] mock server started. options =', options) | ||
} | ||
}) | ||
if (options.middlewares) { | ||
for (const [, layer] of options.middlewares.entries()) { | ||
server.middlewares.use(layer); | ||
server.middlewares.use((req, res, next) => { | ||
const hasMatch = options.urlPrefixes.some((prefix) => req.url.startsWith(prefix)) | ||
if (hasMatch) { | ||
layer(req, res, next) | ||
} else { | ||
next() | ||
} | ||
}); | ||
} | ||
@@ -97,2 +109,14 @@ } | ||
async function importCache(modName: string) { | ||
const mod = await import('file://' + modName) | ||
let module | ||
if (mod.default && mod.default.default) | ||
module = mod.default | ||
else | ||
module = mod | ||
requireCache.set(modName, module) | ||
return module | ||
} | ||
const doHandle = async ( | ||
@@ -108,3 +132,4 @@ options: MockOptions, | ||
for (const [, modName] of options.mockModules.entries()) { | ||
const module = require.cache[modName] | ||
const module = requireCache.get(modName) | ||
if (!module) { | ||
@@ -115,3 +140,3 @@ continue | ||
if (modName.endsWith(TEMPORARY_FILE_SUFFIX)) { | ||
const exports = module.exports.default | ||
const exports = module.default | ||
logInfo('typeof exports', typeof exports) | ||
@@ -124,4 +149,5 @@ if (typeof exports === 'function') { | ||
} else { | ||
handlers = module.exports | ||
handlers = module.default | ||
} | ||
for (const [, handler] of handlers.entries()) { | ||
@@ -165,3 +191,3 @@ const [path, qs] = req.url.split('?') | ||
if (event === 'unlinkDir') { | ||
for (const [, modName] of Object.keys(require.cache).entries()) { | ||
for (const modName of [...requireCache.keys()]) { | ||
if (modName.startsWith(watchDir)) { | ||
@@ -213,3 +239,3 @@ await deleteMockModule(options, modName) | ||
logInfo('loading js mock module', moduleName) | ||
const handlers = require(moduleName) | ||
const handlers = await importCache(moduleName) | ||
if (!moduleName.endsWith(TEMPORARY_FILE_SUFFIX)) { | ||
@@ -251,3 +277,3 @@ logInfo('loaded mock handlers', handlers) | ||
logInfo('delete module cache', moduleName) | ||
delete require.cache[moduleName] | ||
requireCache.delete(moduleName) | ||
for (const [i, modName] of options.mockModules.entries()) { | ||
@@ -254,0 +280,0 @@ if (modName === moduleName) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
37001
572
291
1
4