You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
9.3 KiB

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