Skip to content

Commit

Permalink
feat: add supportTs options,Support es6 and commonjs modules in .js f…
Browse files Browse the repository at this point in the history
…older
  • Loading branch information
anncwb committed Sep 14, 2020
1 parent 58ad7cd commit 37f83f5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default {
```ts
{
mockPath?: string;
supportTs?: boolean;
ignore?: RegExp | ((fileName: string) => boolean);
watchFiles?: boolean;
localEnabled?: boolean;
Expand All @@ -66,6 +67,12 @@ If `watchFiles:true`, the file changes in the folder will be monitored. And sync

If configPath has a value, it is invalid

### supportTs

**default:** true

After opening, the ts file module can be read. Note that you will not be able to monitor .js files after opening.

### ignore

**default:** undefined
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vite-plugin-mock",
"name": "vite-plugin-mock-link",
"version": "1.0.1",
"description": "A mock plugin for vite",
"main": "dist/index.js",
Expand Down
14 changes: 6 additions & 8 deletions src/createMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function createMockServer(
mockPath: 'mock',
ignoreFiles: [],
watchFiles: true,
supportTs: true,
configPath: 'vite.mock.config.ts',
...opt,
};
Expand Down Expand Up @@ -64,11 +65,8 @@ function createWatch(opt: CreateMock) {
const watchDir = [];
const exitsConfigPath = existsSync(absConfigPath);

if (exitsConfigPath && configPath) {
watchDir.push(absConfigPath);
} else {
watchDir.push(absMockPath);
}
exitsConfigPath && configPath ? watchDir.push(absConfigPath) : watchDir.push(absMockPath);

const watcher = chokidar.watch(watchDir, {
ignoreInitial: true,
});
Expand Down Expand Up @@ -96,15 +94,15 @@ function cleanRequireCache(opt: CreateMock) {
async function getMockConfig(opt: CreateMock) {
cleanRequireCache(opt);
const { absConfigPath, absMockPath } = getPath(opt);
const { ignoreFiles = [], ignore, configPath } = opt;
const { ignoreFiles = [], ignore, configPath, supportTs } = opt;
let ret: any[] = [];
if (configPath && existsSync(absConfigPath)) {
console.log(chalk.blue(`[vite:mock-server]:load mock data from ${absConfigPath}`));
let resultModule = await resolveModule(absConfigPath);
ret = resultModule;
} else {
const mockFiles = glob
.sync('**/*.ts', {
.sync(`**/*.${supportTs ? 'ts' : 'js'}`, {
cwd: absMockPath,
ignore: ignoreFiles,
})
Expand Down Expand Up @@ -172,7 +170,7 @@ async function resolveModule(path: string): Promise<any> {
} catch (error) {}
}

// get custom config file and mock dir path
// get custom config file path and mock dir path
function getPath(opt: CreateMock) {
const { mockPath, configPath } = opt;
const cwd = process.cwd();
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface CreateMock {
ignore?: RegExp | ((fileName: string) => boolean);
watchFiles?: boolean;
localEnabled?: boolean;
supportTs?: boolean;
}

export type MethodType = 'get' | 'post' | 'put' | 'delete' | 'patch';
Expand All @@ -13,5 +14,5 @@ export declare interface MockMethod {
url: string;
method?: MethodType;
timeout?: number;
response: ((opt: { [key: string]: string; body: any; query: any }) => any) | object;
response: ((opt: { body: any; query: any }) => any) | object;
}

0 comments on commit 37f83f5

Please sign in to comment.