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

feat: implement html minifier #1015

Merged
merged 5 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions plugins/plugin-webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ npm install --save-dev @snowpack/plugin-webpack
- `outputPattern: {css: string, js: string, assets: string}` - Set the URL for your final bundled files. This is where they will be written to disk in the `build/` directory. See Webpack's [`output.filename`](https://webpack.js.org/configuration/output/#outputfilename) documentation for examples of valid values.
- `extendConfig: (config: WebpackConfig) => WebpackConfig` - extend your webpack config, see below.
- `manifest: boolean | string` - Enable generating a manifest file. The default value is `false`, the default file name is `./asset-manifest.json` if setting manifest to `true`. The relative path is resolved from the output directory.
- `htmlMinifierOptions: boolean | object` - See below.
stefanfrede marked this conversation as resolved.
Show resolved Hide resolved

#### Extending The Default Webpack Config

Expand All @@ -50,3 +51,34 @@ module.exports = {
],
};
```

#### Minify HTML

With `htmlMinifierOptions` you can either disable the minification entirely or provide your own [options](https://github.com/kangax/html-minifier#options-quick-reference).

```js
// snowpack.config.js
module.exports = {
plugins: [
[
"@snowpack/plugin-webpack",
{
htmlMinifierOptions: false // disabled entirely,
},
],
],
};
```

The default options are:

```js
{
collapseWhitespace: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
}
```
1 change: 1 addition & 0 deletions plugins/plugin-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"css-loader": "^3.5.3",
"file-loader": "^6.0.0",
"glob": "^7.1.6",
"html-minifier": "^4.0.0",
"jsdom": "^16.2.2",
"mini-css-extract-plugin": "^0.9.0",
"optimize-css-assets-webpack-plugin": "^5.0.3",
Expand Down
41 changes: 37 additions & 4 deletions plugins/plugin-webpack/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ManifestPlugin = require('webpack-manifest-plugin');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const cwd = process.cwd();
const minify = require('html-minifier').minify;

function insertBefore(newNode, existingNode) {
existingNode.parentNode.insertBefore(newNode, existingNode);
Expand Down Expand Up @@ -51,7 +52,13 @@ function parseHTMLFiles({ buildDirectory }) {
return { doms, jsEntries };
}

function emitHTMLFiles({ doms, jsEntries, stats, baseUrl, buildDirectory }) {
function emitHTMLFiles({
doms,
jsEntries,
stats, baseUrl,
buildDirectory,
htmlMinifierOptions,
}) {
const entrypoints = stats.toJson({ assets: false, hash: true }).entrypoints;

//Now that webpack is done, modify the html files to point to the newly compiled resources
Expand Down Expand Up @@ -87,7 +94,11 @@ function emitHTMLFiles({ doms, jsEntries, stats, baseUrl, buildDirectory }) {

//And write our modified html files out to the destination
for (const [htmlFile, dom] of Object.entries(doms)) {
fs.writeFileSync(path.join(buildDirectory, htmlFile), dom.serialize());
const html = htmlMinifierOptions
? minify(dom.serialize(), htmlMinifierOptions)
: dom.serialize();

fs.writeFileSync(path.join(buildDirectory, htmlFile), html);
}
}

Expand Down Expand Up @@ -161,7 +172,22 @@ module.exports = function plugin(config, args) {
if (!cssOutputPattern.endsWith(".css")) {
throw new Error("Output Pattern for CSS must end in .css");
}


// Default options for HTMLMinifier
// https://github.com/kangax/html-minifier#options-quick-reference
let htmlMinifierOptions = {
collapseWhitespace: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
};

if (typeof args.htmlMinifierOptions !== 'undefined') {
htmlMinifierOptions = args.htmlMinifierOptions;
}
stefanfrede marked this conversation as resolved.
Show resolved Hide resolved

const manifest =
typeof args.manifest === 'string'
? args.manifest
Expand Down Expand Up @@ -355,7 +381,14 @@ module.exports = function plugin(config, args) {
);
}

emitHTMLFiles({ doms, jsEntries, stats, baseUrl, buildDirectory });
emitHTMLFiles({
doms,
jsEntries,
stats,
baseUrl,
buildDirectory,
htmlMinifierOptions,
});
},
};
};