Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue-admin-template4.2.1,打包速度30分钟,怎么解决 #782

Open
liuxuey123 opened this issue Aug 1, 2024 · 4 comments
Open

vue-admin-template4.2.1,打包速度30分钟,怎么解决 #782

liuxuey123 opened this issue Aug 1, 2024 · 4 comments

Comments

@liuxuey123
Copy link

"webpack": ">=4 < 4.29",打包速度30分钟,怎么解决

@liuxuey123
Copy link
Author

1
2
// 扩展 Webpack 配置,进一步定制 Webpack 配置
chainWebpack(config) {
config.plugins.delete('preload') // TODO: need test // 删除预加载插件
config.plugins.delete('prefetch') // TODO: need test// 删除预取插件
// 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()
// set preserveWhitespace
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.compilerOptions.preserveWhitespace = true
return options
})
.end()
config
// https://webpack.js.org/configuration/devtool/#development
.when(process.env.NODE_ENV === 'development',
config => config.devtool('cheap-source-map'),
)
// 开发模式
config
.when(process.env.NODE_ENV !== 'development',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')// 这段代码将 runtime JS 文件内联到 HTML 文件中,而不是生成一个单独的 JS 文件。这有助于减少 HTTP 请求的数量,从而加快页面加载速度
.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' // 仅打包初始依赖的第三方库
},
elementUI: {
name: 'chunk-elementUI', // 单独将 elementUI 拆包
priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
test: /[\/]node_modules[\/]_?element-ui(.
)/ // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // 可自定义拓展你的规则
minChunks: 3, // 被多个模块引用的代码才会被拆分,这可以避免重复打包相同的代码,从而减少整体打包体积
priority: 5,
reuseExistingChunk: true // 这个配置确保了已经存在的 chunk 会被重用,而不是重新打包。这避免了重复打包相同代码的情况,进而减少了最终的文件体积
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true

            }
          }
        })
      config.optimization.runtimeChunk('single')// 这样可以让 Webpack 在每次构建时生成更少的哈希变化,从而提升缓存效果
    }
  )

}

@liuxuey123
Copy link
Author

这个是现在的代码,怎么优化呀,

@lgl48128244
Copy link

是不是打包命令用错了
用下面的命令试试
npm run build:prod

@liuxuey123
Copy link
Author

是的就是npm run build:prod和npm run build:stage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants