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

fix: perf #300

Merged
merged 1 commit into from Aug 11, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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