vite-plugin-imagemin-upload
is a Vite plugin that facilitates image compression and upload to cloud storage services such as AWS S3 or Alibaba Cloud OSS during the build process. It leverages various image optimization plugins like imagemin to compress images and then uploads the optimized images to the specified cloud storage.
npm install vite-plugin-imagemin-upload --save-dev
// vite.config.js or vite.config.ts
import { imageminUpload } from "vite-plugin-imagemin-upload";
export default {
plugins: [
imageminUpload({
// Options here
}),
],
};
- Specifies the build mode. The plugin will only run in the specified mode.
- Configuration for lossless compression.
type
: 'asset' or 'public' (default: 'public')include
: A filter pattern for files to include in compression (default:/\.(jpe?g|png|gif)$/i
).exclude
: A filter pattern for files to exclude from compression.progressive
: Enable progressive compression for JPEGs (default: true).interlaced
: Enable interlaced compression for GIFs and PNGs (default: true).gifsicle
: Configuration for imagemin-gifsicle (default: true).jpegtran
: Configuration for imagemin-jpegtran (default: true).optipng
: Configuration for imagemin-optipng (default: true).svgo
: Configuration for imagemin-svgo (default: true).webp
: Configuration for imagemin-webp (default: true).
- Configuration for lossy compression.
type
: 'asset' or 'public' (default: 'asset')include
: A filter pattern for files to include in compression (default:/\.(jpe?g|png|gif)$/i
).exclude
: A filter pattern for files to exclude from compression.quality
: Quality setting for compression (default: 80).progressive
: Enable progressive compression for JPEGs (default: true).interlaced
: Enable interlaced compression for GIFs and PNGs (default: true).gifsicle
: Configuration for imagemin-gifsicle (default: true).mozjpeg
: Configuration for imagemin-mozjpeg (default: true).pngquant
: Configuration for imagemin-pngquant (default: true).svgo
: Configuration for imagemin-svgo (default: true).webp
: Configuration for imagemin-webp (default: true).
- Configuration for AWS S3 storage.
baseURL
: The base URL for accessing S3.dir
: The directory in S3 to upload files to.client
: Configuration for the S3 client.head
: Configuration for the S3HeadObjectCommand
.put
: Configuration for the S3PutObjectCommand
.
- Configuration for Alibaba Cloud OSS storage.
baseURL
: The base URL for accessing OSS.dir
: The directory in OSS to upload files to.client
: Configuration for the OSS client.head
: Configuration for the OSShead
method.put
: Configuration for the OSSput
method.
// main.js or main.ts
import "vite-plugin-imagemin-upload/polyfill";
// vite.config.js or vite.config.ts
import { imageminUpload } from "vite-plugin-imagemin-upload";
export default {
plugins: [imageminUpload()],
};
// main.js or main.ts
import "vite-plugin-imagemin-upload/polyfill";
// vite.config.js or vite.config.ts
import { imageminUpload } from "vite-plugin-imagemin-upload";
import { name } from "./package.json";
export default {
plugins: [
imageminUpload({
s3: {
baseURL: "https://your-s3-bucket.s3.amazonaws.com",
dir: name + "/images",
client: {
region: "your-s3-region",
credentials: {
accessKeyId: "your-s3-id",
secretAccessKey: "your-s3-key",
},
},
head: {
Bucket: "your-s3-bucket",
},
put: {
Bucket: "your-s3-bucket",
ACL: "public-read",
},
},
}),
],
};
// main.js or main.ts
import "vite-plugin-imagemin-upload/polyfill";
// vite.config.js or vite.config.ts
import { imageminUpload } from "vite-plugin-imagemin-upload";
import { name } from "./package.json";
export default {
plugins: [
imageminUpload({
oss: {
baseURL: "https://your-oss-bucket.oss-cn-hangzhou.aliyuncs.com",
dir: name + "/images",
client: {
region: "your-oss-region",
bucket: "your-oss-bucket",
accessKeyId: "your-oss-id",
accessKeySecret: "your-oss-key",
},
},
}),
],
};
// vite.config.js or vite.config.ts
import { imageminUpload } from "vite-plugin-imagemin-upload";
import { name } from "./package.json";
export default {
plugins: [
imageminUpload({
lossless: {
// All lossless compressions will not be converted to webp.
webp: false,
},
lossy: {
// All lossy compressions will not be converted to webp.
webp: false,
},
}),
],
};
<!-- html -->
<!-- This image will not be converted to webp. -->
<img src="@/assets/images/logo.png?no-webp" />
/* css */
/* This image will not be converted to webp. */
background-image: url("@/assets/images/logo.png?no-webp");
This plugin is licensed under the MIT License - see the LICENSE file for details.