From 3f958fb1f7b5edf7c83f0f6e224553730889775f Mon Sep 17 00:00:00 2001 From: eiinu Date: Thu, 7 Dec 2023 20:07:17 +0800 Subject: [PATCH] refactor(button): script setup --- scripts/generate-nutui-taro-vue.cjs | 20 ++- scripts/generate-nutui.cjs | 22 ++- scripts/generate-types-taro.cjs | 29 +++- scripts/generate-types.cjs | 29 +++- src/config.json | 1 + src/packages/__VUE/button/index.taro.ts | 11 ++ src/packages/__VUE/button/index.taro.vue | 162 +++++++++--------- src/packages/__VUE/button/index.ts | 11 ++ src/packages/__VUE/button/index.vue | 153 ++++++++--------- .../__VUE/button/{type.ts => types.ts} | 0 src/packages/utils/index.ts | 1 + src/packages/utils/with-install.ts | 16 ++ tsconfig.declaration.json | 8 +- tsconfig.declaration.taro.json | 7 +- vite.config.build.disperse.ts | 6 +- vite.config.build.taro.vue.disperse.ts | 10 +- 16 files changed, 276 insertions(+), 210 deletions(-) create mode 100644 src/packages/__VUE/button/index.taro.ts create mode 100644 src/packages/__VUE/button/index.ts rename src/packages/__VUE/button/{type.ts => types.ts} (100%) create mode 100644 src/packages/utils/with-install.ts diff --git a/scripts/generate-nutui-taro-vue.cjs b/scripts/generate-nutui-taro-vue.cjs index a98f37d3a6..b5e11054f7 100644 --- a/scripts/generate-nutui-taro-vue.cjs +++ b/scripts/generate-nutui-taro-vue.cjs @@ -11,14 +11,20 @@ declare module 'vue' { const packages = []; config.nav.map((item) => { item.packages.forEach((element) => { - let { name, exclude, taro } = element; + let { name, exclude, taro, setup } = element; if (taro == true) { - dts += ` Nut${name}: typeof import('@/packages/__VUE/${name.toLowerCase()}/index.taro.vue')['default']\n` - const filePath = path.join(`src/packages/__VUE/${name.toLowerCase()}/index.taro.vue`); - if (name !== 'Icon') { - importStr += `import ${name} from './__VUE/${name.toLowerCase()}/index${ - fs.existsSync(filePath) ? '.taro' : '' - }.vue';\n`; + if (setup === true) { + dts += ` Nut${name}: typeof import('@/packages/__VUE/${name.toLowerCase()}/index.taro')['default']\n` + importStr += `import ${name} from './__VUE/${name.toLowerCase()}/index.taro';\n`; + importStr += `export * from './__VUE/${name.toLowerCase()}/index.taro';\n`; + } else { + dts += ` Nut${name}: typeof import('@/packages/__VUE/${name.toLowerCase()}/index.taro.vue')['default']\n` + const filePath = path.join(`src/packages/__VUE/${name.toLowerCase()}/index.taro.vue`); + if (name !== 'Icon') { + importStr += `import ${name} from './__VUE/${name.toLowerCase()}/index${ + fs.existsSync(filePath) ? '.taro' : '' + }.vue';\n`; + } } importScssStr += `import './__VUE/${name.toLowerCase()}/index.scss';\n`; if (exclude != true) { diff --git a/scripts/generate-nutui.cjs b/scripts/generate-nutui.cjs index bb2e89aba0..49fea83374 100644 --- a/scripts/generate-nutui.cjs +++ b/scripts/generate-nutui.cjs @@ -12,14 +12,20 @@ const packages = []; const methods = []; config.nav.map((item) => { item.packages.forEach((element) => { - let { name, type, exclude } = element; - dts += ` Nut${name}: typeof import('./__VUE/${name.toLowerCase()}/index.vue')['default']\n` - if (name !== 'Icon') { - importStr += `import ${name} from './__VUE/${name.toLowerCase()}/index.vue';\n`; - } - if (type === 'methods') { - importStr += `import { show${name} } from './__VUE/${name.toLowerCase()}/index';\n`; - methods.push(`show${name}`); + let { name, type, exclude, setup } = element; + if (setup === true) { + dts += ` Nut${name}: typeof import('./__VUE/${name.toLowerCase()}/index')['default']\n` + importStr += `import ${name} from './__VUE/${name.toLowerCase()}/index';\n`; + importStr += `export * from './__VUE/${name.toLowerCase()}/index';\n`; + } else { + dts += ` Nut${name}: typeof import('./__VUE/${name.toLowerCase()}/index.vue')['default']\n` + if (name !== 'Icon') { + importStr += `import ${name} from './__VUE/${name.toLowerCase()}/index.vue';\n`; + } + if (type === 'methods') { + importStr += `import { show${name} } from './__VUE/${name.toLowerCase()}/index';\n`; + methods.push(`show${name}`); + } } importScssStr += `import './__VUE/${name.toLowerCase()}/index.scss';\n`; if (exclude != true) { diff --git a/scripts/generate-types-taro.cjs b/scripts/generate-types-taro.cjs index 6e3b551526..aa9aca2e42 100644 --- a/scripts/generate-types-taro.cjs +++ b/scripts/generate-types-taro.cjs @@ -44,7 +44,13 @@ const getCompName = (name) => { }); } const packageName = packages.find((item) => item.name.toLowerCase() === name.toLowerCase()); - return packageName ? packageName.name : ''; + if (packageName) { + if (packageName?.setup === true) { + return [packageName.name, true] + } + return [packageName.name, false] + } + return '' }; const getLocale = () => { @@ -101,16 +107,23 @@ fs.cp(sourceDir, toDir, { recursive: true }, (err) => { if (inputs && inputs.length) { let name = item.substring(0, item.lastIndexOf('/')); name = name.substring(name.lastIndexOf('/') + 1); - const componentName = getCompName(name); - if (componentName) { + const _ComponentName = getCompName(name); + if (_ComponentName) { + const [componentName, setup] = _ComponentName; let remain = ` declare module 'vue' { - interface GlobalComponents { - Nut${componentName}: typeof _default; - } + interface GlobalComponents { + Nut${componentName}: typeof _default; + } }`; - let changeContent = content.replace(regex, `${preContent}${start} Install<${inputs[1]}>${end}${remain}`); - fs.writeFileSync(item, changeContent); + if (setup) { + let changeContent = content.replace('export default _default;', `declare const _nut_defualt: WithInstall;\nexport default _nut_defualt;\n${remain}`); + changeContent = `import type { WithInstall } from '../../utils';\n` + changeContent + fs.writeFileSync(item, changeContent); + } else { + let changeContent = content.replace(regex, `${preContent}${start} Install<${inputs[1]}>${end}${remain}`); + fs.writeFileSync(item, changeContent); + } } } }); diff --git a/scripts/generate-types.cjs b/scripts/generate-types.cjs index e744357c02..a5cb49ec09 100644 --- a/scripts/generate-types.cjs +++ b/scripts/generate-types.cjs @@ -44,7 +44,13 @@ const getCompName = (name) => { }); } const packageName = packages.find((item) => item.name.toLowerCase() === name.toLowerCase()); - return packageName ? packageName.name : ''; + if (packageName) { + if (packageName?.setup === true) { + return [packageName.name, true] + } + return [packageName.name, false] + } + return '' }; const getLocale = () => { @@ -101,16 +107,23 @@ fs.cp(sourceDir, toDir, { recursive: true }, (err) => { if (inputs && inputs.length) { let name = item.substring(0, item.lastIndexOf('/')); name = name.substring(name.lastIndexOf('/') + 1); - const componentName = getCompName(name); - if (componentName) { + const _ComponentName = getCompName(name); + if (_ComponentName) { + const [componentName, setup] = _ComponentName; let remain = ` declare module 'vue' { - interface GlobalComponents { - Nut${componentName}: typeof _default; - } + interface GlobalComponents { + Nut${componentName}: typeof _default; + } }`; - let changeContent = content.replace(regex, `${preContent}${start} Install<${inputs[1]}>${end}${remain}`); - fs.writeFileSync(item, changeContent); + if (setup) { + let changeContent = content.replace('export default _default;', `declare const _nut_defualt: WithInstall;\nexport default _nut_defualt;\n${remain}`); + changeContent = `import type { WithInstall } from '../../utils';\n` + changeContent + fs.writeFileSync(item, changeContent); + } else { + let changeContent = content.replace(regex, `${preContent}${start} Install<${inputs[1]}>${end}${remain}`); + fs.writeFileSync(item, changeContent); + } } } }); diff --git a/src/config.json b/src/config.json index 49972068b5..3e77882ae0 100644 --- a/src/config.json +++ b/src/config.json @@ -80,6 +80,7 @@ "cName": "按钮", "type": "component", "show": true, + "setup": true, "desc": "按钮用于触发一个操作,如提交表单。", "author": "richard1015" }, diff --git a/src/packages/__VUE/button/index.taro.ts b/src/packages/__VUE/button/index.taro.ts new file mode 100644 index 0000000000..b300dd3c3b --- /dev/null +++ b/src/packages/__VUE/button/index.taro.ts @@ -0,0 +1,11 @@ +import Button from './index.taro.vue'; +import type { ComponentPublicInstance } from 'vue'; +import { withInstall } from '@/packages/utils'; + +withInstall(Button); + +export type { ButtonType, ButtonSize, ButtonShape, ButtonFormType } from './types'; + +export type ButtonInstance = ComponentPublicInstance & InstanceType; + +export { Button, Button as default }; diff --git a/src/packages/__VUE/button/index.taro.vue b/src/packages/__VUE/button/index.taro.vue index 1845bb284b..cfff16cfee 100644 --- a/src/packages/__VUE/button/index.taro.vue +++ b/src/packages/__VUE/button/index.taro.vue @@ -16,98 +16,94 @@ - diff --git a/src/packages/__VUE/button/index.ts b/src/packages/__VUE/button/index.ts new file mode 100644 index 0000000000..c0b9142754 --- /dev/null +++ b/src/packages/__VUE/button/index.ts @@ -0,0 +1,11 @@ +import Button from './index.vue'; +import type { ComponentPublicInstance } from 'vue'; +import { withInstall } from '@/packages/utils'; + +withInstall(Button); + +export type { ButtonType, ButtonSize, ButtonShape, ButtonFormType } from './types'; + +export type ButtonInstance = ComponentPublicInstance & InstanceType; + +export { Button, Button as default }; diff --git a/src/packages/__VUE/button/index.vue b/src/packages/__VUE/button/index.vue index b582188a2b..ebc0959dc2 100644 --- a/src/packages/__VUE/button/index.vue +++ b/src/packages/__VUE/button/index.vue @@ -10,92 +10,89 @@ - diff --git a/src/packages/__VUE/button/type.ts b/src/packages/__VUE/button/types.ts similarity index 100% rename from src/packages/__VUE/button/type.ts rename to src/packages/__VUE/button/types.ts diff --git a/src/packages/utils/index.ts b/src/packages/utils/index.ts index d28bcc615f..0f2897866d 100644 --- a/src/packages/utils/index.ts +++ b/src/packages/utils/index.ts @@ -1 +1,2 @@ export * from './useRelation'; +export * from './with-install'; diff --git a/src/packages/utils/with-install.ts b/src/packages/utils/with-install.ts new file mode 100644 index 0000000000..8b8dac4aa0 --- /dev/null +++ b/src/packages/utils/with-install.ts @@ -0,0 +1,16 @@ +import type { App, Component } from 'vue'; + +export type WithInstall = T & { + install(app: App): void; +}; + +export const withInstall = (comp: T) => { + const _comp = comp as WithInstall; + _comp.install = (vue: App) => { + if (_comp.name) { + vue.component(_comp.name, _comp); + } + }; + + return _comp as WithInstall; +}; diff --git a/tsconfig.declaration.json b/tsconfig.declaration.json index 3c729a5d7e..304348f82d 100644 --- a/tsconfig.declaration.json +++ b/tsconfig.declaration.json @@ -6,18 +6,12 @@ "emitDeclarationOnly": true }, "include": [ - "src/**/*.d.ts", - "src/packages/__VUE/**/index.vue", - "src/packages/__VUE/**/*.ts", "src/packages/nutui.vue.build.ts", "src/packages/locale", "src/packages/utils", "src/packages/resolver" ], "exclude": [ - "node_modules", - "**/test/**/*", - "**/demo/**/*", - "src/packages/__VUE/**/__tests__/" + "node_modules" ] } diff --git a/tsconfig.declaration.taro.json b/tsconfig.declaration.taro.json index 74de1b2874..31706beb43 100644 --- a/tsconfig.declaration.taro.json +++ b/tsconfig.declaration.taro.json @@ -6,17 +6,12 @@ "emitDeclarationOnly": true }, "include": [ - "src/packages/__VUE/**/index.taro.vue", - "src/packages/__VUE/**/*.ts", "src/packages/nutui.taro.vue.build.ts", "src/packages/locale", "src/packages/utils", "src/packages/resolver" ], "exclude": [ - "node_modules", - "**/test/**/*", - "**/demo/**/*", - "src/packages/__VUE/**/__tests__/" + "node_modules" ] } diff --git a/vite.config.build.disperse.ts b/vite.config.build.disperse.ts index b2c17a9bfa..695589b143 100644 --- a/vite.config.build.disperse.ts +++ b/vite.config.build.disperse.ts @@ -7,9 +7,11 @@ let input = {}; configPkg.nav.map((item) => { item.packages.forEach((element) => { - let { name, type, exclude } = element; + const { name, type, exclude, setup } = element; if (exclude != true) { - input[name] = `./src/packages/__VUE/${name.toLowerCase()}/index${type === 'methods' ? '.ts' : '.vue'}`; + input[name] = `./src/packages/__VUE/${name.toLowerCase()}/index${ + type === 'methods' || setup === true ? '.ts' : '.vue' + }`; } }); }); diff --git a/vite.config.build.taro.vue.disperse.ts b/vite.config.build.taro.vue.disperse.ts index d8e9459ff2..5342265561 100644 --- a/vite.config.build.taro.vue.disperse.ts +++ b/vite.config.build.taro.vue.disperse.ts @@ -8,11 +8,15 @@ const input = {}; configPkg.nav.map((item) => { item.packages.forEach((element) => { - const { name, exclude, taro } = element; + const { name, exclude, taro, setup } = element; if (taro === false) return; // 排除非 Taro 组件 if (exclude != true) { - const filePath = path.join(`./src/packages/__VUE/${name.toLowerCase()}/index.taro.vue`); - input[name] = `./src/packages/__VUE/${name.toLowerCase()}/index${fs.existsSync(filePath) ? '.taro' : ''}.vue`; + if (setup === true) { + input[name] = `./src/packages/__VUE/${name.toLowerCase()}/index.taro.ts`; + } else { + const filePath = path.join(`./src/packages/__VUE/${name.toLowerCase()}/index.taro.vue`); + input[name] = `./src/packages/__VUE/${name.toLowerCase()}/index${fs.existsSync(filePath) ? '.taro' : ''}.vue`; + } } // } });