UNPKG

9.54 kBMarkdownView Raw
1[![npm][npm]][npm-url]
2[![deps][deps]][deps-url]
3[![test][test]][test-url]
4[![coverage][cover]][cover-url]
5[![chat][chat]][chat-url]
6
7<div align="center">
8 <img width="200" height="200"
9 src="https://worldvectorlogo.com/logos/html5.svg">
10 <a href="https://github.com/webpack/webpack">
11 <img width="200" height="200" vspace="" hspace="25"
12 src="https://worldvectorlogo.com/logos/webpack.svg">
13 </a>
14 <h1>HTML Loader</h1>
15 <p>Exports HTML as string. HTML is minimized when the compiler demands.<p>
16</div>
17
18<h2 align="center">Install</h2>
19
20```bash
21npm i -D html-loader
22```
23
24<h2 align="center">Usage</h2>
25
26By default every local `<img src="image.png">` is required (`require('./image.png')`). You may need to specify loaders for images in your configuration (recommended `file-loader` or `url-loader`).
27
28You can specify which tag-attribute combination should be processed by this loader via the query parameter `attrs`. Pass an array or a space-separated list of `<tag>:<attribute>` combinations. (Default: `attrs=img:src`)
29
30If you use `<custom-elements>`, and lots of them make use of a `custom-src` attribute, you don't have to specify each combination `<tag>:<attribute>`: just specify an empty tag like `attrs=:custom-src` and it will match every element.
31
32```js
33{
34 test: /\.(html)$/,
35 use: {
36 loader: 'html-loader',
37 options: {
38 attrs: [':data-src']
39 }
40 }
41}
42```
43
44To completely disable tag-attribute processing (for instance, if you're handling image loading on the client side) you can pass in `attrs=false`.
45
46<h2 align="center">Examples</h2>
47
48With this configuration:
49
50```js
51{
52 module: {
53 rules: [
54 { test: /\.jpg$/, use: [ "file-loader" ] },
55 { test: /\.png$/, use: [ "url-loader?mimetype=image/png" ] }
56 ]
57 },
58 output: {
59 publicPath: "http://cdn.example.com/[hash]/"
60 }
61}
62```
63
64``` html
65<!-- file.html -->
66<img src="image.png" data-src="image2x.png" >
67```
68
69```js
70require("html-loader!./file.html");
71
72// => '<img src="http://cdn.example.com/49eba9f/a992ca.png"
73// data-src="image2x.png">'
74```
75
76```js
77require("html-loader?attrs=img:data-src!./file.html");
78
79// => '<img src="image.png" data-src="data:image/png;base64,..." >'
80```
81
82```js
83require("html-loader?attrs=img:src img:data-src!./file.html");
84require("html-loader?attrs[]=img:src&attrs[]=img:data-src!./file.html");
85
86// => '<img src="http://cdn.example.com/49eba9f/a992ca.png"
87// data-src="data:image/png;base64,..." >'
88```
89
90```js
91require("html-loader?-attrs!./file.html");
92
93// => '<img src="image.jpg" data-src="image2x.png" >'
94```
95
96minimized by running `webpack --optimize-minimize`
97
98```html
99'<img src=http://cdn.example.com/49eba9f/a9f92ca.jpg
100 data-src=data:image/png;base64,...>'
101```
102
103or specify the `minimize` property in the rule's options in your `webpack.conf.js`
104
105```js
106module: {
107 rules: [{
108 test: /\.html$/,
109 use: [ {
110 loader: 'html-loader',
111 options: {
112 minimize: true
113 }
114 }],
115 }]
116}
117```
118
119See [html-minifier](https://github.com/kangax/html-minifier#options-quick-reference)'s documentation for more information on the available options.
120
121The enabled rules for minimizing by default are the following ones:
122 - removeComments
123 - removeCommentsFromCDATA
124 - removeCDATASectionsFromCDATA
125 - collapseWhitespace
126 - conservativeCollapse
127 - removeAttributeQuotes
128 - useShortDoctype
129 - keepClosingSlash
130 - minifyJS
131 - minifyCSS
132 - removeScriptTypeAttributes
133 - removeStyleTypeAttributes
134
135 The rules can be disabled using the following options in your `webpack.conf.js`
136
137```js
138module: {
139 rules: [{
140 test: /\.html$/,
141 use: [ {
142 loader: 'html-loader',
143 options: {
144 minimize: true,
145 removeComments: false,
146 collapseWhitespace: false
147 }
148 }],
149 }]
150}
151```
152
153### 'Root-relative' URLs
154
155For urls that start with a `/`, the default behavior is to not translate them.
156If a `root` query parameter is set, however, it will be prepended to the url
157and then translated.
158
159With the same configuration as above:
160
161``` html
162<!-- file.html -->
163<img src="/image.jpg">
164```
165
166```js
167require("html-loader!./file.html");
168
169// => '<img src="/image.jpg">'
170```
171
172```js
173require("html-loader?root=.!./file.html");
174
175// => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg">'
176```
177
178### Interpolation
179
180You can use `interpolate` flag to enable interpolation syntax for ES6 template strings, like so:
181
182```js
183require("html-loader?interpolate!./file.html");
184```
185
186```html
187<img src="${require(`./images/gallery.png`)}">
188
189<div>${require('./components/gallery.html')}</div>
190```
191And if you only want to use `require` in template and any other `${}` are not to be translated, you can set `interpolate` flag to `require`, like so:
192
193```js
194require("html-loader?interpolate=require!./file.ftl");
195```
196
197```html
198
199<#list list as list>
200 <a href="${list.href!}" />${list.name}</a>
201</#list>
202
203<img src="${require(`./images/gallery.png`)}">
204
205<div>${require('./components/gallery.html')}</div>
206```
207
208### Export formats
209
210There are different export formats available:
211
212+ ```module.exports``` (default, cjs format). "Hello world" becomes ```module.exports = "Hello world";```
213+ ```exports.default``` (when ```exportAsDefault``` param is set, es6to5 format). "Hello world" becomes ```exports.default = "Hello world";```
214+ ```export default``` (when ```exportAsEs6Default``` param is set, es6 format). "Hello world" becomes ```export default "Hello world";```
215
216### Advanced options
217
218If you need to pass [more advanced options](https://github.com/webpack/html-loader/pull/46), especially those which cannot be stringified, you can also define an `htmlLoader`-property on your `webpack.config.js`:
219
220```js
221var path = require('path')
222
223module.exports = {
224 ...
225 module: {
226 rules: [
227 {
228 test: /\.html$/,
229 use: [ "html-loader" ]
230 }
231 ]
232 },
233 htmlLoader: {
234 ignoreCustomFragments: [/\{\{.*?}}/],
235 root: path.resolve(__dirname, 'assets'),
236 attrs: ['img:src', 'link:href']
237 }
238};
239```
240
241If you need to define two different loader configs, you can also change the config's property name via `html-loader?config=otherHtmlLoaderConfig`:
242
243```js
244module.exports = {
245 ...
246 module: {
247 rules: [
248 {
249 test: /\.html$/,
250 use: [ "html-loader?config=otherHtmlLoaderConfig" ]
251 }
252 ]
253 },
254 otherHtmlLoaderConfig: {
255 ...
256 }
257};
258```
259
260### Export into HTML files
261
262A very common scenario is exporting the HTML into their own _.html_ file, to
263serve them directly instead of injecting with javascript. This can be achieved
264with a combination of 3 loaders:
265
266- [file-loader](https://github.com/webpack/file-loader)
267- [extract-loader](https://github.com/peerigon/extract-loader)
268- html-loader
269
270The html-loader will parse the URLs, require the images and everything you
271expect. The extract loader will parse the javascript back into a proper html
272file, ensuring images are required and point to proper path, and the file loader
273will write the _.html_ file for you. Example:
274
275```js
276{
277 test: /\.html$/,
278 use: [ 'file-loader?name=[path][name].[ext]!extract-loader!html-loader' ]
279}
280```
281
282<h2 align="center">Maintainers</h2>
283
284<table>
285 <tbody>
286 <tr>
287 <td align="center">
288 <img width="150" height="150"
289 src="https://avatars.githubusercontent.com/u/18315?v=3">
290 </br>
291 <a href="https://github.com/hemanth">Hemanth</a>
292 </td>
293 <td align="center">
294 <img width="150" height="150"
295 src="https://avatars.githubusercontent.com/u/8420490?v=3">
296 </br>
297 <a href="https://github.com/d3viant0ne">Joshua Wiens</a>
298 </td>
299 <td align="center">
300 <img width="150" height="150" src="https://avatars.githubusercontent.com/u/5419992?v=3">
301 </br>
302 <a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
303 </td>
304 <td align="center">
305 <img width="150" height="150"
306 src="https://avatars.githubusercontent.com/u/6542274?v=3">
307 </br>
308 <a href="https://github.com/imvetri">Imvetri</a>
309 </td>
310 </tr>
311 <tr>
312 <td align="center">
313 <img width="150" height="150"
314 src="https://avatars.githubusercontent.com/u/1520965?v=3">
315 </br>
316 <a href="https://github.com/andreicek">Andrei Crnković</a>
317 </td>
318 <td align="center">
319 <img width="150" height="150"
320 src="https://avatars.githubusercontent.com/u/3367801?v=3">
321 </br>
322 <a href="https://github.com/abouthiroppy">Yuta Hiroto</a>
323 </td>
324 <td align="center">
325 <img width="150" height="150" src="https://avatars.githubusercontent.com/u/80044?v=3">
326 </br>
327 <a href="https://github.com/petrunov">Vesselin Petrunov</a>
328 </td>
329 <td align="center">
330 <img width="150" height="150"
331 src="https://avatars.githubusercontent.com/u/973543?v=3">
332 </br>
333 <a href="https://github.com/gajus">Gajus Kuizinas</a>
334 </td>
335 </tr>
336 </tbody>
337</table>
338
339
340[npm]: https://img.shields.io/npm/v/html-loader.svg
341[npm-url]: https://npmjs.com/package/html-loader
342
343[deps]: https://david-dm.org/webpack/html-loader.svg
344[deps-url]: https://david-dm.org/webpack/html-loader
345
346[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
347[chat-url]: https://gitter.im/webpack/webpack
348
349[test]: http://img.shields.io/travis/webpack/html-loader.svg
350[test-url]: https://travis-ci.org/webpack/html-loader
351
352[cover]: https://codecov.io/gh/webpack/html-loader/branch/master/graph/badge.svg
353[cover-url]: https://codecov.io/gh/webpack/html-loader