How Can I Fix Missing WebP Images in Production Build with CRACO Configuration?

I’m working on a React project using Craco to manage Webpack configurations. My goal is to use WebP images for better performance, falling back to PNGs if WebP is not supported. I’ve already converted all my images to WebP and placed them in the same directories as the original PNGs.

However, when I run npm run build, the WebP images are not being included in the build/static/media folder, while the PNGs are.

Here’s a summary of my configurations:

craco.config.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const path = require("path");
module.exports = {
webpack: {
alias: {
"@components": path.resolve(__dirname, "src/components/"),
"@context": path.resolve(__dirname, "src/context/"),
"@assets": path.resolve(__dirname, "src/assets/"),
"@utils": path.resolve(__dirname, "src/utils/"),
"@data": path.resolve(__dirname, "src/data/"),
},
configure: (webpackConfig) => {
console.log("Adding file-loader for WebP");
const oneOfRule = webpackConfig.module.rules.find((rule) => rule.oneOf);
if (oneOfRule) {
console.log("Rule before modification:", oneOfRule.oneOf.map(rule => rule.test));
oneOfRule.oneOf.unshift({
test: /.(png|jpg|jpeg|webp)$/i,
use: [
{
loader: require.resolve("file-loader"),
options: {
name: "static/media/[name].[hash:8].[ext]",
},
},
],
});
console.log("WebP loader applied for: /\.(png|jpg|jpeg|webp)$/i");
console.log("Rule after modification:", oneOfRule.oneOf.map(rule => rule.test));
}
return webpackConfig;
},
},
};
</code>
<code>const path = require("path"); module.exports = { webpack: { alias: { "@components": path.resolve(__dirname, "src/components/"), "@context": path.resolve(__dirname, "src/context/"), "@assets": path.resolve(__dirname, "src/assets/"), "@utils": path.resolve(__dirname, "src/utils/"), "@data": path.resolve(__dirname, "src/data/"), }, configure: (webpackConfig) => { console.log("Adding file-loader for WebP"); const oneOfRule = webpackConfig.module.rules.find((rule) => rule.oneOf); if (oneOfRule) { console.log("Rule before modification:", oneOfRule.oneOf.map(rule => rule.test)); oneOfRule.oneOf.unshift({ test: /.(png|jpg|jpeg|webp)$/i, use: [ { loader: require.resolve("file-loader"), options: { name: "static/media/[name].[hash:8].[ext]", }, }, ], }); console.log("WebP loader applied for: /\.(png|jpg|jpeg|webp)$/i"); console.log("Rule after modification:", oneOfRule.oneOf.map(rule => rule.test)); } return webpackConfig; }, }, }; </code>
const path = require("path");

module.exports = {
  webpack: {
    alias: {
      "@components": path.resolve(__dirname, "src/components/"),
      "@context": path.resolve(__dirname, "src/context/"),
      "@assets": path.resolve(__dirname, "src/assets/"),
      "@utils": path.resolve(__dirname, "src/utils/"),
      "@data": path.resolve(__dirname, "src/data/"),
    },
    configure: (webpackConfig) => {
      console.log("Adding file-loader for WebP");
      const oneOfRule = webpackConfig.module.rules.find((rule) => rule.oneOf);

      if (oneOfRule) {
        console.log("Rule before modification:", oneOfRule.oneOf.map(rule => rule.test));
        oneOfRule.oneOf.unshift({
          test: /.(png|jpg|jpeg|webp)$/i,
          use: [
            {
              loader: require.resolve("file-loader"),
              options: {
                name: "static/media/[name].[hash:8].[ext]",
              },
            },
          ],
        });
        console.log("WebP loader applied for: /\.(png|jpg|jpeg|webp)$/i");
        console.log("Rule after modification:", oneOfRule.oneOf.map(rule => rule.test));
      }

      return webpackConfig;
    },
  },
};

As you can see I added a few logs in the config to show the rules before and after modification. The logs show that the WebP loader is being applied, but the WebP files are still not included in the build.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Adding file-loader for WebP
Rule before modification: [
[ /.avif$/ ],
[ /.bmp$/, /.gif$/, /.jpe?g$/, /.png$/ ],
/.svg$/,
/.(js|mjs|jsx|ts|tsx)$/,
/.(js|mjs)$/,
/.css$/,
/.module.css$/,
/.(scss|sass)$/,
/.module.(scss|sass)$/,
undefined
]
WebP loader applied for: /\.(png|jpg|jpeg|webp)$/i
Rule after modification: [
/.(png|jpg|jpeg|webp)$/i,
[ /.avif$/ ],
[ /.bmp$/, /.gif$/, /.jpe?g$/, /.png$/ ],
/.svg$/,
/.(js|mjs|jsx|ts|tsx)$/,
/.(js|mjs)$/,
/.css$/,
/.module.css$/,
/.(scss|sass)$/,
/.module.(scss|sass)$/,
undefined
]
</code>
<code>Adding file-loader for WebP Rule before modification: [ [ /.avif$/ ], [ /.bmp$/, /.gif$/, /.jpe?g$/, /.png$/ ], /.svg$/, /.(js|mjs|jsx|ts|tsx)$/, /.(js|mjs)$/, /.css$/, /.module.css$/, /.(scss|sass)$/, /.module.(scss|sass)$/, undefined ] WebP loader applied for: /\.(png|jpg|jpeg|webp)$/i Rule after modification: [ /.(png|jpg|jpeg|webp)$/i, [ /.avif$/ ], [ /.bmp$/, /.gif$/, /.jpe?g$/, /.png$/ ], /.svg$/, /.(js|mjs|jsx|ts|tsx)$/, /.(js|mjs)$/, /.css$/, /.module.css$/, /.(scss|sass)$/, /.module.(scss|sass)$/, undefined ] </code>
Adding file-loader for WebP
Rule before modification: [
  [ /.avif$/ ],
  [ /.bmp$/, /.gif$/, /.jpe?g$/, /.png$/ ],
  /.svg$/,
  /.(js|mjs|jsx|ts|tsx)$/,
  /.(js|mjs)$/,
  /.css$/,
  /.module.css$/,
  /.(scss|sass)$/,
  /.module.(scss|sass)$/,
  undefined
]
WebP loader applied for: /\.(png|jpg|jpeg|webp)$/i
Rule after modification: [
  /.(png|jpg|jpeg|webp)$/i,
  [ /.avif$/ ],
  [ /.bmp$/, /.gif$/, /.jpe?g$/, /.png$/ ],
  /.svg$/,
  /.(js|mjs|jsx|ts|tsx)$/,
  /.(js|mjs)$/,
  /.css$/,
  /.module.css$/,
  /.(scss|sass)$/,
  /.module.(scss|sass)$/,
  undefined
]

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật