@@ -50,12 +50,66 @@ module.exports = {
50
50
51
51
## Options
52
52
53
- | Name | Type | Default | Description |
54
- | :-----------------------------: | :------------------------: | :------------------------------------------: | :--------------------------------------- |
55
- | ** [ ` attributes ` ] ( #attributes ) ** | ` {Boolean\/Array\/Object} ` | ` true ` | Enables/Disables attributes handling |
56
- | ** [ ` root ` ] ( #root ) ** | ` {String} ` | ` undefiend ` | Allow to handle root-relative attributes |
57
- | ** [ ` minimize ` ] ( #minimize ) ** | ` {Boolean\|Object} ` | ` true ` in production mode, otherwise ` false ` | Tell ` html-loader ` to minimize HTML |
58
- | ** [ ` esModule ` ] ( #esmodule ) ** | ` {Boolean} ` | ` false ` | Use ES modules syntax |
53
+ | Name | Type | Default | Description |
54
+ | :---------------------------------: | :------------------------: | :------------------------------------------: | :----------------------------------------------- |
55
+ | ** [ ` preprocessor ` ] ( #preprocessor ) ** | ` {Function} ` | ` undefined ` | Allows pre-processing of content before handling |
56
+ | ** [ ` attributes ` ] ( #attributes ) ** | ` {Boolean\/Array\/Object} ` | ` true ` | Enables/Disables attributes handling |
57
+ | ** [ ` root ` ] ( #root ) ** | ` {String} ` | ` undefiend ` | Allow to handle root-relative attributes |
58
+ | ** [ ` minimize ` ] ( #minimize ) ** | ` {Boolean\|Object} ` | ` true ` in production mode, otherwise ` false ` | Tell ` html-loader ` to minimize HTML |
59
+ | ** [ ` esModule ` ] ( #esmodule ) ** | ` {Boolean} ` | ` false ` | Use ES modules syntax |
60
+
61
+ ### ` preprocessor `
62
+
63
+ Type: ` Function `
64
+ Default: ` undefined `
65
+
66
+ Allows pre-processing of content before handling.
67
+
68
+ > ⚠ You should always return valid HTML
69
+
70
+ ** file.hbs**
71
+
72
+ ``` hbs
73
+ <div>
74
+ <p>{{firstname}} {{lastname}}</p>
75
+ <img src="image.png" alt="alt" />
76
+ <div>
77
+ ```
78
+
79
+ ** webpack.config.js**
80
+
81
+ ``` js
82
+ const Handlebars = require (' handlebars' );
83
+
84
+ module .exports = {
85
+ module: {
86
+ rules: [
87
+ {
88
+ test: / \. hbs$ / i ,
89
+ loader: ' html-loader' ,
90
+ options: {
91
+ preprocessor : (content , loaderContext ) => {
92
+ let result;
93
+
94
+ try {
95
+ result = Handlebars .compile (content)({
96
+ firstname: ' Value' ,
97
+ lastname: ' OtherValue' ,
98
+ });
99
+ } catch (error) {
100
+ loaderContext .emitError (error);
101
+
102
+ return content;
103
+ }
104
+
105
+ return result;
106
+ },
107
+ },
108
+ },
109
+ ],
110
+ },
111
+ };
112
+ ```
59
113
60
114
### ` attributes `
61
115
@@ -501,6 +555,99 @@ require('html-loader?root=.!./file.html');
501
555
// => '<img src="http://cdn.example.com/49eba9f/a992ca.jpg">'
502
556
```
503
557
558
+ ### Templating
559
+
560
+ You can use any template system. Below is an example for [ handlebars] ( https://handlebarsjs.com/ ) .
561
+
562
+ ** file.hbs**
563
+
564
+ ``` hbs
565
+ <div>
566
+ <p>{{firstname}} {{lastname}}</p>
567
+ <img src="image.png" alt="alt" />
568
+ <div>
569
+ ```
570
+
571
+ ** webpack.config.js**
572
+
573
+ ``` js
574
+ const Handlebars = require (' handlebars' );
575
+
576
+ module .exports = {
577
+ module: {
578
+ rules: [
579
+ {
580
+ test: / \. hbs$ / i ,
581
+ loader: ' html-loader' ,
582
+ options: {
583
+ preprocessor : (content , loaderContext ) => {
584
+ let result;
585
+
586
+ try {
587
+ result = Handlebars .compile (content)({
588
+ firstname: ' Value' ,
589
+ lastname: ' OtherValue' ,
590
+ });
591
+ } catch (error) {
592
+ loaderContext .emitError (error);
593
+
594
+ return content;
595
+ }
596
+
597
+ return result;
598
+ },
599
+ },
600
+ },
601
+ ],
602
+ },
603
+ };
604
+ ```
605
+
606
+ ### PostHTML
607
+
608
+ You can use [ PostHTML] ( https://github.com/posthtml/posthtml ) without any additional loaders.
609
+
610
+ ** file.html**
611
+
612
+ ``` html
613
+ <img src =" image.jpg" />
614
+ ```
615
+
616
+ ** webpack.config.js**
617
+
618
+ ``` js
619
+ const posthtml = require (' posthtml' );
620
+ const posthtmlWebp = require (' posthtml-webp' );
621
+
622
+ module .exports = {
623
+ module: {
624
+ rules: [
625
+ {
626
+ test: / \. hbs$ / i ,
627
+ loader: ' html-loader' ,
628
+ options: {
629
+ preprocessor : () => {
630
+ let result;
631
+
632
+ try {
633
+ result = posthtml ()
634
+ .use (plugin)
635
+ .process (content, { sync: true });
636
+ } catch (error) {
637
+ loaderContext .emitError (error);
638
+
639
+ return content;
640
+ }
641
+
642
+ return result .html ;
643
+ },
644
+ },
645
+ },
646
+ ],
647
+ },
648
+ };
649
+ ```
650
+
504
651
### Export into HTML files
505
652
506
653
A very common scenario is exporting the HTML into their own _ .html_ file, to
0 commit comments