惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Vercel News
Vercel News
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
The Cloudflare Blog
N
Netflix TechBlog - Medium
GbyAI
GbyAI
Help Net Security
Help Net Security
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
Martin Fowler
Martin Fowler
量子位
P
Palo Alto Networks Blog
Security Latest
Security Latest
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
H
Help Net Security
C
Check Point Blog
T
Troy Hunt's Blog
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
V
Visual Studio Blog
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Recent Announcements
Recent Announcements
Forbes - Security
Forbes - Security
IT之家
IT之家
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
LangChain Blog
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
Stack Overflow Blog
Stack Overflow Blog
H
Hacker News: Front Page
B
Blog
W
WeLiveSecurity
罗磊的独立博客
Jina AI
Jina AI
博客园 - 【当耐特】

博客园 - Liaofy

前端新范式:用 AI 提效开发,用 E2E 保证迭代质量 把SPA的加载速度提升一倍,有没有搞头? 使用mumu模拟器抓包 andriod app 如何在本地给 vue-router4 扩展功能? Vue3 路由优化,使页面初次渲染效率翻倍 【eslint 插件开发】禁用 location 跳转外部链接 vue 项目使用 charles 代理线上页面到本地后显示404 避免使用单个数字作为对象的key 高扩展弹出层组件设计实现(H5&小程序) 轻松搞懂POST与PUT的区别 vite(or rollup) 实现 webpackChunkName webapp项目架构畅想(vue3).md 修剪AST树减少webapck打包尺寸 【uniapp】改善中大型uniapp小程序项目开发体验 vscode 调试 webpack 打包 5分钟,搞清 Unicode 与 UTF-8 的区别 deAsync.js 真正让异步变同步 盘的转——使用缓动函数完成动画 多个请求间断失败,如何优雅处理?
vite试玩:老项目启动从6分钟到秒开
Liaofy · 2021-12-16 · via 博客园 - Liaofy

背景

基于复杂的历史原因,我们正在开发、维护一个非典型前端项目:使用 vue2 开发页面,编译打包后,由 express 进行缝合、响应。

目前 gitlab 上统计仓库大小为341.9 MB,我本机全量编译打包时间已经攀升到 6 分钟左右。当多个分支频繁切换时,“那画面太美我不敢看”。

前不久优化了我司uniapp小程序项目开发体验,效果显著。正好 vite2 已经稳定,就想着能不能改善下 H5 开发阶段的构建流程。

vite 后端集成

vite 提供了后端集成的方案。所以只需要:用 vite 构建 vue2 项目,用 express 起一个服务,并在服务器的 HTML 模板注入:

// localhost:3000 是 vite 启动的服务器,next/src/_entry.js 是你自己项目的入口文件
<script type="module" src="http://localhost:3000/@vite/client"></script>
<script type="module" src="http://localhost:3000/next/src/_entry.js"></script>

vite config

vite 配置非常简单,不过,我在配置的时候遇到了三个问题:

问题1:如何适配 vue2

官方给出了插件 vite-plugin-vue2

问题2:导入外置的 vue、vue-router等库报错

譬如 vue、vue-router,我们是通过 script 标签加载的,vite 编译时会报错。此时需要插件 vite-plugin-externals。

问题3:静态资源 404

服务端集成后,使用相对路径的前端资源 404。在开发阶段,可以配置server.origin = http://localhost:${PORT}解决。

具体配置如下:

import { resolve } from 'path'
import { defineConfig } from 'vite'
import { createVuePlugin } from "vite-plugin-vue2"
import { viteExternalsPlugin } from 'vite-plugin-externals'

function pathResolve(dir) {
  return resolve(__dirname, './' + dir)
}

const PORT = 3000

export default defineConfig({
  resolve: {
    alias: [
      {
        find: /^@\//,
        replacement: pathResolve('next/src') + '/',
      },
    ],
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  },
  server: {
    host: true,
    port: PORT,
    strictPort: true,
    origin: `http://localhost:${PORT}`, // 静态资源前缀
    // 避免轮询页面刷新的问题
    hmr: { host: 'localhost', port: PORT, protocol: 'ws' },
  },
  build: {
    manifest: true,
    rollupOptions: {
      input: pathResolve('next/src/_entry.js'),
    }
  },
  plugins: [
    createVuePlugin(),
    viteExternalsPlugin({
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'vuex': 'Vuex',
    }),
  ],
});

从此,享受极致快速的开发体验吧。

然鹅,只能自己爽爽。