vue3 编译很慢_vue-cli3 开发编译时间太长

针对Vue3项目使用vue-cli3时遇到的编译速度慢问题,可以通过调整vue.config.js配置,如启用HappyPack并设置多核处理,以及合理设置externals,来提升编译效率。文章详细介绍了如何配置happypack、externals和webpack的优化策略,以缩短编译时间。
摘要由CSDN通过智能技术生成

vue-cli3 单页项目

文件大概 400多个

保存一次 编译要20多秒

怎么优化打包配置

每次都是从这里开始卡

vue.config.js

/*

happypack

*/

const HappyPack = require('happypack');

const os = require('os');

const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });

/*

*/

const externals = {

vue: "Vue",

vuex: "Vuex",

"vue-router": "VueRouter",

"element-ui": "ELEMENT"

}

const path = require('path');

const webpack = require('webpack');

function resolve(dir) {

return path.join(__dirname, dir)

}

module.exports = {

// 基本路径

publicPath: process.env.NODE_ENV === 'production' ?

'./' :

'/',

// 输出文件目录

// outputDir: 'dist', // 默认dist

// 用于嵌套生成的静态资产(js,css,img,fonts)目录

// assetsDir: '',

// 指定生成的 index.html 的输出路径 (相对于 outputDir)。也可以是一个绝对路径

// indexPath: 'index.html', // Default: 'index.html'

filenameHashing: true,

// 构建多页时使用

pages: {

index: {

// page 的入口

entry: 'src/pages/index/main.js',

// 模板来源

template: 'public/index.html',

// 在 dist/index.html 的输出

filename: 'index.html',

},

// 当使用只有入口的字符串格式时,

sub: {

// page 的入口

entry: 'src/pages/sub/main.js',

// 模板来源

template: 'public/sub.html',

// 在 dist/index.html 的输出

filename: 'sub.html',

},

},

// eslint-loader是否在保存的时候检查

lintOnSave: false,

runtimeCompiler: true,

transpileDependencies: [],

productionSourceMap: false,

configureWebpack: config => {

if (process.env.NODE_ENV === 'production') {

// 为生产环境修改配置...

return {

/* happypack */

module: {

rules: [

{

test: /\.js$/,

//把对.js 的文件处理交给id为happyBabel 的HappyPack 的实例执行

loader: 'happypack/loader?id=happyBabel',

//排除node_modules 目录下的文件

exclude: /node_modules/

},

]

},

externals,

plugins: [

new HappyPack({

//用id来标识 happypack处理那里类文件

id: 'happyBabel',

//如何处理 用法和loader 的配置一样

loaders: [{

loader: 'babel-loader?cacheDirectory=true',

}],

//共享进程池

threadPool: happyThreadPool,

//允许 HappyPack 输出日志

verbose: true,

})

],

}

} else {

// 为开发环境修改配置...

return {

/* happypack */

module: {

rules: [

{

test: /\.js$/,

//把对.js 的文件处理交给id为happyBabel 的HappyPack 的实例执行

loader: 'happypack/loader?id=happyBabel',

//排除node_modules 目录下的文件

exclude: /node_modules/

},

]

},

externals,

plugins: [

new HappyPack({

//用id来标识 happypack处理那里类文件

id: 'happyBabel',

//如何处理 用法和loader 的配置一样

loaders: [{

loader: 'babel-loader?cacheDirectory=true',

}],

//共享进程池

threadPool: happyThreadPool,

//允许 HappyPack 输出日志

verbose: true,

})

],

}

}

},

// 是一个函数,会接收一个基于 webpack-chain 的 ChainableConfig 实例。允许对内部的 webpack 配置进行更细粒度的修改。

chainWebpack: config => {

config

.plugin('provide')

.use(webpack.ProvidePlugin, [{

Api: resolve('src/api/Api.js'),

Util: resolve('src/utils/js/index.js'),

// Img: resolve('src/utils/imgs/imgs.js'),

// R: 'ramda'

}])

.end()

if (process.env.use_analyzer) {

config

.plugin('webpack-bundle-analyzer')

.use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)

.end()

}

// set svg-sprite-loader

config.module

.rule('svg')

.exclude.add(resolve('src/icons'))

.end()

config.module

.rule('icons')

.test(/\.svg$/)

.include.add(resolve('src/icons'))

.end()

.use('svg-sprite-loader')

.loader('svg-sprite-loader')

.options({

symbolId: 'icon-[name]'

})

.end()

config

.when(process.env.NODE_ENV === 'development',

config => {

config.devtool('cheap-source-map')

}

).end()

config

.when(process.env.NODE_ENV !== 'development',

config => {

config

.plugin('ScriptExtHtmlWebpackPlugin')

.after('html')

.use('script-ext-html-webpack-plugin', [{

// `runtime` must same as runtimeChunk name. default is `runtime`

inline: /runtime\..*\.js$/

}])

.end()

config

.optimization.splitChunks({

chunks: 'all',

cacheGroups: {

libs: {

name: 'chunk-libs',

test: /[\\/]node_modules[\\/]/,

priority: 10,

chunks: 'initial' // only package third parties that are initially dependent

},

elementUI: {

name: 'chunk-elementUI', // split elementUI into a single package

priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app

test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm

},

commons: {

name: 'chunk-commons',

test: resolve('src/components'), // can customize your rules

minChunks: 3, // minimum common number

priority: 5,

reuseExistingChunk: true

}

}

})

.end()

}

)

},

// css相关配置

css: {

// 启用 CSS modules

modules: false,

// 是否使用css分离插件

// extract: true,

// 开启 CSS source maps?

sourceMap: false,

// css预设器配置项

loaderOptions: {

sass: {

data: `@import "@/styles/_util.scss";`

}

},

},

// webpack-dev-server 相关配置

devServer: {

// 设置热替换

disableHostCheck: true,//webpack4.0 开启热更新

// 设置页面引入

inline: false,

port: 8080,

// https: false,

open: false,

proxy: {

[process.env.VUE_APP_API_PREFIX_URL]: {

target: process.env.VUE_APP_API_PROXY_URL,

changeOrigin: true,

ws: true,

pathRewrite: {

[process.env.VUE_APP_API_PREFIX_URL]: ''

},

}

},

},

}

每次都是卡在70% 70%-100% 要26秒多

.env.development

VUE_APP_API_PREFIX_URL = /api

VUE_APP_API_BASE_URL = /xxx

VUE_APP_UPLOAD_URL = /xxx

VUE_APP_SOCKET_URL = /xxx

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值