|
| 1 | +# Migration Guide |
| 2 | + |
| 3 | +## From v1.x to v2.x |
| 4 | + |
| 5 | +### Breaking Changes |
| 6 | + |
| 7 | +#### 1. `algorithm` → `algorithms` (Array Support) |
| 8 | + |
| 9 | +- **Old**: Single algorithm with `algorithm` option |
| 10 | +- **New**: Multiple algorithms with `algorithms` array |
| 11 | + |
| 12 | +#### 2. Removed `compressionOptions` |
| 13 | + |
| 14 | +- **Old**: Separate `compressionOptions` object |
| 15 | +- **New**: Use `defineAlgorithm()` helper function |
| 16 | + |
| 17 | +#### 3. Enhanced Algorithm Definition |
| 18 | + |
| 19 | +- **New**: Support for custom algorithm functions |
| 20 | +- **New**: Better type safety with `defineAlgorithm()` |
| 21 | + |
| 22 | +### Migration Examples |
| 23 | + |
| 24 | +#### Basic Algorithm Configuration |
| 25 | + |
| 26 | +**Before (v1.x)** |
| 27 | + |
| 28 | +```js |
| 29 | +import { compression } from 'vite-plugin-compression' |
| 30 | + |
| 31 | +compression({ |
| 32 | + algorithm: 'gzip', |
| 33 | + compressionOptions: { level: 9 } |
| 34 | +}) |
| 35 | +``` |
| 36 | + |
| 37 | +**After (v2.x)** |
| 38 | + |
| 39 | +```js |
| 40 | +import { compression, defineAlgorithm } from 'vite-plugin-compression2' |
| 41 | + |
| 42 | +compression({ |
| 43 | + algorithms: [defineAlgorithm('gzip', { level: 9 })] |
| 44 | +}) |
| 45 | +``` |
| 46 | + |
| 47 | +#### Multiple Algorithms |
| 48 | + |
| 49 | +**Before (v1.x)** |
| 50 | + |
| 51 | +```js |
| 52 | +// Required multiple plugin instances |
| 53 | +compression({ algorithm: 'gzip' }), compression({ algorithm: 'brotliCompress' }) |
| 54 | +``` |
| 55 | + |
| 56 | +**After (v2.x)** |
| 57 | + |
| 58 | +```js |
| 59 | +import { compression, defineAlgorithm } from 'vite-plugin-compression2' |
| 60 | + |
| 61 | +compression({ |
| 62 | + algorithms: [ |
| 63 | + defineAlgorithm('gzip', { level: 9 }), |
| 64 | + defineAlgorithm('brotliCompress', { |
| 65 | + params: { |
| 66 | + [require('zlib').constants.BROTLI_PARAM_QUALITY]: 11 |
| 67 | + } |
| 68 | + }) |
| 69 | + ] |
| 70 | +}) |
| 71 | +``` |
| 72 | + |
| 73 | +#### Mixed Algorithm Types |
| 74 | + |
| 75 | +**New in v2.x** - Support for custom algorithm functions: |
| 76 | + |
| 77 | +```js |
| 78 | +import { compression, defineAlgorithm } from 'vite-plugin-compression2' |
| 79 | + |
| 80 | +compression({ |
| 81 | + algorithms: [ |
| 82 | + // Built-in algorithm |
| 83 | + 'gzip', |
| 84 | + |
| 85 | + // Built-in algorithm with options |
| 86 | + defineAlgorithm('brotliCompress', { |
| 87 | + params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 8 } |
| 88 | + }), |
| 89 | + |
| 90 | + // Custom algorithm function |
| 91 | + defineAlgorithm( |
| 92 | + async (buffer, options) => { |
| 93 | + // Your custom compression logic |
| 94 | + return compressedBuffer |
| 95 | + }, |
| 96 | + { customOption: true } |
| 97 | + ) |
| 98 | + ] |
| 99 | +}) |
| 100 | +``` |
| 101 | + |
| 102 | +### Step-by-Step Migration |
| 103 | + |
| 104 | +#### Step 1: Update Package |
| 105 | + |
| 106 | +```bash |
| 107 | +npm uninstall vite-plugin-compression |
| 108 | +npm install vite-plugin-compression2 -D |
| 109 | +``` |
| 110 | + |
| 111 | +#### Step 2: Update Import |
| 112 | + |
| 113 | +```js |
| 114 | +// Old |
| 115 | +import { compression } from 'vite-plugin-compression' |
| 116 | + |
| 117 | +// New |
| 118 | +import { compression, defineAlgorithm } from 'vite-plugin-compression2' |
| 119 | +``` |
| 120 | + |
| 121 | +#### Step 3: Update Configuration |
| 122 | + |
| 123 | +**Simple case:** |
| 124 | + |
| 125 | +```js |
| 126 | +// Old |
| 127 | +compression({ |
| 128 | + algorithm: 'gzip' |
| 129 | +}) |
| 130 | + |
| 131 | +// New |
| 132 | +compression({ |
| 133 | + algorithms: ['gzip'] // or [defineAlgorithm('gzip')] |
| 134 | +}) |
| 135 | +``` |
| 136 | + |
| 137 | +**With options:** |
| 138 | + |
| 139 | +```js |
| 140 | +// Old |
| 141 | +compression({ |
| 142 | + algorithm: 'gzip', |
| 143 | + compressionOptions: { level: 6 } |
| 144 | +}) |
| 145 | + |
| 146 | +// New |
| 147 | +compression({ |
| 148 | + algorithms: [defineAlgorithm('gzip', { level: 6 })] |
| 149 | +}) |
| 150 | +``` |
| 151 | + |
| 152 | +#### Step 4: Update Multiple Instances |
| 153 | + |
| 154 | +```js |
| 155 | +// Old - Multiple plugin instances |
| 156 | +export default defineConfig({ |
| 157 | + plugins: [ |
| 158 | + compression({ algorithm: 'gzip' }), |
| 159 | + compression({ algorithm: 'brotliCompress' }) |
| 160 | + ] |
| 161 | +}) |
| 162 | + |
| 163 | +// New - Single plugin instance |
| 164 | +export default defineConfig({ |
| 165 | + plugins: [ |
| 166 | + compression({ |
| 167 | + algorithms: ['gzip', 'brotliCompress'] |
| 168 | + }) |
| 169 | + ] |
| 170 | +}) |
| 171 | +``` |
| 172 | + |
| 173 | +### New Features in v2.x |
| 174 | + |
| 175 | +#### 1. Simplified Array Syntax |
| 176 | + |
| 177 | +```js |
| 178 | +compression({ |
| 179 | + algorithms: ['gzip', 'brotliCompress'] // Auto-uses default options |
| 180 | +}) |
| 181 | +``` |
| 182 | + |
| 183 | +#### 2. Enhanced Type Safety |
| 184 | + |
| 185 | +```js |
| 186 | +// TypeScript will infer correct option types |
| 187 | +const gzipAlg = defineAlgorithm('gzip', { level: 9 }) // ZlibOptions |
| 188 | +const brotliAlg = defineAlgorithm('brotliCompress', { |
| 189 | + params: {/* BrotliOptions */} |
| 190 | +}) |
| 191 | +``` |
| 192 | + |
| 193 | +#### 3. Custom Algorithm Support |
| 194 | + |
| 195 | +```js |
| 196 | +const customAlgorithm = defineAlgorithm( |
| 197 | + async (buffer: Buffer, options: { quality: number }) => { |
| 198 | + // Custom compression implementation |
| 199 | + return compressedBuffer; |
| 200 | + }, |
| 201 | + { quality: 8 } |
| 202 | +); |
| 203 | +``` |
| 204 | + |
| 205 | +### Compatibility Notes |
| 206 | + |
| 207 | +#### Unchanged Options |
| 208 | + |
| 209 | +These options work the same way in v2.x: |
| 210 | + |
| 211 | +- ✅ `include` |
| 212 | +- ✅ `exclude` |
| 213 | +- ✅ `threshold` |
| 214 | +- ✅ `filename` |
| 215 | +- ✅ `deleteOriginalAssets` |
| 216 | +- ✅ `skipIfLargerOrEqual` |
| 217 | + |
| 218 | +#### Filename Patterns |
| 219 | + |
| 220 | +Filename patterns remain the same: |
| 221 | + |
| 222 | +```js |
| 223 | +compression({ |
| 224 | + filename: '[path][base].gz', // Still works |
| 225 | + algorithms: ['gzip'] |
| 226 | +}) |
| 227 | +``` |
| 228 | + |
| 229 | +### Common Migration Patterns |
| 230 | + |
| 231 | +#### Pattern 1: Default Gzip |
| 232 | + |
| 233 | +```js |
| 234 | +// v1.x |
| 235 | +compression() |
| 236 | + |
| 237 | +// v2.x |
| 238 | +compression() // Still works! Defaults to ['gzip', 'brotliCompress'] |
| 239 | +``` |
| 240 | + |
| 241 | +#### Pattern 2: Custom Gzip Level |
| 242 | + |
| 243 | +```js |
| 244 | +// v1.x |
| 245 | +compression({ |
| 246 | + algorithm: 'gzip', |
| 247 | + compressionOptions: { level: 6 } |
| 248 | +}) |
| 249 | + |
| 250 | +// v2.x |
| 251 | +compression({ |
| 252 | + algorithms: [defineAlgorithm('gzip', { level: 6 })] |
| 253 | +}) |
| 254 | +``` |
| 255 | + |
| 256 | +#### Pattern 3: Brotli Compression |
| 257 | + |
| 258 | +```js |
| 259 | +// v1.x |
| 260 | +compression({ |
| 261 | + algorithm: 'brotliCompress', |
| 262 | + compressionOptions: { |
| 263 | + params: { |
| 264 | + [zlib.constants.BROTLI_PARAM_QUALITY]: 11 |
| 265 | + } |
| 266 | + } |
| 267 | +}) |
| 268 | + |
| 269 | +// v2.x |
| 270 | +compression({ |
| 271 | + algorithms: [defineAlgorithm('brotliCompress', { |
| 272 | + params: { |
| 273 | + [zlib.constants.BROTLI_PARAM_QUALITY]: 11 |
| 274 | + } |
| 275 | + })] |
| 276 | +}) |
| 277 | +``` |
| 278 | + |
| 279 | +### Troubleshooting |
| 280 | + |
| 281 | +#### Issue: TypeScript Errors |
| 282 | + |
| 283 | +**Solution**: Make sure to import `defineAlgorithm`: |
| 284 | + |
| 285 | +```js |
| 286 | +import { compression, defineAlgorithm } from 'vite-plugin-compression2' |
| 287 | +``` |
| 288 | + |
| 289 | +#### Issue: Options Not Applied |
| 290 | + |
| 291 | +**Solution**: Use `defineAlgorithm()` for custom options: |
| 292 | + |
| 293 | +```js |
| 294 | +// Wrong |
| 295 | +algorithms: ;['gzip'] |
| 296 | + |
| 297 | +// Correct with options |
| 298 | +algorithms: ;[defineAlgorithm('gzip', { level: 9 })] |
| 299 | +``` |
| 300 | + |
| 301 | +#### Issue: Multiple Plugin Instances |
| 302 | + |
| 303 | +**Solution**: Combine into single instance: |
| 304 | + |
| 305 | +```js |
| 306 | +// Old approach |
| 307 | +compression({ algorithm: 'gzip' }), compression({ algorithm: 'brotliCompress' }) |
| 308 | + |
| 309 | +// New approach |
| 310 | +compression({ |
| 311 | + algorithms: ['gzip', 'brotliCompress'] |
| 312 | +}) |
| 313 | +``` |
| 314 | + |
| 315 | +### Need Help? |
| 316 | + |
| 317 | +- 📖 Check the [Q&A Guide](./Q&A.md) |
| 318 | +- 🐛 Report issues on [GitHub Issues](https://github.com/nonzzz/vite-plugin-compression/issues) |
| 319 | +- 💡 See more examples in the [README](./README.md) |
0 commit comments