Skip to content

Commit d038cb6

Browse files
committed
init: init
0 parents  commit d038cb6

11 files changed

Lines changed: 3690 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
es

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 1.0.0-beta.1 (2020-10-27)
2+
3+
4+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020-present, Vben
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# vite-plugin-mock
2+
3+
Provide local and prod mocks for vite.
4+
5+
A mock plugin for vite, developed based on mockjs. And support the local environment and production environment at the same time. Koa service middleware is used locally, mockjs is used online
6+
7+
## Getting Started
8+
9+
### Install (yarn or npm)
10+
11+
**node version:** >=12.0.0
12+
13+
`yarn add mockjs` or `yarn add mockjs -S`
14+
15+
and
16+
17+
`yarn add vite-plugin-mock -D` or `npm i vite-plugin-mock -D`
18+
19+
**Run Example**
20+
21+
```bash
22+
23+
# ts example
24+
cd ./examples/ts-examples
25+
26+
yarn install
27+
28+
yarn serve
29+
30+
# js example
31+
32+
cd ./examples/js-examples
33+
34+
yarn install
35+
36+
yarn serve
37+
```
38+
39+
## Usage
40+
41+
**Development environment**
42+
43+
The development environment is implemented using koa middleware。
44+
45+
Different from the production environment, you can view the network request record in the Google Chrome console
46+
47+
- Config plugin in vite.config.ts
48+
49+
```ts
50+
import { createMockServer } from 'vite-plugin-mock';
51+
52+
export default {
53+
plugins: [
54+
createMockServer({
55+
ignore: /^\_/,
56+
mockPath: 'mock',
57+
watchFiles: true,
58+
localEnabled: process.env.NODE_ENV === 'development',
59+
}),
60+
],
61+
};
62+
```
63+
64+
- createMockServer Options
65+
66+
```ts
67+
{
68+
mockPath?: string;
69+
supportTs?: boolean;
70+
ignore?: RegExp | ((fileName: string) => boolean);
71+
watchFiles?: boolean;
72+
localEnabled?: boolean;
73+
ignoreFiles?: string[];
74+
configPath?: string;
75+
}
76+
```
77+
78+
## Option description
79+
80+
### mockPath
81+
82+
**default:** true
83+
84+
Set the folder where the mock .ts file is stored
85+
86+
If `watchFiles:true`, the file changes in the folder will be monitored. And synchronize to the request result in real time
87+
88+
If configPath has a value, it is invalid
89+
90+
### supportTs
91+
92+
**default:** true
93+
94+
After opening, the ts file module can be read. Note that you will not be able to monitor .js files after opening.
95+
96+
### ignore
97+
98+
**default:** undefined
99+
100+
Ignore files in the specified format when automatically reading mock .ts files
101+
102+
### watchFiles
103+
104+
**default:** true
105+
106+
Set whether to monitor changes in mock .ts files
107+
108+
### localEnabled
109+
110+
**default:** process.env.NODE_ENV==='developments'
111+
112+
Set whether to enable the local mock .ts file, do not open it in the production environment
113+
114+
### configPath
115+
116+
**default:** vite.mock.config.ts
117+
118+
Set the data entry that the mock reads. When the file exists and is located in the project root directory, the file will be read and used first. The configuration file returns an array
119+
120+
### ignoreFiles
121+
122+
**default:** []
123+
124+
The project uses glob to read the folder set by mockPath. This parameter is used as the parameter of the glob module
125+
126+
## Mock file example
127+
128+
`/path/mock`
129+
130+
```ts
131+
// test.ts
132+
133+
import { MockMethod } from 'vite-plugin-mock';
134+
export default [
135+
{
136+
url: '/api/get',
137+
method: 'get',
138+
response: ({ query }) => {
139+
return {
140+
code: 0,
141+
data: {
142+
name: 'vben'
143+
}
144+
};
145+
},
146+
},
147+
{
148+
url: '/api/post',
149+
method: 'post',
150+
timeout:2000,
151+
response: {
152+
code: 0,
153+
data: {
154+
name: 'vben'
155+
}
156+
};
157+
},
158+
] as MockMethod[];
159+
160+
161+
```
162+
163+
### MockMethod
164+
165+
```ts
166+
{
167+
// request url
168+
url: string;
169+
// request method
170+
method?: MethodType;
171+
// Request time in milliseconds
172+
timeout?: number;
173+
// response data
174+
response: ((opt: { [key: string]: string; body: any; query: any }) => any) | any;
175+
}
176+
177+
```
178+
179+
## Usage in Production environment
180+
181+
### Example
182+
183+
Create a new mockProdServer.ts file
184+
185+
```ts
186+
// mockProdServer.ts
187+
188+
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer';
189+
190+
// Import your mock .ts files one by one
191+
// If you use vite.mock.config.ts, just import the file directly
192+
import testModule from '../mock/test';
193+
194+
export function setupProdMockServer() {
195+
createProdMockServer([...testModule]);
196+
}
197+
```
198+
199+
In mian.ts
200+
201+
```ts
202+
// src/main.ts
203+
204+
import { setupProdMockServer } from './mockProdServer';
205+
206+
if (process.env.NODE_ENV === 'production') {
207+
setupProdMockServer();
208+
}
209+
```
210+
211+
## Note
212+
213+
The node module cannot be used in the mock .ts file, otherwise the production environment will fail
214+
215+
## License
216+
217+
MIT

