Skip to content

feat: 支持 TypeScript 和 Composition API #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Nov 20, 2020

Conversation

gd4Ark
Copy link
Contributor

@gd4Ark gd4Ark commented Nov 17, 2020

Why

随着技术栈更新,有必要为项目模板加入 TypeScript 和 Composition API 支持。

How

支持 TypeScript

增加询问 “Programming language”:

  • JavaScript(默认选择)
  • TypeScript

理由:官方 create-nuxt-app 采用该方式,默认值为 JavaScript

如果选择使用 TypeScript,则自动做以下事情:

  1. 增加 TypeScript 支持
    1. 安装 @nuxt/typescript-build、@nuxt/types
    2. nuxt.config.js 中注入 @nuxt/typescript-build
    3. 增加 tsconfig.json 文件
    4. 增加 vue-shim.d.ts 类型文件
    5. 使用 TypeScript 版本的 login.vue 文件(写两份 login.vue)
  2. ESLint 支持 TypeScript
    1. 安装 @nuxtjs/eslint-config-typescript
    2. ESLint 使用 @nuxtjs/eslint-config-typescript
    3. 开启运行时检查 TypeScript 错误

参考:如何在 Nuxt 项目中引入 TypeScript

Composition API

长远来看,@nuxtjs/composition-api 是比 @vue/composition-api 更好的选择,理由:

但由于目前 Nuxt 版本为 v2.11.0,在这个版本无法正常使用 @nuxtjs/composition-api,具体可以看这个 issue,但将 Nuxt 升级到目前的最新版本(v2.14.7),已解决该问题。

相关文档:https://composition-api.nuxtjs.org

默认添加 @nuxtjs/composition-api 依赖,也即不作任何询问,同时将 login 页面使用 composition-api 重构。

除此之外,增加 babel-preset-vca-jsx 依赖,解决 JSX 的问题

参考:如何在 Nuxt 项目中引入 Composition API

Other

  1. 除此之外,还 fix 了一些因引入 @nuxtjs/eslint-config-typescript 导致的 eslint 失败文件
    i. 经测试 fix 后的这部分文件,即便在不用 typescript(也就是没有 @nuxtjs/eslint-config-typescript),eslint 也依旧正常
  2. 将 jest 相关的 eslint 配置放在 unit 目录下,否则会影响 cypress 测试文件的 eslint
  3. .babelrc 需要在 template 时改名为 _.babelrc,否则 test:snapshot 时会报找不到 babel/preset-env 模块(该问题是本次引入的,之前没有这个问题

Preview

2020-11-18 16 53 11
2020-11-19 18 17 26

Test

image

Docs

@auto-add-label auto-add-label bot added the enhancement New feature or request label Nov 17, 2020
@netlify
Copy link

netlify bot commented Nov 17, 2020

Deploy preview for femessage-create-nuxt-app ready!

Built with commit b27fee8

https://deploy-preview-161--femessage-create-nuxt-app.netlify.app

@gd4Ark gd4Ark changed the title feat: 支持 TypeScript feat: 支持 TypeScript 和 Composition API Nov 18, 2020
@@ -0,0 +1,240 @@
## TypeScript FAQ

<span style="color: red; font-weight: bold;">注意</span>:本文内容不一定能及时更新,建议到 [《如何在 Nuxt 项目中引入 TypeScript》](https://deepexi.yuque.com/docs/share/ec81b0e7-b1b4-426c-a66c-8c293e7185c2) 查看最新内容。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

�直接给个链接就好了

@levy9527 levy9527 merged commit 3757e6f into FEMessage:dev Nov 20, 2020
@levy9527
Copy link
Collaborator

🎉 This PR is included in version 2.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants