Skip to content

Commit 9356525

Browse files
committed
Build task to make umd and esm dist files
1 parent 3f5dc50 commit 9356525

File tree

9 files changed

+272
-128
lines changed

9 files changed

+272
-128
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ _A package for testing HTML files or URLs against the W3C validator_
88
[![Vulnerabilities](https://snyk.io/test/github/center-key/w3c-html-validator/badge.svg)](https://snyk.io/test/github/center-key/w3c-html-validator)
99
[![Build](https://github.com/center-key/w3c-html-validator/workflows/build/badge.svg)](https://github.com/center-key/w3c-html-validator/actions?query=workflow%3Abuild)
1010

11+
1112
## Setup
13+
14+
### Install
1215
Install package for node:
1316
```shell
14-
$ npm install w3c-html-validator
17+
$ npm install --save-dev w3c-html-validator
1518
```
19+
20+
## Import
1621
Import into your application:
1722
```javascript
18-
import { htmlValidator } from 'w3c-html-validator';
23+
import { w3cHtmlValidator } from 'w3c-html-validator';
1924
```

dist/w3c-html-validator.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! W3C HTML Validator v0.5.0 ~ github.com/center-key/w3c-html-validator ~ MIT License
2+
3+
export declare type ValidatorOptions = {
4+
output?: string;
5+
doctype?: string;
6+
charset?: string;
7+
proxy?: string;
8+
callback?: (response: unknown, info?: unknown) => void;
9+
file: string;
10+
input: string;
11+
};
12+
declare const w3cHtmlValidator: {
13+
version: string;
14+
w3cCheckUrl: string;
15+
setW3cCheckUrl(newW3cCheckUrl: string): void;
16+
validate(options: ValidatorOptions): void;
17+
};
18+
export { w3cHtmlValidator };

dist/w3c-html-validator.js

Lines changed: 59 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,59 @@
1-
var request = require('superagent');
2-
var proxyRequest = require('superagent-proxy')(request);
3-
4-
var fs = require('fs');
5-
6-
var w3cCheckUrl = 'http://validator.w3.org/nu/';
7-
var defaultOutput = 'json';
8-
var defaultDoctype = null;
9-
var defaultCharset = null;
10-
var defaultProxy = null;
11-
12-
var defaultCallback = function (res) {
13-
console.log(res);
14-
}
15-
16-
function setW3cCheckUrl(newW3cCheckUrl) {
17-
w3cCheckUrl = newW3cCheckUrl;
18-
}
19-
20-
function validate(options) {
21-
var output = options.output || defaultOutput;
22-
var callback = options.callback || defaultCallback;
23-
var doctype = options.doctype || defaultDoctype;
24-
var charset = options.charset || defaultCharset;
25-
var file = options.file;
26-
var input = options.input;
27-
var context = '';
28-
29-
var type;
30-
if(typeof input !== 'undefined'){
31-
type = 'string';
32-
context = input;
33-
} else if(typeof file !== 'undefined' && (file.substr(0,5) === 'http:' || file.substr(0, 6) === 'https:')){
34-
type = 'remote';
35-
context = file;
36-
} else if(typeof file !== 'undefined'){
37-
type = 'local';
38-
context = file;
39-
} else {
40-
return false;
41-
}
42-
43-
var req = getRequest(type !== 'remote', options);
44-
45-
if(type === 'remote') {
46-
req.query({ out: output });
47-
req.query({ doc: file });
48-
} else {
49-
req.query({ out: output });
50-
req.send((type === 'local') ? fs.readFileSync(file, 'utf8') : input + "");
51-
};
52-
req.end(function(error, res){
53-
if(error) {
54-
callback(error);
55-
} else if(output === 'json'){
56-
res.body.context = context;
57-
callback(null, res.body);
58-
} else {
59-
callback(null, res.text);
60-
}
61-
});
62-
}
63-
64-
var getRequest = function(isLocal, options) {
65-
var req = isLocal ? proxyRequest.post(w3cCheckUrl) : proxyRequest.get(w3cCheckUrl);
66-
67-
var proxy = options.proxy || defaultProxy;
68-
if (proxy !== null) {
69-
req.proxy(proxy);
70-
}
71-
72-
req.set('User-Agent', 'w3cjs - npm module');
73-
req.set('Content-Type', 'text/html; encoding=utf-8');
74-
75-
return req;
76-
}
77-
78-
var w3cjs = {
79-
validate: validate,
80-
setW3cCheckUrl: setW3cCheckUrl
81-
}
82-
if (typeof exports !== 'undefined') {
83-
if (typeof module !== 'undefined' && module.exports) {
84-
exports = module.exports = w3cjs;
85-
}
86-
exports.w3cjs = w3cjs
87-
} else {
88-
root['w3cjs'] = w3cjs;
89-
}
1+
//! W3C HTML Validator v0.5.0 ~ github.com/center-key/w3c-html-validator ~ MIT License
2+
3+
import { readFileSync } from 'fs';
4+
import * as request from 'superagent';
5+
import withProxy from 'superagent-proxy';
6+
withProxy(request);
7+
const w3cHtmlValidator = {
8+
version: '0.5.0',
9+
w3cCheckUrl: 'https://validator.w3.org/nu/',
10+
setW3cCheckUrl(newW3cCheckUrl) {
11+
w3cHtmlValidator.w3cCheckUrl = newW3cCheckUrl;
12+
},
13+
validate(options) {
14+
const defaults = {
15+
output: 'json',
16+
doctype: null,
17+
charset: null,
18+
proxy: null,
19+
callback: (response) => console.log(response),
20+
};
21+
const settings = { ...defaults, ...options };
22+
const checkUrl = w3cHtmlValidator.w3cCheckUrl;
23+
const getRequest = (isLocal) => {
24+
const req = isLocal ? request.default.post(checkUrl) : request.default.get(checkUrl);
25+
if (settings.proxy)
26+
req.proxy(settings.proxy);
27+
req.set('User-Agent', 'w3c-html-validator');
28+
req.set('Content-Type', 'text/html; encoding=utf-8');
29+
return req;
30+
};
31+
if (!settings.input && !settings.file)
32+
throw Error('No "input" or "file" specified.');
33+
const remoteMode = /^http[s]?:/.test(settings.file);
34+
const type = settings.input ? 'string' : remoteMode ? 'remote' : 'local';
35+
const context = settings.input || settings.file;
36+
const req = getRequest(type !== 'remote');
37+
if (type === 'remote') {
38+
req.query({ out: settings.output });
39+
req.query({ doc: settings.file });
40+
}
41+
else {
42+
req.query({ out: settings.output });
43+
req.send((type === 'local') ? readFileSync(settings.file, 'utf8') : settings.input + '');
44+
}
45+
req.end(function (error, res) {
46+
if (error) {
47+
settings.callback(error);
48+
}
49+
else if (settings.output === 'json') {
50+
res.body.context = context;
51+
settings.callback(null, res.body);
52+
}
53+
else {
54+
settings.callback(null, res.text);
55+
}
56+
});
57+
},
58+
};
59+
export { w3cHtmlValidator };

dist/w3c-html-validator.umd.cjs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//! W3C HTML Validator v0.5.0 ~ github.com/center-key/w3c-html-validator ~ MIT License
2+
3+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4+
if (k2 === undefined) k2 = k;
5+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
6+
}) : (function(o, m, k, k2) {
7+
if (k2 === undefined) k2 = k;
8+
o[k2] = m[k];
9+
}));
10+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
11+
Object.defineProperty(o, "default", { enumerable: true, value: v });
12+
}) : function(o, v) {
13+
o["default"] = v;
14+
});
15+
var __importStar = (this && this.__importStar) || function (mod) {
16+
if (mod && mod.__esModule) return mod;
17+
var result = {};
18+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
19+
__setModuleDefault(result, mod);
20+
return result;
21+
};
22+
var __importDefault = (this && this.__importDefault) || function (mod) {
23+
return (mod && mod.__esModule) ? mod : { "default": mod };
24+
};
25+
(function (factory) {
26+
if (typeof module === "object" && typeof module.exports === "object") {
27+
var v = factory(require, exports);
28+
if (v !== undefined) module.exports = v;
29+
}
30+
else if (typeof define === "function" && define.amd) {
31+
define(["require", "exports", "fs", "superagent", "superagent-proxy"], factory);
32+
}
33+
})(function (require, exports) {
34+
"use strict";
35+
Object.defineProperty(exports, "__esModule", { value: true });
36+
exports.w3cHtmlValidator = void 0;
37+
const fs_1 = require("fs");
38+
const request = __importStar(require("superagent"));
39+
const superagent_proxy_1 = __importDefault(require("superagent-proxy"));
40+
superagent_proxy_1.default(request);
41+
const w3cHtmlValidator = {
42+
version: '0.5.0',
43+
w3cCheckUrl: 'https://validator.w3.org/nu/',
44+
setW3cCheckUrl(newW3cCheckUrl) {
45+
w3cHtmlValidator.w3cCheckUrl = newW3cCheckUrl;
46+
},
47+
validate(options) {
48+
const defaults = {
49+
output: 'json',
50+
doctype: null,
51+
charset: null,
52+
proxy: null,
53+
callback: (response) => console.log(response),
54+
};
55+
const settings = { ...defaults, ...options };
56+
const checkUrl = w3cHtmlValidator.w3cCheckUrl;
57+
const getRequest = (isLocal) => {
58+
const req = isLocal ? request.default.post(checkUrl) : request.default.get(checkUrl);
59+
if (settings.proxy)
60+
req.proxy(settings.proxy);
61+
req.set('User-Agent', 'w3c-html-validator');
62+
req.set('Content-Type', 'text/html; encoding=utf-8');
63+
return req;
64+
};
65+
if (!settings.input && !settings.file)
66+
throw Error('No "input" or "file" specified.');
67+
const remoteMode = /^http[s]?:/.test(settings.file);
68+
const type = settings.input ? 'string' : remoteMode ? 'remote' : 'local';
69+
const context = settings.input || settings.file;
70+
const req = getRequest(type !== 'remote');
71+
if (type === 'remote') {
72+
req.query({ out: settings.output });
73+
req.query({ doc: settings.file });
74+
}
75+
else {
76+
req.query({ out: settings.output });
77+
req.send((type === 'local') ? fs_1.readFileSync(settings.file, 'utf8') : settings.input + '');
78+
}
79+
req.end(function (error, res) {
80+
if (error) {
81+
settings.callback(error);
82+
}
83+
else if (settings.output === 'json') {
84+
res.body.context = context;
85+
settings.callback(null, res.body);
86+
}
87+
else {
88+
settings.callback(null, res.text);
89+
}
90+
});
91+
},
92+
};
93+
exports.w3cHtmlValidator = w3cHtmlValidator;
94+
});

