Skip to content

Commit

Permalink
feat: 自动按需生成 api 预设
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Apr 23, 2023
1 parent 41835d1 commit 5095cb1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"unplugin-vue-macros": "^2.0.0",
"unplugin-vue-router": "^0.5.5",
"vite": "^4.3.1",
"vite-auto-import-resolvers": "^3.0.5",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-inspect": "^0.7.24",
"vite-plugin-md": "^0.22.5",
Expand Down
48 changes: 48 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions presets/eslint/.eslintrc-auto-import.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
"Ref": true,
"VNode": true,
"acceptHMRUpdate": true,
"afterAll": true,
"afterEach": true,
"assert": true,
"asyncComputed": true,
"autoResetRef": true,
"beforeAll": true,
"beforeEach": true,
"chai": true,
"computed": true,
"computedAsync": true,
"computedEager": true,
Expand All @@ -37,8 +43,10 @@
"defineLoader": true,
"definePage": true,
"defineStore": true,
"describe": true,
"eagerComputed": true,
"effectScope": true,
"expect": true,
"extendRef": true,
"getActivePinia": true,
"getCurrentInstance": true,
Expand All @@ -51,6 +59,7 @@
"isReactive": true,
"isReadonly": true,
"isRef": true,
"it": true,
"makeDestructurable": true,
"mapActions": true,
"mapGetters": true,
Expand Down Expand Up @@ -102,9 +111,11 @@
"shallowReadonly": true,
"shallowRef": true,
"storeToRefs": true,
"suite": true,
"syncRef": true,
"syncRefs": true,
"templateRef": true,
"test": true,
"throttledRef": true,
"throttledWatch": true,
"toRaw": true,
Expand Down Expand Up @@ -283,6 +294,8 @@
"useWindowFocus": true,
"useWindowScroll": true,
"useWindowSize": true,
"vi": true,
"vitest": true,
"watch": true,
"watchArray": true,
"watchAtMost": true,
Expand Down
5 changes: 3 additions & 2 deletions presets/plugins/html.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Plugin } from 'vite'

