Skip to content

Commit c0c1937

Browse files
committed
first commit
0 parents  commit c0c1937

File tree

7 files changed

+292
-0
lines changed

7 files changed

+292
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# editorconfig.org
2+
# config based on Bootstrap's: https://github.com/twbs/bootstrap/blob/main/.editorconfig
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
indent_size = 2
10+
indent_style = space
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true

.gitignore

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Modified from: https://github.com/toptal/gitignore/blob/master/templates/Node.gitignore
2+
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
8+
# Diagnostic reports (https://nodejs.org/api/report.html)
9+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
*.pid.lock
16+
17+
# node-waf configuration
18+
.lock-wscript
19+
20+
# Compiled binary addons (https://nodejs.org/api/addons.html)
21+
build/Release
22+
23+
# Dependency directories
24+
node_modules/
25+
26+
# TypeScript v1 declaration files
27+
typings/
28+
29+
# TypeScript cache
30+
*.tsbuildinfo
31+
32+
# Optional npm cache directory
33+
.npm
34+
35+
# Optional eslint cache
36+
.eslintcache
37+
38+
# Optional stylelint cache
39+
.stylelintcache
40+
41+
# Optional REPL history
42+
.node_repl_history
43+
44+
# Output of 'npm pack'
45+
*.tgz
46+
47+
# dotenv environment variables file
48+
.env
49+
.env.test
50+
.env*.local
51+
52+
# npm dependency tree files
53+
package-lock.json
54+
55+
# stats file generated by webpack-bundle-analyzer
56+
stats.json
57+
58+
# bundle output directories
59+
dist/
60+
build/
61+
62+
# jest folder of detailed test coverage
63+
coverage/

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) 2021 Matthew Waldron
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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<div align="center">
2+
<a href="https://www.w3.org/html/logo/downloads/HTML5_1Color_Black.svg"><img width="200" height="200" src="https://www.w3.org/html/logo/downloads/HTML5_1Color_Black.svg"></a>
3+
<a href="https://webpack.js.org/assets/icon-square-big.svg"><img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg"></a>
4+
</div>
5+
6+
# html-validate-webpack-plugin
7+
8+
> An [html-validate.org](https://html-validate.org/) webpack plugin
9+
10+
## About plugin
11+
12+
This plugin is a simple wrapper around the [html-validate.org](https://html-validate.org/) cli.
13+
14+
Validate your html automatically after each webpack compilation; similarly to how you would use eslint and stylelint.
15+
16+
## Install
17+
18+
```bash
19+
npm install html-validate-webpack-plugin --save-dev
20+
```
21+
22+
**Note**: Install `html-validate` if you haven't already.
23+
24+
```bash
25+
npm install html-validate --save-dev
26+
```
27+
28+
Or globally:
29+
30+
```bash
31+
npm install html-validate -g
32+
```
33+
34+
**Note**: Make sure you have created a `.htmlvalidate.json` file with your configurations. [See this page for instructions](https://html-validate.org/usage/index.html).
35+
36+
## Usage
37+
38+
In your webpack configuration (development builds):
39+
40+
```js
41+
const HtmlValidatePlugin = require("html-validate-webpack-plugin");
42+
43+
module.exports = {
44+
// ...
45+
plugins: [new HtmlValidatePlugin()],
46+
// ...
47+
};
48+
```
49+
50+
## Options
51+
52+
You can pass [html-validate.org](https://html-validate.org/) cli options.
53+
54+
### `path`
55+
56+
- Type: `String`
57+
- Default: `src/**/*`
58+
59+
Specifies the directories/files for html-validate to search.
60+
61+
Examples:
62+
63+
- A single file: `src/**/index`
64+
65+
### `extensions`
66+
67+
- Type: `array`
68+
- Default: `['html']`
69+
70+
Specifies the file extensions to use when searching for files in directories.
71+
72+
Examples:
73+
74+
- Multiple extensions: `['html', 'ejs']`
75+
76+
### `config`
77+
78+
- Type: `String`
79+
- Default: `.htmlvalidate.json`
80+
81+
Specify a different configuration file.
82+
83+
Examples:
84+
85+
- Your custom configuration: `'myconfig'` (omit the `.json` extension)
86+
87+
### `global`
88+
89+
- Type: `boolean`
90+
- Default: `false`
91+
92+
Specify the run context of html-validate. If you installed html-validate globally, set the value to `true`.
93+
94+
## Contributing
95+
96+
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
97+
98+
## License
99+
100+
MIT

index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const spawn = require("child_process").spawn;
2+
3+
class HTMLValidatePlugin {
4+
constructor(options) {
5+
({
6+
path: this.path,
7+
extensions: this.extensions,
8+
config: this.config,
9+
global: this.global,
10+
} = options);
11+
}
12+
13+
convertExtensionArrayToRegex() {
14+
// replace array as curly braced string for replacing commas and spaces
15+
let processedExtension = `"{${this.extensions}}"`
16+
.replace(/\'/g, "")
17+
.replace(/\ /g, "");
18+
// strip out curly braces if there was only one index provided in extensions array
19+
return processedExtension.includes(",")
20+
? processedExtension
21+
: processedExtension.replace(/\{/g, "").replace(/\}/g, "");
22+
}
23+
24+
runCliBasedOnScope(userParams, spawnParams) {
25+
/*
26+
arguments are in an array and shell option is 'false' by default; this is better for security
27+
https://stackoverflow.com/a/50424976d
28+
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
29+
*/
30+
return this.global
31+
? spawn("html-validate", [`${userParams}`], spawnParams)
32+
: spawn(
33+
"node",
34+
["node_modules/.bin/html-validate", `${userParams}`],
35+
spawnParams
36+
);
37+
}
38+
39+
apply(compiler) {
40+
// initiate script when webpack compilation is completed
41+
compiler.hooks.done.tap("HTMLValidatePlugin", () => {
42+
const path = `${this.path || "src/**/*"}`;
43+
const extension = `${
44+
this.extensions ? this.convertExtensionArrayToRegex() : "html"
45+
}`;
46+
const config = `${
47+
this.config ? "--config " + this.config + ".json" : false
48+
}`;
49+
50+
// set up cli payload
51+
const userParams = `${path}.${extension} ${config}`;
52+
const spawnParams = {
53+
shell: true,
54+
/*inherit color output */ stdio: "inherit",
55+
};
56+
57+
this.runCliBasedOnScope(userParams, spawnParams);
58+
});
59+
}
60+
}
61+
62+
module.exports = HTMLValidatePlugin;

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "html-validate-webpack-plugin",
3+
"version": "1.0.0",
4+
"description": "An html-validate.org webpack plugin",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/waldronmatt/html-validate-webpack-plugin.git"
12+
},
13+
"keywords": [
14+
"html-validate",
15+
"webpack",
16+
"plugin",
17+
"validate",
18+
"html"
19+
],
20+
"author": "Matthew Waldron",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/waldronmatt/html-validate-webpack-plugin/issues"
24+
},
25+
"homepage": "https://github.com/waldronmatt/html-validate-webpack-plugin#readme",
26+
"devDependencies": {
27+
"prettier": "^2.2.1"
28+
}
29+
}

prettier.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
printWidth: 100,
3+
singleQuote: true,
4+
trailingComma: "es5",
5+
};

0 commit comments

Comments
 (0)