-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the inferDimensions option (#18)
* feat: add the dimensions option The dimensions option adds `width` and `height` attributes to the <img> element based on the dimensions of the original image. * fix: use dimensions of the last src instead of the input * fix: fix dimensions option in production build * fix: use inferDimensions instead of dimensions as the option name
- Loading branch information
Showing
6 changed files
with
107 additions
and
3 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
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,86 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import type { ImagePreset } from 'vite-plugin-image-presets' | ||
import ImagePresetsPlugin, { formatPreset, widthPreset } from 'vite-plugin-image-presets' | ||
|
||
async function getResolvedImage (preset: ImagePreset, isBuild: boolean) { | ||
const plugin = ImagePresetsPlugin({ default: preset }) | ||
await plugin.configResolved({ | ||
base: '', | ||
command: isBuild ? 'build' : 'serve', | ||
root: __dirname, | ||
build: { assetsDir: '' } as any, | ||
} as any) | ||
|
||
return plugin.api.resolveImage('assets/white.png', { preset: 'default' }) | ||
} | ||
|
||
describe('inferDimensions', () => { | ||
test('original width and height', async () => { | ||
const resolved = await getResolvedImage(formatPreset({ | ||
inferDimensions: true, | ||
formats: { | ||
webp: { quality: 70 }, | ||
original: {}, | ||
}, | ||
}), false) | ||
expect(resolved[1]).toHaveProperty('width', 5) | ||
expect(resolved[1]).toHaveProperty('height', 3) | ||
}) | ||
|
||
test('original width and height in production build', async () => { | ||
const resolved = await getResolvedImage(formatPreset({ | ||
inferDimensions: true, | ||
formats: { | ||
webp: { quality: 70 }, | ||
original: {}, | ||
}, | ||
}), true) | ||
expect(resolved[1]).toHaveProperty('width', 5) | ||
expect(resolved[1]).toHaveProperty('height', 3) | ||
}) | ||
|
||
test('scaled width and height', async () => { | ||
const resolved = await getResolvedImage(widthPreset({ | ||
inferDimensions: true, | ||
widths: [4, 2], | ||
formats: { | ||
webp: { quality: 70 }, | ||
}, | ||
}), false) | ||
expect(resolved[0]).toHaveProperty('width', 2) | ||
expect(resolved[0]).toHaveProperty('height', 1) | ||
}) | ||
|
||
test('overriden by attrs', async () => { | ||
const resolved = await getResolvedImage(formatPreset({ | ||
inferDimensions: true, | ||
height: 20, | ||
formats: { | ||
original: {}, | ||
}, | ||
}), false) | ||
expect(resolved[0]).toHaveProperty('width', 5) | ||
expect(resolved[0]).toHaveProperty('height', 20) | ||
}) | ||
|
||
test('inferDimensions = false', async () => { | ||
const resolved = await getResolvedImage(formatPreset({ | ||
inferDimensions: false, | ||
formats: { | ||
original: {}, | ||
}, | ||
}), false) | ||
expect(resolved[0]).not.toHaveProperty('width') | ||
expect(resolved[0]).not.toHaveProperty('height') | ||
}) | ||
|
||
test('inferDimensions undefined', async () => { | ||
const resolved = await getResolvedImage(formatPreset({ | ||
formats: { | ||
original: {}, | ||
}, | ||
}), false) | ||
expect(resolved[0]).not.toHaveProperty('width') | ||
expect(resolved[0]).not.toHaveProperty('height') | ||
}) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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