Skip to content

Commit 0de7f77

Browse files
Major change: support for multiple algorithms (#74)
* chore: switch build tool * feat: add defineAlgorithm and init major * test: add compression test * chore: docs * chore: filename * test: add rolldown vite e2e * chore: fix typo * chore: ci * chore: lock file * chore: make type beter --------- Co-authored-by: mengdaoshizhongxinyang <[email protected]>
1 parent 13d0c2d commit 0de7f77

21 files changed

+1698
-3552
lines changed

.github/workflows/e2e-test.yaml

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
run-e2e-test:
77
strategy:
88
matrix:
9-
version: [16, 18]
9+
version: [22, 23, 24]
1010
os: [ubuntu-latest]
1111
runs-on: ${{ matrix.os }}
1212
steps:
@@ -19,23 +19,5 @@ jobs:
1919
- name: install playwright
2020
run: pnpm playwright install
2121

22-
- name: test 2.x to 4.x
23-
run: pnpm exec vitest e2e/vite[2-4]/*.spec.ts --coverage.enabled=false
24-
run-stable-e2e-test:
25-
strategy:
26-
matrix:
27-
version: [18, 20]
28-
os: [ubuntu-latest]
29-
runs-on: ${{ matrix.os }}
30-
steps:
31-
- uses: actions/checkout@v4
32-
with:
33-
node-version: ${{ matrix.version }}
34-
- name: Instanll pnpm
35-
run: make install
36-
37-
- name: install playwright
38-
run: pnpm playwright install
39-
40-
- name: test 5.x
41-
run: pnpm exec vitest e2e/vite[5-6]/*.spec.ts --coverage.enabled=false
22+
- name: e2e test
23+
run: make end-to-end-test

MIGRATION-GUIDE.md

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
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)

Makefile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
JK = pnpm exec jiek -f vite-plugin-compression2
1+
ROLLUP = pnpm exec rollup --config rollup.config.mjs
22

33
install:
44
@echo "Setup pnpm package manager..."
@@ -9,15 +9,25 @@ install:
99
build:
1010
@echo "Building..."
1111
-rm -rf dist
12-
$(JK) build
13-
mv 'dist/index.min.js' 'dist/index.js'
14-
mv 'dist/index.min.mjs' 'dist/index.mjs'
12+
$(ROLLUP)
1513

1614
bootstrap: install build
1715

18-
build-pub: install build
16+
build-pub: install
1917
@echo "Building for publish..."
20-
$(JK) pub -no-b
18+
@$(MAKE) publish
19+
20+
publish: build
21+
@echo "Publishing package..."
22+
$(eval VERSION = $(shell awk -F'"' '/"version":/ {print $4}' package.json))
23+
$(eval TAG = $(shell echo $(VERSION) | awk -F'-' '{if (NF > 1) print $$2; else print ""}' | cut -d'.' -f1))
24+
$(eval FLAGS += $(shell \
25+
if [ "$(TAG)" != "" ]; then \
26+
echo "--tag $(TAG)"; \
27+
fi \
28+
))
29+
@npm publish $(FLAGS) --provenance
30+
2131

2232
test:
2333
@echo "Running tests..."

0 commit comments

Comments
 (0)