Skip to content

Commit 872c612

Browse files
Peter Bengtssonianschmitz
Peter Bengtsson
authored andcommitted
Add environment variable to control image inlining threshold (facebook#6060)
Fixes facebook#3437
1 parent 409b336 commit 872c612

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

β€Ždocusaurus/docs/adding-images-fonts-and-files.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ With Webpack, using static assets like images and fonts works similarly to CSS.
77

88
You can **`import` a file right in a JavaScript module**. This tells Webpack to include that file in the bundle. Unlike CSS imports, importing a file gives you a string value. This value is the final path you can reference in your code, e.g. as the `src` attribute of an image or the `href` of a link to a PDF.
99

10-
To reduce the number of requests to the server, importing images that are less than 10,000 bytes returns a [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) instead of a path. This applies to the following file extensions: bmp, gif, jpg, jpeg, and png. SVG files are excluded due to [#1153](https://github.com/facebook/create-react-app/issues/1153).
10+
To reduce the number of requests to the server, importing images that are less than 10,000 bytes returns a [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) instead of a path. This applies to the following file extensions: bmp, gif, jpg, jpeg, and png. SVG files are excluded due to [#1153](https://github.com/facebook/create-react-app/issues/1153). You can control the 10,000 byte threshold by setting the `IMAGE_INLINE_SIZE_LIMIT` environment variable as documented in our [advanced configuration](advanced-configuration.md).
1111

1212
Here is an example:
1313

β€Ždocusaurus/docs/advanced-configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ You can adjust various development and production settings by setting environmen
2020
| GENERATE_SOURCEMAP | 🚫 Ignored | βœ… Used | When set to `false`, source maps are not generated for a production build. This solves out of memory (OOM) issues on some smaller machines. |
2121
| NODE_PATH | βœ… Used | βœ… Used | Same as [`NODE_PATH` in Node.js](https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders), but only relative folders are allowed. Can be handy for emulating a monorepo setup by setting `NODE_PATH=src`. |
2222
| INLINE_RUNTIME_CHUNK | 🚫 Ignored | βœ… Used | By default, Create React App will embed the runtime script into `index.html` during the production build. When set to `false`, the script will not be embedded and will be imported as usual. This is normally required when dealing with CSP. |
23+
| IMAGE_INLINE_SIZE_LIMIT | 🚫 Ignored | βœ… Used | By default, images smaller than 10,000 bytes are encoded as a data URI in base64 and inlined in the CSS or JS build artifact. Set this to control the size limit in bytes. Setting it to 0 will disable the inlining of images. |

β€Žpackages/react-scripts/config/webpack.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
4444
// makes for a smoother build process.
4545
const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
4646

47+
const imageInlineSizeLimit = parseInt(
48+
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
49+
);
50+
4751
// Check if TypeScript is setup
4852
const useTypeScript = fs.existsSync(paths.appTsConfig);
4953

@@ -343,7 +347,7 @@ module.exports = function(webpackEnv) {
343347
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
344348
loader: require.resolve('url-loader'),
345349
options: {
346-
limit: 10000,
350+
limit: imageInlineSizeLimit,
347351
name: 'static/media/[name].[hash:8].[ext]',
348352
},
349353
},

0 commit comments

Comments
Β (0)