从零构建一个TS-Node项目

本文档将从总体概括、装包问题、代码规范、测试部分和代码构建等方面,详细讲解如何从零构建一个TS-Node项目。推荐使用yarn进行包管理,并集成eslint、prettier和commitlint来确保代码质量和规范。测试部分介绍了jest和ts-jest的使用,同时提供了项目配置的参考链接。
摘要由CSDN通过智能技术生成

总体概括

因为搭建的是node项目, 所以比前端项目搭建要简单很多。

从以下因素考虑

  • 装包问题
    • yarn
    • 配置项: .yarnrc 、.npmrc
    • .gitignore
  • 代码规范
    • eslint
    • prettier
    • commititlint
    • 每次提交自动检测eslint\commitLint
  • 测试
    • jest、ts-jest
  • 代码构建
    • tsconfig
    • typeings

装包问题

这个问题比较简单没有什么好说的, 推荐 yarn , 如果是非常复杂的项目, 推荐使用 yarn/workSpace + lerna 组合拳, 爽歪歪。

.yarnrc:

registry "https://registry.npm.taobao.org"

npmrc:

registry = https://registry.npm.taobao.org

.gitignore:

# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules/

# testing
coverage/
/doc/
/mock2easy/

# production
/build
/coverage

worker/

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.idea
.idea/
/packages/**/*.js
/packages/**/*.js.map
!jest.config.js

tempDownload/

npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock
package-lock.json

代码规范

集成eslint、prettier、recommended
.eslintrc.js:

module.exports = {
   
  parser: '@typescript-eslint/parser',
  parserOptions: {
   
    ecmaFeatures: {
   
      jsx: true,
    },
    project: './tsconfig.json',
  },
  plugins: ['@typescript-eslint'],
  extends: [
    'prettier',
    'prettier/@typescript-eslint',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'plugin:prettier/recommended',
  ],
  env: {
   
    node: true,
    browser: false,
  },
  rules: {
   
    '@typescript-eslint/no-var-requires': 1,
    '@typescript-eslint/explicit-function-return-type': 2,
    '@typescript-eslint/explicit-member-accessibility': 2,
    'no-unused-vars': 2,
    semi: [2, 'always', {
    omitLastInOneLineBlock: true }],
  },
};

.prettierrc:

{
   
  "singleQuote": true,
  "trailingComma"
首先,你需要安装 Node.js 和 npm。然后,可以按照以下步骤构建你的项目: 1. 创建项目文件夹并进入该文件夹: ``` mkdir my-project cd my-project ``` 2. 初始化项目: ``` npm init -y ``` 3. 安装 Vite: ``` npm install vite --save-dev ``` 4. 安装 Vue.js: ``` npm install vue@next --save ``` 5. 安装 TypeScript: ``` npm install typescript --save-dev ``` 6. 安装 Electron: ``` npm install electron --save-dev ``` 7. 安装 Element Plus: ``` npm install element-plus --save ``` 8. 安装 Vuex: ``` npm install vuex --save ``` 9. 创建 TypeScript 配置文件: ``` npx tsc --init ``` 10. 创建主进程和渲染进程的 Electron 入口文件: ``` // 主进程入口文件 main.js const { app, BrowserWindow } = require('electron') function createWindow () { const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }) win.loadFile('index.html') } app.whenReady().then(() => { createWindow() app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } }) }) app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } }) ``` ``` // 渲染进程入口文件 src/renderer/main.ts import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' createApp(App).use(router).use(store).use(ElementPlus).mount('#app') ``` 11. 创建 Vue 组件,例如 App.vue: ``` <template> <div id="app"> <router-view /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ name: 'App' }) </script> ``` 12. 创建 Vuex store,例如 store/index.ts: ``` import { createStore } from 'vuex' export default createStore({ state: { count: 0 }, mutations: { increment (state) { state.count++ } } }) ``` 13. 创建路由器,例如 router/index.ts: ``` import { createRouter, createWebHashHistory } from 'vue-router' import Home from '../views/Home.vue' const routes = [ { path: '/', name: 'Home', component: Home } ] const router = createRouter({ history: createWebHashHistory(), routes }) export default router ``` 14. 创建主页面组件,例如 views/Home.vue: ``` <template> <div> <h1>{{ message }}</h1> <button @click="increment">Increment</button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { useStore } from 'vuex' export default defineComponent({ name: 'Home', setup () { const store = useStore() return { message: 'Hello, World!', increment () { store.commit('increment') } } } }) </script> ``` 15. 运行项目: ``` npm run dev ``` 以上就是使用 Vite、Vue.js、TypeScript、Electron、Element Plus 和 Vuex 构建项目的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值