refactor_options-API-composition-API-correspondence.md 4.7 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# hello uvue 选项式 API 与 组合式 API 对应重构

## 文件目录逻辑

按 API 类型划分文件夹,每个文件夹下有多个示例或 API\
每个示例或 API 有一个文件夹,文件夹名为示例或 API 名称\
每个文件夹下有 xxx-options.uvue 和 xxx-composition.uvue,分别对应选项式 API 示例和组合式 API 示例
<!-- template 和 style 通过 src 引入,两种 API 示例共用 -->

## 代码规范

不要有空的style, script\
script 中不要有空的 data, onLoad, methods\
DCloud-WZF's avatar
DCloud-WZF 已提交
14 15
缩进使用两个空格\
公共 css class 维护在 styles/common.css 中
DCloud-WZF's avatar
DCloud-WZF 已提交
16

DCloud-WZF's avatar
DCloud-WZF 已提交
17 18 19 20 21 22 23
## GoGoCode 

一个用于进行代码转换的 vscode 插件,在本项目中,可用于将 options API 写法转为 composition setup API 写法

### transform function

目前仅处理了 script 节点
DCloud-WZF's avatar
DCloud-WZF 已提交
24 25 26 27
已知问题:
- 无法处理函数返回值类型
- 无法处理函数换行

DCloud-WZF's avatar
DCloud-WZF 已提交
28 29 30
```js
function transform(fileInfo, api) {
  const $ = api.gogocode
DCloud-WZF's avatar
DCloud-WZF 已提交
31 32
  let source = fileInfo.source
  source = source.replace(/<script/g, "<script setup");
DCloud-WZF's avatar
DCloud-WZF 已提交
33
  const ast = $(source, { parseOptions: { language: 'vue' } })
DCloud-WZF's avatar
DCloud-WZF 已提交
34
  const script = ast.find('<script setup></script>')
DCloud-WZF's avatar
DCloud-WZF 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48

  script.replace('components:{},', '').replace('mixins:[]', '')

  script
    .find('data(){return {}},')
    .replace('$_$:$_$', 'const $_$ = ref($_$)')
    .replace('data(){return {$$$}}', '$$$')

  script
    .find('computed:{}')
    .replace('$_$(){$$$}', 'const $_$ = computed(() => {$$$})')

  script
    .find('watch:{}')
DCloud-WZF's avatar
DCloud-WZF 已提交
49 50 51 52
    .replace(
      '$_$:{handler($_$){$$$}}',
      'watch(() => $_$,($_$) => {$$$})'
    )
DCloud-WZF's avatar
DCloud-WZF 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66
    .replace('$_$:{handler(){$$$}}', 'watch(() => $_$,() => {$$$})')
    .replace(
      "'$_$':{handler($_$){$$$},deep: true}",
      'watch(() => $_$,($_$) => {$$$},{deep: true})'
    )
    .replace(
      "'$_$':{handler($_$){$$$}}",
      'watch(() => $_$,($_$) => {$$$})'
    )
    .replace('$_$($_$){$$$}', 'watch(() => $_$,($_$) => {$$$})')
    .replace('$_$(){$$$}', 'watch(() => $_$,() => {$$$})')
    .replace('watch:{$$$}', '$$$')

  script
DCloud-WZF's avatar
DCloud-WZF 已提交
67 68 69 70 71 72
    .replace('onLoad(){$$$}', 'onLoad(() => {$$$})')
    .replace('onShow(){$$$}', 'onShow(() => {$$$})')
    .replace('onReady(){$$$}', 'onReady(() => {$$$})')
    .replace('onHide(){$$$}', 'onHide(() => {$$$})')
    .replace('onUnload(){$$$}', 'onUnload(() => {$$$})')
    .replace('onBackPress(){$$$}', 'onBackPress(() => {$$$})')
DCloud-WZF's avatar
DCloud-WZF 已提交
73 74 75 76 77 78 79 80 81 82
    .replace('created(){$$$}', 'onBeforeMount(() => {$$$})')
    .replace('mounted(){$$$}', 'onMounted(() => {$$$})')
    .replace('beforeUnmount(){$$$}', 'onBeforeUnmount(() => {$$$})')
    .replace('unmounted(){$$$}', 'onUnmounted(() => {$$$})')
    .replace('beforeDestroy(){$$$}', 'onBeforeUnmount(() => {$$$})')
    .replace('destoryed(){$$$}', 'onUnmounted(() => {$$$})')

  script
    .find('methods:{}')
    .replace(
DCloud-WZF's avatar
DCloud-WZF 已提交
83 84
      'async $_$($$$0){$$$1}',
      'const $_$ = async ($$$0) => {$$$1}'
DCloud-WZF's avatar
DCloud-WZF 已提交
85
    )
DCloud-WZF's avatar
DCloud-WZF 已提交
86
    .replace('$_$($$$0){$$$1}', 'const $_$ = ($$$0) => {$$$1}')
DCloud-WZF's avatar
DCloud-WZF 已提交
87 88 89 90 91 92 93 94 95 96
    .replace('async $_$(){$$$}', 'const $_$ = async () => {$$$}')
    .replace('$_$(){$$$}', 'const $_$ = () => {$$$}')
    .replace('methods:{$$$}', '$$$')

  script.replace('export default {$$$}', '$$$')

  return ast.generate()
}
```

