Skip to content

Commit d69f259

Browse files
authoredAug 11, 2020
fix: perf (#300)
1 parent 9e3a780 commit d69f259

File tree

3 files changed

+37
-62
lines changed

3 files changed

+37
-62
lines changed
 

‎src/index.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313

1414
import schema from './options.json';
1515

16-
export default async function htmlLoader(content) {
16+
export default async function loader(content) {
1717
const options = getOptions(this);
1818

1919
validateOptions(schema, options, {
@@ -27,12 +27,23 @@ export default async function htmlLoader(content) {
2727
}
2828

2929
const plugins = [];
30+
const errors = [];
31+
const imports = [];
32+
const replacements = [];
3033

3134
const attributes =
3235
typeof options.attributes === 'undefined' ? true : options.attributes;
3336

3437
if (attributes) {
35-
plugins.push(sourcePlugin({ attributes, resourcePath: this.resourcePath }));
38+
plugins.push(
39+
sourcePlugin({
40+
attributes,
41+
resourcePath: this.resourcePath,
42+
imports,
43+
errors,
44+
replacements,
45+
})
46+
);
3647
}
3748

3849
const minimize =
@@ -41,29 +52,10 @@ export default async function htmlLoader(content) {
4152
: options.minimize;
4253

4354
if (minimize) {
44-
plugins.push(minimizerPlugin({ minimize }));
55+
plugins.push(minimizerPlugin({ minimize, errors }));
4556
}
4657

47-
const { html, messages } = pluginRunner(plugins).process(content);
48-
49-
const errors = [];
50-
const imports = [];
51-
const replacements = [];
52-
53-
for (const message of messages) {
54-
// eslint-disable-next-line default-case
55-
switch (message.type) {
56-
case 'error':
57-
errors.push(message.value);
58-
break;
59-
case 'import':
60-
imports.push(message.value);
61-
break;
62-
case 'replacement':
63-
replacements.push(message.value);
64-
break;
65-
}
66-
}
58+
const { html } = pluginRunner(plugins).process(content);
6759

6860
for (const error of errors) {
6961
this.emitError(error instanceof Error ? error : new Error(error));

‎src/plugins/minimizer-plugin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { minify } from 'html-minifier-terser';
22

33
export default (options) =>
4-
function process(html, result) {
4+
function process(html) {
55
const minimizeOptions =
66
typeof options.minimize === 'boolean' ||
77
typeof options.minimize === 'undefined'
@@ -23,7 +23,7 @@ export default (options) =>
2323
// eslint-disable-next-line no-param-reassign
2424
html = minify(html, minimizeOptions);
2525
} catch (error) {
26-
result.messages.push({ type: 'error', value: error });
26+
options.errors.push(error);
2727
}
2828

2929
return html;

‎src/plugins/source-plugin.js

+20-37
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function parseSource(source) {
131131
}
132132

133133
export default (options) =>
134-
function process(html, result) {
134+
function process(html) {
135135
let attributeList;
136136
let maybeUrlFilter;
137137
let root;
@@ -180,14 +180,7 @@ export default (options) =>
180180
name = `___HTML_LOADER_IMPORT_${imports.size}___`;
181181
imports.set(key, name);
182182

183-
result.messages.push({
184-
type: 'import',
185-
value: {
186-
type: 'source',
187-
source: key,
188-
importName: name,
189-
},
190-
});
183+
options.imports.push({ importName: name, source: key });
191184

192185
return { key, name };
193186
};
@@ -204,15 +197,11 @@ export default (options) =>
204197
name = `___HTML_LOADER_REPLACEMENT_${replacements.size}___`;
205198
replacements.set(key, name);
206199

207-
result.messages.push({
208-
type: 'replacement',
209-
value: {
210-
type: 'source',
211-
hash,
212-
importName: importItem.name,
213-
replacementName: name,
214-
unquoted,
215-
},
200+
options.replacements.push({
201+
replacementName: name,
202+
importName: importItem.name,
203+
hash,
204+
unquoted,
216205
});
217206

218207
return { key, name };
@@ -257,15 +246,14 @@ export default (options) =>
257246
try {
258247
source = parseSrc(value);
259248
} catch (error) {
260-
result.messages.push({
261-
type: 'error',
262-
value: new HtmlSourceError(
249+
options.errors.push(
250+
new HtmlSourceError(
263251
`Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`,
264252
parser.startIndex,
265253
parser.endIndex,
266254
html
267-
),
268-
});
255+
)
256+
);
269257

270258
return;
271259
}
@@ -294,15 +282,14 @@ export default (options) =>
294282
try {
295283
sourceSet = parseSrcset(value);
296284
} catch (error) {
297-
result.messages.push({
298-
type: 'error',
299-
value: new HtmlSourceError(
285+
options.errors.push(
286+
new HtmlSourceError(
300287
`Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`,
301288
parser.startIndex,
302289
parser.endIndex,
303290
html
304-
),
305-
});
291+
)
292+
);
306293

307294
return;
308295
}
@@ -335,15 +322,14 @@ export default (options) =>
335322
try {
336323
source = parseSrc(value);
337324
} catch (error) {
338-
result.messages.push({
339-
type: 'error',
340-
value: new HtmlSourceError(
325+
options.errors.push(
326+
new HtmlSourceError(
341327
`Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`,
342328
parser.startIndex,
343329
parser.endIndex,
344330
html
345-
),
346-
});
331+
)
332+
);
347333

348334
return;
349335
}
@@ -370,10 +356,7 @@ export default (options) =>
370356
this.attributesMeta = {};
371357
},
372358
onerror(error) {
373-
result.messages.push({
374-
type: 'error',
375-
value: error,
376-
});
359+
options.errors.push(error);
377360
},
378361
},
379362
{

0 commit comments

Comments
 (0)
Please sign in to comment.