ReactUnity / core

React and HTML framework for Unity UI & UIToolkit
https://reactunity.github.io/
MIT License
723 stars 37 forks source link

[BUG] Images are loaded incorrectly in production #113

Open xiaohai-huang opened 3 weeks ago

xiaohai-huang commented 3 weeks ago

Description

Images load correctly during development, but when the React app is served from the Resources folder, incorrect images are displayed due to identical file names with different extensions.

Steps to Reproduce

  1. Import two images with identical file names but different file extensions from separate folders.
import PlaceholderAvatar from "src/assets/images/icons/default-avatar.png";
import PlayerAvatar from "src/assets/images/default-avatar.jpg";
  1. Build the react app. Below shows the files are all stored under /static/media/ folder. image

  2. ResourcesHelper.LoadResource is unable to load the correct image.

Potential Cause

Currently, the files in Resources folder are loaded without file extension.

https://github.com/ReactUnity/core/blob/fef8c873b41af0922c3cffe6e98d37dc0efee34f/Runtime/Types/AssetReference.cs#L168-L188

https://github.com/ReactUnity/core/blob/fef8c873b41af0922c3cffe6e98d37dc0efee34f/Runtime/Helpers/ResourcesHelper.cs#L55-L63

xiaohai-huang commented 3 weeks ago

My current workaround is to either change the file names or specify the assetModuleFilename in webpack config using webpack-merge.

const { merge } = require("webpack-merge");

module.exports = (env, baseConfig) => {
  const config = merge(baseConfig, {
    output: {
      assetModuleFilename: "static/media/[name].[hash][ext]",
    },
  });

  return config;
};