DCloud-WZF's avatar
DCloud-WZF 已提交
97 98 99 100
## app instance

- [x] app.component
- [x] app.globalProperties  app.globalProperties
DCloud-WZF's avatar
DCloud-WZF 已提交
101
- [x] app.use               app.use
DCloud-WZF's avatar
DCloud-WZF 已提交
102 103

## component instance
DCloud-WZF's avatar
DCloud-WZF 已提交
104 105
- [x] attrs useAttrs
- [x] data
DCloud-WZF's avatar
DCloud-WZF 已提交
106
- [x] props defineProps
DCloud-WZF's avatar
DCloud-WZF 已提交
107
- [x] el
108
- [x] options defineOptions
109
- [x] parent
DCloud-WZF's avatar
DCloud-WZF 已提交
110 111 112 113 114 115 116 117
- [x] root  root
- [x] slots defineSlots useSlots
- [x] refs  ref
- [x] emit defineEmits
- [x] forceUpdate
- [x] methods
- [x] provide  provide
- [x] inject   inject
DCloud-WZF's avatar
DCloud-WZF 已提交
118 119 120 121 122
- [x] mixins
- [x] nextTick
- [x] options API setup
- [x] defineExpose
- [x] circular reference
DCloud-WZF's avatar
DCloud-WZF 已提交
123 124

## reactivity
125 126 127 128 129 130 131
- [x] ref
- [x] reactive
- [x] computed computed
- [x] watch    watch
- [x] watchEffect
- [x] watchPostEffect
- [x] watchSyncEffect
DCloud-WZF's avatar
DCloud-WZF 已提交
132 133 134
- [x] readonly

- [x] isProxy
DCloud-WZF's avatar
DCloud-WZF 已提交
135
- [x] isReactive
DCloud-WZF's avatar
DCloud-WZF 已提交
136 137 138 139 140 141 142
- [ ] isReadonly
- [ ] isRef
- [ ] toRef
- [ ] toRefs
- [ ] toValue
- [ ] unRef

DCloud-WZF's avatar
DCloud-WZF 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
- [ ] customRef
- [ ] effectScope
- [ ] getCurrentScope
- [ ] markRaw
- [ ] onScopeDispose
- [ ] shallowReactive
- [ ] shallowReadonly
- [ ] shallowRef
- [ ] toRaw
- [ ] triggerRef

## directives

- [ ] v-bind      v-bind
- [ ] v-for       v-for
- [ ] v-if        v-if
- [ ] v-model     defineModel
- [ ] v-on        v-on
- [ ] v-show      v-show
- [ ] v-slot      v-slot
DCloud-WZF's avatar
DCloud-WZF 已提交
163 164 165 166
- [ ] v-once      v-once
- [ ] v-memo      v-memo
- [ ] v-pre       v-pre
- [ ] v-html      v-html
DCloud-WZF's avatar
DCloud-WZF 已提交
167 168 169

## lifecycle

DCloud-WZF's avatar
DCloud-WZF 已提交
170 171
- [x] component
- [x] page
DCloud-WZF's avatar
DCloud-WZF 已提交
172 173 174

## built-in components

DCloud-WZF's avatar
DCloud-WZF 已提交
175 176
- [x] keepAlive keepAlive
- [x] teleport  teleport 
DCloud-WZF's avatar
DCloud-WZF 已提交
177 178 179

## template

DCloud-WZF's avatar
DCloud-WZF 已提交
180 181
- [x] nested-component-communication
- [x] set-custom-child-component-root-node-class
DCloud-WZF's avatar
DCloud-WZF 已提交
182 183 184

## rendering

DCloud-WZF's avatar
DCloud-WZF 已提交
185 186 187 188 189 190
- [x] component is
- [x] render function 暂不支持 composition API
- [x] slots
- [x] template  节点中 style class v-bind map 数据
- [x] template 标签测试
- [x] unrecognized-component