fd7d17c449d7d7274f42100148091611d43a74f936001c3680a496d64c5e0f194e631a53f9652e7ea25d4910cdd50b4be75c66aeb1c1c8c6a372d42aef8ef3 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # file loader for webpack
  2. ## Usage
  3. [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
  4. ``` javascript
  5. var url = require("file!./file.png");
  6. // => emits file.png as file in the output directory and returns the public url
  7. // => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png"
  8. ```
  9. By default the filename of the resulting file is the MD5 hash of the file's contents
  10. with the original extension of the required resource.
  11. By default a file is emitted, however this can be disabled if required (e.g. for server
  12. side packages).
  13. ``` javascript
  14. var url = require("file?emitFile=false!./file.png");
  15. // => returns the public url but does NOT emit a file
  16. // => returns i. e. "/public-path/0dcbbaa701328a3c262cfd45869e351f.png"
  17. ```
  18. ## Filename templates
  19. You can configure a custom filename template for your file using the query
  20. parameter `name`. For instance, to copy a file from your `context` directory
  21. into the output directory retaining the full directory structure, you might
  22. use `?name=[path][name].[ext]`.
  23. ### Filename template placeholders
  24. * `[ext]` the extension of the resource
  25. * `[name]` the basename of the resource
  26. * `[path]` the path of the resource relative to the `context` query parameter or option.
  27. * `[hash]` the hash of the content, `hex`-encoded `md5` by default
  28. * `[<hashType>:hash:<digestType>:<length>]` optionally you can configure
  29. * other `hashType`s, i. e. `sha1`, `md5`, `sha256`, `sha512`
  30. * other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
  31. * and `length` the length in chars
  32. * `[N]` the N-th match obtained from matching the current file name against the query param `regExp`
  33. ## Examples
  34. ``` javascript
  35. require("file?name=js/[hash].script.[ext]!./javascript.js");
  36. // => js/0dcbbaa701328a3c262cfd45869e351f.script.js
  37. require("file?name=html-[hash:6].html!./page.html");
  38. // => html-109fa8.html
  39. require("file?name=[hash]!./flash.txt");
  40. // => c31e9820c001c9c4a86bce33ce43b679
  41. require("file?name=[sha512:hash:base64:7].[ext]!./image.png");
  42. // => gdyb21L.png
  43. // use sha512 hash instead of md5 and with only 7 chars of base64
  44. require("file?name=img-[sha512:hash:base64:7].[ext]!./image.jpg");
  45. // => img-VqzT5ZC.jpg
  46. // use custom name, sha512 hash instead of md5 and with only 7 chars of base64
  47. require("file?name=picture.png!./myself.png");
  48. // => picture.png
  49. require("file?name=[path][name].[ext]?[hash]!./dir/file.png")
  50. // => dir/file.png?e43b20c069c4a01867c31e98cbce33c9
  51. ```
  52. ## Installation
  53. ```npm install file-loader --save-dev```
  54. ## License
  55. MIT (http://www.opensource.org/licenses/mit-license.php)