-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
HOMEDO\zhangjiayun
committed
Jul 14, 2022
1 parent
b727792
commit ed57d51
Showing
2 changed files
with
84 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# vite-plugin-autogeneration-import-file | ||
vite 自动生成 引入文件插件 | ||
|
||
支持vite2 和 vite^3.0.0-beta | ||
|
||
## 快速开始 | ||
1. 安装 | ||
- vite2: | ||
`npm i vite-plugin-autogeneration-import-file@">=1.0.0 < 2.0.0" -D` | ||
|
||
- vite3: | ||
`npm i vite-plugin-autogeneration-import-file@">=2.0.0 < 3.0.0" -D` | ||
|
||
2. vite.config.js中使用 | ||
|
||
``` | ||
import {default as autogenerationImportFile,getName} from 'vite-plugin-autogeneration-import-file'; | ||
import { defineConfig } from 'vite' | ||
export default defineConfig({ | ||
root:'./index.html', | ||
plugins: [autogenerationImportFile([ | ||
{ | ||
pattern:['**/*.{ts,js}','*.{ts,js}'], | ||
dir:'test/store/modules', | ||
toFile:'test/store/module.ts', | ||
name:(name)=>{ | ||
name = getName(name); | ||
return name[0].toUpperCase()+name.slice(1)+'Store'; | ||
} | ||
}, | ||
{ | ||
pattern:['**/{Index.vue,index.ts,index.js}','*.{vue,ts,js}'], | ||
dir:'test/components', | ||
toFile:'test/types/components.d.ts', | ||
template:'//import code\ndeclare module "@vue/runtime-core" {\n interface GlobalComponents {\n //key code\n }\n}\nexport {};', | ||
codeTemplates:[ | ||
{key:'//import code\n',template:'import {{name}} from "{{path}}"\n'}, | ||
{key:' //key code\n',template:' {{name}}:typeof {{name}}\n'}, | ||
] | ||
} | ||
])] | ||
}); | ||
``` | ||
|
||
## 插件配置说明(dirOptions) | ||
``` | ||
interface codeTemplate { //代码模板 | ||
key: string, //标识符 | ||
template: string,//模板 codeTemplate.template里的{{name}}会被替换为name {{path}}会被替换为被导入的相对路径 | ||
value?: string //根据模板自动生成,不可传入 | ||
} | ||
type dirOptions = { //插件配置 | ||
dir: string,//遍历路径 | ||
toFile: string,//写入目标文件地址 | ||
pattern: fg.Pattern | fg.Pattern[],//匹配规则 参考 fast-glob | ||
options?: fg.Options,//fast-glob 匹配参数 | ||
name?: string | ((fileName:string)=>string),//名称 当为字符串时里面的{{name}}会被替换为格式化后的驼峰名称, 默认值为:"{{name}}" | ||
codeTemplates?: codeTemplate[] //代码模板 默认值为:"[{key: '//code',template: 'export { default as {{name}} } from "{{path}}"\n'}]" | ||
template?: string//文件模板 会递归循环codeTemplates把template里的codeTemplate.key替换为对应的codeTemplate.value 默认值为:"当前文件由vite-plugin-autogeneration-import-file自动生成\n//code" | ||
}[] | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters