Skip to content

Commit

Permalink
fix: perf (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 11, 2020
1 parent 9e3a780 commit d69f259
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 62 deletions.
38 changes: 15 additions & 23 deletions src/index.js
Expand Up @@ -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, {
Expand All @@ -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 =
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions 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'
Expand All @@ -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;
Expand Down
57 changes: 20 additions & 37 deletions src/plugins/source-plugin.js
Expand Up @@ -131,7 +131,7 @@ function parseSource(source) {
}

export default (options) =>
function process(html, result) {
function process(html) {
let attributeList;
let maybeUrlFilter;
let root;
Expand Down Expand Up @@ -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 };
};
Expand All @@ -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 };
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -370,10 +356,7 @@ export default (options) =>
this.attributesMeta = {};
},
onerror(error) {
result.messages.push({
type: 'error',
value: error,
});
options.errors.push(error);
},
},
{
Expand Down

0 comments on commit d69f259

Please sign in to comment.