-
Notifications
You must be signed in to change notification settings - Fork 4
feat: 支持 vite-imagetools 6.x #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jhygreatbug
wants to merge
6
commits into
explore-frontend:main
Choose a base branch
from
jhygreatbug:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81e97e1
feat: 支持 vite-imagetools 6.x
jhygreatbug 85b31a6
style: 去掉无用的注释
jhygreatbug 806d927
feat: 兼容vite-imagetools@5.x
jhygreatbug b500960
docs: 更新README
jhygreatbug 8c6f982
feat: 更新.d.ts
jhygreatbug 4a8d0bd
docs: 修改文档
jhygreatbug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,20 +1,31 @@ | ||
| import { extname } from 'node:path'; | ||
| import { defineConfig } from 'vite'; | ||
| import vue from '@vitejs/plugin-vue'; | ||
| import { imagetools } from 'vite-imagetools'; | ||
| import imagePresets, { formatPreset } from 'vite-plugin-image-presets'; | ||
|
|
||
| // https://vitejs.dev/config/ | ||
| export default defineConfig({ | ||
| plugins: [ | ||
| vue(), | ||
| imagePresets({ | ||
| modern: formatPreset({ | ||
| formats: { | ||
| avif: {}, | ||
| webp: {}, | ||
| original: {}, | ||
| // imagePresets({ | ||
| // modern: formatPreset({ | ||
| // formats: { | ||
| // avif: {}, | ||
| // webp: {}, | ||
| // original: {}, | ||
| // }, | ||
| // loading: 'lazy', | ||
| // }), | ||
| // }), | ||
| // 验证否兼容vite-imagetools | ||
| imagetools({ | ||
| defaultDirectives: (url) => { | ||
| return new URLSearchParams({ | ||
| format: 'avif;webp;' + extname(url.pathname).slice(1), | ||
| as: 'picture', | ||
| }); | ||
| }, | ||
| loading: 'lazy', | ||
| }), | ||
| }), | ||
| ], | ||
| }); |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,20 +9,43 @@ export type ImgOption = { | |
| src: string; | ||
| } & SimpleImgHTMLAttributes; | ||
|
|
||
| export type ImageToolsPictureOptionImg = { | ||
| src: string; | ||
| w?: number; | ||
| } & SimpleImgHTMLAttributes; | ||
|
|
||
| // TODO: 封装 provider 来应对不同的接口 | ||
| /** vite-imagetools 风格的 picture 数据格式 */ | ||
| export type ImageToolsPictureOption = { | ||
| fallback: { | ||
| src: string; | ||
| w?: number; | ||
| } & SimpleImgHTMLAttributes; | ||
| // avif: [{src: 'xxx.avif'}], webp: [{src: xx.webp}] | ||
| img: ImageToolsPictureOptionImg; | ||
| // 帮助区分 ImageToolsPictureOptionOld | ||
| fallback: never; | ||
| // avif: 'xxx.avif', webp: 'xx.webp' | ||
| sources: { | ||
| [key: string]: string; | ||
| }; | ||
| }; | ||
|
|
||
| // v6.0.0以下 | ||
| export type ImageToolsPictureOptionOld = { | ||
| // avif: [{src: 'xxx.avif'}], webp: [{src: xx.webp}]({ | ||
| sources: { | ||
| [key: string]: { | ||
| src: string; | ||
| w?: number; | ||
| }[]; | ||
| }; | ||
| img: never; | ||
| fallback: ImageToolsPictureOptionImg; | ||
| } | { | ||
| sources: { | ||
| [key: string]: { | ||
| src: string; | ||
| w?: number; | ||
| }[]; | ||
| }; | ||
| img: ImageToolsPictureOptionImg; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个old是代表啥,之前支持这样吗?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ImageToolsPictureOptionOld 吗,是 4.x 和 5.x 的类型,跟最新的 6.x 区分了下 |
||
| fallback: never; | ||
| }; | ||
|
|
||
|
|
||
|
|
@@ -44,11 +67,11 @@ export interface SimpleImgHTMLAttributes { | |
| type?: string; | ||
| } | ||
|
|
||
| export type PictureOption = ImagePresetPictureOption | ImageToolsPictureOption; | ||
| export type PictureOption = ImagePresetPictureOption | ImageToolsPictureOption | ImageToolsPictureOptionOld; | ||
|
|
||
| // 这里的属性其实也有点奇怪...理论上大部分只需要放在最后一个就可以了 | ||
| // 其实跟生产端不太一样 | ||
| interface PictureProp { | ||
| export interface PictureProp { | ||
| src: PictureOption; | ||
| // color 会展示一个渐变色块的 loading 效果,加上 fade-in 的加载成功的渐变 | ||
| placeholder?: 'empty' | 'color'; | ||
|
|
@@ -90,22 +113,29 @@ function assertNotNil<T>(v: T, message?: string): asserts v is NonNullable<T> { | |
| throw new Error(message ?? 'Must not be null or undefined'); | ||
| } | ||
| } | ||
| function isImageToolsPictureOption(sources: PictureOption): sources is ImageToolsPictureOption | ImageToolsPictureOptionOld { | ||
| if (!('fallback' in sources) && !('img' in sources)) { | ||
| return false; | ||
| } | ||
| return 'sources' in sources; | ||
| } | ||
| const allSources = computed(() => props.src); | ||
| const sources = computed<{srcset?: string; type?: string;}[]>(() => | ||
| 'fallback' in allSources.value | ||
| ? Object.entries(allSources.value.sources ?? {}).map(([key, val]) => { | ||
| const sources = computed<{srcset?: string; type?: string;}[]>(() => { | ||
| if (isImageToolsPictureOption(allSources.value)) { | ||
| return Object.entries(allSources.value.sources ?? {}).map(([key, val]) => { | ||
| return { | ||
| type: `image/${key}`, | ||
| srcset: val[0]?.src, | ||
| } | ||
| }) | ||
| : allSources.value.slice(0, -1), | ||
| ); | ||
| srcset: typeof val === 'string' ? val : val[0]?.src, | ||
| }; | ||
| }); | ||
| } | ||
| return allSources.value.slice(0, -1); | ||
| }); | ||
| const lastSource = computed(() => { | ||
| const res = | ||
| 'fallback' in allSources.value | ||
| ? allSources.value.fallback | ||
| : allSources.value[allSources.value.length - 1]; | ||
| const res = isImageToolsPictureOption(allSources.value) | ||
| ? allSources.value.img ?? allSources.value.fallback | ||
| : allSources.value[allSources.value.length - 1] | ||
|
|
||
| assertNotNil(res); | ||
| return res as ImgOption; | ||
| }); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
实际上是4.x,5.x 不是这样的