Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] ES6-ify #427

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

'use strict';

const os = require('os');
const path = require('path');
const chalk = require('chalk');
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const path = require('path');
const fs = require('fs-extra');
const assign = require('lodash/assign');
Expand Down Expand Up @@ -82,7 +83,7 @@ function prepareOptions(opts) {
* @accepts src, base, width, height, dimensions, dest
* @return {Promise}|undefined
*/
exports.generate = function (opts, cb) {
exports.generate = (opts, cb) => {
opts = prepareOptions(opts);

// Generate critical css
Expand Down Expand Up @@ -134,14 +135,14 @@ exports.generate = function (opts, cb) {
/**
* Deprecated has been removed
*/
exports.generateInline = function () {
exports.generateInline = () => {
throw new Error('"generateInline" has been removed. Use "generate" with the inline option instead. https://goo.gl/7VbE4b');
};

/**
* Deprecated has been removed
*/
exports.inline = function () {
exports.inline = () => {
throw new Error('"inline" has been removed. Consider using "inline-critical" instead. https://goo.gl/MmTrUZ');
};

Expand All @@ -151,7 +152,7 @@ exports.inline = function () {
* @param {object} opts
* @returns {*}
*/
exports.stream = function (opts) {
exports.stream = opts => {
// Return stream
return through2.obj(function (file, enc, cb) {
if (file.isNull()) {
Expand Down
17 changes: 9 additions & 8 deletions lib/core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const os = require('os');
const path = require('path');
const url = require('url');
Expand Down Expand Up @@ -52,7 +53,7 @@ function combineCss(cssArray) {
* @returns {function}
*/
function appendStylesheets(opts) {
return function (htmlfile) {
return htmlfile => {
// Consider opts.css and map to array if it isn't one
if (opts.css) {
const css = Array.isArray(opts.css) ? opts.css : [opts.css];
Expand Down Expand Up @@ -88,7 +89,7 @@ function appendStylesheets(opts) {
* @returns {function}
*/
function inlineImages(opts) {
return function (vinyl) {
return vinyl => {
if (opts.inlineImages) {
const assetPaths = opts.assetPaths || [];

Expand All @@ -100,7 +101,7 @@ function inlineImages(opts) {
if (file.isExternal(opts.src)) {
// eslint-disable-next-line node/no-deprecated-api
const urlObj = url.parse(opts.src);
const domain = urlObj.protocol + '//' + urlObj.host;
const domain = `${urlObj.protocol}//${urlObj.host}`;
assetPaths.push(domain, domain + path.dirname(urlObj.pathname));
}

Expand Down Expand Up @@ -132,7 +133,7 @@ function inlineImages(opts) {
* @returns {function}
*/
function vinylize(opts) {
return function (filepath) {
return filepath => {
if (filepath._isVinyl) {
return filepath;
}
Expand All @@ -151,7 +152,7 @@ function vinylize(opts) {
* @returns {function}
*/
function processStylesheets(opts) {
return function (htmlfile) {
return htmlfile => {
debug('processStylesheets', htmlfile.stylesheets);
return Bluebird.map(htmlfile.stylesheets, vinylize(opts))
.map(inlineImages(opts))
Expand All @@ -174,8 +175,8 @@ function processStylesheets(opts) {
* @returns {function}
*/
function computeCritical(dimensions, opts) {
return function (htmlfile) {
debug('Processing: ' + htmlfile.path + ' [' + dimensions.width + 'x' + dimensions.height + ']');
return htmlfile => {
debug(`Processing: ${htmlfile.path} [${dimensions.width}x${dimensions.height}]`);
const penthouseOpts = assign({}, opts.penthouse, {
url: file.getPenthouseUrl(opts, htmlfile),
cssString: htmlfile.cssString,
Expand All @@ -185,7 +186,7 @@ function computeCritical(dimensions, opts) {
});

if (opts.user && opts.pass) {
penthouseOpts.customPageHeaders = {Authorization: 'Basic ' + file.token(opts.user, opts.pass)};
penthouseOpts.customPageHeaders = {Authorization: `Basic ${file.token(opts.user, opts.pass)}`};
}

return penthouse(penthouseOpts);
Expand Down
11 changes: 6 additions & 5 deletions lib/file-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const os = require('os');
const url = require('url');
const path = require('path');
Expand Down Expand Up @@ -41,7 +42,7 @@ function replaceAssetPaths(html, opts) {
/**
* The resulting function should get passed an vinyl object with the css file
*/
return function (stylesheet) {
return stylesheet => {
// Normalize relative paths
const css = stylesheet.contents.toString().replace(/url\(['"]?([^'"\\)]+)['"]?\)/g, (match, assetPath) => {
// Skip absolute paths, urls and data-uris
Expand Down Expand Up @@ -84,8 +85,8 @@ function getPenthouseUrl(opts, file) {
filepath = path.resolve(file.history[0]);
}

debug('Fetching local html:', 'file://' + filepath);
return 'file://' + filepath;
debug('Fetching local html:', `file://${filepath}`);
return `file://${filepath}`;
}

/**
Expand Down Expand Up @@ -141,7 +142,7 @@ function requestAsync(uri, secure = true, opts = {}) {
debug(`Fetching resource: ${resourceUrl}`);
const options = {rejectUnauthorized: false, headers: {}};
if (user && pass) {
options.headers.Authorization = 'Basic ' + token(user, pass);
options.headers.Authorization = `Basic ${token(user, pass)}`;
}

if (userAgent) {
Expand Down Expand Up @@ -211,7 +212,7 @@ function assertLocal(filePath, opts = {}) {
* @returns {function}
*/
function resourcePath(htmlfile, opts) {
return function (filepath) {
return filepath => {
if (isExternal(filepath)) {
debug('resourcePath - remote', filepath);
return filepath;
Expand Down
3 changes: 2 additions & 1 deletion lib/gc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const fs = require('fs-extra');
const exitHook = require('async-exit-hook');
const _ = require('lodash');
Expand Down Expand Up @@ -28,6 +29,6 @@ exitHook(done => cleanup().then(() => done()));
process.on('cleanup', cleanup);

module.exports.cleanup = cleanup;
module.exports.addFile = function (file) {
module.exports.addFile = file => {
files.push(file);
};
3 changes: 2 additions & 1 deletion lib/vinyl-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

'use strict';

const url = require('url');
const File = require('vinyl');

Expand All @@ -25,7 +26,7 @@ Object.defineProperty(File.prototype, 'remotePath', {

// eslint-disable-next-line node/no-deprecated-api
const urlObj = url.parse(remotePath);
remotePath = urlObj.protocol + '//' + urlObj.host + urlObj.pathname;
remotePath = `${urlObj.protocol}//${urlObj.host}${urlObj.pathname}`;

// Record history only when path changed
if (remotePath && remotePath !== this.remotePath) {
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
"space": 4,
"rules": {
"valid-jsdoc": "off"
}
},
"overrides": [
{
"files": "test/*.js",
"envs": [
"mocha"
]
}
]
}
}
10 changes: 5 additions & 5 deletions test/00-lib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env node, mocha */
'use strict';

const path = require('path');
const {assert} = require('chai');
const File = require('vinyl');
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('Lib', () => {
function mock(p) {
return new File({
path: 'fixtures/a/b/file.css',
contents: Buffer.from('url(' + p + ')')
contents: Buffer.from(`url(${p})`)
});
}

Expand All @@ -60,7 +60,7 @@ describe('Lib', () => {
function mock(p) {
return new File({
path: 'fixtures/a/b/file.css',
contents: Buffer.from('url(' + p + ')')
contents: Buffer.from(`url(${p})`)
});
}

Expand All @@ -82,7 +82,7 @@ describe('Lib', () => {
function mock(p) {
return new File({
path: 'fixtures/a/b/file.css',
contents: Buffer.from('url(' + p + ')')
contents: Buffer.from(`url(${p})`)
});
}

Expand All @@ -103,7 +103,7 @@ describe('Lib', () => {
function mock(p) {
return new File({
path: 'fixtures/a/file.css',
contents: Buffer.from('url(' + p + ')')
contents: Buffer.from(`url(${p})`)
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/01-module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env node, mocha */
'use strict';

const {assert} = require('chai');
const critical = require('..');

Expand Down
2 changes: 1 addition & 1 deletion test/02-generate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env node, mocha */
'use strict';

const path = require('path');
const http = require('http');
const fs = require('fs');
Expand Down
6 changes: 3 additions & 3 deletions test/03-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env node, mocha */
'use strict';

const fs = require('fs');
const path = require('path');
const http = require('http');
Expand Down Expand Up @@ -216,10 +216,10 @@ describe('CLI', () => {
});

mockery.registerMock('.', {
generate: function (opts) {
generate: opts => {
this.mockOpts = opts;
this.method = 'generate';
}.bind(this)
}
});
});

Expand Down
3 changes: 2 additions & 1 deletion test/04-streams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Unit tests for Critical.
*/
/* eslint-env node, mocha */

'use strict';

const path = require('path');
const fs = require('fs');
const {assert} = require('chai');
Expand Down
2 changes: 1 addition & 1 deletion test/helper/testhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function read(file) {
* @returns {Function}
*/
function assertCritical(target, expected, done, skipTarget) {
return function (err, output) {
return (err, output) => {
if (err) {
console.log(err);
done(err);
Expand Down