package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "vite-plugin-html",
3+
"version": "1.0.0-beta.1",
4+
"description": "A simple vite plugin. It is developed based on lodash template",
5+
"main": "dist/index.js",
6+
"files": [
7+
"dist"
8+
],
9+
"scripts": {
10+
"dev": "rimraf dist && rimraf es && tsc -w --p tsconfig.json ",
11+
"build": "rimraf dist && rimraf es && tsc -p tsconfig.json",
12+
"prepublishOnly": "yarn build",
13+
"log": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
14+
},
15+
"keywords": [
16+
"vite",
17+
"html",
18+
"hmr"
19+
],
20+
"author": "Vben",
21+
"license": "MIT",
22+
"repository": {
23+
"type": "git",
24+
"url": "https://github.com/anncwb/vite-plugin-html"
25+
},
26+
"bugs": {
27+
"url": "https://github.com/anncwb/vite-plugin-html/issues"
28+
},
29+
"homepage": "https://github.com/anncwb/vite-plugin-html/tree/master/#readme",
30+
"dependencies": {
31+
"html-minifier-terser": "^5.1.1",
32+
"lodash": "^4.17.20"
33+
},
34+
"peerDependencies": {
35+
"vite": ">=1.0.0-rc.8"
36+
},
37+
"devDependencies": {
38+
"@types/html-minifier-terser": "^5.1.1",
39+
"@types/lodash": "^4.14.162",
40+
"@types/node": "^14.0.3",
41+
"conventional-changelog-cli": "^2.1.0",
42+
"prettier": "^2.1.2",
43+
"rimraf": "^3.0.2",
44+
"typescript": "^4.0.3",
45+
"vite": "^1.0.0-rc.8"
46+
}
47+
}

prettier.config.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
printWidth: 100,
3+
tabWidth: 2,
4+
useTabs: false,
5+
semi: true,
6+
vueIndentScriptAndStyle: true,
7+
singleQuote: true,
8+
quoteProps: 'as-needed',
9+
bracketSpacing: true,
10+
trailingComma: 'es5',
11+
jsxBracketSameLine: false,
12+
jsxSingleQuote: false,
13+
arrowParens: 'always',
14+
insertPragma: false,
15+
requirePragma: false,
16+
proseWrap: 'never',
17+
htmlWhitespaceSensitivity: 'strict',
18+
endOfLine: 'lf',
19+
rangeStart: 0,
20+
overrides: [
21+
{
22+
files: '*.md',
23+
options: {
24+
tabWidth: 2,
25+
},
26+
},
27+
],
28+
};

src/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Plugin } from 'vite';
2+
import { VitePluginHtml } from './types';
3+
import { template, isBoolean } from 'lodash';
4+
import { injectTitle } from './utils';
5+
import { minify as HtmlMinify, Options } from 'html-minifier-terser';
6+
7+
const defaultMinifyOptions: Options = {
8+
minifyCSS: true,
9+
minifyJS: true,
10+
minifyURLs: true,
11+
removeAttributeQuotes: true,
12+
trimCustomFragments: true,
13+
collapseWhitespace: true,
14+
};
15+
16+
export default (opt: VitePluginHtml = {}): Plugin => {
17+
return {
18+
indexHtmlTransforms: [
19+
{
20+
apply: 'pre',
21+
transform: ({ code }) => {
22+
const { options = {} } = opt;
23+
try {
24+
const compiled = template(code);
25+
return compiled({
26+
viteHtmlPluginOptions: options,
27+
});
28+
} catch (error) {
29+
console.warn('[vite-plugin-html:pre]:Template Compiled Faild\n' + error);
30+
return code;
31+
}
32+
},
33+
},
34+
{
35+
apply: 'post',
36+
transform: ({ code }) => {
37+
const { title = '', minify } = opt;
38+
try {
39+
let processCode = injectTitle(code, title);
40+
if (!minify) {
41+
return processCode;
42+
}
43+
if (isBoolean(minify)) {
44+
return HtmlMinify(processCode, defaultMinifyOptions);
45+
}
46+
return HtmlMinify(processCode, {
47+
...defaultMinifyOptions,
48+
...minify,
49+
});
50+
} catch (error) {
51+
console.warn('[vite-plugin-html:post]:Template Compiled Faild\n' + error);
52+
return code;
53+
}
54+
},
55+
},
56+
],
57+
};
58+
};
59+
60+
export * from './types';

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Options } from 'html-minifier-terser';
2+
3+
export interface VitePluginHtml {
4+
title?: string;
5+
minify?: boolean | Options;
6+
options?: {
7+
[key: string]: any;
8+
};
9+
}

0 commit comments

Comments
 (0)