Skip to content

Commit d11a15e

Browse files
2007heavendevongovett
authored andcommitted
Make htmlnano configurable for production (#445)
* Parse htmlnano config Note that htmlnano fires only if Bundler.options.minify is true. * Add .htmlnanorc test * Fix naming
1 parent 14e7880 commit d11a15e

4 files changed

Lines changed: 68 additions & 7 deletions

File tree

src/transforms/posthtml.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ async function getConfig(asset) {
3232
config.plugins = await loadPlugins(config.plugins, asset.name);
3333

3434
if (asset.options.minify) {
35-
const htmlNanoOptions = {
36-
collapseWhitespace: 'conservative'
37-
};
38-
config.plugins.push(htmlnano(htmlNanoOptions));
35+
const htmlNanoConfig = asset.package.htmlnano ||
36+
(await Config.load(asset.name, ['.htmlnanorc', '.htmlnanorc.js'])) || {
37+
collapseWhitespace: 'conservative'
38+
};
39+
40+
config.plugins.push(htmlnano(htmlNanoConfig));
3941
}
4042

4143
config.skipParse = true;

test/html.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,36 @@ describe('html', function() {
124124
production: true
125125
});
126126

127-
let css = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
128-
assert(css.includes('Other page'));
129-
assert(!css.includes('\n'));
127+
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
128+
assert(html.includes('Other page'));
129+
assert(!html.includes('\n'));
130+
});
131+
132+
it('should read .htmlnanorc and minify HTML in production mode', async function() {
133+
await bundle(__dirname + '/integration/htmlnano-config/index.html', {
134+
production: true
135+
});
136+
137+
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
138+
139+
// mergeStyles
140+
assert(
141+
html.includes(
142+
'<style>h1{color:red}div{font-size:20px}</style><style media="print">div{color:blue}</style>'
143+
)
144+
);
145+
146+
// minifyJson
147+
assert(
148+
html.includes('<script type="application/json">{"user":"me"}</script>')
149+
);
150+
151+
// minifySvg is false
152+
assert(
153+
html.includes(
154+
'<svg version="1.1" baseprofile="full" width="300" height="200" xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%" fill="red"></rect><circle cx="150" cy="100" r="80" fill="green"></circle><text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text></svg>'
155+
)
156+
);
130157
});
131158

132159
it('should not prepend the public path to assets with remote URLs', async function() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
mergeStyles: true,
3+
minifySvg: false,
4+
minifyJson: true
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Test</title>
5+
<style>h1 { color: red }</style>
6+
<style media="print">div { color: blue }</style>
7+
8+
<style type="text/css" media="print">a {}</style>
9+
<style>div { font-size: 20px }</style>
10+
</head>
11+
<body>
12+
<h1>Index page</h1>
13+
<svg version="1.1" baseProfile="full" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
14+
<rect width="100%" height="100%" fill="red" />
15+
16+
<circle cx="150" cy="100" r="80" fill="green" />
17+
18+
<text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
19+
</svg>
20+
<script type="application/json">
21+
{
22+
"user": "me"
23+
}
24+
</script>
25+
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)