From ffbe155ed26f5f53375e9be0d9f02a7d8bdee35f Mon Sep 17 00:00:00 2001 From: Bruno Dutra Date: Sat, 17 Mar 2018 20:51:35 +0100 Subject: [PATCH] fix: remove unused variables and unnecessary branching from loader --- src/compat.js | 3 +++ src/loader.js | 18 ++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/compat.js b/src/compat.js index 496ca2e..1ca8eb4 100644 --- a/src/compat.js +++ b/src/compat.js @@ -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; diff --git a/src/loader.js b/src/loader.js index e6af5b7..bd2ccfd 100644 --- a/src/loader.js +++ b/src/loader.js @@ -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'))); });