【发布时间】:2019-08-18 09:08:14
【问题描述】:
我在使用 webpack 构建 webp 图像时遇到问题。有 SSR 反应项目。 反应代码示例:
import slotroulette from './slotroulette.png';
import slotrouletteWebp from './slotroulette.webp';
...
return (
...
<picture>
<source srcSet={slotrouletteWebp} type="image/webp" />
<img
css={{ width: '200px' }}
src={slotroulette}
alt="slot-roulette"
/>
</picture>
...
)
);
};
示例 webpack 配置文件:
{
test: /\.(jpe?g|gif|svg|ico|png)$/,
use: [
{
loader: 'file-loader',
options: {
hash: 'sha512',
digest: 'hex',
publicPath: `${CDN_HOST}/pub/`,
outputPath: 'pub/',
name: '[hash].[ext]',
},
},
{
loader: 'image-webpack-loader',
query: {
mozjpeg: {
progressive: true,
},
gifsicle: {
interlaced: false,
},
svgo: {
plugins: [
{
removeViewBox: false,
},
],
},
webp: {
quality: 80,
},
},
},
],
},
在这种情况下,我有下一个错误:
ERROR in .../slotroulette.webp
Module parse failed: Unexpected character '' (1:6)
You may need an appropriate loader to handle this file type.
如果我将 'webp' 添加到 'test'( test: /.(jpe?g|gif|svg|ico|png|webp)$/,) 我将得到下一个问题 webpack convert webp to png that打不开。
【问题讨论】:
-
图片元素中的img元素没有srcSet属性,只有src属性。另外,请确保您的服务器支持提供图片/webp meme 类型
-
lol meme type @Christian4423 - 它是一种新的内容类型吗?
-
@DerekPollard 我相信是的。我不得不将它添加到我的 IIS 配置中,因为它没有被列为默认类型
标签: javascript reactjs webpack webp