diff --git a/src/index.js b/src/index.js index aec23fe9..ce749764 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,7 @@ import { import schema from './options.json'; -export default async function htmlLoader(content) { +export default async function loader(content) { const options = getOptions(this); validateOptions(schema, options, { @@ -27,12 +27,23 @@ export default async function htmlLoader(content) { } const plugins = []; + const errors = []; + const imports = []; + const replacements = []; const attributes = typeof options.attributes === 'undefined' ? true : options.attributes; if (attributes) { - plugins.push(sourcePlugin({ attributes, resourcePath: this.resourcePath })); + plugins.push( + sourcePlugin({ + attributes, + resourcePath: this.resourcePath, + imports, + errors, + replacements, + }) + ); } const minimize = @@ -41,29 +52,10 @@ export default async function htmlLoader(content) { : options.minimize; if (minimize) { - plugins.push(minimizerPlugin({ minimize })); + plugins.push(minimizerPlugin({ minimize, errors })); } - const { html, messages } = pluginRunner(plugins).process(content); - - const errors = []; - const imports = []; - const replacements = []; - - for (const message of messages) { - // eslint-disable-next-line default-case - switch (message.type) { - case 'error': - errors.push(message.value); - break; - case 'import': - imports.push(message.value); - break; - case 'replacement': - replacements.push(message.value); - break; - } - } + const { html } = pluginRunner(plugins).process(content); for (const error of errors) { this.emitError(error instanceof Error ? error : new Error(error)); diff --git a/src/plugins/minimizer-plugin.js b/src/plugins/minimizer-plugin.js index 78098708..6ac08ba9 100644 --- a/src/plugins/minimizer-plugin.js +++ b/src/plugins/minimizer-plugin.js @@ -1,7 +1,7 @@ import { minify } from 'html-minifier-terser'; export default (options) => - function process(html, result) { + function process(html) { const minimizeOptions = typeof options.minimize === 'boolean' || typeof options.minimize === 'undefined' @@ -23,7 +23,7 @@ export default (options) => // eslint-disable-next-line no-param-reassign html = minify(html, minimizeOptions); } catch (error) { - result.messages.push({ type: 'error', value: error }); + options.errors.push(error); } return html; diff --git a/src/plugins/source-plugin.js b/src/plugins/source-plugin.js index 30be9728..60e0ed69 100644 --- a/src/plugins/source-plugin.js +++ b/src/plugins/source-plugin.js @@ -131,7 +131,7 @@ function parseSource(source) { } export default (options) => - function process(html, result) { + function process(html) { let attributeList; let maybeUrlFilter; let root; @@ -180,14 +180,7 @@ export default (options) => name = `___HTML_LOADER_IMPORT_${imports.size}___`; imports.set(key, name); - result.messages.push({ - type: 'import', - value: { - type: 'source', - source: key, - importName: name, - }, - }); + options.imports.push({ importName: name, source: key }); return { key, name }; }; @@ -204,15 +197,11 @@ export default (options) => name = `___HTML_LOADER_REPLACEMENT_${replacements.size}___`; replacements.set(key, name); - result.messages.push({ - type: 'replacement', - value: { - type: 'source', - hash, - importName: importItem.name, - replacementName: name, - unquoted, - }, + options.replacements.push({ + replacementName: name, + importName: importItem.name, + hash, + unquoted, }); return { key, name }; @@ -257,15 +246,14 @@ export default (options) => try { source = parseSrc(value); } catch (error) { - result.messages.push({ - type: 'error', - value: new HtmlSourceError( + options.errors.push( + new HtmlSourceError( `Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`, parser.startIndex, parser.endIndex, html - ), - }); + ) + ); return; } @@ -294,15 +282,14 @@ export default (options) => try { sourceSet = parseSrcset(value); } catch (error) { - result.messages.push({ - type: 'error', - value: new HtmlSourceError( + options.errors.push( + new HtmlSourceError( `Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`, parser.startIndex, parser.endIndex, html - ), - }); + ) + ); return; } @@ -335,15 +322,14 @@ export default (options) => try { source = parseSrc(value); } catch (error) { - result.messages.push({ - type: 'error', - value: new HtmlSourceError( + options.errors.push( + new HtmlSourceError( `Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`, parser.startIndex, parser.endIndex, html - ), - }); + ) + ); return; } @@ -370,10 +356,7 @@ export default (options) => this.attributesMeta = {}; }, onerror(error) { - result.messages.push({ - type: 'error', - value: error, - }); + options.errors.push(error); }, }, {