Skip to content

Commit f831c75

Browse files
committed
initial commit
0 parents  commit f831c75

File tree

14 files changed

+10142
-0
lines changed

14 files changed

+10142
-0
lines changed

.circleci/config.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: 2
2+
jobs:
3+
build:
4+
working_directory: /usr/src/app
5+
docker:
6+
- image: banian/node
7+
steps:
8+
# Checkout repository
9+
- checkout
10+
11+
# Restore cache
12+
- restore_cache:
13+
key: yarn-{{ checksum "yarn.lock" }}
14+
15+
# Install dependencies
16+
- run:
17+
name: Install Dependencies
18+
command: NODE_ENV=dev yarn
19+
20+
# Keep cache
21+
- save_cache:
22+
key: yarn-{{ checksum "yarn.lock" }}
23+
paths:
24+
- "node_modules"
25+
26+
# Test
27+
- run:
28+
name: Tests
29+
command: yarn test
30+
31+
# Coverage
32+
- run:
33+
name: Coverage
34+
command: yarn codecov

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_size = 2
6+
indent_style = space
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
root: true,
3+
parserOptions: {
4+
parser: 'babel-eslint',
5+
sourceType: 'module'
6+
},
7+
extends: '@nuxtjs'
8+
}

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
*.iml
3+
.idea
4+
*.log*
5+
.nuxt
6+
.vscode
7+
.DS_STORE
8+
coverage
9+
dist

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
:warning: This module is experimental, try it and let us know how it performs
2+
3+
# @nuxtjs/html-minifier
4+
[![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/html-minifier/latest.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/html-minifier)
5+
[![npm](https://img.shields.io/npm/dt/@nuxtjs/html-minifier.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/html-minifier)
6+
[![CircleCI](https://img.shields.io/circleci/project/github/nuxt-community/html-minifier-module.svg?style=flat-square)](https://circleci.com/gh/nuxt-community/html-minifier-module)
7+
8+
Minify the html for each request served by nuxt server (`nuxt start`)
9+
10+
> :information_source: `nuxt generate` already has built-in support for minifying html
11+
12+
> :fire: Using this module could be a performance hit :fire:
13+
> it is recommended to only use it when you have a caching proxy in front of your Nuxt server (at least until we have benchmarks to determine the real world impact)
14+
15+
## Usage
16+
17+
`yarn add @nuxtjs/html-minifier` OR `npm i @nuxtjs/html-minifier`
18+
19+
Add `@nuxtjs/html-minifier` to `modules` section of `nuxt.config.js`
20+
21+
```js
22+
{
23+
modules: [
24+
['@nuxtjs/html-minifier', { log: 'once', logHtml: true }]
25+
]
26+
}
27+
```
28+
29+
## Options
30+
31+
### `keepTrying`
32+
33+
- Default: `true`
34+
35+
If `false` then every url which generated an error wont be minified in future requests
36+
37+
### `log`
38+
- Default: `always`
39+
40+
- `always`: always log html-minifier errors
41+
- `once`: only log html-minifier errors once for each url
42+
- `false`: never log html-minifier errors
43+
44+
45+
### `logHtml`
46+
- Default: `false`
47+
48+
> Be wary to enable this in production, your disk could fill up quickly!
49+
50+
If `true` then the html which failed to minimize is also logged
51+
52+
## Development
53+
54+
- Clone this repository
55+
- Install dependencies using `yarn install` or `npm install`
56+
- Start development server using `npm run dev`
57+
58+
## License
59+
60+
[MIT License](./LICENSE)
61+
62+
Copyright (c) Nuxt Community.

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
testEnvironment: "node",
3+
collectCoverage: true
4+
}

lib/module.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import consola from 'consola'
2+
3+
const defaultOptions = {
4+
keepTrying: true,
5+
log: 'always',
6+
logHtml: false
7+
}
8+
9+
export default function htmlMinificationModule(moduleOptions) {
10+
// only add hook when server is ready we could
11+
this.nuxt.hook('render:done', async () => {
12+
const htmlMinifier = await import('html-minifier')
13+
const figures = await import('figures')
14+
15+
this.options.cli.badgeMessages.push(`${figures.radioOn} HTML minification enabled`)
16+
17+
const options = Object.assign({}, defaultOptions, moduleOptions)
18+
if (options.log && !['always', 'once'].includes(options.log)) {
19+
options.log = defaultOptions.log
20+
}
21+
22+
const minifierErrors = []
23+
this.nuxt.hook('render:route', (url, result, context) => {
24+
if (options.keepTrying || !minifierErrors.includes(url)) {
25+
try {
26+
const html = htmlMinifier.minify(result.html, this.options.build.html.minify)
27+
result.html = html
28+
} catch (err) { /* istanbul ignore next */
29+
if (this.options.dev || options.log === 'always' || (options.log === 'once' && !minifierErrors.includes(url))) {
30+
consola.error(`HTML minification failed for ${url}.` + (options.logHtml ? ` Failed HTML:\n ${result.html}` : ''))
31+
}
32+
33+
// keep track of url's with minifier errors when needed
34+
if (!options.keepTrying || options.log === 'once') {
35+
minifierErrors.push(url)
36+
}
37+
}
38+
}
39+
})
40+
})
41+
}

package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@nuxtjs/html-minifier",
3+
"version": "0.1.0",
4+
"description": "",
5+
"license": "MIT",
6+
"contributors": [
7+
"pimlie <[email protected]>"
8+
],
9+
"main": "lib/module.js",
10+
"repository": "https://github.com/nuxt-community/html-minifier-module",
11+
"publishConfig": {
12+
"access": "public"
13+
},
14+
"scripts": {
15+
"dev": "test/fixture",
16+
"lint": "eslint lib test",
17+
"test": "jest test",
18+
"release": "yarn lint && yarn test && standard-version"
19+
},
20+
"files": [
21+
"lib"
22+
],
23+
"dependencies": {
24+
"consola": "^2.3.2",
25+
"figures": "^2.0.0",
26+
"html-minifier": "^3.5.21"
27+
},
28+
"devDependencies": {
29+
"@nuxtjs/eslint-config": "^0.0.1",
30+
"babel-eslint": "^10.0.1",
31+
"codecov": "3.1.0",
32+
"eslint": "^5.10.0",
33+
"eslint-config-standard": "^12.0.0",
34+
"eslint-loader": "^2.1.1",
35+
"eslint-plugin-import": "^2.14.0",
36+
"eslint-plugin-jest": "^22.1.2",
37+
"eslint-plugin-node": "^8.0.0",
38+
"eslint-plugin-promise": "^4.0.1",
39+
"eslint-plugin-standard": "^4.0.0",
40+
"eslint-plugin-vue": "^5.0.0",
41+
"get-port": "^4.1.0",
42+
"jest": "23.6.0",
43+
"nuxt-edge": "^2.4.0-25794725.9eab5589",
44+
"request-promise-native": "^1.0.5",
45+
"standard-version": "4.4.0"
46+
}
47+
}

test/fixture/nuxt.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { resolve } = require('path')
2+
3+
module.exports = {
4+
rootDir: resolve(__dirname, '../..'),
5+
buildDir: resolve(__dirname, '.nuxt'),
6+
srcDir: __dirname,
7+
dev: false,
8+
modules: [
9+
['@@', {
10+
logHtml: true
11+
}]
12+
]
13+
}

0 commit comments

Comments
 (0)