to-zip-webpack-plugin
v1.0.3
Published
A simple and easy-to-use webpack compression plugin that can automatically zip/rar the output.path configured by webpack after packaging, and can customize the compression source, generate compression logs, and automatically generate multiple types of uni
Downloads
3
Readme
to-zip-webpack-plugin
一个简单易用的 webpack
压缩插件,在打包后可以自动对webpack
配置的output.path
进行zip/tar
压缩,并且可以自定义压缩源、生成压缩日志、自动生成多种类型的唯一文件名
NPM : https://www.npmjs.com/package/to-zip-webpack-plugin
npm i --save-dev to-zip-webpack-plugin
webpack.config.js
const ToZipWebpackPlugin = require('to-zip-webpack-plugin')
module.exports = {
entry: 'index.js',
output: {
path: __dirname + '/dist',
filename: 'index_bundle.js'
},
plugins: [
new ToZipWebpackPlugin()
]
}
支持零配置,默认对webpack
打包完成后output.path
进行压缩到path
同级目录。
同时支持多个可选参数:
plugins: [
new ToZipWebpackPlugin({
deleteFileList: ["./last.zip", "/dist"], //在压缩完成后删除deleteFileList中的文件夹或目录
format: "zip", //要压缩的格式 支持zip、tar 默认zip
fileName: "test", //压缩文件名 默认自动生成时间字符串文件名
defaultFileName: "uuid", //当不设置fileName 时会自动生成文件名,支持uuid timestamp time
archive: {
//自定义压缩源/目标,设置archive时以下参数必填
source: __dirname + "/other.config-test.js", //要压缩的文件/目录的路径
targetDir: __dirname, //压缩文件最终的输出目录
fileName: "bundlejs", //压缩的文件名
},
log: true, //是否记录压缩操作日志到当前目录ToZipWebpackPlugin.log
zlibLevel: 9, //设置压缩等级、默认9
}),
]
打包后看到ToZipWebpackPlugin.log
以下输出说明压缩完成:
[2022-05-14T20:05:52.028] [INFO] ToZipWebpackPlugin - Compression finish ! - /Users/wsp/vuetest/bundlejs.zip Size: 0.00M
[2022-05-14T20:05:52.171] [INFO] ToZipWebpackPlugin - Compression finish ! - /Users/wsp/vuetest/497bfd78-7da6-45ab-97b3-0d126981bfcd.zip Size: 0.21M
设置defaultFileName
会自动生成唯一的压缩文件名支持uuid、时间戳、时间字符串 格式yyyymmddhhMMss:
plugins: [
new ToZipWebpackPlugin({
format: "tar", //要压缩的格式 支持zip、tar
defaultFileName: "timestamp", //当不设置fileName 时会自动生成文件名,支持uuid timestamp time
zlibLevel: 3, //设置压缩等级
log:true
}),
],
ToZipWebpackPlugin.log
defaultFileName: "timestamp"
[2022-05-14T20:44:56.946] [INFO] ToZipWebpackPlugin - Compression finish ! - /Users/wsp/vuetest/1652532292483.tar Size: 0.68M
defaultFileName: "time"
[2022-05-14T20:45:47.496] [INFO] ToZipWebpackPlugin - Compression finish ! - /Users/wsp/vuetest/20220514204542.tar Size: 0.68M
内部压缩操作发生在webpack
的compiler
引擎afterEmit
钩子,hook详情:https://webpack.docschina.org/api/compiler-hooks/#afteremit