将 React 构建目录中的文件名缩短至少于 32 个字符
这听起来很愚蠢,但我需要一种方法来缩短 React 构建文件夹的文件名,因为我需要将 React 应用程序上传到 ESP32 上的 SPIFFS 文件系统,该系统仅支持最多 32 个字符的文件名(包括文件夹) 。
我认为一个简单的方法是删除文件末尾的哈希值,但我不知道该怎么做。
那么如何缩短 React 应用程序的文件名呢?
This is going to sound really dumb but I need a way to shorten file names of my React build folder because I need to upload my React app to a SPIFFS file system on an ESP32 that only supports file names up to 32 characters (including folders).
I think an easy way would be to get rid of the hashes at the end of the files but I don't know how to do that.
So how can I shorten the file names of a React app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,不要完全摆脱哈希,它们对于缓存失效非常有用。我还建议不要从头开始自己的构建,除非您知道自己在做什么; CRA 确实很擅长这些东西,即使它确实抛出了一堆你可能永远不会使用的东西。 (Vite 模板也是一个很好的解决方案,请参阅其他答案,尤其是那里的评论)。
如果您决定使用 CRA,请首先运行
npm runject
。这意味着您现在将负责自己的配置,而不是完全依赖 create-react-app,但您需要它来更改您的 Webpack 配置(或者您可以使用react-app-rewired、customize-cra 或某些其他类似的选项)。更改 输出 使用较短的 contenthash(可能是 4)并更改
[name]
表示较短的内容,例如仅返回两个字符的随机字符生成器的结果。如果您总是只生成一个包,请使用静态名称而不是动态名称(在括号中)。如果不需要 chunk,请删除 chunk 相关配置。如果您想要最简单的解决方案,仍然从
create-react-app
开始并弹出,但将所有output
更改为如下所示:并且肯定会 gzip 输出,使用 Make 目标或 shell 脚本或包。
Firstly, don't get rid of hashes entirely, they're incredibly useful for cache invalidation. I also recommend not starting your own build from scratch unless you know what you're doing; CRA is really good at this stuff, even if it does throw in a bunch of stuff you probably will never use. (Vite templates are also a good solution, see the other answer and especially the comments there).
If you're set on using CRA, first run
npm run eject
. This means you'll now be in charge of your own config rather than relying entirely on create-react-app, but you need this to change your Webpack config (or you could use react-app-rewired, customize-cra, or some other similar option).Change the output to use a shorter contenthash (maybe 4) and change out
[name]
for something shorter, like the result of a random character generator that only returns two chars. If you always just produce one bundle, use a static name rather than something dynamic (in brackets). If you don't want chunks, delete the chunk related config.And if you want the simplest possible solution, still start from
create-react-app
and ejecting, but change all ofoutput
to something like this:And definitely gzip the output, with a Make target or shell script or a package.
我使用了 vite 和 preact 创建我的 React for ESP32 项目 (
npm create vite@latest my_project --template preact
)。 vite 使用 rollup 作为捆绑器,并且 rollup 有简单的配置选项来更改其捆绑策略。为了创建与 ESP32 SPIFFS 兼容的文件名(即不超过 31 个字符的完整路径),我在
vite.config.js
中使用了以下配置选项:您可以阅读有关汇总配置选项的信息 此处。
I used vite with preact to create my React for ESP32 project (
npm create vite@latest my_project --template preact
). vite uses rollup as it's bundler and rollup has simple configuration options to change its bundling strategy.To create filenames compatible with ESP32 SPIFFS (i.e. the full path not longer than 31 characters), I used the following config options in
vite.config.js
:You can read about the rollup configuration options here.