Skip to content

Commit 04d6a5c

Browse files
refactor: code
1 parent d17f912 commit 04d6a5c

18 files changed

+1263
-576
lines changed

src/Warning.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default class Warning extends Error {
2+
constructor(warning) {
3+
super(warning);
4+
const { text, line, column } = warning;
5+
this.name = 'Warning';
6+
this.message = `${this.name}\n\n`;
7+
8+
if (typeof line !== 'undefined') {
9+
this.message += `(${line}:${column}) `;
10+
}
11+
12+
this.message += `${text}`;
13+
14+
this.stack = false;
15+
}
16+
}

src/index.js

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,33 @@
1-
import { parse } from 'url';
2-
3-
import { compile } from 'es6-templates';
4-
import { minify } from 'html-minifier-terser';
5-
import { getOptions, isUrlRequest } from 'loader-utils';
1+
import { getOptions } from 'loader-utils';
62
import validateOptions from 'schema-utils';
73

4+
import { attributePlugin, interpolatePlugin, minimizerPlugin } from './plugins';
5+
import Warning from './Warning';
6+
87
import {
9-
getLinks,
10-
getUniqueIdent,
11-
replaceLinkWithIdent,
8+
pluginRunner,
129
isProductionMode,
1310
getImportCode,
1411
getExportCode,
1512
} from './utils';
1613

1714
import schema from './options.json';
1815

19-
export const raw = true;
20-
21-
export default function htmlLoader(source) {
16+
export default function htmlLoader(content) {
2217
const options = getOptions(this) || {};
2318

2419
validateOptions(schema, options, {
2520
name: 'HTML Loader',
2621
baseDataPath: 'options',
2722
});
2823

29-
let content = source.toString();
30-
31-
const links = getLinks(content, options.attributes);
32-
const replacers = new Map();
33-
34-
let offset = 0;
24+
const plugins = [];
3525

36-
for (const link of links) {
37-
if (link.value && isUrlRequest(link.value, options.root)) {
38-
const uri = parse(link.value);
26+
const attributes =
27+
typeof options.attributes === 'undefined' ? true : options.attributes;
3928

40-
if (typeof uri.hash !== 'undefined') {
41-
uri.hash = null;
42-
link.value = uri.format();
43-
link.length = link.value.length;
44-
}
45-
46-
const ident = getUniqueIdent(replacers);
47-
48-
replacers.set(ident, link.value);
49-
50-
content = replaceLinkWithIdent(content, link, ident, offset);
51-
52-
offset += ident.length - link.length;
53-
}
29+
if (attributes) {
30+
plugins.push(attributePlugin(options));
5431
}
5532

5633
const minimize =
@@ -59,46 +36,40 @@ export default function htmlLoader(source) {
5936
: options.minimize;
6037

6138
if (minimize) {
62-
const minimizeOptions =
63-
typeof minimize === 'boolean'
64-
? {
65-
collapseWhitespace: true,
66-
conservativeCollapse: true,
67-
keepClosingSlash: true,
68-
minifyCSS: true,
69-
minifyJS: true,
70-
removeAttributeQuotes: true,
71-
removeComments: true,
72-
removeScriptTypeAttributes: true,
73-
removeStyleLinkTypeAttributes: true,
74-
useShortDoctype: true,
75-
}
76-
: minimize;
77-
78-
try {
79-
content = minify(content, minimizeOptions);
80-
} catch (error) {
81-
this.emitError(error);
82-
}
39+
plugins.push(minimizerPlugin(options));
40+
}
41+
42+
const { interpolate } = options;
43+
44+
if (interpolate) {
45+
plugins.push(interpolatePlugin(options));
46+
}
47+
48+
const { html, messages, warnings, errors } = pluginRunner(plugins).process(
49+
content
50+
);
51+
52+
for (const warning of warnings) {
53+
this.emitWarning(new Warning(warning));
54+
}
55+
56+
for (const error of errors) {
57+
this.emitError(new Error(error));
8358
}
8459

85-
if (options.interpolate) {
86-
try {
87-
// Double escape quotes so that they are not unescaped completely in the template string
88-
content = compile(
89-
`\`${content.replace(/\\"/g, '\\\\"').replace(/\\'/g, "\\\\\\'")}\``
90-
).code;
91-
} catch (error) {
92-
this.emitError(error);
60+
const replacers = [];
9361

94-
content = JSON.stringify(content);
62+
for (const message of messages) {
63+
// eslint-disable-next-line default-case
64+
switch (message.type) {
65+
case 'replacer':
66+
replacers.push(message.value);
67+
break;
9568
}
96-
} else {
97-
content = JSON.stringify(content);
9869
}
9970

100-
const importCode = getImportCode(this, content, replacers, options);
101-
const exportCode = getExportCode(content, replacers, options);
71+
const importCode = getImportCode(this, html, replacers, options);
72+
const exportCode = getExportCode(html, replacers, options);
10273

10374
return `${importCode}${exportCode};`;
10475
}

0 commit comments

Comments
 (0)