Skip to content

Commit

Permalink
chore(presets): 添加插件注释
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Aug 20, 2023
1 parent ec61807 commit dd17fab
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[toml]": {
"editor.defaultFormatter": "bodil.prettier-toml"
Expand Down
100 changes: 82 additions & 18 deletions presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
VueUseComponentsResolver,
} from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import Markdown from 'unplugin-vue-markdown/vite'
import { VueRouterAutoImports } from 'unplugin-vue-router'
import Router from 'unplugin-vue-router/vite'
import { fileURLToPath } from 'url'
Expand All @@ -37,7 +38,6 @@ import { viteMockServe as Mock } from 'vite-plugin-mock'
import Removelog from 'vite-plugin-removelog'
import Modules from 'vite-plugin-use-modules'
import VueDevTools from 'vite-plugin-vue-devtools'
import Markdown from 'unplugin-vue-markdown/vite'
import Layouts from 'vite-plugin-vue-meta-layouts'
import { warmup as Warmup } from 'vite-plugin-warmup'

Expand All @@ -54,7 +54,10 @@ export default function () {
const safelist =
'prose px-2 sm:px-0 md:prose-lg lg:prose-lg dark:prose-invert text-left w-screen prose-slate prose-img:rounded-xl prose-headings:underline prose-a:text-blue-600'
const plugins = [
// 兼容不支持 esmModule 的浏览器
/**
* 兼容不支持 esmModule 的浏览器
* https://www.npmjs.com/package/@vitejs/plugin-legacy
*/
Legacy({
targets: [
'>= 0.25%',
Expand All @@ -65,38 +68,65 @@ export default function () {
'iOS >= 8',
],
}),
/**
* 环境变量类型提示
* https://github.com/dishait/vite-plugin-env-types
*/
EnvTypes({
dts: 'presets/types/env.d.ts',
}),
// https://github.com/bluwy/vite-plugin-warmup (依赖预热,加快渲染,未来可能会内置到 vite 中)
/**
* 依赖预热,加快渲染 (未来可能会内置到 vite 中)
* https://github.com/bluwy/vite-plugin-warmup
*/
Warmup({
clientFiles: ['./src/**/*'],
}),
// https://github.com/posva/unplugin-vue-router
/**
* 文件路由
* https://github.com/posva/unplugin-vue-router
*/
Router({
routesFolder: 'src/pages',
extensions: ['.md', '.vue', '.tsx', '.jsx'],
dts: 'presets/types/type-router.d.ts',
}),
// 模块自动加载
/**
* 自动安装 vue 插件
* https://github.com/dishait/vite-plugin-use-modules
*/
Modules({
auto: true,
target: 'src/plugins',
}),
// vue 官方插件,用来解析 sfc
/**
* vue 官方插件,用来解析 sfc 单文件组件
* https://www.npmjs.com/package/@vitejs/plugin-vue
*/
Vue({
include: [/\.vue$/, /\.md$/],
}),
// 布局系统
/**
* 布局系统
* https://github.com/dishait/vite-plugin-vue-meta-layouts
*/
Layouts(),
// 调试工具
/**
* 开发面板
* https://github.com/webfansplz/vite-plugin-vue-devtools
*/
env.VITE_APP_DEV_TOOLS && VueDevTools(),
// mock 服务
/**
* mock 服务
* https://github.com/vbenjs/vite-plugin-mock
*/
Mock({
prodEnabled: env.VITE_APP_MOCK_IN_PRODUCTION,
}),

// 组件自动按需引入
/**
* 组件自动按需引入
* https://github.com/antfu/unplugin-vue-components
*/
Components({
directoryAsNamespace: true,
include: [/\.vue$/, /\.vue\?vue/, /\.[tj]sx$/, /\.md$/],
Expand Down Expand Up @@ -130,31 +160,53 @@ export default function () {
],
}),
}),
// i18n 国际化支持
/**
* i18n 国际化支持
* https://www.npmjs.com/package/@intlify/unplugin-vue-i18n
*/
I18N({
runtimeOnly: false,
compositionOnly: true,
include: ['locales/**'],
}),
// jsx 和 tsx 支持
/**
* jsx 和 tsx 支持
* https://www.npmjs.com/package/@vitejs/plugin-vue-jsx
*/
Jsx(),
// 生产环境资源压缩
/**
* 生产环境资源压缩
* https://github.com/vbenjs/vite-plugin-compression
*/
Compression({
// @ts-ignore
algorithm: env.VITE_APP_COMPRESSINON_ALGORITHM,
}),
// 生产环境下移除 console.log, console.warn, console.error
/**
* 生产环境下移除 console.log, console.warn, console.error
* https://github.com/dishait/vite-plugin-removelog
*/
process.env.NODE_ENV !== 'debug' && Removelog(),
// 别名插件
/**
* 别名插件 (内置)
* 支持 `~` 和 `@` 别名到 `src`
*/
Alias(),
/**
* 强制重启 (内置)
* 如果 package.json 或 pnpm-lock.yaml 更新的话,强制重启
*/
ForceRestart(),
]

if (env.VITE_APP_API_AUTO_IMPORT) {
const dirs = env.VITE_APP_DIR_API_AUTO_IMPORT
? ['src/stores/**', 'src/composables/**', 'src/api/**']
: undefined
// api 自动按需引入
/**
* api 自动按需引入
* https://github.com/antfu/unplugin-auto-import
*/
plugins.push(
AutoImport({
dirs,
Expand Down Expand Up @@ -182,6 +234,10 @@ export default function () {
}

if (env.VITE_APP_MARKDOWN) {
/**
* markdown 渲染插件
* https://github.com/mdit-vue/unplugin-vue-markdown
*/
plugins.push(
Markdown({
wrapperClasses: safelist,
Expand All @@ -191,6 +247,10 @@ export default function () {
}),
)
}
/**
* css 原子引擎
* https://github.com/unocss/unocss
*/
plugins.push(
// @ts-ignore
UnoCss({
Expand Down Expand Up @@ -264,7 +324,10 @@ export const _dirname =
? __dirname
: dirname(fileURLToPath(import.meta.url))

// 别名插件
/**
* 别名插件
* @description 支持 `~` 和 `@` 别名到 `src`
*/
function Alias(): Plugin {
const src = resolve(_dirname, '../src')
return {
Expand All @@ -288,6 +351,7 @@ function Alias(): Plugin {

/**
* 强制重启
* @description 如果 package.json 或 pnpm-lock.yaml 更新的话,强制重启项目
*/
function ForceRestart(paths = ['package.json', 'pnpm-lock.yaml']): Plugin {
const restart = debounce(async function touch() {
Expand Down

0 comments on commit dd17fab

Please sign in to comment.