cli模式vite+vue3差量编译机制优化,项目体积大后(如:100+页面)差量编译巨慢,可以手动创建个100+项目试试,并且差量编译机制有问题,因为慢每次的代码修改后需要等上次差量编译完才能执行下一次代码修改,差量编译(假如为4s,4s内又有修改 是不会提前结束掉此次编译的 需要等上一次产量编译结束后才能开始 如果你在4s内保存了多久 那编译时间将是 4s*次数 ) 导致开发无法正常进行
潮流
- 发布:2023-09-27 17:04
- 更新:2026-06-25 11:04
- 阅读:1087
最佳回复
在@飞翔的青蛙 电脑上验证了一下,确实是rollup内部机制导致的。手动关闭全部关于sourcemap相关的处理时速度有明显的提升。临时解决方案: 可以使用 https://www.npmjs.com/package/@uni_toolkit/vite-plugin-sourcemap-pro (和楼上@飞翔的青蛙发的逻辑差不多)
这个插件来解决。
后续我们内部也会修复相关问题。
vue3 + vite 发行到什么平台, h5 还是那一家小程序?
如果切换其他的发行平台,比如 h5 - 微信小程序 互相切换,能有改善吗?
手动升级 vite 到 v4.3+ 或者 v5 版本能改善你的问题吗,尝试手动升级依赖。
找官方大佬写了一个插件: 在vite.config.js中添加以下代码可以提升一些重载速度
然后在
export default defineConfig({
plugins: [killPluginSourcemaps()],
build: {
sourcemap:false
......
});
function killPluginSourcemaps() {
forceDisableSourcemap()
return {
name: 'kill-plugin-sourcemaps',
enforce: 'pre',
config() {
forceDisableSourcemap()
return {
build: {
sourcemap: false,
rollupOptions: {
output: {
sourcemap: false,
sourcemapExcludeSources: true,
},
},
},
css: {
devSourcemap: false,
},
esbuild: {
sourcemap: false,
},
}
},
configResolved(config) {
forceDisableSourcemap()
for (const plugin of config.plugins) {
if (plugin.name === 'kill-plugin-sourcemaps') continue
for (const hookName of ['load', 'transform', 'renderChunk']) {
const hook = plugin[hookName]
if (!hook) continue
if (typeof hook === 'function') {
plugin[hookName] = function (...args) {
return runWithoutSourcemap(hook, this, args)
}
} else if (hook && typeof hook.handler === 'function') {
const original = hook.handler
hook.handler = function (...args) {
return runWithoutSourcemap(original, this, args)
}
}
}
}
},
outputOptions(options) {
forceDisableSourcemap()
return {
...options,
sourcemap: false,
sourcemapExcludeSources: true,
}
},
}
}
function forceDisableSourcemap() {
process.env.UNI_APP_SOURCEMAP = 'false'
process.env.GENERATE_SOURCEMAP = 'false'
}
function runWithoutSourcemap(fn, ctx, args) {
const noMapCtx = createNoMapContext(ctx)
const result = fn.apply(noMapCtx, args)
if (result && typeof result.then === 'function') {
return result.then(stripMap)
}
return stripMap(result)
}
function createNoMapContext(ctx) {
return new Proxy(ctx, {
get(target, key, receiver) {
if (key === 'getCombinedSourcemap') {
return () => null
}
const value = Reflect.get(target, key, receiver)
if (typeof value === 'function') {
return value.bind(target)
}
return value
},
})
}
function stripMap(result) {
if (!result) return result
if (typeof result === 'object' && 'map' in result) {
return {
...result,
map: null,
}
}
return result
}
DCloud_HB_WJ
感谢@飞翔的青蛙 提供的复现环境
2026-06-25 11:09