UNPKG

11.7 kBMarkdownView Raw
1<div align="center">
2 <a href="https://github.com/webpack/webpack">
3 <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
4 </a>
5</div>
6
7[![npm][npm]][npm-url]
8[![node][node]][node-url]
9[![deps][deps]][deps-url]
10[![tests][tests]][tests-url]
11[![coverage][cover]][cover-url]
12[![chat][chat]][chat-url]
13[![size][size]][size-url]
14
15# file-loader
16
17The `file-loader` resolves `import`/`require()` on a file into a url and emits the file into the output directory.
18
19## Getting Started
20
21To begin, you'll need to install `file-loader`:
22
23```console
24$ npm install file-loader --save-dev
25```
26
27Import (or `require`) the target file(s) in one of the bundle's files:
28
29**file.js**
30
31```js
32import img from './file.png';
33```
34
35Then add the loader to your `webpack` config. For example:
36
37**webpack.config.js**
38
39```js
40module.exports = {
41 module: {
42 rules: [
43 {
44 test: /\.(png|jpe?g|gif)$/,
45 use: [
46 {
47 loader: 'file-loader',
48 options: {},
49 },
50 ],
51 },
52 ],
53 },
54};
55```
56
57And run `webpack` via your preferred method. This will emit `file.png` as a file
58in the output directory (with the specified naming convention, if options are
59specified to do so) and returns the public URI of the file.
60
61> ℹ️ By default the filename of the resulting file is the hash of the file's contents with the original extension of the required resource.
62
63## Options
64
65### `name`
66
67Type: `String|Function`
68Default: `'[hash].[ext]'`
69
70Specifies a custom filename template for the target file(s) using the query
71parameter `name`. For example, to emit a file from your `context` directory into
72the output directory retaining the full directory structure, you might use:
73
74#### `String`
75
76**webpack.config.js**
77
78```js
79module.exports = {
80 module: {
81 rules: [
82 {
83 test: /\.(png|jpe?g|gif)$/,
84 use: [
85 {
86 loader: 'file-loader',
87 options: {
88 name: '[path][name].[ext]',
89 },
90 },
91 ],
92 },
93 ],
94 },
95};
96```
97
98#### `Function`
99
100**webpack.config.js**
101
102```js
103module.exports = {
104 module: {
105 rules: [
106 {
107 test: /\.(png|jpe?g|gif)$/,
108 use: [
109 {
110 loader: 'file-loader',
111 options: {
112 name(file) {
113 if (process.env.NODE_ENV === 'development') {
114 return '[path][name].[ext]';
115 }
116
117 return '[hash].[ext]';
118 },
119 },
120 },
121 ],
122 },
123 ],
124 },
125};
126```
127
128> ℹ️ By default the path and name you specify will output the file in that same directory, and will also use the same URI path to access the file.
129
130### `outputPath`
131
132Type: `String|Function`
133Default: `undefined`
134
135Specify a filesystem path where the target file(s) will be placed.
136
137#### `String`
138
139**webpack.config.js**
140
141```js
142module.exports = {
143 module: {
144 rules: [
145 {
146 test: /\.(png|jpe?g|gif)$/,
147 use: [
148 {
149 loader: 'file-loader',
150 options: {
151 outputPath: 'images',
152 },
153 },
154 ],
155 },
156 ],
157 },
158};
159```
160
161#### `Function`
162
163**webpack.config.js**
164
165```js
166module.exports = {
167 module: {
168 rules: [
169 {
170 test: /\.(png|jpe?g|gif)$/,
171 use: [
172 {
173 loader: 'file-loader',
174 options: {
175 outputPath: (url, resourcePath, context) => {
176 // `resourcePath` is original absolute path to asset
177 // `context` is directory where stored asset (`rootContext`) or `context` option
178
179 // To get relative path you can use
180 // const relativePath = path.relative(context, resourcePath);
181
182 if (/my-custom-image\.png/.test(resourcePath)) {
183 return `other_output_path/${url}`;
184 }
185
186 if (/images/.test(context)) {
187 return `image_output_path/${url}`;
188 }
189
190 return `output_path/${url}`;
191 },
192 },
193 },
194 ],
195 },
196 ],
197 },
198};
199```
200
201### `publicPath`
202
203Type: `String|Function`
204Default: [`__webpack_public_path__`](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)
205
206Specifies a custom public path for the target file(s).
207
208#### `String`
209
210**webpack.config.js**
211
212```js
213module.exports = {
214 module: {
215 rules: [
216 {
217 test: /\.(png|jpe?g|gif)$/,
218 use: [
219 {
220 loader: 'file-loader',
221 options: {
222 publicPath: 'assets',
223 },
224 },
225 ],
226 },
227 ],
228 },
229};
230```
231
232#### `Function`
233
234**webpack.config.js**
235
236```js
237module.exports = {
238 module: {
239 rules: [
240 {
241 test: /\.(png|jpe?g|gif)$/,
242 use: [
243 {
244 loader: 'file-loader',
245 options: {
246 publicPath: (url, resourcePath, context) => {
247 // `resourcePath` is original absolute path to asset
248 // `context` is directory where stored asset (`rootContext`) or `context` option
249
250 // To get relative path you can use
251 // const relativePath = path.relative(context, resourcePath);
252
253 if (/my-custom-image\.png/.test(resourcePath)) {
254 return `other_public_path/${url}`;
255 }
256
257 if (/images/.test(context)) {
258 return `image_output_path/${url}`;
259 }
260
261 return `public_path/${url}`;
262 },
263 },
264 },
265 ],
266 },
267 ],
268 },
269};
270```
271
272### `context`
273
274Type: `String`
275Default: [`context`](https://webpack.js.org/configuration/entry-context/#context)
276
277Specifies a custom file context.
278
279```js
280module.exports = {
281 module: {
282 rules: [
283 {
284 test: /\.(png|jpe?g|gif)$/,
285 use: [
286 {
287 loader: 'file-loader',
288 options: {
289 context: 'project',
290 },
291 },
292 ],
293 },
294 ],
295 },
296};
297```
298
299### `emitFile`
300
301Type: `Boolean`
302Default: `true`
303
304If true, emits a file (writes a file to the filesystem). If false, the loader
305will return a public URI but **will not** emit the file. It is often useful to
306disable this option for server-side packages.
307
308**file.js**
309
310```js
311// bundle file
312import img from './file.png';
313```
314
315**webpack.config.js**
316
317```js
318module.exports = {
319 module: {
320 rules: [
321 {
322 test: /\.css$/,
323 use: [
324 {
325 loader: 'file-loader',
326 options: {
327 emitFile: false,
328 },
329 },
330 ],
331 },
332 ],
333 },
334};
335```
336
337### `regExp`
338
339Type: `RegExp`
340Default: `undefined`
341
342Specifies a Regular Expression to one or many parts of the target file path.
343The capture groups can be reused in the `name` property using `[N]`
344[placeholder](https://github.com/webpack-contrib/file-loader#placeholders).
345
346**file.js**
347
348```js
349import img from './customer01/file.png';
350```
351
352**webpack.config.js**
353
354```js
355module.exports = {
356 module: {
357 rules: [
358 {
359 test: /\.(png|jpe?g|gif)$/,
360 use: [
361 {
362 loader: 'file-loader',
363 options: {
364 regExp: /\/([a-z0-9]+)\/[a-z0-9]+\.png$/,
365 name: '[1]-[name].[ext]',
366 },
367 },
368 ],
369 },
370 ],
371 },
372};
373```
374
375> ℹ️ If `[0]` is used, it will be replaced by the entire tested string, whereas `[1]` will contain the first capturing parenthesis of your regex and so on...
376
377## Placeholders
378
379Full information about placeholders you can find [here](https://github.com/webpack/loader-utils#interpolatename).
380
381### `[ext]`
382
383Type: `String`
384Default: `file.extname`
385
386The file extension of the target file/resource.
387
388### `[name]`
389
390Type: `String`
391Default: `file.basename`
392
393The basename of the file/resource.
394
395### `[path]`
396
397Type: `String`
398Default: `file.directory`
399
400The path of the resource relative to the webpack/config `context`.
401
402### `[folder]`
403
404Type: `String`
405Default: `file.folder`
406
407The folder of the resource is in.
408
409### `[emoji]`
410
411Type: `String`
412Default: `undefined`
413
414A random emoji representation of `content`.
415
416### `[emoji:<length>]`
417
418Type: `String`
419Default: `undefined`
420
421Same as above, but with a customizable number of emojis
422
423### `[hash]`
424
425Type: `String`
426Default: `md5`
427
428Specifies the hash method to use for hashing the file content.
429
430### `[<hashType>:hash:<digestType>:<length>]`
431
432Type: `String`
433
434The hash of options.content (Buffer) (by default it's the hex digest of the hash).
435
436#### `digestType`
437
438Type: `String`
439Default: `'hex'`
440
441The [digest](https://en.wikipedia.org/wiki/Cryptographic_hash_function) that the
442hash function should use. Valid values include: base26, base32, base36,
443base49, base52, base58, base62, base64, and hex.
444
445#### `hashType`
446
447Type: `String`
448Default: `'md5'`
449
450The type of hash that the has function should use. Valid values include: `md5`,
451`sha1`, `sha256`, and `sha512`.
452
453#### `length`
454
455Type: `Number`
456Default: `undefined`
457
458Users may also specify a length for the computed hash.
459
460### `[N]`
461
462Type: `String`
463Default: `undefined`
464
465The n-th match obtained from matching the current file name against the `regExp`.
466
467## Examples
468
469The following examples show how one might use `file-loader` and what the result would be.
470
471**file.js**
472
473```js
474import png from './image.png';
475```
476
477**webpack.config.js**
478
479```js
480module.exports = {
481 module: {
482 rules: [
483 {
484 test: /\.(png|jpe?g|gif)$/,
485 use: [
486 {
487 loader: 'file-loader',
488 options: {
489 name: 'dirname/[hash].[ext]',
490 },
491 },
492 ],
493 },
494 ],
495 },
496};
497```
498
499Result:
500
501```bash
502# result
503dirname/0dcbbaa701328ae351f.png
504```
505
506---
507
508**file.js**
509
510```js
511import png from './image.png';
512```
513
514**webpack.config.js**
515
516```js
517module.exports = {
518 module: {
519 rules: [
520 {
521 test: /\.(png|jpe?g|gif)$/,
522 use: [
523 {
524 loader: 'file-loader',
525 options: {
526 name: '[sha512:hash:base64:7].[ext]',
527 },
528 },
529 ],
530 },
531 ],
532 },
533};
534```
535
536Result:
537
538```bash
539# result
540gdyb21L.png
541```
542
543---
544
545**file.js**
546
547```js
548import png from './path/to/file.png';
549```
550
551**webpack.config.js**
552
553```js
554module.exports = {
555 module: {
556 rules: [
557 {
558 test: /\.(png|jpe?g|gif)$/,
559 use: [
560 {
561 loader: 'file-loader',
562 options: {
563 name: '[path][name].[ext]?[hash]',
564 },
565 },
566 ],
567 },
568 ],
569 },
570};
571```
572
573Result:
574
575```bash
576# result
577path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
578```
579
580## Contributing
581
582Please take a moment to read our contributing guidelines if you haven't yet done so.
583
584[CONTRIBUTING](./.github/CONTRIBUTING.md)
585
586## License
587
588[MIT](./LICENSE)
589
590[npm]: https://img.shields.io/npm/v/file-loader.svg
591[npm-url]: https://npmjs.com/package/file-loader
592[node]: https://img.shields.io/node/v/file-loader.svg
593[node-url]: https://nodejs.org
594[deps]: https://david-dm.org/webpack-contrib/file-loader.svg
595[deps-url]: https://david-dm.org/webpack-contrib/file-loader
596[tests]: https://dev.azure.com/webpack-contrib/file-loader/_apis/build/status/webpack-contrib.file-loader?branchName=master
597[tests-url]: https://dev.azure.com/webpack-contrib/file-loader/_build/latest?definitionId=2&branchName=master
598[cover]: https://codecov.io/gh/webpack-contrib/file-loader/branch/master/graph/badge.svg
599[cover-url]: https://codecov.io/gh/webpack-contrib/file-loader
600[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
601[chat-url]: https://gitter.im/webpack/webpack
602[size]: https://packagephobia.now.sh/badge?p=file-loader
603[size-url]: https://packagephobia.now.sh/result?p=file-loader