1 | <div align="center">
|
2 | <img width="200" height="200" src="https://www.w3.org/html/logo/downloads/HTML5_Logo.svg" alt="html-loader">
|
3 | <a href="https://github.com/webpack/webpack">
|
4 | <img width="200" height="200" vspace="" hspace="25" src="https://webpack.js.org/assets/icon-square-big.svg" alt="webpack">
|
5 | </a>
|
6 | </div>
|
7 |
|
8 | [![npm][npm]][npm-url]
|
9 | [![node][node]][node-url]
|
10 | [![tests][tests]][tests-url]
|
11 | [![coverage][cover]][cover-url]
|
12 | [![chat][chat]][chat-url]
|
13 | [![size][size]][size-url]
|
14 |
|
15 | # html-loader
|
16 |
|
17 | Exports HTML as string. HTML is minimized when the compiler demands.
|
18 |
|
19 | ## Getting Started
|
20 |
|
21 | To begin, you'll need to install `html-loader`:
|
22 |
|
23 | ```console
|
24 | npm install --save-dev html-loader
|
25 | ```
|
26 |
|
27 | or
|
28 |
|
29 | ```console
|
30 | yarn add -D html-loader
|
31 | ```
|
32 |
|
33 | or
|
34 |
|
35 | ```console
|
36 | pnpm add -D html-loader
|
37 | ```
|
38 |
|
39 | Then add the plugin to your `webpack` config. For example:
|
40 |
|
41 | **file.js**
|
42 |
|
43 | ```js
|
44 | import html from "./file.html";
|
45 | ```
|
46 |
|
47 | **webpack.config.js**
|
48 |
|
49 | ```js
|
50 | module.exports = {
|
51 | module: {
|
52 | rules: [
|
53 | {
|
54 | test: /\.html$/i,
|
55 | loader: "html-loader",
|
56 | },
|
57 | ],
|
58 | },
|
59 | };
|
60 | ```
|
61 |
|
62 | ## Options
|
63 |
|
64 | - **[`sources`](#sources)**
|
65 | - **[`preprocessor`](#preprocessor)**
|
66 | - **[`minimize`](#minimize)**
|
67 | - **[`esModule`](#esmodule)**
|
68 |
|
69 | ### `sources`
|
70 |
|
71 | Type:
|
72 |
|
73 | ```ts
|
74 | type sources =
|
75 | | boolean
|
76 | | {
|
77 | list?: Array<{
|
78 | tag?: string;
|
79 | attribute?: string;
|
80 | type?: string;
|
81 | filter?: (
|
82 | tag: string,
|
83 | attribute: string,
|
84 | attributes: string,
|
85 | resourcePath: string
|
86 | ) => boolean;
|
87 | }>;
|
88 | urlFilter?: (
|
89 | attribute: string,
|
90 | value: string,
|
91 | resourcePath: string
|
92 | ) => boolean;
|
93 | scriptingEnabled?: boolean;
|
94 | };
|
95 | ```
|
96 |
|
97 | Default: `true`
|
98 |
|
99 | By default every loadable attributes (for example - `<img src="image.png">`) is imported (`const img = require('./image.png')` or `import img from "./image.png""`).
|
100 | You may need to specify loaders for images in your configuration (recommended [`asset modules`](https://webpack.js.org/guides/asset-modules/)).
|
101 |
|
102 | Supported tags and attributes:
|
103 |
|
104 | - the `src` attribute of the `audio` tag
|
105 | - the `src` attribute of the `embed` tag
|
106 | - the `src` attribute of the `img` tag
|
107 | - the `srcset` attribute of the `img` tag
|
108 | - the `src` attribute of the `input` tag
|
109 | - the `data` attribute of the `object` tag
|
110 | - the `src` attribute of the `script` tag
|
111 | - the `href` attribute of the `script` tag
|
112 | - the `xlink:href` attribute of the `script` tag
|
113 | - the `src` attribute of the `source` tag
|
114 | - the `srcset` attribute of the `source` tag
|
115 | - the `src` attribute of the `track` tag
|
116 | - the `poster` attribute of the `video` tag
|
117 | - the `src` attribute of the `video` tag
|
118 | - the `xlink:href` attribute of the `image` tag
|
119 | - the `href` attribute of the `image` tag
|
120 | - the `xlink:href` attribute of the `use` tag
|
121 | - the `href` attribute of the `use` tag
|
122 | - the `href` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage`
|
123 | - the `imagesrcset` attribute of the `link` tag when the `rel` attribute contains `stylesheet`, `icon`, `shortcut icon`, `mask-icon`, `apple-touch-icon`, `apple-touch-icon-precomposed`, `apple-touch-startup-image`, `manifest`, `prefetch`, `preload`
|
124 | - the `content` attribute of the `meta` tag when the `name` attribute is `msapplication-tileimage`, `msapplication-square70x70logo`, `msapplication-square150x150logo`, `msapplication-wide310x150logo`, `msapplication-square310x310logo`, `msapplication-config`, `twitter:image` or when the `property` attribute is `og:image`, `og:image:url`, `og:image:secure_url`, `og:audio`, `og:audio:secure_url`, `og:video`, `og:video:secure_url`, `vk:image` or when the `itemprop` attribute is `image`, `logo`, `screenshot`, `thumbnailurl`, `contenturl`, `downloadurl`, `duringmedia`, `embedurl`, `installurl`, `layoutimage`
|
125 | - the `icon-uri` value component in `content` attribute of the `meta` tag when the `name` attribute is `msapplication-task`
|
126 |
|
127 | #### `boolean`
|
128 |
|
129 | The `true` value enables processing of all default elements and attributes, the `false` disable processing of all attributes.
|
130 |
|
131 | **webpack.config.js**
|
132 |
|
133 | ```js
|
134 | module.exports = {
|
135 | module: {
|
136 | rules: [
|
137 | {
|
138 | test: /\.html$/i,
|
139 | loader: "html-loader",
|
140 | options: {
|
141 | // Disables attributes processing
|
142 | sources: false,
|
143 | },
|
144 | },
|
145 | ],
|
146 | },
|
147 | };
|
148 | ```
|
149 |
|
150 | #### `object`
|
151 |
|
152 | Allows you to specify which tags and attributes to process, filter them, filter urls and process sources starts with `/`.
|
153 |
|
154 | For example:
|
155 |
|
156 | **webpack.config.js**
|
157 |
|
158 | ```js
|
159 | module.exports = {
|
160 | module: {
|
161 | rules: [
|
162 | {
|
163 | test: /\.html$/i,
|
164 | loader: "html-loader",
|
165 | options: {
|
166 | sources: {
|
167 | list: [
|
168 | // All default supported tags and attributes
|
169 | "...",
|
170 | {
|
171 | tag: "img",
|
172 | attribute: "data-src",
|
173 | type: "src",
|
174 | },
|
175 | {
|
176 | tag: "img",
|
177 | attribute: "data-srcset",
|
178 | type: "srcset",
|
179 | },
|
180 | ],
|
181 | urlFilter: (attribute, value, resourcePath) => {
|
182 | // The `attribute` argument contains a name of the HTML attribute.
|
183 | // The `value` argument contains a value of the HTML attribute.
|
184 | // The `resourcePath` argument contains a path to the loaded HTML file.
|
185 |
|
186 | if (/example\.pdf$/.test(value)) {
|
187 | return false;
|
188 | }
|
189 |
|
190 | return true;
|
191 | },
|
192 | },
|
193 | },
|
194 | },
|
195 | ],
|
196 | },
|
197 | };
|
198 | ```
|
199 |
|
200 | #### `list`
|
201 |
|
202 | Type:
|
203 |
|
204 | ```ts
|
205 | type list = Array<{
|
206 | tag?: string;
|
207 | attribute?: string;
|
208 | type?: string;
|
209 | filter?: (
|
210 | tag: string,
|
211 | attribute: string,
|
212 | attributes: string,
|
213 | resourcePath: string
|
214 | ) => boolean;
|
215 | }>;
|
216 | ```
|
217 |
|
218 | Default: [supported tags and attributes](#sources).
|
219 |
|
220 | Allows to setup which tags and attributes to process and how, and the ability to filter some of them.
|
221 |
|
222 | Using `...` syntax allows you to extend [default supported tags and attributes](#sources).
|
223 |
|
224 | For example:
|
225 |
|
226 | **webpack.config.js**
|
227 |
|
228 | ```js
|
229 | module.exports = {
|
230 | module: {
|
231 | rules: [
|
232 | {
|
233 | test: /\.html$/i,
|
234 | loader: "html-loader",
|
235 | options: {
|
236 | sources: {
|
237 | list: [
|
238 | // All default supported tags and attributes
|
239 | "...",
|
240 | {
|
241 | tag: "img",
|
242 | attribute: "data-src",
|
243 | type: "src",
|
244 | },
|
245 | {
|
246 | tag: "img",
|
247 | attribute: "data-srcset",
|
248 | type: "srcset",
|
249 | },
|
250 | {
|
251 | // Tag name
|
252 | tag: "link",
|
253 | // Attribute name
|
254 | attribute: "href",
|
255 | // Type of processing, can be `src` or `scrset`
|
256 | type: "src",
|
257 | // Allow to filter some attributes
|
258 | filter: (tag, attribute, attributes, resourcePath) => {
|
259 | // The `tag` argument contains a name of the HTML tag.
|
260 | // The `attribute` argument contains a name of the HTML attribute.
|
261 | // The `attributes` argument contains all attributes of the tag.
|
262 | // The `resourcePath` argument contains a path to the loaded HTML file.
|
263 |
|
264 | if (/my-html\.html$/.test(resourcePath)) {
|
265 | return false;
|
266 | }
|
267 |
|
268 | if (!/stylesheet/i.test(attributes.rel)) {
|
269 | return false;
|
270 | }
|
271 |
|
272 | if (
|
273 | attributes.type &&
|
274 | attributes.type.trim().toLowerCase() !== "text/css"
|
275 | ) {
|
276 | return false;
|
277 | }
|
278 |
|
279 | return true;
|
280 | },
|
281 | },
|
282 | ],
|
283 | },
|
284 | },
|
285 | },
|
286 | ],
|
287 | },
|
288 | };
|
289 | ```
|
290 |
|
291 | If the tag name is not specified it will process all the tags.
|
292 |
|
293 | > You can use your custom filter to specify html elements to be processed.
|
294 |
|
295 | For example:
|
296 |
|
297 | **webpack.config.js**
|
298 |
|
299 | ```js
|
300 | module.exports = {
|
301 | module: {
|
302 | rules: [
|
303 | {
|
304 | test: /\.html$/i,
|
305 | loader: "html-loader",
|
306 | options: {
|
307 | sources: {
|
308 | list: [
|
309 | {
|
310 | // Attribute name
|
311 | attribute: "src",
|
312 | // Type of processing, can be `src` or `scrset`
|
313 | type: "src",
|
314 | // Allow to filter some attributes (optional)
|
315 | filter: (tag, attribute, attributes, resourcePath) => {
|
316 | // The `tag` argument contains a name of the HTML tag.
|
317 | // The `attribute` argument contains a name of the HTML attribute.
|
318 | // The `attributes` argument contains all attributes of the tag.
|
319 | // The `resourcePath` argument contains a path to the loaded HTML file.
|
320 |
|
321 | // choose all HTML tags except img tag
|
322 | return tag.toLowerCase() !== "img";
|
323 | },
|
324 | },
|
325 | ],
|
326 | },
|
327 | },
|
328 | },
|
329 | ],
|
330 | },
|
331 | };
|
332 | ```
|
333 |
|
334 | Filter can also be used to extend the supported elements and attributes.
|
335 |
|
336 | For example, filter can help process meta tags that reference assets:
|
337 |
|
338 | ```js
|
339 | module.exports = {
|
340 | module: {
|
341 | rules: [
|
342 | {
|
343 | test: /\.html$/i,
|
344 | loader: "html-loader",
|
345 | options: {
|
346 | sources: {
|
347 | list: [
|
348 | {
|
349 | tag: "meta",
|
350 | attribute: "content",
|
351 | type: "src",
|
352 | filter: (tag, attribute, attributes, resourcePath) => {
|
353 | if (
|
354 | attributes.value === "og:image" ||
|
355 | attributes.name === "twitter:image"
|
356 | ) {
|
357 | return true;
|
358 | }
|
359 |
|
360 | return false;
|
361 | },
|
362 | },
|
363 | ],
|
364 | },
|
365 | },
|
366 | },
|
367 | ],
|
368 | },
|
369 | };
|
370 | ```
|
371 |
|
372 | > **Note**
|
373 | >
|
374 | > source with a `tag` option takes precedence over source without.
|
375 |
|
376 | Filter can be used to disable default sources.
|
377 |
|
378 | For example:
|
379 |
|
380 | ```js
|
381 | module.exports = {
|
382 | module: {
|
383 | rules: [
|
384 | {
|
385 | test: /\.html$/i,
|
386 | loader: "html-loader",
|
387 | options: {
|
388 | sources: {
|
389 | list: [
|
390 | "...",
|
391 | {
|
392 | tag: "img",
|
393 | attribute: "src",
|
394 | type: "src",
|
395 | filter: () => false,
|
396 | },
|
397 | ],
|
398 | },
|
399 | },
|
400 | },
|
401 | ],
|
402 | },
|
403 | };
|
404 | ```
|
405 |
|
406 | #### `urlFilter`
|
407 |
|
408 | Type:
|
409 |
|
410 | ```ts
|
411 | type urlFilter = (
|
412 | attribute: string,
|
413 | value: string,
|
414 | resourcePath: string
|
415 | ) => boolean;
|
416 | ```
|
417 |
|
418 | Default: `undefined`
|
419 |
|
420 | Allow to filter urls. All filtered urls will not be resolved (left in the code as they were written).
|
421 | All non requestable sources (for example `<img src="javascript:void(0)">`) do not handle by default.
|
422 |
|
423 | ```js
|
424 | module.exports = {
|
425 | module: {
|
426 | rules: [
|
427 | {
|
428 | test: /\.html$/i,
|
429 | loader: "html-loader",
|
430 | options: {
|
431 | sources: {
|
432 | urlFilter: (attribute, value, resourcePath) => {
|
433 | // The `attribute` argument contains a name of the HTML attribute.
|
434 | // The `value` argument contains a value of the HTML attribute.
|
435 | // The `resourcePath` argument contains a path to the loaded HTML file.
|
436 |
|
437 | if (/example\.pdf$/.test(value)) {
|
438 | return false;
|
439 | }
|
440 |
|
441 | return true;
|
442 | },
|
443 | },
|
444 | },
|
445 | },
|
446 | ],
|
447 | },
|
448 | };
|
449 | ```
|
450 |
|
451 | #### `scriptingEnabled`
|
452 |
|
453 | Type:
|
454 |
|
455 | ```ts
|
456 | type scriptingEnabled = boolean;
|
457 | ```
|
458 |
|
459 | Default: `true`
|
460 |
|
461 | By default, the parser in `html-loader` interprets content inside `<noscript>` tags as `#text`, so processing of content inside this tag will be ignored.
|
462 |
|
463 | In order to enable processing inside `<noscript>` for content recognition by the parser as `#AST`, set this parameter to: `false`
|
464 |
|
465 | Additional information: [scriptingEnabled](https://parse5.js.org/interfaces/parse5.ParserOptions.html#scriptingEnabled)
|
466 |
|
467 | **webpack.config.js**
|
468 |
|
469 | ```js
|
470 | module.exports = {
|
471 | module: {
|
472 | rules: [
|
473 | {
|
474 | test: /\.html$/i,
|
475 | loader: "html-loader",
|
476 | options: {
|
477 | sources: {
|
478 | // Enables processing inside the <noscript> tag
|
479 | scriptingEnabled: false,
|
480 | },
|
481 | },
|
482 | },
|
483 | ],
|
484 | },
|
485 | };
|
486 | ```
|
487 |
|
488 | ### `preprocessor`
|
489 |
|
490 | Type:
|
491 |
|
492 | ```ts
|
493 | type preprocessor = (
|
494 | content: string | Buffer,
|
495 | loaderContext: LoaderContext
|
496 | ) => HTMLElement;
|
497 | ```
|
498 |
|
499 | Default: `undefined`
|
500 |
|
501 | Allows pre-processing of content before handling.
|
502 |
|
503 | > **Warning**
|
504 | >
|
505 | > You should always return valid HTML
|
506 |
|
507 | **file.hbs**
|
508 |
|
509 | ```hbs
|
510 | <div>
|
511 | <p>{{firstname}} {{lastname}}</p>
|
512 | <img src="image.png" alt="alt" />
|
513 | <div>
|
514 | ```
|
515 |
|
516 | #### `function`
|
517 |
|
518 | You can set the `preprocessor` option as a `function` instance.
|
519 |
|
520 | **webpack.config.js**
|
521 |
|
522 | ```js
|
523 | const Handlebars = require("handlebars");
|
524 |
|
525 | module.exports = {
|
526 | module: {
|
527 | rules: [
|
528 | {
|
529 | test: /\.hbs$/i,
|
530 | loader: "html-loader",
|
531 | options: {
|
532 | preprocessor: (content, loaderContext) => {
|
533 | let result;
|
534 |
|
535 | try {
|
536 | result = Handlebars.compile(content)({
|
537 | firstname: "Value",
|
538 | lastname: "OtherValue",
|
539 | });
|
540 | } catch (error) {
|
541 | loaderContext.emitError(error);
|
542 |
|
543 | return content;
|
544 | }
|
545 |
|
546 | return result;
|
547 | },
|
548 | },
|
549 | },
|
550 | ],
|
551 | },
|
552 | };
|
553 | ```
|
554 |
|
555 | You can also set the `preprocessor` option as an asynchronous function instance.
|
556 |
|
557 | For example:
|
558 |
|
559 | **webpack.config.js**
|
560 |
|
561 | ```js
|
562 | const Handlebars = require("handlebars");
|
563 |
|
564 | module.exports = {
|
565 | module: {
|
566 | rules: [
|
567 | {
|
568 | test: /\.hbs$/i,
|
569 | loader: "html-loader",
|
570 | options: {
|
571 | preprocessor: async (content, loaderContext) => {
|
572 | let result;
|
573 |
|
574 | try {
|
575 | result = await Handlebars.compile(content)({
|
576 | firstname: "Value",
|
577 | lastname: "OtherValue",
|
578 | });
|
579 | } catch (error) {
|
580 | await loaderContext.emitError(error);
|
581 |
|
582 | return content;
|
583 | }
|
584 |
|
585 | return result;
|
586 | },
|
587 | },
|
588 | },
|
589 | ],
|
590 | },
|
591 | };
|
592 | ```
|
593 |
|
594 | ### `minimize`
|
595 |
|
596 | Type:
|
597 |
|
598 | ```ts
|
599 | type minimize =
|
600 | | boolean
|
601 | | {
|
602 | caseSensitive?: boolean;
|
603 | collapseWhitespace?: boolean;
|
604 | conservativeCollapse?: boolean;
|
605 | keepClosingSlash?: boolean;
|
606 | minifyCSS?: boolean;
|
607 | minifyJS?: boolean;
|
608 | removeComments?: boolean;
|
609 | removeRedundantAttributes?: boolean;
|
610 | removeScriptTypeAttributes?: boolean;
|
611 | removeStyleLinkTypeAttributes?: boolean;
|
612 | };
|
613 | ```
|
614 |
|
615 | Default: `true` in production mode, otherwise `false`
|
616 |
|
617 | Tell `html-loader` to minimize HTML.
|
618 |
|
619 | #### `boolean`
|
620 |
|
621 | The enabled rules for minimizing by default are the following ones:
|
622 |
|
623 | ```js
|
624 | ({
|
625 | caseSensitive: true,
|
626 | collapseWhitespace: true,
|
627 | conservativeCollapse: true,
|
628 | keepClosingSlash: true,
|
629 | minifyCSS: true,
|
630 | minifyJS: true,
|
631 | removeComments: true,
|
632 | removeRedundantAttributes: true,
|
633 | removeScriptTypeAttributes: true,
|
634 | removeStyleLinkTypeAttributes: true,
|
635 | });
|
636 | ```
|
637 |
|
638 | **webpack.config.js**
|
639 |
|
640 | ```js
|
641 | module.exports = {
|
642 | module: {
|
643 | rules: [
|
644 | {
|
645 | test: /\.html$/i,
|
646 | loader: "html-loader",
|
647 | options: {
|
648 | minimize: true,
|
649 | },
|
650 | },
|
651 | ],
|
652 | },
|
653 | };
|
654 | ```
|
655 |
|
656 | #### `object`
|
657 |
|
658 | **webpack.config.js**
|
659 |
|
660 | See [html-minifier-terser](https://github.com/DanielRuf/html-minifier-terser)'s documentation for more information on the available options.
|
661 |
|
662 | The default rules can be overridden using the following options in your `webpack.conf.js`
|
663 |
|
664 | **webpack.config.js**
|
665 |
|
666 | ```js
|
667 | module.exports = {
|
668 | module: {
|
669 | rules: [
|
670 | {
|
671 | test: /\.html$/i,
|
672 | loader: "html-loader",
|
673 | options: {
|
674 | minimize: {
|
675 | removeComments: false,
|
676 | collapseWhitespace: false,
|
677 | },
|
678 | },
|
679 | },
|
680 | ],
|
681 | },
|
682 | };
|
683 | ```
|
684 |
|
685 | The default rules can be extended:
|
686 |
|
687 | **webpack.config.js**
|
688 |
|
689 | ```js
|
690 | const { defaultMinimizerOptions } = require("html-loader");
|
691 |
|
692 | module.exports = {
|
693 | module: {
|
694 | rules: [
|
695 | {
|
696 | test: /\.html$/i,
|
697 | loader: "html-loader",
|
698 | options: {
|
699 | minimize: {
|
700 | ...defaultMinimizerOptions,
|
701 | removeComments: false,
|
702 | collapseWhitespace: false,
|
703 | },
|
704 | },
|
705 | },
|
706 | ],
|
707 | },
|
708 | };
|
709 | ```
|
710 |
|
711 | ### `esModule`
|
712 |
|
713 | Type:
|
714 |
|
715 | ```ts
|
716 | type esModule = boolean;
|
717 | ```
|
718 |
|
719 | Default: `true`
|
720 |
|
721 | By default, `html-loader` generates JS modules that use the ES modules syntax.
|
722 | There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
|
723 |
|
724 | You can enable a CommonJS modules syntax using:
|
725 |
|
726 | **webpack.config.js**
|
727 |
|
728 | ```js
|
729 | module.exports = {
|
730 | module: {
|
731 | rules: [
|
732 | {
|
733 | test: /\.html$/i,
|
734 | loader: "html-loader",
|
735 | options: {
|
736 | esModule: false,
|
737 | },
|
738 | },
|
739 | ],
|
740 | },
|
741 | };
|
742 | ```
|
743 |
|
744 | ## Examples
|
745 |
|
746 | ### Disable url resolving using the `<!-- webpackIgnore: true -->` comment
|
747 |
|
748 | With `<!-- webpackIgnore: true -->` comment, can to disable sources handling for next tag.
|
749 |
|
750 | ```html
|
751 | <!-- Disabled url handling for the src attribute -->
|
752 | <!-- webpackIgnore: true -->
|
753 | <img src="image.png" />
|
754 |
|
755 | <!-- Disabled url handling for the src and srcset attributes -->
|
756 | <!-- webpackIgnore: true -->
|
757 | <img
|
758 | srcset="image.png 480w, image.png 768w"
|
759 | src="image.png"
|
760 | alt="Elva dressed as a fairy"
|
761 | />
|
762 |
|
763 | <!-- Disabled url handling for the content attribute -->
|
764 | <!-- webpackIgnore: true -->
|
765 | <meta itemprop="image" content="./image.png" />
|
766 |
|
767 | <!-- Disabled url handling for the href attribute -->
|
768 | <!-- webpackIgnore: true -->
|
769 | <link rel="icon" type="image/png" sizes="192x192" href="./image.png" />
|
770 | ```
|
771 |
|
772 | ### roots
|
773 |
|
774 | With [`resolve.roots`](https://webpack.js.org/configuration/resolve/#resolveroots) can specify a list of directories where requests of server-relative URLs (starting with '/') are resolved.
|
775 |
|
776 | **webpack.config.js**
|
777 |
|
778 | ```js
|
779 | module.exports = {
|
780 | context: __dirname,
|
781 | module: {
|
782 | rules: [
|
783 | {
|
784 | test: /\.html$/i,
|
785 | loader: "html-loader",
|
786 | options: {},
|
787 | },
|
788 | {
|
789 | test: /\.jpg$/,
|
790 | type: "asset/resource",
|
791 | },
|
792 | ],
|
793 | },
|
794 | resolve: {
|
795 | roots: [path.resolve(__dirname, "fixtures")],
|
796 | },
|
797 | };
|
798 | ```
|
799 |
|
800 | **file.html**
|
801 |
|
802 | ```html
|
803 | <img src="/image.jpg" />
|
804 | ```
|
805 |
|
806 | ```js
|
807 | // => image.jpg in __dirname/fixtures will be resolved
|
808 | ```
|
809 |
|
810 | ### CDN
|
811 |
|
812 | **webpack.config.js**
|
813 |
|
814 | ```js
|
815 | module.exports = {
|
816 | module: {
|
817 | rules: [
|
818 | {
|
819 | test: /\.jpg$/,
|
820 | type: "asset/resource",
|
821 | },
|
822 | {
|
823 | test: /\.png$/,
|
824 | type: "asset/inline",
|
825 | },
|
826 | ],
|
827 | },
|
828 | output: {
|
829 | publicPath: "http://cdn.example.com/[fullhash]/",
|
830 | },
|
831 | };
|
832 | ```
|
833 |
|
834 | **file.html**
|
835 |
|
836 | ```html
|
837 | <img src="image.jpg" data-src="image2x.png" />
|
838 | ```
|
839 |
|
840 | **index.js**
|
841 |
|
842 | ```js
|
843 | require("html-loader!./file.html");
|
844 |
|
845 | // => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg" data-src="image2x.png">'
|
846 | ```
|
847 |
|
848 | ```js
|
849 | require('html-loader?{"sources":{"list":[{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html');
|
850 |
|
851 | // => '<img src="image.jpg" data-src="data:image/png;base64,..." >'
|
852 | ```
|
853 |
|
854 | ```js
|
855 | require('html-loader?{"sources":{"list":[{"tag":"img","attribute":"src","type":"src"},{"tag":"img","attribute":"data-src","type":"src"}]}}!./file.html');
|
856 |
|
857 | // => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg" data-src="data:image/png;base64,..." >'
|
858 | ```
|
859 |
|
860 | ### Process `script` and `link` tags
|
861 |
|
862 | **script.file.js**
|
863 |
|
864 | ```js
|
865 | console.log(document);
|
866 | ```
|
867 |
|
868 | **style.file.css**
|
869 |
|
870 | ```css
|
871 | a {
|
872 | color: red;
|
873 | }
|
874 | ```
|
875 |
|
876 | **file.html**
|
877 |
|
878 | ```html
|
879 | <!DOCTYPE html>
|
880 | <html>
|
881 | <head>
|
882 | <meta charset="UTF-8" />
|
883 | <title>Title of the document</title>
|
884 | <link rel="stylesheet" type="text/css" href="./style.file.css" />
|
885 | </head>
|
886 | <body>
|
887 | Content of the document......
|
888 | <script src="./script.file.js"></script>
|
889 | </body>
|
890 | </html>
|
891 | ```
|
892 |
|
893 | **webpack.config.js**
|
894 |
|
895 | ```js
|
896 | module.exports = {
|
897 | module: {
|
898 | rules: [
|
899 | {
|
900 | test: /\.html$/,
|
901 | type: "asset/resource",
|
902 | generator: {
|
903 | filename: "[name][ext]",
|
904 | },
|
905 | },
|
906 | {
|
907 | test: /\.html$/i,
|
908 | use: ["html-loader"],
|
909 | },
|
910 | {
|
911 | test: /\.js$/i,
|
912 | exclude: /\.file.js$/i,
|
913 | loader: "babel-loader",
|
914 | },
|
915 | {
|
916 | test: /\.file.js$/i,
|
917 | type: "asset/resource",
|
918 | },
|
919 | {
|
920 | test: /\.css$/i,
|
921 | exclude: /\.file.css$/i,
|
922 | loader: "css-loader",
|
923 | },
|
924 | {
|
925 | test: /\.file.css$/i,
|
926 | type: "asset/resource",
|
927 | },
|
928 | ],
|
929 | },
|
930 | };
|
931 | ```
|
932 |
|
933 | ### Templating
|
934 |
|
935 | You can use any template system. Below is an example for [handlebars](https://handlebarsjs.com/).
|
936 |
|
937 | **file.hbs**
|
938 |
|
939 | ```hbs
|
940 | <div>
|
941 | <p>{{firstname}} {{lastname}}</p>
|
942 | <img src="image.png" alt="alt" />
|
943 | <div>
|
944 | ```
|
945 |
|
946 | **webpack.config.js**
|
947 |
|
948 | ```js
|
949 | const Handlebars = require("handlebars");
|
950 |
|
951 | module.exports = {
|
952 | module: {
|
953 | rules: [
|
954 | {
|
955 | test: /\.hbs$/i,
|
956 | loader: "html-loader",
|
957 | options: {
|
958 | preprocessor: (content, loaderContext) => {
|
959 | let result;
|
960 |
|
961 | try {
|
962 | result = Handlebars.compile(content)({
|
963 | firstname: "Value",
|
964 | lastname: "OtherValue",
|
965 | });
|
966 | } catch (error) {
|
967 | loaderContext.emitError(error);
|
968 |
|
969 | return content;
|
970 | }
|
971 |
|
972 | return result;
|
973 | },
|
974 | },
|
975 | },
|
976 | ],
|
977 | },
|
978 | };
|
979 | ```
|
980 |
|
981 | ### PostHTML
|
982 |
|
983 | You can use [PostHTML](https://github.com/posthtml/posthtml) without any additional loaders.
|
984 |
|
985 | **file.html**
|
986 |
|
987 | ```html
|
988 | <img src="image.jpg" />
|
989 | ```
|
990 |
|
991 | **webpack.config.js**
|
992 |
|
993 | ```js
|
994 | const posthtml = require("posthtml");
|
995 | const posthtmlWebp = require("posthtml-webp");
|
996 |
|
997 | module.exports = {
|
998 | module: {
|
999 | rules: [
|
1000 | {
|
1001 | test: /\.hbs$/i,
|
1002 | loader: "html-loader",
|
1003 | options: {
|
1004 | preprocessor: (content, loaderContext) => {
|
1005 | let result;
|
1006 |
|
1007 | try {
|
1008 | result = posthtml().use(plugin).process(content, { sync: true });
|
1009 | } catch (error) {
|
1010 | loaderContext.emitError(error);
|
1011 |
|
1012 | return content;
|
1013 | }
|
1014 |
|
1015 | return result.html;
|
1016 | },
|
1017 | },
|
1018 | },
|
1019 | ],
|
1020 | },
|
1021 | };
|
1022 | ```
|
1023 |
|
1024 | ### Export into HTML files
|
1025 |
|
1026 | A very common scenario is exporting the HTML into their own _.html_ file, to
|
1027 | serve them directly instead of injecting with javascript. This can be achieved
|
1028 | with a combination of 2 loaders:
|
1029 |
|
1030 | - [extract-loader](https://github.com/peerigon/extract-loader)
|
1031 | - html-loader
|
1032 |
|
1033 | and [`asset modules`](https://webpack.js.org/guides/asset-modules/)
|
1034 |
|
1035 | The html-loader will parse the URLs, require the images and everything you
|
1036 | expect. The extract loader will parse the javascript back into a proper html
|
1037 | file, ensuring images are required and point to proper path, and the [`asset modules`](https://webpack.js.org/guides/asset-modules/)
|
1038 | will write the _.html_ file for you. Example:
|
1039 |
|
1040 | **webpack.config.js**
|
1041 |
|
1042 | ```js
|
1043 | module.exports = {
|
1044 | output: {
|
1045 | assetModuleFilename: "[name][ext]",
|
1046 | },
|
1047 | module: {
|
1048 | rules: [
|
1049 | {
|
1050 | test: /\.html$/,
|
1051 | type: "asset/resource",
|
1052 | generator: {
|
1053 | filename: "[name][ext]",
|
1054 | },
|
1055 | },
|
1056 | {
|
1057 | test: /\.html$/i,
|
1058 | use: ["html-loader"],
|
1059 | },
|
1060 | ],
|
1061 | },
|
1062 | };
|
1063 | ```
|
1064 |
|
1065 | ## Contributing
|
1066 |
|
1067 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
1068 |
|
1069 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
1070 |
|
1071 | ## License
|
1072 |
|
1073 | [MIT](./LICENSE)
|
1074 |
|
1075 | [npm]: https://img.shields.io/npm/v/html-loader.svg
|
1076 | [npm-url]: https://npmjs.com/package/html-loader
|
1077 | [node]: https://img.shields.io/node/v/html-loader.svg
|
1078 | [node-url]: https://nodejs.org
|
1079 | [tests]: https://github.com/webpack-contrib/html-loader/workflows/html-loader/badge.svg
|
1080 | [tests-url]: https://github.com/webpack-contrib/html-loader/actions
|
1081 | [cover]: https://codecov.io/gh/webpack-contrib/html-loader/branch/master/graph/badge.svg
|
1082 | [cover-url]: https://codecov.io/gh/webpack-contrib/html-loader
|
1083 | [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
1084 | [chat-url]: https://gitter.im/webpack/webpack
|
1085 | [size]: https://packagephobia.now.sh/badge?p=html-loader
|
1086 | [size-url]: https://packagephobia.now.sh/result?p=html-loader
|