如何利用image-webpack-loader插件在Vue打包过程中对图片进行高效压缩优化?

2026-04-02 10:361阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1235个文字,预计阅读时间需要5分钟。

如何利用image-webpack-loader插件在Vue打包过程中对图片进行高效压缩优化?

Vue项目中打包后,某些图片文件体积较大,导致整体打包体积过大。使用`image-webpack-loader`插件可以有效地压缩这些大图片,减小打包体积。

+ 步骤1:执行命令 `npm install image-webpack-loader --save-dev`+ 步骤2:在`build/webpack.base.conf.js`文件中配置插件:javascriptmodule.exports={ // ...其他配置 module: { rules: [ // ...其他规则 { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, use: [ { loader: 'url-loader', options: { limit: 10240, name: 'img/[name].[hash:7].[ext]' } }, { loader: 'image-webpack-loader', options: { mozjpeg: { progressive: true, quality: 65 }, // optipng.enabled: false will disable optipng optipng: { enabled: false, }, pngquant: { quality: '65-90', speed: 4 }, gifsicle: { interlaced: false, }, // the webp option will enable webp webp: { quality: 75 } } } ] } ] }}

vue正常打包之后一些图片文件很大,使打包体积很大,通过image-webpack-loader插件可将大的图片进行压缩从而缩小打包体积

step1:

npm install image-webpack-loader --save-dev

step2:

在build/webpack.base.conf.js中改如下配置

module.exports = { module: { { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, // loader: 'url-loader', // options: { // limit: 100000, // name: utils.assetsPath('img/[name].[hash:7].[ext]') // } loader: ['url-loader?limit=10000&name=' + utils.assetsPath('img/[name].[hash:7].[ext]'), 'image-webpack-loader' ] }, ] }, }

补充知识:vuecli3项目打包优化配置要点

一、新建项目

使用 vue-cli3 构建一个初始的Vue项目

因为使用了cli3,很多目录结构不见了,而相关配置是放在vue.config.js里面的,因此在根目录,新建一个vue.config.js

module.exports = {}

二、正式优化

1、将 productionSourceMap 设为 false

(1) 在vue.config.js中module.exports写入:

module.exports = { productionSourceMap: false }

2、图片压缩

vue正常打包之后一些图片文件很大,使打包体积很大,通过image-webpack-loader插件可将大的图片进行压缩从而缩小打包体积

(1) 先安装依赖:cnpm install image-webpack-loader --save-dev

(2) 在vue.config.js中module.exports写入:

module.exports = { productionSourceMap: false, chainWebpack: config => { // ============压缩图片 start============ config.module .rule('images') .use('image-webpack-loader') .loader('image-webpack-loader') .options({ bypassOnDebug: true }) .end() // ============压缩图片 end============ } }

3、cdn配置(可选)

(1) 在vue.config.js 最上边写入:

// 是否为生产环境 const isProduction = process.env.NODE_ENV !== 'development' // 本地环境是否需要使用cdn const devNeedCdn = false // cdn链接 const cdn = { // cdn:模块名称和模块作用域命名(对应window里面挂载的变量名称) externals: { vue: 'Vue', vuex: 'Vuex', 'vue-router': 'VueRouter' }, // cdn的css链接 css: [], // cdn的js链接 js: [ 'cdn.staticfile.org/vue/2.6.10/vue.min.js', 'cdn.staticfile.org/vuex/3.0.1/vuex.min.js', 'cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js' ] }

(2) 在vue.config.js module.exports chainWebpack中写入:

// ============注入cdn start============ config.plugin('html').tap(args => { // 生产环境或本地需要cdn时,才注入cdn if (isProduction || devNeedCdn) args[0].cdn = cdn return args }) // ============注入cdn start============

(3) 在vue.config.js module.exports configureWebpack中写入:

configureWebpack: config => { // 用cdn方式引入,则构建时要忽略相关资源 if (isProduction || devNeedCdn) config.externals = cdn.externals }

(4) 当前配置的vue.config.js

// 是否为生产环境 const isProduction = process.env.NODE_ENV !== 'development' // 本地环境是否需要使用cdn const devNeedCdn = false // cdn链接 const cdn = { // cdn:模块名称和模块作用域命名(对应window里面挂载的变量名称) externals: { vue: 'Vue', vuex: 'Vuex', 'vue-router': 'VueRouter' }, // cdn的css链接 css: [], // cdn的js链接 js: [ 'cdn.staticfile.org/vue/2.6.10/vue.min.js', 'cdn.staticfile.org/vuex/3.0.1/vuex.min.js', 'cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js' ] } module.exports = { productionSourceMap: false, chainWebpack: config => { // ============压缩图片 start============ config.module .rule('images') .use('image-webpack-loader') .loader('image-webpack-loader') .options({ bypassOnDebug: true }) .end() // ============压缩图片 end============ // ============注入cdn start============ config.plugin('html').tap(args => { // 生产环境或本地需要cdn时,才注入cdn if (isProduction || devNeedCdn) args[0].cdn = cdn return args }) // ============注入cdn start============ }, configureWebpack: config => { // 用cdn方式引入,则构建时要忽略相关资源 if (isProduction || devNeedCdn) config.externals = cdn.externals } }

(5) 在public index.html 写入

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta cdn.staticfile.org/vue/2.6.10/vue.min.js', 'cdn.staticfile.org/vuex/3.0.1/vuex.min.js', 'cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js' ] } module.exports = { productionSourceMap: false, chainWebpack: config => { // ============压缩图片 start============ config.module .rule('images') .use('image-webpack-loader') .loader('image-webpack-loader') .options({ bypassOnDebug: true }) .end() // ============压缩图片 end============ // ============注入cdn start============ config.plugin('html').tap(args => { // 生产环境或本地需要cdn时,才注入cdn if (isProduction || devNeedCdn) args[0].cdn = cdn return args }) // ============注入cdn start============ }, configureWebpack: config => { // 用cdn方式引入,则构建时要忽略相关资源 if (isProduction || devNeedCdn) config.externals = cdn.externals // 生产环境相关配置 if (isProduction) { // 代码压缩 config.plugins.push( new UglifyJsPlugin({ uglifyOptions: { //生产环境自动删除console compress: { warnings: false, // 若打包错误,则注释这行 drop_debugger: true, drop_console: true, pure_funcs: ['console.log'] } }, sourceMap: false, parallel: true }) ) // gzip压缩 const productionGzipExtensions = ['html', 'js', 'css'] config.plugins.push( new CompressionWebpackPlugin({ filename: '[path].gz[query]', algorithm: 'gzip', test: new RegExp( '\\.(' + productionGzipExtensions.join('|') + ')$' ), threshold: 10240, // 只有大小大于该值的资源会被处理 10240 minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理 deleteOriginalAssets: false // 删除原文件 }) ) // 公共代码抽离 config.optimization = { splitChunks: { cacheGroups: { vendor: { chunks: 'all', test: /node_modules/, name: 'vendor', minChunks: 1, maxInitialRequests: 5, minSize: 0, priority: 100 }, common: { chunks: 'all', test: /[\\/]src[\\/]js[\\/]/, name: 'common', minChunks: 2, maxInitialRequests: 5, minSize: 0, priority: 60 }, styles: { name: 'styles', test: /\.(sa|sc|c)ss$/, chunks: 'all', enforce: true }, runtimeChunk: { name: 'manifest' } } } } } } }

以上这篇vue打包通过image-webpack-loader插件对图片压缩优化操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

如何利用image-webpack-loader插件在Vue打包过程中对图片进行高效压缩优化?

本文共计1235个文字,预计阅读时间需要5分钟。

如何利用image-webpack-loader插件在Vue打包过程中对图片进行高效压缩优化?

Vue项目中打包后,某些图片文件体积较大,导致整体打包体积过大。使用`image-webpack-loader`插件可以有效地压缩这些大图片,减小打包体积。

+ 步骤1:执行命令 `npm install image-webpack-loader --save-dev`+ 步骤2:在`build/webpack.base.conf.js`文件中配置插件:javascriptmodule.exports={ // ...其他配置 module: { rules: [ // ...其他规则 { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, use: [ { loader: 'url-loader', options: { limit: 10240, name: 'img/[name].[hash:7].[ext]' } }, { loader: 'image-webpack-loader', options: { mozjpeg: { progressive: true, quality: 65 }, // optipng.enabled: false will disable optipng optipng: { enabled: false, }, pngquant: { quality: '65-90', speed: 4 }, gifsicle: { interlaced: false, }, // the webp option will enable webp webp: { quality: 75 } } } ] } ] }}

vue正常打包之后一些图片文件很大,使打包体积很大,通过image-webpack-loader插件可将大的图片进行压缩从而缩小打包体积

step1:

npm install image-webpack-loader --save-dev

step2:

在build/webpack.base.conf.js中改如下配置

module.exports = { module: { { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, // loader: 'url-loader', // options: { // limit: 100000, // name: utils.assetsPath('img/[name].[hash:7].[ext]') // } loader: ['url-loader?limit=10000&name=' + utils.assetsPath('img/[name].[hash:7].[ext]'), 'image-webpack-loader' ] }, ] }, }

补充知识:vuecli3项目打包优化配置要点

一、新建项目

使用 vue-cli3 构建一个初始的Vue项目

因为使用了cli3,很多目录结构不见了,而相关配置是放在vue.config.js里面的,因此在根目录,新建一个vue.config.js

module.exports = {}

二、正式优化

1、将 productionSourceMap 设为 false

(1) 在vue.config.js中module.exports写入:

module.exports = { productionSourceMap: false }

2、图片压缩

vue正常打包之后一些图片文件很大,使打包体积很大,通过image-webpack-loader插件可将大的图片进行压缩从而缩小打包体积

(1) 先安装依赖:cnpm install image-webpack-loader --save-dev

(2) 在vue.config.js中module.exports写入:

module.exports = { productionSourceMap: false, chainWebpack: config => { // ============压缩图片 start============ config.module .rule('images') .use('image-webpack-loader') .loader('image-webpack-loader') .options({ bypassOnDebug: true }) .end() // ============压缩图片 end============ } }

3、cdn配置(可选)

(1) 在vue.config.js 最上边写入:

// 是否为生产环境 const isProduction = process.env.NODE_ENV !== 'development' // 本地环境是否需要使用cdn const devNeedCdn = false // cdn链接 const cdn = { // cdn:模块名称和模块作用域命名(对应window里面挂载的变量名称) externals: { vue: 'Vue', vuex: 'Vuex', 'vue-router': 'VueRouter' }, // cdn的css链接 css: [], // cdn的js链接 js: [ 'cdn.staticfile.org/vue/2.6.10/vue.min.js', 'cdn.staticfile.org/vuex/3.0.1/vuex.min.js', 'cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js' ] }

(2) 在vue.config.js module.exports chainWebpack中写入:

// ============注入cdn start============ config.plugin('html').tap(args => { // 生产环境或本地需要cdn时,才注入cdn if (isProduction || devNeedCdn) args[0].cdn = cdn return args }) // ============注入cdn start============

(3) 在vue.config.js module.exports configureWebpack中写入:

configureWebpack: config => { // 用cdn方式引入,则构建时要忽略相关资源 if (isProduction || devNeedCdn) config.externals = cdn.externals }

(4) 当前配置的vue.config.js

// 是否为生产环境 const isProduction = process.env.NODE_ENV !== 'development' // 本地环境是否需要使用cdn const devNeedCdn = false // cdn链接 const cdn = { // cdn:模块名称和模块作用域命名(对应window里面挂载的变量名称) externals: { vue: 'Vue', vuex: 'Vuex', 'vue-router': 'VueRouter' }, // cdn的css链接 css: [], // cdn的js链接 js: [ 'cdn.staticfile.org/vue/2.6.10/vue.min.js', 'cdn.staticfile.org/vuex/3.0.1/vuex.min.js', 'cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js' ] } module.exports = { productionSourceMap: false, chainWebpack: config => { // ============压缩图片 start============ config.module .rule('images') .use('image-webpack-loader') .loader('image-webpack-loader') .options({ bypassOnDebug: true }) .end() // ============压缩图片 end============ // ============注入cdn start============ config.plugin('html').tap(args => { // 生产环境或本地需要cdn时,才注入cdn if (isProduction || devNeedCdn) args[0].cdn = cdn return args }) // ============注入cdn start============ }, configureWebpack: config => { // 用cdn方式引入,则构建时要忽略相关资源 if (isProduction || devNeedCdn) config.externals = cdn.externals } }

(5) 在public index.html 写入

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta cdn.staticfile.org/vue/2.6.10/vue.min.js', 'cdn.staticfile.org/vuex/3.0.1/vuex.min.js', 'cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js' ] } module.exports = { productionSourceMap: false, chainWebpack: config => { // ============压缩图片 start============ config.module .rule('images') .use('image-webpack-loader') .loader('image-webpack-loader') .options({ bypassOnDebug: true }) .end() // ============压缩图片 end============ // ============注入cdn start============ config.plugin('html').tap(args => { // 生产环境或本地需要cdn时,才注入cdn if (isProduction || devNeedCdn) args[0].cdn = cdn return args }) // ============注入cdn start============ }, configureWebpack: config => { // 用cdn方式引入,则构建时要忽略相关资源 if (isProduction || devNeedCdn) config.externals = cdn.externals // 生产环境相关配置 if (isProduction) { // 代码压缩 config.plugins.push( new UglifyJsPlugin({ uglifyOptions: { //生产环境自动删除console compress: { warnings: false, // 若打包错误,则注释这行 drop_debugger: true, drop_console: true, pure_funcs: ['console.log'] } }, sourceMap: false, parallel: true }) ) // gzip压缩 const productionGzipExtensions = ['html', 'js', 'css'] config.plugins.push( new CompressionWebpackPlugin({ filename: '[path].gz[query]', algorithm: 'gzip', test: new RegExp( '\\.(' + productionGzipExtensions.join('|') + ')$' ), threshold: 10240, // 只有大小大于该值的资源会被处理 10240 minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理 deleteOriginalAssets: false // 删除原文件 }) ) // 公共代码抽离 config.optimization = { splitChunks: { cacheGroups: { vendor: { chunks: 'all', test: /node_modules/, name: 'vendor', minChunks: 1, maxInitialRequests: 5, minSize: 0, priority: 100 }, common: { chunks: 'all', test: /[\\/]src[\\/]js[\\/]/, name: 'common', minChunks: 2, maxInitialRequests: 5, minSize: 0, priority: 60 }, styles: { name: 'styles', test: /\.(sa|sc|c)ss$/, chunks: 'all', enforce: true }, runtimeChunk: { name: 'manifest' } } } } } } }

以上这篇vue打包通过image-webpack-loader插件对图片压缩优化操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

如何利用image-webpack-loader插件在Vue打包过程中对图片进行高效压缩优化?