1 | [![npm][npm]][npm-url]
|
2 | [![node][node]][node-url]
|
3 | [![deps][deps]][deps-url]
|
4 | [![tests][tests]][tests-url]
|
5 | [![coverage][cover]][cover-url]
|
6 | [![chat][chat]][chat-url]
|
7 |
|
8 | <div align="center">
|
9 | <a href="https://github.com/webpack/webpack">
|
10 | <img width="200" height="200"
|
11 | src="https://webpack.js.org/assets/icon-square-big.svg">
|
12 | </a>
|
13 | <h1>File Loader</h1>
|
14 | <p>Instructs webpack to emit the required object as file and to return its public URL</p>
|
15 | </div>
|
16 |
|
17 | <h2 align="center">Install</h2>
|
18 |
|
19 | ```bash
|
20 | npm install --save-dev file-loader
|
21 | ```
|
22 |
|
23 | <h2 align="center"><a href="https://webpack.js.org/concepts/loaders">Usage</a></h2>
|
24 |
|
25 | By default the filename of the resulting file is the MD5 hash of the file's contents with the original extension of the required resource.
|
26 |
|
27 | ```js
|
28 | import img from './file.png'
|
29 | ```
|
30 |
|
31 | **webpack.config.js**
|
32 | ```js
|
33 | module.exports = {
|
34 | module: {
|
35 | rules: [
|
36 | {
|
37 | test: /\.(png|jpg|gif)$/,
|
38 | use: [
|
39 | {
|
40 | loader: 'file-loader',
|
41 | options: {}
|
42 | }
|
43 | ]
|
44 | }
|
45 | ]
|
46 | }
|
47 | }
|
48 | ```
|
49 |
|
50 | Emits `file.png` as file in the output directory and returns the public URL
|
51 |
|
52 | ```
|
53 | "/public/path/0dcbbaa7013869e351f.png"
|
54 | ```
|
55 |
|
56 | <h2 align="center">Options</h2>
|
57 |
|
58 | |Name|Type|Default|Description|
|
59 | |:--:|:--:|:-----:|:----------|
|
60 | |**`name`**|`{String}`|`[hash].[ext]`|Configure a custom filename template for your file|
|
61 | |**`context`**|`{String}`|`this.options.context`|Configure a custom file context, defaults to `webpack.config.js` [context](https://webpack.js.org/configuration/entry-context/#context)|
|
62 | |**`publicPath`**|`{String\|Function}`|[`__webpack_public_path__ `](https://webpack.js.org/api/module-variables/#__webpack_public_path__-webpack-specific-)|Configure a custom `public` path for your files|
|
63 | |**`outputPath`**|`{String\|Function}`|`'undefined'`|Configure a custom `output` path for your files|
|
64 | |**`useRelativePath`**|`{Boolean}`|`false`|Should be `true` if you wish to generate a `context` relative URL for each file|
|
65 | |**`emitFile`**|`{Boolean}`|`true`|By default a file is emitted, however this can be disabled if required (e.g. for server side packages)|
|
66 |
|
67 | ### `name`
|
68 |
|
69 | You can configure a custom filename template for your file using the query parameter `name`. For instance, to copy a file from your `context` directory into the output directory retaining the full directory structure, you might use
|
70 |
|
71 | **webpack.config.js**
|
72 | ```js
|
73 | {
|
74 | loader: 'file-loader',
|
75 | options: {
|
76 | name: '[path][name].[ext]'
|
77 | }
|
78 | }
|
79 | ```
|
80 |
|
81 | #### `placeholders`
|
82 |
|
83 | |Name|Type|Default|Description|
|
84 | |:--:|:--:|:-----:|:----------|
|
85 | |**`[ext]`**|`{String}`|`file.extname`|The extension of the resource|
|
86 | |**`[name]`**|`{String}`|`file.basename`|The basename of the resource|
|
87 | |**`[path]`**|`{String}`|`file.dirname`|The path of the resource relative to the `context`|
|
88 | |**`[hash]`**|`{String}`|`md5`|The hash of the content, hashes below for more info|
|
89 | |**`[N]`**|`{Number}`|``|The `n-th` match obtained from matching the current file name against the query param `regExp`|
|
90 |
|
91 | #### `hashes`
|
92 |
|
93 | `[<hashType>:hash:<digestType>:<length>]` optionally you can configure
|
94 |
|
95 | |Name|Type|Default|Description|
|
96 | |:--:|:--:|:-----:|:----------|
|
97 | |**`hashType`**|`{String}`|`md5`|`sha1`, `md5`, `sha256`, `sha512`|
|
98 | |**`digestType`**|`{String}`|`base64`|`hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`|
|
99 | |**`length`**|`{Number}`|`8`|The length in chars|
|
100 |
|
101 | By default, the path and name you specify will output the file in that same directory and will also use that same URL path to access the file.
|
102 |
|
103 | ### `context`
|
104 |
|
105 | **webpack.config.js**
|
106 | ```js
|
107 | {
|
108 | loader: 'file-loader',
|
109 | options: {
|
110 | name: '[path][name].[ext]',
|
111 | context: ''
|
112 | }
|
113 | }
|
114 | ```
|
115 |
|
116 | You can specify custom `output` and `public` paths by using `outputPath`, `publicPath` and `useRelativePath`
|
117 |
|
118 | ### `publicPath`
|
119 |
|
120 | **webpack.config.js**
|
121 | ```js
|
122 | {
|
123 | loader: 'file-loader',
|
124 | options: {
|
125 | name: '[path][name].[ext]',
|
126 | publicPath: 'assets'
|
127 | }
|
128 | }
|
129 | ```
|
130 |
|
131 | ### `outputPath`
|
132 |
|
133 | **webpack.config.js**
|
134 | ```js
|
135 | {
|
136 | loader: 'file-loader',
|
137 | options: {
|
138 | name: '[path][name].[ext]',
|
139 | outputPath: 'images'
|
140 | }
|
141 | }
|
142 | ```
|
143 |
|
144 | ### `useRelativePath`
|
145 |
|
146 | `useRelativePath` should be `true` if you wish to generate a relative URL to the for each file context.
|
147 |
|
148 | ```js
|
149 | {
|
150 | loader: 'file-loader',
|
151 | options: {
|
152 | useRelativePath: process.env.NODE_ENV === "production"
|
153 | }
|
154 | }
|
155 | ```
|
156 |
|
157 | ### `emitFile`
|
158 |
|
159 | By default a file is emitted, however this can be disabled if required (e.g. for server side packages).
|
160 |
|
161 | ```js
|
162 | import img from './file.png'
|
163 | ```
|
164 |
|
165 | ```js
|
166 | {
|
167 | loader: 'file-loader',
|
168 | options: {
|
169 | emitFile: false
|
170 | }
|
171 | }
|
172 | ```
|
173 |
|
174 | > ⚠️ Returns the public URL but does **not** emit a file
|
175 |
|
176 | ```
|
177 | `${publicPath}/0dcbbaa701328e351f.png`
|
178 | ```
|
179 |
|
180 | <h2 align="center">Examples</h2>
|
181 |
|
182 |
|
183 | ```js
|
184 | import png from 'image.png'
|
185 | ```
|
186 |
|
187 | **webpack.config.js**
|
188 | ```js
|
189 | {
|
190 | loader: 'file-loader',
|
191 | options: {
|
192 | name: 'dirname/[hash].[ext]'
|
193 | }
|
194 | }
|
195 | ```
|
196 |
|
197 | ```
|
198 | dirname/0dcbbaa701328ae351f.png
|
199 | ```
|
200 |
|
201 | **webpack.config.js**
|
202 | ```js
|
203 | {
|
204 | loader: 'file-loader',
|
205 | options: {
|
206 | name: '[sha512:hash:base64:7].[ext]'
|
207 | }
|
208 | }
|
209 | ```
|
210 |
|
211 | ```
|
212 | gdyb21L.png
|
213 | ```
|
214 |
|
215 | ```js
|
216 | import png from 'path/to/file.png'
|
217 | ```
|
218 |
|
219 | **webpack.config.js**
|
220 | ```js
|
221 | {
|
222 | loader: 'file-loader',
|
223 | options: {
|
224 | name: '[path][name].[ext]?[hash]'
|
225 | }
|
226 | }
|
227 | ```
|
228 |
|
229 | ```
|
230 | path/to/file.png?e43b20c069c4a01867c31e98cbce33c9
|
231 | ```
|
232 |
|
233 | <h2 align="center">Maintainers</h2>
|
234 |
|
235 | <table>
|
236 | <tbody>
|
237 | <tr>
|
238 | <td align="center">
|
239 | <a href="https://github.com/bebraw">
|
240 | <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
|
241 | </br>
|
242 | Juho Vepsäläinen
|
243 | </a>
|
244 | </td>
|
245 | <td align="center">
|
246 | <a href="https://github.com/d3viant0ne">
|
247 | <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
|
248 | </br>
|
249 | Joshua Wiens
|
250 | </a>
|
251 | </td>
|
252 | <td align="center">
|
253 | <a href="https://github.com/michael-ciniawsky">
|
254 | <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
|
255 | </br>
|
256 | Michael Ciniawsky
|
257 | </a>
|
258 | </td>
|
259 | <td align="center">
|
260 | <a href="https://github.com/evilebottnawi">
|
261 | <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
|
262 | </br>
|
263 | Alexander Krasnoyarov
|
264 | </a>
|
265 | </td>
|
266 | </tr>
|
267 | <tbody>
|
268 | </table>
|
269 |
|
270 |
|
271 | [npm]: https://img.shields.io/npm/v/file-loader.svg
|
272 | [npm-url]: https://npmjs.com/package/file-loader
|
273 |
|
274 | [node]: https://img.shields.io/node/v/file-loader.svg
|
275 | [node-url]: https://nodejs.org
|
276 |
|
277 | [deps]: https://david-dm.org/webpack-contrib/file-loader.svg
|
278 | [deps-url]: https://david-dm.org/webpack-contrib/file-loader
|
279 |
|
280 | [tests]: http://img.shields.io/travis/webpack-contrib/file-loader.svg
|
281 | [tests-url]: https://travis-ci.org/webpack-contrib/file-loader
|
282 |
|
283 | [cover]: https://img.shields.io/codecov/c/github/webpack-contrib/file-loader.svg
|
284 | [cover-url]: https://codecov.io/gh/webpack-contrib/file-loader
|
285 |
|
286 | [chat]: https://badges.gitter.im/webpack/webpack.svg
|
287 | [chat-url]: https://gitter.im/webpack/webpack
|