Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
fix: remove unused variables and unnecessary branching from loader
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocodutra committed Mar 17, 2018
1 parent da5f66d commit ffbe155
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ module.exports.tap = (tappable, hook, name, plugin) => (
? tappable.hooks[camelCase(hook)] && tappable.hooks[camelCase(hook)].tapAsync(name, plugin)
: tappable.plugin(hook, plugin)
);

/* istanbul ignore next */
module.exports.getContext = (loader) => (loader.options && loader.options.context) || loader.rootContext;
18 changes: 8 additions & 10 deletions src/loader.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
const favicons = require('favicons');
const msgpack = require('msgpack-lite');
const {parseQuery, interpolateName} = require('loader-utils');
const {getPublicPath} = require('./compat');
const {getPublicPath, getContext} = require('./compat');

module.exports = function (content) {
if (!this.emitFile) throw new Error('emitFile is required');
/* istanbul ignore next */
if (!this.async) throw new Error('async is required');

const {
regExp, prefix, options,
context = (this.options && this.options.context) || this.rootContext,
} = parseQuery(this.query);
const {prefix, options} = parseQuery(this.query);

const callback = this.async();
const context = getContext(this);
const publicPath = getPublicPath(this._compilation);
const path = interpolateName(this, prefix, {context, regExp, content});
const path = interpolateName(this, prefix, {context, content});

// Generate icons
favicons(content, options, (err, {images = [], files = [], html = []} = {}) => {
favicons(content, options, (err, result) => {
if (err) {
return callback(new Error(err));
}

const assets = [...images, ...files].map(({name, contents}) => ({name: path + name, contents}));
html = html.map((entry) => entry.replace(/(href=['"])/g, '$1' + publicPath + path)).sort().join('');
const assets = [...result.images, ...result.files].map(({name, contents}) => ({name: path + name, contents}));
const html = result.html.map((entry) => entry.replace(/(href=['"])/g, '$1' + publicPath + path)).sort().join('');

return callback(null, 'module.exports = ' + JSON.stringify(msgpack.encode({html, assets}).toString('base64')));
});
Expand Down

0 comments on commit ffbe155

Please sign in to comment.