Skip to content

Commit

Permalink
Merge pull request #1 from ranb2002/first-version
Browse files Browse the repository at this point in the history
First version of the plugin
  • Loading branch information
benjaminrancourt authored Oct 16, 2020
2 parents 68b6427 + b9efed2 commit b80a7d0
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 120
trim_trailing_whitespace = true
5 changes: 5 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const filesMinifier = require("./src/files-minifier");

module.exports = (eleventyConfig) => {
eleventyConfig.addTransform("files-minifier", filesMinifier);
};
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
// Extends the UdeS ESLint config
extends: "eslint-config-udes/node-14",

// Limit ESLint to a specific project
root: true,
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
/node_modules/
/package-lock.json
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org
File renamed without changes.
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# eleventy-plugin-files-minifier
# eleventy-plugin-files-minifier

This plugin allow you to automatically **minify** files when builting with **[Eleventy](https://www.11ty.dev/)**.
It currently supports `html`, `json`, `xml`, `xsl` and `webmanifest` files.

Why should you minify your files? Simply to reduce the data transfered between your hosting servers and your visitors,
even if some of them maybe some bots.

Under the hood, this plugin use the following plugins to minify code:

- [pretty-data](https://www.npmjs.com/package/pretty-data): `json`, `xml` and `webmanifest`
- [html-minifier](https://www.npmjs.com/package/html-minifier): `html` and `xsl`

## Installation

Install the dependency with NPM:

```shell script
npm install @sherby/eleventy-plugin-files-minifier --save-dev
```

Open up your Eleventy config file (probably `.eleventy.js`) and use `addPlugin`:

```javascript
const eleventyPluginFilesMinifier = require("@sherby/eleventy-plugin-files-minifier");
module.exports = (eleventyConfig) => {
eleventyConfig.addPlugin(eleventyPluginFilesMinifier);
};
```

## Usage

The plugin will automatically minify supported files, you don't need to do anything except the installation!

Make sure that the files you want to minify are currently written by Eleventy. If not, you can easily rename it and add
Front matter options. By example, for the `manifest.webmanifest` file, I could rename it as `manifest.webmanifest.njk`
and add the following code at his top:

```
---
eleventyExcludeFromCollections: true
permalink: /manifest.webmanifest
---
```

## Publish

Increment the `version` defined in the `package.json` file and run the command below to publish the module in the
registry:

```bash
# Dry run
npm publish --dry-run

# For real (are you really sure?)
npm publish
```

## License

The [MIT License][1] (MIT)

[1]: https://opensource.org/licenses/MIT
57 changes: 57 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@sherby/eleventy-plugin-files-minifier",
"version": "1.0.0",
"description": "An Eleventy plugin to automatically minify files",
"keywords": [
"11ty",
"code",
"eleventy",
"eleventy-plugin",
"files",
"html",
"json",
"min",
"minification",
"sherby",
"transform",
"webmanifest",
"xml",
"xsl"
],
"homepage": "https://github.com/ranb2002/eleventy-plugin-files-minifier#readme",
"bugs": {
"url": "https://github.com/ranb2002/eleventy-plugin-files-minifier/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ranb2002/eleventy-plugin-files-minifier.git"
},
"license": "MIT",
"author": "Benjamin Rancourt",
"main": ".eleventy.js",
"files": [
".eleventy.js",
"src/files-minifier.js"
],
"scripts": {
"format": "npm-run-all format:*",
"format:eslint": "npm run lint:eslint -- --fix",
"format:prettier": "npm run lint:prettier -- --write",
"lint": "npm-run-all lint:*",
"lint:eslint": "eslint --ignore-path .gitignore . --ext js,json,md",
"lint:prettier": "prettier --ignore-path .gitignore --check '**/*.{js,json,md}'"
},
"dependencies": {
"html-minifier": "^4.0.0",
"pretty-data": "^0.40.0"
},
"devDependencies": {
"eslint-config-udes": "^0.4.3",
"eslint-plugin-json-format": "^2.0.1",
"eslint-plugin-markdown": "^1.0.2",
"eslint-plugin-prettier": "^3.1.4",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"prettier-config-udes": "0.0.6"
}
}
43 changes: 43 additions & 0 deletions src/files-minifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const htmlmin = require("html-minifier");
const { pd: prettyData } = require("pretty-data");

module.exports = (value, outputPath) => {
// Check if the outputPath end by the extension
const pathEndBy = (extension) => outputPath.includes(extension);

// HTML and XSL
if (pathEndBy(".html") || pathEndBy(".xsl")) {
const config = {
collapseBooleanAttributes: true, // Omit attribute values from boolean attributes
collapseWhitespace: true, // Collapse white space that contributes to text nodes in a document tree
decodeEntities: true, // Use direct Unicode characters whenever possible
html5: true, // Parse input according to HTML5 specifications
minifyCSS: true, // Minify CSS in style elements and style attributes (uses clean-css)
minifyJS: true, // Minify JavaScript in script elements and event attributes (uses UglifyJS)
removeComments: true, // Strip HTML comments
removeEmptyAttributes: true, // Remove all attributes with whitespace-only values
removeEmptyElements: true, // Remove all elements with empty contents
sortAttributes: true, // Sort attributes by frequency
sortClassName: true, // Sort style classes by frequency
useShortDoctype: true, // Replaces the doctype with the short (HTML5) doctype
};

if (pathEndBy(".xsl")) {
config.keepClosingSlash = true;
}

return htmlmin.minify(value, config);
}

// XML
if (pathEndBy(".xml")) {
return prettyData.xmlmin(value);
}

// JSON
if (pathEndBy(".json") || pathEndBy(".webmanifest")) {
return prettyData.jsonmin(value);
}

return value;
};

0 comments on commit b80a7d0

Please sign in to comment.