example/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
var w3cjs = require('..');
1+
import { w3cHtmlValidator } from '../dist/w3c-html-validator.js';
22

3-
var results = w3cjs.validate({
4-
file: __dirname + '/demo.html', // file can either be a local file or a remote file
5-
// file: 'http://html5boilerplate.com/',
6-
// input: '<html>...</html>',
7-
// input: myBuffer,
8-
output: 'json', // Defaults to 'json', other option includes html
9-
// proxy: 'http://proxy:8080', // Default to null
10-
callback: function (error, res) {
11-
console.log(error || res);
12-
// depending on the output type, res will either be a json object or a html string
13-
}
14-
});
3+
const results = w3cHtmlValidator.validate({
4+
file: 'example/demo.html', // file can either be a local file or a remote file
5+
// file: 'https://pretty-print-json.js.org/',
6+
// input: '<html>...</html>',
7+
// input: myBuffer,
8+
output: 'json', // Defaults to 'json', other option includes html
9+
// proxy: 'http://proxy:8080', // Default to null
10+
callback: function (error, res) {
11+
console.log(error || res);
12+
// depending on the output type, res will either be a json object or a html string
13+
}
14+
});

gulpfile.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// W3C HTML Validator
2+
// gulp configuration and tasks
3+
4+
// Imports
5+
import gulp from 'gulp';
6+
import header from 'gulp-header';
7+
import mergeStream from 'merge-stream';
8+
import rename from 'gulp-rename';
9+
import replace from 'gulp-replace';
10+
import size from 'gulp-size';
11+
import { readFileSync } from 'fs';
12+
13+
// Setup
14+
const pkg = JSON.parse(readFileSync('./package.json'));
15+
const home = pkg.repository.replace('github:', 'github.com/');
16+
const banner = '//! W3C HTML Validator v' + pkg.version + ' ~ ' + home + ' ~ MIT License\n\n';
17+
const setPkgVersion = () => replace('[VERSION]', pkg.version);
18+
19+
// Tasks
20+
const task = {
21+
22+
makeDistribution() {
23+
const buildDts = () =>
24+
gulp.src('build/w3c-html-validator.d.ts')
25+
.pipe(header(banner))
26+
.pipe(size({ showFiles: true }))
27+
.pipe(gulp.dest('dist'));
28+
const buildJs = () =>
29+
gulp.src('build/w3c-html-validator.js')
30+
.pipe(header(banner))
31+
.pipe(setPkgVersion())
32+
.pipe(size({ showFiles: true }))
33+
.pipe(gulp.dest('dist'));
34+
const buildUmd = () =>
35+
gulp.src('build/umd/w3c-html-validator.js')
36+
.pipe(header(banner))
37+
.pipe(setPkgVersion())
38+
.pipe(rename({ extname: '.umd.cjs' }))
39+
.pipe(size({ showFiles: true }))
40+
.pipe(gulp.dest('dist'));
41+
return mergeStream(buildDts(), buildJs(), buildUmd());
42+
},
43+
44+
};
45+
46+
// Gulp
47+
gulp.task('make-dist', task.makeDistribution);

0 commit comments

Comments
 (0)