export const GenerateTitle = (): Plugin => {
export function GenerateTitle(): Plugin {
let title: string
const reg = /<title>(.*?)<\/title>/
return {
name: 'vite-plugin-env-to-generate-title',
configResolved(config) {
title = config.env.VITE_APP_TITLE
},
transformIndexHtml(html) {
return html.replace(/<title>(.*?)<\/title>/, `<title>${title}</title>`)
return html.replace(reg, `<title>${title}</title>`)
},
}
}
13 changes: 6 additions & 7 deletions presets/plugins/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { env } from './../shared/env'
import { env } from '../shared/env'
import Prism from 'markdown-it-prism'
import Markdown from 'vite-plugin-md'
import { default as _Markdown } from 'vite-plugin-md'

export const markdownWrapperClasses = env.VITE_APP_MARKDOWN
? 'prose md:prose-lg lg:prose-lg dark:prose-invert text-left p-10 prose-slate prose-img:rounded-xl prose-headings:underline prose-a:text-blue-600'
: ''

export default () => {
return (
env.VITE_APP_MARKDOWN &&
Markdown({
export default function Markdown() {
if (env.VITE_APP_MARKDOWN) {
return _Markdown({
wrapperClasses: markdownWrapperClasses,
markdownItSetup(md) {
md.use(Prism)
},
})
)
}
}
13 changes: 5 additions & 8 deletions presets/tov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import viteCompression from 'vite-plugin-compression'
import { markdownWrapperClasses } from './plugins/markdown'
import { VueRouterAutoImports } from 'unplugin-vue-router'
import I18N from '@intlify/unplugin-vue-i18n/vite'
import { AutoGenerateImports } from 'vite-auto-import-resolvers'

import {
ArcoResolver,
Expand Down Expand Up @@ -127,16 +128,12 @@ export default () => {
// api 自动按需引入
env.VITE_APP_API_AUTO_IMPORT &&
AutoImport({
dirs: [
env.VITE_APP_API_AUTO_IMPORT && 'src/stores/**/*.ts',
env.VITE_APP_API_AUTO_IMPORT && 'src/composables/**/*.ts',
],
dirs: env.VITE_APP_DIR_API_AUTO_IMPORT
? ['src/stores/**/*.ts', 'src/composables/**/*.ts']
: undefined,
dts: './presets/types/auto-imports.d.ts',
imports: [
'vue',
'pinia',
'vue-i18n',
'@vueuse/core',
...AutoGenerateImports({ exclude: ['vue-router'] }),
VueRouterAutoImports,
],
resolvers: AutoImportResolvers,
Expand Down
13 changes: 13 additions & 0 deletions presets/types/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ export {}
declare global {
const EffectScope: typeof import('vue')['EffectScope']
const acceptHMRUpdate: typeof import('pinia')['acceptHMRUpdate']
const afterAll: typeof import('vitest')['afterAll']
const afterEach: typeof import('vitest')['afterEach']
const assert: typeof import('vitest')['assert']
const asyncComputed: typeof import('@vueuse/core')['asyncComputed']
const autoResetRef: typeof import('@vueuse/core')['autoResetRef']
const beforeAll: typeof import('vitest')['beforeAll']
const beforeEach: typeof import('vitest')['beforeEach']
const chai: typeof import('vitest')['chai']
const computed: typeof import('vue')['computed']
const computedAsync: typeof import('@vueuse/core')['computedAsync']
const computedEager: typeof import('@vueuse/core')['computedEager']
Expand All @@ -34,8 +40,10 @@ declare global {
const defineLoader: typeof import('vue-router/auto')['defineLoader']
const definePage: typeof import('unplugin-vue-router/runtime')['_definePage']
const defineStore: typeof import('pinia')['defineStore']
const describe: typeof import('vitest')['describe']
const eagerComputed: typeof import('@vueuse/core')['eagerComputed']
const effectScope: typeof import('vue')['effectScope']
const expect: typeof import('vitest')['expect']
const extendRef: typeof import('@vueuse/core')['extendRef']
const getActivePinia: typeof import('pinia')['getActivePinia']
const getCurrentInstance: typeof import('vue')['getCurrentInstance']
Expand All @@ -48,6 +56,7 @@ declare global {
const isReactive: typeof import('vue')['isReactive']
const isReadonly: typeof import('vue')['isReadonly']
const isRef: typeof import('vue')['isRef']
const it: typeof import('vitest')['it']
const makeDestructurable: typeof import('@vueuse/core')['makeDestructurable']
const mapActions: typeof import('pinia')['mapActions']
const mapGetters: typeof import('pinia')['mapGetters']
Expand Down Expand Up @@ -99,9 +108,11 @@ declare global {
const shallowReadonly: typeof import('vue')['shallowReadonly']
const shallowRef: typeof import('vue')['shallowRef']
const storeToRefs: typeof import('pinia')['storeToRefs']
const suite: typeof import('vitest')['suite']
const syncRef: typeof import('@vueuse/core')['syncRef']
const syncRefs: typeof import('@vueuse/core')['syncRefs']
const templateRef: typeof import('@vueuse/core')['templateRef']
const test: typeof import('vitest')['test']
const throttledRef: typeof import('@vueuse/core')['throttledRef']
const throttledWatch: typeof import('@vueuse/core')['throttledWatch']
const toRaw: typeof import('vue')['toRaw']
Expand Down Expand Up @@ -280,6 +291,8 @@ declare global {
const useWindowFocus: typeof import('@vueuse/core')['useWindowFocus']
const useWindowScroll: typeof import('@vueuse/core')['useWindowScroll']
const useWindowSize: typeof import('@vueuse/core')['useWindowSize']
const vi: typeof import('vitest')['vi']
const vitest: typeof import('vitest')['vitest']
const watch: typeof import('vue')['watch']
const watchArray: typeof import('@vueuse/core')['watchArray']
const watchAtMost: typeof import('@vueuse/core')['watchAtMost']
Expand Down

0 comments on commit 5095cb1

Please sign in to comment.