1 | # html loader for webpack
|
2 |
|
3 | Exports HTML as string. HTML is minimized when the compiler demands.
|
4 |
|
5 | By 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`).
|
6 |
|
7 | You 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`)
|
8 |
|
9 | To completely disable tag-attribute processing (for instance, if you're handling image loading on the client side) you can pass in `attrs=false`.
|
10 |
|
11 | ## Usage
|
12 |
|
13 | [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
|
14 |
|
15 | ## Examples
|
16 |
|
17 | With this configuration:
|
18 |
|
19 | ``` javascript
|
20 | {
|
21 | module: { loaders: [
|
22 | { test: /\.jpg$/, loader: "file-loader" },
|
23 | { test: /\.png$/, loader: "url-loader?mimetype=image/png" }
|
24 | ]},
|
25 | output: {
|
26 | publicPath: "http://cdn.example.com/[hash]/"
|
27 | }
|
28 | }
|
29 | ```
|
30 |
|
31 | ``` html
|
32 | <!-- fileA.html -->
|
33 | <img src="image.jpg" data-src="image2x.png" >
|
34 | ```
|
35 |
|
36 | ``` javascript
|
37 | require("html!./fileA.html");
|
38 | // => '<img src="http://cdn.example.com/49e...ba9f/a9f...92ca.jpg" data-src="image2x.png" >'
|
39 |
|
40 | require("html?attrs=img:data-src!./file.html");
|
41 | // => '<img src="image.png" data-src="data:image/png;base64,..." >'
|
42 |
|
43 | require("html?attrs=img:src img:data-src!./file.html");
|
44 | require("html?attrs[]=img:src&attrs[]=img:data-src!./file.html");
|
45 | // => '<img src="http://cdn.example.com/49e...ba9f/a9f...92ca.jpg" data-src="data:image/png;base64,..." >'
|
46 |
|
47 | require("html?-attrs!./file.html");
|
48 | // => '<img src="image.jpg" data-src="image2x.png" >'
|
49 |
|
50 | /// minimized by running `webpack --optimize-minimize`
|
51 | // => '<img src=http://cdn.example.com/49e...ba9f/a9f...92ca.jpg data-src=data:image/png;base64,...>'
|
52 |
|
53 | ```
|
54 |
|
55 | ## 'Root-relative' urls
|
56 |
|
57 | For urls that start with a `/`, the default behavior is to not translate them.
|
58 | If a `root` query parameter is set, however, it will be prepended to the url
|
59 | and then translated.
|
60 |
|
61 | With the same configuration above:
|
62 | ``` html
|
63 | <!-- fileB.html -->
|
64 | <img src="/image.jpg">
|
65 | ```
|
66 |
|
67 | ``` javascript
|
68 |
|
69 | require("html!./fileB.html");
|
70 | // => '<img src="/image.jpg">'
|
71 |
|
72 | require("html?root=.!./fileB.html");
|
73 | // => '<img src="http://cdn.example.com/49e...ba9f/a9f...92ca.jpg">'
|
74 |
|
75 | ```
|
76 |
|
77 | ## Interpolation
|
78 |
|
79 | You can use `interpolate` flag to enable interpolation syntax for ES6 template strings, like so:
|
80 |
|
81 | ```
|
82 | require("html?interpolate!./file.html");
|
83 | ```
|
84 |
|
85 | ```
|
86 | <img src="${require(`./images/gallery.png`)}" />
|
87 | <div>${require('./partials/gallery.html')}</div>
|
88 | ```
|
89 |
|
90 | ## Advanced options
|
91 |
|
92 | If 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`:
|
93 |
|
94 | ``` javascript
|
95 | module.exports = {
|
96 | ...
|
97 | module: {
|
98 | loaders: [
|
99 | {
|
100 | test: /\.html$/,
|
101 | loader: "html"
|
102 | }
|
103 | ]
|
104 | }
|
105 | htmlLoader: {
|
106 | ignoreCustomFragments: [/\{\{.*?}}/]
|
107 | }
|
108 | };
|
109 | ```
|
110 |
|
111 | If you need to define two different loader configs, you can also change the config's property name via `html?config=otherHtmlLoaderConfig`:
|
112 |
|
113 | ```javascript
|
114 | module.exports = {
|
115 | ...
|
116 | module: {
|
117 | loaders: [
|
118 | {
|
119 | test: /\.html$/,
|
120 | loader: "html?config=otherHtmlLoaderConfig"
|
121 | }
|
122 | ]
|
123 | }
|
124 | otherHtmlLoaderConfig: {
|
125 | ...
|
126 | }
|
127 | };
|
128 | ```
|
129 |
|
130 | ## License
|
131 |
|
132 | MIT (http://www.opensource.org/licenses/mit-license.php)
|