Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minimize is more safely #304

Merged
merged 2 commits into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions README.md
Expand Up @@ -484,16 +484,20 @@ Tell `html-loader` to minimize HTML.

The enabled rules for minimizing by default are the following ones:

- `collapseWhitespace`
- `conservativeCollapse`
- `keepClosingSlash`
- `minifyCSS`
- `minifyJS`
- `removeAttributeQuotes`
- `removeComments`
- `removeScriptTypeAttributes`
- `removeStyleTypeAttributes`
- `useShortDoctype`
```js
({
caseSensitive: true,
collapseWhitespace: true,
conservativeCollapse: true,
keepClosingSlash: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
});
```

**webpack.config.js**

Expand Down
10 changes: 8 additions & 2 deletions src/plugins/minimizer-plugin.js
Expand Up @@ -6,16 +6,22 @@ export default (options) =>
typeof options.minimize === 'boolean' ||
typeof options.minimize === 'undefined'
? {
caseSensitive: true,
// `collapseBooleanAttributes` is not always safe, since this can break CSS attribute selectors and not safe for XHTML
collapseWhitespace: true,
conservativeCollapse: true,
keepClosingSlash: true,
// We need ability to use cssnano, or setup own function without extra dependencies
minifyCSS: true,
minifyJS: true,
removeAttributeQuotes: true,
// `minifyURLs` is unsafe, because we can't guarantee what the base URL is
// `removeAttributeQuotes` is not safe in some rare cases, also HTML spec recommends against doing this
removeComments: true,
// `removeEmptyAttributes` is not safe, can affect certain style or script behavior
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
// `useShortDoctype` is not safe for XHTML
}
: options.minimize;

Expand Down
352 changes: 330 additions & 22 deletions test/__snapshots__/attributes-option.test.js.snap

Large diffs are not rendered by default.

96 changes: 90 additions & 6 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

32 changes: 30 additions & 2 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

121 changes: 109 additions & 12 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions test/fixtures/XHTML.html
@@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
</head>
<body>

some content here...

<br />

<a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
<a HREF="https://www.w3schools.com/html/">Visit our HTML tutorial</a>

<input type="checkbox" name="vehicle" value="car" checked="checked" />

<tag>Text</tag>
<TAG>Text</TAG>

</body>
</html>
3 changes: 3 additions & 0 deletions test/fixtures/XHTML.js
@@ -0,0 +1,3 @@
import xhtml from './XHTML.html';

export default xhtml;
29 changes: 29 additions & 0 deletions test/fixtures/simple.html
Expand Up @@ -289,3 +289,32 @@ <h2>An Ordered HTML List</h2>
<svg width="200" height="200">
<use xlink:href="./webpack.svg"></use>
</svg>

<span>foo</span> <span>bar</span>

<tag>Text</tag>
<TAG>Text</TAG>

<input type="checkbox" name="vehicle" value="car" checked="checked" />

<br />

<a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
<a HREF="https://www.w3schools.com/html/">Visit our HTML tutorial</a>

<script type="text/javascript">
console.log('HERE')
</script>

<style type="text/css">
p {
color: #26b72b;
}
</style>

<div data-b="2" data-a="1" data-c="3">text</div>
<div class="f a b e d c">text</div>

<input type="text">

<button onclick=" document.getElementById('demo').innerHTML = Date()">The time is?</button>
12 changes: 12 additions & 0 deletions test/minimize-option.test.js
Expand Up @@ -111,4 +111,16 @@ describe('"minimize" option', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work with XHTML', async () => {
const compiler = getCompiler('XHTML.js', { minimize: true });
const stats = await compile(compiler);

expect(getModuleSource('./XHTML.html', stats)).toMatchSnapshot('module');
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});