Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Project Deployment

Project Deployment

After polishing through the previous chapters, our TaskHub project's features are very complete. Now we take the final step:pushing it from the local development environment to the production server.

In enterprise deployment, we need to ensure the code can automatically switch configurations across different environments (testing, production), and that loading is extremely fast.


Environment Variable Management: one codebase, run in many places

In real development, API addresses or some configurations differ between the development and production environments.

Vite supports by default .env files.

Create two files in the project root directory:

.env.development (local development)

VITE_API_BASE_URL=http://localhost:3000/api

VITE_APP_TITLE=TaskHub (Dev)

.env.production (online production)

VITE_API_BASE_URL=https://api.taskhub.com

VITE_APP_TITLE=TaskHub

Explanation:


Bundle optimization: extreme loading speed

After the project imports libraries such as Vue Router and Pinia, the bundled index.js will become very large. We need to optimize through "code splitting" and "compression".

Code splitting strategy (Manual Chunks)

Modify vite.config.jsto bundle third-party libraries into separate JS files, leveraging browser caching.

Example

// vite.config.js

export

default

defineConfig

(

{

  build

:

{

    rollupOptions

:

{

      output

:

{

        manualChunks

:

{

         

// bundle vue-related libraries together

         

'vue-vendor'

:

[

'vue'

,

'vue-router'

,

'pinia'

]

,

         

// other third-party libraries (such as axios or echarts introduced later)

         

// 'utils-vendor': ['axios'],

       

}

     

}

   

}

 

}

}

)

Gzip Compression Configuration

Install the plugin:npm install -D vite-plugin-compression

Example

import

viteCompression from

'vite-plugin-compression'

export

default

defineConfig

(

{

  plugins

:

[

   

// ...

    viteCompression

(

{

      threshold

:

10240

,

// only compress files larger than 10kb

      algorithm

:

'gzip'

,

      ext

:

'.gz'

   

}

)

 

]

}

)


Automated Deployment (CI/CD)

We use the most popular GitHub Actions as an example; when you push code to GitHub, it automatically packages and deploys it to GitHub Pages or your server.

Create the Workflow Script

Create in the project root directory .github/workflows/deploy.yml:

name: Deploy TaskHub



on:

  push:

    branches: [ main ] # 监听 main 分支的推送



jobs:

  build-and-deploy:

    runs-on: ubuntu-latest

    steps:

      - name: Checkout 🛎️

        uses: actions/checkout@v4



      - name: Install and Build 🔧

        run: |

          npm install

          npm run build



      - name: Deploy 🚀

        uses: JamesIves/github-pages-deploy-action@v4

        with:

          folder: dist # 打包产物目录

          branch: gh-pages # 部署到这个分支


Docker Containerization in Production (Advanced)

If your company uses container cloud, you need a Dockerfile. It guarantees that your TaskHub runs in exactly the same environment on any server.

Create in the root directory Dockerfile:

# 1. 使用 Nginx 镜像作为基础

FROM nginx:alpine



# 2. Copy the packaged dist directory to the Nginx static resource directory

COPY dist/ /usr/share/nginx/html/



# 3. Copy custom nginx configuration (solve single page application routing 404 issue)

COPY nginx.conf /etc/nginx/conf.d/default.conf



EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]


TaskHub Project Full Route Summary

At this point, you have completed a from-scratch, Vue 3 (JavaScript) + Tailwind v4 enterprise-level hands-on project based on:

  1. Architecture layer:Vite + Tailwind v4 + Vue Router + Pinia.
  2. Logic layer: Composition API + custom Composables for reuse.
  3. Performance layer: shallowRef + v-memo + bundle code splitting.
  4. Engineering layer: environment variables + CI/CD automated deployment.

The entire project directory structure:

src/

├── assets/          # 静态资源

├── components/      # 高复用原子组件 (Header, Input, Item, Filter)

├── composables/     # 自定义逻辑钩子 (useLocalStorage, useNotification)

├── router/          # 路由配置与权限守卫

├── stores/          # Pinia 状态仓库 (TaskStore)

├── views/           # 页面级组件 (Dashboard, Login)

├── App.vue          # 全局容器与页面过渡动画

└── main.js          # 项目入口

Related commands:

# 安装依赖

npm install



# Local development

npm run dev



# Production build

npm run build