Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit c7dca60

Browse files
committed
htmlminify, fixes bazilio91#9
1 parent c6d4e12 commit c7dca60

File tree

7 files changed

+66
-16
lines changed

7 files changed

+66
-16
lines changed

.editorconfig

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[*]
2+
end_of_line = lf
3+
insert_final_newline = true
4+
5+
indent_style = space
6+
indent_size = 2

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ template(data) // Pass object with data
2424
<%- include templates/child -%>
2525
```
2626

27+
## Options
28+
29+
Following options can be specified in query:
30+
31+
`beatify` — enable or disable uglify-js beautify of teamplate ast
32+
33+
`compileDebug` — see ejs compileDebug option
34+
35+
`htmlmin` — see [htmlminify section](#htmlminify)
36+
37+
## htmlminify
38+
39+
```javascript
40+
module: {
41+
loaders: [
42+
{test: /\.ejs$/, loader: 'ejs-compiled?htmlmin'} // enable here
43+
]
44+
},
45+
'ejs-compiled-loader': {
46+
'htmlmin': true, // or enable here
47+
'htmlminOptions': {
48+
removeComments: true
49+
}
50+
}
51+
```
52+
53+
See [all options reference](https://github.com/kangax/html-minifier#options-quick-reference)
54+
2755
## License
2856

2957
MIT (http://www.opensource.org/licenses/mit-license.php)

index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
var ejs = require('ejs'),
22
UglifyJS = require('uglify-js'),
33
utils = require('loader-utils'),
4-
path = require('path');
4+
path = require('path'),
5+
htmlmin = require('html-minifier'),
6+
merge = require('merge');
57

68

79
module.exports = function (source) {
810
this.cacheable && this.cacheable();
9-
var opts = utils.parseQuery(this.query);
11+
var opts = merge(this.options['ejs-compiled-loader'] || {}, utils.parseQuery(this.query));
1012
opts.client = true;
1113

1214
// Skip compile debug for production when running with
@@ -18,6 +20,10 @@ module.exports = function (source) {
1820
// Use filenames relative to working dir, which should be project root
1921
opts.filename = path.relative(process.cwd(), this.resourcePath);
2022

23+
if (opts.htmlmin) {
24+
source = htmlmin.minify(source, opts['htmlminOptions'] || {});
25+
}
26+
2127
var template = ejs.compile(source, opts);
2228

2329
// Beautify javascript code

test/app.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const assert = require('assert');
33
var tpl = require("./template.ejs");
44
assert.equal(tpl({noun: "World"}), 'Hello, World!');
55

6+
var tpl3 = require("./subdir/parent.ejs");
7+
assert.equal(tpl3({foo: "foo"}), "parent: child: foo\n");
8+
9+
var tpl4 = require("./htmlmin.ejs");
10+
assert.equal(tpl4({test: 123}), '123');
11+
612
var tpl2 = require("!!../?delimiter=?!./template2.ejs");
713
assert.equal(tpl2({hobbies: ["food", "code"]}).trimRight(), " I like food.\n I like code.");
8-
9-
var tpl3 = require("./subdir/parent.ejs");
10-
assert.equal(tpl3({foo: "foo"}), "parent: child: foo\n\n");

test/htmlmin.ejs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- test minify --><%= test %>

test/template.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Hello, <%=noun%>!
1+
Hello, <%=noun%>!

test/webpack.config.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
module.exports = {
2-
entry: "./app.js",
3-
output: {
4-
path: __dirname,
5-
filename: "bundle.js"
6-
},
7-
module: {
8-
loaders: [
9-
{ test: /\.ejs$/, loader: require.resolve("../") }
10-
]
2+
entry: "./app.js",
3+
cache: false,
4+
output: {
5+
path: __dirname,
6+
filename: "bundle.js"
7+
},
8+
module: {
9+
loaders: [
10+
{test: /\.ejs$/, loader: require.resolve("../") + "?htmlmin"}
11+
]
12+
},
13+
'ejs-compiled-loader': {
14+
'htmlminOptions': {
15+
removeComments: true
1116
}
12-
}
17+
}
18+
}

0 commit comments

Comments
 (0)