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 |
|
17 | The `file-loader` resolves `import`/`require()` on a file into a url and emits the file into the output directory.
|
18 |
|
19 | ## Getting Started
|
20 |
|
21 | To begin, you'll need to install `file-loader`:
|
22 |
|
23 | ```console
|
24 | $ npm install file-loader --save-dev
|
25 | ```
|
26 |
|
27 | Import (or `require`) the target file(s) in one of the bundle's files:
|
28 |
|
29 | **file.js**
|
30 |
|
31 | ```js
|
32 | import img from './file.png';
|
33 | ```
|
34 |
|
35 | Then add the loader to your `webpack` config. For example:
|
36 |
|
37 | **webpack.config.js**
|
38 |
|
39 | ```js
|
40 | module.exports = {
|
41 | module: {
|
42 | rules: [
|
43 | {
|
44 | test: /\.(png|jpe?g|gif)$/i,
|
45 | use: [
|
46 | {
|
47 | loader: 'file-loader',
|
48 | },
|
49 | ],
|
50 | },
|
51 | ],
|
52 | },
|
53 | };
|
54 | ```
|
55 |
|
56 | And run `webpack` via your preferred method. This will emit `file.png` as a file
|
57 | in the output directory (with the specified naming convention, if options are
|
58 | specified to do so) and returns the public URI of the file.
|
59 |
|
60 | > ℹ️ By default the filename of the resulting file is the hash of the file's contents with the original extension of the required resource.
|
61 |
|
62 | ## Options
|
63 |
|
64 | ### `name`
|
65 |
|
66 | Type: `String|Function`
|
67 | Default: `'[contenthash].[ext]'`
|
68 |
|
69 | Specifies a custom filename template for the target file(s) using the query
|
70 | parameter `name`. For example, to emit a file from your `context` directory into
|
71 | the output directory retaining the full directory structure, you might use:
|
72 |
|
73 | #### `String`
|
74 |
|
75 | **webpack.config.js**
|
76 |
|
77 | ```js
|
78 | module.exports = {
|
79 | module: {
|
80 | rules: [
|
81 | {
|
82 | test: /\.(png|jpe?g|gif)$/i,
|
83 | loader: 'file-loader',
|
84 | options: {
|
85 | name: '[path][name].[ext]',
|
86 | },
|
87 | },
|
88 | ],
|
89 | },
|
90 | };
|
91 | ```
|
92 |
|
93 | #### `Function`
|
94 |
|
95 | **webpack.config.js**
|
96 |
|
97 | ```js
|
98 | module.exports = {
|
99 | module: {
|
100 | rules: [
|
101 | {
|
102 | test: /\.(png|jpe?g|gif)$/i,
|
103 | loader: 'file-loader',
|
104 | options: {
|
105 | name(file) {
|
106 | if (process.env.NODE_ENV === 'development') {
|
107 | return '[path][name].[ext]';
|
108 | }
|
109 |
|
110 | return '[contenthash].[ext]';
|
111 | },
|
112 | },
|
113 | },
|
114 | ],
|
115 | },
|
116 | };
|
117 | ```
|
118 |
|
119 | > ℹ️ 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.
|
120 |
|
121 | ### `outputPath`
|
122 |
|
123 | Type: `String|Function`
|
124 | Default: `undefined`
|
125 |
|
126 | Specify a filesystem path where the target file(s) will be placed.
|
127 |
|
128 | #### `String`
|
129 |
|
130 | **webpack.config.js**
|
131 |
|
132 | ```js
|
133 | module.exports = {
|
134 | module: {
|
135 | rules: [
|
136 | {
|
137 | test: /\.(png|jpe?g|gif)$/i,
|
138 | loader: 'file-loader',
|
139 | options: {
|
140 | outputPath: 'images',
|
141 | },
|
142 | },
|
143 | ],
|
144 | },
|
145 | };
|
146 | ```
|
147 |
|
148 | #### `Function`
|
149 |
|
150 | **webpack.config.js**
|
151 |
|
152 | ```js
|
153 | module.exports = {
|
154 | module: {
|
155 | rules: [
|
156 | {
|
157 | test: /\.(png|jpe?g|gif)$/i,
|
158 | loader: 'file-loader',
|
159 | options: {
|
160 | outputPath: (url, resourcePath, context) => {
|
161 | // `resourcePath` is original absolute path to asset
|
162 | // `context` is directory where stored asset (`rootContext`) or `context` option
|
163 |
|
164 | // To get relative path you can use
|
165 | // const relativePath = path.relative(context, resourcePath);
|
166 |
|
167 | if (/my-custom-image\.png/.test(resourcePath)) {
|
168 | return `other_output_path/${url}`;
|
169 | }
|
170 |
|
171 | if (/images/.test(context)) {
|
172 | return `image_output_path/${url}`;
|
173 | }
|
174 |
|
175 | return `output_path/${url}`;
|
176 | },
|
177 | },
|
178 | },
|
179 | ],
|
180 | },
|
181 | };
|
182 | ```
|
183 |
|
184 | ### `publicPath`
|
185 |
|
186 | Type: `String|Function`
|
187 | Default: [`__webpack_public_path__`](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)
|
188 |
|
189 | Specifies a custom public path for the target file(s).
|
190 |
|
191 | #### `String`
|
192 |
|
193 | **webpack.config.js**
|
194 |
|
195 | ```js
|
196 | module.exports = {
|
197 | module: {
|
198 | rules: [
|
199 | {
|
200 | test: /\.(png|jpe?g|gif)$/i,
|
201 | loader: 'file-loader',
|
202 | options: {
|
203 | publicPath: 'assets',
|
204 | },
|
205 | },
|
206 | ],
|
207 | },
|
208 | };
|
209 | ```
|
210 |
|
211 | #### `Function`
|
212 |
|
213 | **webpack.config.js**
|
214 |
|
215 | ```js
|
216 | module.exports = {
|
217 | module: {
|
218 | rules: [
|
219 | {
|
220 | test: /\.(png|jpe?g|gif)$/i,
|
221 | loader: 'file-loader',
|
222 | options: {
|
223 | publicPath: (url, resourcePath, context) => {
|
224 | // `resourcePath` is original absolute path to asset
|
225 | // `context` is directory where stored asset (`rootContext`) or `context` option
|
226 |
|
227 | // To get relative path you can use
|
228 | // const relativePath = path.relative(context, resourcePath);
|
229 |
|
230 | if (/my-custom-image\.png/.test(resourcePath)) {
|
231 | return `other_public_path/${url}`;
|
232 | }
|
233 |
|
234 | if (/images/.test(context)) {
|
235 | return `image_output_path/${url}`;
|
236 | }
|
237 |
|
238 | return `public_path/${url}`;
|
239 | },
|
240 | },
|
241 | },
|
242 | ],
|
243 | },
|
244 | };
|
245 | ```
|
246 |
|
247 | ### `postTransformPublicPath`
|
248 |
|
249 | Type: `Function`
|
250 | Default: `undefined`
|
251 |
|
252 | Specifies a custom function to post-process the generated public path. This can be used to prepend or append dynamic global variables that are only available at runtime, like `__webpack_public_path__`. This would not be possible with just `publicPath`, since it stringifies the values.
|
253 |
|
254 | **webpack.config.js**
|
255 |
|
256 | ```js
|
257 | module.exports = {
|
258 | module: {
|
259 | rules: [
|
260 | {
|
261 | test: /\.(png|jpg|gif)$/i,
|
262 | loader: 'file-loader',
|
263 | options: {
|
264 | publicPath: '/some/path/',
|
265 | postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`,
|
266 | },
|
267 | },
|
268 | ],
|
269 | },
|
270 | };
|
271 | ```
|
272 |
|
273 | ### `context`
|
274 |
|
275 | Type: `String`
|
276 | Default: [`context`](https://webpack.js.org/configuration/entry-context/#context)
|
277 |
|
278 | Specifies a custom file context.
|
279 |
|
280 | ```js
|
281 | module.exports = {
|
282 | module: {
|
283 | rules: [
|
284 | {
|
285 | test: /\.(png|jpe?g|gif)$/i,
|
286 | use: [
|
287 | {
|
288 | loader: 'file-loader',
|
289 | options: {
|
290 | context: 'project',
|
291 | },
|
292 | },
|
293 | ],
|
294 | },
|
295 | ],
|
296 | },
|
297 | };
|
298 | ```
|
299 |
|
300 | ### `emitFile`
|
301 |
|
302 | Type: `Boolean`
|
303 | Default: `true`
|
304 |
|
305 | If true, emits a file (writes a file to the filesystem). If false, the loader
|
306 | will return a public URI but **will not** emit the file. It is often useful to
|
307 | disable this option for server-side packages.
|
308 |
|
309 | **file.js**
|
310 |
|
311 | ```js
|
312 | // bundle file
|
313 | import img from './file.png';
|
314 | ```
|
315 |
|
316 | **webpack.config.js**
|
317 |
|
318 | ```js
|
319 | module.exports = {
|
320 | module: {
|
321 | rules: [
|
322 | {
|
323 | test: /\.css$/i,
|
324 | use: [
|
325 | {
|
326 | loader: 'file-loader',
|
327 | options: {
|
328 | emitFile: false,
|
329 | },
|
330 | },
|
331 | ],
|
332 | },
|
333 | ],
|
334 | },
|
335 | };
|
336 | ```
|
337 |
|
338 | ### `regExp`
|
339 |
|
340 | Type: `RegExp`
|
341 | Default: `undefined`
|
342 |
|
343 | Specifies a Regular Expression to one or many parts of the target file path.
|
344 | The capture groups can be reused in the `name` property using `[N]`
|
345 | [placeholder](https://github.com/webpack-contrib/file-loader#placeholders).
|
346 |
|
347 | **file.js**
|
348 |
|
349 | ```js
|
350 | import img from './customer01/file.png';
|
351 | ```
|
352 |
|
353 | **webpack.config.js**
|
354 |
|
355 | ```js
|
356 | module.exports = {
|
357 | module: {
|
358 | rules: [
|
359 | {
|
360 | test: /\.(png|jpe?g|gif)$/i,
|
361 | use: [
|
362 | {
|
363 | loader: 'file-loader',
|
364 | options: {
|
365 | regExp: /\/([a-z0-9]+)\/[a-z0-9]+\.png$/i,
|
366 | name: '[1]-[name].[ext]',
|
367 | },
|
368 | },
|
369 | ],
|
370 | },
|
371 | ],
|
372 | },
|
373 | };
|
374 | ```
|
375 |
|
376 | > ℹ️ 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...
|
377 |
|
378 | ### `esModule`
|
379 |
|
380 | Type: `Boolean`
|
381 | Default: `false`
|
382 |
|
383 | By default, `file-loader` generates JS modules that use the ES modules syntax.
|
384 | There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
|
385 |
|
386 | You can enable a CommonJS module using:
|
387 |
|
388 | **webpack.config.js**
|
389 |
|
390 | ```js
|
391 | module.exports = {
|
392 | module: {
|
393 | rules: [
|
394 | {
|
395 | test: /\.css$/,
|
396 | use: [
|
397 | {
|
398 | loader: 'file-loader',
|
399 | options: {
|
400 | esModule: false,
|
401 | },
|
402 | },
|
403 | ],
|
404 | },
|
405 | ],
|
406 | },
|
407 | };
|
408 | ```
|
409 |
|
410 | ## Placeholders
|
411 |
|
412 | Full information about placeholders you can find [here](https://github.com/webpack/loader-utils#interpolatename).
|
413 |
|
414 | ### `[ext]`
|
415 |
|
416 | Type: `String`
|
417 | Default: `file.extname`
|
418 |
|
419 | The file extension of the target file/resource.
|
420 |
|
421 | ### `[name]`
|
422 |
|
423 | Type: `String`
|
424 | Default: `file.basename`
|
425 |
|
426 | The basename of the file/resource.
|
427 |
|
428 | ### `[path]`
|
429 |
|
430 | Type: `String`
|
431 | Default: `file.directory`
|
432 |
|
433 | The path of the resource relative to the webpack/config `context`.
|
434 |
|
435 | ### `[folder]`
|
436 |
|
437 | Type: `String`
|
438 | Default: `file.folder`
|
439 |
|
440 | The folder of the resource is in.
|
441 |
|
442 | ### `[emoji]`
|
443 |
|
444 | Type: `String`
|
445 | Default: `undefined`
|
446 |
|
447 | A random emoji representation of `content`.
|
448 |
|
449 | ### `[emoji:<length>]`
|
450 |
|
451 | Type: `String`
|
452 | Default: `undefined`
|
453 |
|
454 | Same as above, but with a customizable number of emojis
|
455 |
|
456 | ### `[hash]`
|
457 |
|
458 | Type: `String`
|
459 | Default: `md5`
|
460 |
|
461 | Specifies the hash method to use for hashing the file content.
|
462 |
|
463 | ### `[contenthash]`
|
464 |
|
465 | Type: `String`
|
466 | Default: `md5`
|
467 |
|
468 | Specifies the hash method to use for hashing the file content.
|
469 |
|
470 | ### `[<hashType>:hash:<digestType>:<length>]`
|
471 |
|
472 | Type: `String`
|
473 |
|
474 | The hash of options.content (Buffer) (by default it's the hex digest of the hash).
|
475 |
|
476 | #### `digestType`
|
477 |
|
478 | Type: `String`
|
479 | Default: `'hex'`
|
480 |
|
481 | The [digest](https://en.wikipedia.org/wiki/Cryptographic_hash_function) that the
|
482 | hash function should use. Valid values include: base26, base32, base36,
|
483 | base49, base52, base58, base62, base64, and hex.
|
484 |
|
485 | #### `hashType`
|
486 |
|
487 | Type: `String`
|
488 | Default: `'md5'`
|
489 |
|
490 | The type of hash that the has function should use. Valid values include: `md5`,
|
491 | `sha1`, `sha256`, and `sha512`.
|
492 |
|
493 | #### `length`
|
494 |
|
495 | Type: `Number`
|
496 | Default: `undefined`
|
497 |
|
498 | Users may also specify a length for the computed hash.
|
499 |
|
500 | ### `[N]`
|
501 |
|
502 | Type: `String`
|
503 | Default: `undefined`
|
504 |
|
505 | The n-th match obtained from matching the current file name against the `regExp`.
|
506 |
|
507 | ## Examples
|
508 |
|
509 | ### Names
|
510 |
|
511 | The following examples show how one might use `file-loader` and what the result would be.
|
512 |
|
513 | **file.js**
|
514 |
|
515 | ```js
|
516 | import png from './image.png';
|
517 | ```
|
518 |
|
519 | **webpack.config.js**
|
520 |
|
521 | ```js
|
522 | module.exports = {
|
523 | module: {
|
524 | rules: [
|
525 | {
|
526 | test: /\.(png|jpe?g|gif)$/i,
|
527 | use: [
|
528 | {
|
529 | loader: 'file-loader',
|
530 | options: {
|
531 | name: 'dirname/[contenthash].[ext]',
|
532 | },
|
533 | },
|
534 | ],
|
535 | },
|
536 | ],
|
537 | },
|
538 | };
|
539 | ```
|
540 |
|
541 | Result:
|
542 |
|
543 | ```bash
|
544 | # result
|
545 | dirname/0dcbbaa701328ae351f.png
|
546 | ```
|
547 |
|
548 | ---
|
549 |
|
550 | **file.js**
|
551 |
|
552 | ```js
|
553 | import png from './image.png';
|
554 | ```
|
555 |
|
556 | **webpack.config.js**
|
557 |
|
558 | ```js
|
559 | module.exports = {
|
560 | module: {
|
561 | rules: [
|
562 | {
|
563 | test: /\.(png|jpe?g|gif)$/i,
|
564 | use: [
|
565 | {
|
566 | loader: 'file-loader',
|
567 | options: {
|
568 | name: '[sha512:hash:base64:7].[ext]',
|
569 | },
|
570 | },
|
571 | ],
|
572 | },
|
573 | ],
|
574 | },
|
575 | };
|
576 | ```
|
577 |
|
578 | Result:
|
579 |
|
580 | ```bash
|
581 | # result
|
582 | gdyb21L.png
|
583 | ```
|
584 |
|
585 | ---
|
586 |
|
587 | **file.js**
|
588 |
|
589 | ```js
|
590 | import png from './path/to/file.png';
|
591 | ```
|
592 |
|
593 | **webpack.config.js**
|
594 |
|
595 | ```js
|
596 | module.exports = {
|
597 | module: {
|
598 | rules: [
|
599 | {
|
600 | test: /\.(png|jpe?g|gif)$/i,
|
601 | use: [
|
602 | {
|
603 | loader: 'file-loader',
|
604 | options: {
|
605 | name: '[path][name].[ext]?[contenthash]',
|
606 | },
|
607 | },
|
608 | ],
|
609 | },
|
610 | ],
|
611 | },
|
612 | };
|
613 | ```
|
614 |
|
615 | Result:
|
616 |
|
617 | ```bash
|
618 | # result
|
619 | path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
|
620 | ```
|
621 |
|
622 | ### Dynamic public path depending on environment variable at run time
|
623 |
|
624 | An application might want to configure different CDN hosts depending on an environment variable that is only available when running the application. This can be an advantage, as only one build of the application is necessary, which behaves differntly depending on environment variables of the deployment environment. Since file-loader is applied when compiling the application, and not when running it, the environment variable cannot be used in the file-loader configuration. A way around this is setting the `__webpack_public_path__` to the desired CDN host depending on the environment variable at the entrypoint of the application. The option `postTransformPublicPath` can be used to configure a custom path depending on a variable like `__webpack_public_path__`.
|
625 |
|
626 | **main.js**
|
627 |
|
628 | ```js
|
629 | const namespace = process.env.NAMESPACE;
|
630 | const assetPrefixForNamespace = (namespace) => {
|
631 | switch (namespace) {
|
632 | case 'prod':
|
633 | return 'https://cache.myserver.net/web';
|
634 | case 'uat':
|
635 | return 'https://cache-uat.myserver.net/web';
|
636 | case 'st':
|
637 | return 'https://cache-st.myserver.net/web';
|
638 | case 'dev':
|
639 | return 'https://cache-dev.myserver.net/web';
|
640 | default:
|
641 | return '';
|
642 | }
|
643 | };
|
644 | __webpack_public_path__ = `${assetPrefixForNamespace(namespace)}/`;
|
645 | ```
|
646 |
|
647 | **file.js**
|
648 |
|
649 | ```js
|
650 | import png from './image.png';
|
651 | ```
|
652 |
|
653 | **webpack.config.js**
|
654 |
|
655 | ```js
|
656 | module.exports = {
|
657 | module: {
|
658 | rules: [
|
659 | {
|
660 | test: /\.(png|jpg|gif)$/i,
|
661 | loader: 'file-loader',
|
662 | options: {
|
663 | name: '[name].[contenthash].[ext]',
|
664 | outputPath: 'static/assets/',
|
665 | publicPath: 'static/assets/',
|
666 | postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`,
|
667 | },
|
668 | },
|
669 | ],
|
670 | },
|
671 | };
|
672 | ```
|
673 |
|
674 | Result when run with `NAMESPACE=prod` env variable:
|
675 |
|
676 | ```bash
|
677 | # result
|
678 | https://cache.myserver.net/web/static/assets/image.somehash.png
|
679 | ```
|
680 |
|
681 | Result when run with `NAMESPACE=dev` env variable:
|
682 |
|
683 | ```bash
|
684 | # result
|
685 | https://cache-dev.myserver.net/web/static/assets/image.somehash.png
|
686 | ```
|
687 |
|
688 | ## Contributing
|
689 |
|
690 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
691 |
|
692 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
693 |
|
694 | ## License
|
695 |
|
696 | [MIT](./LICENSE)
|
697 |
|
698 | [npm]: https://img.shields.io/npm/v/file-loader.svg
|
699 | [npm-url]: https://npmjs.com/package/file-loader
|
700 | [node]: https://img.shields.io/node/v/file-loader.svg
|
701 | [node-url]: https://nodejs.org
|
702 | [deps]: https://david-dm.org/webpack-contrib/file-loader.svg
|
703 | [deps-url]: https://david-dm.org/webpack-contrib/file-loader
|
704 | [tests]: https://dev.azure.com/webpack-contrib/file-loader/_apis/build/status/webpack-contrib.file-loader?branchName=master
|
705 | [tests-url]: https://dev.azure.com/webpack-contrib/file-loader/_build/latest?definitionId=2&branchName=master
|
706 | [cover]: https://codecov.io/gh/webpack-contrib/file-loader/branch/master/graph/badge.svg
|
707 | [cover-url]: https://codecov.io/gh/webpack-contrib/file-loader
|
708 | [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
709 | [chat-url]: https://gitter.im/webpack/webpack
|
710 | [size]: https://packagephobia.now.sh/badge?p=file-loader
|
711 | [size-url]: https://packagephobia.now.sh/result?p=file-loader
|