-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 improve(@roots/bud-compress): general improvements (#2092)
- deprecates bespoke compression functions for `brotli` and `gzip` - tightens up the public type declarations and bud augmentations for easier configuration refers: - #2079 ## Type of change **PATCH: backwards compatible change** This PR includes breaking changes to the following core packages: - none This PR includes breaking changes to the follow extensions: - none ## Dependencies ### Adds - none ### Removes - none
- Loading branch information
1 parent
6a8d448
commit b91f9de
Showing
9 changed files
with
259 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {factory, Bud} from '@repo/test-kit/bud' | ||
import {beforeEach, describe, expect, it, vitest} from 'vitest' | ||
import BudBrotli from './brotli.js' | ||
import BudGzip from './gzip.js' | ||
|
||
import Brotli from './brotli.js' | ||
import Compression from './extension.js' | ||
|
||
describe(`@roots/bud-compress`, () => { | ||
let bud: Bud | ||
let compress: Compression | ||
let brotli: Brotli | ||
|
||
beforeEach(async () => { | ||
bud = await factory() | ||
brotli = new Brotli(bud) | ||
compress = new Compression(bud) | ||
|
||
await bud.extensions.add([BudBrotli, BudGzip]) | ||
await compress.register(bud) | ||
}) | ||
|
||
it(`should be constructable`, () => { | ||
expect(brotli).toBeInstanceOf(Brotli) | ||
}) | ||
|
||
it(`should call enabled when config is called`, () => { | ||
const enableSpy = vitest.spyOn(brotli, 'enable') | ||
brotli.config() | ||
expect(enableSpy).toHaveBeenCalled() | ||
}) | ||
it(`should call setOptions when config is called`, () => { | ||
const options = { | ||
algorithm: `test.algorithm`, | ||
filename: `test.filename`, | ||
test: /test-regex$/, | ||
compressionOptions: {test: `option`}, | ||
threshold: 9001, | ||
minRatio: 0.1, | ||
deleteOriginalAssets: false, | ||
} | ||
const setOptionsSpy = vitest.spyOn(brotli, 'setOptions') | ||
brotli.config(options) | ||
expect(setOptionsSpy).toHaveBeenCalledWith(options) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,38 @@ | ||
import {describe, expect, it, test} from 'vitest' | ||
import {factory, Bud} from '@repo/test-kit/bud' | ||
import {beforeEach, describe, expect, it, test} from 'vitest' | ||
import BudBrotli from './brotli.js' | ||
import BudGzip from './gzip.js' | ||
|
||
import Extension from './index.js' | ||
|
||
describe(`@roots/bud-compress`, () => { | ||
let bud: Bud | ||
let compress: Extension | ||
|
||
beforeEach(async () => { | ||
bud = await factory() | ||
compress = new Extension(bud) | ||
await bud.extensions.add([BudBrotli, BudGzip]) | ||
await compress.register(bud) | ||
}) | ||
|
||
it(`should be constructable`, () => { | ||
expect(Extension).toBeInstanceOf(Function) | ||
}) | ||
|
||
test.todo(`improve this spec`) | ||
it(`should have gzip interface`, () => { | ||
expect(compress.gzip).toBeInstanceOf(BudGzip) | ||
}) | ||
|
||
it(`should have br interface`, () => { | ||
expect(compress.brotli).toBeInstanceOf(BudBrotli) | ||
}) | ||
|
||
it(`should have gzip disabled`, () => { | ||
expect(compress.gzip.enabled).toBe(false) | ||
}) | ||
|
||
it(`should have brotli disabled`, () => { | ||
expect(compress.brotli.enabled).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,37 @@ | ||
import type {Bud} from '@roots/bud-framework' | ||
import type {Modules} from '@roots/bud-framework' | ||
import {Extension} from '@roots/bud-framework/extension' | ||
import {bind, label} from '@roots/bud-framework/extension/decorators' | ||
|
||
import Brotli from './brotli.js' | ||
import Gzip from './gzip.js' | ||
import { | ||
bind, | ||
dependsOn, | ||
label, | ||
} from '@roots/bud-framework/extension/decorators' | ||
|
||
/** | ||
* Bud compression extension options | ||
* Compression options | ||
*/ | ||
export interface Options { | ||
filename: string | ||
algorithm: string | ||
test: RegExp | ||
compressionOptions: { | ||
[key: string]: any | ||
} | ||
compressionOptions: Record<string, any> | ||
threshold: number | ||
minRatio: number | ||
deleteOriginalAssets: boolean | ||
} | ||
|
||
/** | ||
* Bud compression extension | ||
* | ||
* @public | ||
* @decorator `@label` | ||
* Compression configuration | ||
*/ | ||
@label(`@roots/bud-compress`) | ||
@dependsOn([`@roots/bud-compress/brotli`, `@roots/bud-compress/gzip`]) | ||
export default class BudCompressionExtension extends Extension<any, any> { | ||
/** | ||
* `register` callback | ||
* | ||
* @public | ||
* @decorator `@bind` | ||
*/ | ||
public declare gzip: Modules[`@roots/bud-compress/gzip`] | ||
public declare brotli: Modules[`@roots/bud-compress/brotli`] | ||
|
||
@bind | ||
public override async register(bud: Bud) { | ||
await bud.extensions.add([Brotli, Gzip]) | ||
this.gzip = bud.extensions.get(`@roots/bud-compress/gzip`) | ||
this.brotli = bud.extensions.get(`@roots/bud-compress/brotli`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {factory, Bud} from '@repo/test-kit/bud' | ||
import {beforeEach, describe, expect, it, vitest} from 'vitest' | ||
|
||
import Gzip from './gzip.js' | ||
import Compression from './extension.js' | ||
|
||
describe(`@roots/bud-compress`, () => { | ||
let bud: Bud | ||
let compress: Compression | ||
let gzip: Gzip | ||
|
||
beforeEach(async () => { | ||
bud = await factory() | ||
gzip = new Gzip(bud) | ||
compress = new Compression(bud) | ||
|
||
await bud.extensions.add([Gzip]) | ||
await compress.register(bud) | ||
}) | ||
|
||
it(`should be constructable`, () => { | ||
expect(gzip).toBeInstanceOf(Gzip) | ||
}) | ||
|
||
it(`should call enabled when config is called`, () => { | ||
const enableSpy = vitest.spyOn(gzip, 'enable') | ||
gzip.config() | ||
expect(enableSpy).toHaveBeenCalled() | ||
}) | ||
it(`should call setOptions when config is called`, () => { | ||
const options = { | ||
algorithm: `test.algorithm`, | ||
filename: `test.filename`, | ||
test: /test-regex$/, | ||
compressionOptions: {test: `option`}, | ||
threshold: 9001, | ||
minRatio: 0.1, | ||
deleteOriginalAssets: false, | ||
} | ||
const setOptionsSpy = vitest.spyOn(gzip, 'setOptions') | ||
gzip.config(options) | ||
expect(setOptionsSpy).toHaveBeenCalledWith(options) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.