mirror of
https://github.com/remy/inliner.git
synced 2024-09-11 17:25:30 +03:00
chore: merge branch 'feature/headers'
This commit is contained in:
commit
7c33ee7d23
@ -30,6 +30,7 @@ function options(args) {
|
||||
s: 'nosvg',
|
||||
o: 'videos',
|
||||
m: 'inlinemin',
|
||||
H: 'header',
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -7,24 +7,26 @@
|
||||
|
||||
Flags:
|
||||
|
||||
-n, --nocompress don't compress CSS or HTML - useful for debugging
|
||||
-i, --noimages don't encode images - keeps files size small, but more requests
|
||||
-o, --videos encode videos (and their poster image) - disabled by default
|
||||
-s, --nosvg don't compress SVG (through SVGO)
|
||||
-v, --verbose echo on STDERR the progress of inlining
|
||||
-V, --version output the version number
|
||||
-h, --help output usage information
|
||||
-m, --inlinemin inline minified files
|
||||
-n, --nocompress don't compress CSS or HTML - useful for debugging
|
||||
-i, --noimages don't encode images - keeps files size small, but more requests
|
||||
-o, --videos encode videos (and their poster image) - disabled by default
|
||||
-s, --nosvg don't compress SVG (through SVGO)
|
||||
-v, --verbose echo on STDERR the progress of inlining
|
||||
-V, --version output the version number
|
||||
-h, --help output usage information
|
||||
-m, --inlinemin inline minified files
|
||||
|
||||
Options:
|
||||
|
||||
-e, --encoding override encoding detection
|
||||
-H, --header LINE custom header pass to the server
|
||||
-e, --encoding ENC override encoding detection
|
||||
|
||||
Examples:
|
||||
|
||||
$ inliner -v https://twitter.com > twitter.html
|
||||
$ inliner -ni local-file.html > local-file.min.html
|
||||
$ inliner -e windows-1253 http://foofootos.gr > foofootos-utf8.html
|
||||
$ inliner -H User-Agent="Inliner Custom" https://httpbin.org/headers
|
||||
$ cat local-file.html | inliner
|
||||
|
||||
For more details see http://github.com/remy/inliner/
|
||||
|
@ -88,4 +88,4 @@ function compress(css) {
|
||||
// .replace(/\{ /g, '{')
|
||||
.replace(/; /g, ';')
|
||||
.replace(/\n+/g, '');
|
||||
}
|
||||
}
|
||||
|
@ -44,9 +44,10 @@ module.exports = function get(url, options) {
|
||||
var settings = assign({}, options, {
|
||||
encoding: null,
|
||||
followRedirect: true,
|
||||
headers: inliner.headers,
|
||||
});
|
||||
|
||||
debug('request %s', url);
|
||||
debug('request %s', url, settings);
|
||||
|
||||
cache[url] = new Promise(function promise(resolve, reject) {
|
||||
request(url, settings, function response(error, res, body) {
|
||||
|
24
lib/index.js
24
lib/index.js
@ -12,6 +12,7 @@ var findAssets = require('./find-assets');
|
||||
var iconv = require('iconv-lite');
|
||||
var charset = require('charset');
|
||||
var jschardet = require('jschardet');
|
||||
var querystring = require('querystring');
|
||||
|
||||
// note: these tasks (excluding HTML), match up to files in lib/tasks/*.js
|
||||
var tasks = {
|
||||
@ -65,6 +66,29 @@ function Inliner(source, options, callback) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
// this is an intentioal change. `.headers` is compatible with the request
|
||||
// module, but -H and --header is compatible (i.e. the same as) cURL
|
||||
if (options.header) {
|
||||
options.headers = options.header;
|
||||
delete options.header;
|
||||
}
|
||||
|
||||
if (options.headers && !Array.isArray(options.headers)) {
|
||||
options.headers = [options.headers];
|
||||
}
|
||||
|
||||
if (options.headers && typeof options.headers[0] === 'string') {
|
||||
// convert to an object of key/value pairs
|
||||
options.headers = options.headers.reduce(function (acc, curr) {
|
||||
var pair = querystring.parse(curr);
|
||||
var key = Object.keys(pair).shift();
|
||||
acc[key] = pair[key];
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
this.headers = options.headers || {};
|
||||
|
||||
this.callback = function wrapper(error, res) {
|
||||
// noop the callback once it's fired
|
||||
inliner.callback = function noop() {
|
||||
|
@ -6,13 +6,24 @@ var UglifyJS = require('uglify-js');
|
||||
function uglify(source) {
|
||||
this.emit('progress', 'compressing javascript');
|
||||
|
||||
if (source.trim() === '') {
|
||||
source = source.trim();
|
||||
|
||||
if (source === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
debug('uglifying %sbytes', source.length);
|
||||
|
||||
return UglifyJS.minify(source, {
|
||||
fromString: true,
|
||||
}).code;
|
||||
var result = '';
|
||||
|
||||
try {
|
||||
result = UglifyJS.minify(source, {
|
||||
fromString: true,
|
||||
}).code;
|
||||
} catch (e) {
|
||||
// failed to uglify, just return it plain
|
||||
result = source;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
3
test/fixtures/headers.opts.json
vendored
Normal file
3
test/fixtures/headers.opts.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"headers": "User-Agent=inliner"
|
||||
}
|
1
test/fixtures/headers.result.html
vendored
Normal file
1
test/fixtures/headers.result.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
<script>{ "user-agent": "inliner" }</script>
|
1
test/fixtures/headers.src.html
vendored
Normal file
1
test/fixtures/headers.src.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
<script src="https://httpbin.org/user-agent"></script>
|
Loading…
Reference in New Issue
Block a user