Skip to content

Commit 5d290de

Browse files
pugnascotiaTimer
authored andcommitted
Whitelist files that can be embedded through url-loader
Change the current catch-all loader to use file-loader instead of url-loader, and exclude common image file extensons. Add another url-loader for images, configured identically to the original catch-all loader. Part of issue facebook#1293.
1 parent ed5c016 commit 5d290de

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

packages/react-scripts/config/webpack.config.dev.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,34 @@ module.exports = {
134134
// When adding a new loader, you must add its `test`
135135
// as a new entry in the `exclude` list for "url" loader.
136136

137-
// "url" loader embeds assets smaller than specified size as data URLs to avoid requests.
138-
// Otherwise, it acts like the "file" loader.
137+
// "file" loader makes sure those assets get served by WebpackDevServer.
138+
// When you `import` an asset, you get its (virtual) filename.
139+
// In production, they would get copied to the `build` folder.
139140
{
140141
exclude: [
141142
/\.html$/,
142143
/\.(js|jsx)$/,
143144
/\.css$/,
144145
/\.json$/,
145-
/\.svg$/
146+
/\.bmp$/,
147+
/\.gif$/,
148+
/\.jpe?g$/,
149+
/\.png$/
150+
],
151+
loader: 'file-loader',
152+
options: {
153+
name: 'static/media/[name].[hash:8].[ext]'
154+
}
155+
},
156+
// "url" loader works like "file" loader except that it embeds assets
157+
// smaller than specified limit in bytes as data URLs to avoid requests.
158+
// A missing `test` is equivalent to a match.
159+
{
160+
test: [
161+
/\.bmp$/,
162+
/\.gif$/,
163+
/\.jpe?g$/,
164+
/\.png$/
146165
],
147166
loader: 'url-loader',
148167
options: {
@@ -198,14 +217,6 @@ module.exports = {
198217
}
199218
}
200219
]
201-
},
202-
// "file" loader for svg
203-
{
204-
test: /\.svg$/,
205-
loader: 'file-loader',
206-
options: {
207-
name: 'static/media/[name].[hash:8].[ext]'
208-
}
209220
}
210221
// ** STOP ** Are you adding a new loader?
211222
// Remember to add the new extension(s) to the "url" loader exclusion list.

packages/react-scripts/config/webpack.config.prod.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,32 @@ module.exports = {
138138
// When adding a new loader, you must add its `test`
139139
// as a new entry in the `exclude` list in the "url" loader.
140140

141-
// "url" loader embeds assets smaller than specified size as data URLs to avoid requests.
142-
// Otherwise, it acts like the "file" loader.
141+
// "file" loader makes sure those assets end up in the `build` folder.
142+
// When you `import` an asset, you get its filename.
143143
{
144144
exclude: [
145145
/\.html$/,
146146
/\.(js|jsx)$/,
147147
/\.css$/,
148148
/\.json$/,
149-
/\.svg$/
149+
/\.bmp$/,
150+
/\.gif$/,
151+
/\.jpe?g$/,
152+
/\.png$/
153+
],
154+
loader: 'file-loader',
155+
query: {
156+
name: 'static/media/[name].[hash:8].[ext]'
157+
}
158+
},
159+
// "url" loader works just like "file" loader but it also embeds
160+
// assets smaller than specified size as data URLs to avoid requests.
161+
{
162+
test: [
163+
/\.bmp$/,
164+
/\.gif$/,
165+
/\.jpe?g$/,
166+
/\.png$/
150167
],
151168
loader: 'url-loader',
152169
options: {
@@ -209,14 +226,6 @@ module.exports = {
209226
]
210227
}, extractTextPluginOptions))
211228
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
212-
},
213-
// "file" loader for svg
214-
{
215-
test: /\.svg$/,
216-
loader: 'file-loader',
217-
options: {
218-
name: 'static/media/[name].[hash:8].[ext]'
219-
}
220229
}
221230
// ** STOP ** Are you adding a new loader?
222231
// Remember to add the new extension(s) to the "url" loader exclusion list.

0 commit comments

Comments
 (0)