简体   繁体   English

如何禁用内联小图像的ReactDOMServer.renderToStaticMarkup?

[英]How to disable ReactDOMServer.renderToStaticMarkup from inlining small images?

My React app is currently using ReactDOMServer.renderToStaticMarkup to generate HTML emails. 我的React应用程序当前正在使用ReactDOMServer.renderToStaticMarkup生成HTML电子邮件。

The problem I'm having with ReactDOMServer.renderToStaticMarkup is that it converts small images (under 12kb) from a image src URL to an inline image. 我在ReactDOMServer.renderToStaticMarkup的问题是,它将小图像(12kb以下)从图像src URL转换为嵌入式图像。 This is resulting in images having attachments which is undesirable. 这导致图像具有不期望的附件。

How can I configure ReactDOMServer.renderToStaticMarkup to not inline small images under (12kb)? 如何配置ReactDOMServer.renderToStaticMarkup不内嵌(12kb)以下的小图像?

By the tags i assume you are using create-react-app . 通过标签,我认为您正在使用create-react-app Under the hood it uses url-loader which inlines images smaller than 10kb. 在引擎盖下,它使用 url-loader来内嵌小于10kb的图像。 You can eject webpack config and either increase the threshold or replace url-loader with file-loader . 您可以弹出webpack配置并增加阈值或将url-loader替换为file-loader


Ways to do it without ejecting 不弹出的方式

  • You can put them in a public folder and use them from there. 您可以将它们放在公用文件夹中 ,然后从那里使用它们。 Instead of importing the file it can be referenced like that: <img src={process.env.PUBLIC_URL + '/img/logo.png'} /> 除了导入文件外,还可以这样引用: <img src={process.env.PUBLIC_URL + '/img/logo.png'} />

  • Specify file loader directly in the import. 直接在导入中指定文件加载器。 Ie instead of import imageUrl from './image.png'; 即,而不是import imageUrl from './image.png';import imageUrl from './image.png'; use import imageUrl from '!!file!./image.png'; import imageUrl from '!!file!./image.png';使用import imageUrl from '!!file!./image.png'; . Double exclamation points at the start are used to ignore loaders from webpack config and file! 开头的双感叹号用于忽略webpack配置和file!加载file! means use file-loader, which doesn't do inlining. 表示使用不进行内联的文件加载器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM